2022-08-18 23:52:01 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
final class RssBridge
|
|
|
|
{
|
2023-07-08 18:03:12 +03:00
|
|
|
private static CacheInterface $cache;
|
2023-09-21 23:05:55 +03:00
|
|
|
private static Logger $logger;
|
|
|
|
private static HttpClient $httpClient;
|
2023-07-08 18:03:12 +03:00
|
|
|
|
2023-09-10 22:50:15 +03:00
|
|
|
public function __construct()
|
2022-08-18 23:52:01 +03:00
|
|
|
{
|
2023-09-21 23:05:55 +03:00
|
|
|
self::$logger = new SimpleLogger('rssbridge');
|
|
|
|
if (Debug::isEnabled()) {
|
|
|
|
self::$logger->addHandler(new StreamHandler(Logger::DEBUG));
|
|
|
|
} else {
|
|
|
|
self::$logger->addHandler(new StreamHandler(Logger::INFO));
|
|
|
|
}
|
2023-07-16 23:07:34 +03:00
|
|
|
self::$httpClient = new CurlHttpClient();
|
2023-09-21 23:05:55 +03:00
|
|
|
$cacheFactory = new CacheFactory(self::$logger);
|
2023-09-10 22:50:15 +03:00
|
|
|
if (Debug::isEnabled()) {
|
|
|
|
self::$cache = $cacheFactory->create('array');
|
|
|
|
} else {
|
|
|
|
self::$cache = $cacheFactory->create();
|
|
|
|
}
|
|
|
|
}
|
2022-08-18 23:52:01 +03:00
|
|
|
|
2024-01-25 01:06:23 +03:00
|
|
|
public function main(array $argv = []): Response
|
2023-09-10 22:50:15 +03:00
|
|
|
{
|
|
|
|
if ($argv) {
|
|
|
|
parse_str(implode('&', array_slice($argv, 1)), $cliArgs);
|
2024-01-25 18:06:24 +03:00
|
|
|
$request = Request::fromCli($cliArgs);
|
2023-09-10 22:50:15 +03:00
|
|
|
} else {
|
2024-01-25 18:06:24 +03:00
|
|
|
$request = Request::fromGlobals();
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($request->toArray() as $key => $value) {
|
|
|
|
if (!is_string($value)) {
|
|
|
|
return new Response(render(__DIR__ . '/../templates/error.html.php', [
|
|
|
|
'message' => "Query parameter \"$key\" is not a string.",
|
|
|
|
]), 400);
|
|
|
|
}
|
2022-08-18 23:52:01 +03:00
|
|
|
}
|
|
|
|
|
2024-01-25 01:06:23 +03:00
|
|
|
if (Configuration::getConfig('system', 'enable_maintenance_mode')) {
|
|
|
|
return new Response(render(__DIR__ . '/../templates/error.html.php', [
|
|
|
|
'title' => '503 Service Unavailable',
|
|
|
|
'message' => 'RSS-Bridge is down for maintenance.',
|
|
|
|
]), 503);
|
|
|
|
}
|
2023-09-10 22:50:15 +03:00
|
|
|
|
2024-01-25 20:20:02 +03:00
|
|
|
// HTTP Basic auth check
|
2024-01-25 01:06:23 +03:00
|
|
|
if (Configuration::getConfig('authentication', 'enable')) {
|
|
|
|
if (Configuration::getConfig('authentication', 'password') === '') {
|
|
|
|
return new Response('The authentication password cannot be the empty string', 500);
|
2023-09-10 22:50:15 +03:00
|
|
|
}
|
2024-01-25 18:06:24 +03:00
|
|
|
$user = $request->server('PHP_AUTH_USER');
|
|
|
|
$password = $request->server('PHP_AUTH_PW');
|
2024-01-25 01:06:23 +03:00
|
|
|
if ($user === null || $password === null) {
|
|
|
|
$html = render(__DIR__ . '/../templates/error.html.php', [
|
|
|
|
'message' => 'Please authenticate in order to access this instance!',
|
|
|
|
]);
|
|
|
|
return new Response($html, 401, ['WWW-Authenticate' => 'Basic realm="RSS-Bridge"']);
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
(Configuration::getConfig('authentication', 'username') !== $user)
|
|
|
|
|| (! hash_equals(Configuration::getConfig('authentication', 'password'), $password))
|
|
|
|
) {
|
|
|
|
$html = render(__DIR__ . '/../templates/error.html.php', [
|
|
|
|
'message' => 'Please authenticate in order to access this instance!',
|
|
|
|
]);
|
|
|
|
return new Response($html, 401, ['WWW-Authenticate' => 'Basic realm="RSS-Bridge"']);
|
|
|
|
}
|
|
|
|
// At this point the username and password was correct
|
|
|
|
}
|
2023-09-10 22:50:15 +03:00
|
|
|
|
2024-01-25 20:20:02 +03:00
|
|
|
// Add token as attribute to request
|
|
|
|
$request = $request->withAttribute('token', $request->get('token'));
|
|
|
|
|
|
|
|
// Token authentication check
|
|
|
|
if (Configuration::getConfig('authentication', 'token')) {
|
|
|
|
if (! $request->attribute('token')) {
|
|
|
|
return new Response(render(__DIR__ . '/../templates/token.html.php', [
|
|
|
|
'message' => '',
|
|
|
|
]), 401);
|
|
|
|
}
|
|
|
|
if (! hash_equals(Configuration::getConfig('authentication', 'token'), $request->attribute('token'))) {
|
|
|
|
return new Response(render(__DIR__ . '/../templates/token.html.php', [
|
|
|
|
'message' => 'Invalid token',
|
|
|
|
]), 401);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-25 18:06:24 +03:00
|
|
|
$action = $request->get('action', 'Frontpage');
|
|
|
|
$actionName = strtolower($action) . 'Action';
|
2024-01-25 01:06:23 +03:00
|
|
|
$actionName = implode(array_map('ucfirst', explode('-', $actionName)));
|
|
|
|
$filePath = __DIR__ . '/../actions/' . $actionName . '.php';
|
|
|
|
if (!file_exists($filePath)) {
|
|
|
|
return new Response(render(__DIR__ . '/../templates/error.html.php', ['message' => 'Invalid action']), 400);
|
|
|
|
}
|
|
|
|
|
|
|
|
$className = '\\' . $actionName;
|
2024-01-25 18:06:24 +03:00
|
|
|
$actionObject = new $className();
|
2024-01-25 01:06:23 +03:00
|
|
|
|
2024-01-25 18:06:24 +03:00
|
|
|
$response = $actionObject->execute($request);
|
2024-01-25 01:06:23 +03:00
|
|
|
|
|
|
|
if (is_string($response)) {
|
|
|
|
$response = new Response($response);
|
|
|
|
}
|
|
|
|
return $response;
|
2022-08-18 23:52:01 +03:00
|
|
|
}
|
2023-07-08 18:03:12 +03:00
|
|
|
|
2023-09-21 23:05:55 +03:00
|
|
|
public static function getCache(): CacheInterface
|
2023-07-08 18:03:12 +03:00
|
|
|
{
|
2023-09-21 23:05:55 +03:00
|
|
|
return self::$cache;
|
2023-07-08 18:03:12 +03:00
|
|
|
}
|
|
|
|
|
2023-09-21 23:05:55 +03:00
|
|
|
public static function getLogger(): Logger
|
2023-07-08 18:03:12 +03:00
|
|
|
{
|
2023-09-21 23:05:55 +03:00
|
|
|
return self::$logger;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function getHttpClient(): HttpClient
|
|
|
|
{
|
|
|
|
return self::$httpClient;
|
2023-09-10 22:50:15 +03:00
|
|
|
}
|
2022-08-18 23:52:01 +03:00
|
|
|
}
|