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

class Forgot_password extends CI_Controller {
	

	private $cart_qty 	= '' ;	
	private $logged 	= 0  ;	
	private $user 		= '' ;
	private $new_pass   = '' ;
	
	

	public function __construct()
	{
		parent::__construct();
		
		$this->load->model('Db_model');
		
		$this->load->library(array('cart','encrypt'));
		
		$this->load->helper('basic_helper');
		
		$this->cart_qty = '('.$this->cart->total_items().')';
		
		if($this->session->userdata('shop_user2'))
		{
			$this->load->library('encrypt') ;
			$this->user = $this->encrypt->decode($this->session->userdata('shop_user2'));
			$this->logged = 1;
		}
		
	}
	
	
	public function index()
	{
		$this->home() ;
	}
	
	
	public function home()
	{
		$products = array() ;
		$a = rand(1,9);
		$b = rand(1,9);
		$capimage = '<b>'.$a.'&nbsp; + &nbsp;'.$b.'&nbsp; = &nbsp;</b>' ;
		$cap_result = $a + $b ;
		
		$data['result'] = array() ;
		$data['cart_qty'] = $this->cart_qty;
		$data['user'] = $this->user;
		$data['email'] = $this->user;
		
		$data['logged'] = $this->logged;
		$data['error'] = '';
		$data['suc_msg'] = '';
		$data['capimage'] = $capimage ;
		$data['c_val'] = $this->encrypt->encode($cap_result);
		
		$data['site'] = $this->Db_model->get_all('site',array()) ;
		
		$data['category']=$data['result'] = $this->Db_model->get_where_by_order('shop_category',array('gift'=>1),'order','asc') ;	
		$data['gift'] = $this->Db_model->get_where_by_order('shop_category',array('gift'=>2),'order','asc') ;
		
		$data['contactus'] = $this->Db_model->get_where_by_order('shop_contact',array('status'=>'1'),'id','desc') ;
		
		$data['offer'] = $this->Db_model->get_where_by_order_limit1('shop_product',array('active'=>'Y'),'order','asc') ;
		$data['category']= $this->Db_model->get_all_by_order('shop_category','order','asc') ;	
		$this->load->library('form_validation');
		
		//check if submit button clicked
		if($this->input->post('get_password'))
		{
	
			$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
			$this->form_validation->set_rules('captext', 'Security Text', 'trim|required');
	
			//if there are form validation errors
			if ($this->form_validation->run() === FALSE)
			{
				$this->_forgot_inc($data); 
			}
			
			else
			{ 
			
			
				/**
				*
				*	send temporary password to user
				*/
				
				//mail not sent
				if( ! $this->_send_temp_pass($this->input->post('email')))
				{
					
					
					
					$data['error'] = 'Problem on sending mail. Try again.';
					
					$this->_forgot_inc($data);	
				}
				
				//mail successfully sent
				else
				{
					//change password in database
					$this->Db_model->update('shop_checkoutusers',array('password'=>$this->new_pass),array('email'=>$this->input->post('email')));
					
					$data['suc_msg'] = 'Temporary password has been sent to your mail.<br /><br />Also check spam folder in case mail not delivered.' ;
					
					$this->_forgot_inc($data);	
					
					$this->output->set_header('refresh:5;url=Customer/login');  
					
				}

				
			}
		
		}//if($this->input->post('login'))
		
		else
		{
			/**
			*
			*	show login form at first time page loads,
			*	ie, before clicking submit button
			*
			*/
			
			$this->_forgot_inc($data);
		}
			

		
	}
	
	private function _forgot_inc($data=array())
	{
		
	
	
		$this->load->view('website/header',$data);
		//$this->load->view('website/topbar',$data);
		$this->load->view('website/menu',$data);
		$this->load->view('website/forgot_pass',$data);
		$this->load->view('website/footer');
		
		
	}
	
	public function email_exist($email='')
	{
		$stat = $this->Db_model->get_where('shop_checkoutusers',array('email'=>$email)) ;
		
		if($stat===FALSE)
		{
			$this->form_validation->set_message('email_exist','Email Id does not exist.') ;
			return FALSE;
		}
		else
		{
			return TRUE;
		}
	}
	
	private function _send_temp_pass($mailto='')
	{
		$this->new_pass = uniqid();
				
				
		$this->load->config('email');
        $this->load->library('email');
        
        $from = $this->config->item('smtp_user');
        $to = $this->input->post('to');
        $subject = $this->input->post('subject');
        $message = $this->input->post('message');

        $this->email->set_newline("\r\n");
        $this->email->from('info@keralapropertyfinder.com', 'Kerala Property Finder');
		//$this->email->fromname('Asian');
        $this->email->to($mailto);
        $this->email->subject('Forgot Password');
		
		
		
		
		
		$user_det = $this->Db_model->get_one('shop_checkoutusers',array('email'=>$mailto)) ;
		
		if($user_det!==FALSE)
		{
			$username = $user_det->username ;
			
				
			$msg='
					<div style="width:95%; padding:10px ; background-color:#E0E0E0;">
					
					<table width="100%"  border="0" cellpadding="0" cellspacing="0" align="center" bgcolor="#FBFBFB" 
					style="padding:20px;font-family:Arial, Helvetica, sans-serif; color:#2C2C2C; font-size:13px;">
					
					<tr><td colspan="2"><font style="font-size:18px;">FORGOT PASSWORD</font></td></tr>
					<tr height="20"><td colspan="2"></td></tr>
					<tr><td width="30%" height="30">
					
					<p>Dear Customer,</p>
					<p>&nbsp;</p>
					<p>Your password has been changed successfully.</p>
					<p>Username: <b>'.$mailto.'</b></p>
					<p>Temporary Password : <b>'.$this->new_pass.'</b> </p>
					<p>&nbsp;</p>
					<p>
					Thank You,<br /><br />
					Fadlor Holdings
					</p>
					
					</td></tr>
					
					<tr height="40"></tr>
					
					<tr><td colspan="2">
					
					<div style="font-family:Arial, Helvetica, sans-serif; font-size:10px; color:#575757;">
					<p>This is a system generated message, Please do not reply to this email.</p>
					<p></p>
					<p></p>
					</div>
					
					</td></tr>
					</table>
					
					</div>    
					';		
		}
		else
		{
			$msg = "<p>Dear Customer,</p>
					<p>Problem on retrieving your account details.</p>
					<p>Please contact your site administrator.</p>
					<p>
					<br /><br />
					Thank You,
					<br /><br />
					Edans e-mart
					</p>";
		}
		
		
		
		$this->email->message($msg);
		
		//email not sent
		if(! $this->email->send())
		{
			return false;			
		}
		else
		{
			return true;
		}
		
	}
	
		
	public function captcha_check($captext)
	{
		if( $captext==$this->encrypt->decode($this->input->post('c_val')) )
		{
			return true;
		}
		else
		{
			$this->form_validation->set_message('captcha_check','Capcha Error') ;
			return false;
		}
		
	}
	
	
	

	
}
