Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
- class Auth extends MY_Controller {
- public function __construct()
- {
- parent::__construct();
- $this->load->config('auth');
- $this->load->language('auth');
- $this->load->library('Auth_lib', '', 'auth'); // We give the auth library an alias (auth)
- $this->load->library('messages', array('lang' => 'auth'));
- $this->load->library('form_validation');
- $this->load->helper('form');
- }
- function index()
- {
- redirect(index_page());
- }
- /**
- * Resend activation key
- */
- function resend_activation_key()
- {
- $this->view_data['title'] = lang('resend_activation_key');
- if ($this->auth->logged_in())
- {
- redirect(base_url());
- }
- if ($_POST)
- {
- $email = $this->input->post('email');
- if ($this->auth->resend_activation_key($email)) // success
- {
- $this->messages->get();
- redirect(index_page());
- }
- else // failed
- {
- $this->messages->get();
- redirect('resend-activation-key');
- }
- }
- else // show form
- {
- $this->view_data['email'] = array(
- 'type' => 'text',
- 'name' => 'email',
- 'id' => 'email',
- 'value' => $this->form_validation->set_value('email'),
- 'placeholder' => lang('resend_activation_key_email'),
- 'autocomplete' => 'off'
- );
- }
- }
- /**
- * Activate
- */
- function activate()
- {
- $key = $this->uri->segment(2);
- if ($key)
- {
- if ($this->auth->activate($key)) // Successfully activated
- {
- $this->messages->get();
- redirect(index_page());
- }
- else
- { // Activation failed
- $this->messages->get();
- redirect(index_page());
- }
- }
- else
- { // No code
- echo show_404();
- }
- }
- /**
- * Forgot password
- */
- function forgot_password()
- {
- $this->view_data['title'] = lang('forgot_password');
- if ($this->auth->logged_in())
- {
- redirect(base_url());
- }
- if ($_POST)
- {
- $email = $this->input->post('email');
- if ($this->auth->forgot_password($email)) // Success
- {
- $this->messages->get();
- redirect('forgot-password');
- }
- else // Failed
- {
- $this->messages->get();
- redirect('forgot-password');
- }
- }
- else // Show form
- {
- $this->view_data['email'] = array(
- 'type' => 'text',
- 'name' => 'email',
- 'id' => 'email',
- 'value' => $this->form_validation->set_value('email'),
- 'placeholder' => lang('forgot_password_email'),
- 'autocomplete' => 'off'
- );
- }
- }
- /**
- * Reset password
- */
- function reset_password()
- {
- $this->view_data['title'] = lang('reset_password');
- $user_id = $this->uri->segment(2);
- $key = $this->uri->segment(3);
- // Only show the form if the key is valid
- if ($user_id && $key && $this->auth->is_valid_password_recovery_key($user_id, $key))
- {
- $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').']');
- $this->form_validation->set_rules('confirm_password', lang('reset_password_confirm_password'), 'matches[password]');
- if ($this->form_validation->run() == TRUE)
- {
- $password = $this->input->post('password');
- if ($this->auth->reset_password($user_id, $key, $password))
- {
- $this->messages->get();
- redirect('login');
- }
- else // For some reason change password failed
- {
- $this->messages->get();
- redirect('forgot-password');
- }
- }
- else // Show form
- {
- $this->view_data['password'] = array(
- 'type' => 'password',
- 'name' => 'password',
- 'id' => 'password',
- 'value' => $this->form_validation->set_value('password'),
- 'placeholder' => lang('reset_password_password'),
- 'autocomplete' => 'off'
- );
- $this->view_data['confirm_password'] = array(
- 'type' => 'password',
- 'name' => 'confirm_password',
- 'id' => 'confirm_password',
- 'value' => $this->form_validation->set_value('confirm_password'),
- 'placeholder' => lang('reset_password_confirm_password'),
- 'autocomplete' => 'off'
- );
- }
- }
- else
- {
- //$this->messages->set('error', $user_id . ' | ' . $key);
- $this->messages->get();
- redirect('forgot-password');
- }
- }
- /**
- * Login
- */
- function login()
- {
- $this->view_data['title'] = lang('login');
- if ($this->auth->logged_in())
- {
- redirect(base_url());
- }
- // Login
- if ($this->input->post('username') && $this->input->post('password'))
- {
- if ($this->auth->login($this->input->post('username'), $this->input->post('password'))) // Login success
- {
- $this->messages->get();
- redirect(index_page());
- }
- else // login failed
- {
- $this->messages->get();
- redirect('login');
- }
- }
- else // Show form
- {
- $this->view_data['username'] = array(
- 'type' => 'text',
- 'name' => 'username',
- 'id' => 'username',
- 'value' => $this->form_validation->set_value('username'),
- 'placeholder' => lang('auth_username'),
- 'autocomplete' => 'off'
- );
- $this->view_data['password'] = array(
- 'type' => 'password',
- 'name' => 'password',
- 'id' => 'password',
- 'placeholder' => lang('auth_password'),
- 'autocomplete' => 'off'
- );
- }
- }
- /**
- * Register
- */
- function register()
- {
- $this->view_data['title'] = lang('register');
- if ($this->auth->logged_in())
- {
- redirect(index_page());
- }
- // Check the config if registration is allowed
- if ($this->config->item('register_status') === 1) // Registration allowed
- {
- // Form validation
- $this->form_validation->set_rules('first_name', lang('auth_first_name'),'required|trim|alpha|xss_clean');
- $this->form_validation->set_rules('last_name', lang('auth_last_name'), 'required|trim|alpha|xss_clean');
- $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').']');
- $this->form_validation->set_rules('email', lang('auth_email'), 'required|valid_email|is_unique[users.email]');
- $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').']');
- if ($this->form_validation->run() == TRUE) // Validation true, register
- {
- $require_activation = $this->config->item('manual_activation');
- $data['first_name'] = ucfirst(strtolower($this->input->post('first_name')));
- $data['last_name'] = ucfirst(strtolower($this->input->post('last_name')));
- $data['username'] = $this->input->post('username');
- $data['email'] = $this->input->post('email');
- $data['password'] = $this->input->post('password');
- if ($this->auth->register($data, $require_activation)) // Successfully registered
- {
- $this->messages->get();
- redirect(index_page());
- }
- else // Registration failed
- {
- $this->messages->get();
- redirect('register');
- }
- }
- else // Show form
- {
- $this->view_data['first_name'] = array(
- 'type' => 'text',
- 'name' => 'first_name',
- 'id' => 'first_name',
- 'value' => $this->form_validation->set_value('first_name'),
- 'placeholder' => lang('auth_first_name'),
- 'autocomplete' => 'off'
- );
- $this->view_data['last_name'] = array(
- 'type' => 'text',
- 'name' => 'last_name',
- 'id' => 'last_name',
- 'value' => $this->form_validation->set_value('last_name'),
- 'placeholder' => lang('auth_last_name'),
- 'autocomplete' => 'off'
- );
- $this->view_data['username'] = array(
- 'type' => 'text',
- 'name' => 'username',
- 'id' => 'username',
- 'value' => $this->form_validation->set_value('username'),
- 'placeholder' => lang('auth_username'),
- 'autocomplete' => 'off'
- );
- $this->view_data['email'] = array(
- 'type' => 'text',
- 'name' => 'email',
- 'id' => 'email',
- 'value' => $this->form_validation->set_value('email'),
- 'placeholder' => lang('auth_email'),
- 'autocomplete' => 'off'
- );
- $this->view_data['password'] = array(
- 'type' => 'password',
- 'name' => 'password',
- 'id' => 'password',
- 'placeholder' => lang('auth_password'),
- 'autocomplete' => 'off'
- );
- }
- }
- else // No registration allowed
- {
- $this->view_data['message'] = message('warning', $this->config->item('register_status_reason'));
- }
- }
- /**
- * Logout
- */
- function logout()
- {
- if ( ! $this->auth->logged_in())
- {
- redirect(base_url());
- }
- $this->session->sess_destroy();
- $this->session->sess_create();
- echo flash('success', lang('logout_success'), base_url());
- }
- }
- /* End of file auth.php */
- /* Location: ./application/controllers/auth.php */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement