<?php
if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly.
}

class fbbttmMyaccount
{
	public function fbbttm_get_keyvalid($request=null)
	{

		global $fbbttm_helperobj;

		$message = array();

		$my_c = $fbbttm_helperobj->fbbttm_paramval("ukey" );
		$uid = $fbbttm_helperobj->fbbttm_paramval("uid" );

		$useractkey=base64_decode($my_c);
		$userid=base64_decode($uid);

		$user_data = get_user_by('id', $userid);

		if(empty($user_data)){

		  $message['showresetbox'] = 'No';
		  $message['errormessage'] = esc_html__('This key is invalid or has already been used. Please reset your password again if needed. ','foodboxbooker');

		}else{

		$user_id = $user_data->ID;
		$key = $user_data->user_activation_key;
		
		if(!empty($key))
		 {	
			if($user_id ==  $userid && $key == $useractkey)
			{
				$message['showresetbox'] = 'Yes';
				$message['fbbttm-userid'] = $user_id;
				$message['fbbttm-activation-key'] = $key;
			}
			else
			{
				$message['showresetbox'] = 'No';
				$message['errormessage'] = esc_html__('This key is invalid or has already been used. Please reset your password again if needed. ','foodboxbooker');
			}
		}
		else
		{
				$message['showresetbox'] = 'No';
				$message['errormessage'] = esc_html__('This key is invalid or has already been used. Please reset your password again if needed. ','foodboxbooker');
		}

		}

		return $message;
	}

	/**
	 * functon for reset password for user
	 */ 
	public function fbbttm_user_reset_password($userdata=null)
	{	

		$message = array();
		$error_msg= array();
		$fbbttmuserid = $fbbttmactivekey = $fbbttmpassword = $fbbttmpassword1 = '';

		if(isset($_POST['fbbttm-userid'])){
			$fbbttmuserid = sanitize_text_field(wp_unslash($_POST['fbbttm-userid']));
		}

		if(isset($_POST['fbbttm-activekey'])){
			$fbbttmactivekey = sanitize_text_field(wp_unslash($_POST['fbbttm-activekey']));
		}

		if(isset($_POST['fbbttm-password'])){
			$fbbttmpassword = sanitize_text_field(wp_unslash($_POST['fbbttm-password']));
		}

		if(isset($_POST['fbbttm-password-1'])){
			$fbbttmpassword1 = sanitize_text_field(wp_unslash($_POST['fbbttm-password-1']));
		}
		$noncename = '';
		if(isset($_POST['fbbttm_meta_box_noncename'])){
			$noncename = sanitize_text_field(wp_unslash($_POST['fbbttm_meta_box_noncename']));
		}

		if ( ! isset( $noncename ) || ! wp_verify_nonce( $noncename, 'fbbttm_meta_box_nonce' ) ) {
			  $error_msg['php-error'] = esc_html__('Sorry, Your request can not be processed due to security reason.','foodboxbooker'); 
					 
		 }

		if(empty($fbbttmpassword)){
	  	   $error_msg['fbbttm-password'] = esc_html__('Please enter your new password','foodboxbooker'); 
	  	}

	  	if(empty($fbbttmpassword1)){
	  	   $error_msg['fbbttm-password-1'] = esc_html__('Please enter confirm new password','foodboxbooker'); 
	  	}

	  	if(!empty($fbbttmpassword) && !empty($fbbttmpassword1)){

	  	  	if($fbbttmpassword != $fbbttmpassword1 ){

	  	  		$error_msg['fbbttm-password'] = esc_html__('Password mismatch.','foodboxbooker'); 
	  	  		$error_msg['fbbttm-password-1'] = esc_html__('Password mismatch.','foodboxbooker'); 

	  	  	}

	  	  }

	     $user_data = get_user_by('id', $fbbttmuserid);

		 if(empty($user_data)){

		    	$error_msg['fbbttm-common-error'] = esc_html__('User is not exist please reset your password again if needed.','foodboxbooker'); 
		  }
	     else
	       {
	      		$user_id = $user_data->ID;
				$key = $user_data->user_activation_key;
				if(!empty($key))
				{
					$res = hash_equals($key, $fbbttmactivekey);
					if($res == true){}
					else if($res == false){
					   $error_msg['fbbttm-common-error'] = esc_html__('This key is invalid or has already been used. Please reset your password again if needed. ','foodboxbooker');
					}
					if($user_id !=  $fbbttmuserid && $res == false)
					{
						$error_msg['fbbttm-common-error'] = esc_html__('This key is invalid or has already been used. Please reset your password again if needed. ','foodboxbooker');
					}
				}
	     	}
	  	   
	  	   if(!empty($fbbttmpassword))
		  	{
		  		$length = strlen ($fbbttmpassword);
		  		if($length < 5)	{
		  			$error_msg['fbbttm-password'] = esc_html__('Please enter password atleast five character','foodboxbooker'); 
		  		}
		  	}
 		  $message['error_message'] = $error_msg;
	  	  if(empty($error_msg)){
		  	    $pagelink = FBBTTM_MYACCOUNT_PAGE_LINK;
		  	  	wp_set_password( $fbbttmpassword, $user_id );
		  	  	$message['successmessage'] = esc_html__('Password reset successfull','foodboxbooker'); 
		  	  	$message['redirct_url'] = $pagelink; 
	  	   }
	  	  return $message;
	}
}
?>