Advertisement
Guest User

Auth.php

a guest
Apr 1st, 2019
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.43 KB | None | 0 0
  1. <?php
  2. defined('BASEPATH') or exit('No direct script access allowed');
  3.  
  4. class Auth extends CI_Controller
  5. {
  6.     public function __construct()
  7.     {
  8.         parent::__construct();
  9.         $this->load->library('form_validation');
  10.     }
  11.  
  12.     public function index()
  13.     {
  14.         if ($this->session->userdata('email')) {
  15.             redirect('user');
  16.         }
  17.  
  18.         $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
  19.         $this->form_validation->set_rules('password', 'Password', 'trim|required');
  20.  
  21.         if ($this->form_validation->run() == false) {
  22.             $data['tittle'] = 'WPU user login';
  23.             $this->load->view('templates/auth_header', $data);
  24.             $this->load->view('auth/login');
  25.             $this->load->view('templates/auth_footer');
  26.         } else {
  27.             $this->_login();
  28.         }
  29.     }
  30.  
  31.     private function _login()
  32.     {
  33.         $email = $this->input->post('email', true);
  34.         $password = $this->input->post('password', true);
  35.  
  36.         $user = $this->db->get_where('user', ['email' => $email])->row_array();
  37.  
  38.         // jika user ada
  39.         if ($user) {
  40.             // jika user aktif
  41.             if ($user['is_active'] == 1) {
  42.                 // cek password
  43.                 if (password_verify($password, $user['password'])) {
  44.                     $data = [
  45.                         'email' => $user['email'],
  46.                         'role_id' => $user['role_id']
  47.                     ];
  48.                     $this->session->set_userdata($data);
  49.                     if ($user['role_id'] == 1) {
  50.                         redirect('admin');
  51.                     } else {
  52.                         redirect('user');
  53.                     }
  54.                 } else {
  55.                     $this->session->set_flashdata('message', '<div class="alert alert-danger" role="alert">Wrong password!</div>');
  56.                     redirect('auth');
  57.                 }
  58.             } else {
  59.                 $this->session->set_flashdata('message', '<div class="alert alert-danger" role="alert">This email has been not activated!</div>');
  60.                 redirect('auth');
  61.             }
  62.         } else {
  63.             $this->session->set_flashdata('message', '<div class="alert alert-danger" role="alert">Email has not been registered!</div>');
  64.             redirect('auth');
  65.         }
  66.     }
  67.  
  68.     public function register()
  69.     {
  70.         if ($this->session->userdata('email')) {
  71.             redirect('user');
  72.         }
  73.  
  74.         $this->form_validation->set_rules('name', 'Name', 'required|trim');
  75.         $this->form_validation->set_rules('email', 'Email', 'required|trim|valid_email|is_unique[user.email]', [
  76.             'is_unique' => 'This email has alredy register!'
  77.         ]);
  78.         $this->form_validation->set_rules('password1', 'Password', 'required|trim|min_length[3]|matches[password2]', [
  79.             'matches' => 'The Password field does not match!'
  80.         ]);
  81.         $this->form_validation->set_rules('password2', 'Password', 'required|trim|matches[password1]');
  82.  
  83.         if ($this->form_validation->run() == false) {
  84.             $data['tittle'] = 'WPU user registration';
  85.             $this->load->view('templates/auth_header', $data);
  86.             $this->load->view('auth/register');
  87.             $this->load->view('templates/auth_footer');
  88.         } else {
  89.             $data = [
  90.                 'name' => htmlspecialchars($this->input->post('name', true)),
  91.                 'email' => htmlspecialchars($this->input->post('email', true)),
  92.                 'image' => 'default.jpg',
  93.                 'password' => password_hash($this->input->post('password1'), PASSWORD_DEFAULT),
  94.                 'role_id' => 2,
  95.                 'is_active' => 0,
  96.                 'date_created' => time()
  97.             ];
  98.  
  99.             // $this->db->insert('user', $data);
  100.  
  101.             $this->_sendEmail();
  102.  
  103.             $this->session->set_flashdata('message', '<div class="alert alert-success" role="alert">Your account has been created</div>');
  104.             redirect('auth');
  105.         }
  106.     }
  107.     private function _sendEmail()
  108.     {
  109.         $config = [
  110.             'protocol'  => 'smtp',
  111.             'smtp_host' => 'ssl://smpt.googlemail.com',
  112.             'smtp_user' => 'wpu.login@gmail.com',
  113.             'smtp_pass' => 'Avenged@23',
  114.             'smtp_port' => 465,
  115.             'mailtype'  => 'html',
  116.             'charset'   => 'utf-8',
  117.             'newline'   => "\r\n"
  118.         ];
  119.  
  120.         $this->load->library('email', $config);
  121.  
  122.         $this->email->from('wpu.login@gmail.com', 'Wep Programing UNPAS');
  123.         $this->email->to('arifintajul4@gmail.com');
  124.         $this->email->subject('Testing');
  125.         $this->email->message('hello world');
  126.  
  127.         if ($this->email->send()) {
  128.             return true;
  129.         } else {
  130.             echo $this->email->print_debugger();
  131.             die;
  132.         }
  133.     }
  134.  
  135.     public function logout()
  136.     {
  137.         $this->session->unset_userdata('email');
  138.         $this->session->unset_userdata('role_id');
  139.  
  140.         $this->session->set_flashdata('message', '<div class="alert alert-success" role="alert">You have been logged out!</div>');
  141.         redirect('auth');
  142.     }
  143.  
  144.     public function blocked()
  145.     {
  146.         $data['tittle'] = 'Access Blocked';
  147.         $this->load->view('templates/header', $data);
  148.         $this->load->view('auth/blocked');
  149.         $this->load->view('templates/footer', $data);
  150.     }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement