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 Auth_lib
- {
- /**
- *
- * @var object
- */
- private $CI;
- /**
- * construct function
- */
- public function __construct()
- {
- $this->CI =& get_instance();
- $this->CI->load->library('session');
- $this->CI->load->helper('cookie');
- $this->load->library('email');
- $this->CI->load->model('auth_mod');
- }
- /**
- * create a new user
- *
- * @param string $name
- * @param string $email
- * @param string $password
- */
- public function create_user($name, $email, $password)
- {
- if ($this->check_email($email)) return FALSE;
- $user = $this->CI->auth_mod->create_user($name, $email, $password);
- return TRUE;
- }
- /**
- * login the user
- *
- * @param string $email
- * @param string $password
- */
- public function login($email, $password, $remember_me)
- {
- $user = $this->CI->auth_mod->login($email, $password);
- if ($user)
- {
- $session = array(
- 'logged' => TRUE,
- 'id' => $user->id,
- 'name' => $user->name,
- 'email' => $user->email
- );
- $this->create_session($session);
- if ($remember_me)
- {
- $value = sha1(uniqid(rand(), TRUE));
- $this->CI->auth_mod->create_cookie($user->id, $value);
- $cookie = array(
- 'name' => 'remember_me',
- 'value' => $value,
- 'expire' => '86500',
- 'domain' => '',
- 'path' => '/',
- 'prefix' => '',
- 'secure' => TRUE
- );
- set_cookie($cookie);
- }
- }
- }
- /**
- * auto login the user for "remember me"
- *
- * @param integer $id
- */
- public function auto_login()
- {
- $cookie_value = get_cookie('remember_me');
- if ($this->session->userdata('logged') !== TRUE && $cookie_value)
- {
- $user = $this->CI->auth_mod->auto_login($cookie_value);
- if ($user)
- {
- $session = array(
- 'logged' => TRUE,
- 'id' => $user->id,
- 'name' => $user->name,
- 'email' => $user->email,
- 'last_login' => $user->last_login
- );
- $this->create_session($session);
- return TRUE;
- }
- }
- return FALSE;
- }
- /**
- * create user session
- *
- * @param array $session
- */
- public function create_session($session)
- {
- $this->CI->auth_mod->update_last_login($session['id']);
- $this->CI->session->set_userdata($session);
- }
- /**
- * logout the user
- */
- public function logout()
- {
- $this->CI->session->sess_destroy();
- if (get_cookie('remember_me'))
- {
- delete_cookie('remember_me');
- $this->CI->auth_mod->delete_cookie($user_id);
- }
- }
- /**
- * create forgotten password token
- *
- * @param integer $user_email
- */
- public function create_forgotten_password($user_email)
- {
- if (!$this->CI->auth_mod->check_email($user_email)) return FALSE;
- $value = sha1(uniqid(rand(), TRUE));
- $this->CI->auth_mod->create_forgotten_password($user_email, $value);
- return $value;
- }
- /**
- * check if forgotten password exist and set a session with his email
- *
- * @param string $value
- */
- public function check_forgotten_password($value)
- {
- $user_email = $this->CI->auth_mod->check_forgotten_password($value);
- if ($user_email)
- {
- $this->CI->session->set_flashdata('email', $user_email);
- return TRUE;
- }
- return FALSE;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement