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

class Customer extends CI_Controller {
	

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

	public function __construct()
	{
		parent::__construct();
		

		
		
		$this->load->model(array('admin_model','Db_model'));
		
		$this->load->helper('basic_helper');
		
		$this->load->library(array('cart','user_loginfunctions','form_validation','encrypt'));
		
		$ic=0;
		foreach ($this->cart->contents() as $items):
		$ic++;
		endforeach;
		$this->cart_qty = ''.$ic.'';
		//$this->cart_qty = ''.$this->cart->total_items().'';
		
		//if logged, redirect to ckeckout page
		if($this->user_loginfunctions->checkloggedin())
		{
			//redirect('Userpage/vieworders') ;
		}
		
		if($this->session->userdata('shop_user2'))
		{
			$this->load->library('encrypt') ;
			$this->user = $this->encrypt->decode($this->session->userdata('shop_user2'));
			$this->logged = 1;//echo $this->user;
		}
		
	if($_COOKIE['username_m'] !='' && $_COOKIE['password_m']!='' )
		{	
		if($this->user_loginfunctions->mobile_log($_COOKIE['username_m'],$_COOKIE['password_m']))
		{
			// echo $_COOKIE['username_m'];
			$this->load->library('encrypt') ;
			$this->user = $_COOKIE['username_m'];
			$this->logged = 1;
		}	
		}
		
		
		
		
	}
	
	
	
	
	private function _userid()
	{
		$user_det = $this->Db_model->get_one('shop_checkoutusers',array('email'=>$this->user));
		if($user_det!==FALSE)
			return $user_det->id ;
		else
			return '';
	}
	
	
	
	
	
	public function index()
	{
		$this->login() ;
	}
	
	// login page
	public function login()
	{
		
		
		$data['email'] = $this->user;
		  
		if($data['email']!='') { redirect('Customer/dashboard'); }

		//check if submit button clicked
		if($this->input->post('login'))
		{
	
			$this->form_validation->set_rules('username', 'Username', 'trim|required');
			$this->form_validation->set_rules('password', 'Password', 'trim|required');
	
			//if there are form validation errors
			if ($this->form_validation->run() === FALSE)
			{
				$this->_login_form(); 
			}
			
			else
			{
				
				
				
				
				
				//if user exist
				if( $this->user_loginfunctions->confirmuser( $this->input->post('username'),$this->input->post('password') ) )
				{
					$this->user_loginfunctions->createsessions( $this->input->post('username'),$this->input->post('password') );
					
					
					
					
					
					
				
			
					
					
					
					
					
					
					
					
					
								
					
if (!empty($_SERVER['HTTP_CLIENT_IP']))   
  {
    $ip_address = $_SERVER['HTTP_CLIENT_IP'];
  }
//whether ip is from proxy
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))  
  {
    $ip_address = $_SERVER['HTTP_X_FORWARDED_FOR'];
  }
//whether ip is from remote address
else
  {
    $ip_address = $_SERVER['REMOTE_ADDR'];
  }
 $ip_address;
					
					


$useragent=$_SERVER['HTTP_USER_AGENT'];

if(preg_match('/(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows (ce|phone)|xda|xiino/i',$useragent)||preg_match('/1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas\-|your|zeto|zte\-/i',substr($useragent,0,4)))
{
 $divice="M";
}
else
{
 $divice="W";
 		
			
		 
 
}



    date_default_timezone_set("Asia/Kolkata");
	 $date1 =  date("Y/m/d H:i a");


// then use the date functions, not the other way around


// Checking chode for androyid web view


	
$data = array(							  
'email' => $this->input->post('username',TRUE),
'ip_add_ress' => $ip_address,
'browser' => $useragent,
'browser_type' => $divice,
'log_date' => $date1,
'log_out' => 1,
);

$insert_id = $this->Db_model->insert('log_in',$data) ;


					
					
					
					
					
$isWebView = false;
if((strpos($_SERVER['HTTP_USER_AGENT'], 'Mobile/') !== false) && (strpos($_SERVER['HTTP_USER_AGENT'], 'Safari/') == false)) :
    $isWebView = true;
elseif(isset($_SERVER['HTTP_X_REQUESTED_WITH'])) :
    $isWebView = true;
endif;

if(!$isWebView) : 
    // Browserview
	
			
	
	
else :
    // Webview
		setcookie('username_m',$this->input->post('username'),time() + (60 * 60* 24 * 365 * 10),'/'); // 86400 = 1 day	
		setcookie('password_m',$this->input->post('password'),time() + (60 * 60* 24 * 365 * 10),'/'); // 86400 = 1 day
endif;				
					
					
					
					
					
					
					
					
					
					
					
					

					
					$dataret=array();
					$emaills = $this->input->post('username');
					$user_det = $this->Db_model->get_one('shop_checkoutusers',array('email'=>$emaills));
					/*if($this->Db_model->get_where_by_order('shop_shipdetails',array('user_id'=>$user_det->id),'order_date','desc'))
					{*/
					$dataret= $this->Db_model->get_where_by_order('shop_shipdetails',array('user_id'=>$user_det->id),'order_date','desc') ;
					
					$data['service'] = $this->Db_model->get_where_by_order('shop_pages',array('active'=>'Y','type'=>'service'),'order','asc') ;
					//}
					
					
					//$user_id = $this->_userid() ;echo $user_id;
				//	print_r($dataret);
					// redirect to home page
					if($this->cart_qty!=0)
					{
					redirect('Userpage/shipaddress');	
					}
					if($dataret!='')
					{
					redirect('Userpage/vieworders');		
					}
					else
					{
						redirect('Customer/dashboard');
					}
				}
				
				//if user is deactivated
				else if( $this->user_loginfunctions->is_deactive( $this->input->post('username'),$this->input->post('password') ) )
				{
				
					$this->user_loginfunctions->clearsessionscookies();
					
					$this->_login_form('Account temporarily suspended.');
				}
				
				else
				{
					$this->user_loginfunctions->clearsessionscookies();
					
					$this->_login_form('Incorrect Username or Password.');
				}
			}
		
		}//if($this->input->post('login'))
		
		else
		{
			/**
			*
			*	show login form at first time page loads,
			*	ie, before clicking submit button
			*
			*/
			
			$this->_login_form();
		}
			

		
	}
	
	
	
	function cartredi()
	{
		$data = array(
				'cart_qty'=>$this->cart_qty,	  
				'email' => $this->user,
				'logged' => $this->logged
				);
		
		
		//$data['result'] = $this->Db_model->get_where_by_order('shop_pages',array('type'=>'about','active'=>'Y'),'order','desc') ;
		
		$data['contactus'] = $this->Db_model->get_where_by_order('shop_contact',array('status'=>'1'),'id','desc') ;
		$data['service'] = $this->Db_model->get_where_by_order('shop_pages',array('active'=>'Y','type'=>'service'),'order','asc') ;
		$data['site'] = $this->Db_model->get_all('site',array()) ;
		//$data['banneresult'] = $this->Db_model->get_where_by_order('shop_banner',array('status'=>'1'),'id','desc') ;
		
		$offer_products = $this->Db_model->get_all_by_order('shop_category','order','asc') ;
		
		$data['contactus'] = $this->Db_model->get_where_by_order('shop_contact',array('status'=>'1'),'id','desc') ;
		
	//	$data['category'] = $this->Db_model->get_all_by_order('shop_category','order','asc') ;
		
		$data['result'] = $this->Db_model->get_where_by_order('shop_category',array(),'order','asc') ;	
		$data['gift'] = $this->Db_model->get_where_by_order('shop_category',array(),'order','asc') ;
		
		$data['thanks']="Thank you, You registration completed.<br> <a href='https://www.keralapropertyfinder.com/'>Click to continue shopping</a> ";
		
		if($offer_products!==FALSE)
		{
			$data['offer'] = $offer_products;
		}
		else
		{
			$data['offer'] = array() ;
		}
		
		
		
		
		
			
		$this->load->view('website/header',$data);
		$this->load->view('website/menu',$data);
		$this->load->view('website/cart_view',$data);
		$this->load->view('website/footer');
		/*echo '<pre>' ;
		print_r($this->cart->contents());
		echo '</pre>' ;*/
	}
	
	
	private function _login_form($error ='')
	{
		$data['result'] =array();
		$data = array(
				'cart_qty'=>$this->cart_qty,	  
				'error' => $error,
				'user' => $this->user,
				'email'=> $this->user,
				'logged' => $this->logged
				);

		$data['result'] = $this->Db_model->get_where_by_order('shop_category',array(),'order','asc') ;	
		$data['gift'] = $this->Db_model->get_where_by_order('shop_category',array(),'order','asc') ;
		$data['site'] = $this->Db_model->get_all('site',array()) ;
		$data['allproduct']= $this->Db_model->get_where_by_order('shop_product',array('active'=>'Y'),'order','asc') ;
			$data['service'] = $this->Db_model->get_where_by_order('shop_pages',array('active'=>'Y','type'=>'service'),'order','asc') ;
		$data['subcate'] = $this->Db_model->get_all_by_order('shop_subcategory','id','desc') ;
		$data['contactus'] = $this->Db_model->get_where_by_order('shop_contact',array('status'=>'1'),'id','desc') ;
		
		$data['category'] = $this->Db_model->get_all_by_order('shop_category','order','asc') ;
		
		
		
		$this->load->view('website/header',$data);
	
		$this->load->view('website/menu',$data);
		$this->load->view('website/login',$data);
		$this->load->view('website/footer');
		
	}
	
	public function subcategorys($searchtxt)
	{
		$data['result'] = array() ;
		$data['books'] = array() ;
		$data['category']=array() ;
		$data['cart_qty'] = $this->cart_qty ;
		$data['email'] = $this->user;
		$data['logged'] = $this->logged;
		$data['title']=$searchtxt;
		$data['categoryname']=array();
		$data['allproduct']= $this->Db_model->get_where_by_order('shop_product',array('active'=>'Y'),'order','asc') ;
		
		$data['contactus'] = $this->Db_model->get_where_by_order('shop_contact',array('status'=>'1'),'id','desc') ;
		
		$data['result'] = $this->Db_model->get_where_by_order('shop_category',array(),'order','asc') ;	
		$data['gift'] = $this->Db_model->get_where_by_order('shop_category',array(),'order','asc') ;
		
		$data['service'] = $this->Db_model->get_where_by_order('shop_pages',array('active'=>'Y','type'=>'service'),'order','asc') ;
		$data['subcate'] = $this->Db_model->get_all_by_order('shop_subcategory','id','desc') ;
		$data['category'] = $this->Db_model->get_all_by_order('shop_category','order','asc') ;
		$subcategory = $this->Db_model->get_like_only('shop_subcategory',$searchtxt);//print_r($category);
		$sub_id=$subcategory[0]['id'];
		//$data['result'] = $this->Db_model->get_where_by_order('shop_product',array('active'=>'Y'),'order','asc') ;
		$books = $this->Db_model->get_where_by_order('shop_product',array('subcategory_id'=>$sub_id,'active'=>'Y'),'order','asc') ;
		
		if($books!==FALSE)
		{
			$data['books'] = $books;
		}
				
		$this->load->view('website/base_header');
		$this->load->view('website/menu',$data);
		$this->load->view('website/item_list',$data);
		
		$this->load->view('website/footer');
	}
	
	
	
	public function categorys($searchtxt)
	{
		
		$data['result'] = array() ;
		$data['books'] = array() ;
		$data['category']=array() ;
		$data['cart_qty'] = $this->cart_qty ;
		$data['email'] = $this->user;
		$data['logged'] = $this->logged;
		$data['title']=$searchtxt;
		$data['categoryname'] = array() ;
	$data['service'] = $this->Db_model->get_where_by_order('shop_pages',array('active'=>'Y','type'=>'service'),'order','asc') ;
		//$data['category'] = get_category_by_id($category_id) ;
		$data['contactus'] = $this->Db_model->get_where_by_order('shop_contact',array('status'=>'1'),'id','desc') ;
$data['allproduct']= $this->Db_model->get_where_by_order('shop_product',array('active'=>'Y'),'order','asc') ;
		$data['result'] = $this->Db_model->get_where_by_order('shop_category',array(),'order','asc') ;	
		$data['gift'] = $this->Db_model->get_where_by_order('shop_category',array(),'order','asc') ;



		$data['subcate'] = $this->Db_model->get_all_by_order('shop_subcategory','id','desc') ;
		//$data['categoryname'] = $this->Db_model->get_one('shop_category',array('id'=>$category_id)) ;
		$category = $this->Db_model->get_like_only('shop_category',$searchtxt);//print_r($category);
		$catid=$category[0]['id'];
		//$data['result'] = $this->Db_model->get_where_by_order('shop_product',array('active'=>'Y'),'order','asc') ;
		//$category = $this->Db_model->get_like_only('shop_category',$searchtxt);
		$books = $this->Db_model->get_where_by_order('shop_product',array('category_id'=>$catid,'active'=>'Y'),'order','asc') ;
		if($books!==FALSE)
		{
			$data['books'] = $books;
		}
			
		$this->load->view('website/base_header');
		$this->load->view('website/menu',$data);
		$this->load->view('website/item_list',$data);
		
		$this->load->view('website/footer');
	}
	
	
	public function search()
	{
		$data['result'] = array() ;
		$data['books'] = array() ;
		$data['category']=array() ;
		$data['cart_qty'] = $this->cart_qty ;
		$data['email'] = $this->user;
		$data['logged'] = $this->logged;
		$data['title']='';
		$data['categoryname']=array();
		
		$data['service'] = $this->Db_model->get_where_by_order('shop_pages',array('active'=>'Y','type'=>'service'),'order','asc') ;
		//$data['category'] = get_category_by_id($category_id) ;
		$data['contactus'] = $this->Db_model->get_where_by_order('shop_contact',array('status'=>'1'),'id','desc') ;
		
		$data['allproduct']= $this->Db_model->get_where_by_order('shop_product',array('active'=>'Y'),'order','asc') ;
		$data['category']=$data['result'] = $this->Db_model->get_where_by_order('shop_category',array(),'order','asc') ;	
		$data['gift'] = $this->Db_model->get_where_by_order('shop_category',array(),'order','asc') ;
		
		
		//$data['category']=$data['result'] = $this->Db_model->get_all_by_order('shop_category','order','asc') ;
		$data['subcate'] = $this->Db_model->get_all_by_order('shop_subcategory','id','desc') ;
		//$data['result'] = $this->Db_model->get_where_by_order('shop_product',array('active'=>'Y'),'order','asc') ;
		//$books = $this->Db_model->get_where_by_order('shop_product',array('subcategory_id'=>$sub_id,'active'=>'Y'),'order','asc') ;
		
		$searchtxt = $this->input->post('searchtxt'); 
		$searchresult = $this->Db_model->get_where_like_only('shop_product',array('active'=>'Y'),$searchtxt);
		$searchcategory = $this->Db_model->get_like_only('shop_category',$searchtxt);
		
		if($searchresult!==FALSE)
		{
		$data['searchresult']=$searchresult;
		}
		else 
		{
		
		}
		
		if($searchresult!==FALSE)
		{
				
		$this->load->view('website/base_header');
		$this->load->view('website/menu',$data);
		$this->load->view('website/item_search',$data);
		$this->load->view('website/footer');
		}
		else if($searchcategory!==FALSE)
		{
		$this->categorys($searchtxt);
		}
		else
		{
		$this->subcategorys($searchtxt);
		}
		
	}
	
	public function register()
	{
		
		
		
		
		$a = rand(1,9);
		$b = rand(1,9);
		$capimage = '<b>'.$a.'&nbsp; + &nbsp;'.$b.'&nbsp; = &nbsp;</b>' ;
		$cap_result = $a + $b ;
		
		$data = array(
				'cart_qty'=>$this->cart_qty,	  
				'user' => $this->user,
				'email' => $this->user,
				'logged' => $this->logged,
				'capimage' => $capimage,
				'c_val' => $this->encrypt->encode($cap_result),
				'suc_msg' => '',
				'result' => '',
				'offer' => '',
				'error' => ''
				);
       
	   if($data['email']!='') { redirect('Customer/dashboard'); }
		
		$data['result'] = array() ;
		$data['contactus'] = $this->Db_model->get_where_by_order('shop_contact',array('status'=>'1'),'id','desc') ;	
		$data['service'] = $this->Db_model->get_where_by_order('shop_pages',array('active'=>'Y','type'=>'service'),'order','asc') ;
		$data['site'] = $this->Db_model->get_all('site',array()) ;
		
		
		$data['post_list'] = $this->Db_model->get_where_by_order('delivery',array('type'=>'delivery','active'=>'Y'),'title','asc') ;
		//$data['post_list'] = $this->Db_model->get_where_by_order('shop_pages',array('type'=>'about','active'=>'Y'),'title','asc') ;
		
		$data['category']=$data['result'] = $this->Db_model->get_where_by_order('shop_category',array(),'order','asc') ;	
		$data['gift'] = $this->Db_model->get_where_by_order('shop_category',array(),'order','asc') ;
		$data['allproduct']= $this->Db_model->get_where_by_order('shop_product',array('active'=>'Y'),'order','asc') ;
		
		$data['subcate'] = $this->Db_model->get_all_by_order('shop_subcategory','id','desc') ;
		
		
		
		
		//check if submit button clicked
		if($this->input->post('register'))
		{
			

			
		
	        $this->form_validation->set_rules('first_name', 'First Name', 'trim|required');
			$this->form_validation->set_rules('address', 'Address', 'trim|required');
			$this->form_validation->set_rules('mobile', 'Mobile Number', 'trim|required|min_length[10]|max_length[12]|numeric');
			$this->form_validation->set_rules('email', 'Email', 'trim|required|is_unique[shop_checkoutusers.email]|valid_email');
			$this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[6]|max_length[15]');
			$this->form_validation->set_rules('vpassword', 'Confirm Password', 'trim|required|matches[password]|min_length[6]|max_length[15]');
			$this->form_validation->set_rules('captext', 'Security Text', 'trim|required|callback_captcha_check');
			
			
			
			$this->form_validation->set_message('is_unique', 'This User ID already exists.');
			
		
			//if there are form validation errors
			if ($this->form_validation->run() === FALSE)
			{
				//echo $this->input->post('captext').'-'.$this->session->userdata('captext') ;
				$this->_register_form($data);
			}
			
			else
			{
				//do registration
				
				$data = array(
							  'first_name'=>$this->input->post('first_name',TRUE),
        					  'password'=>$this->input->post('password',TRUE),
							  'email'=>$this->input->post('email',TRUE),
							  'address'=>$this->input->post('address',TRUE),
							  'tel'=>$this->input->post('tel',TRUE),
							  'mobile'=>$this->input->post('mobile',TRUE),
							  'landmark'=>$this->input->post('noti',TRUE),
				
							  
							  'active'=>1
							  );
			
				$email = $this->input->post('email');
				$username = $this->input->post('email');
				$password = $this->input->post('password');
				
				$insert_id = $this->Db_model->insert('shop_checkoutusers',$data);
				//print_r($insert_id);
				if($insert_id!==FALSE)
				{
					//details inserted to database
					$alert="";
					
					$data['suc_msg'] = 'Thank you for registration '.$alert.'.<br />A message has been sent to your Email ID with login details
					<br />(Please check spam folder if our mail is not in your Inbox).' ;
					$data['error'] = '';
					$data['cart_qty'] =$this->cart_qty;
					$data['user'] = $this->user;
					$data['logged'] = $this->logged;
					$data['capimage'] = $capimage;
					$data['c_val'] = $this->encrypt->encode($cap_result);
					
					//send mail to registered user
					$mail_stat = $this->_send_logindetails($email,$username,$password) ;
					
					$this->_register_form($data);	
					
					$this->user_loginfunctions->createsessions( $this->input->post('email'),$this->input->post('password') );
					
				 
				 
				 
				 
				 
				 
				 
				 
				 
				 				
if (!empty($_SERVER['HTTP_CLIENT_IP']))   
  {
    $ip_address = $_SERVER['HTTP_CLIENT_IP'];
  }
//whether ip is from proxy
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))  
  {
    $ip_address = $_SERVER['HTTP_X_FORWARDED_FOR'];
  }
//whether ip is from remote address
else
  {
    $ip_address = $_SERVER['REMOTE_ADDR'];
  }
 $ip_address;
					
					


$useragent=$_SERVER['HTTP_USER_AGENT'];

if(preg_match('/(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows (ce|phone)|xda|xiino/i',$useragent)||preg_match('/1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas\-|your|zeto|zte\-/i',substr($useragent,0,4)))
{
 $divice="M";
}
else
{
 $divice="W";
}



    date_default_timezone_set("Asia/Kolkata");
	 $date1 =  date("Y/m/d H:i a");


// then use the date functions, not the other way around


// Checking chode for androyid web view


	
$data = array(							  
'email' => $this->input->post('email',TRUE),
'ip_add_ress' => $ip_address,
'browser' => $useragent,
'browser_type' => $divice,
'log_date' => $date1,
'log_out' => 2,
);

$insert_id = $this->Db_model->insert('log_in',$data) ;
				 
				 
				 
				 
				 
				 
$isWebView = false;
if((strpos($_SERVER['HTTP_USER_AGENT'], 'Mobile/') !== false) && (strpos($_SERVER['HTTP_USER_AGENT'], 'Safari/') == false)) :
    $isWebView = true;
elseif(isset($_SERVER['HTTP_X_REQUESTED_WITH'])) :
    $isWebView = true;
endif;

if(!$isWebView) : 
    // Browserview
	
		
	
	
else :
    // Webview
	
			setcookie('username_m',$this->input->post('email'),time() + (60 * 60* 24 * 365 * 10),'/'); // 86400 = 1 day	
			setcookie('password_m',$this->input->post('password'),time() + (60 * 60* 24 * 365 * 10),'/'); // 86400 = 1 day
endif;					 
				 
				 
				 
				 
				 
				 
				 
				 
				 
				 
				 
				 
				 
				 
					
					//$this->output->set_header('refresh:0;url=dashboard/?id=1');  
					
					redirect('Customer/dashboard/?id=1');
				}
				else
				{
					//not inserted
					$data['error'] = 'Problem on creating account<br />Try again.' ;
					$data['suc_msg'] = '';
					$data['cart_qty'] =$this->cart_qty;
					$data['user'] = $this->user;
					$data['logged'] = $this->logged;
					$data['capimage'] = $capimage;
					$data['c_val'] = $this->encrypt->encode($cap_result);
					
					$this->_register_form($data);	
				}
				
			}
			
			
		}
		else
		{
			/**
			*
			*	show login form at first time page loads,
			*	ie, before clicking submit button
			*
			*/
			
			$this->_register_form($data);			
		}
		
		
	}
	
	
	
	
	
	
	
	
	
	public function Profile()
	{
		
		$a = rand(1,9);
		$b = rand(1,9);
		$capimage = '<b>'.$a.'&nbsp; + &nbsp;'.$b.'&nbsp; = &nbsp;</b>' ;
		$cap_result = $a + $b ;
		
		$data = array(
				'cart_qty'=>$this->cart_qty,	  
				'user' => $this->user,
				'email' => $this->user,
				'logged' => $this->logged,
				'capimage' => $capimage,
				'c_val' => $this->encrypt->encode($cap_result),
				'suc_msg' => '',
				'result' => '',
				'offer' => '',
				'error' => ''
				);

		
		$data['result'] = array() ;
		$data['contactus'] = $this->Db_model->get_where_by_order('shop_contact',array('status'=>'1'),'id','desc') ;	
		$data['service'] = $this->Db_model->get_where_by_order('shop_pages',array('active'=>'Y','type'=>'service'),'order','asc') ;
		$data['site'] = $this->Db_model->get_all('site',array()) ;
		
		
		$data['post_list'] = $this->Db_model->get_where_by_order('delivery',array('type'=>'delivery','active'=>'Y'),'title','asc') ;
		//$data['post_list'] = $this->Db_model->get_where_by_order('shop_pages',array('type'=>'about','active'=>'Y'),'title','asc') ;
		
		$data['category']=$data['result'] = $this->Db_model->get_where_by_order('shop_category',array(),'order','asc') ;	
		$data['gift'] = $this->Db_model->get_where_by_order('shop_category',array(),'order','asc') ;
		$data['allproduct']= $this->Db_model->get_where_by_order('shop_product',array('active'=>'Y'),'order','asc') ;
		
		$data['subcate'] = $this->Db_model->get_all_by_order('shop_subcategory','id','desc') ;
		
		
		
		
		//check if submit button clicked
		if($this->input->post('update'))
		{
		
	        $this->form_validation->set_rules('first_name', 'First Name', 'trim|required');
			$this->form_validation->set_rules('address', 'Address', 'trim|required');
			$this->form_validation->set_rules('mobile', 'Mobile Number', 'trim|required|min_length[10]|max_length[12]|numeric');
			$this->form_validation->set_rules('email', 'Email', 'trim|required|is_unique[shop_checkoutusers.email]|valid_email');
			$this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[6]|max_length[15]');
			$this->form_validation->set_rules('vpassword', 'Confirm Password', 'trim|required|matches[password]|min_length[6]|max_length[15]');
			$this->form_validation->set_rules('captext', 'Security Text', 'trim|required|callback_captcha_check');
			
			
			
			$this->form_validation->set_message('is_unique', 'This User ID already exists.');
			
		
			//if there are form validation errors
			if ($this->form_validation->run() === FALSE)
			{
				//echo $this->input->post('captext').'-'.$this->session->userdata('captext') ;
				$this->_register_form($data);
			}
			
			else
			{
				//do registration
				
				$data = array(
							  'first_name'=>$this->input->post('first_name',TRUE),
        					  'password'=>$this->input->post('password',TRUE),
							  'email'=>$this->input->post('email',TRUE),
							  'address'=>$this->input->post('address',TRUE),
							  'tel'=>$this->input->post('tel',TRUE),
							  'mobile'=>$this->input->post('mobile',TRUE),
				
							  
							  'active'=>1
							  );
			
				echo $email = $this->input->post('email');
				$username = $this->input->post('email');
				$password = $this->input->post('password');
				
				$insert_id = $this->Db_model->insert('shop_checkoutusers',$data);
				//print_r($insert_id);
				if($insert_id!==FALSE)
				{
					//details inserted to database
					$alert="";
					
					$data['suc_msg'] = 'Thank you for registration '.$alert.'.<br />A message has been sent to your Email ID with login details
					<br />(Please check spam folder if our mail is not in your Inbox).' ;
					$data['error'] = '';
					$data['cart_qty'] =$this->cart_qty;
					$data['user'] = $this->user;
					$data['logged'] = $this->logged;
					$data['capimage'] = $capimage;
					$data['c_val'] = $this->encrypt->encode($cap_result);
					
					//send mail to registered user
					$mail_stat = $this->_send_logindetails($email,$username,$password) ;
					
					$this->_register_form($data);	
					
					$this->user_loginfunctions->createsessions( $this->input->post('email'),$this->input->post('password') );
					
					$this->output->set_header('refresh:0;url=dashboard');  
					
					//redirect('user/login');
				}
				else
				{
					//not inserted
					$data['error'] = 'Problem on creating account<br />Try again.' ;
					$data['suc_msg'] = '';
					$data['cart_qty'] =$this->cart_qty;
					$data['user'] = $this->user;
					$data['logged'] = $this->logged;
					$data['capimage'] = $capimage;
					$data['c_val'] = $this->encrypt->encode($cap_result);
					
					$this->_edit_profile($data);	
				}
				
			}
			
			
		}
		else
		{
			/**
			*
			*	show login form at first time page loads,
			*	ie, before clicking submit button
			*
			*/
			
			$this->_edit_profile($data);			
		}
		
		
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
		private function _edit_profile($data=array())
	{
		$this->load->view('website/header',$data);
		$this->load->view('website/menu',$data);
		$this->load->view('website/edit_profile',$data);
		$this->load->view('website/footer');
	}
	
	
	
	
	
	private function _register_form($data=array())
	{
		$this->load->view('website/header',$data);
		$this->load->view('website/menu',$data);
		$this->load->view('website/register',$data);
		$this->load->view('website/footer');
	}
	
	private function _send_logindetails($mailto='',$username='',$password='')
	{
			

		
		
		
		$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('Account Registration');

		$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;">Account Registration</font></td></tr>
				<tr height="20"><td colspan="2"></td></tr>
				<tr><td width="30%" height="30">
				
				<p>Dear Customer,</p>
				
				<p>Your account has been registered successfully.</p>
				<p>Login details are given below : </p>
				<p>Username: <b>'.$username.'</b></p>
				<p>Password : <b>'.$password.'</b></p>
				<p>
				Thanking You,<br /><br />
				Yours Sincerely <br /> Kerala Property Finder
				</p>
				
				</td></tr>
				
				<tr height="40"></tr>
				
				<tr><td colspan="2">
				
				<a href="">
				<img src=""  border="0" width="300" height="60" alt=""/>
				</a>
			
				<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>
				</div>
				
				</td></tr>
				</table>
				
				</div>    
				';
					
					
		$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;
		}
		
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
		public function dashboard()
	{
		$data['result'] = array() ;
		$data['books'] = array() ;
		$data['category']=array() ;
		$data['cart_qty'] = $this->cart_qty ;
		$data['email'] = $this->user;
		$data['logged'] = $this->logged;
		$data['title']=$searchtxt;
		$data['categoryname']=array();
		
		
		if($data['email']=='') { redirect('Customer/register'); }
		
		
		
		$data['allproduct']= $this->Db_model->get_where_by_order('shop_product',array('active'=>'Y'),'order','asc') ;
		
		$data['contactus'] = $this->Db_model->get_where_by_order('shop_contact',array('status'=>'1'),'id','desc') ;
		
		$data['result'] = $this->Db_model->get_where_by_order('shop_category',array(),'order','asc') ;	
		$data['gift'] = $this->Db_model->get_where_by_order('shop_category',array(),'order','asc') ;
		
		$data['service'] = $this->Db_model->get_where_by_order('shop_pages',array('active'=>'Y','type'=>'service'),'order','asc') ;
		$data['subcate'] = $this->Db_model->get_all_by_order('shop_subcategory','id','desc') ;
		$data['category'] = $this->Db_model->get_all_by_order('shop_category','order','asc') ;
		$subcategory = $this->Db_model->get_like_only('shop_subcategory',$searchtxt);//print_r($category);
		$sub_id=$subcategory[0]['id'];
		//$data['result'] = $this->Db_model->get_where_by_order('shop_product',array('active'=>'Y'),'order','asc') ;
		$books = $this->Db_model->get_where_by_order('shop_product',array('subcategory_id'=>$sub_id,'active'=>'Y'),'order','asc') ;
		
		if($books!==FALSE)
		{
			$data['books'] = $books;
		}
				
		$this->load->view('website/header');
		$this->load->view('website/menu',$data);
	   $this->load->view('website/dash',$data);
		
		$this->load->view('website/footer');
	}
	
	
	
	
	
	
	
	

	
}
