web-base/Core/Objects/Context.class.php

246 lines
6.6 KiB
PHP
Raw Permalink Normal View History

2022-06-20 19:52:31 +02:00
<?php
2022-11-18 18:06:46 +01:00
namespace Core\Objects;
use Core\Configuration\Configuration;
use Core\Configuration\Settings;
2024-04-23 20:14:32 +02:00
use Core\Driver\Redis\RedisConnection;
use Core\Driver\SQL\Column\Column;
2022-11-18 18:06:46 +01:00
use Core\Driver\SQL\Condition\Compare;
use Core\Driver\SQL\Condition\CondLike;
use Core\Driver\SQL\Condition\CondOr;
use Core\Driver\SQL\Join\InnerJoin;
2022-11-18 18:06:46 +01:00
use Core\Driver\SQL\SQL;
use Core\Objects\DatabaseEntity\Language;
use Core\Objects\DatabaseEntity\Session;
use Core\Objects\DatabaseEntity\User;
use Core\Objects\Router\Router;
2022-06-20 19:52:31 +02:00
class Context {
2023-01-07 15:34:05 +01:00
private static Context $instance;
2022-06-20 19:52:31 +02:00
private ?SQL $sql;
2024-04-24 10:11:54 +02:00
protected ?Session $session;
2022-06-20 19:52:31 +02:00
private ?User $user;
private Configuration $configuration;
private Language $language;
2022-08-20 22:17:17 +02:00
public ?Router $router;
2024-04-24 10:11:54 +02:00
protected ?RedisConnection $redis;
2022-06-20 19:52:31 +02:00
2024-04-24 10:11:54 +02:00
protected function __construct() {
2022-06-20 19:52:31 +02:00
$this->sql = null;
$this->session = null;
$this->user = null;
2022-08-20 22:17:17 +02:00
$this->router = null;
2024-04-23 20:14:32 +02:00
$this->redis = null;
2022-06-20 19:52:31 +02:00
$this->configuration = new Configuration();
$this->setLanguage(Language::DEFAULT_LANGUAGE());
if (!$this->isCLI()) {
@session_start();
}
}
2023-01-07 15:34:05 +01:00
public static function instance(): self {
if (!isset(self::$instance)) {
self::$instance = new Context();
}
return self::$instance;
}
2022-06-20 19:52:31 +02:00
public function __destruct() {
if ($this->sql && $this->sql->isConnected()) {
$this->sql->close();
$this->sql = null;
}
2024-04-23 20:14:32 +02:00
if ($this->redis && $this->redis->isConnected()) {
$this->redis->close();
$this->redis = null;
}
2022-06-20 19:52:31 +02:00
}
public function setLanguage(Language $language): void {
2024-05-06 15:30:43 +02:00
// 1st: check if the language really exists...
if ($language->getId() === null) {
$language = Language::findBy(Language::createBuilder($this->sql, true)
->whereEq("code", $language->getCode()));
if ($language === false || $language === null) {
// if not, load the default language
$language = Language::DEFAULT_LANGUAGE();
}
}
2022-06-20 19:52:31 +02:00
$this->language = $language;
$this->language->activate();
if ($this->user && $this->user->language->getId() !== $language->getId()) {
$this->user->language = $language;
}
}
public function initSQL(): ?SQL {
$databaseConf = $this->configuration->getDatabase();
if ($databaseConf) {
$this->sql = SQL::createConnection($databaseConf);
if ($this->sql->isConnected()) {
$settings = $this->configuration->getSettings();
$settings->loadFromDatabase($this);
return $this->sql;
}
} else {
$this->sql = null;
}
return null;
}
public function getSQL(): ?SQL {
return $this->sql;
}
public function getSettings(): Settings {
return $this->configuration->getSettings();
}
public function getUser(): ?User {
return $this->user;
}
public function sendCookies(): void {
$domain = getCurrentHostName();
2022-06-20 19:52:31 +02:00
$this->language->sendCookie($domain);
$this->session?->sendCookie($domain);
$this->session?->update();
session_write_close();
}
2024-03-24 17:36:16 +01:00
private function loadSession(string $sessionUUID): void {
$this->session = Session::init($this, $sessionUUID);
2022-06-20 19:52:31 +02:00
$this->user = $this->session?->getUser();
}
2023-01-07 15:34:05 +01:00
public function parseCookies(): void {
2024-03-27 14:12:01 +01:00
$settings = $this->getSettings();
if (!$settings->isInstalled()) {
// we cannot process user sessions or localization yet.
return;
}
if (isset($_COOKIE['session']) && is_string($_COOKIE['session']) && !empty($_COOKIE['session'])) {
$this->loadSession($_COOKIE['session']);
2022-06-20 19:52:31 +02:00
}
2024-05-06 15:30:43 +02:00
// set language by priority: 1. GET parameter, 2. cookie, 3. user's settings, 4. accept-language header
2022-06-20 19:52:31 +02:00
if (isset($_GET['lang']) && is_string($_GET["lang"]) && !empty($_GET["lang"])) {
$this->updateLanguage($_GET['lang']);
} else if (isset($_COOKIE['lang']) && is_string($_COOKIE["lang"]) && !empty($_COOKIE["lang"])) {
$this->updateLanguage($_COOKIE['lang']);
} else if ($this->user) {
$this->setLanguage($this->user->language);
2024-05-06 15:30:43 +02:00
} else {
2024-05-07 21:57:49 +02:00
$language = Language::fromHeader();
if ($language !== null) {
$this->setLanguage($language);
}
2022-06-20 19:52:31 +02:00
}
}
public function updateLanguage(string $lang): bool {
if ($this->sql) {
2022-11-20 17:13:53 +01:00
$language = Language::findBy(Language::createBuilder($this->sql, true)
2022-06-20 19:52:31 +02:00
->where(new CondOr(
2024-05-07 21:57:49 +02:00
new CondLike(new Column("name"), "%$lang%"), // english
new Compare("code", $lang), // de_DE
new CondLike(new Column("code"), "{$lang}_%") // de -> de_%
2022-11-20 17:13:53 +01:00
))
);
2022-06-20 19:52:31 +02:00
if ($language) {
$this->setLanguage($language);
return true;
}
}
return false;
}
private function isBot(): bool {
if (empty($_SERVER["HTTP_USER_AGENT"])) {
return false;
}
return preg_match('/robot|spider|crawler|curl|^$/i', $_SERVER['HTTP_USER_AGENT']) === 1;
}
public function isCLI(): bool {
return php_sapi_name() === "cli";
}
public function getConfig(): Configuration {
return $this->configuration;
}
public function getSession(): ?Session {
return $this->session;
}
public function loadApiKey(string $apiKey): bool {
2022-11-20 17:13:53 +01:00
$this->user = User::findBy(User::createBuilder($this->sql, true)
->addJoin(new InnerJoin("ApiKey", "ApiKey.user_id", "User.id"))
->whereEq("ApiKey.api_key", $apiKey)
->whereGt("valid_until", $this->sql->currentTimestamp())
->whereTrue("ApiKey.active")
->whereTrue("User.confirmed")
->whereTrue("User.active")
2022-11-20 17:13:53 +01:00
->fetchEntities());
2022-06-20 19:52:31 +02:00
return $this->user !== null;
}
2022-11-19 01:15:34 +01:00
public function createSession(User $user, bool $stayLoggedIn): ?Session {
$this->user = $user;
$this->session = new Session($this, $this->user);
$this->session->stayLoggedIn = $stayLoggedIn;
if ($this->session->update()) {
return $this->session;
} else {
$this->user = null;
$this->session = null;
return null;
2022-06-20 19:52:31 +02:00
}
}
public function getLanguage(): Language {
return $this->language;
}
public function invalidateSessions(bool $keepCurrent = false): bool {
$query = $this->sql->update("Session")
->set("active", false)
->whereEq("user_id", $this->user->getId());
if ($keepCurrent && $this->session !== null) {
$query->whereNeq("id", $this->session->getId());
}
return $query->execute();
}
2024-04-23 20:14:32 +02:00
public function getRedis(): ?RedisConnection {
if ($this->redis === null) {
$settings = $this->getSettings();
$connectionData = $settings->getRedisConfiguration();
$this->redis = new RedisConnection($this->sql);
if (!$this->redis->connect($connectionData)) {
$this->redis = null;
}
}
return $this->redis;
}
2022-06-20 19:52:31 +02:00
}