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





/*

*

*    DON'T TRY TO EXTEND LIBRARY CLASSES

*

*/





class 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_user'  => $this->ci->encrypt->encode($username),

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

				   		);

		

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

		

		



		

		

		// cookies 

		

		$this->user_cookie = array(

							'name'   => 'user',

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

							'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'   => 'pkey',

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

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

							'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_user') )

		{

										 

			$this->array_items = array('shop_user' => '', 'shop_pkey' => '');

			

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

		

		}

		

		

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

		{ 

		

			$this->array_items = array('shop_user' => '', 'shop_pkey' => '');

			

			$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_user') )

		{  

			

			$this->user_cookie = array(

								'name'   => 'user',

								'value'  => '',

								'expire' => time() - 86500*365*50,

								'path'   => '/',

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

								'prefix' => 'shop_'

								);

			

			delete_cookie($this->user_cookie);

		

		}

		

		

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

		{

			

			$this->pkey_cookie = array(

								'name'   => 'pkey',

								'value'  => '',

								'expire' => time() - 86500*365*50,

								'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->userexist($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 checkloggedin()

	{

		

		

		if( $this->ci->session->userdata('shop_user') && $this->ci->session->userdata('shop_pkey') )

		{ 

			return true; 

		}

			

		else if( $this->ci->input->cookie('shop_user') &&  $this->ci->input->cookie('shop_pkey') )

		{

			

						

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

			

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

			

					

			// 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();

	}

	

	

	public function password_check($password)

{

   if (preg_match('#[0-9]#', $str) && preg_match('#[a-zA-Z]#', $str)) {

     return TRUE;

   }

   return FALSE;

}

	

}









/* End of file loginfunctions.php _rpr*/

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



