2019-02-06 20:34:51 +03:00
|
|
|
<?php
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2019-02-06 20:34:51 +03:00
|
|
|
/**
|
|
|
|
* This file is part of RSS-Bridge, a PHP project capable of generating RSS and
|
|
|
|
* Atom feeds for websites that don't have one.
|
|
|
|
*
|
|
|
|
* For the full license information, please view the UNLICENSE file distributed
|
|
|
|
* with this source code.
|
|
|
|
*
|
|
|
|
* @package Core
|
|
|
|
* @license http://unlicense.org/ UNLICENSE
|
|
|
|
* @link https://github.com/rss-bridge/rss-bridge
|
|
|
|
*/
|
|
|
|
|
2022-06-22 19:30:37 +03:00
|
|
|
class DisplayAction implements ActionInterface
|
|
|
|
{
|
2022-07-08 22:06:14 +03:00
|
|
|
public function execute(array $request)
|
2019-02-06 20:34:51 +03:00
|
|
|
{
|
2023-07-06 16:09:44 +03:00
|
|
|
if (Configuration::getConfig('system', 'enable_maintenance_mode')) {
|
|
|
|
return new Response('503 Service Unavailable', 503);
|
|
|
|
}
|
|
|
|
|
2022-08-06 23:46:28 +03:00
|
|
|
$bridgeFactory = new BridgeFactory();
|
2022-07-08 13:54:23 +03:00
|
|
|
|
2023-06-11 04:16:03 +03:00
|
|
|
$bridgeClassName = $bridgeFactory->createBridgeClassName($request['bridge'] ?? '');
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-08-06 23:46:28 +03:00
|
|
|
$format = $request['format'] ?? null;
|
|
|
|
if (!$format) {
|
|
|
|
throw new \Exception('You must specify a format!');
|
|
|
|
}
|
2023-06-11 04:16:03 +03:00
|
|
|
if (!$bridgeFactory->isEnabled($bridgeClassName)) {
|
2022-08-06 23:46:28 +03:00
|
|
|
throw new \Exception('This bridge is not whitelisted');
|
2019-02-06 20:34:51 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-09-08 20:07:57 +03:00
|
|
|
$formatFactory = new FormatFactory();
|
|
|
|
$format = $formatFactory->create($format);
|
|
|
|
|
2022-07-08 13:54:23 +03:00
|
|
|
$bridge = $bridgeFactory->create($bridgeClassName);
|
2022-01-25 21:52:28 +03:00
|
|
|
$bridge->loadConfiguration();
|
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-07-08 18:06:33 +03:00
|
|
|
$cacheTimeout = $request['_cache_timeout'] ?? null;
|
|
|
|
if (Configuration::getConfig('cache', 'custom_timeout') && $cacheTimeout) {
|
|
|
|
$cacheTimeout = (int) $cacheTimeout;
|
2019-02-06 20:34:51 +03:00
|
|
|
} else {
|
2023-07-08 18:06:33 +03:00
|
|
|
// At this point the query argument might still be in the url but it won't be used
|
|
|
|
$cacheTimeout = $bridge->getCacheTimeout();
|
2019-02-06 20:34:51 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2019-02-06 20:34:51 +03:00
|
|
|
// Remove parameters that don't concern bridges
|
|
|
|
$bridge_params = array_diff_key(
|
2022-07-08 22:06:14 +03:00
|
|
|
$request,
|
2019-02-06 20:34:51 +03:00
|
|
|
array_fill_keys(
|
2022-07-01 16:10:30 +03:00
|
|
|
[
|
2019-02-06 20:34:51 +03:00
|
|
|
'action',
|
|
|
|
'bridge',
|
|
|
|
'format',
|
|
|
|
'_noproxy',
|
|
|
|
'_cache_timeout',
|
|
|
|
'_error_time'
|
|
|
|
],
|
|
|
|
''
|
|
|
|
)
|
|
|
|
);
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2019-02-06 20:34:51 +03:00
|
|
|
// Remove parameters that don't concern caches
|
|
|
|
$cache_params = array_diff_key(
|
2022-07-08 22:06:14 +03:00
|
|
|
$request,
|
2019-02-06 20:34:51 +03:00
|
|
|
array_fill_keys(
|
2022-07-01 16:10:30 +03:00
|
|
|
[
|
2019-02-06 20:34:51 +03:00
|
|
|
'action',
|
|
|
|
'format',
|
|
|
|
'_noproxy',
|
|
|
|
'_cache_timeout',
|
|
|
|
'_error_time'
|
|
|
|
],
|
|
|
|
''
|
|
|
|
)
|
|
|
|
);
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2023-07-08 18:03:12 +03:00
|
|
|
$cache = RssBridge::getCache();
|
2019-04-29 21:12:43 +03:00
|
|
|
$cache->setScope('');
|
|
|
|
$cache->setKey($cache_params);
|
2023-07-06 19:52:19 +03:00
|
|
|
// This cache purge will basically delete all cache items older than 24h, regardless of scope and key
|
|
|
|
$cache->purgeCache(86400);
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2019-02-06 20:34:51 +03:00
|
|
|
$items = [];
|
|
|
|
$infos = [];
|
|
|
|
$mtime = $cache->getTime();
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2019-02-06 20:34:51 +03:00
|
|
|
if (
|
2023-07-05 18:37:21 +03:00
|
|
|
$mtime
|
2023-07-08 18:06:33 +03:00
|
|
|
&& (time() - $cacheTimeout < $mtime)
|
2019-02-06 20:34:51 +03:00
|
|
|
&& !Debug::isEnabled()
|
2022-08-06 23:46:28 +03:00
|
|
|
) {
|
2023-06-02 21:22:09 +03:00
|
|
|
// At this point we found the feed in the cache and debug mode is disabled
|
2023-02-03 00:53:01 +03:00
|
|
|
|
2019-02-06 20:34:51 +03:00
|
|
|
if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
|
2023-02-03 00:53:01 +03:00
|
|
|
// The client wants to know if the feed has changed since its last check
|
2019-02-06 20:34:51 +03:00
|
|
|
$stime = strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']);
|
2022-08-06 23:46:28 +03:00
|
|
|
if ($mtime <= $stime) {
|
2022-11-07 20:22:54 +03:00
|
|
|
$lastModified2 = gmdate('D, d M Y H:i:s ', $mtime) . 'GMT';
|
|
|
|
return new Response('', 304, ['Last-Modified' => $lastModified2]);
|
2019-02-06 20:34:51 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
|
|
|
|
2023-06-02 21:22:09 +03:00
|
|
|
// Load the feed from cache and prepare it
|
2019-02-06 20:34:51 +03:00
|
|
|
$cached = $cache->loadData();
|
|
|
|
if (isset($cached['items']) && isset($cached['extraInfos'])) {
|
|
|
|
foreach ($cached['items'] as $item) {
|
2022-08-06 23:46:28 +03:00
|
|
|
$items[] = new FeedItem($item);
|
2019-02-06 20:34:51 +03:00
|
|
|
}
|
|
|
|
$infos = $cached['extraInfos'];
|
|
|
|
}
|
2022-08-06 23:46:28 +03:00
|
|
|
} else {
|
2023-06-02 21:22:09 +03:00
|
|
|
// At this point we did NOT find the feed in the cache or debug mode is enabled.
|
2019-02-06 20:34:51 +03:00
|
|
|
try {
|
|
|
|
$bridge->setDatas($bridge_params);
|
|
|
|
$bridge->collectData();
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2019-02-06 20:34:51 +03:00
|
|
|
$items = $bridge->getItems();
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2019-02-06 20:34:51 +03:00
|
|
|
if (isset($items[0]) && is_array($items[0])) {
|
|
|
|
$feedItems = [];
|
|
|
|
foreach ($items as $item) {
|
2022-08-06 23:46:28 +03:00
|
|
|
$feedItems[] = new FeedItem($item);
|
2019-02-06 20:34:51 +03:00
|
|
|
}
|
|
|
|
$items = $feedItems;
|
|
|
|
}
|
|
|
|
$infos = [
|
|
|
|
'name' => $bridge->getName(),
|
|
|
|
'uri' => $bridge->getURI(),
|
2021-06-25 22:45:25 +03:00
|
|
|
'donationUri' => $bridge->getDonationURI(),
|
2019-02-06 20:34:51 +03:00
|
|
|
'icon' => $bridge->getIcon()
|
|
|
|
];
|
2023-07-16 08:18:38 +03:00
|
|
|
} catch (\Exception $e) {
|
2022-11-18 23:36:06 +03:00
|
|
|
if ($e instanceof HttpException) {
|
2023-07-16 08:18:38 +03:00
|
|
|
Logger::warning(sprintf('Exception in DisplayAction(%s): %s', $bridgeClassName, create_sane_exception_message($e)));
|
|
|
|
if ($e->getCode() === 429) {
|
|
|
|
return new Response('503 Service Unavailable', 503);
|
|
|
|
}
|
2022-11-18 23:36:06 +03:00
|
|
|
} else {
|
2023-07-16 08:18:38 +03:00
|
|
|
Logger::error(sprintf('Exception in DisplayAction(%s): %s', $bridgeClassName, create_sane_exception_message($e)), ['e' => $e]);
|
2022-11-18 23:36:06 +03:00
|
|
|
}
|
|
|
|
|
2023-02-03 00:53:01 +03:00
|
|
|
// Emit error only if we are passed the error report limit
|
|
|
|
$errorCount = self::logBridgeError($bridge->getName(), $e->getCode());
|
2022-08-06 23:46:28 +03:00
|
|
|
if ($errorCount >= Configuration::getConfig('error', 'report_limit')) {
|
2019-10-31 20:49:45 +03:00
|
|
|
if (Configuration::getConfig('error', 'output') === 'feed') {
|
2023-02-03 00:53:01 +03:00
|
|
|
// Emit the error as a feed item in a feed so that feed readers can pick it up
|
|
|
|
$items[] = $this->createFeedItemFromException($e, $bridge);
|
2019-10-31 20:49:45 +03:00
|
|
|
} elseif (Configuration::getConfig('error', 'output') === 'http') {
|
2022-07-08 21:39:13 +03:00
|
|
|
throw $e;
|
2019-10-31 20:49:45 +03:00
|
|
|
}
|
2019-10-31 20:40:51 +03:00
|
|
|
}
|
2019-02-06 20:34:51 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2023-07-08 18:03:12 +03:00
|
|
|
// Unfortunately need to set scope and key again because they might be modified
|
|
|
|
$cache->setScope('');
|
|
|
|
$cache->setKey($cache_params);
|
2019-02-06 20:34:51 +03:00
|
|
|
$cache->saveData([
|
2022-08-06 23:46:28 +03:00
|
|
|
'items' => array_map(function (FeedItem $item) {
|
|
|
|
return $item->toArray();
|
2019-02-06 20:34:51 +03:00
|
|
|
}, $items),
|
|
|
|
'extraInfos' => $infos
|
|
|
|
]);
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-07-08 21:39:13 +03:00
|
|
|
$format->setItems($items);
|
|
|
|
$format->setExtraInfos($infos);
|
|
|
|
$lastModified = $cache->getTime();
|
|
|
|
$format->setLastModified($lastModified);
|
2022-11-07 20:22:54 +03:00
|
|
|
$headers = [];
|
2022-07-08 21:39:13 +03:00
|
|
|
if ($lastModified) {
|
2022-11-07 20:22:54 +03:00
|
|
|
$headers['Last-Modified'] = gmdate('D, d M Y H:i:s ', $lastModified) . 'GMT';
|
2019-02-06 20:34:51 +03:00
|
|
|
}
|
2022-11-07 20:22:54 +03:00
|
|
|
$headers['Content-Type'] = $format->getMimeType() . '; charset=' . $format->getCharset();
|
|
|
|
return new Response($format->stringify(), 200, $headers);
|
2022-07-08 21:39:13 +03:00
|
|
|
}
|
|
|
|
|
2023-02-03 00:53:01 +03:00
|
|
|
private function createFeedItemFromException($e, BridgeInterface $bridge): FeedItem
|
|
|
|
{
|
|
|
|
$item = new FeedItem();
|
|
|
|
|
|
|
|
// Create a unique identifier every 24 hours
|
|
|
|
$uniqueIdentifier = urlencode((int)(time() / 86400));
|
|
|
|
$itemTitle = sprintf('Bridge returned error %s! (%s)', $e->getCode(), $uniqueIdentifier);
|
|
|
|
$item->setTitle($itemTitle);
|
|
|
|
$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', [
|
|
|
|
'error' => render_template(__DIR__ . '/../templates/error.html.php', ['e' => $e]),
|
|
|
|
'searchUrl' => self::createGithubSearchUrl($bridge),
|
|
|
|
'issueUrl' => self::createGithubIssueUrl($bridge, $e, create_sane_exception_message($e)),
|
|
|
|
'maintainer' => $bridge->getMaintainer(),
|
|
|
|
]);
|
|
|
|
$item->setContent($content);
|
|
|
|
return $item;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static function logBridgeError($bridgeName, $code)
|
|
|
|
{
|
2023-07-08 18:03:12 +03:00
|
|
|
$cache = RssBridge::getCache();
|
2023-02-03 00:53:01 +03:00
|
|
|
$cache->setScope('error_reporting');
|
|
|
|
$cache->setkey([$bridgeName . '_' . $code]);
|
2023-07-06 19:52:19 +03:00
|
|
|
|
2023-02-03 00:53:01 +03:00
|
|
|
if ($report = $cache->loadData()) {
|
|
|
|
$report = Json::decode($report);
|
|
|
|
$report['time'] = time();
|
|
|
|
$report['count']++;
|
|
|
|
} else {
|
|
|
|
$report = [
|
|
|
|
'error' => $code,
|
|
|
|
'time' => time(),
|
|
|
|
'count' => 1,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
$cache->saveData(Json::encode($report));
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|