<?php 
require_once('includes/config.php'); 
if(isset($_GET['tamp'])){
$tamp =$_GET['tamp'];
if(time() - $tamp > 3600) // one hour
{     
header("location:".$base_url."?OTP Link Expired");
exit();
}
}
if(empty($_GET['encrypt'])){
header("location:".$base_url."");
exit();
}	
$encrypt=$_GET['encrypt'];
$reset_qry= $con->query("SELECT user_id,otp,otp_verified FROM users WHERE md5(90*13+user_id)='".$encrypt."'");
if(mysqli_num_rows($reset_qry)>0){
$result=mysqli_fetch_array($reset_qry);
$otp_verified=$result['otp_verified'];   
$user_id=$result['user_id'];  
$otp=$result['otp'];   
}else{
header("location:".$base_url."");
exit();   
}    
$error="";
if(isset($_POST['submit'])){
    $getotp=$_POST['otp']; 
    if($otp==$getotp){
     $reset_qry= $con->query("UPDATE users SET otp_verified='1' WHERE `user_id` = '".$user_id."'");
      header('Location:'.$base_url.'reset-password.php?encrypt='.$encrypt.'&tamp='.$tamp.'');
    }else{
        $error="OTP not matching";
    }
}
if(isset($_POST['reset'])){
     $password=sha1($_POST['password']);
     $reset_qry= $con->query("UPDATE users SET password='$password' WHERE `user_id` = '".$user_id."'");
    if($reset_qry){
    header("location:".$base_url."");
    exit();     
    }else{
        $error="Error Occured";
    }
}
require_once('includes/pre-header.php'); 
?>
<body class="hold-transition login-page">
<div class="login-box">
  <div class="login-logo">
    <a href="index.php">
      <b>WORK</b>
      <small>Together</small>
    </a>
  </div>
  <!-- /.login-logo -->
  <div class="login-box-body">
      <p class="login-box-msg" style="color:red;"><?php echo $error;?></p>
    <div class="col-lg-12 message"></div>
    <form id="loginForm" method="POST">
        <?php if($otp_verified=='0'){?>
      <div class="form-group has-feedback">
        <input type="number" class="form-control" id="otp" name="otp" placeholder="Enter OTP" required/>
        <span class="glyphicon glyphicon-phone form-control-feedback"></span>
      </div>
      <div class="row">
        <div class="col-xs-8">
        </div>
        <!-- /.col -->
        <div class="col-xs-4">
          <input type="submit" name="submit" class="btn btn-primary btn-block btn-flat" id="sign_in" value="Submit" />
        </div>
        <!-- /.col -->
      </div>
      <?php } else {?>
      <div class="form-group has-feedback">
        <input type="password" class="form-control" id="password" name="password" placeholder="Enter Password" required/>
        <span class="glyphicon glyphicon-lock form-control-feedback"></span>
      </div>
      <div class="row">
        <div class="col-xs-6">
        </div>
        <!-- /.col -->
        <div class="col-xs-6">
          <input type="submit" name="reset" class="btn btn-primary btn-block btn-flat" id="reset" value="Reset Password" />
        </div>
        <!-- /.col -->
      </div>
      <?php }?>
    </form>    
  </div>
  <!-- /.login-box-body -->
</div>
<!-- /.login-box -->
<!-- jQuery 3 -->
<script src="jquery/jquery.min.js"></script>
<!-- Bootstrap 3.3.7 -->
<script src="bootstrap/dist/js/bootstrap.min.js"></script>
<!-- For data alerts -->
<script src="dist/js/simply-toast.min.js"></script>
<script src="js/common.js"></script>
</body>
</html>