Advertisement
Guest User

Untitled

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