<?php session_start();
include("header.php");
include("session.php");
require_once('dbcon.php');
$id = $_SESSION['id'];
//generate otp
    $length=4;
    $code = '';
    $codes = array_merge(range(0, 9), range('A', 'Z'));

    for ($i = 0; $i < $length; $i++) {
        $code .= $codes[array_rand($codes)];
    }
    //hash
    $hashed = password_hash($code,PASSWORD_BCRYPT);
    // insert to db
    $query = mysql_query("INSERT INTO `customer_auths`(`otp`,`otp_status`,`customer_id`) VALUES ('$hashed',0,$id)") or die (mysql_error());
    //sms code
    $username = "demo1";
    $password = '$mob1d3m01$';
    $phone = 254708058225;//$_SESSION['phone'];
    $sms = urlencode("Your OTP code is $code");

    $parameters = "username=$username&password=$password&destination=$phone&message=$sms&source=MOBISKY";
    $url="http://www.mobisky.biz/api/sendsms2a.php?$parameters";  

    $ch = curl_init();

	// set URL and other appropriate options
    curl_setopt($ch, CURLOPT_URL, "$url");
    curl_setopt($ch, CURLOPT_HEADER, 0);

    // Pass it to the browser
    curl_exec($ch);

    // close cURL resource, and free up system resources
    curl_close($ch);
    
    //update otp status
    if(isset($_POST['otp']))
    {
        //check current auth
        $checkqry = mysql_query("SELECT * FROM `customer_auths` WHERE customer_id=$id AND `otp_status`=0") or die (mysql_error());
        $checkcount = mysql_num_rows($checkqry);
        if($checkcount)
        {
            $checkfetch = mysql_fetch_array($checkqry);
            if(password_verify($_POST['otp'], $checkfetch['otp']))
            {
                //update auth
                $authupdate=mysql_query("DELETE FROM `customer_auths` WHERE customer_id=$id") or die(mysql_error());
            	$query2 = mysql_query("update airtime_customer SET otp_status='1' where customer_id=$id") or die (mysql_error());
            	$_SESSION['otp_status'] = 1;
            	?>
            	
            	<script>
            		window.location="homepage.php";
            	</script>
            	<?php
            }
            else
            {
                echo "Invalid OTP";
            }
        }
        else
        {
            echo "Invalid OTP";
        }
    }
?>
<head>
<!-- Bootstrap -->
    <link href="vendors/bootstrap/dist/css/bootstrap.min.css" rel="stylesheet">
	</head>
	
<div class="container">
	 <div id="loginbox" style="display:block;margin-top:200px;" class="mainbox col-md-6 col-md-offset-3 col-sm-8 col-sm-offset-2">                    
            <div class="panel panel-info" >
                    <div class="panel-heading" style="background-color:#ea7d09;color:#fff;font-weight:bold;" >
                        <div class="panel-title">Verify OTP</div>
                        
                    </div>     

                    <div style="padding-top:30px" class="panel-body" >

                        <div style="display:none" id="login-alert" class="alert alert-danger col-sm-12"></div>
                            
                         <form  class="form-horizontal" role="form" method="post" action="">
                                    
                            <div style="margin-bottom: 25px" class="input-group">
                                        <span class="input-group-addon"><i class="glyphicon glyphicon-lock"></i></span>
                                        <input id="otp" type="text" class="form-control" name="otp" placeholder="Enter OTP" required>                                        
                            </div>
                                    
                                								
                                <hr/>
                                <div style="margin-top:10px;margin-bottom:20px;" class="form-group">
                                    <!-- Button -->
                                    <div class="col-sm-12 controls">
                                        <input type="submit" name="submit1"  value="Submit" id="btn-login"  class="btn btn-success pull-left"  />
    									<button id="lft" type="button" class="btn btn-error pull-right"><i class="icon-hand-right"></i><a href="otp.php">Resend</a></button>
                                    </div>
                                </div>

                            </form>     
	


                        </div>                     
                    </div>  
        </div>
</div>

   <!--?php include("footerlinks.php"); ?-->
	 
	  <script src="vendors/jquery/dist/jquery.min.js"></script>
    <!-- Bootstrap -->
    <script src="vendors/bootstrap/dist/js/bootstrap.min.js"></script>
    <?php
    exit();
    ?>