2020-02-09 23:02:19 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Objects;
|
|
|
|
|
2020-04-03 15:56:04 +02:00
|
|
|
use Configuration\Configuration;
|
2022-06-14 10:30:35 +02:00
|
|
|
use Driver\SQL\Condition\CondAnd;
|
2020-04-03 15:56:04 +02:00
|
|
|
use Exception;
|
|
|
|
use Driver\SQL\SQL;
|
2020-04-02 00:02:51 +02:00
|
|
|
use Driver\SQL\Condition\Compare;
|
2022-06-14 16:55:53 +02:00
|
|
|
use Firebase\JWT\JWT;
|
2022-02-20 16:53:26 +01:00
|
|
|
use Objects\TwoFactor\TwoFactorToken;
|
2020-04-02 00:02:51 +02:00
|
|
|
|
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;
|
2021-12-08 16:53:43 +01:00
|
|
|
private string $fullName;
|
2020-06-24 23:33:34 +02:00
|
|
|
private ?string $email;
|
2021-12-08 16:53:43 +01:00
|
|
|
private ?string $profilePicture;
|
2020-04-03 15:56:04 +02:00
|
|
|
private Language $language;
|
2020-04-03 18:09:01 +02:00
|
|
|
private array $groups;
|
2022-02-20 16:53:26 +01:00
|
|
|
private ?GpgKey $gpgKey;
|
|
|
|
private ?TwoFactorToken $twoFactorToken;
|
2020-02-09 23:02:19 +01:00
|
|
|
|
|
|
|
public function __construct($configuration) {
|
|
|
|
$this->configuration = $configuration;
|
|
|
|
$this->reset();
|
2022-05-31 16:14:49 +02:00
|
|
|
$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() {
|
2022-06-14 10:30:35 +02:00
|
|
|
if ($this->sql && $this->sql->isConnected()) {
|
2020-02-09 23:02:19 +01:00
|
|
|
$this->sql->close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-31 16:14:49 +02:00
|
|
|
public function connectDB(): bool {
|
2020-02-09 23:02:19 +01:00
|
|
|
$databaseConf = $this->configuration->getDatabase();
|
2022-05-31 16:14:49 +02:00
|
|
|
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);
|
2022-05-31 16:14:49 +02:00
|
|
|
return true;
|
2020-06-25 16:54:58 +02:00
|
|
|
}
|
2020-04-03 15:56:04 +02:00
|
|
|
} else {
|
|
|
|
$this->sql = null;
|
2020-02-09 23:02:19 +01:00
|
|
|
}
|
2022-05-31 16:14:49 +02:00
|
|
|
|
|
|
|
return false;
|
2020-02-09 23:02:19 +01:00
|
|
|
}
|
|
|
|
|
2022-06-14 10:30:35 +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 getFullName(): string {
|
|
|
|
return $this->fullName;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getEmail(): ?string {
|
|
|
|
return $this->email;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getSQL(): ?SQL {
|
|
|
|
return $this->sql;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getLanguage(): Language {
|
|
|
|
return $this->language;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setLanguage(Language $language) {
|
|
|
|
$this->language = $language;
|
|
|
|
$language->load();
|
|
|
|
}
|
|
|
|
|
|
|
|
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]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getGPG(): ?GpgKey {
|
|
|
|
return $this->gpgKey;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getTwoFactorToken(): ?TwoFactorToken {
|
|
|
|
return $this->twoFactorToken;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getProfilePicture(): ?string {
|
|
|
|
return $this->profilePicture;
|
|
|
|
}
|
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(),
|
|
|
|
);
|
|
|
|
|
2022-06-14 10:30:35 +02:00
|
|
|
if ($this->loggedIn) {
|
2020-02-09 23:02:19 +01:00
|
|
|
$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,
|
2021-12-08 16:53:43 +01:00
|
|
|
'fullName' => $this->fullName,
|
|
|
|
'profilePicture' => $this->profilePicture,
|
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(),
|
2022-02-20 16:53:26 +01:00
|
|
|
"gpg" => ($this->gpgKey ? $this->gpgKey->jsonSerialize() : null),
|
|
|
|
"2fa" => ($this->twoFactorToken ? $this->twoFactorToken->jsonSerialize() : null),
|
2020-06-14 19:39:52 +02:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return array(
|
2022-06-14 10:30:35 +02:00
|
|
|
'language' => $this->language->jsonSerialize(),
|
2020-06-14 19:39:52 +02:00
|
|
|
);
|
|
|
|
}
|
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 = '';
|
2021-12-08 16:53:43 +01:00
|
|
|
$this->groups = [];
|
2020-02-09 23:02:19 +01:00
|
|
|
$this->loggedIn = false;
|
2020-04-03 15:56:04 +02:00
|
|
|
$this->session = null;
|
2021-12-08 16:53:43 +01:00
|
|
|
$this->profilePicture = null;
|
2022-02-20 16:53:26 +01:00
|
|
|
$this->gpgKey = null;
|
|
|
|
$this->twoFactorToken = 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;
|
2022-02-20 16:53:26 +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 {
|
2022-06-14 10:30:35 +02: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 {
|
2022-06-14 10:30:35 +02:00
|
|
|
return false;
|
2020-02-10 00:52:25 +01:00
|
|
|
}
|
2020-02-09 23:02:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function sendCookies() {
|
2022-02-20 16:53:26 +01:00
|
|
|
|
|
|
|
$baseUrl = $this->getConfiguration()->getSettings()->getBaseUrl();
|
|
|
|
$domain = parse_url($baseUrl, PHP_URL_HOST);
|
|
|
|
|
|
|
|
if ($this->loggedIn) {
|
|
|
|
$this->session->sendCookie($domain);
|
2020-02-09 23:02:19 +01:00
|
|
|
}
|
|
|
|
|
2022-02-20 16:53:26 +01:00
|
|
|
$this->language->sendCookie($domain);
|
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
|
|
|
|
*/
|
2022-06-14 10:30:35 +02:00
|
|
|
public function loadSession($userId, $sessionId, bool $sessionUpdate = true): bool {
|
|
|
|
|
|
|
|
$userRow = $this->loadUser("Session", ["Session.data", "Session.stay_logged_in", "Session.csrf_token"], [
|
|
|
|
new Compare("User.uid", $userId),
|
|
|
|
new Compare("Session.uid", $sessionId),
|
|
|
|
new Compare("Session.active", true),
|
|
|
|
]);
|
|
|
|
|
|
|
|
if ($userRow !== false) {
|
|
|
|
$this->session = new Session($this, $sessionId, $userRow["csrf_token"]);
|
|
|
|
$this->session->setData(json_decode($userRow["data"] ?? '{}', true));
|
|
|
|
$this->session->stayLoggedIn($this->sql->parseBool($userRow["stay_logged_in"]));
|
|
|
|
if ($sessionUpdate) {
|
|
|
|
$this->session->update();
|
2020-02-09 23:02:19 +01:00
|
|
|
}
|
2022-06-14 10:30:35 +02:00
|
|
|
$this->loggedIn = true;
|
2020-02-09 23:02:19 +01:00
|
|
|
}
|
|
|
|
|
2022-06-14 10:30:35 +02:00
|
|
|
return $userRow !== false;
|
2020-02-09 23:02:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private function parseCookies() {
|
2022-02-20 16:53:26 +01:00
|
|
|
if (isset($_COOKIE['session']) && is_string($_COOKIE['session']) && !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();
|
2022-06-14 16:55:53 +02:00
|
|
|
$decoded = (array)JWT::decode($token, $settings->getJwtKey());
|
2022-06-14 10:30:35 +02:00
|
|
|
if (!is_null($decoded)) {
|
2021-12-08 16:53:43 +01:00
|
|
|
$userId = ($decoded['userId'] ?? NULL);
|
|
|
|
$sessionId = ($decoded['sessionId'] ?? NULL);
|
2022-06-14 10:30:35 +02:00
|
|
|
if (!is_null($userId) && !is_null($sessionId)) {
|
|
|
|
$this->loadSession($userId, $sessionId);
|
2020-02-09 23:02:19 +01:00
|
|
|
}
|
|
|
|
}
|
2022-06-14 10:30:35 +02:00
|
|
|
} catch (Exception $e) {
|
2020-04-02 21:19:06 +02:00
|
|
|
// ignored
|
2020-02-09 23:02:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-14 10:30:35 +02:00
|
|
|
if (isset($_GET['lang']) && is_string($_GET["lang"]) && !empty($_GET["lang"])) {
|
2020-02-09 23:02:19 +01:00
|
|
|
$this->updateLanguage($_GET['lang']);
|
2022-06-14 10:30:35 +02: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
|
|
|
}
|
|
|
|
|
2022-02-20 16:53:26 +01:00
|
|
|
public function createSession(int $userId, bool $stayLoggedIn = false): 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
|
|
|
}
|
|
|
|
|
2022-06-14 10:30:35 +02:00
|
|
|
private function loadUser(string $table, array $columns, array $conditions) {
|
|
|
|
$userRow = $this->sql->select(
|
|
|
|
// User meta
|
|
|
|
"User.uid as userId", "User.name", "User.email", "User.fullName", "User.profilePicture", "User.confirmed",
|
2020-04-02 00:02:51 +02:00
|
|
|
|
2022-06-14 10:30:35 +02:00
|
|
|
// GPG
|
|
|
|
"User.gpg_id", "GpgKey.confirmed as gpg_confirmed", "GpgKey.fingerprint as gpg_fingerprint",
|
|
|
|
"GpgKey.expires as gpg_expires", "GpgKey.algorithm as gpg_algorithm",
|
2020-02-09 23:02:19 +01:00
|
|
|
|
2022-06-14 10:30:35 +02:00
|
|
|
// 2FA
|
2022-02-20 16:53:26 +01:00
|
|
|
"User.2fa_id", "2FA.confirmed as 2fa_confirmed", "2FA.data as 2fa_data", "2FA.type as 2fa_type",
|
2022-06-14 10:30:35 +02:00
|
|
|
|
|
|
|
// Language
|
2021-11-11 14:25:26 +01:00
|
|
|
"Language.uid as langId", "Language.code as langCode", "Language.name as langName",
|
2022-06-14 10:30:35 +02:00
|
|
|
|
|
|
|
// additional data
|
|
|
|
...$columns)
|
|
|
|
->from("User")
|
|
|
|
->innerJoin("$table", "$table.user_id", "User.uid")
|
2020-04-02 00:02:51 +02:00
|
|
|
->leftJoin("Language", "User.language_id", "Language.uid")
|
2022-02-20 16:53:26 +01:00
|
|
|
->leftJoin("GpgKey", "User.gpg_id", "GpgKey.uid")
|
|
|
|
->leftJoin("2FA", "User.2fa_id", "2FA.uid")
|
2022-06-14 10:30:35 +02:00
|
|
|
->where(new CondAnd(...$conditions))
|
|
|
|
->first()
|
2020-04-02 00:02:51 +02:00
|
|
|
->execute();
|
2020-02-09 23:02:19 +01:00
|
|
|
|
2022-06-14 10:30:35 +02:00
|
|
|
if ($userRow === null || $userRow === false) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-02-09 23:02:19 +01:00
|
|
|
|
2022-06-14 10:30:35 +02:00
|
|
|
// Meta data
|
|
|
|
$userId = $userRow["userId"];
|
|
|
|
$this->uid = $userId;
|
|
|
|
$this->username = $userRow['name'];
|
|
|
|
$this->fullName = $userRow["fullName"];
|
|
|
|
$this->email = $userRow['email'];
|
|
|
|
$this->profilePicture = $userRow["profilePicture"];
|
|
|
|
|
|
|
|
// GPG
|
|
|
|
if (!empty($userRow["gpg_id"])) {
|
|
|
|
$this->gpgKey = new GpgKey($userRow["gpg_id"], $this->sql->parseBool($userRow["gpg_confirmed"]),
|
|
|
|
$userRow["gpg_fingerprint"], $userRow["gpg_algorithm"], $userRow["gpg_expires"]
|
|
|
|
);
|
|
|
|
}
|
2022-02-20 16:53:26 +01:00
|
|
|
|
2022-06-14 10:30:35 +02:00
|
|
|
// 2FA
|
|
|
|
if (!empty($userRow["2fa_id"])) {
|
|
|
|
$this->twoFactorToken = TwoFactorToken::newInstance($userRow["2fa_type"], $userRow["2fa_data"],
|
|
|
|
$userRow["2fa_id"], $this->sql->parseBool($userRow["2fa_confirmed"]));
|
|
|
|
}
|
2022-02-20 16:53:26 +01:00
|
|
|
|
2022-06-14 10:30:35 +02:00
|
|
|
// Language
|
|
|
|
if (!is_null($userRow['langId'])) {
|
|
|
|
$this->setLanguage(Language::newInstance($userRow['langId'], $userRow['langCode'], $userRow['langName']));
|
|
|
|
}
|
2021-11-11 14:25:26 +01:00
|
|
|
|
2022-06-14 10:30:35 +02:00
|
|
|
// select groups
|
|
|
|
$groupRows = $this->sql->select("Group.uid as groupId", "Group.name as groupName")
|
|
|
|
->from("UserGroup")
|
|
|
|
->where(new Compare("UserGroup.user_id", $userId))
|
|
|
|
->innerJoin("Group", "UserGroup.group_id", "Group.uid")
|
|
|
|
->execute();
|
|
|
|
if (is_array($groupRows)) {
|
|
|
|
foreach ($groupRows as $row) {
|
|
|
|
$this->groups[$row["groupId"]] = $row["groupName"];
|
2020-02-09 23:02:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-14 10:30:35 +02:00
|
|
|
return $userRow;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function loadApiKey($apiKey): bool {
|
|
|
|
|
|
|
|
if ($this->loggedIn) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$userRow = $this->loadUser("ApiKey", [], [
|
|
|
|
new Compare("ApiKey.api_key", $apiKey),
|
|
|
|
new Compare("valid_until", $this->sql->currentTimestamp(), ">"),
|
|
|
|
new Compare("ApiKey.active", 1),
|
|
|
|
]);
|
|
|
|
|
|
|
|
// User must be confirmed to use API-Keys
|
|
|
|
if ($userRow === false || !$this->sql->parseBool($userRow["confirmed"])) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2020-02-09 23:02:19 +01:00
|
|
|
}
|
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 {
|
2022-06-14 10:30:35 +02:00
|
|
|
if (empty($_SERVER["HTTP_USER_AGENT"])) {
|
2020-06-18 15:08:09 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return preg_match('/robot|spider|crawler|curl|^$/i', $_SERVER['HTTP_USER_AGENT']) === 1;
|
|
|
|
}
|
2020-02-09 23:02:19 +01:00
|
|
|
}
|