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

289 lines
8.7 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 Configuration\Configuration;
use Exception;
use External\JWT;
use Driver\SQL\SQL;
2020-04-02 00:02:51 +02:00
use Driver\SQL\Condition\Compare;
use Driver\SQL\Condition\CondBool;
2020-02-09 23:02:19 +01:00
class User extends ApiObject {
2020-04-03 15:56:04 +02:00
private ?SQL $sql;
private Configuration $configuration;
private bool $loggedIn;
private ?Session $session;
private int $uid;
private string $username;
2020-06-24 23:33:34 +02:00
private ?string $email;
2020-04-03 15:56:04 +02:00
private Language $language;
2020-04-03 18:09:01 +02:00
private array $groups;
2020-02-09 23:02:19 +01:00
public function __construct($configuration) {
$this->configuration = $configuration;
$this->reset();
$this->connectDb();
2021-04-09 12:37:24 +02:00
if (!is_cli()) {
2021-11-11 14:25:26 +01:00
@session_start();
2021-04-09 12:37:24 +02:00
$this->setLanguage(Language::DEFAULT_LANGUAGE());
$this->parseCookies();
}
2020-02-09 23:02:19 +01:00
}
public function __destruct() {
if($this->sql && $this->sql->isConnected()) {
$this->sql->close();
}
}
private function connectDb() {
$databaseConf = $this->configuration->getDatabase();
if($databaseConf) {
2020-04-03 15:56:04 +02:00
$this->sql = SQL::createConnection($databaseConf);
2020-06-25 16:54:58 +02:00
if ($this->sql->isConnected()) {
$settings = $this->configuration->getSettings();
$settings->loadFromDatabase($this);
}
2020-04-03 15:56:04 +02:00
} else {
$this->sql = null;
2020-02-09 23:02:19 +01:00
}
}
2021-04-02 21:58:06 +02:00
public function getId(): int { return $this->uid; }
public function isLoggedIn(): bool { return $this->loggedIn; }
public function getUsername(): string { return $this->username; }
public function getEmail(): ?string { return $this->email; }
public function getSQL(): ?SQL { return $this->sql; }
public function getLanguage(): Language { return $this->language; }
2020-04-03 15:56:04 +02:00
public function setLanguage(Language $language) { $this->language = $language; $language->load(); }
2021-04-02 21:58:06 +02:00
public function getSession(): ?Session { return $this->session; }
public function getConfiguration(): Configuration { return $this->configuration; }
public function getGroups(): array { return $this->groups; }
public function hasGroup(int $group): bool { return isset($this->groups[$group]); }
2020-02-09 23:02:19 +01:00
2021-04-02 21:58:06 +02:00
public function __debugInfo(): array {
2020-02-09 23:02:19 +01:00
$debugInfo = array(
'loggedIn' => $this->loggedIn,
'language' => $this->language->getName(),
);
if($this->loggedIn) {
$debugInfo['uid'] = $this->uid;
$debugInfo['username'] = $this->username;
}
return $debugInfo;
}
2021-04-02 21:58:06 +02:00
public function jsonSerialize(): array {
2020-06-14 19:39:52 +02:00
if ($this->isLoggedIn()) {
return array(
'uid' => $this->uid,
'name' => $this->username,
2020-06-23 17:55:52 +02:00
'email' => $this->email,
2020-06-14 19:39:52 +02:00
'groups' => $this->groups,
'language' => $this->language->jsonSerialize(),
'session' => $this->session->jsonSerialize(),
);
} else {
return array(
'language' => $this->language->jsonSerialize(),
);
}
2020-02-09 23:02:19 +01:00
}
private function reset() {
$this->uid = 0;
$this->username = '';
2020-06-23 17:55:52 +02:00
$this->email = '';
2020-02-09 23:02:19 +01:00
$this->loggedIn = false;
2020-04-03 15:56:04 +02:00
$this->session = null;
2020-02-09 23:02:19 +01:00
}
2021-04-02 21:58:06 +02:00
public function logout(): bool {
2020-04-02 00:02:51 +02:00
$success = true;
2020-02-09 23:02:19 +01:00
if($this->loggedIn) {
2020-04-02 00:02:51 +02:00
$success = $this->session->destroy();
2020-02-09 23:02:19 +01:00
$this->reset();
}
2020-04-02 00:02:51 +02:00
return $success;
2020-02-09 23:02:19 +01:00
}
2021-04-02 21:58:06 +02:00
public function updateLanguage($lang): bool {
2020-02-10 00:52:25 +01:00
if($this->sql) {
2020-06-20 20:13:51 +02:00
$request = new \Api\Language\Set($this);
2020-02-10 00:52:25 +01:00
return $request->execute(array("langCode" => $lang));
2020-04-03 15:56:04 +02:00
} else {
return false;
2020-02-10 00:52:25 +01:00
}
2020-02-09 23:02:19 +01:00
}
public function sendCookies() {
if($this->loggedIn) {
$this->session->sendCookie();
}
$this->language->sendCookie();
2020-06-17 23:50:08 +02:00
session_write_close();
2020-02-09 23:02:19 +01:00
}
2021-04-02 21:58:06 +02:00
/**
* @param $userId user's id
* @param $sessionId session's id
* @param bool $sessionUpdate update session information, including session's lifetime and browser information
* @return bool true, if the data could be loaded
*/
public function readData($userId, $sessionId, $sessionUpdate = true): bool {
2020-02-17 00:18:37 +01:00
2020-06-23 17:55:52 +02:00
$res = $this->sql->select("User.name", "User.email",
"Language.uid as langId", "Language.code as langCode", "Language.name as langName",
2020-06-14 19:39:52 +02:00
"Session.data", "Session.stay_logged_in", "Session.csrf_token", "Group.uid as groupId", "Group.name as groupName")
2020-04-02 00:02:51 +02:00
->from("User")
->innerJoin("Session", "Session.user_id", "User.uid")
->leftJoin("Language", "User.language_id", "Language.uid")
2020-04-03 18:09:01 +02:00
->leftJoin("UserGroup", "UserGroup.user_id", "User.uid")
->leftJoin("Group", "UserGroup.group_id", "Group.uid")
2020-04-02 00:02:51 +02:00
->where(new Compare("User.uid", $userId))
->where(new Compare("Session.uid", $sessionId))
->where(new Compare("Session.active", true))
2020-04-02 15:08:14 +02:00
->where(new CondBool("Session.stay_logged_in"), new Compare("Session.expires", $this->sql->currentTimestamp(), '>'))
2020-04-02 00:02:51 +02:00
->execute();
$success = ($res !== FALSE);
2020-02-09 23:02:19 +01:00
if($success) {
2020-04-02 00:02:51 +02:00
if(empty($res)) {
2020-02-09 23:02:19 +01:00
$success = false;
} else {
2020-04-02 00:02:51 +02:00
$row = $res[0];
2020-06-14 19:39:52 +02:00
$csrfToken = $row["csrf_token"];
2020-04-02 00:02:51 +02:00
$this->username = $row['name'];
2020-06-23 17:55:52 +02:00
$this->email = $row["email"];
2020-02-09 23:02:19 +01:00
$this->uid = $userId;
2020-06-14 19:39:52 +02:00
$this->session = new Session($this, $sessionId, $csrfToken);
2020-04-02 21:32:26 +02:00
$this->session->setData(json_decode($row["data"] ?? '{}'));
2020-06-25 16:54:58 +02:00
$this->session->stayLoggedIn($this->sql->parseBool(["stay_logged_in"]));
2020-02-09 23:02:19 +01:00
if($sessionUpdate) $this->session->update();
$this->loggedIn = true;
2020-04-03 18:09:01 +02:00
2020-02-09 23:02:19 +01:00
if(!is_null($row['langId'])) {
2020-04-03 15:56:04 +02:00
$this->setLanguage(Language::newInstance($row['langId'], $row['langCode'], $row['langName']));
2020-02-09 23:02:19 +01:00
}
2020-04-03 18:09:01 +02:00
foreach($res as $row) {
$this->groups[$row["groupId"]] = $row["groupName"];
}
2020-02-09 23:02:19 +01:00
}
}
return $success;
}
private function parseCookies() {
if(isset($_COOKIE['session'])
&& is_string($_COOKIE['session'])
2020-06-25 16:54:58 +02:00
&& !empty($_COOKIE['session'])) {
2020-02-09 23:02:19 +01:00
try {
$token = $_COOKIE['session'];
2020-06-25 16:54:58 +02:00
$settings = $this->configuration->getSettings();
$decoded = (array)JWT::decode($token, $settings->getJwtSecret());
2020-02-09 23:02:19 +01:00
if(!is_null($decoded)) {
$userId = (isset($decoded['userId']) ? $decoded['userId'] : NULL);
$sessionId = (isset($decoded['sessionId']) ? $decoded['sessionId'] : NULL);
if(!is_null($userId) && !is_null($sessionId)) {
$this->readData($userId, $sessionId);
}
}
2020-04-03 15:56:04 +02:00
} catch(Exception $e) {
2020-04-02 21:19:06 +02:00
// ignored
2020-02-09 23:02:19 +01:00
}
}
if(isset($_GET['lang']) && is_string($_GET["lang"]) && !empty($_GET["lang"])) {
$this->updateLanguage($_GET['lang']);
2020-02-17 00:18:37 +01:00
} else if(isset($_COOKIE['lang']) && is_string($_COOKIE["lang"]) && !empty($_COOKIE["lang"])) {
2020-02-09 23:02:19 +01:00
$this->updateLanguage($_COOKIE['lang']);
2020-02-17 00:18:37 +01:00
}
2020-02-09 23:02:19 +01:00
}
2021-04-02 21:58:06 +02:00
public function createSession($userId, $stayLoggedIn): bool {
2020-02-09 23:02:19 +01:00
$this->uid = $userId;
2020-02-17 00:18:37 +01:00
$this->session = Session::create($this, $stayLoggedIn);
2021-04-02 21:58:06 +02:00
if ($this->session) {
2020-02-17 00:18:37 +01:00
$this->loggedIn = true;
return true;
}
return false;
2020-02-09 23:02:19 +01:00
}
2021-04-02 21:58:06 +02:00
public function authorize($apiKey): bool {
2020-04-02 00:02:51 +02:00
2021-04-02 21:58:06 +02:00
if ($this->loggedIn) {
2020-02-09 23:02:19 +01:00
return true;
2021-04-02 21:58:06 +02:00
}
2020-02-09 23:02:19 +01:00
2020-06-29 16:47:02 +02:00
$res = $this->sql->select("ApiKey.user_id as uid", "User.name", "User.email", "User.confirmed",
2021-11-11 14:25:26 +01:00
"Language.uid as langId", "Language.code as langCode", "Language.name as langName",
"Group.uid as groupId", "Group.name as groupName")
2020-04-02 00:02:51 +02:00
->from("ApiKey")
->innerJoin("User", "ApiKey.user_id", "User.uid")
2021-11-11 14:25:26 +01:00
->leftJoin("UserGroup", "UserGroup.user_id", "User.uid")
->leftJoin("Group", "UserGroup.group_id", "Group.uid")
2020-04-02 00:02:51 +02:00
->leftJoin("Language", "User.language_id", "Language.uid")
->where(new Compare("ApiKey.api_key", $apiKey))
2020-04-02 15:08:14 +02:00
->where(new Compare("valid_until", $this->sql->currentTimestamp(), ">"))
2020-04-03 15:56:04 +02:00
->where(new Compare("ApiKey.active", 1))
2020-04-02 00:02:51 +02:00
->execute();
2020-02-09 23:02:19 +01:00
2020-04-02 00:02:51 +02:00
$success = ($res !== FALSE);
2020-02-09 23:02:19 +01:00
if($success) {
2020-04-02 00:02:51 +02:00
if(empty($res)) {
2020-02-09 23:02:19 +01:00
$success = false;
} else {
2020-04-02 00:02:51 +02:00
$row = $res[0];
2020-06-29 16:47:02 +02:00
if (!$this->sql->parseBool($row["confirmed"])) {
return false;
}
2020-02-09 23:02:19 +01:00
$this->uid = $row['uid'];
2020-06-23 17:55:52 +02:00
$this->username = $row['name'];
$this->email = $row['email'];
2020-02-09 23:02:19 +01:00
if(!is_null($row['langId'])) {
2020-04-03 15:56:04 +02:00
$this->setLanguage(Language::newInstance($row['langId'], $row['langCode'], $row['langName']));
2020-02-09 23:02:19 +01:00
}
2021-11-11 14:25:26 +01:00
foreach($res as $row) {
$this->groups[$row["groupId"]] = $row["groupName"];
}
2020-02-09 23:02:19 +01:00
}
}
return $success;
}
2020-06-17 23:50:08 +02:00
public function processVisit() {
2020-06-24 21:18:26 +02:00
if ($this->sql && $this->sql->isConnected() && isset($_COOKIE["PHPSESSID"]) && !empty($_COOKIE["PHPSESSID"])) {
2020-06-18 15:08:09 +02:00
if ($this->isBot()) {
return;
}
2020-06-17 23:50:08 +02:00
$cookie = $_COOKIE["PHPSESSID"];
2021-01-07 16:02:52 +01:00
$req = new \Api\Visitors\ProcessVisit($this);
$req->execute(array("cookie" => $cookie));
2020-06-17 23:50:08 +02:00
}
}
2020-06-18 15:08:09 +02:00
2021-04-02 21:58:06 +02:00
private function isBot(): bool {
2020-06-18 15:08:09 +02:00
if (!isset($_SERVER["HTTP_USER_AGENT"]) || empty($_SERVER["HTTP_USER_AGENT"])) {
return false;
}
return preg_match('/robot|spider|crawler|curl|^$/i', $_SERVER['HTTP_USER_AGENT']) === 1;
}
2020-02-09 23:02:19 +01:00
}