2022-08-18 23:52:01 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
final class RssBridge
|
|
|
|
{
|
2023-07-16 23:07:34 +03:00
|
|
|
private static HttpClient $httpClient;
|
2023-07-08 18:03:12 +03:00
|
|
|
private static CacheInterface $cache;
|
|
|
|
|
2023-09-10 22:50:15 +03:00
|
|
|
public function __construct()
|
2022-08-18 23:52:01 +03:00
|
|
|
{
|
|
|
|
Configuration::verifyInstallation();
|
|
|
|
|
2022-08-23 22:19:53 +03:00
|
|
|
$customConfig = [];
|
|
|
|
if (file_exists(__DIR__ . '/../config.ini.php')) {
|
|
|
|
$customConfig = parse_ini_file(__DIR__ . '/../config.ini.php', true, INI_SCANNER_TYPED);
|
|
|
|
}
|
|
|
|
Configuration::loadConfiguration($customConfig, getenv());
|
2022-08-18 23:52:01 +03:00
|
|
|
|
2023-09-10 22:50:15 +03:00
|
|
|
set_exception_handler(function (\Throwable $e) {
|
|
|
|
Logger::error('Uncaught Exception', ['e' => $e]);
|
|
|
|
http_response_code(500);
|
|
|
|
print render(__DIR__ . '/../templates/error.html.php', ['e' => $e]);
|
|
|
|
exit(1);
|
|
|
|
});
|
|
|
|
|
2022-09-08 20:07:57 +03:00
|
|
|
set_error_handler(function ($code, $message, $file, $line) {
|
|
|
|
if ((error_reporting() & $code) === 0) {
|
|
|
|
return false;
|
|
|
|
}
|
2023-03-06 23:47:25 +03:00
|
|
|
$text = sprintf(
|
|
|
|
'%s at %s line %s',
|
|
|
|
sanitize_root($message),
|
|
|
|
sanitize_root($file),
|
|
|
|
$line
|
|
|
|
);
|
2022-09-08 20:07:57 +03:00
|
|
|
Logger::warning($text);
|
|
|
|
if (Debug::isEnabled()) {
|
2022-10-29 11:27:02 +03:00
|
|
|
print sprintf("<pre>%s</pre>\n", e($text));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// There might be some fatal errors which are not caught by set_error_handler() or \Throwable.
|
|
|
|
register_shutdown_function(function () {
|
|
|
|
$error = error_get_last();
|
|
|
|
if ($error) {
|
|
|
|
$message = sprintf(
|
2023-07-06 19:52:19 +03:00
|
|
|
'(shutdown) %s: %s in %s line %s',
|
2022-10-29 11:27:02 +03:00
|
|
|
$error['type'],
|
2023-03-06 23:47:25 +03:00
|
|
|
sanitize_root($error['message']),
|
|
|
|
sanitize_root($error['file']),
|
2022-10-29 11:27:02 +03:00
|
|
|
$error['line']
|
|
|
|
);
|
|
|
|
Logger::error($message);
|
|
|
|
if (Debug::isEnabled()) {
|
2023-06-02 21:22:09 +03:00
|
|
|
// todo: extract to log handler
|
2022-10-29 11:27:02 +03:00
|
|
|
print sprintf("<pre>%s</pre>\n", e($message));
|
|
|
|
}
|
2022-09-08 20:07:57 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-11-07 20:22:54 +03:00
|
|
|
// Consider: ini_set('error_reporting', E_ALL & ~E_DEPRECATED);
|
2022-08-23 22:19:53 +03:00
|
|
|
date_default_timezone_set(Configuration::getConfig('system', 'timezone'));
|
2022-08-18 23:52:01 +03:00
|
|
|
|
2023-07-16 23:07:34 +03:00
|
|
|
self::$httpClient = new CurlHttpClient();
|
2023-09-10 22:50:15 +03:00
|
|
|
|
|
|
|
$cacheFactory = new CacheFactory();
|
|
|
|
if (Debug::isEnabled()) {
|
|
|
|
self::$cache = $cacheFactory->create('array');
|
|
|
|
} else {
|
|
|
|
self::$cache = $cacheFactory->create();
|
|
|
|
}
|
2023-07-08 18:03:12 +03:00
|
|
|
|
2022-08-18 23:52:01 +03:00
|
|
|
if (Configuration::getConfig('authentication', 'enable')) {
|
2023-03-06 22:43:44 +03:00
|
|
|
$authenticationMiddleware = new AuthenticationMiddleware();
|
2022-08-18 23:52:01 +03:00
|
|
|
$authenticationMiddleware();
|
|
|
|
}
|
2023-09-10 22:50:15 +03:00
|
|
|
}
|
2022-08-18 23:52:01 +03:00
|
|
|
|
2023-09-10 22:50:15 +03:00
|
|
|
public function main(array $argv = []): void
|
|
|
|
{
|
|
|
|
if ($argv) {
|
|
|
|
parse_str(implode('&', array_slice($argv, 1)), $cliArgs);
|
|
|
|
$request = $cliArgs;
|
|
|
|
} else {
|
|
|
|
$request = array_merge($_GET, $_POST);
|
2022-08-18 23:52:01 +03:00
|
|
|
}
|
|
|
|
|
2023-09-10 22:50:15 +03:00
|
|
|
try {
|
|
|
|
foreach ($request as $key => $value) {
|
|
|
|
if (!is_string($value)) {
|
|
|
|
throw new \Exception("Query parameter \"$key\" is not a string.");
|
|
|
|
}
|
|
|
|
}
|
2022-11-07 20:22:54 +03:00
|
|
|
|
2023-09-10 22:50:15 +03:00
|
|
|
$actionName = $request['action'] ?? 'Frontpage';
|
|
|
|
$actionName = strtolower($actionName) . 'Action';
|
|
|
|
$actionName = implode(array_map('ucfirst', explode('-', $actionName)));
|
|
|
|
|
|
|
|
$filePath = __DIR__ . '/../actions/' . $actionName . '.php';
|
|
|
|
if (!file_exists($filePath)) {
|
|
|
|
throw new \Exception('Invalid action', 400);
|
|
|
|
}
|
|
|
|
$className = '\\' . $actionName;
|
|
|
|
$action = new $className();
|
|
|
|
|
|
|
|
$response = $action->execute($request);
|
|
|
|
if (is_string($response)) {
|
|
|
|
print $response;
|
|
|
|
} elseif ($response instanceof Response) {
|
|
|
|
$response->send();
|
|
|
|
}
|
|
|
|
} catch (\Throwable $e) {
|
|
|
|
Logger::error('Exception in RssBridge::main()', ['e' => $e]);
|
|
|
|
http_response_code(500);
|
|
|
|
print render(__DIR__ . '/../templates/error.html.php', ['e' => $e]);
|
2022-11-07 20:22:54 +03:00
|
|
|
}
|
2022-08-18 23:52:01 +03:00
|
|
|
}
|
2023-07-08 18:03:12 +03:00
|
|
|
|
2023-07-16 23:07:34 +03:00
|
|
|
public static function getHttpClient(): HttpClient
|
2023-07-08 18:03:12 +03:00
|
|
|
{
|
2023-07-16 23:07:34 +03:00
|
|
|
return self::$httpClient;
|
2023-07-08 18:03:12 +03:00
|
|
|
}
|
|
|
|
|
2023-07-16 23:07:34 +03:00
|
|
|
public static function getCache(): CacheInterface
|
2023-07-08 18:03:12 +03:00
|
|
|
{
|
2023-09-10 22:50:15 +03:00
|
|
|
return self::$cache ?? new NullCache();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function clearCache()
|
|
|
|
{
|
|
|
|
$cache = self::getCache();
|
|
|
|
$cache->clear();
|
2023-07-08 18:03:12 +03:00
|
|
|
}
|
2022-08-18 23:52:01 +03:00
|
|
|
}
|