Advertisement
Guest User

Untitled

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