2013-08-09 20:57:25 +04:00
|
|
|
<?php
|
2018-06-27 20:09:41 +03:00
|
|
|
|
2024-01-25 01:06:23 +03:00
|
|
|
if (version_compare(\PHP_VERSION, '7.4.0') === -1) {
|
|
|
|
http_response_code(500);
|
2024-08-07 04:15:43 +03:00
|
|
|
exit("RSS-Bridge requires minimum PHP version 7.4\n");
|
2024-01-25 01:06:23 +03:00
|
|
|
}
|
2018-06-27 20:09:41 +03:00
|
|
|
|
2024-08-07 04:15:43 +03:00
|
|
|
require_once __DIR__ . '/lib/bootstrap.php';
|
|
|
|
|
|
|
|
$config = [];
|
|
|
|
if (file_exists(__DIR__ . '/config.ini.php')) {
|
|
|
|
$config = parse_ini_file(__DIR__ . '/config.ini.php', true, INI_SCANNER_TYPED);
|
|
|
|
if (!$config) {
|
|
|
|
http_response_code(500);
|
|
|
|
exit("Error parsing config.ini.php\n");
|
|
|
|
}
|
2024-02-02 20:06:08 +03:00
|
|
|
}
|
2024-08-07 04:15:43 +03:00
|
|
|
Configuration::loadConfiguration($config, getenv());
|
2024-02-02 20:06:08 +03:00
|
|
|
|
2024-08-07 04:15:43 +03:00
|
|
|
$logger = new SimpleLogger('rssbridge');
|
2023-10-12 23:14:04 +03:00
|
|
|
|
2024-08-07 04:15:43 +03:00
|
|
|
set_exception_handler(function (\Throwable $e) use ($logger) {
|
2024-01-25 01:06:23 +03:00
|
|
|
$response = new Response(render(__DIR__ . '/templates/exception.html.php', ['e' => $e]), 500);
|
|
|
|
$response->send();
|
2024-08-07 04:15:43 +03:00
|
|
|
$logger->error('Uncaught Exception', ['e' => $e]);
|
2023-10-12 23:14:04 +03:00
|
|
|
});
|
|
|
|
|
2024-08-07 04:15:43 +03:00
|
|
|
set_error_handler(function ($code, $message, $file, $line) use ($logger) {
|
|
|
|
// Consider: ini_set('error_reporting', E_ALL & ~E_DEPRECATED);
|
2023-10-12 23:14:04 +03:00
|
|
|
if ((error_reporting() & $code) === 0) {
|
2024-01-25 01:06:23 +03:00
|
|
|
// Deprecation messages and other masked errors are typically ignored here
|
2023-10-12 23:14:04 +03:00
|
|
|
return false;
|
|
|
|
}
|
2024-07-31 18:51:44 +03:00
|
|
|
if (Debug::isEnabled()) {
|
|
|
|
// This might be annoying, but it's for the greater good
|
|
|
|
throw new \ErrorException($message, 0, $code, $file, $line);
|
|
|
|
}
|
2023-10-12 23:14:04 +03:00
|
|
|
$text = sprintf(
|
|
|
|
'%s at %s line %s',
|
|
|
|
sanitize_root($message),
|
|
|
|
sanitize_root($file),
|
|
|
|
$line
|
|
|
|
);
|
2024-08-07 04:15:43 +03:00
|
|
|
$logger->warning($text);
|
|
|
|
// todo: return false to prevent default error handler from running?
|
2023-10-12 23:14:04 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
// There might be some fatal errors which are not caught by set_error_handler() or \Throwable.
|
2024-08-07 04:15:43 +03:00
|
|
|
register_shutdown_function(function () use ($logger) {
|
2023-10-12 23:14:04 +03:00
|
|
|
$error = error_get_last();
|
|
|
|
if ($error) {
|
|
|
|
$message = sprintf(
|
|
|
|
'(shutdown) %s: %s in %s line %s',
|
|
|
|
$error['type'],
|
|
|
|
sanitize_root($error['message']),
|
|
|
|
sanitize_root($error['file']),
|
|
|
|
$error['line']
|
|
|
|
);
|
2024-08-07 04:15:43 +03:00
|
|
|
$logger->error($message);
|
2023-10-12 23:14:04 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-08-07 04:15:43 +03:00
|
|
|
$cacheFactory = new CacheFactory($logger);
|
2024-08-07 19:09:44 +03:00
|
|
|
|
2024-08-07 04:15:43 +03:00
|
|
|
if (Debug::isEnabled()) {
|
2024-08-07 19:09:44 +03:00
|
|
|
$logger->addHandler(new ErrorLogHandler(Logger::DEBUG));
|
2024-08-07 04:15:43 +03:00
|
|
|
$cache = $cacheFactory->create('array');
|
|
|
|
} else {
|
2024-08-07 19:09:44 +03:00
|
|
|
$logger->addHandler(new ErrorLogHandler(Logger::INFO));
|
2024-08-07 04:15:43 +03:00
|
|
|
$cache = $cacheFactory->create();
|
2024-01-25 01:06:23 +03:00
|
|
|
}
|
2024-08-07 04:15:43 +03:00
|
|
|
$httpClient = new CurlHttpClient();
|
2024-01-25 01:06:23 +03:00
|
|
|
|
|
|
|
date_default_timezone_set(Configuration::getConfig('system', 'timezone'));
|
|
|
|
|
|
|
|
try {
|
2024-08-07 04:15:43 +03:00
|
|
|
$rssBridge = new RssBridge($logger, $cache, $httpClient);
|
2024-01-25 01:06:23 +03:00
|
|
|
$response = $rssBridge->main($argv ?? []);
|
|
|
|
$response->send();
|
|
|
|
} catch (\Throwable $e) {
|
|
|
|
// Probably an exception inside an action
|
2024-08-07 04:15:43 +03:00
|
|
|
$logger->error('Exception in RssBridge::main()', ['e' => $e]);
|
|
|
|
$response = new Response(render(__DIR__ . '/templates/exception.html.php', ['e' => $e]), 500);
|
|
|
|
$response->send();
|
2024-01-25 01:06:23 +03:00
|
|
|
}
|