<?php
session_start();
require_once('config/config.php');
require_once('class/functions.php');
dbconnect();

if (!is_user()) {
    redirect(LOGIN);
}

$page = "Transfer to other Bank OTP";
$geo = ip2location($_SERVER['REMOTE_ADDR']);
$country = $geo->country;
$city = $geo->city;
visit_log($_SERVER['REMOTE_ADDR'], $country, $city, $_SERVER['HTTP_USER_AGENT'], getBrowser($_SERVER['HTTP_USER_AGENT']), getOS($_SERVER['HTTP_USER_AGENT']), $page, $_SESSION['user_login']);


//Verify OTP
if (isset($_POST['authorize']) && $_POST['authorize'] == 'authorize')
{
    if ($_POST['otpCode'] != get_otp($_SESSION['user_login'])){ store_msg("Invalid OTP Code", "danger"); redirect(OTHERTRANSFER);
    set_otp($_SESSION['user_login'], '');}
    else{
    // clear OTP
    set_otp($_SESSION['user_login'], '');
    log_info($_SERVER['REMOTE_ADDR'], $_SESSION['user_login'], $_SESSION['user_login'], 'Successfully Entered Transfer to other Bank OTP');
    store_msg("Authorization Code Accepted", "success"); redirect(COMPLETED);
    }
}else{
//Set and Send OTP
$otp = getRandomString(6);
set_otp($_SESSION['user_login'], $otp);
$otpsms = inotp_sms();
$otpemail = inotp_email();
if (USE_SMS){
    //send OTP via SMS
    //send SMS
    $phone = clean_phone(user_phone($_SESSION['user_login']));
	$sms_cnt = ''.strtoupper($otpsms).' '.$otp.'';
    sms_scryba($phone, $sms_cnt);
}else{
    //send OTP via Email
    //send Email
    $message = "
    <html>
    <head>
    <title>".WEB_NAME."</title>
    </head>
    <body>
    <img src=".LOGO_URL." alt=".WEB_NAME."/><br><br>
    <p>".strtoupper($otpemail).":<br><br>
    OTP CODE: ".$otp." <br>
    </p><br><br><br>
    ===========
    <p>".WEB_NAME."</p>
    </body>
    </html>
    ";
    $email = get_email($_SESSION['user_login']);
    $subject = "AUTHORIZATION CODE";
    //send_email($fromname, $fromemail, $toemail, $subject, $message)
    send_email(WEB_NAME, WEB_EMAIL, $email, $subject, $message);
}
}

require_once('head.php'); 
?>

<body>
  <div class="app" id="app">

<!-- ############ LAYOUT START-->
  <!-- aside -->
  <?php require_once('aside.php'); ?>
  <!-- / aside -->



  <!-- content -->
  <div id="content" class="app-content box-shadow-z3" role="main">

    <?php require_once('header.php'); ?>

    <?php require_once('footer.php'); ?>

    <div ui-view class="app-body" id="view">
    <!-- ############ PAGE START-->

    <?php require_once('topbar.php'); ?>


    <?php
    $notify = get_alerts($_SESSION['user_login'], 'all');
    foreach($notify as $ntfy){
    echo "<div class=\"alert alert-warning\" role=\"alert\">
    <strong>".date('l jS F Y', strtotime($ntfy['alrt_date']))."</strong><br>".$ntfy['alrt_cnt']."
    </div>";
    }
    ?>

    <?php if (!empty($_SESSION['msg_text'])) {
    $msg = create_alert($_SESSION['msg_text'], $_SESSION['msg_type']);
    echo $msg;
    clear_msg(); }
    ?>


    <div class="padding">
      <div class="p-x-lg">
        <div class="row">
        <div class="col-lg-6 offset-lg-3">
          <div class="box">
            <div class="box-header">
              <h2>Authorize Transfer</h2>
              <small>Enter the code sent to your registered <?php if (USE_SMS){echo 'Phone';}else{echo 'Email';} ?>.</small>
            </div>
            <div class="box-divider m-a-0"></div>
            <div class="box-body p-v-md">
            <form id="Ownotp" method="post" action="" enctype="multipart/form-data" ui-jp="parsley">
              <div class="row row-sm">
                <div class="col-sm-12">
                    <div class="md-form-group float-label">
                      <input type="password" name="otpCode" class="md-input" data-parsley-error-message="Enter OTP Code" required>
                      <label>OTP Code</label>
                    </div>
                </div>
              </div>
              <div class="row row-sm">
                <div class="col-sm-12">
                    <button type="submit" name="authorize" value="authorize" class="btn rounded btn-outline main mainbd p-x-md">Authorize</button>
                </div>
              </div>
            </form>
            </div>
          </div>
        </div>
        </div>
      </div>
    </div>


    <!-- ############ PAGE END-->
    </div>



    </div>
<!-- ############ LAYOUT END-->

  </div>
<!-- build:js scripts/app.html.js -->
<!-- jQuery -->
  <script src="libs/jquery/jquery/dist/jquery.js"></script>

<!-- Bootstrap -->
  <script src="libs/jquery/tether/dist/js/tether.min.js"></script>
  <script src="libs/jquery/bootstrap/dist/js/bootstrap.js"></script>
  <script src="scripts/jquery.maskMoney.js"></script>
  <script>
    $(function() {
        $("#tfAmt").maskMoney();
    });
  </script>
<!-- core -->
  <script src="libs/jquery/underscore/underscore-min.js"></script>
  <script src="libs/jquery/jQuery-Storage-API/jquery.storageapi.min.js"></script>
  <script src="libs/jquery/PACE/pace.min.js"></script>

  <script src="scripts/config.lazyload.js"></script>

  <script src="scripts/palette.js"></script>
  <script src="scripts/ui-load.js"></script>
  <script src="scripts/ui-jp.js"></script>
  <script src="scripts/ui-include.js"></script>
  <script src="scripts/ui-device.js"></script>
  <script src="scripts/ui-form.js"></script>
  <script src="scripts/ui-nav.js"></script>
  <script src="scripts/ui-screenfull.js"></script>
  <script src="scripts/ui-scroll-to.js"></script>
  <script src="scripts/ui-toggle-class.js"></script>

  <script src="scripts/app.js"></script>

  <!-- ajax -->
  <script src="libs/jquery/jquery-pjax/jquery.pjax.js"></script>
  <script src="scripts/ajax.js"></script>
<!-- endbuild -->
</body>
</html>
