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





/*

*

*    DON'T TRY TO EXTEND LIBRARY CLASSES

*

*/





class User_loginfunctions {

	

	

	

	/**

	*

	*   initialize variables

	*

	*/

	

	private $ci, $sesdata, $user_cookie, $pkey_cookie, $array_items ;

	

	private $decoded_username, $decoded_password ;

	

	

	

	

	

	

	public function __construct()

	{

		

		/**

		*

		*    get_instance() function returns the CodeIgniter super object

		*	

		*    $this->load won't work in libraries

		*

		*    so assign the CodeIgniter object to a variable ( ie, $this->ci  )

		*

		*    and use codeigniter functions(eg: load,session,input etc ) using that object

		*

		*

		*/

		

		$this->ci = & get_instance();

		

		$this->ci->load->library(array('session','encrypt'));

		

		$this->ci->load->helper('cookie');

		

	}

	

	

	

	

	

	

	

	

	/**

	*

	*  create sessions and cookies

	*

	*/

	

	public function createsessions($username,$password)

	{

		

		

		

		//sessions

		

		$this->sesdata = array(

					   'shop_user2'  => $this->ci->encrypt->encode($username),

					   'shop_pkey2'  => $this->ci->encrypt->encode($password)

				   		);

		

		$this->ci->session->set_userdata($this->sesdata);

		

		



		

		

		// cookies 

		

		$this->user_cookie = array(

							'name'   => 'user2',

							'value'  => $this->ci->session->userdata('shop_user2'),

							'expire' => time() + 86500*365*50, //7 days

							'path'   => '/',

							//'domain' => '.some-domain.com',

							'prefix' => 'shop_'

							);

		

		$this->ci->input->set_cookie($this->user_cookie); 







		$this->pkey_cookie = array(

							'name'   => 'pkey2',

							'value'  => $this->ci->session->userdata('shop_pkey2'),

							'expire' => time() + 86500*365*50, //7days

							'path'   => '/',

							//'domain' => '.some-domain.com',

							'prefix' => 'shop_'

							);

		

		$this->ci->input->set_cookie($this->pkey_cookie); 

		

	}

	

	

	

	

	

	

	

	

	

	/**

	*

	*   clear sessions and cookies

	*

	*/

	

	

	public function clearsessionscookies()

	{	

		

		

		//if seesion exist destroy them

		

		if( $this->ci->session->userdata('shop_user2') )

		{

										 

			$this->array_items = array('shop_user2' => '', 'shop_pkey2' => '');

			

			$this->ci->session->unset_userdata($this->array_items);

		

		}

		

		

		if( $this->ci->session->userdata('shop_pkey2') )

		{ 

		

			$this->array_items = array('shop_user2' => '', 'shop_pkey2' => '');

			

			$this->ci->session->unset_userdata($this->array_items);

		

		}

		

		

		//destroy all sessions in this domain

		

		//$this->ci->session->sess_destroy();

		

		



		

		//if cookies exist delete them

		

		if( $this->ci->input->cookie('shop_user2') )

		{  

			

			$this->user_cookie = array(

								'name'   => 'user2',

								'value'  => '',

								'expire' => time() - 157680000,

								'path'   => '/',

								//'domain' => '.some-domain.com',

								'prefix' => 'shop_'

								);

			

			delete_cookie($this->user_cookie);

		

		}

		

		

		if( $this->ci->input->cookie('shop_pkey2') )

		{

			

			$this->pkey_cookie = array(

								'name'   => 'pkey2',

								'value'  => '',

								'expire' => time() - 157680000,

								'path'   => '/',

								//'domain' => '.some-domain.com',

								'prefix' => 'shop_'

								);

			

			delete_cookie($this->pkey_cookie);

		

		}

		

		



	}

	

	

	

	

	

	

	

	

	/**

	*

	*   check if username and password exist in database

	*

	*/

	

	public function confirmuser($username,$password)

	{

		

		 $this->ci->load->model('Admin_model') ;	

		 

		 if( $this->ci->Admin_model->checkout_userexist($username,$password) )

		 {

			 return true;

		 }

		 else

		 {

		     return false ;

		 }

		 

	}

	

	

	

	

	

	

	/**

	*

	*   check if user is deactive

	*

	*/

	

	public function is_deactive($username,$password)

	{

		

		 $this->ci->load->model('Admin_model') ;	

		 

		 if( $this->ci->Admin_model->is_deactived_user($username,$password) )

		 {

			 return true;

		 }

		 else

		 {

		     return false ;

		 }

		 

	}

	

	

	

	

	

	

	

	

	

	/**

	*

	*     check if session exist, then return true

	*

	*

	*     if cookie exist,

	*  		check if it is a valid user

	*			if valid user create sessions and return true

	*           else clear cookies and return false

	*	  

	*	

	*     if sessions and cookies not exist, then return false

	*      

	*/


public function mobile_log($username_m,$password_m)
{
	
	echo $this->decoded_username  = $this->ci->encrypt->decode($username_m) ;
  echo   $this->decoded_password = $this->ci->encrypt->decode($password_m) ;

	
	if( $this->confirmuser( $this->decoded_username , $this->decoded_password ) )

			{
				
		//	$this->createsessions($this->decoded_username,$this->decoded_password);
            return true;
			}
			else
			{
				//return false;
			}
	
	
	
}
	

	public function checkloggedin()

	{

		

		

		if( $this->ci->session->userdata('shop_user2') && $this->ci->session->userdata('shop_pkey2') )

		{ 
		
		

			return true; 

		}

			

		else if( $this->ci->input->cookie('shop_user2') &&  $this->ci->input->cookie('shop_pkey2') )

		{
        
		
		
			

						

			$this->decoded_username  = $this->ci->encrypt->decode($this->ci->input->cookie('shop_user2')) ;

			

			$this->decoded_password = $this->ci->encrypt->decode($this->ci->input->cookie('shop_pkey2')) ;

			

					

			// user exist

			if( $this->confirmuser( $this->decoded_username , $this->decoded_password ) )

			{

				

				$this->createsessions( $this->decoded_username , $this->decoded_password );

				

				return true;

				

			}

			

			//user not exist

			else

			{

				

				//$this->clearsessionscookies();

				

				return false;

				

			}

			

		}

		

		else

		{

			

			return false; 

			

		}

		

	}

	

	

	

	//log out

	public function logout()

	{

		$this->clearsessionscookies();

		$this->ci->session->sess_destroy();

	}

	

	

	

	

}





/* End of file loginfunctions.php _rpr*/

/* Location: ./application/libraries/User_loginfunctions.php */



