Advertisement
Guest User

Untitled

a guest
Feb 4th, 2013
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.19 KB | None | 0 0
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2.  
  3. class Auth extends MY_Controller {
  4.    
  5.     public function __construct()
  6.     {
  7.         parent::__construct();
  8.        
  9.         $this->load->config('auth');
  10.         $this->load->language('auth');
  11.         $this->load->library('Auth_lib', '', 'auth'); // We give the auth library an alias (auth)
  12.         $this->load->library('messages', array('lang' => 'auth'));
  13.         $this->load->library('form_validation');
  14.         $this->load->helper('form');
  15.     }
  16.    
  17.     function index()
  18.     {
  19.         redirect(index_page());
  20.     }
  21.    
  22.     /**
  23.      * Resend activation key
  24.      */
  25.      function resend_activation_key()
  26.      {
  27.         $this->view_data['title'] = lang('resend_activation_key'); 
  28.        
  29.         if ($this->auth->logged_in())
  30.         {
  31.             redirect(base_url());
  32.         }
  33.        
  34.         if ($_POST)
  35.         {
  36.             $email = $this->input->post('email');
  37.            
  38.             if ($this->auth->resend_activation_key($email)) // success
  39.             {
  40.                 $this->messages->get();
  41.                 redirect(index_page());
  42.             }
  43.             else // failed
  44.             {
  45.                 $this->messages->get();
  46.                 redirect('resend-activation-key');
  47.             }
  48.            
  49.         }
  50.         else // show form
  51.         {
  52.             $this->view_data['email'] = array(
  53.                 'type'          =>  'text',
  54.                 'name'          =>  'email',
  55.                 'id'            =>  'email',
  56.                 'value'         =>  $this->form_validation->set_value('email'),
  57.                 'placeholder'   =>  lang('resend_activation_key_email'),
  58.                 'autocomplete'  =>  'off'
  59.             ); 
  60.         }  
  61.      }
  62.        
  63.     /**
  64.      * Activate
  65.      */
  66.      function activate()
  67.      {
  68.         $key = $this->uri->segment(2);
  69.        
  70.         if ($key)
  71.         {      
  72.             if ($this->auth->activate($key)) // Successfully activated
  73.             {
  74.                 $this->messages->get();
  75.                 redirect(index_page());
  76.             }
  77.             else
  78.             { // Activation failed
  79.                 $this->messages->get();
  80.                 redirect(index_page());
  81.             }      
  82.         }
  83.         else
  84.         { // No code
  85.             echo show_404();   
  86.         }
  87.      }
  88.  
  89.     /**
  90.      * Forgot password
  91.      */
  92.      function forgot_password()
  93.      {
  94.         $this->view_data['title'] = lang('forgot_password');   
  95.        
  96.         if ($this->auth->logged_in())
  97.         {
  98.             redirect(base_url());
  99.         }
  100.        
  101.         if ($_POST)
  102.         {
  103.             $email = $this->input->post('email');
  104.            
  105.             if ($this->auth->forgot_password($email)) // Success
  106.             {
  107.                 $this->messages->get();
  108.                 redirect('forgot-password');
  109.             }
  110.             else // Failed
  111.             {
  112.                 $this->messages->get();
  113.                 redirect('forgot-password');
  114.             }
  115.         }
  116.         else // Show form
  117.         {
  118.             $this->view_data['email'] = array(
  119.                 'type'          =>  'text',
  120.                 'name'          =>  'email',
  121.                 'id'            =>  'email',
  122.                 'value'         =>  $this->form_validation->set_value('email'),
  123.                 'placeholder'   =>  lang('forgot_password_email'),
  124.                 'autocomplete'  =>  'off'
  125.             );
  126.         }
  127.      }
  128.      
  129.      /**
  130.       * Reset password
  131.       */   
  132.      function reset_password()
  133.      {
  134.         $this->view_data['title'] = lang('reset_password');
  135.        
  136.         $user_id = $this->uri->segment(2); 
  137.         $key = $this->uri->segment(3);
  138.        
  139.         // Only show the form if the key is valid
  140.         if ($user_id && $key && $this->auth->is_valid_password_recovery_key($user_id, $key))
  141.         {
  142.             $this->form_validation->set_rules('password', lang('reset_password_password'), 'required|min_length['.$this->config->item('min_password_length').']|max_length['.$this->config->item('max_password_length').']');      
  143.             $this->form_validation->set_rules('confirm_password', lang('reset_password_confirm_password'), 'matches[password]');
  144.            
  145.             if ($this->form_validation->run() == TRUE)
  146.             {
  147.                 $password = $this->input->post('password');
  148.                
  149.                 if ($this->auth->reset_password($user_id, $key, $password))
  150.                 {
  151.                     $this->messages->get();
  152.                     redirect('login');
  153.                 }
  154.                 else // For some reason change password failed
  155.                 {
  156.                     $this->messages->get();
  157.                     redirect('forgot-password');
  158.                 }
  159.             }
  160.             else // Show form
  161.             {
  162.                 $this->view_data['password'] = array(
  163.                     'type'          =>  'password',
  164.                     'name'          =>  'password',
  165.                     'id'            =>  'password',
  166.                     'value'         =>  $this->form_validation->set_value('password'),
  167.                     'placeholder'   =>  lang('reset_password_password'),
  168.                     'autocomplete'  =>  'off'
  169.                 );
  170.                
  171.                 $this->view_data['confirm_password'] = array(
  172.                     'type'          =>  'password',
  173.                     'name'          =>  'confirm_password',
  174.                     'id'            =>  'confirm_password',
  175.                     'value'         =>  $this->form_validation->set_value('confirm_password'),
  176.                     'placeholder'   =>  lang('reset_password_confirm_password'),
  177.                     'autocomplete'  =>  'off'
  178.                 ); 
  179.             }
  180.         }
  181.         else
  182.         {
  183.             //$this->messages->set('error', $user_id . ' | ' . $key);
  184.             $this->messages->get();
  185.             redirect('forgot-password');
  186.         }
  187.      }
  188.      
  189.     /**
  190.      * Login
  191.      */
  192.      function login()
  193.      { 
  194.         $this->view_data['title'] = lang('login');
  195.        
  196.         if ($this->auth->logged_in())
  197.         {
  198.             redirect(base_url());
  199.         }
  200.        
  201.         // Login
  202.         if ($this->input->post('username') && $this->input->post('password'))
  203.         {      
  204.             if ($this->auth->login($this->input->post('username'), $this->input->post('password'))) // Login success
  205.             {  
  206.                 $this->messages->get();
  207.                 redirect(index_page());
  208.             }
  209.             else // login failed
  210.             {
  211.                 $this->messages->get();
  212.                 redirect('login');
  213.             }      
  214.         }
  215.         else // Show form
  216.         {
  217.             $this->view_data['username'] = array(
  218.                 'type'          =>  'text',
  219.                 'name'          =>  'username',
  220.                 'id'            =>  'username',
  221.                 'value'         =>  $this->form_validation->set_value('username'),
  222.                 'placeholder'   =>  lang('auth_username'),
  223.                 'autocomplete'  =>  'off'
  224.             );
  225.            
  226.             $this->view_data['password'] = array(
  227.                 'type'          =>  'password',
  228.                 'name'          =>  'password',
  229.                 'id'            =>  'password',
  230.                 'placeholder'   =>  lang('auth_password'),
  231.                 'autocomplete'  =>  'off'
  232.             ); 
  233.         }
  234.        
  235.      }
  236.    
  237.     /**
  238.      * Register
  239.      */
  240.     function register()
  241.     {
  242.         $this->view_data['title'] = lang('register');
  243.        
  244.         if ($this->auth->logged_in())
  245.         {
  246.             redirect(index_page());
  247.         }
  248.        
  249.         // Check the config if registration is allowed
  250.         if ($this->config->item('register_status') === 1) // Registration allowed
  251.         {
  252.                
  253.             // Form validation
  254.             $this->form_validation->set_rules('first_name', lang('auth_first_name'),'required|trim|alpha|xss_clean');
  255.             $this->form_validation->set_rules('last_name',  lang('auth_last_name'), 'required|trim|alpha|xss_clean');
  256.             $this->form_validation->set_rules('username',   lang('auth_username'),  'required|alpha_numeric|is_unique[users.username]|min_length['.$this->config->item('min_username_length').']|max_length['.$this->config->item('max_username_length').']');
  257.             $this->form_validation->set_rules('email',      lang('auth_email'),     'required|valid_email|is_unique[users.email]');
  258.             $this->form_validation->set_rules('password',   lang('auth_password'),  'required|min_length['.$this->config->item('min_password_length').']|max_length['.$this->config->item('max_password_length').']');
  259.        
  260.             if ($this->form_validation->run() == TRUE) // Validation true, register
  261.             {          
  262.                 $require_activation =   $this->config->item('manual_activation');  
  263.                    
  264.                 $data['first_name'] = ucfirst(strtolower($this->input->post('first_name')));
  265.                 $data['last_name']  = ucfirst(strtolower($this->input->post('last_name')));
  266.                 $data['username']   = $this->input->post('username');
  267.                 $data['email']      = $this->input->post('email');
  268.                 $data['password']   = $this->input->post('password');
  269.                
  270.                 if ($this->auth->register($data, $require_activation)) // Successfully registered
  271.                 {
  272.                     $this->messages->get();
  273.                     redirect(index_page());
  274.                 }
  275.                 else // Registration failed
  276.                 {
  277.                     $this->messages->get();
  278.                     redirect('register');
  279.                 }
  280.             }
  281.             else // Show form
  282.             {  
  283.                 $this->view_data['first_name'] = array(
  284.                     'type'          =>  'text',
  285.                     'name'          =>  'first_name',
  286.                     'id'            =>  'first_name',
  287.                     'value'         =>  $this->form_validation->set_value('first_name'),
  288.                     'placeholder'   =>  lang('auth_first_name'),
  289.                     'autocomplete'  =>  'off'
  290.                 );
  291.                
  292.                 $this->view_data['last_name'] = array(
  293.                     'type'          =>  'text',
  294.                     'name'          =>  'last_name',
  295.                     'id'            =>  'last_name',
  296.                     'value'         =>  $this->form_validation->set_value('last_name'),
  297.                     'placeholder'   =>  lang('auth_last_name'),
  298.                     'autocomplete'  =>  'off'
  299.                 );
  300.                                      
  301.                 $this->view_data['username'] = array(
  302.                     'type'          =>  'text',
  303.                     'name'          =>  'username',
  304.                     'id'            =>  'username',
  305.                     'value'         =>  $this->form_validation->set_value('username'),
  306.                     'placeholder'   =>  lang('auth_username'),
  307.                     'autocomplete'  =>  'off'
  308.                 );
  309.                
  310.                 $this->view_data['email'] = array(
  311.                     'type'          =>  'text',
  312.                     'name'          =>  'email',
  313.                     'id'            =>  'email',
  314.                     'value'         =>  $this->form_validation->set_value('email'),
  315.                     'placeholder'   =>  lang('auth_email'),
  316.                     'autocomplete'  =>  'off'
  317.                 );
  318.                
  319.                 $this->view_data['password'] = array(
  320.                     'type'          =>  'password',
  321.                     'name'          =>  'password',
  322.                     'id'            =>  'password',
  323.                     'placeholder'   =>  lang('auth_password'),
  324.                     'autocomplete'  =>  'off'
  325.                 );                 
  326.             }  
  327.         }
  328.         else // No registration allowed
  329.         {
  330.             $this->view_data['message'] = message('warning', $this->config->item('register_status_reason'));
  331.         }
  332.     }
  333.  
  334.     /**
  335.      * Logout
  336.      */
  337.      function logout()
  338.      {
  339.         if ( ! $this->auth->logged_in())
  340.         {
  341.             redirect(base_url());
  342.         }
  343.        
  344.         $this->session->sess_destroy();
  345.         $this->session->sess_create();
  346.        
  347.         echo flash('success', lang('logout_success'), base_url());
  348.      }
  349.    
  350. }
  351.  
  352. /* End of file auth.php */
  353. /* Location: ./application/controllers/auth.php */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement