2020-02-09 23:02:19 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Objects;
|
|
|
|
|
2020-04-03 15:56:04 +02:00
|
|
|
use DateTime;
|
2020-04-02 00:02:51 +02:00
|
|
|
use \Driver\SQL\Condition\Compare;
|
2020-04-03 15:56:04 +02:00
|
|
|
use Exception;
|
|
|
|
use External\JWT;
|
2020-04-02 00:02:51 +02:00
|
|
|
|
2020-02-09 23:02:19 +01:00
|
|
|
class Session extends ApiObject {
|
|
|
|
|
2020-04-04 01:15:59 +02:00
|
|
|
# in minutes
|
|
|
|
const DURATION = 60*24;
|
2020-02-09 23:02:19 +01:00
|
|
|
|
2020-04-03 15:56:04 +02:00
|
|
|
private ?int $sessionId;
|
|
|
|
private User $user;
|
|
|
|
private int $expires;
|
|
|
|
private string $ipAddress;
|
|
|
|
private ?string $os;
|
|
|
|
private ?string $browser;
|
|
|
|
private bool $stayLoggedIn;
|
2020-02-09 23:02:19 +01:00
|
|
|
|
2020-04-03 15:56:04 +02:00
|
|
|
public function __construct(User $user, ?int $sessionId) {
|
2020-02-09 23:02:19 +01:00
|
|
|
$this->user = $user;
|
|
|
|
$this->sessionId = $sessionId;
|
2020-02-17 00:18:37 +01:00
|
|
|
$this->stayLoggedIn = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function create($user, $stayLoggedIn) {
|
|
|
|
$session = new Session($user, null);
|
|
|
|
if($session->insert($stayLoggedIn)) {
|
|
|
|
return $session;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
2020-02-09 23:02:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private function updateMetaData() {
|
|
|
|
$this->expires = time() + Session::DURATION * 60;
|
|
|
|
$this->ipAddress = $_SERVER['REMOTE_ADDR'];
|
2020-02-10 00:52:25 +01:00
|
|
|
try {
|
|
|
|
$userAgent = @get_browser($_SERVER['HTTP_USER_AGENT'], true);
|
|
|
|
$this->os = $userAgent['platform'] ?? "Unknown";
|
|
|
|
$this->browser = $userAgent['parent'] ?? "Unknown";
|
2020-04-03 15:56:04 +02:00
|
|
|
} catch(Exception $ex) {
|
2020-02-10 00:52:25 +01:00
|
|
|
$this->os = "Unknown";
|
|
|
|
$this->browser = "Unknown";
|
|
|
|
}
|
2020-02-09 23:02:19 +01:00
|
|
|
}
|
|
|
|
|
2020-02-17 00:18:37 +01:00
|
|
|
public function setData($data) {
|
|
|
|
foreach($data as $key => $value) {
|
|
|
|
$_SESSION[$key] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function stayLoggedIn($val) {
|
|
|
|
$this->stayLoggedIn = $val;
|
|
|
|
}
|
|
|
|
|
2020-02-09 23:02:19 +01:00
|
|
|
public function sendCookie() {
|
|
|
|
$this->updateMetaData();
|
2020-02-10 00:52:25 +01:00
|
|
|
$jwt = $this->user->getConfiguration()->getJwt();
|
|
|
|
if($jwt) {
|
|
|
|
$token = array('userId' => $this->user->getId(), 'sessionId' => $this->sessionId);
|
2020-04-03 15:56:04 +02:00
|
|
|
$sessionCookie = JWT::encode($token, $jwt->getKey());
|
2020-02-10 00:52:25 +01:00
|
|
|
$secure = strcmp(getProtocol(), "https") === 0;
|
2020-02-17 00:18:37 +01:00
|
|
|
setcookie('session', $sessionCookie, $this->getExpiresTime(), "/", "", $secure);
|
2020-02-10 00:52:25 +01:00
|
|
|
}
|
2020-02-09 23:02:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getExpiresTime() {
|
2020-02-17 00:18:37 +01:00
|
|
|
return ($this->stayLoggedIn == 0 ? 0 : $this->expires);
|
2020-02-09 23:02:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getExpiresSeconds() {
|
2020-02-17 00:18:37 +01:00
|
|
|
return ($this->stayLoggedIn == 0 ? -1 : $this->expires - time());
|
2020-02-09 23:02:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function jsonSerialize() {
|
|
|
|
return array(
|
|
|
|
'uid' => $this->sessionId,
|
2020-02-17 00:18:37 +01:00
|
|
|
'user_id' => $this->user->getId(),
|
2020-02-09 23:02:19 +01:00
|
|
|
'expires' => $this->expires,
|
|
|
|
'ipAddress' => $this->ipAddress,
|
|
|
|
'os' => $this->os,
|
|
|
|
'browser' => $this->browser,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-02-17 00:18:37 +01:00
|
|
|
public function insert($stayLoggedIn) {
|
2020-02-09 23:02:19 +01:00
|
|
|
$this->updateMetaData();
|
2020-04-02 00:02:51 +02:00
|
|
|
$sql = $this->user->getSQL();
|
|
|
|
|
2020-04-04 01:15:59 +02:00
|
|
|
$minutes = Session::DURATION;
|
2020-04-02 00:02:51 +02:00
|
|
|
$columns = array("expires", "user_id", "ipAddress", "os", "browser", "data", "stay_logged_in");
|
|
|
|
|
|
|
|
$success = $sql
|
|
|
|
->insert("Session", $columns)
|
|
|
|
->addRow(
|
2020-04-04 01:15:59 +02:00
|
|
|
(new DateTime())->modify("+$minutes minute"),
|
2020-04-02 00:02:51 +02:00
|
|
|
$this->user->getId(),
|
|
|
|
$this->ipAddress,
|
|
|
|
$this->os,
|
|
|
|
$this->browser,
|
|
|
|
json_encode($_SESSION),
|
|
|
|
$stayLoggedIn)
|
2020-04-02 01:48:46 +02:00
|
|
|
->returning("uid")
|
2020-04-02 00:02:51 +02:00
|
|
|
->execute();
|
2020-02-09 23:02:19 +01:00
|
|
|
|
|
|
|
if($success) {
|
|
|
|
$this->sessionId = $this->user->getSQL()->getLastInsertId();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function destroy() {
|
2020-04-03 15:56:04 +02:00
|
|
|
return $this->user->getSQL()->update("Session")
|
2020-04-02 00:02:51 +02:00
|
|
|
->set("active", false)
|
|
|
|
->where(new Compare("Session.uid", $this->sessionId))
|
|
|
|
->where(new Compare("Session.user_id", $this->user->getId()))
|
|
|
|
->execute();
|
2020-02-09 23:02:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function update() {
|
|
|
|
$this->updateMetaData();
|
2020-04-04 01:15:59 +02:00
|
|
|
$minutes = Session::DURATION;
|
2020-04-02 00:02:51 +02:00
|
|
|
|
|
|
|
$sql = $this->user->getSQL();
|
2020-04-03 15:56:04 +02:00
|
|
|
return $sql->update("Session")
|
2020-04-04 01:15:59 +02:00
|
|
|
->set("Session.expires", (new DateTime())->modify("+$minutes minute"))
|
2020-04-02 00:02:51 +02:00
|
|
|
->set("Session.ipAddress", $this->ipAddress)
|
|
|
|
->set("Session.os", $this->os)
|
|
|
|
->set("Session.browser", $this->browser)
|
|
|
|
->set("Session.data", json_encode($_SESSION))
|
|
|
|
->where(new Compare("Session.uid", $this->sessionId))
|
|
|
|
->where(new Compare("Session.user_id", $this->user->getId()))
|
|
|
|
->execute();
|
2020-02-09 23:02:19 +01:00
|
|
|
}
|
|
|
|
}
|