Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.39 KB | None | 0 0
  1. <?php
  2. class Dubauth
  3. {
  4.     private $error = array();
  5.    
  6.     function __construct()
  7.     {
  8.         $this->ci =& get_instance();
  9.         $this->ci->load->library('session');
  10.         $this->ci->load->model('user');
  11.         $this->ci->load->helper('string');
  12.     }
  13.    
  14.     function login($username, $password)
  15.     {
  16.         if(!($user = $this->ci->user->login_username($username)))
  17.         {
  18.             $this->error = 'invalid_user_pass';
  19.             return false;
  20.         }
  21.         elseif($user->password != $password)
  22.         {
  23.             $this->error = 'invalid_user_pass';
  24.             return false;
  25.         }
  26.         elseif($user->active == 0)
  27.         {
  28.             $this->error = 'account_not_verified';
  29.             return false;
  30.         }
  31.         elseif($user->banned == 1)
  32.         {
  33.             $this->error = 'account_banned';
  34.             return false;
  35.         }
  36.         else
  37.         {
  38.             $this->ci->session->set_userdata(array(
  39.                 'user_id'   => $user->user_id,
  40.                 'username'  => $user->username
  41.             ));
  42.             return true;
  43.         }
  44.     }
  45.    
  46.     function verify($key)
  47.     {
  48.         if(!($user_id = $this->ci->user->is_key_valid($key)))
  49.         {
  50.             $this->error = 'invalid_key';
  51.             return false;
  52.         }
  53.         else
  54.         {
  55.             $this->ci->user->validate($user_id, $key);
  56.             return true;   
  57.         }
  58.     }
  59.    
  60.     function register($username, $email, $password)
  61.     {
  62.         if($this->ci->user->is_username_registered($username))
  63.         {
  64.             $this->error = 'username_registered';
  65.             return false;
  66.         }
  67.         elseif($this->ci->user->is_email_registered($email))
  68.         {
  69.             $this->error = 'email_registered';
  70.             return false;
  71.         }
  72.         else
  73.         {
  74.             $array = array(
  75.                 'username'  => $this->ci->input->post('username'),
  76.                 'email'     => $this->ci->input->post('email'),
  77.                 'password'  => md5($this->ci->input->post('password'))
  78.             );
  79.            
  80.             $this->ci->db->insert('users', $array);
  81.            
  82.             $array = array(
  83.                 'user_id' => $this->ci->db->insert_id(),
  84.                 'key'     => random_string('unique')
  85.             );
  86.            
  87.             $this->ci->db->insert('user_verify_keys', $array);
  88.            
  89.             return true;   
  90.         }
  91.     }
  92.    
  93.     function logout()
  94.     {
  95.         $this->ci->session->sess_destroy();
  96.     }
  97.    
  98.     function is_logged_in()
  99.     {
  100.         $user_id = $this->ci->session->userdata('user_id');
  101.         if(!isset($user_id))
  102.         {
  103.             return false;
  104.         }
  105.         else
  106.         {
  107.             $this->ci->db->select('user_id');
  108.             $this->ci->db->where('user_id', $this->ci->session->userdata('user_id'));
  109.             $query = $this->ci->db->get('users');
  110.             if($query->num_rows() == 0)
  111.             {
  112.                 return false;
  113.             }
  114.             else
  115.             {
  116.                 return true;   
  117.             }
  118.         }
  119.     }
  120.    
  121.     function get_error_message()
  122.     {
  123.         return $this->error;
  124.     }
  125. }
  126. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement