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 {
- private $_ip_address;
- private $_manual_activation;
- private $_log_invalid_logins;
- private $_max_invalid_logins;
- private $_time_locked_out;
- private $_noreply_email;
- public function __construct($params = false)
- {
- $this->load->language('auth');
- $this->load->config('auth');
- $this->load->library('email');
- $this->load->helper('email');
- //$this->load->library('user_agent');
- $this->load->library('messages', array('lang' => 'auth'));
- $this->_ip_address = $this->session->userdata('ip_address');
- $this->_manual_activation = $this->config->item('manual_activation');
- $this->_log_invalid_logins = $this->config->item('log_invalid_logins');
- $this->_max_invalid_logins = $this->config->item('max_invalid_logins');
- $this->_time_locked_out = $this->config->item('time_locked_out');
- $this->_noreply_email = $this->config->item('noreply_email');
- }
- /**
- * __get
- *
- * Enables the use of CI super-global without having to define an extra variable.
- *
- * @access public
- * @param $var
- * @return mixed
- */
- public function __get($var)
- {
- return get_instance()->$var;
- }
- /**
- * activate
- *
- * @access public
- * @param string $key
- * @return bool
- */
- public function activate($key)
- {
- // Create a new temporary user object
- $user = new User();
- if ($user->activate($key)) // Successfully activated
- {
- if ($this->auth->logged_in())
- {
- $this->session->userdata('activated', TRUE); // Update the sessions if user is logged in
- }
- $this->messages->set('success', 'activation_success');
- return TRUE;
- }
- else // Activation failed
- {
- $this->messages->set('error', 'activation_failed');
- return FALSE;
- }
- }
- /**
- * login
- *
- * @access public
- * @param string $username
- * @param string $password
- * @return bool
- */
- public function login($username, $password)
- {
- // Create a new temporary user object
- $user = new User();
- if ($this->is_max_login_attempts_exceeded())
- {
- $this->messages->set('error', 'login_locked_out');
- return FALSE;
- }
- if ($user->login($username, $password))
- {
- if (!$this->is_activated($user->id))
- {
- $this->messages->set('error', 'login_not_activated');
- return FALSE;
- }
- // Prepare session data
- $userdata = array(
- 'user_id' => $user->id,
- 'username' => $user->username,
- 'activated' => $user->activated === TRUE ? TRUE : FALSE,
- 'logged_in' => TRUE
- );
- // Set sesssion data
- $this->session->set_userdata($userdata);
- $this->messages->set('success', 'login_success');
- return TRUE;
- }
- else
- {
- // Log login attempt
- $this->log_login_attempt();
- $this->messages->set('error', 'login_failed');
- return FALSE;
- }
- }
- /**
- * register
- *
- * @access public
- * @param mixed $data
- * @return bool
- */
- public function register($data)
- {
- // Create a new temporary user object
- $user = new User();
- // Send data to the model to complete registration
- if ($user->register($data, $this->_manual_activation)) // Successfully registered
- {
- if ($this->_manual_activation) // Email activation
- {
- $msg = $this->load->view('emails/new_user', array('firstname' => $user->first_name, 'key' => $user->activation_key), true);
- $this->email->set_mailtype("html");
- send_email('test@live.se', 'Welcome '.$user->first_name.'!', $msg);
- $this->messages->set('success', 'register_success_activation');
- return TRUE;
- }
- else // No email activation necessary
- {
- $this->messages->set('success', 'register_success');
- return TRUE;
- }
- }
- else // Registration failed
- {
- $this->messages->set('error', 'register_failed');
- return FALSE;
- }
- }
- /**
- * forgot_password
- *
- * Sets a new key for password recovory and
- * sends the key to the specified email
- *
- * @access public
- * @param string $email
- * @return bool
- */
- public function forgot_password($email)
- {
- $user = new User();
- if ($user->email_exists($email)) // Email exists
- {
- if ($user->set_password_recovery_key($email))
- {
- send_email('test@live.se', 'Password recovery', "localhost/ci213/rstpw/".$user->id."/".$user->recover_password_key."");
- $this->messages->set('success', 'forgot_password_success');
- return TRUE;
- }
- else
- {
- $this->messages->set('warning', 'forgot_password_failed');
- return FALSE;
- }
- }
- else // Given email doesn't exist
- {
- $this->messages->set('error', 'forgot_password_invalid_email');
- return FALSE;
- }
- }
- /**
- * reset_password
- *
- * @access public
- * @param int $user_id
- * @param string $password_recovery_key
- * @param string $new_password
- */
- public function reset_password($user_id, $key, $new_password)
- {
- $user = new User();
- if ($this->is_valid_password_recovery_key($user_id, $key, $new_password)) // valid key
- {
- if ($user->reset_password($user_id, $key, $new_password)) // password changed
- {
- $this->messages->set('success', 'reset_password_success');
- return TRUE;
- }
- else // reset failed
- {
- $this->messages->set('error', 'reset_password_failed');
- return FALSE;
- }
- }
- else // invalid key
- {
- $this->messages->set('error', 'reset_password_failed');
- return FALSE;
- }
- }
- /**
- * resend_activation_key
- *
- * @access public
- * @param string $email
- */
- function resend_activation_key($email)
- {
- $user = new User();
- // check if the given email exists
- if ($user->email_exists($email))
- {
- // set user where clause to email
- // instead of user id
- $user->where('email', $email);
- if ($this->is_activated($user->id)) // don't go further if user already is activated
- {
- $this->messages->set('info', 'resend_activation_key_already_activated');
- return FALSE;
- }
- else
- {
- if ($user->set_activation_key($user->id)) // success!
- {
- $this->messages->set('success', 'resend_activation_key_success');
- return TRUE;
- }
- else // something went wrong (database error)
- {
- $this->messages->set('warning', 'resend_activation_key_failed');
- return FALSE;
- }
- }
- }
- else // email doesn't exist
- {
- $this->messages->set('error', 'resend_activation_key_invalid_email');
- return FALSE;
- }
- }
- /**
- * log_login_attempt
- *
- * @access public
- */
- public function log_login_attempt()
- {
- // If log_invalid_logins is set to TRUE
- // and the user's not locked out,
- // log attempt
- if ($this->_log_invalid_logins || ! $this->locked_out())
- {
- $login_attempt = new Login_attempt();
- $data = array(
- 'session_id' => $this->session->userdata('session_id'),
- 'user_agent' => $this->session->userdata('user_agent'),
- );
- $login_attempt->insert_attempt($this->_ip_address, $data);
- }
- }
- /**
- * is_max_login_attempts_exceeded
- *
- * Number of login attempts is specified
- * in the auth config.
- *
- * @access public
- * @return bool
- */
- public function is_max_login_attempts_exceeded()
- {
- // If log_invalid_logins is set to FALSE,
- // can't be locked out
- if ( ! $this->_log_invalid_logins)
- {
- return FALSE;
- }
- else
- {
- $login_attempt = new Login_attempt();
- $login_attempt->refresh_attempts($this->_ip_address);
- if ($login_attempt->number_of_attempts($this->_ip_address) >= $this->_max_invalid_logins)
- {
- return TRUE;
- }
- }
- }
- /**
- * is_valid_password_recovery_key
- *
- * @access public
- * @param int $user_id
- * @param string $key
- * @return bool
- */
- public function is_valid_password_recovery_key($user_id, $key)
- {
- $user = new User();
- if ($user->is_valid_password_recovery_key($user_id, $key))
- {
- return TRUE;
- }
- else
- {
- $this->messages->set('error', 'invalid_key');
- return FALSE;
- }
- }
- /**
- * is_activated
- *
- * @access public
- * @param int $user_id
- * @return bool
- */
- public function is_activated($user_id)
- {
- $user = new User();
- if ($user->is_activated($user_id))
- {
- return TRUE;
- }
- }
- /**
- * logged_in
- *
- * @access public
- * @return bool
- */
- public function logged_in()
- {
- return $this->session->userdata('logged_in');
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement