Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php defined('BASEPATH') or exit('No direct script access allowed');
- /*
- | --------------------------------------------------------------------
- | Authentication Class
- | --------------------------------------------------------------------
- |
- | Authentication and authorization class.
- |
- | @package UnizyCart
- | @category Libraries
- | @author Bruno Gaspar <brunofgaspar@live.com.pt>
- | @copyright Copyright (c) 2012, xxx.pt
- | @since Version 1.0
- */
- class Authentication
- {
- /*
- | --------------------------------------------------------------------
- | Variables.
- | --------------------------------------------------------------------
- */
- public $CI = null;
- public $email = null;
- public $user = null;
- public $message = null;
- public $error = null;
- /*
- | --------------------------------------------------------------------
- | Function: __construct()
- | --------------------------------------------------------------------
- |
- | Initializer.
- |
- | @access public
- | @return void
- | @author Bruno Gaspar <brunofgaspar@live.com.pt>
- */
- public function __construct()
- {
- // Instantiate CodeIgniter.
- //
- $this->CI =& get_instance();
- // Load the cookie helper.
- //
- $this->CI->load->helper('cookie');
- // Load the authentication language file.
- //
- $this->CI->lang->load('auth');
- // Check if the user is BANNED !
- //
- if ( $banned = $this->isBanned() ):
- // Show the banned screen !
- //
- show_ban( $banned );
- endif;
- // Check if the user is not logged in but we have the remember code.
- //
- if ( ! $this->loggedIn() && get_cookie('rememberCode') ):
- // Remember the user ...
- //
- $this->remembered();
- endif;
- // If we are logged in, get some user information.
- //
- if ( $this->loggedIn() ):
- // Get and save the user data.
- //
- if ( ! $this->user = $this->CI->user->getUserByEmail( $this->email ) ):
- // There must be an error, the user doesn't exist, so let's logout.
- //
- $this->logout();
- endif;
- // Log the user out if he is been inactive.
- //
- if ( $this->isInactive() ):
- // Set a message.
- //
- $this->set_message('auth:session_expired', null, true);
- // The user is inactive, logout.
- //
- $this->logout();
- endif;
- endif;
- }
- /*
- | --------------------------------------------------------------------
- | Function: login()
- | --------------------------------------------------------------------
- |
- | Login function, do you need more info about this one ?
- |
- | @access public
- | @param string
- | @param string
- | @param boolean
- | @return boolean
- | @author Bruno Gaspar <brunofgaspar@live.com.pt>
- */
- public function login( $email = null, $password = null, $remember = false )
- {
- // Load the pass hash library.
- //
- $this->CI->load->library('passhash');
- // Check if the user is blocked.
- //
- if ( $this->isBlocked() ):
- echo 'blockeddd';
- // User is not blocked, try to login.
- //
- else:
- // Check if the email exists on the database.
- //
- if ( $user = $this->CI->db->select('userID, password, status')->where('email', $email)->get('users')->row() ):
- // Compare both passwords.
- //
- if ( $this->CI->passhash->compare( $user->password, $password ) ):
- // Is the user account activated ?
- //
- if ( $user->status == 0 ):
- // Set the error message.
- //
- $this->set_error('auth:login.unsuccessful_not_active', null, true);
- // We are done here.
- //
- return false;
- endif;
- // Prepare the data to be saved on the session.
- //
- $session_data = array(
- 'email' => $email ,
- 'id' => $user->userID ,
- 'uid' => $user->userID ,
- 'user_id' => $user->userID ,
- 'activity' => strtotime('now')
- );
- // Update this user last login.
- //
- $this->updateLastLogin( $user->userID );
- // Set the session.
- //
- $this->CI->session->set_userdata( $session_data );
- // Do the user wants to be remembered ?
- //
- if ( $remember ):
- // Set the cookie, so this user gets remembered.
- //
- $this->remember( $user->userID );
- endif;
- // Set the success message.
- //
- $this->set_message('auth:login.successful', null, true);
- // Save to the log.
- //
- $this->CI->user->log($user->userID, 'login', true);
- // Logged in.
- //
- $this->loginAttempt( true );
- // We are done here.
- //
- return true;
- endif;
- endif;
- // Email and/or password doesn't seem to be valid.
- //
- $this->set_error('auth:login.unsuccessful', null, true);
- // Save the failed attempt
- //
- $this->CI->user->log($email, 'login', false);
- // Failed login attempt.
- //
- $this->loginAttempt();
- endif;
- // We are done here.
- //
- return false;
- }
- /*
- | --------------------------------------------------------------------
- | Function: logout()
- | --------------------------------------------------------------------
- |
- | Logs a user out of the system.
- |
- | @access public
- | @return boolean
- | @author Bruno Gaspar <brunofgaspar@live.com.pt>
- */
- public function logout()
- {
- // userID.
- //
- $userID = $this->user->userID;
- // Prepare the data to be removed from the session.
- //
- $session_data = array(
- 'email' => '' ,
- 'id' => '' ,
- 'uid' => '' ,
- 'user_id' => '' ,
- 'activity' => ''
- );
- // Remove the sessions.
- //
- $this->CI->session->unset_userdata( $session_data );
- // Delete the remember me cookies if they exist.
- //
- if ( get_cookie('rememberCode') ):
- // Delete the cookie.
- //
- delete_cookie('rememberCode');
- endif;
- // Clear the current session.
- //
- $this->CI->session->sess_destroy();
- // Set the success message.
- //
- $this->set_message('auth:logout.successful', null, true);
- // Save to the log.
- //
- $this->CI->user->log($userID, 'logout', true);
- // We are done here.
- //
- return true;
- }
- /*
- | --------------------------------------------------------------------
- | Function: loggedIn()
- | --------------------------------------------------------------------
- |
- | Checks if a user is logged in.
- |
- | @access public
- | @return boolean
- | @author Bruno Gaspar <brunofgaspar@live.com.pt>
- */
- public function loggedIn()
- {
- // Save the email, so we can use it later to get the user information.
- //
- $this->email = $this->CI->session->userdata('email');
- // Return true or false.
- //
- return (bool) $this->email;
- }
- /*
- | --------------------------------------------------------------------
- | Function: isAdmin()
- | --------------------------------------------------------------------
- |
- | Checks if a user is and administrator.
- |
- | @access public
- | @return boolean
- | @author Bruno Gaspar <brunofgaspar@live.com.pt>
- */
- public function isAdmin()
- {
- // Check if the user is logged in and check the user type.
- //
- if ( ! $this->loggedIn() or $this->user->profileType !== 'A' ):
- // We are done here.
- //
- return false;
- endif;
- // We got this far, it means the user is an administrator.
- //
- return true;
- }
- /*
- | --------------------------------------------------------------------
- | Function: isBanned()
- | --------------------------------------------------------------------
- |
- | Checks if a user is banned.
- |
- | @access public
- | @return boolean
- | @author Bruno Gaspar <brunofgaspar@live.com.pt>
- */
- public function isBanned()
- {
- // Get this user IP Address.
- //
- $ip_address = $this->CI->input->ip_address();
- // Get the bans list.
- //
- #$bans_list = $this->CI->bans->list();
- $bans_list = array(
- ## '127.0.0.1' => 'Foste banido por seres um filho da putaaaa'
- );
- // Check the banned list.
- //
- if ( array_key_exists( $ip_address, $bans_list ) ):
- // Yep, the user is banned, return the reason.
- //
- return $bans_list[ $ip_address ];
- endif;
- // The user is not banned.
- //
- return false;
- }
- /*
- | --------------------------------------------------------------------
- | Function: isInactive()
- | --------------------------------------------------------------------
- |
- | Checks if a user session expired.
- |
- | @access public
- | @return boolean
- | @author Bruno Gaspar <brunofgaspar@live.com.pt>
- */
- public function isInactive()
- {
- // Time the user needs to be inactive.
- //
- // - Time is in seconds -> 1800 = 30 minutes
- //
- $logLength = 1800;
- // Convert the current time to string.
- //
- $cTime = time(); //strtotime('now');
- // Check if the user have activity session.
- //
- if ( ! $activity = $this->CI->session->userdata('activity') ):
- // Create the activity session.
- //
- $this->CI->session->set_userdata('activity', $cTime);
- // Is the user session expired ?
- //
- elseif( ( (/*strtotime('now')*/ time() - $activity ) > $logLength) ):
- // Yes, the user session has expired.
- //
- return true;
- endif;
- // We keep the user logged in since the user session has not expired, yet.
- //
- $this->CI->session->set_userdata('activity', $cTime);
- // The user session has not expired.
- //
- return false;
- }
- public function isBlocked()
- {
- // Get this user IP Address.
- //
- $ip_address = $this->CI->input->ip_address();
- // Time that a user gets blocked.
- //
- $blockTime = 1800;
- // Check if we have the user record.
- //
- $record = $this->CI->db->where('ip_address', $ip_address)->get('login_attempts')->row();
- if ( ! empty( $record ) ):
- // Check this user login attempts.
- //
- if ( $record->attempts >= 3 ):
- // Check if the user block has expired.
- //
- if( ( time() - $record->lastLogin ) > $blckTime ):
- // User is not blocked anymore.
- //
- return false;
- else:
- // The user is blocked.
- //
- return true;
- endif;
- endif;
- endif;
- // The user is not blocked.
- //
- return false;
- }
- private function loginAttempt( $passed = false )
- {
- // Get this user IP Address.
- //
- $ip_address = $this->CI->input->ip_address();
- // If the user logged in with success.
- //
- if ( $passed ):
- // Clear this user loginAttempts.
- //
- $this->CI->db->where('ip_address', $ip_address)->update('login_attempts', array( 'attempts' => 0, 'lastLogin' => time() ) );
- // This is a failed login attempt.
- //
- else:
- // Check if we have the user record.
- //
- $record = $this->CI->db->where('ip_address', $ip_address)->get('login_attempts')->row();
- if ( empty( $record ) ):
- // Create the user record.
- //
- $this->CI->db->insert('login_attempts', array( 'ip_address' => $ip_address, 'attempts' => 1, 'lastLogin' => time() ) );
- // We do, check if the user needs to be blocked.
- //
- else:
- // The user exceeded the login attempts.
- //
- if ( $record->attempts < 3 ):
- // Update the user record.
- //
- $this->CI->db->where('ip_address', $ip_address)->update('login_attempts', array( 'attempts' => ( $record->attempts + 1), 'lastLogin' => time() ) );
- endif;
- endif;
- endif;
- // We are done here.
- //
- return true;
- }
- /*
- | --------------------------------------------------------------------
- | Function: remember()
- | --------------------------------------------------------------------
- |
- | Saves some user information for automatic login.
- |
- | @access private
- | @param integer
- | @return boolean
- | @author Bruno Gaspar <brunofgaspar@live.com.pt>
- */
- private function remember( $userID = null )
- {
- // Do we have a userID ?
- //
- if ( $userID === null ):
- // We are done here.
- //
- return false;
- endif;
- // Check if the user exists, just in case.
- //
- if( $user = $this->CI->user->getUser( $userID ) ):
- // Generate a remember code.
- //
- $rememberCode = sha1( $user->password );
- // Update the user profile.
- //
- $this->db->where('userID', $userID)->update('users', array('rememberCode' => $rememberCode) );
- // Set the cookie.
- //
- set_cookie(array(
- 'name' => 'rememberCode',
- 'value' => $rememberCode,
- 'expire' => $this->config->item('user_expire'),
- ));
- // We are done here.
- //
- return true;
- endif;
- // We are done here.
- //
- return false;
- }
- /*
- | --------------------------------------------------------------------
- | Function: remembered()
- | --------------------------------------------------------------------
- |
- | Remembers the user login.
- |
- | @access private
- | @return boolean
- | @author Bruno Gaspar <brunofgaspar@live.com.pt>
- */
- private function remembered()
- {
- // Check if we have the user email address.
- //
- if ( ! $email = $this->CI->session->userdata('email') ):
- // We are done here.
- //
- return false;
- endif;
- // Check if we have a rememberCode.
- //
- if ( ! $rememberCode = get_cookie('rememberCode') ):
- // We are done here.
- //
- return false;
- endif;
- // Get the user information based on the email address.
- //
- $user = $this->CI->user->getUserByEmail( $email );
- // Compare the remember me codes.
- //
- if ( $user->rememberCode === $rememberCode ):
- // Update this user last login.
- //
- $this->updateLastLogin( $user->userID );
- // Prepare the data to be saved on the session.
- //
- $session_data = array(
- 'email' => $user->email ,
- 'id' => $user->userID,
- 'uid' => $user->userID,
- 'user_id' => $user->userID
- );
- // Set the session.
- //
- $this->CI->session->set_userdata($session_data);
- // Extend the users cookies.
- //
- $this->remember( $user->userID );
- // We are done here.
- //
- return true;
- endif;
- // We are done here.
- //
- return false;
- }
- /*
- | --------------------------------------------------------------------
- | Function: forgotten_password()
- | --------------------------------------------------------------------
- |
- |
- |
- | @access public
- | @param string
- | @return boolean
- | @author Bruno Gaspar <brunofgaspar@live.com.pt>
- */
- public function forgotten_password( $email = null )
- {
- // Do we have an email ? And it is a valid one ?
- //
- if ( $email === null && ! valid_email( $email ) ):
- // We are done here.
- //
- return false;
- endif;
- // Check if the email is assigned to a profile.
- //
- if ( $this->CI->users->getProfileByEmail( $email ) ):
- // Load the PassHash library.
- //
- $this->CI->load->library('passhash');
- // Generate a new key.
- //
- $forgottenPasswordCode = $this->CI->passhash->hash( microtime() . $email );
- // Update the user record.
- //
- $this->CI->db->where('email', $email)->update('users', array('forgottenPasswordCode' => $forgottenPasswordCode) );
- // Send the email message.
- //
- # Assign data.
- #
- $data = array(
- 'email' => $email ,
- 'code' => $forgottenPasswordCode
- );
- $this->email->assign( $data );
- # Recipient email.
- #
- $this->email->to( $email );
- #
- #
- $this->email->template('auth/forgotten_password');
- # Try to send the email.
- #
- if ( $this->email->send() ):
- $this->set_message('forgot_password_successful');
- return true;
- else:
- $this->set_error('forgot_password_unsuccessful');
- return false;
- endif;
- // We are done here.
- //
- return true;
- endif;
- // We are done here.
- //
- return false;
- }
- public function forgotten_password_complete( $code = null )
- {
- // apos efectuar o pedido de recuperação de password, vamos obter um código, e
- // com esse código vamos poder fazer o reset da password e geração de uma nova.
- }
- /*
- | --------------------------------------------------------------------
- | Function: set_message()
- | --------------------------------------------------------------------
- |
- | Set a success message.
- |
- | @access public
- | @param string
- | @return string
- | @author Bruno Gaspar <brunofgaspar@live.com.pt>
- */
- public function set_message( $line, $args = null, $set_flashdata = false )
- {
- // Prepare the line.
- //
- $line = lang($line);
- // We have arguments ?
- //
- if ( $args ):
- // Do we have only one argument ?
- //
- if ( ! is_array($args) ):
- // Set the message.
- //
- $this->message = sprintf( $line, lang($args) );
- // Nope, we have multiple arguments.
- //
- else:
- // Loop through the arguments.
- //
- $arr = array();
- foreach($args as $arg):
- $arr[] = lang( $arg );
- endforeach;
- // Set the message.
- //
- $this->message = vsprintf($line, $arr);
- endif;
- // No arguments passed.
- //
- else:
- $this->message = $line;
- endif;
- // Set flashdata, just if the user gets redirected :)
- //
- if ( $set_flashdata ):
- $this->CI->session->set_flashdata('message', $this->message);
- endif;
- // Return the success message.
- //
- return $this->show_message();
- }
- /*
- | --------------------------------------------------------------------
- | Function: set_error()
- | --------------------------------------------------------------------
- |
- | Set an error message.
- |
- | @access public
- | @param string
- | @return string
- | @author Bruno Gaspar <brunofgaspar@live.com.pt>
- */
- public function set_error( $line, $args = null, $set_flashdata = false )
- {
- // Prepare the line.
- //
- $line = lang($line);
- // We have arguments ?
- //
- if ( $args ):
- // Do we have only one argument ?
- //
- if ( ! is_array($args) ):
- // Set the message.
- //
- $this->message = sprintf( $line, lang($args) );
- // Nope, we have multiple arguments.
- //
- else:
- // Loop through the arguments.
- //
- $arr = array();
- foreach($args as $arg):
- $arr[] = lang( $arg );
- endforeach;
- // Set the message.
- //
- $this->error = vsprintf($line, $arr);
- endif;
- // No arguments passed.
- //
- else:
- $this->error = $line;
- endif;
- // Set flashdata, just if the user gets redirected :)
- //
- if ( $set_flashdata ):
- $this->CI->session->set_flashdata('error', $this->error);
- endif;
- // Return the error message.
- //
- return $this->show_error();
- }
- /*
- | --------------------------------------------------------------------
- | Function: show_message()
- | --------------------------------------------------------------------
- |
- | Function to return a success message.
- |
- | @access public
- | @param string
- | @return string
- | @author Bruno Gaspar <brunofgaspar@live.com.pt>
- */
- public function show_message()
- {
- // Single success message ?
- //
- if ( $this->message ):
- return $this->message;
- // Do we have the success message on the session flashdata ?
- //
- elseif ( $message = $this->CI->session->flashdata('message') ):
- return $message;
- endif;
- // No success message, we are done here.
- //
- return false;
- }
- /*
- | --------------------------------------------------------------------
- | Function: show_error()
- | --------------------------------------------------------------------
- |
- | Function to return an error message.
- |
- | @access public
- | @param string
- | @return string
- | @author Bruno Gaspar <brunofgaspar@live.com.pt>
- */
- public function show_error()
- {
- // Single error message ?
- //
- if ( $this->error ):
- return $this->error;
- // Do we have the error on the session flashdata ?
- //
- elseif ( $error = $this->CI->session->flashdata('error') ):
- return $error;
- endif;
- // No error message, we are done here.
- //
- return false;
- }
- /*
- | --------------------------------------------------------------------
- | Function: updateLastLogin()
- | --------------------------------------------------------------------
- |
- | This function is used to update the last login of a user.
- |
- | @access public
- | @param integer
- | @return boolean
- | @author Bruno Gaspar <brunofgaspar@live.com.pt>
- */
- public function updateLastLogin( $userID = null )
- {
- // Do we have a userID ?
- //
- if( $userID === null ):
- // We are done here.
- //
- return false;
- endif;
- // Get the user ip address.
- //
- $ip_address = $this->CI->input->ip_address();
- // Update the user record.
- //
- $data = array(
- 'lastLogin' => time(),
- 'ip_address' => sprintf( '%u', ip2long( $ip_address ) )
- );
- $this->CI->db->where('userID', $userID)->update('users', $data );
- /*
- // Insert a new record on the logins table.
- //
- $data = array(
- 'userID' => $userID,
- 'ip_address' => sprintf('%u', ip2long( $ip_address ) ),
- 'date' => time()
- );
- $this->CI->db->insert('users_logins', $data);
- */
- // We are done here.
- //
- return $this->CI->db->affected_rows() == 1;
- }
- /*
- | --------------------------------------------------------------------
- | Function: forgottenPassword()
- | --------------------------------------------------------------------
- |
- | Function usefull when some customer don't remembers the password.
- |
- | @access public
- | @param string
- | @param string
- | @return boolean or string
- | @author Bruno Gaspar <brunofgaspar@live.com.pt>
- */
- public function forgottenPassword( $email = null, $forgottenPasswordCode = null )
- {
- // No email or forgotten_password passed ?
- //
- if ( $email === null && $forgottenPasswordCode === null ):
- // We are done here.
- //
- return false;
- endif;
- // Do we have a forgotten_password_code ?
- //
- if ( $forgotten_password_code !== null ):
- // Check if the code is valid.
- //
- if ( $password = $this->forgottenPasswordComplete( $forgottenPasswordCode ) ):
- // Return the new password.
- //
- return $password;
- else:
- // We are done here.
- //
- return false;
- endif;
- // No we don't have the forgottenPasswordCode.
- //
- else:
- // Check if email exists on the database.
- //
- if ( $this->checkEmail($email) ):
- // Generate a new code.
- //
- $forgottenPasswordCode = $this->generateForgottenPasswordCode();
- // Update the customer record with the new code.
- //
- $this->CI->db->where('email', $email)->update('customers', array('forgottenPasswordCode' => $forgottenPasswordCode));
- // Return the new code.
- //
- return $forgottenPasswordCode;
- // No email doesn't exist.
- //
- else:
- // We are done here.
- //
- return false;
- endif;
- endif;
- // We are done here.
- //
- return false;
- }
- public function generateForgottenPasswordCode()
- {
- // Return the code.
- //
- return generatePassword(15);
- }
- }
- /* End of file auth.php */
- /* Location: ./unizycart/libraries/auth.php */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement