Advertisement
Guest User

Untitled

a guest
Mar 30th, 2012
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.95 KB | None | 0 0
  1. <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2.  
  3. class Ibn_auth_mod extends CI_Model
  4. {
  5.     /**
  6.      * all database tables
  7.      *
  8.      * @var array
  9.      */
  10.     private $tables = array('users' => 'users', 'cookies' => 'cookies');
  11.    
  12.     public function __construct()
  13.     {
  14.         parent::__construct();
  15.    
  16.         $this->load->database();
  17.     }
  18.    
  19.     /**
  20.      * create a new user
  21.      *
  22.      * @param string $name
  23.      * @param string $email
  24.      * @param string $password
  25.      */
  26.     public function create_user($name, $email, $password)
  27.     {
  28.         $this->db->insert($this->tables['users'], array('name' => $name, 'email' => $email, 'password' => $password));
  29.     }
  30.    
  31.     /**
  32.      * check if email is avaiable
  33.      *
  34.      * @param string $email
  35.      *
  36.      * @return boolean
  37.      */
  38.     public function check_email($email)
  39.     {
  40.         if ($this->db->where('email', $email)->count_all_results($this->tables['users']) == 1) return TRUE;
  41.        
  42.         return FALSE;
  43.     }
  44.    
  45.     /**
  46.      * reset user password
  47.      *
  48.      * @param string $email
  49.      */
  50.     public function reset_password($email, $password)
  51.     {
  52.         $this->db->where('email', $email)->update('password', $password);
  53.     }
  54.    
  55.     /**
  56.      * login the user
  57.      *
  58.      * @param string $email
  59.      * @param string $password
  60.      *
  61.      * @return object|FALSE
  62.      */
  63.     public function login($email, $password)
  64.     {
  65.         $query = $this->db->select('id, name, email, last_login')->from($this->tables['users'])->where(array('email' => $email, 'password' => $password))->get();
  66.        
  67.         if ($query->num_rows() == 1)
  68.         {
  69.             $user = $query->row();
  70.            
  71.             $this->update_last_login($user->id);
  72.            
  73.             return $user;
  74.         }
  75.        
  76.         return FALSE;
  77.     }
  78.    
  79.     /**
  80.      * update user last login
  81.      *
  82.      * @param integer $id
  83.      */
  84.     public function update_last_login($id)
  85.     {
  86.         $this->db->where('id', $id)->update('last_login', CURRENT_TIMESTAMP);
  87.     }
  88.    
  89.     /**
  90.      * create user session for "remember me"
  91.      *
  92.      * @param integer $user_id
  93.      */
  94.     public function create_cookie($user_id, $cookie)
  95.     {
  96.         $this->db->insert($this->tables['cookies'], array('user_id' => $user_id, 'cookie' => $cookie));
  97.     }
  98.    
  99.     /**
  100.      * check user session for "remember me"
  101.      *
  102.      * @param string $cookie
  103.      */
  104.     public function check_cookie($cookie)
  105.     {
  106.         $query = $this->db->select('id')->from($this->tables['cookies'])->where('cookie', $cookie)->get();
  107.        
  108.         if ($query->num_rows() == 1)
  109.         {
  110.             return $query->row();
  111.         }
  112.        
  113.         return FALSE;
  114.     }
  115.    
  116.     /**
  117.      * delete cookie from database
  118.      *
  119.      * @param integer $user_id
  120.      */
  121.     public function delete_cookie($user_id)
  122.     {
  123.         $this->db->delete($tables['cookies'], array('user_id' => $user_id));
  124.     }
  125.    
  126.     /**
  127.      * auto login the user for "remember me"
  128.      *
  129.      * @param integer $id
  130.      */
  131.     public function auto_login($id)
  132.     {
  133.         $query = $this->db->select('id, name, email, last_login')->from($this->tables['users'])->where('id', $id)->get();
  134.        
  135.         if ($query->num_rows() == 1)
  136.         {
  137.             $user = $query->row();
  138.        
  139.             $this->update_last_login($user->id);
  140.        
  141.             return $user;
  142.         }
  143.        
  144.         return FALSE;
  145.     }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement