<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Login extends CI_Controller {
	
	public function __construct()
	{
	    parent::__construct();
	   
	}
	   
	public function admin_logout()
	{
		if(isset($_SESSION['admin_uid']))
		{		
			unset($_SESSION['admin_uid']);
			redirect(base_url('adminlogin'));
  		}
	}
	
	public function user_logout()
	{
		if(isset($_SESSION['userid']))
		{		
			unset($_SESSION['userid']);
			redirect(base_url());
		}
	}
	
	public function login_user(){
		
		$user_email = $this->input->post('email');
		$user_pwd = $this->input->post('pwd');
	    $user_remember = $this->input->post('u_remeber');
		
        $this->form_validation->set_rules('email','Email', 'trim|required');
        $this->form_validation->set_rules('pwd','Password', 'trim|required|min_length[6]');	
		
		if($this->form_validation->run() == FALSE)
		{
			echo 0; 
		}
		else
		{
			$data =array(
                     'user_email' =>$this->input->post('email'),
					 'user_password' =>md5($this->input->post('pwd')),
					);
					 
			if(!empty($data)){
 
				$query=$this->Data_access_model->select_data('*','hs_user',$data);
				 
				if($query->num_rows() > 0){
					$user=$query->row();
					if($user->user_status == 1){
						//user_type =1 for admin
						if($user->user_type == 1){
						 
							$_SESSION['admin_uid'] = $user->user_id;
							
							if($user_remember == 1){
								// set cookies 
								$email_cookie = array(
												'name'   => 'admin_email',
												'value'  => $user_email,
												'expire' => time() + 86400,
											   );
								$pass_cookie = array(
												'name'   => 'admin_pass',
												'value'  => $user_pwd,
												'expire' => time() + 86400,
											   );
									 
								set_cookie($email_cookie);
								set_cookie($pass_cookie);
							}else{
								$getadmin_email = get_cookie("admin_email");
								$getadmin_pass = get_cookie("admin_pass");
								if(!empty($getadmin_email) && !empty($getadmin_pass)){
										set_cookie("admin_email","");
										set_cookie("admin_pass","");
								}
							}
							echo 1;
									//user_type = 2 for user
						}else if($user->user_type == 2){
							$_SESSION['userid'] = $user->user_id; 
						 
							if($user_remember == 1){
								// set cookies 
								$uemail_cookie = array(
											'name'   => 'user_email',
											'value'  => $user_email,
											'expire' => time() + 86400,
											);
								$upass_cookie = array(
												'name'   => 'user_pass',
												'value'  => $user_pwd,
												'expire' => time() + 86400,
											   );
									 
								set_cookie($uemail_cookie);
								set_cookie($upass_cookie);										
							}else{
								$getuser_email = get_cookie("user_email");
								$getuser_pass = get_cookie("user_pass");
								if(!empty($getuser_email) && !empty($getuser_pass)){
									set_cookie("user_email","");
									set_cookie("user_pass","");
								}
							}
							echo 2;
						}	  
					}else{
						echo 3;
					}
				}else{
					echo 0;
				} 
			}
		} 
	}
	
	public function forgot_password(){
		
		$to_email = $this->input->post('useremail');	
	    if(!empty($to_email)){
	         $user_query = $this->Data_access_model->select_data('*','hs_user',array('user_email' =>$to_email));
		  
			if($user_query->num_rows() > 0){
				$user_detail = $user_query->row();
			  
				$varification_code = md5(substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 8));
				
				$message_link = '<a href=" '. base_url("login/verify_code/").''.$varification_code.' "> '.$this->hs->getwebsettings('forgotpwd_link').' </a>';
				
				
				$data =array('passreset_code'=>$varification_code);
				$this->Data_access_model->update_data('hs_user',array('user_email'=>$to_email),$data);
				if($this->db->affected_rows() > 0){
					   
					$email_content = $this->hs->getwebsettings('forgotpwd_template');  
					$email_content = str_replace("[username]",$user_detail->user_name, $email_content);
					$email_content = str_replace("[link]", $message_link,$email_content);
					$email_content = str_replace("[break]", "<br>", $email_content);
					$data['emcontent'] = $email_content;
	 
					$email_template = $this->load->view('theme/email_template',$data,true);
					$subject = $this->hs->getwebsettings('forgotpwd_mailsubject');;
					//sending mail
					$response = $this->hs->send_mail($to_email,$subject,$email_template);
				   
					if($response > 0){ 
						echo 1;
					}else{ 
						echo 2;
					} 
				}
		   }else{
				echo 0; 
		   }
	   }
    }
    
    
    public function verify_code($verification_code =''){
        
        if(!empty($verification_code)){
        
            $varify_query = $this->Data_access_model->select_data('user_id,passreset_code','hs_user',array('passreset_code' =>$verification_code));
            if($varify_query->num_rows() > 0){
				$check_code = $varify_query->row();
				if($check_code->passreset_code === $verification_code){
					redirect(base_url().'?resetcode='.$verification_code);
				}else{
					redirect(base_url()); 
				}
            }else{
				redirect(base_url()); 
            }
            
        }else{
            
           	redirect(base_url());
        }
     }
    
    
    public function reset_password(){
			 
		$this->form_validation->set_rules('verify_code','Reset Code', 'trim|required'); 
		$this->form_validation->set_rules('u_newpass','Password', 'trim|required');
		$this->form_validation->set_rules('u_confpass','New Password', 'trim|required'); 
         
		if ($this->form_validation->run() == FALSE)
		{
			echo 0; 
		
		}else{
				 
			$ureset_code = $this->input->post('verify_code');
			$unew_pwd = md5($this->input->post('u_newpass'));
			$uconf_pwd = md5($this->input->post('u_confpass'));
	
			$query = $this->Data_access_model->select_data('user_id,passreset_code','hs_user',array('passreset_code' =>$ureset_code));
			if($query->num_rows() > 0){
				$check_user =$query->row();
				$old_uid  = $check_user->user_id;
				if($check_user->passreset_code === $ureset_code){
					if($unew_pwd === $uconf_pwd){  
						$data = array(
									'user_password' => $unew_pwd,
									'passreset_code' =>null,
								);
						$this->Data_access_model->update_data('hs_user',array('user_id' =>$old_uid),$data);
						if($this->db->affected_rows() > 0){
							echo 1;
						}else{ 
							echo 3;
						}
					}else{
						echo 2;
					}
				}else{ 
					echo 3;
				}						
			}else{ 
				 echo 3;
			}
		}
	}
    
    public function set_gdprcookie()
	{
		$gdpr_cookie = array('name'   => 'gdpr_cookie',
							 'value'  => '1',
							 'expire' => (time() +3600 * 24 * 365),
							);
		set_cookie($gdpr_cookie);
		echo 1;
	}
    
}	
