Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
- class Ibn_auth_mod extends CI_Model
- {
- /**
- * all database tables
- *
- * @var array
- */
- private $tables = array('users' => 'users', 'cookies' => 'cookies');
- public function __construct()
- {
- parent::__construct();
- $this->load->database();
- }
- /**
- * create a new user
- *
- * @param string $name
- * @param string $email
- * @param string $password
- */
- public function create_user($name, $email, $password)
- {
- $this->db->insert($this->tables['users'], array('name' => $name, 'email' => $email, 'password' => $password));
- }
- /**
- * check if email is avaiable
- *
- * @param string $email
- *
- * @return boolean
- */
- public function check_email($email)
- {
- if ($this->db->where('email', $email)->count_all_results($this->tables['users']) == 1) return TRUE;
- return FALSE;
- }
- /**
- * reset user password
- *
- * @param string $email
- */
- public function reset_password($email, $password)
- {
- $this->db->where('email', $email)->update('password', $password);
- }
- /**
- * login the user
- *
- * @param string $email
- * @param string $password
- *
- * @return object|FALSE
- */
- public function login($email, $password)
- {
- $query = $this->db->select('id, name, email, last_login')->from($this->tables['users'])->where(array('email' => $email, 'password' => $password))->get();
- if ($query->num_rows() == 1)
- {
- $user = $query->row();
- $this->update_last_login($user->id);
- return $user;
- }
- return FALSE;
- }
- /**
- * update user last login
- *
- * @param integer $id
- */
- public function update_last_login($id)
- {
- $this->db->where('id', $id)->update('last_login', CURRENT_TIMESTAMP);
- }
- /**
- * create user session for "remember me"
- *
- * @param integer $user_id
- */
- public function create_cookie($user_id, $cookie)
- {
- $this->db->insert($this->tables['cookies'], array('user_id' => $user_id, 'cookie' => $cookie));
- }
- /**
- * check user session for "remember me"
- *
- * @param string $cookie
- */
- public function check_cookie($cookie)
- {
- $query = $this->db->select('id')->from($this->tables['cookies'])->where('cookie', $cookie)->get();
- if ($query->num_rows() == 1)
- {
- return $query->row();
- }
- return FALSE;
- }
- /**
- * delete cookie from database
- *
- * @param integer $user_id
- */
- public function delete_cookie($user_id)
- {
- $this->db->delete($tables['cookies'], array('user_id' => $user_id));
- }
- /**
- * auto login the user for "remember me"
- *
- * @param integer $id
- */
- public function auto_login($id)
- {
- $query = $this->db->select('id, name, email, last_login')->from($this->tables['users'])->where('id', $id)->get();
- if ($query->num_rows() == 1)
- {
- $user = $query->row();
- $this->update_last_login($user->id);
- return $user;
- }
- return FALSE;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement