Advertisement
Guest User

Untitled

a guest
Feb 4th, 2013
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.51 KB | None | 0 0
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2.  
  3. class Auth_lib {
  4.        
  5.     private $_ip_address;
  6.     private $_manual_activation;
  7.     private $_log_invalid_logins;
  8.     private $_max_invalid_logins;
  9.     private $_time_locked_out;
  10.     private $_noreply_email;
  11.    
  12.     public function __construct($params = false)
  13.     {
  14.         $this->load->language('auth');
  15.         $this->load->config('auth');
  16.         $this->load->library('email');
  17.         $this->load->helper('email');
  18.         //$this->load->library('user_agent');
  19.         $this->load->library('messages', array('lang' => 'auth'));
  20.        
  21.         $this->_ip_address          = $this->session->userdata('ip_address');
  22.         $this->_manual_activation   = $this->config->item('manual_activation');
  23.         $this->_log_invalid_logins  = $this->config->item('log_invalid_logins');
  24.         $this->_max_invalid_logins  = $this->config->item('max_invalid_logins');
  25.         $this->_time_locked_out     = $this->config->item('time_locked_out');
  26.         $this->_noreply_email       = $this->config->item('noreply_email');
  27.     }
  28.    
  29.     /**
  30.      * __get
  31.      *
  32.      * Enables the use of CI super-global without having to define an extra variable.
  33.      *
  34.      * @access  public
  35.      * @param   $var
  36.      * @return  mixed
  37.      */
  38.     public function __get($var)
  39.     {
  40.         return get_instance()->$var;
  41.     }
  42.  
  43.     /**
  44.      * activate
  45.      *
  46.      * @access public
  47.      * @param  string  $key
  48.      * @return bool
  49.      */
  50.      public function activate($key)
  51.      {
  52.         // Create a new temporary user object
  53.         $user = new User();
  54.        
  55.         if ($user->activate($key)) // Successfully activated
  56.         {  
  57.             if ($this->auth->logged_in())
  58.             {
  59.                 $this->session->userdata('activated', TRUE); // Update the sessions if user is logged in
  60.             }
  61.            
  62.             $this->messages->set('success', 'activation_success');
  63.             return TRUE;
  64.         }
  65.         else // Activation failed
  66.         {
  67.             $this->messages->set('error', 'activation_failed');
  68.             return FALSE;
  69.         }
  70.      }
  71.  
  72.     /**
  73.      * login
  74.      *
  75.      * @access public
  76.      * @param  string  $username
  77.      * @param  string  $password
  78.      * @return bool
  79.      */
  80.     public function login($username, $password)
  81.     {
  82.         // Create a new temporary user object
  83.         $user = new User();    
  84.        
  85.         if ($this->is_max_login_attempts_exceeded())
  86.         {
  87.             $this->messages->set('error', 'login_locked_out');
  88.             return FALSE;
  89.         }
  90.        
  91.         if ($user->login($username, $password))
  92.         {
  93.            
  94.             if (!$this->is_activated($user->id))
  95.             {
  96.                 $this->messages->set('error', 'login_not_activated');
  97.                 return FALSE;
  98.             }
  99.                
  100.             // Prepare session data
  101.             $userdata = array(
  102.                 'user_id'   =>  $user->id,
  103.                 'username'  =>  $user->username,
  104.                 'activated' =>  $user->activated === TRUE ? TRUE : FALSE,
  105.                 'logged_in' =>  TRUE
  106.             );
  107.            
  108.             // Set sesssion data
  109.             $this->session->set_userdata($userdata);   
  110.            
  111.             $this->messages->set('success', 'login_success');  
  112.             return TRUE;
  113.         }
  114.         else
  115.         {
  116.             // Log login attempt
  117.             $this->log_login_attempt();
  118.            
  119.             $this->messages->set('error', 'login_failed');
  120.             return FALSE;  
  121.         }
  122.     }
  123.  
  124.     /**
  125.      * register
  126.      *
  127.      * @access public
  128.      * @param  mixed  $data
  129.      * @return bool
  130.      */
  131.     public function register($data)
  132.     {                  
  133.         // Create a new temporary user object
  134.         $user = new User();
  135.        
  136.         // Send data to the model to complete registration
  137.         if ($user->register($data, $this->_manual_activation)) // Successfully registered
  138.         {  
  139.             if ($this->_manual_activation) // Email activation
  140.             {
  141.                 $msg = $this->load->view('emails/new_user', array('firstname' => $user->first_name, 'key' => $user->activation_key), true);
  142.                
  143.                  $this->email->set_mailtype("html");
  144.                
  145.                 send_email('test@live.se', 'Welcome '.$user->first_name.'!', $msg);
  146.                            
  147.                 $this->messages->set('success', 'register_success_activation');
  148.                 return TRUE;
  149.             }
  150.             else // No email activation necessary
  151.             {
  152.                 $this->messages->set('success', 'register_success');
  153.                 return TRUE;
  154.             }      
  155.         }  
  156.         else // Registration failed
  157.         {
  158.             $this->messages->set('error', 'register_failed');  
  159.             return FALSE;  
  160.         }
  161.     }
  162.    
  163.     /**
  164.      * forgot_password
  165.      *
  166.      * Sets a new key for password recovory and
  167.      * sends the key to the specified email
  168.      *
  169.      * @access public
  170.      * @param  string  $email
  171.      * @return bool
  172.      */
  173.      public function forgot_password($email)
  174.      {
  175.         $user = new User();
  176.        
  177.         if ($user->email_exists($email)) // Email exists
  178.         {
  179.             if ($user->set_password_recovery_key($email))
  180.             {
  181.                 send_email('test@live.se', 'Password recovery', "localhost/ci213/rstpw/".$user->id."/".$user->recover_password_key."");
  182.                
  183.                 $this->messages->set('success', 'forgot_password_success');
  184.                 return TRUE;
  185.             }
  186.             else
  187.             {
  188.                 $this->messages->set('warning', 'forgot_password_failed');
  189.                 return FALSE;
  190.             }  
  191.         }
  192.         else // Given email doesn't exist
  193.         {
  194.             $this->messages->set('error', 'forgot_password_invalid_email');
  195.             return FALSE;
  196.         }
  197.        
  198.      }
  199.      
  200.      /**
  201.       * reset_password
  202.       *
  203.       * @access public
  204.       * @param  int     $user_id
  205.       * @param  string  $password_recovery_key
  206.       * @param  string  $new_password
  207.       */
  208.      public function reset_password($user_id, $key, $new_password)
  209.      {
  210.         $user = new User();
  211.        
  212.         if ($this->is_valid_password_recovery_key($user_id, $key, $new_password)) // valid key
  213.         {
  214.             if ($user->reset_password($user_id, $key, $new_password)) // password changed
  215.             {
  216.                 $this->messages->set('success', 'reset_password_success');
  217.                 return TRUE;   
  218.             }
  219.             else // reset failed
  220.             {
  221.                 $this->messages->set('error', 'reset_password_failed');
  222.                 return FALSE;              
  223.             }
  224.         }
  225.         else // invalid key
  226.         {
  227.             $this->messages->set('error', 'reset_password_failed');
  228.             return FALSE;
  229.         }
  230.      }
  231.      
  232.      /**
  233.       * resend_activation_key
  234.       *
  235.       * @access public
  236.       * @param  string  $email
  237.       */
  238.      function resend_activation_key($email)
  239.      {
  240.         $user = new User();
  241.        
  242.         // check if the given email exists
  243.         if ($user->email_exists($email))
  244.         {
  245.             // set user where clause to email
  246.             // instead of user id
  247.             $user->where('email', $email);
  248.             if ($this->is_activated($user->id)) // don't go further if user already is activated
  249.             {
  250.                 $this->messages->set('info', 'resend_activation_key_already_activated');
  251.                 return FALSE;
  252.             }
  253.             else
  254.             {
  255.                 if ($user->set_activation_key($user->id)) // success!
  256.                 {              
  257.                     $this->messages->set('success', 'resend_activation_key_success');
  258.                     return TRUE;
  259.                 }
  260.                 else // something went wrong (database error)
  261.                 {
  262.                     $this->messages->set('warning', 'resend_activation_key_failed');   
  263.                     return FALSE;              
  264.                 }
  265.             }  
  266.         }
  267.         else // email doesn't exist
  268.         {
  269.             $this->messages->set('error', 'resend_activation_key_invalid_email');
  270.             return FALSE;
  271.         }
  272.      }
  273.    
  274.     /**
  275.      * log_login_attempt
  276.      *
  277.      * @access public
  278.      */
  279.      public function log_login_attempt()
  280.      {
  281.         // If log_invalid_logins is set to TRUE
  282.         // and the user's not locked out,
  283.         // log attempt
  284.         if ($this->_log_invalid_logins || ! $this->locked_out())
  285.         {
  286.             $login_attempt = new Login_attempt();
  287.            
  288.             $data = array(
  289.                 'session_id' => $this->session->userdata('session_id'),
  290.                 'user_agent' => $this->session->userdata('user_agent'),
  291.             );
  292.            
  293.             $login_attempt->insert_attempt($this->_ip_address, $data);         
  294.         }          
  295.      }
  296.    
  297.     /**
  298.      * is_max_login_attempts_exceeded
  299.      *
  300.      * Number of login attempts is specified
  301.      * in the auth config.
  302.      *
  303.      * @access public
  304.      * @return bool
  305.      */
  306.      public function is_max_login_attempts_exceeded()
  307.      {
  308.         // If log_invalid_logins is set to FALSE,
  309.         // can't be locked out
  310.         if ( ! $this->_log_invalid_logins)
  311.         {
  312.             return FALSE;
  313.         }  
  314.         else
  315.         {
  316.             $login_attempt = new Login_attempt();
  317.            
  318.             $login_attempt->refresh_attempts($this->_ip_address);
  319.                
  320.             if ($login_attempt->number_of_attempts($this->_ip_address) >= $this->_max_invalid_logins)
  321.             {
  322.                 return TRUE;
  323.             }  
  324.         }
  325.      }
  326.    
  327.     /**
  328.      * is_valid_password_recovery_key
  329.      *
  330.      * @access public
  331.      * @param  int     $user_id
  332.      * @param  string  $key
  333.      * @return bool
  334.      */
  335.     public function is_valid_password_recovery_key($user_id, $key)
  336.     {
  337.         $user = new User();
  338.        
  339.         if ($user->is_valid_password_recovery_key($user_id, $key))
  340.         {
  341.             return TRUE;   
  342.         }
  343.         else
  344.         {
  345.             $this->messages->set('error', 'invalid_key');
  346.             return FALSE;
  347.         }
  348.     }
  349.    
  350.     /**
  351.      * is_activated
  352.      *
  353.      * @access public
  354.      * @param  int     $user_id
  355.      * @return bool
  356.      */
  357.     public function is_activated($user_id)
  358.     {
  359.         $user = new User();
  360.        
  361.         if ($user->is_activated($user_id))
  362.         {
  363.             return TRUE;
  364.         }
  365.     }
  366.      
  367.     /**
  368.     * logged_in
  369.     *
  370.     * @access public
  371.     * @return bool
  372.     */
  373.     public function logged_in()
  374.     {      
  375.         return $this->session->userdata('logged_in');
  376.     }
  377.    
  378. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement