2016-01-15 03:45:22 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class Session {
|
2016-04-05 01:00:39 +02:00
|
|
|
static $instance = null;
|
2017-06-20 20:52:16 +02:00
|
|
|
private $sessionPrefix = '';
|
|
|
|
|
2016-04-05 01:00:39 +02:00
|
|
|
private function __construct() {
|
|
|
|
$this->initSession();
|
|
|
|
}
|
2016-01-15 03:45:22 +01:00
|
|
|
|
|
|
|
public function initSession() {
|
2016-04-29 02:20:42 +02:00
|
|
|
session_cache_limiter(false);
|
2016-01-15 03:45:22 +01:00
|
|
|
session_start();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function closeSession() {
|
|
|
|
session_destroy();
|
|
|
|
}
|
|
|
|
|
2016-04-05 01:00:39 +02:00
|
|
|
public static function getInstance() {
|
|
|
|
if (!self::$instance) {
|
|
|
|
self::$instance = new Session();
|
2016-01-15 03:45:22 +01:00
|
|
|
}
|
|
|
|
|
2016-04-05 01:00:39 +02:00
|
|
|
return self::$instance;
|
2016-01-15 03:45:22 +01:00
|
|
|
}
|
|
|
|
|
2016-09-25 06:16:10 +02:00
|
|
|
public function createSession($userId, $staff = false) {
|
2016-02-02 22:41:10 +01:00
|
|
|
$this->store('userId', $userId);
|
2016-09-25 06:16:10 +02:00
|
|
|
$this->store('staff', $staff);
|
2016-08-04 20:18:29 +02:00
|
|
|
$this->store('token', Hashing::generateRandomToken());
|
2016-01-15 03:45:22 +01:00
|
|
|
}
|
2017-01-21 05:17:28 +01:00
|
|
|
|
|
|
|
public function createTicketSession($ticketNumber) {
|
|
|
|
$this->store('ticketNumber', $ticketNumber);
|
|
|
|
$this->store('token', Hashing::generateRandomToken());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getTicketNumber() {
|
|
|
|
return $this->getStoredData('ticketNumber');
|
|
|
|
}
|
2016-01-15 03:45:22 +01:00
|
|
|
|
2017-02-18 19:28:23 +01:00
|
|
|
public function getUserId() {
|
|
|
|
return $this->getStoredData('userId');
|
|
|
|
}
|
|
|
|
|
2016-01-15 03:45:22 +01:00
|
|
|
public function getToken() {
|
|
|
|
return $this->getStoredData('token');
|
|
|
|
}
|
|
|
|
|
2016-02-02 22:41:10 +01:00
|
|
|
public function sessionExists() {
|
|
|
|
return !!$this->getToken();
|
|
|
|
}
|
|
|
|
|
2016-09-25 06:16:10 +02:00
|
|
|
public function isStaffLogged() {
|
|
|
|
return $this->getStoredData('staff');
|
|
|
|
}
|
|
|
|
|
2016-01-15 03:45:22 +01:00
|
|
|
public function checkAuthentication($data) {
|
2016-07-04 20:57:00 +02:00
|
|
|
$userId = $this->getStoredData('userId');
|
|
|
|
$token = $this->getStoredData('token');
|
|
|
|
|
|
|
|
return $userId && $token &&
|
|
|
|
$userId === $data['userId'] &&
|
|
|
|
$token === $data['token'];
|
2016-01-15 03:45:22 +01:00
|
|
|
}
|
|
|
|
|
2017-01-21 05:17:28 +01:00
|
|
|
public function store($key, $value) {
|
2017-06-20 20:52:16 +02:00
|
|
|
$_SESSION[$this->sessionPrefix . $key] = $value;
|
2016-01-15 03:45:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private function getStoredData($key) {
|
2016-04-05 01:00:39 +02:00
|
|
|
$storedValue = null;
|
|
|
|
|
2017-06-20 20:52:16 +02:00
|
|
|
if (array_key_exists($this->sessionPrefix . $key, $_SESSION)) {
|
|
|
|
$storedValue = $_SESSION[$this->sessionPrefix . $key];
|
2016-04-05 01:00:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $storedValue;
|
2016-01-15 03:45:22 +01:00
|
|
|
}
|
2016-08-04 20:18:29 +02:00
|
|
|
|
|
|
|
public function isLoggedWithId($userId) {
|
|
|
|
return ($this->getStoredData('userId') === $userId);
|
2016-01-15 03:45:22 +01:00
|
|
|
}
|
2017-06-20 20:52:16 +02:00
|
|
|
|
|
|
|
public function setSessionPrefix($prefix) {
|
|
|
|
$this->sessionPrefix = $prefix;
|
|
|
|
}
|
2016-01-15 03:45:22 +01:00
|
|
|
}
|