Advertisement
Guest User

Auth Controller

a guest
Aug 16th, 2011
401
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 16.55 KB | None | 0 0
  1. <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
  2.  
  3. class Auth extends CI_Controller
  4. {
  5.     function __construct()
  6.     {
  7.         parent::__construct();
  8.  
  9.         $this->load->helper(array('form', 'url'));
  10.         $this->load->library('form_validation');
  11.         $this->load->library('security');
  12.         $this->load->library('tank_auth');
  13.         $this->lang->load('tank_auth');        
  14.     }
  15.  
  16.     function index()
  17.     {
  18.         if ($message = $this->session->flashdata('message')) {
  19.             $this->template->set_layout('default')->enable_parser(false);
  20.             $this->template->build('auth/general_message', array('message' => $message));
  21.         } else {
  22.             redirect('/auth/login/');
  23.         }
  24.     }
  25.  
  26.     /**
  27.      * Login user on the site
  28.      *
  29.      * @return void
  30.      */
  31.     function login()
  32.     {
  33.         if ($this->tank_auth->is_logged_in()) {                                 // logged in
  34.             redirect('');
  35.  
  36.         } elseif ($this->tank_auth->is_logged_in(FALSE)) {                      // logged in, not activated
  37.             redirect('/auth/send_again/');
  38.  
  39.         } else {
  40.             $data['login_by_username'] = ($this->config->item('login_by_username', 'tank_auth') AND
  41.                     $this->config->item('use_username', 'tank_auth'));
  42.             $data['login_by_email'] = $this->config->item('login_by_email', 'tank_auth');
  43.  
  44.             $this->form_validation->set_rules('login', 'Login', 'trim|required|xss_clean');
  45.             $this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean');
  46.             $this->form_validation->set_rules('remember', 'Remember me', 'integer');
  47.  
  48.             // Get login for counting attempts to login
  49.             if ($this->config->item('login_count_attempts', 'tank_auth') AND
  50.                     ($login = $this->input->post('login'))) {
  51.                 $login = $this->security->xss_clean($login);
  52.             } else {
  53.                 $login = '';
  54.             }
  55.  
  56.             $data['errors'] = array();
  57.  
  58.             if ($this->form_validation->run()) {                                // validation ok
  59.                 if ($this->tank_auth->login(
  60.                         $this->form_validation->set_value('login'),
  61.                         $this->form_validation->set_value('password'),
  62.                         $this->form_validation->set_value('remember'),
  63.                         $data['login_by_username'],
  64.                         $data['login_by_email'])) {                             // success
  65.                     redirect('');
  66.  
  67.                 } else {
  68.                     $errors = $this->tank_auth->get_error_message();
  69.                     if (isset($errors['banned'])) {                             // banned user
  70.                         $this->_show_message($this->lang->line('auth_message_banned').' '.$errors['banned']);
  71.  
  72.                     } elseif (isset($errors['not_activated'])) {                // not activated user
  73.                         redirect('/auth/send_again/');
  74.  
  75.                     } else {                                                    // fail
  76.                         foreach ($errors as $k => $v)   $data['errors'][$k] = $this->lang->line($v);
  77.                     }
  78.                 }
  79.             }
  80.             $this->template->set_layout('default')->enable_parser(false);
  81.             $this->template->build('auth/login_form', $data);
  82.         }
  83.     }
  84.  
  85.     /**
  86.      * Logout user
  87.      *
  88.      * @return void
  89.      */
  90.     function logout()
  91.     {
  92.         $this->tank_auth->logout();
  93.  
  94.         $this->_show_message($this->lang->line('auth_message_logged_out'));
  95.     }
  96.  
  97.     /**
  98.      * Register user on the site
  99.      *
  100.      * @return void
  101.      */
  102.     function register()
  103.     {
  104.         if ($this->tank_auth->is_logged_in()) {                                 // logged in
  105.             redirect('');
  106.  
  107.         } elseif ($this->tank_auth->is_logged_in(FALSE)) {                      // logged in, not activated
  108.             redirect('/auth/send_again/');
  109.  
  110.         } elseif (!$this->config->item('allow_registration', 'tank_auth')) {    // registration is off
  111.             $this->_show_message($this->lang->line('auth_message_registration_disabled'));
  112.  
  113.         } else {
  114.             $use_username = $this->config->item('use_username', 'tank_auth');
  115.             if ($use_username) {
  116.                 $this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean|min_length['.$this->config->item('username_min_length', 'tank_auth').']|max_length['.$this->config->item('username_max_length', 'tank_auth').']|alpha_dash');
  117.             }
  118.             $this->form_validation->set_rules('email', 'Email', 'trim|required|xss_clean|valid_email');
  119.             $this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|min_length['.$this->config->item('password_min_length', 'tank_auth').']|max_length['.$this->config->item('password_max_length', 'tank_auth').']|alpha_dash');
  120.             $this->form_validation->set_rules('confirm_password', 'Confirm Password', 'trim|required|xss_clean|matches[password]');
  121.             $this->form_validation->set_rules('first_name', 'First Name', 'trim|required|xss_clean');
  122.             $this->form_validation->set_rules('last_name', 'Last Name', 'trim|required|xss_clean');
  123.            
  124.             $data['errors'] = array();
  125.  
  126.             $email_activation = $this->config->item('email_activation', 'tank_auth');
  127.  
  128.             $userInfo["firstname"] = $this->form_validation->set_value("firstname");
  129.             $userInfo["lastname"]  = $this->form_validation->set_value("lastname");
  130.  
  131.             if ($this->form_validation->run()) {                                // validation ok
  132.                 if (!is_null($data = $this->tank_auth->create_user(
  133.                         $use_username ? $this->form_validation->set_value('username') : '',
  134.                         $this->form_validation->set_value('email'),
  135.                         $this->form_validation->set_value('password'),
  136.                         $email_activation))) {                                  // success
  137.  
  138.                     $data['site_name'] = $this->config->item('website_name', 'tank_auth');
  139.  
  140.                     if ($email_activation) {                                    // send "activate" email
  141.                         $data['activation_period'] = $this->config->item('email_activation_expire', 'tank_auth') / 3600;
  142.  
  143.                         $this->_send_email('activate', $data['email'], $data);
  144.  
  145.                         unset($data['password']); // Clear password (just for any case)
  146.  
  147.                         $this->_show_message($this->lang->line('auth_message_registration_completed_1'));
  148.  
  149.                     } else {
  150.                         if ($this->config->item('email_account_details', 'tank_auth')) {    // send "welcome" email
  151.  
  152.                             $this->_send_email('welcome', $data['email'], $data);
  153.                         }
  154.                         unset($data['password']); // Clear password (just for any case)
  155.  
  156.                         $this->_show_message($this->lang->line('auth_message_registration_completed_2').' '.anchor('/login/', 'Login'));
  157.                     }
  158.                 } else {
  159.                     $errors = $this->tank_auth->get_error_message();
  160.                     foreach ($errors as $k => $v)   $data['errors'][$k] = $this->lang->line($v);
  161.                 }
  162.             }
  163.             $data['use_username'] = $use_username;
  164.             $this->template->set_layout('default')->enable_parser(false);
  165.             $this->template->build('/auth/register_form', $data);
  166.         }
  167.     }
  168.  
  169.     /**
  170.      * Send activation email again, to the same or new email address
  171.      *
  172.      * @return void
  173.      */
  174.     function send_again()
  175.     {
  176.         if (!$this->tank_auth->is_logged_in(FALSE)) {                           // not logged in or activated
  177.             redirect('/auth/login/');
  178.  
  179.         } else {
  180.             $this->form_validation->set_rules('email', 'Email', 'trim|required|xss_clean|valid_email');
  181.  
  182.             $data['errors'] = array();
  183.  
  184.             if ($this->form_validation->run()) {                                // validation ok
  185.                 if (!is_null($data = $this->tank_auth->change_email(
  186.                         $this->form_validation->set_value('email')))) {         // success
  187.  
  188.                     $data['site_name']  = $this->config->item('website_name', 'tank_auth');
  189.                     $data['activation_period'] = $this->config->item('email_activation_expire', 'tank_auth') / 3600;
  190.  
  191.                     $this->_send_email('activate', $data['email'], $data);
  192.  
  193.                     $this->_show_message(sprintf($this->lang->line('auth_message_activation_email_sent'), $data['email']));
  194.  
  195.                 } else {
  196.                     $errors = $this->tank_auth->get_error_message();
  197.                     foreach ($errors as $k => $v)   $data['errors'][$k] = $this->lang->line($v);
  198.                 }
  199.             }
  200.             $this->template->set_layout('default')->enable_parser(false);
  201.             $this->template->build('auth/send_again_form', $data);
  202.         }
  203.     }
  204.  
  205.     /**
  206.      * Activate user account.
  207.      * User is verified by user_id and authentication code in the URL.
  208.      * Can be called by clicking on link in mail.
  209.      *
  210.      * @return void
  211.      */
  212.     function activate()
  213.     {
  214.         $user_id        = $this->uri->segment(3);
  215.         $new_email_key  = $this->uri->segment(4);
  216.  
  217.         // Activate user
  218.         if ($this->tank_auth->activate_user($user_id, $new_email_key)) {        // success
  219.             $this->tank_auth->logout();
  220.             $this->_show_message($this->lang->line('auth_message_activation_completed').' '.anchor('/auth/login/', 'Login'));
  221.  
  222.         } else {                                                                // fail
  223.             $this->_show_message($this->lang->line('auth_message_activation_failed'));
  224.         }
  225.     }
  226.  
  227.     /**
  228.      * Generate reset code (to change password) and send it to user
  229.      *
  230.      * @return void
  231.      */
  232.     function forgot_password()
  233.     {
  234.         if ($this->tank_auth->is_logged_in()) {                                 // logged in
  235.             redirect('');
  236.  
  237.         } elseif ($this->tank_auth->is_logged_in(FALSE)) {                      // logged in, not activated
  238.             redirect('/auth/send_again/');
  239.  
  240.         } else {
  241.             $this->form_validation->set_rules('login', 'Email or login', 'trim|required|xss_clean');
  242.  
  243.             $data['errors'] = array();
  244.  
  245.             if ($this->form_validation->run()) {                                // validation ok
  246.                 if (!is_null($data = $this->tank_auth->forgot_password(
  247.                         $this->form_validation->set_value('login')))) {
  248.  
  249.                     $data['site_name'] = $this->config->item('website_name', 'tank_auth');
  250.  
  251.                     // Send email with password activation link
  252.                     $this->_send_email('forgot_password', $data['email'], $data);
  253.  
  254.                     $this->_show_message($this->lang->line('auth_message_new_password_sent'));
  255.  
  256.                 } else {
  257.                     $errors = $this->tank_auth->get_error_message();
  258.                     foreach ($errors as $k => $v)   $data['errors'][$k] = $this->lang->line($v);
  259.                 }
  260.             }
  261.             $this->template->set_layout('default')->enable_parser(false);
  262.             $this->template->build('/auth/forgot_password_form', $data);
  263.         }
  264.     }
  265.  
  266.     /**
  267.      * Replace user password (forgotten) with a new one (set by user).
  268.      * User is verified by user_id and authentication code in the URL.
  269.      * Can be called by clicking on link in mail.
  270.      *
  271.      * @return void
  272.      */
  273.     function reset_password()
  274.     {
  275.         $user_id        = $this->uri->segment(3);
  276.         $new_pass_key   = $this->uri->segment(4);
  277.  
  278.         $this->form_validation->set_rules('new_password', 'New Password', 'trim|required|xss_clean|min_length['.$this->config->item('password_min_length', 'tank_auth').']|max_length['.$this->config->item('password_max_length', 'tank_auth').']|alpha_dash');
  279.         $this->form_validation->set_rules('confirm_new_password', 'Confirm new Password', 'trim|required|xss_clean|matches[new_password]');
  280.  
  281.         $data['errors'] = array();
  282.  
  283.         if ($this->form_validation->run()) {                                // validation ok
  284.             if (!is_null($data = $this->tank_auth->reset_password(
  285.                     $user_id, $new_pass_key,
  286.                     $this->form_validation->set_value('new_password')))) {  // success
  287.  
  288.                 $data['site_name'] = $this->config->item('website_name', 'tank_auth');
  289.  
  290.                 // Send email with new password
  291.                 $this->_send_email('reset_password', $data['email'], $data);
  292.  
  293.                 $this->_show_message($this->lang->line('auth_message_new_password_activated').' '.anchor('/auth/login/', 'Login'));
  294.  
  295.             } else {                                                        // fail
  296.                 $this->_show_message($this->lang->line('auth_message_new_password_failed'));
  297.             }
  298.         } else {
  299.             // Try to activate user by password key (if not activated yet)
  300.             if ($this->config->item('email_activation', 'tank_auth')) {
  301.                 $this->tank_auth->activate_user($user_id, $new_pass_key, FALSE);
  302.             }
  303.  
  304.             if (!$this->tank_auth->can_reset_password($user_id, $new_pass_key)) {
  305.                 $this->_show_message($this->lang->line('auth_message_new_password_failed'));
  306.             }
  307.         }
  308.         $this->template->set_layout('default')->enable_parser(false);
  309.         $this->template->build('/auth/reset_password_form', $data);
  310.     }
  311.  
  312.     /**
  313.      * Change user password
  314.      *
  315.      * @return void
  316.      */
  317.     function change_password()
  318.     {
  319.         if (!$this->tank_auth->is_logged_in()) {                                // not logged in or not activated
  320.             redirect('/auth/login/');
  321.  
  322.         } else {
  323.             $this->form_validation->set_rules('old_password', 'Old Password', 'trim|required|xss_clean');
  324.             $this->form_validation->set_rules('new_password', 'New Password', 'trim|required|xss_clean|min_length['.$this->config->item('password_min_length', 'tank_auth').']|max_length['.$this->config->item('password_max_length', 'tank_auth').']|alpha_dash');
  325.             $this->form_validation->set_rules('confirm_new_password', 'Confirm new Password', 'trim|required|xss_clean|matches[new_password]');
  326.  
  327.             $data['errors'] = array();
  328.  
  329.             if ($this->form_validation->run()) {                                // validation ok
  330.                 if ($this->tank_auth->change_password(
  331.                         $this->form_validation->set_value('old_password'),
  332.                         $this->form_validation->set_value('new_password'))) {   // success
  333.                     $this->_show_message($this->lang->line('auth_message_password_changed'));
  334.  
  335.                 } else {                                                        // fail
  336.                     $errors = $this->tank_auth->get_error_message();
  337.                     foreach ($errors as $k => $v)   $data['errors'][$k] = $this->lang->line($v);
  338.                 }
  339.             }
  340.             $this->template->set_layout('default')->enable_parser(false);
  341.             $this->template->build('/auth/change_password_form', $data);
  342.         }
  343.     }
  344.  
  345.     /**
  346.      * Change user email
  347.      *
  348.      * @return void
  349.      */
  350.     function change_email()
  351.     {
  352.         if (!$this->tank_auth->is_logged_in()) {                                // not logged in or not activated
  353.             redirect('/auth/login/');
  354.  
  355.         } else {
  356.             $this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean');
  357.             $this->form_validation->set_rules('email', 'Email', 'trim|required|xss_clean|valid_email');
  358.  
  359.             $data['errors'] = array();
  360.  
  361.             if ($this->form_validation->run()) {                                // validation ok
  362.                 if (!is_null($data = $this->tank_auth->set_new_email(
  363.                         $this->form_validation->set_value('email'),
  364.                         $this->form_validation->set_value('password')))) {          // success
  365.  
  366.                     $data['site_name'] = $this->config->item('website_name', 'tank_auth');
  367.  
  368.                     // Send email with new email address and its activation link
  369.                     $this->_send_email('change_email', $data['new_email'], $data);
  370.  
  371.                     $this->_show_message(sprintf($this->lang->line('auth_message_new_email_sent'), $data['new_email']));
  372.  
  373.                 } else {
  374.                     $errors = $this->tank_auth->get_error_message();
  375.                     foreach ($errors as $k => $v)   $data['errors'][$k] = $this->lang->line($v);
  376.                 }
  377.             }
  378.             $this->template->set_layout('default')->enable_parser(false);
  379.             $this->template->build('/auth/change_email_form', $data);
  380.         }
  381.     }
  382.  
  383.     /**
  384.      * Replace user email with a new one.
  385.      * User is verified by user_id and authentication code in the URL.
  386.      * Can be called by clicking on link in mail.
  387.      *
  388.      * @return void
  389.      */
  390.     function reset_email()
  391.     {
  392.         $user_id        = $this->uri->segment(3);
  393.         $new_email_key  = $this->uri->segment(4);
  394.  
  395.         // Reset email
  396.         if ($this->tank_auth->activate_new_email($user_id, $new_email_key)) {   // success
  397.             $this->tank_auth->logout();
  398.             $this->_show_message($this->lang->line('auth_message_new_email_activated').' '.anchor('/auth/login/', 'Login'));
  399.  
  400.         } else {                                                                // fail
  401.             $this->_show_message($this->lang->line('auth_message_new_email_failed'));
  402.         }
  403.     }
  404.  
  405.     /**
  406.      * Delete user from the site (only when user is logged in)
  407.      *
  408.      * @return void
  409.      */
  410.     function unregister()
  411.     {
  412.         if (!$this->tank_auth->is_logged_in()) {                                // not logged in or not activated
  413.             redirect('/auth/login/');
  414.  
  415.         } else {
  416.             $this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean');
  417.  
  418.             $data['errors'] = array();
  419.  
  420.             if ($this->form_validation->run()) {                                // validation ok
  421.                 if ($this->tank_auth->delete_user(
  422.                         $this->form_validation->set_value('password'))) {       // success
  423.                     $this->_show_message($this->lang->line('auth_message_unregistered'));
  424.  
  425.                 } else {                                                        // fail
  426.                     $errors = $this->tank_auth->get_error_message();
  427.                     foreach ($errors as $k => $v)   $data['errors'][$k] = $this->lang->line($v);
  428.                 }
  429.             }
  430.             $this->template->set_layout('default')->enable_parser(false);
  431.             $this->template->build('/auth/unregister_form', $data);
  432.         }
  433.     }
  434.  
  435.     /**
  436.      * Show info message
  437.      *
  438.      * @param   string
  439.      * @return  void
  440.      */
  441.     function _show_message($message)
  442.     {
  443.         $this->session->set_flashdata('message', $message);
  444.         redirect('/auth/');
  445.     }
  446.  
  447.     /**
  448.      * Send email message of given type (activate, forgot_password, etc.)
  449.      *
  450.      * @param   string
  451.      * @param   string
  452.      * @param   array
  453.      * @return  void
  454.      */
  455.     function _send_email($type, $email, &$data)
  456.     {
  457.         $this->load->library('email');
  458.         $this->email->from($this->config->item('webmaster_email', 'tank_auth'), $this->config->item('website_name', 'tank_auth'));
  459.         $this->email->reply_to($this->config->item('webmaster_email', 'tank_auth'), $this->config->item('website_name', 'tank_auth'));
  460.         $this->email->to($email);
  461.         $this->email->subject(sprintf($this->lang->line('auth_subject_'.$type), $this->config->item('website_name', 'tank_auth')));
  462.         $this->email->message($this->load->view('email/'.$type.'-html', $data, TRUE));
  463.         $this->email->set_alt_message($this->load->view('email/'.$type.'-txt', $data, TRUE));
  464.         $this->email->send();
  465.     }
  466.  
  467. }
  468.  
  469. /* End of file auth.php */
  470. /* Location: ./application/controllers/auth.php */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement