Advertisement
Guest User

Untitled

a guest
Apr 7th, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.68 KB | None | 0 0
  1. <?php
  2. class User {
  3.     private $_db,
  4.             $_data,
  5.             $_sessionName,
  6.             $_isLoggedIn,
  7.             $_cookieName;
  8.  
  9.     public function __construct($user = null) {
  10.         $this->_db = DB::getInstance();
  11.         $this->_sessionName = Config::get('session/session_name');
  12.         $this->_cookieName  = Config::get('remember/cookie_name');
  13.  
  14.         if (!$user) {
  15.             if (Session::exists($this->_sessionName)) {
  16.                 $user = Session::get($this->_sessionName);
  17.  
  18.                 if ($this->find($user)) {
  19.                     $this->_isLoggedIn = true;
  20.                 } else {
  21.                     //process logout
  22.                 }
  23.             }
  24.         } else {
  25.             $this->find($user);
  26.         }
  27.     }
  28.  
  29.     public function create($fields = array()) {
  30.  
  31.         if (!$this->_db->insert('users', $fields)) {
  32.             throw new Exception('There was a problem creating an account');
  33.         }
  34.     }
  35.  
  36.     public function update($fields = array(), $id = null) {
  37.  
  38.         if (!$id && $this->isLoggedIn()) {
  39.             $id = $this->data()->id;
  40.         }
  41.  
  42.         if (!$this->_db->update('users', $id, $fields)) {
  43.             throw new Exception("There was a problem updatetin");
  44.         }
  45.     }
  46.  
  47.     public function login($username = null, $password = null, $remember = false) {
  48.  
  49.         if (!$username && !$password && $this->exists()) {
  50.             Session::put($this->_sessionName, $this->data()->id);
  51.  
  52.         } else {
  53.             $user = $this->find($username);
  54.  
  55.             if ($user) {
  56.                 if ($this->data()->password === Hash::make($password, $this->data()->salt)) {
  57.                     Session::put($this->_sessionName, $this->data()->id);
  58.  
  59.                     if ($remember) {
  60.                         $hash = Hash::unique();
  61.                         $hashCheck = $this->_db->get('users_session', array('user_id', '=', $this->data()->id ));
  62.  
  63.                         if (!$hashCheck->count()) {
  64.                             $this->_db->insert('users_session', array(
  65.                                     'user_id' => $this->data()->id,
  66.                                     'hash' => $hash
  67.                                 ));
  68.                         } else {
  69.                             $hash = $hashCheck->first()->hash;
  70.                         }
  71.  
  72.                         Cookie::put($this->_cookieName, $hash, Config::get('remember/cookie_expiry'));
  73.  
  74.                     }
  75.  
  76.                     return true;
  77.                 }
  78.             }
  79.         }
  80.         return false;
  81.     }
  82.  
  83.     public function exists() {
  84.         return (!empty($this->_data)) ? true : false;
  85.     }
  86.  
  87.     public function find($user = null) {
  88.         if ($user) {
  89.             $field = (is_numeric($user)) ? 'id' : 'username';
  90.             $data = $this->_db->get('users', array($field, '=', $user));
  91.  
  92.             if ($data->count()) {
  93.                 $this->_data = $data->first();
  94.                 return true;
  95.             }
  96.         }
  97.         return false;
  98.     }
  99.  
  100.     public function isLoggedIn() {
  101.         return $this->_isLoggedIn;
  102.     }
  103.  
  104.     public function logout() {
  105.         $this->_db->delete('users_session', array('user_id', '=', $this->data()->id));
  106.  
  107.         Session::delete($this->_sessionName);
  108.         Cookie::delete($this->_cookieName);
  109.     }
  110.  
  111.     public function data() {
  112.         return $this->_data;
  113.     }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement