web-base/core/Objects/Session.class.php

150 lines
4.0 KiB
PHP
Raw Normal View History

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-06-14 19:39:52 +02:00
private string $csrfToken;
2020-02-09 23:02:19 +01:00
2020-06-14 19:39:52 +02:00
public function __construct(User $user, ?int $sessionId, ?string $csrfToken) {
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;
2020-06-14 19:39:52 +02:00
$this->csrfToken = $csrfToken ?? generateRandomString(16);
2020-02-17 00:18:37 +01:00
}
public static function create($user, $stayLoggedIn) {
2020-06-14 19:39:52 +02:00
$session = new Session($user, null, null);
2020-02-17 00:18:37 +01:00
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-06-25 16:54:58 +02:00
$settings = $this->user->getConfiguration()->getSettings();
$token = array('userId' => $this->user->getId(), 'sessionId' => $this->sessionId);
$sessionCookie = JWT::encode($token, $settings->getJwtSecret());
$secure = strcmp(getProtocol(), "https") === 0;
setcookie('session', $sessionCookie, $this->getExpiresTime(), "/", "", $secure);
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-06-14 19:39:52 +02:00
'csrf_token' => $this->csrfToken
2020-02-09 23:02:19 +01:00
);
}
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-06-14 19:39:52 +02:00
$columns = array("expires", "user_id", "ipAddress", "os", "browser", "data", "stay_logged_in", "csrf_token");
2020-04-02 00:02:51 +02:00
$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),
2020-06-14 19:39:52 +02:00
$stayLoggedIn,
$this->csrfToken)
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))
2020-06-14 19:39:52 +02:00
->set("Session.csrf_token", $this->csrfToken)
2020-04-02 00:02:51 +02:00
->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
}
2020-06-14 19:39:52 +02:00
public function getCsrfToken(): string {
return $this->csrfToken;
}
2020-02-09 23:02:19 +01:00
}