<?php 
  require_once('../check.php'); 

function check_password($s) 
 {
     $k=0;
     $caps=strlen(preg_replace('![^A-Z]+!', '', $s)); if($caps>0) { $k++; }
     $lower=strlen(preg_replace('![^a-z]+!', '', $s)); if($lower>0) { $k++; }
     $number=strlen(preg_replace('![^0-9]+!', '', $s)); if($number>0) { $k++; }
     $schar = preg_match('/[^a-zA-Z\d]/', $s); if($schar>0) { $k++; }
     $response = array('uppercase '=>$caps, 'lowercase'=>$lower,'numbers'=>$number, 'special char'=>$schar, 'status'=>$k);
     return $response;
 }

  $msg='<span class="error"><i class="fa fa-times"></i> ERROR: access denied</span>';
  if($access=='Yes')
  {
	$a=trim($_POST['a']);
	$b=trim($_POST['b']);
	$c=trim($_POST['c']);
	$sts='true'; $msg='';
	$check= check_password($b);
	if(strlen($b)<8) { $sts='false'; $msg='<span class="error"><i class="fa fa-times"></i> ERROR: please enter atleast 8 char password..!</span> <script> $("#new_password").addClass("error"); </script>'; }
	if($b!=$c && strlen($b)>7) { $sts='false'; $msg='<span class="error"><i class="fa fa-times"></i> ERROR: password do not match</span> <script> $("#r_password").addClass("error"); </script>'; }
	if($check['status']<3 && strlen($b)>7 && $sts=='true') 
	{
	   $sts='false';
	   $msg.='<script> $("#new_password").addClass("error"); </script>';
	   foreach($check as $key => $value)
	    {  $sts='false';
	       if($key!=='status') 
	        {
	           $passerror='<br><span class="error">'.$key.' => '.$value.' <i class="fa fa-times"></i></span>';
	           if($value>0) { $passerror='<br><span class="success">'.$key.' => '.$value.' <i class="fa fa-check"></i></span>'; }
	           $msg.=$passerror;
	         }
	    }
	}
 	if($sts=='true')
 	 {
     $userData = $getData->runQuery("SELECT * FROM `user_login` WHERE `user_pw`='".md5($a)."' ORDER BY `user_id` DESC limit 1");
	 if(($userData[0]['user_id'])>0) 
	 {
	   $updateData = $db_handle->runQuery("UPDATE `user_login` SET `user_pw` = '".md5($b)."' WHERE `user_id` = '".$user_id."' AND `login_key`='".$login_key."' LIMIT 1");
	   $msg='<span class="success"><i class="fa fa-check"></i> Password updated successfully.</span> <script> $("#new_password").val(""); $("#c_password").val("");$("#r_password").val(""); </script>';
	  }
	 else { $msg='<span class="error"><i class="fa fa-times"></i> ERROR: Current Password Not Match..!</span>'; }
 }
} 
print $msg;
?>