Advertisement
Guest User

Untitled

a guest
May 8th, 2012
1,381
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 22.74 KB | None | 0 0
  1. <?php defined('BASEPATH') or exit('No direct script access allowed');
  2.  
  3. /*
  4. | --------------------------------------------------------------------
  5. | Authentication Class
  6. | --------------------------------------------------------------------
  7. |
  8. | Authentication and authorization class.
  9. |
  10. | @package    UnizyCart
  11. | @category   Libraries
  12. | @author     Bruno Gaspar <brunofgaspar@live.com.pt>
  13. | @copyright  Copyright (c) 2012, xxx.pt
  14. | @since      Version 1.0
  15. */
  16. class Authentication
  17. {
  18.     /*
  19.     | --------------------------------------------------------------------
  20.     | Variables.
  21.     | --------------------------------------------------------------------
  22.     */
  23.     public $CI       = null;
  24.     public $email    = null;
  25.     public $user     = null;
  26.     public $message  = null;
  27.     public $error    = null;
  28.  
  29.  
  30.     /*
  31.     | --------------------------------------------------------------------
  32.     | Function: __construct()
  33.     | --------------------------------------------------------------------
  34.     |
  35.     | Initializer.
  36.     |
  37.     | @access   public
  38.     | @return   void
  39.     | @author   Bruno Gaspar <brunofgaspar@live.com.pt>
  40.     */
  41.     public function __construct()
  42.     {
  43.         // Instantiate CodeIgniter.
  44.         //
  45.         $this->CI =& get_instance();
  46.  
  47.         // Load the cookie helper.
  48.         //
  49.         $this->CI->load->helper('cookie');
  50.  
  51.         // Load the authentication language file.
  52.         //
  53.         $this->CI->lang->load('auth');
  54.  
  55.         // Check if the user is BANNED !
  56.         //
  57.         if ( $banned = $this->isBanned() ):
  58.             // Show the banned screen !
  59.             //
  60.             show_ban( $banned );
  61.         endif;
  62.  
  63.         // Check if the user is not logged in but we have the remember code.
  64.         //
  65.         if ( ! $this->loggedIn() && get_cookie('rememberCode') ):
  66.             // Remember the user ...
  67.             //
  68.             $this->remembered();
  69.         endif;
  70.  
  71.         // If we are logged in, get some user information.
  72.         //
  73.         if ( $this->loggedIn() ):
  74.             // Get and save the user data.
  75.             //
  76.             if ( ! $this->user = $this->CI->user->getUserByEmail( $this->email ) ):
  77.                 // There must be an error, the user doesn't exist, so let's logout.
  78.                 //
  79.                 $this->logout();
  80.             endif;
  81.  
  82.             // Log the user out if he is been inactive.
  83.             //
  84.             if ( $this->isInactive() ):
  85.                 // Set a message.
  86.                 //
  87.                 $this->set_message('auth:session_expired', null, true);
  88.  
  89.                 // The user is inactive, logout.
  90.                 //
  91.                 $this->logout();
  92.             endif;
  93.         endif;
  94.     }
  95.  
  96.  
  97.     /*
  98.     | --------------------------------------------------------------------
  99.     | Function: login()
  100.     | --------------------------------------------------------------------
  101.     |
  102.     | Login function, do you need more info about this one ?
  103.     |
  104.     | @access   public
  105.     | @param    string
  106.     | @param    string
  107.     | @param    boolean
  108.     | @return   boolean
  109.     | @author   Bruno Gaspar <brunofgaspar@live.com.pt>
  110.     */
  111.     public function login( $email = null, $password = null, $remember = false )
  112.     {
  113.         // Load the pass hash library.
  114.         //
  115.         $this->CI->load->library('passhash');
  116.  
  117.         // Check if the user is blocked.
  118.         //
  119.         if ( $this->isBlocked() ):
  120.             echo 'blockeddd';
  121.  
  122.         // User is not blocked, try to login.
  123.         //
  124.         else:
  125.             // Check if the email exists on the database.
  126.             //
  127.             if ( $user = $this->CI->db->select('userID, password, status')->where('email', $email)->get('users')->row() ):
  128.                 // Compare both passwords.
  129.                 //
  130.                 if ( $this->CI->passhash->compare( $user->password, $password ) ):
  131.                     // Is the user account activated ?
  132.                     //
  133.                     if ( $user->status == 0 ):
  134.                         // Set the error message.
  135.                         //
  136.                         $this->set_error('auth:login.unsuccessful_not_active', null, true);
  137.  
  138.                         // We are done here.
  139.                         //
  140.                         return false;
  141.                     endif;
  142.  
  143.                     // Prepare the data to be saved on the session.
  144.                     //
  145.                     $session_data = array(
  146.                         'email'    => $email        ,
  147.                         'id'       => $user->userID ,
  148.                         'uid'      => $user->userID ,
  149.                         'user_id'  => $user->userID ,
  150.                         'activity' => strtotime('now')
  151.                     );
  152.  
  153.                     // Update this user last login.
  154.                     //
  155.                     $this->updateLastLogin( $user->userID );
  156.  
  157.                     // Set the session.
  158.                     //
  159.                     $this->CI->session->set_userdata( $session_data );
  160.  
  161.                     // Do the user wants to be remembered ?
  162.                     //
  163.                     if ( $remember ):
  164.                         // Set the cookie, so this user gets remembered.
  165.                         //
  166.                         $this->remember( $user->userID );
  167.                     endif;
  168.  
  169.                     // Set the success message.
  170.                     //
  171.                     $this->set_message('auth:login.successful', null, true);
  172.  
  173.                     // Save to the log.
  174.                     //
  175.                     $this->CI->user->log($user->userID, 'login', true);
  176.  
  177.                     // Logged in.
  178.                     //
  179.                     $this->loginAttempt( true );
  180.  
  181.                     // We are done here.
  182.                     //
  183.                     return true;
  184.                 endif;
  185.             endif;
  186.  
  187.             // Email and/or password doesn't seem to be valid.
  188.             //
  189.             $this->set_error('auth:login.unsuccessful', null, true);
  190.  
  191.             // Save the failed attempt
  192.             //
  193.             $this->CI->user->log($email, 'login', false);
  194.  
  195.             // Failed login attempt.
  196.             //
  197.             $this->loginAttempt();
  198.         endif;
  199.  
  200.         // We are done here.
  201.         //
  202.         return false;
  203.     }
  204.  
  205.  
  206.     /*
  207.     | --------------------------------------------------------------------
  208.     | Function: logout()
  209.     | --------------------------------------------------------------------
  210.     |
  211.     | Logs a user out of the system.
  212.     |
  213.     | @access   public
  214.     | @return   boolean
  215.     | @author   Bruno Gaspar <brunofgaspar@live.com.pt>
  216.     */
  217.     public function logout()
  218.     {
  219.         // userID.
  220.         //
  221.         $userID = $this->user->userID;
  222.  
  223.         // Prepare the data to be removed from the session.
  224.         //
  225.         $session_data = array(
  226.             'email'    => '' ,
  227.             'id'       => '' ,
  228.             'uid'      => '' ,
  229.             'user_id'  => '' ,
  230.             'activity' => ''
  231.         );
  232.  
  233.         // Remove the sessions.
  234.         //
  235.         $this->CI->session->unset_userdata( $session_data );
  236.  
  237.         // Delete the remember me cookies if they exist.
  238.         //
  239.         if ( get_cookie('rememberCode') ):
  240.             // Delete the cookie.
  241.             //
  242.             delete_cookie('rememberCode');
  243.         endif;
  244.  
  245.         // Clear the current session.
  246.         //
  247.         $this->CI->session->sess_destroy();
  248.  
  249.         // Set the success message.
  250.         //
  251.         $this->set_message('auth:logout.successful', null, true);
  252.  
  253.         // Save to the log.
  254.         //
  255.         $this->CI->user->log($userID, 'logout', true);
  256.  
  257.         // We are done here.
  258.         //
  259.         return true;
  260.     }
  261.  
  262.  
  263.     /*
  264.     | --------------------------------------------------------------------
  265.     | Function: loggedIn()
  266.     | --------------------------------------------------------------------
  267.     |
  268.     | Checks if a user is logged in.
  269.     |
  270.     | @access   public
  271.     | @return   boolean
  272.     | @author   Bruno Gaspar <brunofgaspar@live.com.pt>
  273.     */
  274.     public function loggedIn()
  275.     {
  276.         // Save the email, so we can use it later to get the user information.
  277.         //
  278.         $this->email = $this->CI->session->userdata('email');
  279.  
  280.         // Return true or false.
  281.         //
  282.         return (bool) $this->email;
  283.     }
  284.  
  285.  
  286.     /*
  287.     | --------------------------------------------------------------------
  288.     | Function: isAdmin()
  289.     | --------------------------------------------------------------------
  290.     |
  291.     | Checks if a user is and administrator.
  292.     |
  293.     | @access   public
  294.     | @return   boolean
  295.     | @author   Bruno Gaspar <brunofgaspar@live.com.pt>
  296.     */
  297.     public function isAdmin()
  298.     {
  299.         // Check if the user is logged in and check the user type.
  300.         //
  301.         if ( ! $this->loggedIn() or $this->user->profileType !== 'A' ):
  302.             // We are done here.
  303.             //
  304.             return false;
  305.         endif;
  306.  
  307.         // We got this far, it means the user is an administrator.
  308.         //
  309.         return true;
  310.     }
  311.  
  312.  
  313.     /*
  314.     | --------------------------------------------------------------------
  315.     | Function: isBanned()
  316.     | --------------------------------------------------------------------
  317.     |
  318.     | Checks if a user is banned.
  319.     |
  320.     | @access   public
  321.     | @return   boolean
  322.     | @author   Bruno Gaspar <brunofgaspar@live.com.pt>
  323.     */
  324.     public function isBanned()
  325.     {
  326.         // Get this user IP Address.
  327.         //
  328.         $ip_address = $this->CI->input->ip_address();
  329.  
  330.         // Get the bans list.
  331.         //
  332.         #$bans_list = $this->CI->bans->list();
  333.         $bans_list = array(
  334. ##          '127.0.0.1' => 'Foste banido por seres um filho da putaaaa'
  335.         );
  336.  
  337.         // Check the banned list.
  338.         //
  339.         if ( array_key_exists( $ip_address, $bans_list ) ):
  340.             // Yep, the user is banned, return the reason.
  341.             //
  342.             return $bans_list[ $ip_address ];
  343.         endif;
  344.  
  345.         // The user is not banned.
  346.         //
  347.         return false;
  348.     }
  349.  
  350.  
  351.     /*
  352.     | --------------------------------------------------------------------
  353.     | Function: isInactive()
  354.     | --------------------------------------------------------------------
  355.     |
  356.     | Checks if a user session expired.
  357.     |
  358.     | @access   public
  359.     | @return   boolean
  360.     | @author   Bruno Gaspar <brunofgaspar@live.com.pt>
  361.     */
  362.     public function isInactive()
  363.     {
  364.         // Time the user needs to be inactive.
  365.         //
  366.         // - Time is in seconds -> 1800 = 30 minutes
  367.         //
  368.         $logLength = 1800;
  369.  
  370.         // Convert the current time to string.
  371.         //
  372.         $cTime = time(); //strtotime('now');
  373.  
  374.         // Check if the user have activity session.
  375.         //
  376.         if ( ! $activity = $this->CI->session->userdata('activity') ):
  377.             // Create the activity session.
  378.             //
  379.             $this->CI->session->set_userdata('activity', $cTime);
  380.  
  381.         // Is the user session expired ?
  382.         //
  383.         elseif( ( (/*strtotime('now')*/ time() - $activity ) > $logLength) ):
  384.             // Yes, the user session has expired.
  385.             //
  386.             return true;
  387.         endif;
  388.  
  389.         // We keep the user logged in since the user session has not expired, yet.
  390.         //
  391.         $this->CI->session->set_userdata('activity', $cTime);
  392.  
  393.         // The user session has not expired.
  394.         //
  395.         return false;
  396.     }
  397.  
  398.  
  399.     public function isBlocked()
  400.     {
  401.         // Get this user IP Address.
  402.         //
  403.         $ip_address = $this->CI->input->ip_address();
  404.  
  405.         // Time that a user gets blocked.
  406.         //
  407.         $blockTime = 1800;
  408.  
  409.         // Check if we have the user record.
  410.         //
  411.         $record = $this->CI->db->where('ip_address', $ip_address)->get('login_attempts')->row();
  412.         if ( ! empty( $record ) ):
  413.             // Check this user login attempts.
  414.             //
  415.             if ( $record->attempts >= 3 ):
  416.                 // Check if the user block has expired.
  417.                 //
  418.                 if( ( time() - $record->lastLogin ) > $blckTime ):
  419.                     // User is not blocked anymore.
  420.                     //
  421.                     return false;
  422.                 else:
  423.                     // The user is blocked.
  424.                     //
  425.                     return true;
  426.                 endif;
  427.             endif;
  428.         endif;
  429.  
  430.         // The user is not blocked.
  431.         //
  432.         return false;
  433.     }
  434.  
  435.  
  436.     private function loginAttempt( $passed = false )
  437.     {
  438.         // Get this user IP Address.
  439.         //
  440.         $ip_address = $this->CI->input->ip_address();
  441.  
  442.         // If the user logged in with success.
  443.         //
  444.         if ( $passed ):
  445.             // Clear this user loginAttempts.
  446.             //
  447.             $this->CI->db->where('ip_address', $ip_address)->update('login_attempts', array( 'attempts' => 0, 'lastLogin' => time() ) );
  448.  
  449.         // This is a failed login attempt.
  450.         //
  451.         else:
  452.             // Check if we have the user record.
  453.             //
  454.             $record = $this->CI->db->where('ip_address', $ip_address)->get('login_attempts')->row();
  455.             if ( empty( $record ) ):
  456.                 // Create the user record.
  457.                 //
  458.                 $this->CI->db->insert('login_attempts', array( 'ip_address' => $ip_address, 'attempts' => 1, 'lastLogin' => time() ) );
  459.  
  460.             // We do, check if the user needs to be blocked.
  461.             //
  462.             else:
  463.                 // The user exceeded the login attempts.
  464.                 //
  465.                 if ( $record->attempts < 3 ):
  466.                     // Update the user record.
  467.                     //
  468.                     $this->CI->db->where('ip_address', $ip_address)->update('login_attempts', array( 'attempts' => ( $record->attempts + 1), 'lastLogin' => time() ) );
  469.                 endif;
  470.             endif;
  471.         endif;
  472.  
  473.         // We are done here.
  474.         //
  475.         return true;
  476.     }
  477.  
  478.  
  479.     /*
  480.     | --------------------------------------------------------------------
  481.     | Function: remember()
  482.     | --------------------------------------------------------------------
  483.     |
  484.     | Saves some user information for automatic login.
  485.     |
  486.     | @access   private
  487.     | @param    integer
  488.     | @return   boolean
  489.     | @author   Bruno Gaspar <brunofgaspar@live.com.pt>
  490.     */
  491.     private function remember( $userID = null )
  492.     {
  493.         // Do we have a userID ?
  494.         //
  495.         if ( $userID === null ):
  496.             // We are done here.
  497.             //
  498.             return false;
  499.         endif;
  500.  
  501.         // Check if the user exists, just in case.
  502.         //
  503.         if( $user = $this->CI->user->getUser( $userID ) ):  
  504.             // Generate a remember code.
  505.             //
  506.             $rememberCode = sha1( $user->password );
  507.  
  508.             // Update the user profile.
  509.             //
  510.             $this->db->where('userID', $userID)->update('users', array('rememberCode' => $rememberCode) );
  511.  
  512.             // Set the cookie.
  513.             //
  514.             set_cookie(array(
  515.                 'name'   => 'rememberCode',
  516.                 'value'  => $rememberCode,
  517.                 'expire' => $this->config->item('user_expire'),
  518.             ));
  519.  
  520.             // We are done here.
  521.             //
  522.             return true;
  523.         endif;
  524.  
  525.         // We are done here.
  526.         //
  527.         return false;
  528.     }
  529.  
  530.  
  531.     /*
  532.     | --------------------------------------------------------------------
  533.     | Function: remembered()
  534.     | --------------------------------------------------------------------
  535.     |
  536.     | Remembers the user login.
  537.     |
  538.     | @access   private
  539.     | @return   boolean
  540.     | @author   Bruno Gaspar <brunofgaspar@live.com.pt>
  541.     */
  542.     private function remembered()
  543.     {
  544.         // Check if we have the user email address.
  545.         //
  546.         if ( ! $email = $this->CI->session->userdata('email') ):
  547.             // We are done here.
  548.             //
  549.             return false;
  550.         endif;
  551.  
  552.         // Check if we have a rememberCode.
  553.         //
  554.         if ( ! $rememberCode = get_cookie('rememberCode') ):
  555.             // We are done here.
  556.             //
  557.             return false;
  558.         endif;
  559.  
  560.         // Get the user information based on the email address.
  561.         //
  562.         $user = $this->CI->user->getUserByEmail( $email );
  563.  
  564.         // Compare the remember me codes.
  565.         //
  566.         if ( $user->rememberCode === $rememberCode ):
  567.             // Update this user last login.
  568.             //
  569.             $this->updateLastLogin( $user->userID );
  570.  
  571.             // Prepare the data to be saved on the session.
  572.             //
  573.             $session_data = array(
  574.                 'email'   => $user->email ,
  575.                 'id'      => $user->userID,
  576.                 'uid'     => $user->userID,
  577.                 'user_id' => $user->userID
  578.             );
  579.  
  580.             // Set the session.
  581.             //
  582.             $this->CI->session->set_userdata($session_data);
  583.  
  584.             // Extend the users cookies.
  585.             //
  586.             $this->remember( $user->userID );
  587.  
  588.             // We are done here.
  589.             //
  590.             return true;
  591.         endif;
  592.  
  593.         // We are done here.
  594.         //
  595.         return false;
  596.     }
  597.  
  598.  
  599.     /*
  600.     | --------------------------------------------------------------------
  601.     | Function: forgotten_password()
  602.     | --------------------------------------------------------------------
  603.     |
  604.     |
  605.     |
  606.     | @access   public
  607.     | @param    string
  608.     | @return   boolean
  609.     | @author   Bruno Gaspar <brunofgaspar@live.com.pt>
  610.     */
  611.     public function forgotten_password( $email = null )
  612.     {
  613.         // Do we have an email ? And it is a valid one ?
  614.         //
  615.         if ( $email === null && ! valid_email( $email ) ):
  616.             // We are done here.
  617.             //
  618.             return false;
  619.         endif;
  620.  
  621.         // Check if the email is assigned to a profile.
  622.         //
  623.         if ( $this->CI->users->getProfileByEmail( $email ) ):
  624.             // Load the PassHash library.
  625.             //
  626.             $this->CI->load->library('passhash');
  627.  
  628.             // Generate a new key.
  629.             //
  630.             $forgottenPasswordCode = $this->CI->passhash->hash( microtime() . $email );
  631.  
  632.             // Update the user record.
  633.             //
  634.             $this->CI->db->where('email', $email)->update('users', array('forgottenPasswordCode' => $forgottenPasswordCode) );
  635.  
  636.             // Send the email message.
  637.             //
  638.             # Assign data.
  639.             #
  640.             $data = array(
  641.                 'email' => $email ,
  642.                 'code'  => $forgottenPasswordCode
  643.             );
  644.             $this->email->assign( $data );
  645.  
  646.             # Recipient email.
  647.             #
  648.             $this->email->to( $email );
  649.  
  650.             #
  651.             #
  652.             $this->email->template('auth/forgotten_password');
  653.  
  654.             # Try to send the email.
  655.             #
  656.             if ( $this->email->send() ):
  657.                 $this->set_message('forgot_password_successful');
  658.                 return true;
  659.             else:
  660.                 $this->set_error('forgot_password_unsuccessful');
  661.                 return false;
  662.             endif;
  663.  
  664.             // We are done here.
  665.             //
  666.             return true;
  667.         endif;
  668.  
  669.         // We are done here.
  670.         //
  671.         return false;
  672.     }
  673.  
  674.  
  675.  
  676.     public function forgotten_password_complete( $code = null )
  677.     {
  678.         // apos efectuar o pedido de recuperação de password, vamos obter um código, e
  679.         // com esse código vamos poder fazer o reset da password e geração de uma nova.
  680.     }
  681.  
  682.  
  683.     /*
  684.     | --------------------------------------------------------------------
  685.     | Function: set_message()
  686.     | --------------------------------------------------------------------
  687.     |
  688.     | Set a success message.
  689.     |
  690.     | @access   public
  691.     | @param    string
  692.     | @return   string
  693.     | @author   Bruno Gaspar <brunofgaspar@live.com.pt>
  694.     */
  695.     public function set_message( $line, $args = null, $set_flashdata = false )
  696.     {
  697.         // Prepare the line.
  698.         //
  699.         $line = lang($line);
  700.  
  701.         // We have arguments ?
  702.         //
  703.         if ( $args ):
  704.             // Do we have only one argument ?
  705.             //
  706.             if ( ! is_array($args) ):
  707.                 // Set the message.
  708.                 //
  709.                 $this->message = sprintf( $line, lang($args) );
  710.  
  711.             // Nope, we have multiple arguments.
  712.             //
  713.             else:
  714.                 // Loop through the arguments.
  715.                 //
  716.                 $arr = array();
  717.                 foreach($args as $arg):
  718.                     $arr[] = lang( $arg );
  719.                 endforeach;
  720.  
  721.                 // Set the message.
  722.                 //
  723.                 $this->message = vsprintf($line, $arr);
  724.             endif;
  725.  
  726.         // No arguments passed.
  727.         //
  728.         else:
  729.             $this->message = $line;
  730.         endif;
  731.  
  732.         // Set flashdata, just if the user gets redirected :)
  733.         //
  734.         if ( $set_flashdata ):
  735.             $this->CI->session->set_flashdata('message', $this->message);
  736.         endif;
  737.  
  738.         // Return the success message.
  739.         //
  740.         return $this->show_message();
  741.     }
  742.  
  743.  
  744.     /*
  745.     | --------------------------------------------------------------------
  746.     | Function: set_error()
  747.     | --------------------------------------------------------------------
  748.     |
  749.     | Set an error message.
  750.     |
  751.     | @access   public
  752.     | @param    string
  753.     | @return   string
  754.     | @author   Bruno Gaspar <brunofgaspar@live.com.pt>
  755.     */
  756.     public function set_error( $line, $args = null, $set_flashdata = false )
  757.     {
  758.         // Prepare the line.
  759.         //
  760.         $line = lang($line);
  761.  
  762.         // We have arguments ?
  763.         //
  764.         if ( $args ):
  765.             // Do we have only one argument ?
  766.             //
  767.             if ( ! is_array($args) ):
  768.                 // Set the message.
  769.                 //
  770.                 $this->message = sprintf( $line, lang($args) );
  771.  
  772.             // Nope, we have multiple arguments.
  773.             //
  774.             else:
  775.                 // Loop through the arguments.
  776.                 //
  777.                 $arr = array();
  778.                 foreach($args as $arg):
  779.                     $arr[] = lang( $arg );
  780.                 endforeach;
  781.  
  782.                 // Set the message.
  783.                 //
  784.                 $this->error = vsprintf($line, $arr);
  785.             endif;
  786.  
  787.         // No arguments passed.
  788.         //
  789.         else:
  790.             $this->error = $line;
  791.         endif;
  792.  
  793.         // Set flashdata, just if the user gets redirected :)
  794.         //
  795.         if ( $set_flashdata ):
  796.             $this->CI->session->set_flashdata('error', $this->error);
  797.         endif;
  798.  
  799.         // Return the error message.
  800.         //
  801.         return $this->show_error();
  802.     }
  803.  
  804.  
  805.     /*
  806.     | --------------------------------------------------------------------
  807.     | Function: show_message()
  808.     | --------------------------------------------------------------------
  809.     |
  810.     | Function to return a success message.
  811.     |
  812.     | @access   public
  813.     | @param    string
  814.     | @return   string
  815.     | @author   Bruno Gaspar <brunofgaspar@live.com.pt>
  816.     */
  817.     public function show_message()
  818.     {
  819.         // Single success message ?
  820.         //
  821.         if ( $this->message ):
  822.             return $this->message;
  823.  
  824.         // Do we have the success message on the session flashdata ?
  825.         //
  826.         elseif ( $message = $this->CI->session->flashdata('message') ):
  827.             return $message;
  828.         endif;
  829.  
  830.         // No success message, we are done here.
  831.         //
  832.         return false;
  833.     }
  834.  
  835.  
  836.     /*
  837.     | --------------------------------------------------------------------
  838.     | Function: show_error()
  839.     | --------------------------------------------------------------------
  840.     |
  841.     | Function to return an error message.
  842.     |
  843.     | @access   public
  844.     | @param    string
  845.     | @return   string
  846.     | @author   Bruno Gaspar <brunofgaspar@live.com.pt>
  847.     */
  848.     public function show_error()
  849.     {
  850.         // Single error message ?
  851.         //
  852.         if ( $this->error ):
  853.             return $this->error;
  854.  
  855.         // Do we have the error on the session flashdata ?
  856.         //
  857.         elseif ( $error = $this->CI->session->flashdata('error') ):
  858.             return $error;
  859.         endif;
  860.  
  861.         // No error message, we are done here.
  862.         //
  863.         return false;
  864.     }
  865.  
  866.  
  867.     /*
  868.     | --------------------------------------------------------------------
  869.     | Function: updateLastLogin()
  870.     | --------------------------------------------------------------------
  871.     |
  872.     | This function is used to update the last login of a user.
  873.     |
  874.     | @access   public
  875.     | @param    integer
  876.     | @return   boolean
  877.     | @author   Bruno Gaspar <brunofgaspar@live.com.pt>
  878.     */
  879.     public function updateLastLogin( $userID = null )
  880.     {
  881.         // Do we have a userID ?
  882.         //
  883.         if( $userID === null ):
  884.             // We are done here.
  885.             //
  886.             return false;
  887.         endif;
  888.  
  889.         // Get the user ip address.
  890.         //
  891.         $ip_address = $this->CI->input->ip_address();
  892.  
  893.         // Update the user record.
  894.         //
  895.         $data = array(
  896.             'lastLogin'  => time(),
  897.             'ip_address' => sprintf( '%u', ip2long( $ip_address ) )
  898.         );
  899.         $this->CI->db->where('userID', $userID)->update('users', $data );
  900. /*
  901.         // Insert a new record on the logins table.
  902.         //
  903.         $data = array(
  904.             'userID'  => $userID,
  905.             'ip_address' => sprintf('%u', ip2long( $ip_address ) ),
  906.             'date'       => time()
  907.         );
  908.         $this->CI->db->insert('users_logins', $data);
  909. */
  910.         // We are done here.
  911.         //
  912.         return $this->CI->db->affected_rows() == 1;
  913.     }
  914.  
  915.  
  916.     /*
  917.     | --------------------------------------------------------------------
  918.     | Function: forgottenPassword()
  919.     | --------------------------------------------------------------------
  920.     |
  921.     | Function usefull when some customer don't remembers the password.
  922.     |
  923.     | @access   public
  924.     | @param    string
  925.     | @param    string
  926.     | @return   boolean or string
  927.     | @author   Bruno Gaspar <brunofgaspar@live.com.pt>
  928.     */
  929.     public function forgottenPassword( $email = null, $forgottenPasswordCode = null )
  930.     {
  931.         // No email or forgotten_password passed ?
  932.         //
  933.         if ( $email === null && $forgottenPasswordCode === null ):
  934.             // We are done here.
  935.             //
  936.             return false;
  937.         endif;
  938.  
  939.         // Do we have a forgotten_password_code ?
  940.         //
  941.         if ( $forgotten_password_code !== null ):
  942.             // Check if the code is valid.
  943.             //
  944.             if ( $password = $this->forgottenPasswordComplete( $forgottenPasswordCode ) ):
  945.                 // Return the new password.
  946.                 //
  947.                 return $password;
  948.             else:
  949.                 // We are done here.
  950.                 //
  951.                 return false;
  952.             endif;
  953.  
  954.         // No we don't have the forgottenPasswordCode.
  955.         //
  956.         else:
  957.             // Check if email exists on the database.
  958.             //
  959.             if ( $this->checkEmail($email) ):
  960.                 // Generate a new code.
  961.                 //
  962.                 $forgottenPasswordCode = $this->generateForgottenPasswordCode();
  963.  
  964.                 // Update the customer record with the new code.
  965.                 //
  966.                 $this->CI->db->where('email', $email)->update('customers', array('forgottenPasswordCode' => $forgottenPasswordCode));
  967.  
  968.                 // Return the new code.
  969.                 //
  970.                 return $forgottenPasswordCode;
  971.  
  972.             // No email doesn't exist.
  973.             //
  974.             else:
  975.                 // We are done here.
  976.                 //
  977.                 return false;
  978.             endif;
  979.            
  980.         endif;
  981.  
  982.         // We are done here.
  983.         //
  984.         return false;
  985.     }
  986.  
  987.  
  988.  
  989.     public function generateForgottenPasswordCode()
  990.     {
  991.         // Return the code.
  992.         //
  993.         return generatePassword(15);
  994.     }
  995. }
  996.  
  997. /* End of file auth.php */
  998. /* Location: ./unizycart/libraries/auth.php */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement