opensupports/server/models/Session.php

89 lines
2.2 KiB
PHP
Raw Normal View History

<?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();
}
public function initSession() {
2016-04-29 02:20:42 +02:00
session_cache_limiter(false);
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-04-05 01:00:39 +02:00
return self::$instance;
}
public function createSession($userId, $staff = false) {
$this->store('userId', $userId);
$this->store('staff', $staff);
$this->store('token', Hashing::generateRandomToken());
}
public function createTicketSession($ticketNumber) {
$this->store('ticketNumber', $ticketNumber);
$this->store('token', Hashing::generateRandomToken());
}
public function getTicketNumber() {
return $this->getStoredData('ticketNumber');
}
2017-02-18 19:28:23 +01:00
public function getUserId() {
return $this->getStoredData('userId');
}
public function getToken() {
return $this->getStoredData('token');
}
public function sessionExists() {
return !!$this->getToken();
}
public function isStaffLogged() {
return $this->getStoredData('staff');
}
public function checkAuthentication($data) {
$userId = $this->getStoredData('userId');
$token = $this->getStoredData('token');
return $userId && $token &&
$userId === $data['userId'] &&
$token === $data['token'];
}
public function store($key, $value) {
2017-06-20 20:52:16 +02:00
$_SESSION[$this->sessionPrefix . $key] = $value;
}
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;
}
public function isLoggedWithId($userId) {
return ($this->getStoredData('userId') === $userId);
}
2017-06-20 20:52:16 +02:00
public function setSessionPrefix($prefix) {
$this->sessionPrefix = $prefix;
}
}