SHARE
TWEET

Customer Library

wolfgang1983 Jul 29th, 2016 3 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. class Customer {
  4.  
  5.     protected $customer_id;
  6.  
  7.     public function __construct() {
  8.         $this->ci =& get_instance();
  9.         $this->ci->load->library('session');
  10.         $this->ci->load->helper('string');
  11.  
  12.         if ($this->ci->session->userdata('customer_id')) {
  13.  
  14.             $this->ci->db->select('*');
  15.             $this->ci->db->from($this->ci->db->dbprefix . 'customer');
  16.             $this->ci->db->where('customer_id', $this->ci->session->userdata('customer_id'));
  17.             $customer_query = $this->ci->db->get();
  18.  
  19.             if ($customer_query->num_rows()) {
  20.  
  21.                 $this->customer_id = $customer_query->row()->customer_id;
  22.            
  23.             }
  24.  
  25.         } else {
  26.  
  27.             $this->autologin();
  28.         }
  29.  
  30.     }
  31.  
  32.     public function postLogin() {
  33.         if ($this->verifyPassword() == true) {
  34.  
  35.             $this->ci->db->select('*');
  36.             $this->ci->db->from($this->ci->db->dbprefix . 'customer');
  37.             $this->ci->db->where('username', $this->ci->input->post('username'));
  38.             $customer_query = $this->ci->db->get();
  39.  
  40.             if ($customer_query->num_rows() > 0) {
  41.  
  42.                 $data = array(
  43.                     'customer_id' => $customer_query->row()->customer_id,
  44.                     'is_logged_in' => true
  45.                 );
  46.  
  47.                 $this->ci->session->set_userdata($data);
  48.  
  49.                 if ($this->ci->input->post('remember')) {
  50.                     $this->createAutologin($customer_query->row()->customer_id);
  51.                 }
  52.  
  53.                 return true;
  54.            
  55.             } else {
  56.  
  57.                 return false;
  58.             }
  59.  
  60.         }
  61.     }
  62.  
  63.     protected function verifyPassword() {
  64.         $input = $this->ci->input->post('password');
  65.         $hashed = $this->getPassword();
  66.  
  67.         if (password_verify($input, $hashed)) {
  68.             return true;
  69.         } else {
  70.             return false;
  71.         }
  72.     }
  73.  
  74.     protected function getPassword() {
  75.         $this->ci->db->select('*');
  76.         $this->ci->db->from($this->ci->db->dbprefix . 'customer');
  77.         $this->ci->db->where('username', $this->ci->input->post('username'));
  78.         $customer_query = $this->ci->db->get();
  79.  
  80.         if ($customer_query->num_rows() > 0) {
  81.  
  82.             return $customer_query->row()->password;
  83.        
  84.         } else {
  85.  
  86.             return false;
  87.         }
  88.     }
  89.  
  90.     public function autologin() {
  91.         var_dump($this->readAutologin());
  92.     }
  93.  
  94.     protected function createAutologin($customer_id) {
  95.         $token = random_string('alnum', 32);
  96.  
  97.         $data = array(
  98.             'token' => $token
  99.         );
  100.  
  101.         $this->ci->db->where('customer_id', $customer_id);
  102.         $customer_update = $this->ci->db->update('customer', $data);
  103.  
  104.         if ($customer_update) {
  105.  
  106.             $data = array(
  107.                 'customer_id' => $customer_id,
  108.                 'token' => $token,
  109.                 'time' => time()
  110.             );
  111.  
  112.             $customer_autologin_insert = $this->ci->db->insert('customer_autologin', $data);
  113.  
  114.             if ($customer_autologin_insert) {
  115.  
  116.                 setcookie('remember', $token, $this->ci->config->item('start_cookie_time'), '/', '.localhost', false, true);
  117.             }
  118.         }
  119.  
  120.     }
  121.  
  122.     protected function readAutologin() {
  123.         $this->ci->db->select('*');
  124.         $this->ci->db->from($this->ci->db->dbprefix . 'customer_autologin');
  125.         $this->ci->db->where('customer_id', $this->customer_id);
  126.         $customer_autoload_query = $this->ci->db->get();
  127.  
  128.         if ($customer_autoload_query->num_rows() > 0) {
  129.             return $customer_autoload_query->row_array();
  130.         } else {
  131.             return false;
  132.         }
  133.     }
  134. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top