2019-02-06 20:34:51 +03:00
|
|
|
<?php
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-06-22 19:30:37 +03:00
|
|
|
class DisplayAction implements ActionInterface
|
|
|
|
{
|
2023-07-24 00:05:35 +03:00
|
|
|
private CacheInterface $cache;
|
2023-09-21 23:05:55 +03:00
|
|
|
private Logger $logger;
|
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->cache = RssBridge::getCache();
|
|
|
|
$this->logger = RssBridge::getLogger();
|
|
|
|
}
|
2023-07-24 00:05:35 +03:00
|
|
|
|
2022-07-08 22:06:14 +03:00
|
|
|
public function execute(array $request)
|
2019-02-06 20:34:51 +03:00
|
|
|
{
|
2023-09-10 22:50:15 +03:00
|
|
|
$cacheKey = 'http_' . json_encode($request);
|
2023-07-24 00:05:35 +03:00
|
|
|
/** @var Response $cachedResponse */
|
2023-09-10 22:50:15 +03:00
|
|
|
$cachedResponse = $this->cache->get($cacheKey);
|
|
|
|
if ($cachedResponse) {
|
|
|
|
$ifModifiedSince = $_SERVER['HTTP_IF_MODIFIED_SINCE'] ?? null;
|
|
|
|
$lastModified = $cachedResponse->getHeader('last-modified');
|
|
|
|
if ($ifModifiedSince && $lastModified) {
|
|
|
|
$lastModified = new \DateTimeImmutable($lastModified);
|
|
|
|
$lastModifiedTimestamp = $lastModified->getTimestamp();
|
|
|
|
$modifiedSince = strtotime($ifModifiedSince);
|
|
|
|
if ($lastModifiedTimestamp <= $modifiedSince) {
|
|
|
|
$modificationTimeGMT = gmdate('D, d M Y H:i:s ', $lastModifiedTimestamp);
|
|
|
|
return new Response('', 304, ['last-modified' => $modificationTimeGMT . 'GMT']);
|
|
|
|
}
|
|
|
|
}
|
2023-07-24 00:05:35 +03:00
|
|
|
return $cachedResponse;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2023-08-01 20:35:15 +03:00
|
|
|
$bridgeName = $request['bridge'] ?? null;
|
2023-09-10 22:50:15 +03:00
|
|
|
if (!$bridgeName) {
|
2023-09-23 19:54:14 +03:00
|
|
|
return new Response(render(__DIR__ . '/../templates/error.html.php', ['message' => 'Missing bridge parameter']), 400);
|
2023-09-10 22:50:15 +03:00
|
|
|
}
|
|
|
|
$bridgeFactory = new BridgeFactory();
|
2023-08-01 20:35:15 +03:00
|
|
|
$bridgeClassName = $bridgeFactory->createBridgeClassName($bridgeName);
|
|
|
|
if (!$bridgeClassName) {
|
2023-09-23 19:54:14 +03:00
|
|
|
return new Response(render(__DIR__ . '/../templates/error.html.php', ['message' => 'Bridge not found']), 404);
|
2023-08-01 20:35:15 +03:00
|
|
|
}
|
2023-09-10 22:50:15 +03:00
|
|
|
$format = $request['format'] ?? null;
|
2022-08-06 23:46:28 +03:00
|
|
|
if (!$format) {
|
2023-09-23 19:54:14 +03:00
|
|
|
return new Response(render(__DIR__ . '/../templates/error.html.php', ['message' => 'You must specify a format']), 400);
|
2022-08-06 23:46:28 +03:00
|
|
|
}
|
2023-06-11 04:16:03 +03:00
|
|
|
if (!$bridgeFactory->isEnabled($bridgeClassName)) {
|
2023-09-23 19:54:14 +03:00
|
|
|
return new Response(render(__DIR__ . '/../templates/error.html.php', ['message' => 'This bridge is not whitelisted']), 400);
|
2019-02-06 20:34:51 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2023-07-08 18:06:33 +03:00
|
|
|
$noproxy = $request['_noproxy'] ?? null;
|
|
|
|
if (
|
|
|
|
Configuration::getConfig('proxy', 'url')
|
|
|
|
&& Configuration::getConfig('proxy', 'by_bridge')
|
|
|
|
&& $noproxy
|
|
|
|
) {
|
|
|
|
// This const is only used once in getContents()
|
2019-02-06 20:34:51 +03:00
|
|
|
define('NOPROXY', true);
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2023-09-10 22:50:15 +03:00
|
|
|
$bridge = $bridgeFactory->create($bridgeClassName);
|
|
|
|
$formatFactory = new FormatFactory();
|
|
|
|
$format = $formatFactory->create($format);
|
|
|
|
|
|
|
|
$response = $this->createResponse($request, $bridge, $format);
|
|
|
|
|
|
|
|
if ($response->getCode() === 200) {
|
|
|
|
$ttl = $request['_cache_timeout'] ?? null;
|
|
|
|
if (Configuration::getConfig('cache', 'custom_timeout') && $ttl) {
|
|
|
|
$ttl = (int) $ttl;
|
|
|
|
} else {
|
|
|
|
$ttl = $bridge->getCacheTimeout();
|
|
|
|
}
|
|
|
|
$this->cache->set($cacheKey, $response, $ttl);
|
|
|
|
}
|
|
|
|
|
2023-12-13 23:56:14 +03:00
|
|
|
if (in_array($response->getCode(), [403, 429, 503])) {
|
|
|
|
// Cache these responses for about ~20 mins on average
|
|
|
|
$this->cache->set($cacheKey, $response, 60 * 15 + rand(1, 60 * 10));
|
2019-02-06 20:34:51 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2023-09-10 22:50:15 +03:00
|
|
|
if ($response->getCode() === 500) {
|
|
|
|
$this->cache->set($cacheKey, $response, 60 * 15);
|
|
|
|
}
|
2023-12-13 23:56:14 +03:00
|
|
|
|
2023-09-10 22:50:15 +03:00
|
|
|
if (rand(1, 100) === 2) {
|
|
|
|
$this->cache->prune();
|
|
|
|
}
|
2023-12-13 23:56:14 +03:00
|
|
|
|
2023-09-10 22:50:15 +03:00
|
|
|
return $response;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2023-09-25 22:18:48 +03:00
|
|
|
private function createResponse(array $request, BridgeAbstract $bridge, FormatAbstract $format)
|
2023-09-10 22:50:15 +03:00
|
|
|
{
|
2019-02-06 20:34:51 +03:00
|
|
|
$items = [];
|
2024-01-09 22:18:33 +03:00
|
|
|
$feed = [];
|
2023-07-19 06:05:49 +03:00
|
|
|
|
2023-09-10 22:50:15 +03:00
|
|
|
try {
|
|
|
|
$bridge->loadConfiguration();
|
|
|
|
// Remove parameters that don't concern bridges
|
2024-01-25 15:03:00 +03:00
|
|
|
$remove = [
|
|
|
|
'action',
|
|
|
|
'bridge',
|
|
|
|
'format',
|
|
|
|
'_noproxy',
|
|
|
|
'_cache_timeout',
|
|
|
|
'_error_time',
|
|
|
|
'_', // Some RSS readers add a cache-busting parameter (_=<timestamp>) to feed URLs, detect and ignore them.
|
|
|
|
];
|
|
|
|
$input = array_diff_key($request, array_fill_keys($remove, ''));
|
2023-10-01 20:23:30 +03:00
|
|
|
$bridge->setInput($input);
|
2023-09-10 22:50:15 +03:00
|
|
|
$bridge->collectData();
|
|
|
|
$items = $bridge->getItems();
|
|
|
|
if (isset($items[0]) && is_array($items[0])) {
|
|
|
|
$feedItems = [];
|
|
|
|
foreach ($items as $item) {
|
2023-09-25 22:18:48 +03:00
|
|
|
$feedItems[] = FeedItem::fromArray($item);
|
2019-02-06 20:34:51 +03:00
|
|
|
}
|
2023-09-10 22:50:15 +03:00
|
|
|
$items = $feedItems;
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
2024-01-09 22:18:33 +03:00
|
|
|
$feed = $bridge->getFeed();
|
2023-09-10 22:50:15 +03:00
|
|
|
} catch (\Exception $e) {
|
2024-01-25 01:06:23 +03:00
|
|
|
// Probably an exception inside a bridge
|
2023-09-10 22:50:15 +03:00
|
|
|
if ($e instanceof HttpException) {
|
|
|
|
// Reproduce (and log) these responses regardless of error output and report limit
|
|
|
|
if ($e->getCode() === 429) {
|
2023-09-21 23:05:55 +03:00
|
|
|
$this->logger->info(sprintf('Exception in DisplayAction(%s): %s', $bridge->getShortName(), create_sane_exception_message($e)));
|
2023-09-23 19:54:14 +03:00
|
|
|
return new Response(render(__DIR__ . '/../templates/exception.html.php', ['e' => $e]), 429);
|
2022-11-18 23:36:06 +03:00
|
|
|
}
|
2023-09-10 22:50:15 +03:00
|
|
|
if ($e->getCode() === 503) {
|
2023-09-21 23:05:55 +03:00
|
|
|
$this->logger->info(sprintf('Exception in DisplayAction(%s): %s', $bridge->getShortName(), create_sane_exception_message($e)));
|
2023-09-23 19:54:14 +03:00
|
|
|
return new Response(render(__DIR__ . '/../templates/exception.html.php', ['e' => $e]), 503);
|
2023-07-24 00:05:35 +03:00
|
|
|
}
|
2023-09-10 22:50:15 +03:00
|
|
|
}
|
2023-09-21 23:05:55 +03:00
|
|
|
$this->logger->error(sprintf('Exception in DisplayAction(%s)', $bridge->getShortName()), ['e' => $e]);
|
2023-09-20 03:45:48 +03:00
|
|
|
$errorOutput = Configuration::getConfig('error', 'output');
|
|
|
|
$reportLimit = Configuration::getConfig('error', 'report_limit');
|
2023-09-10 22:50:15 +03:00
|
|
|
$errorCount = 1;
|
|
|
|
if ($reportLimit > 1) {
|
|
|
|
$errorCount = $this->logBridgeError($bridge->getName(), $e->getCode());
|
|
|
|
}
|
|
|
|
// Let clients know about the error if we are passed the report limit
|
|
|
|
if ($errorCount >= $reportLimit) {
|
|
|
|
if ($errorOutput === 'feed') {
|
|
|
|
// Render the exception as a feed item
|
|
|
|
$items[] = $this->createFeedItemFromException($e, $bridge);
|
|
|
|
} elseif ($errorOutput === 'http') {
|
2023-09-23 19:54:14 +03:00
|
|
|
return new Response(render(__DIR__ . '/../templates/exception.html.php', ['e' => $e]), 500);
|
2023-09-10 22:50:15 +03:00
|
|
|
} elseif ($errorOutput === 'none') {
|
|
|
|
// Do nothing (produces an empty feed)
|
2019-10-31 20:40:51 +03:00
|
|
|
}
|
2019-02-06 20:34:51 +03:00
|
|
|
}
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-07-08 21:39:13 +03:00
|
|
|
$format->setItems($items);
|
2024-01-09 22:18:33 +03:00
|
|
|
$format->setFeed($feed);
|
2023-09-10 22:50:15 +03:00
|
|
|
$now = time();
|
|
|
|
$format->setLastModified($now);
|
|
|
|
$headers = [
|
|
|
|
'last-modified' => gmdate('D, d M Y H:i:s ', $now) . 'GMT',
|
|
|
|
'content-type' => $format->getMimeType() . '; charset=' . $format->getCharset(),
|
|
|
|
];
|
2022-11-07 20:22:54 +03:00
|
|
|
return new Response($format->stringify(), 200, $headers);
|
2022-07-08 21:39:13 +03:00
|
|
|
}
|
|
|
|
|
2023-09-20 03:45:48 +03:00
|
|
|
private function createFeedItemFromException($e, BridgeAbstract $bridge): FeedItem
|
2023-02-03 00:53:01 +03:00
|
|
|
{
|
|
|
|
$item = new FeedItem();
|
|
|
|
|
|
|
|
// Create a unique identifier every 24 hours
|
|
|
|
$uniqueIdentifier = urlencode((int)(time() / 86400));
|
2023-09-25 22:18:48 +03:00
|
|
|
$title = sprintf('Bridge returned error %s! (%s)', $e->getCode(), $uniqueIdentifier);
|
|
|
|
$item->setTitle($title);
|
2023-02-03 00:53:01 +03:00
|
|
|
$item->setURI(get_current_url());
|
|
|
|
$item->setTimestamp(time());
|
|
|
|
|
2023-06-02 21:22:09 +03:00
|
|
|
// Create an item identifier for feed readers e.g. "staysafetv twitch videos_19389"
|
2023-02-03 00:53:01 +03:00
|
|
|
$item->setUid($bridge->getName() . '_' . $uniqueIdentifier);
|
|
|
|
|
|
|
|
$content = render_template(__DIR__ . '/../templates/bridge-error.html.php', [
|
2023-09-23 19:54:14 +03:00
|
|
|
'error' => render_template(__DIR__ . '/../templates/exception.html.php', ['e' => $e]),
|
2023-02-03 00:53:01 +03:00
|
|
|
'searchUrl' => self::createGithubSearchUrl($bridge),
|
|
|
|
'issueUrl' => self::createGithubIssueUrl($bridge, $e, create_sane_exception_message($e)),
|
|
|
|
'maintainer' => $bridge->getMaintainer(),
|
|
|
|
]);
|
|
|
|
$item->setContent($content);
|
|
|
|
return $item;
|
|
|
|
}
|
|
|
|
|
2023-07-24 00:05:35 +03:00
|
|
|
private function logBridgeError($bridgeName, $code)
|
2023-02-03 00:53:01 +03:00
|
|
|
{
|
2023-12-13 23:56:14 +03:00
|
|
|
// todo: it's not really necessary to json encode $report
|
2023-09-10 22:50:15 +03:00
|
|
|
$cacheKey = 'error_reporting_' . $bridgeName . '_' . $code;
|
|
|
|
$report = $this->cache->get($cacheKey);
|
2023-07-24 00:05:35 +03:00
|
|
|
if ($report) {
|
2023-02-03 00:53:01 +03:00
|
|
|
$report = Json::decode($report);
|
|
|
|
$report['time'] = time();
|
|
|
|
$report['count']++;
|
|
|
|
} else {
|
|
|
|
$report = [
|
|
|
|
'error' => $code,
|
|
|
|
'time' => time(),
|
|
|
|
'count' => 1,
|
|
|
|
];
|
|
|
|
}
|
2023-09-10 22:50:15 +03:00
|
|
|
$ttl = 86400 * 5;
|
|
|
|
$this->cache->set($cacheKey, Json::encode($report), $ttl);
|
2023-02-03 00:53:01 +03:00
|
|
|
return $report['count'];
|
|
|
|
}
|
|
|
|
|
2022-07-08 21:39:13 +03:00
|
|
|
private static function createGithubIssueUrl($bridge, $e, string $message): string
|
|
|
|
{
|
|
|
|
return sprintf('https://github.com/RSS-Bridge/rss-bridge/issues/new?%s', http_build_query([
|
|
|
|
'title' => sprintf('%s failed with error %s', $bridge->getName(), $e->getCode()),
|
|
|
|
'body' => sprintf(
|
2022-09-04 08:21:57 +03:00
|
|
|
"```\n%s\n\n%s\n\nQuery string: %s\nVersion: %s\nOs: %s\nPHP version: %s\n```",
|
2022-07-08 21:39:13 +03:00
|
|
|
$message,
|
2022-10-16 18:55:43 +03:00
|
|
|
implode("\n", trace_to_call_points(trace_from_exception($e))),
|
2022-07-08 21:39:13 +03:00
|
|
|
$_SERVER['QUERY_STRING'] ?? '',
|
|
|
|
Configuration::getVersion(),
|
2022-08-03 18:05:13 +03:00
|
|
|
PHP_OS_FAMILY,
|
|
|
|
phpversion() ?: 'Unknown'
|
2022-07-08 21:39:13 +03:00
|
|
|
),
|
|
|
|
'labels' => 'Bridge-Broken',
|
|
|
|
'assignee' => $bridge->getMaintainer(),
|
|
|
|
]));
|
|
|
|
}
|
|
|
|
|
|
|
|
private static function createGithubSearchUrl($bridge): string
|
|
|
|
{
|
|
|
|
return sprintf(
|
|
|
|
'https://github.com/RSS-Bridge/rss-bridge/issues?q=%s',
|
|
|
|
urlencode('is:issue is:open ' . $bridge->getName())
|
|
|
|
);
|
2019-02-06 20:34:51 +03:00
|
|
|
}
|
|
|
|
}
|