<?php 

 //FUNCION QUE ENCRIPTA UN VALOR
  function crypt_blowfish_bydinvaders($password, $digito = 7) {
    $set_salt = './1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
    $salt = sprintf('$2a$%02d$', $digito);
    for($i = 0; $i < 22; $i++)
    {
       $salt .= $set_salt[mt_rand(0, 22)];
    }
    return crypt($password, $salt);
  }
  
  include("header.php");
  include("conexionOdo.php");
  
  if(isset($_POST['cambiar']))
  {
  
    $s_tipoId = $_POST['tipoId'];
    $s_numId  = $_POST['numId'];
    $s_email  = $_POST['email'];
    
    $s_claveArea   = $_POST['password1'];
    //ENCRIPTA LA CLAVE
    $passwdNwArea = crypt_blowfish_bydinvaders($s_claveArea); 
    $query = "UPDATE SIME_BAS_N_USUARIOS SET claveUsuario='$passwdNwArea' WHERE tipoId='$s_tipoId' AND idUsuario=$s_numId";
   
    // echo $query;
    $result = mysql_query($query) or die('Modificacion de password fallida: ' . mysql_error()); 
    //echo "Password Modificado Exitosamente!";
  } 

  $token     = $_GET['token'];
  $idusuario = $_GET['idusuario'];
  
  $conexion  = new mysqli('localhost', 'healthdo_admin', 'softmakr2016', 'healthdo_sio');
  $sql       = "SELECT * FROM tblreseteopass WHERE token = '$token'";
  
  $resultado = $conexion->query($sql);
  if( $resultado->num_rows > 0 ){
    $usuario = $resultado->fetch_assoc();
    if( sha1($usuario['idusuario']) == $idusuario ){
        $s_idUsu =  $usuario['idusuario'];
        $query="SELECT tipoId, email FROM SIME_BAS_N_USUARIOS WHERE idUsuario=$s_idUsu";
        $result = mysql_query($query) or die('Consulta de tipo de documento: ' . mysql_error());
        $line = mysql_fetch_array($result, MYSQL_ASSOC);  
        $s_email  = $line['email'];
        $s_tipoId = $line['tipoId'];
?>
<html lang="en">
<head><meta http-equiv="Content-Type" content="text/html; charset=gb18030">
    
    <title>Cambio de password</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="http://netdna.bootstrapcdn.com/bootstrap/3.1.0/css/bootstrap.min.css" rel="stylesheet">
    <style type="text/css">
        
    </style>
    <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
    <script src="http://netdna.bootstrapcdn.com/bootstrap/3.1.0/js/bootstrap.min.js"></script>
</head>

<body>
 <div class="container">
  <div class="row">
    <div class="col-sm-12">
      <h1>Cambio de Password</h1>
    </div>
  </div>
  <div class="row">
    <div class="col-sm-6 col-sm-offset-3">
      <p class="text-center">Por favor ingrese el nuevo Password</p>
      <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
        <input type="password" class="input-lg form-control" name="password1" id="password1" required placeholder="Nuevo Password" autocomplete="on">
        <div class="row">
          <div class="col-sm-6" align="left">
           <span id="8char" class="glyphicon glyphicon-remove" style="color:#FF0004;"></span> M&iacute;nimo de 8 Caracteres<br>
           <span id="ucase" class="glyphicon glyphicon-remove" style="color:#FF0004;"></span> Una Letra May&uacute;scula
          </div>
          <div class="col-sm-6" align="left">
           <span id="lcase" class="glyphicon glyphicon-remove" style="color:#FF0004;"></span> Una Letra Min&uacute;scula<br>
           <span id="num" class="glyphicon glyphicon-remove" style="color:#FF0004;"></span> Un N&uacute;mero
          </div>
        </div>
        <input type="password" class="input-lg form-control" name="password2" id="password2" required placeholder="Repita Password" autocomplete="off">
        <div class="row">
           <div class="col-sm-12" align="left">
             <span id="pwmatch" class="glyphicon glyphicon-remove" style="color:#FF0004;"></span> Los Password Coinciden
           </div>
        </div>
        <input type="submit" class="col-xs-12 btn btn-primary btn-load btn-lg" data-loading-text="Modificando Password..." value="Cambio de Password" id="cambiar" name="cambiar">
        
        <input type="hidden" name="tipoId" id="tipoId" value=<?=$s_tipoId?>>
        <input type="hidden" name="numId" id="numId" value=<?=$s_idUsu?>>
        <input type="hidden" name="email" id="email" value=<?=$s_email?>>
        
      
      </form>
      
     </div><!--/col-sm-6-->
   </div><!--/row-->
 </div><!--/container-->
 
 <table align="center" width="900">
   <tr>
      <td class='titulosTabla'> <?=$mensaje?> </td>
   </tr>
   <tr>  
     <td align="center"> 
      <a href=index.php><img src="../health/imagenes/btnVol.jpg"></a>
     </td>         
   </tr>  
 </table>
 
<?php 
 }
else{
  $mensaje="ESTA SOLICITUD YA EXPIRO, SOLICITE UNA NUEVAMENTE";
  //header('Location:index.html'); 
  }
}
else{ 
   $mensaje="PASSWORD MODIFICADO EXITOSAMENTE"; 
//header('Location:index.html');

 
 //echo $mensaje;
?>

<table align="center" width="900">
   <tr>
      <td class='titulosTabla'> <?=$mensaje?> </td>
   </tr>
   <tr>  
     <td align="center"> 
      <a href=index.php><img src="imagenes/btnVol.jpg"></a>
     </td>         
   </tr>  
 </table>

<?php
}
?>

<script type="text/javascript">
$("input[type=password]").keyup(function(){
    var ucase = new RegExp("[A-Z]+");
	var lcase = new RegExp("[a-z]+");
	var num = new RegExp("[0-9]+");
	var i = 0;
	
	if($("#password1").val().length >= 8){
		$("#8char").removeClass("glyphicon-remove");
		$("#8char").addClass("glyphicon-ok");
		$("#8char").css("color","#00A41E");
		i=i+1;
	}else{
		$("#8char").removeClass("glyphicon-ok");
		$("#8char").addClass("glyphicon-remove");
		$("#8char").css("color","#FF0004");
	}
	
	if(ucase.test($("#password1").val())){
		$("#ucase").removeClass("glyphicon-remove");
		$("#ucase").addClass("glyphicon-ok");
		$("#ucase").css("color","#00A41E");
		i=i+1;
	}else{
		$("#ucase").removeClass("glyphicon-ok");
		$("#ucase").addClass("glyphicon-remove");
		$("#ucase").css("color","#FF0004");
	}
	
	if(lcase.test($("#password1").val())){
		$("#lcase").removeClass("glyphicon-remove");
		$("#lcase").addClass("glyphicon-ok");
		$("#lcase").css("color","#00A41E");
		i=i+1;
	}else{
		$("#lcase").removeClass("glyphicon-ok");
		$("#lcase").addClass("glyphicon-remove");
		$("#lcase").css("color","#FF0004");
	}
	
	if(num.test($("#password1").val())){
		$("#num").removeClass("glyphicon-remove");
		$("#num").addClass("glyphicon-ok");
		$("#num").css("color","#00A41E");
		i=i+1;
	}else{
		$("#num").removeClass("glyphicon-ok");
		$("#num").addClass("glyphicon-remove");
		$("#num").css("color","#FF0004");
	}
	
	if($("#password1").val() == $("#password2").val()){
		$("#pwmatch").removeClass("glyphicon-remove");
		$("#pwmatch").addClass("glyphicon-ok");
		$("#pwmatch").css("color","#00A41E");
		i=i+1;
		
	}else{
		$("#pwmatch").removeClass("glyphicon-ok");
		$("#pwmatch").addClass("glyphicon-remove");
		$("#pwmatch").css("color","#FF0004");
		
	}
	
	if (i == 5) {
           document.getElementById('cambiar').disabled = false;
        } else {
           document.getElementById('cambiar').disabled = true;
        }	
});
</script>
</body>
</html>