Advertisement
Guest User

Untitled

a guest
Oct 1st, 2017
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.41 KB | None | 0 0
  1. <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2.  
  3. class User extends CI_Controller {
  4.  
  5.     public function __construct() {
  6.  
  7.         parent::__construct();
  8.  
  9.         $this->load->library('session');
  10.         $this->load->model('users_model', '', TRUE);
  11.     }
  12.  
  13.     public function index() {
  14.  
  15.         // Authenticate
  16.         echo 'Logged in as ' . $this->session->userdata('screen_name');
  17.     }
  18.  
  19.     public function authenticate() {
  20.  
  21.         if( ! $this->session->userdata('id')) {
  22.  
  23.             // Load libraries
  24.             $this->load->library('form_validation');
  25.  
  26.             // Header
  27.             $data['page_title'] = 'Authentication Page';
  28.             $this->load->view('header', $data);
  29.  
  30.             // Set validation rules
  31.             $this->form_validation->set_rules('username', 'Username', 'required|min_length[3]|max_length[15]');
  32.             $this->form_validation->set_rules('password', 'Password', 'required|min_length[5]');
  33.  
  34.             // Validate
  35.             if( ! $this->form_validation->run()) {
  36.  
  37.                 // Form
  38.                 $this->load->view('login');
  39.             } else {
  40.  
  41.                 // Get data
  42.                 $username = $this->input->post('username');
  43.                 $password = sha1($this->input->post('password'));
  44.  
  45.                 // Check the credentials
  46.                 $user_info = $this->users_model->check_user($username, $password);
  47.  
  48.                 if($user_info) {
  49.  
  50.                     // Login successful
  51.  
  52.                     // Create user info array to be passed to set_userdata()
  53.                     $session_info['id']          = $user_info->id;
  54.                     $session_info['username']    = $user_info->username;
  55.                     $session_info['screen_name'] = $user_info->screen_name;
  56.  
  57.                     // Set the data
  58.                     $this->session->set_userdata($session_info);
  59.  
  60.                     $this->username = $this->session->userdata('username');
  61.                     $this->screen_name = $this->session->userdata('screen_name');
  62.  
  63.                     // Redirect to the main page
  64.                     redirect('/');
  65.                 } else {
  66.  
  67.                     // Invalid data; Reload the form
  68.                     $data['invalid'] = 'Sorry, but the username/password combination is invalid,
  69.                                        or you do not have enough privileges yet. Please try again!';
  70.                     $this->load->view('login', $data);
  71.                 }
  72.             }
  73.  
  74.             // Footer
  75.             $this->load->view('footer');
  76.         } else {
  77.  
  78.             redirect('/');
  79.         }
  80.     }
  81.  
  82.     public function logout() {
  83.  
  84.         if($this->session->userdata('id')) {
  85.  
  86.             // Clear user data
  87.             $this->session->sess_destroy();
  88.             redirect('/user/authenticate');
  89.         } else {
  90.  
  91.             // Already logged out
  92.         }
  93.     }
  94.  
  95.     public function register() {
  96.  
  97.         if( ! $this->session->userdata('id')) {
  98.  
  99.             // Form validation
  100.             $this->load->library('form_validation');
  101.  
  102.             // Header
  103.             $data['page_title'] = 'Registration Page';
  104.             $this->load->view('header', $data);
  105.  
  106.             // Form validation rules
  107.             $this->form_validation->set_rules('username', 'Username', 'required|min_length[4]|max_length[15]');
  108.             $this->form_validation->set_rules('screen_name', 'Screen Name', 'min_length[4]|max_length[25]');
  109.             $this->form_validation->set_rules('password', 'Password', 'required|min_length[5]|matches[pass_conf]');
  110.             $this->form_validation->set_rules('pass_conf', 'Password Confirmation', 'required|min_length[5]|');
  111.             $this->form_validation->set_rules('email', 'Email', 'required|valid_email|min_length[5]');
  112.  
  113.             // Run the validation
  114.             if( ! $this->form_validation->run()) {
  115.  
  116.                 // Form
  117.                 $this->load->view('register');
  118.             } else {
  119.  
  120.                 // Get the data
  121.                 $username    = $this->input->post('username');
  122.                 $password    = sha1($this->input->post('password'));
  123.                 $email       = $this->input->post('email');
  124.                 $screen_name = $this->input->post('screen_name');
  125.  
  126.                 // If the Username, the Screen Name or the Email Address are already in use, display an error message
  127.                 if( ! ($this->users_model->check_field('username', $username)
  128.                     && $this->users_model->check_field('screen_name', $screen_name)
  129.                     && $this->users_model->check_field('email', $email))) {
  130.  
  131.                     // Rebuild the form
  132.                     $data['invalid'] = 'The Username, Screen Name or Email Address are already in use';
  133.                     $this->load->view('register', $data);
  134.                 } else {
  135.  
  136.                     // Add the user
  137.                     $user_id = $this->users_model->add_user($username, $password, $email, $screen_name);
  138.  
  139.                     // Display registration link
  140.                     $registration_url = base_url() . 'user/activate/' . sha1($user_id);
  141.                     $data['success'] = 'Congratulations! You are now registered in our database,
  142.                                        but in order to start using your account,
  143.                                        you will need to activate it by pressing the link below:
  144.                                        ' . "\n\n" .
  145.                                         '<a href="' . $registration_url . '">' . $registration_url . '</a>';
  146.                     $this->load->view('message', $data);
  147.                 }
  148.             }
  149.  
  150.             $this->load->view('footer');
  151.         } else {
  152.  
  153.             redirect('/');
  154.         }
  155.     }
  156.  
  157.     public function activate($user_id) {
  158.  
  159.         $result = $this->users_model->activate_account($user_id);
  160.  
  161.         if($result == 1) {
  162.  
  163.             $data['error'] = 'This account is already activated!';
  164.             $this->load->view('message', $data);
  165.         } else if($result == 2) {
  166.  
  167.             $data['error'] = 'The requested account could not be found!';
  168.             $this->load->view('message', $data);
  169.         } else {
  170.  
  171.             $data['success'] = 'The account has been successfully activated! <a href="' . site_url('user/authenticate') . '">Click here to authenticate</a>!';
  172.             $this->load->view('message', $data);
  173.         }
  174.     }
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement