2020-02-09 23:02:19 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Objects;
|
|
|
|
|
|
|
|
class Session extends ApiObject {
|
|
|
|
|
|
|
|
const DURATION = 120;
|
|
|
|
|
|
|
|
private $sessionId;
|
|
|
|
private $user;
|
|
|
|
private $expires;
|
|
|
|
private $ipAddress;
|
|
|
|
private $os;
|
|
|
|
private $browser;
|
2020-02-17 00:18:37 +01:00
|
|
|
private $stayLoggedIn;
|
2020-02-09 23:02:19 +01:00
|
|
|
|
2020-02-17 00:18:37 +01:00
|
|
|
public function __construct($user, $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";
|
|
|
|
} catch(\Exception $ex) {
|
|
|
|
$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);
|
|
|
|
$sessionCookie = \External\JWT::encode($token, $jwt->getKey());
|
|
|
|
$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-02-17 00:18:37 +01:00
|
|
|
$query = "INSERT INTO Session (expires, user_id, ipAddress, os, browser, data, stay_logged_in)
|
|
|
|
VALUES (DATE_ADD(NOW(), INTERVAL ? MINUTE),?,?,?,?,?,?)";
|
2020-02-10 00:52:25 +01:00
|
|
|
$request = new \Api\ExecuteStatement($this->user);
|
2020-02-09 23:02:19 +01:00
|
|
|
|
|
|
|
$success = $request->execute(array(
|
|
|
|
'query' => $query,
|
|
|
|
Session::DURATION,
|
|
|
|
$this->user->getId(),
|
|
|
|
$this->ipAddress,
|
|
|
|
$this->os,
|
|
|
|
$this->browser,
|
2020-02-17 00:18:37 +01:00
|
|
|
json_encode($_SESSION),
|
|
|
|
$stayLoggedIn
|
2020-02-09 23:02:19 +01:00
|
|
|
));
|
|
|
|
|
|
|
|
if($success) {
|
|
|
|
$this->sessionId = $this->user->getSQL()->getLastInsertId();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function destroy() {
|
2020-02-17 00:18:37 +01:00
|
|
|
$query = 'DELETE FROM Session WHERE Session.uid=? OR (Session.stay_logged_in = 0 AND Session.expires<=NOW())';
|
2020-02-10 00:52:25 +01:00
|
|
|
$request = new \Api\ExecuteStatement($this->user);
|
2020-02-09 23:02:19 +01:00
|
|
|
$success = $request->execute(array('query' => $query, $this->sessionId));
|
|
|
|
return $success;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function update() {
|
|
|
|
$this->updateMetaData();
|
2020-02-17 00:18:37 +01:00
|
|
|
|
2020-02-09 23:02:19 +01:00
|
|
|
$query = 'UPDATE Session
|
2020-02-17 00:18:37 +01:00
|
|
|
SET Session.expires=DATE_ADD(NOW(), INTERVAL ? MINUTE),
|
|
|
|
Session.ipAddress=?, Session.os=?, Session.browser=?, Session.data=?
|
|
|
|
WHERE Session.uid=?';
|
|
|
|
|
2020-02-10 00:52:25 +01:00
|
|
|
$request = new \Api\ExecuteStatement($this->user);
|
2020-02-09 23:02:19 +01:00
|
|
|
$success = $request->execute(array(
|
|
|
|
'query' => $query,
|
|
|
|
Session::DURATION,
|
|
|
|
$this->ipAddress,
|
|
|
|
$this->os,
|
|
|
|
$this->browser,
|
2020-02-17 00:18:37 +01:00
|
|
|
json_encode($_SESSION),
|
2020-02-09 23:02:19 +01:00
|
|
|
$this->sessionId,
|
|
|
|
));
|
|
|
|
return $success;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
?>
|