Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class User {
- const SESSION_NAME = "user";
- const COOKIE_NAME = "hash";
- const COOKIE_EXPIRY = 604800;
- /**
- * @var object $data
- */
- private $data;
- /**
- * @var object
- */
- private $db;
- private $isLoggedIn = false;
- public function __construct($user = null) {
- $this->db = App::getDatabase();
- if (!$user) {
- if (Session::has(self::SESSION_NAME)) {
- $user = Session::get(self::SESSION_NAME);
- if ($this->find($user)) {
- $this->isLoggedIn = true;
- } else {
- $this->logout();
- }
- }
- } else {
- $this->find($user);
- }
- }
- /**
- * Retrieves User Data from Database and sotres it into $this->data
- *
- * @param type $id
- * @throws Exception
- */
- public function find($user = null) {
- if ($user) {
- if (!is_numeric($user)) {
- throw new Exception('User value was not an integer: ' . $user);
- }
- $query = App::getQBuilder()->flush()->select()->from("YRP_Spieler")->where("id = :value");
- $query_result = $this->db->query($query, array(":value" => $user));
- if ($query_result->count()) {
- $this->data = $query_result->result(0);
- return true;
- }
- }
- return false;
- }
- /**
- * Checks if user exists
- *
- * @return boolean
- */
- public function exists() {
- return (!empty($this->data)) ? true : false;
- }
- private function createSession() {
- $query = App::getQBuilder()->flush()->deleteFrom("UCP_Spieler_Session")->where("userID = :id")->orWhere("sessionID = :sid");
- App::getDatabase()->query($query, array(":id" => $this->data()->id, ":sid" => session_id()));
- $query = App::getQBuilder()->flush()->insertInto("UCP_Spieler_Session", array("sessionID" => ":sid", "userID" => ":uid", "ipAddress" => ":ip", "userAgent" => ":uagent", "lastAcitivity" => time()));
- App::getDatabase()->query($query, array(
- ":sid" => session_id(),
- ":uid" => $this->data()->id,
- ":ip" => $_SERVER['REMOTE_ADDR'],
- ":uagent" => $_SERVER['HTTP_USER_AGENT']
- ));
- return true;
- }
- public function updateActivity() {
- if(!$this->isLoggedIn()) {
- return false;
- }
- $query = App::getQBuilder()->flush()->update("UCP_Spieler_Session", array("lastAcitivity" => time()))->where("sessionID = :sid")->limit(1);
- App::getDatabase()->query($query, array(":sid" => session_id()));
- }
- public function getActiveUsers() {
- $query = App::getQBuilder()->flush()->select()->from("UCP_Spieler_Session")->where("lastAcitivity >= :time");
- $result = App::getDatabase()->query($query, array(":time" => time()-900));
- $return["count"] = $result->count();
- $return["users"] = array();
- foreach ($result->result() as $user) {
- $userquery = App::getQBuilder()->flush()->select()->from("YRP_Spieler")->where("id = :id")->limit(1);
- $userresult = App::getDatabase()->query($userquery, array(":id" => $user->userID))->result(0);
- array_push($return["users"], $userresult->Vorname . " " . $userresult->Nachname);
- }
- return $return;
- }
- /**
- * Logs the User in
- *
- * @param string $username
- * @param string $password
- * @param boolean $remember
- * @return boolean
- */
- public function login($username = null, $password = null, $remember = false) {
- if (!$username && !$password && $this->exists()) {
- Session::put(self::SESSION_NAME, $this->data()->id);
- $this->createSession();
- $this->isLoggedIn = true;
- } else {
- $username = explode('_', $username);
- if (count($username) != 2) {
- return false;
- }
- $query = App::getQBuilder()->flush()->select()->from('YRP_Spieler')->where('Vorname = :pname')->andWhere('Nachname = :nname');
- $result = $this->db->query($query, array(":pname" => $username[0], ":nname" => $username[1]));
- if ($result->count() == 0) {
- return false;
- }
- if ($this->find($result->result(0)->id)) {
- if ($this->data()->Passwort == strtoupper(md5($password))) {
- Session::put(self::SESSION_NAME, $this->data()->id);
- $this->createSession();
- $this->isLoggedIn = true;
- if ($remember === true) {
- $hash = Hash::unique();
- $hash_check["query"] = App::getQBuilder()->flush()->select()->from("UCP_Spieler_Remember")->where("SpielerID = :id");
- $hash_check["result"] = $this->db->query($hash_check["query"], array(":id" => $this->data()->id));
- if ($hash_check["result"]->count() === 0) {
- $hash_put["query"] = App::getQBuilder()->flush()->insertInto("UCP_Spieler_Remember", array("SpielerID" => ":id", "Token" => ":hash"));
- $hash_put["result"] = $this->db->query($hash_put["query"], array(":id" => $this->data()->id, ":hash" => $hash));
- } else {
- $hash = $hash_check["result"]->result(0)->Token;
- }
- Cookie::put(self::COOKIE_NAME, $hash, self::COOKIE_EXPIRY);
- }
- return true;
- }
- }
- }
- return false;
- }
- /**
- * Logs a User out
- *
- * @return boolean
- */
- public function logout() {
- if (Cookie::exists(self::COOKIE_NAME)) {
- Cookie::delete(self::COOKIE_NAME);
- $query = App::getQBuilder()->flush()->deleteFrom("UCP_Spieler_Remember")->where("SpielerID = :id");
- $this->db->query($query, array(":id" => $this->data()->id));
- }
- $query = App::getQBuilder()->flush()->deleteFrom("UCP_Spieler_Session")->where("userID = :id")->orWhere("sessionID = :sid");
- App::getDatabase()->query($query, array(":id" => $this->data()->id, ":sid" => session_id()));
- Session::forget(self::SESSION_NAME);
- return true;
- }
- /**
- * Returns User Data
- *
- * @return type
- */
- public function data() {
- return $this->data;
- }
- /**
- * returns if a user is logged in
- *
- * @return type
- */
- public function isLoggedIn() {
- return $this->isLoggedIn;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement