Advertisement
Guest User

Untitled

a guest
Mar 31st, 2012
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.35 KB | None | 0 0
  1. <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2.  
  3. class Auth_lib
  4. {
  5.     /**
  6.      *
  7.      * @var object
  8.      */
  9.     private $CI;
  10.    
  11.     /**
  12.      * construct function
  13.      */
  14.     public function __construct()
  15.     {
  16.         $this->CI =& get_instance();
  17.  
  18.         $this->CI->load->library('session');
  19.         $this->CI->load->helper('cookie');
  20.         $this->load->library('email');
  21.         $this->CI->load->model('auth_mod');
  22.     }
  23.    
  24.     /**
  25.      * create a new user
  26.      *
  27.      * @param string $name
  28.      * @param string $email
  29.      * @param string $password
  30.      */
  31.     public function create_user($name, $email, $password)
  32.     {
  33.         if ($this->check_email($email)) return FALSE;
  34.        
  35.         $user = $this->CI->auth_mod->create_user($name, $email, $password);
  36.        
  37.         return TRUE;
  38.     }
  39.    
  40.     /**
  41.      * login the user
  42.      *
  43.      * @param string $email
  44.      * @param string $password
  45.      */
  46.     public function login($email, $password, $remember_me)
  47.     {
  48.         $user = $this->CI->auth_mod->login($email, $password);
  49.        
  50.         if ($user)
  51.         {
  52.             $session = array(
  53.                             'logged' => TRUE,
  54.                             'id' => $user->id,
  55.                             'name' => $user->name,
  56.                             'email' => $user->email
  57.                         );
  58.            
  59.             $this->create_session($session);
  60.            
  61.             if ($remember_me)
  62.             {
  63.                 $value = sha1(uniqid(rand(), TRUE));
  64.                
  65.                 $this->CI->auth_mod->create_cookie($user->id, $value);
  66.        
  67.                 $cookie = array(
  68.                         'name'   => 'remember_me',
  69.                         'value'  => $value,
  70.                         'expire' => '86500',
  71.                         'domain' => '',
  72.                         'path'   => '/',
  73.                         'prefix' => '',
  74.                         'secure' => TRUE
  75.                 );
  76.                
  77.                 set_cookie($cookie);
  78.             }
  79.         }
  80.     }
  81.    
  82.     /**
  83.      * auto login the user for "remember me"
  84.      *
  85.      * @param integer $id
  86.      */
  87.     public function auto_login()
  88.     {
  89.         $cookie_value = get_cookie('remember_me');
  90.        
  91.         if ($this->session->userdata('logged') !== TRUE && $cookie_value)
  92.         {
  93.             $user = $this->CI->auth_mod->auto_login($cookie_value);
  94.        
  95.             if ($user)
  96.             {
  97.                 $session = array(
  98.                         'logged' => TRUE,
  99.                         'id' => $user->id,
  100.                         'name' => $user->name,
  101.                         'email' => $user->email,
  102.                         'last_login' => $user->last_login
  103.                 );
  104.        
  105.                 $this->create_session($session);
  106.                
  107.                 return TRUE;
  108.             }
  109.         }
  110.        
  111.         return FALSE;
  112.     }
  113.    
  114.     /**
  115.      * create user session
  116.      *
  117.      * @param array $session
  118.      */
  119.     public function create_session($session)
  120.     {
  121.         $this->CI->auth_mod->update_last_login($session['id']);
  122.        
  123.         $this->CI->session->set_userdata($session);
  124.     }
  125.    
  126.     /**
  127.      * logout the user
  128.      */
  129.     public function logout()
  130.     {
  131.         $this->CI->session->sess_destroy();
  132.        
  133.         if (get_cookie('remember_me'))
  134.         {
  135.             delete_cookie('remember_me');
  136.        
  137.             $this->CI->auth_mod->delete_cookie($user_id);
  138.         }
  139.     }
  140.    
  141.     /**
  142.      * create forgotten password token
  143.      *
  144.      * @param integer $user_email
  145.      */
  146.     public function create_forgotten_password($user_email)
  147.     {
  148.         if (!$this->CI->auth_mod->check_email($user_email)) return FALSE;
  149.        
  150.         $value = sha1(uniqid(rand(), TRUE));
  151.        
  152.         $this->CI->auth_mod->create_forgotten_password($user_email, $value);
  153.        
  154.         return $value;
  155.     }
  156.    
  157.     /**
  158.      * check if forgotten password exist and set a session with his email
  159.      *
  160.      * @param string $value
  161.      */
  162.     public function check_forgotten_password($value)
  163.     {
  164.         $user_email = $this->CI->auth_mod->check_forgotten_password($value);
  165.        
  166.         if ($user_email)
  167.         {
  168.             $this->CI->session->set_flashdata('email', $user_email);
  169.            
  170.             return TRUE;
  171.         }
  172.        
  173.         return FALSE;
  174.     }
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement