<?php
require_once 'db.php';
include_once "ewcfg12.php";
include_once ((EW_USE_ADODB) ? "adodb5/adodb.inc.php" : "ewmysql12.php");
include_once "phpfn12.php";

// grab recaptcha library
require_once "phpinc/recaptchalib.php";

$secretKey   = '6LfM3yUTAAAAAIN9C0U154G3M04Gxb76YdWtsI_4';
$response    = false;
$updateOk    = false;

$id    = trim(ew_RemoveXSS(ew_StripSlashes($_GET['id'])));
$email = trim(ew_RemoveXSS(ew_StripSlashes($_GET['email'])));
$hash  = trim(ew_RemoveXSS(ew_StripSlashes($_GET['hash'])));     


if((isset($_GET['id']) && !empty($_GET['id']) AND isset($_GET['email']) && !empty($_GET['email']) AND isset($_GET['hash']) && !empty($_GET['hash'])) || ($_SERVER['REQUEST_METHOD'] == 'POST')){    
    if($_SERVER['REQUEST_METHOD'] == 'POST'){
      //die("not redirect");
      if(!empty($_POST['g-recaptcha-response'])){
        $objRecaptcha = new ReCaptcha($secretKey);
        $response = $objRecaptcha->verifyResponse($_SERVER['REMOTE_ADDR'], $_POST['g-recaptcha-response']);
      }
      if(isset($response->success) && 1 == $response->success){
        //die("sdfsdf");
          //
          // GOOGLE RECAPTCHA VERIFICATION SUCCESS, PROCEED WITH LOGIC
          //          
          // Verify data
          if(isset($_POST['btn-sendemail'])) {
              $upass  = trim(ew_RemoveXSS(ew_StripSlashes($_POST['newpassword'])));
              $upass2 = trim(ew_RemoveXSS(ew_StripSlashes($_POST['newpasswordconfirm'])));              
              $id     = trim(ew_RemoveXSS(ew_StripSlashes($_POST['id'])));
              $email  = trim(ew_RemoveXSS(ew_StripSlashes($_POST['email'])));
              $hash   = trim(ew_RemoveXSS(ew_StripSlashes($_POST['hash'])));     
              
              if($upass=="") {
                 $errorMsg = "Kata laluan mesti diisikan!";
              } else if ($upass !== $upass2){
                 $errorMsg = "Pengesahan kata laluan gagal.";
              } else {
                  try
                  {
                      $upass = password_hash($upass, PASSWORD_DEFAULT);
                    
                      $query = "UPDATE WEBUSER
                                   SET USERPASSWORD = :upin2,
                                       DATEMODIFIED = getdate()
                                 WHERE USERID           =:uid
                                   AND USEREMAIL        =:umail
                                   AND USERACTIVATIONPIN=:upin
                               ";
                      $stmt = $DB_con->prepare($query);
                      $stmt->bindparam(":uid",   $id);
                      $stmt->bindparam(":umail", $email);
                      $stmt->bindparam(":upin",  $hash);
                      $stmt->bindparam(":upin2", $upass);
                      if($stmt->execute()){
                          //$errorMsg = "Kata laluan telah ditukar.";
                          $updateOk = true;
                      } else {
                          $errorMsg = "Kata laluan tidak berjaya ditukar.";
                      }
                  }
                  catch(PDOException $e)
                  {
                       $errorMsg = $e->getMessage();
                  }
              } 
          }
          

          
      } else {
        $loginError = true;
        $errorMsg   = "Pengesahan Catcha gagal!";
      }
      
      if ($updateOk){
          $errors="<div class=\"alert alert-success alert-dismissable\" id=\"success-alert\"><button type=\"button\" class=\"close\" data-dismiss=\"alert\">x</button>Update Success</div>";
      } else {
          $errors="<div class=\"alert alert-danger alert-dismissable\" id=\"fail-alert\"><button type=\"button\" class=\"close\" data-dismiss=\"alert\">x</button>Update Error! $errorMsg</div>";
      }      
      
    }
} else {
    // No parameter of id, email, and hash passed. Redirect to login page
    header("location: login.php");
}


    
?>

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <!-- This file has been downloaded from Bootsnipp.com. Enjoy! -->
    <title>i-Wira - Sistem Semakan Caruman Atas Talian</title>
    <!-- Tell the browser to be responsive to screen width -->
    <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
    <!-- Bootstrap 3.3.6 -->
    <link rel="stylesheet" href="bootstrap/css/bootstrap.min.css">
    <!-- Font Awesome -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.5.0/css/font-awesome.min.css">
    <!-- Ionicons -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/ionicons/2.0.1/css/ionicons.min.css">
    <link rel="stylesheet" href="dist/css/AdminLTE.min.css">
    <link rel="stylesheet" href="dist/css/skins/skin-blue.min.css">
    <!-- Google reCAPTCHA API -->
    <script src='https://www.google.com/recaptcha/api.js'></script>
    <!--<script src="https://www.google.com/recaptcha/api.js?onload=CaptchaCallback&render=explicit" async defer></script>-->
    

    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->  

    <style type="text/css">
        .example-modal .modal {
          position: relative;
          top: auto;
          bottom: auto;
          right: auto;
          left: auto;
          display: block;
          z-index: 1;
        }

        .example-modal .modal {
          background: transparent !important;
        }
        
        .footer {
          position: absolute;
          bottom: 0;
          width: 100%;
          /* Set the fixed height of the footer here */
          height: 40px;
          background-color: #f5f5f5;
        }       
        
        .container {
          width: auto;
          max-width: 680px;
          padding: 0 15px;
        }
        .container .text-muted {
          margin: 20px 0;
        }

        /* #########################################
           Resize Google ReCaptcha hack
           ######################################### */
        #rc-imageselect, .g-recaptcha {
              transform:scale(0.77);
              -webkit-transform:scale(0.77);
              transform-origin:0 0;
              -webkit-transform-origin:0 0;
        }        
    </style>

</head>
<body class="hold-transition skin-blue layout-top-nav">
<div class="wrapper">

    <div class="content-wrapper">
        <div class="container">

            <section class="content-header">
                <br>
                <img class="img-responsive hidden-lg hidden-md center-block" src="dist/img/LTATLogo3.png">
                <img class="img-responsive hidden-sm hidden-xs center-block" src="dist/img/logo_ltat_587x70.png"><br>
                <h1 class="text-center">      
                i-Wira<br>      
                <small>Sistem Semakan Caruman Atas Talian</small>
                </h1>
            </section>          
        
        
            <section class="content">
                <div class="row">                    
                    <div class="col-md-12">
                    
                      <div class="nav-tabs-custom">
                        <ul class="nav nav-tabs">
                          <li class="active"><a href="#settings" data-toggle="tab">Change Password</a></li>
                        </ul>
                        <div class="tab-content">
                          <div class="active tab-pane" id="settings">                          
                            <form class="form-horizontal" id="profile-form" action="<?php echo htmlspecialchars(ew_CurrentPage()) ?>" data-toggle="validator" role="form" method="POST" remote="true">
                              <div id="error">
                                    <!-- error will be showen here ! -->  
                                <?php echo $errors; ?>                            
                              </div>                            
                              <input type="hidden" name="id"    value="<?php echo $id ?>">
                              <input type="hidden" name="email" value="<?php echo $email ?>">
                              <input type="hidden" name="hash"  value="<?php echo $hash ?>">
                            
                              <div class="form-group">
                                <label for="armyno" class="col-sm-4 control-label">No Tentera</label>

                                <div class="col-sm-8">
                                  <input type="text" name="armyno" class="form-control" id="armyno" placeholder="No Tentera" value="<?php echo $armyno;?>">
                                </div>
                              </div>
                              <div class="form-group">
                                <label for="newpassword" class="col-sm-4 control-label">Kata Laluan Baru</label>

                                <div class="col-sm-8">
                                  <input type="password" name="newpassword" data-minlength="6" class="form-control" id="newpassword" placeholder="Kata Laluan Baru" pattern="(?=^.{6,}$)((?=.*\d)|(?=.*\W+))(?![.\n])(?=.*[A-Z])(?=.*[a-z]).*$" data-error="Sekurang-kurangnya 8 aksara atau lebih dan perlu memenuhi 4 jenis aksara berikut: Huruf besar [ABC], huruf kecil [abc], nombor [123] dan simbol [@#$]" required>
                                  <span class="help-block with-errors"></span>
                                </div>
                              </div>
                              <div class="form-group">
                                <label for="newpasswordconfirm" class="col-sm-4 control-label">Sah Kata Laluan Baru</label>

                                <div class="col-sm-8">
                                  <input type="password" name="newpasswordconfirm" data-minlength="6" data-match="#newpassword" class="form-control" id="newpasswordconfirm" placeholder="Sah Kata Laluan Baru"  data-error="Kata laluan tidak sama!" required>
                                  <span class="help-block with-errors"></span>
                                  
                                </div>
                              </div>
                              <!-- Google Recaptcha here -->
                              <div class="col-sm-offset-4 g-recaptcha" data-size="normal" data-sitekey="6LfM3yUTAAAAAJBkHNnZNKtx0xARtQ3N-FwwX_HQ"></div>
                              <p>
                              
                              <div class="form-group">
                                <div class="col-sm-offset-4 col-sm-8">
                                  <button type="submit" name="btn-sendemail" class="btn btn-danger">Hantar</button>
                                  <a href="login.php" class="btn btn-info">Kembali ke Login</a>
                                </div>
                              </div>
                            </form>
                            
                          </div>
                          <!-- /.tab-pane -->
                        </div>
                        <!-- /.tab-content -->
                      </div>
                      <!-- /.nav-tabs-custom -->
                      
                      
                      
                    </div>
                    <!-- /.col -->
                </div>
                <!-- /.row -->
            </section>
            
        </div>
        <!-- /.container -->
    </div>
    <!-- /.content-wrapper -->

    <footer class="footer">
        <p>
        <div class="container">
          <div class="pull-right hidden-xs">
            <b>Versi</b> 1.1.0
          </div>
          <div class="pull-left"><strong>&copy; 2017 <a href="http://www.ltat.org.my">Lembaga Tabung Angkatan Tentera</a>.</strong></div><div class="pull-left hidden-xs">&nbsp;Hak Cipta Terpelihara.</div>
        </div>

    </footer>
</div>
  
<!-- jQuery 2.2.3 -->
<script src="plugins/jQuery/jquery-2.2.3.min.js"></script>
<!-- Bootstrap 3.3.6 -->
<script src="bootstrap/js/bootstrap.min.js"></script>
<!-- InputMask -->
<script src="plugins/input-mask/jquery.inputmask.js"></script>
<script src="plugins/input-mask/jquery.inputmask.date.extensions.js"></script>
<script src="plugins/input-mask/jquery.inputmask.extensions.js"></script>
<!-- SlimScroll -->
<script src="plugins/slimScroll/jquery.slimscroll.min.js"></script>
<!-- FastClick -->
<script src="plugins/fastclick/fastclick.js"></script>
<!-- Validator.js -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/1000hz-bootstrap-validator/0.11.9/validator.min.js"></script>
<!-- AdminLTE App -->
<script src="dist/js/app.min.js"></script>
<script>
    //var CaptchaCallback = function() {
    //  $('.g-recaptcha').each(function(index, el) {
    //    grecaptcha.render(el, {'sitekey' : '6LfM3yUTAAAAAJBkHNnZNKtx0xARtQ3N-FwwX_HQ'});
    //  });
    //};
</script>
<script>
    // close the success message
    $(document).ready (function(){
        $("#success-alert").fadeTo(6000, 1000).slideUp(1000, function(){
            $("#success-alert").alert('close');
        });
        $("#fail-alert").fadeTo(6000, 1000).slideUp(1000, function(){
            $("#fail-alert").alert('close');
        });
    });
</script>

</body>
</html>
