<?php
include("core/init.php");
include("session.php");






/****************Change Password Process Starts Here**********************/
if(Input::exists())
{


  if(Input::getInput("new_password")=="" || Input::getInput("retype_password")=="" || Input::getInput("old_password")=="")
  {
    Session::flash("message",'<div class="alert-error" style="padding: 5px;">All The Fields Required.</div><Br>');
    Redirect::to("updatePassword");
  }

  else if(Input::getInput("new_password")!=Input::getInput("retype_password"))
  {

    Session::flash("message",'<div class="alert-error" style="padding: 5px;">New Password Do Not Match.</div><Br>');
    Redirect::to("updatePassword");



  }

  else if(Db::getInstance()->num_rows("select id from admin where password='".Hash::make(INPUT::getInput("old_password"),Config::get("salt/adminSalt"))."' AND id='1'")>0)
  {
    Db::getInstance()->update("admin",
        array("password"=>Hash::make(INPUT::getInput("new_password"),Config::get("salt/adminSalt"))),
        array("id"=>1));
    Session::flash("message",'<div class="alert-success" style="padding: 5px;">Password UPdated.</div><Br>');
    Redirect::to("updatePassword");
  }


  else
  {
    Session::flash("message",'<div class="alert-error" style="padding: 5px;">Wrong Old Password.</div><Br>');
    Redirect::to("updatePassword");
  }

}

/****************Change Password Process Ends Here**********************/



?><!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>Change Password - <?php echo Config::get("project/site_title"); ?></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">
  <link rel="shortcut icon" href="dist/img/admin-panel.png">
  <!-- 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">
  <!-- Theme style -->
  <link rel="stylesheet" href="plugins/datatables/dataTables.bootstrap.css">


  <link rel="stylesheet" href="dist/css/AdminLTE.min.css">
  <!-- AdminLTE Skins. Choose a skin from the css/skins
       folder instead of downloading all of them to reduce the load. -->
  <link rel="stylesheet" href="dist/css/skins/_all-skins.min.css">

  <!-- 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>
    #result{
      margin-left:5px;
    }

    .short{
      color:#FF0000;
    }

    .weak{
      color:#E66C2C;
    }

    .good{
      color:#2D98F3;
    }

    .strong{
      color:#006400;
    }</Style>

</head>
<body class="hold-transition skin-blue sidebar-mini">
<div class="wrapper">




  <?php include("includes/header.php"); ?>






<?php include("includes/sidebar.php"); ?>


  <!-- Content Wrapper. Contains page content -->
  <div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <section class="content-header">
      <h1>
        Update Password
        <small></small>
      </h1>
      <ol class="breadcrumb">
        <li><a href="Dashboard"><i class="fa fa-dashboard"></i> Home</a></li>
        <li class="active">Update Password</li>
      </ol>
    </section>

    <!-- Main content -->
    <section class="content">

      <div class="row">


        <div class="col-md-12">

            <?php echo Session::flashDisplay("message"); ?>
            
          <!-- general form elements -->
          <div class="box box-primary">
            <div class="box-header with-border">

            </div>
            <!-- /.box-header -->
            <!-- form start -->
            <form role="form" action="updatePassword" method="post">
              <div class="box-body">
                <div class="form-group">
                  <label for="exampleInputPassword1">New Password</label>
                  <input type="password" class="form-control" name="new_password"  id="password"><span id="result"></span>
                </div>
                <div class="form-group">
                  <label for="exampleInputPassword1">Repeat Password</label>
                  <input type="password" class="form-control" name="retype_password">
                </div>

                <div class="form-group">
                  <label for="exampleInputEmail1">Old Password</label>
                  <input type="password" class="form-control"  name="old_password">
                </div>


              </div>
              <!-- /.box-body -->

              <div class="box-footer">
                <button type="submit" class="btn btn-primary">Submit</button>
              </div>
            </form>
          </div>
          <!-- /.box -->



        </div>
        <!-- /.col -->


      </div>





    </section>
    <!-- /.content -->
  </div>
  <!-- /.content-wrapper -->

 <?php include("includes/footer.php"); ?>


  <!-- Add the sidebar's background. This div must be placed
       immediately after the control sidebar -->
  <div class="control-sidebar-bg"></div>
</div>
<!-- ./wrapper -->




<!-- 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>
<!-- Slimscroll -->
<script src="plugins/slimScroll/jquery.slimscroll.min.js"></script>
<!-- FastClick -->
<script src="plugins/fastclick/fastclick.js"></script>
<!-- AdminLTE App -->
<script src="dist/js/app.min.js"></script>
<!-- AdminLTE for demo purposes -->
<script src="dist/js/demo.js"></script>

<script>




  $(document).ready(function()
  {
    /*
     assigning keyup event to password field
     so everytime user type code will execute
     */

    $('#password').keyup(function()
    {
      $('#result').html(checkStrength($('#password').val()))
    })

    /*
     checkStrength is function which will do the
     main password strength checking for us
     */

    function checkStrength(password)
    {
      //initial strength
      var strength = 0

      //if the password length is less than 6, return message.
      if (password.length < 6) {
        $('#result').removeClass()
        $('#result').addClass('short')
        return 'Too short'
      }

      //length is ok, lets continue.

      //if length is 8 characters or more, increase strength value
      if (password.length > 7) strength += 1

      //if password contains both lower and uppercase characters, increase strength value
      if (password.match(/([a-z].*[A-Z])|([A-Z].*[a-z])/))  strength += 1

      //if it has numbers and characters, increase strength value
      if (password.match(/([a-zA-Z])/) && password.match(/([0-9])/))  strength += 1

      //if it has one special character, increase strength value
      if (password.match(/([!,%,&,@,#,$,^,*,?,_,~])/))  strength += 1

      //if it has two special characters, increase strength value
      if (password.match(/(.*[!,%,&,@,#,$,^,*,?,_,~].*[!,%,&,@,#,$,^,*,?,_,~])/)) strength += 1

      //now we have calculated strength value, we can return messages

      //if value is less than 2
      if (strength < 2 )
      {
        $('#result').removeClass()
        $('#result').addClass('weak')
        return 'Weak'
      }
      else if (strength == 2 )
      {
        $('#result').removeClass()
        $('#result').addClass('good')
        return 'Good'
      }
      else
      {
        $('#result').removeClass()
        $('#result').addClass('strong')
        return 'Strong'
      }
    }
  });


</script>
</body>
</html>
