Advertisement
Guest User

user.class.php

a guest
Jul 22nd, 2015
313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.91 KB | None | 0 0
  1. <?php
  2.  
  3. class User {
  4.  
  5.     const SESSION_NAME = "user";
  6.     const COOKIE_NAME = "hash";
  7.     const COOKIE_EXPIRY = 604800;
  8.  
  9.     /**
  10.      * @var object $data
  11.      */
  12.     private $data;
  13.  
  14.     /**
  15.      * @var object
  16.      */
  17.     private $db;
  18.     private $isLoggedIn = false;
  19.  
  20.     public function __construct($user = null) {
  21.         $this->db = App::getDatabase();
  22.  
  23.         if (!$user) {
  24.             if (Session::has(self::SESSION_NAME)) {
  25.                 $user = Session::get(self::SESSION_NAME);
  26.  
  27.                 if ($this->find($user)) {
  28.                     $this->isLoggedIn = true;
  29.                 } else {
  30.                     $this->logout();
  31.                 }
  32.             }
  33.         } else {
  34.             $this->find($user);
  35.         }
  36.     }
  37.  
  38.     /**
  39.      * Retrieves User Data from Database and sotres it into $this->data
  40.      *
  41.      * @param type $id
  42.      * @throws Exception
  43.      */
  44.     public function find($user = null) {
  45.         if ($user) {
  46.             if (!is_numeric($user)) {
  47.                 throw new Exception('User value was not an integer: ' . $user);
  48.             }
  49.             $query = App::getQBuilder()->flush()->select()->from("YRP_Spieler")->where("id = :value");
  50.             $query_result = $this->db->query($query, array(":value" => $user));
  51.  
  52.             if ($query_result->count()) {
  53.                 $this->data = $query_result->result(0);
  54.                 return true;
  55.             }
  56.         }
  57.         return false;
  58.     }
  59.    
  60.     /**
  61.      * Checks if user exists
  62.      *
  63.      * @return boolean
  64.      */
  65.     public function exists() {
  66.         return (!empty($this->data)) ? true : false;
  67.     }
  68.    
  69.     private function createSession() {
  70.         $query = App::getQBuilder()->flush()->deleteFrom("UCP_Spieler_Session")->where("userID = :id")->orWhere("sessionID = :sid");
  71.         App::getDatabase()->query($query, array(":id" => $this->data()->id, ":sid" => session_id()));
  72.        
  73.         $query = App::getQBuilder()->flush()->insertInto("UCP_Spieler_Session", array("sessionID" => ":sid", "userID" => ":uid", "ipAddress" => ":ip", "userAgent" => ":uagent", "lastAcitivity" => time()));
  74.         App::getDatabase()->query($query, array(
  75.             ":sid" => session_id(),
  76.             ":uid" => $this->data()->id,
  77.             ":ip" => $_SERVER['REMOTE_ADDR'],
  78.             ":uagent" => $_SERVER['HTTP_USER_AGENT']
  79.         ));
  80.        
  81.         return true;
  82.     }
  83.    
  84.     public function updateActivity() {
  85.         if(!$this->isLoggedIn()) {
  86.             return false;
  87.         }
  88.        
  89.         $query = App::getQBuilder()->flush()->update("UCP_Spieler_Session", array("lastAcitivity" => time()))->where("sessionID = :sid")->limit(1);
  90.         App::getDatabase()->query($query, array(":sid" => session_id()));
  91.     }
  92.    
  93.     public function getActiveUsers() {
  94.         $query = App::getQBuilder()->flush()->select()->from("UCP_Spieler_Session")->where("lastAcitivity >= :time");
  95.         $result = App::getDatabase()->query($query, array(":time" => time()-900));
  96.        
  97.         $return["count"] = $result->count();
  98.         $return["users"] = array();
  99.        
  100.         foreach ($result->result() as $user) {
  101.             $userquery = App::getQBuilder()->flush()->select()->from("YRP_Spieler")->where("id = :id")->limit(1);
  102.             $userresult = App::getDatabase()->query($userquery, array(":id" => $user->userID))->result(0);
  103.            
  104.             array_push($return["users"], $userresult->Vorname . " " . $userresult->Nachname);
  105.         }
  106.        
  107.         return $return;
  108.     }
  109.    
  110.     /**
  111.      * Logs the User in
  112.      *
  113.      * @param string $username
  114.      * @param string $password
  115.      * @param boolean $remember
  116.      * @return boolean
  117.      */
  118.     public function login($username = null, $password = null, $remember = false) {
  119.         if (!$username && !$password && $this->exists()) {
  120.             Session::put(self::SESSION_NAME, $this->data()->id);
  121.             $this->createSession();
  122.             $this->isLoggedIn = true;
  123.         } else {
  124.             $username = explode('_', $username);
  125.             if (count($username) != 2) {
  126.                 return false;
  127.             }
  128.            
  129.             $query = App::getQBuilder()->flush()->select()->from('YRP_Spieler')->where('Vorname = :pname')->andWhere('Nachname = :nname');
  130.             $result = $this->db->query($query, array(":pname" => $username[0], ":nname" => $username[1]));
  131.            
  132.             if ($result->count() == 0) {
  133.                 return false;
  134.             }
  135.            
  136.             if ($this->find($result->result(0)->id)) {
  137.                 if ($this->data()->Passwort == strtoupper(md5($password))) {
  138.                     Session::put(self::SESSION_NAME, $this->data()->id);
  139.                     $this->createSession();
  140.                     $this->isLoggedIn = true;
  141.                    
  142.                     if ($remember === true) {
  143.                         $hash = Hash::unique();
  144.                         $hash_check["query"] = App::getQBuilder()->flush()->select()->from("UCP_Spieler_Remember")->where("SpielerID = :id");
  145.                         $hash_check["result"] = $this->db->query($hash_check["query"], array(":id" => $this->data()->id));
  146.  
  147.                         if ($hash_check["result"]->count() === 0) {
  148.                             $hash_put["query"] = App::getQBuilder()->flush()->insertInto("UCP_Spieler_Remember", array("SpielerID" => ":id", "Token" => ":hash"));
  149.                             $hash_put["result"] = $this->db->query($hash_put["query"], array(":id" => $this->data()->id, ":hash" => $hash));
  150.                         } else {
  151.                             $hash = $hash_check["result"]->result(0)->Token;
  152.                         }
  153.  
  154.                         Cookie::put(self::COOKIE_NAME, $hash, self::COOKIE_EXPIRY);
  155.                     }
  156.  
  157.                     return true;
  158.                 }
  159.             }
  160.         }
  161.         return false;
  162.     }
  163.  
  164.     /**
  165.      * Logs a User out
  166.      *
  167.      * @return boolean
  168.      */
  169.     public function logout() {
  170.         if (Cookie::exists(self::COOKIE_NAME)) {
  171.             Cookie::delete(self::COOKIE_NAME);
  172.             $query = App::getQBuilder()->flush()->deleteFrom("UCP_Spieler_Remember")->where("SpielerID = :id");
  173.             $this->db->query($query, array(":id" => $this->data()->id));
  174.         }
  175.        
  176.         $query = App::getQBuilder()->flush()->deleteFrom("UCP_Spieler_Session")->where("userID = :id")->orWhere("sessionID = :sid");
  177.         App::getDatabase()->query($query, array(":id" => $this->data()->id, ":sid" => session_id()));
  178.        
  179.         Session::forget(self::SESSION_NAME);
  180.         return true;
  181.     }
  182.  
  183.     /**
  184.      * Returns User Data
  185.      *
  186.      * @return type
  187.      */
  188.     public function data() {
  189.         return $this->data;
  190.     }
  191.  
  192.     /**
  193.      * returns if a user is logged in
  194.      *
  195.      * @return type
  196.      */
  197.     public function isLoggedIn() {
  198.         return $this->isLoggedIn;
  199.     }
  200.  
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement