Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

display feed icons on frontpage #3787

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 129 additions & 0 deletions actions/FeedIconAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
<?php

class FeedIconAction implements ActionInterface
{
private CacheInterface $cache;
private BridgeFactory $bridgeFactory;

public function __construct()
{
$this->cache = RssBridge::getCache();
$this->bridgeFactory = new BridgeFactory();
}

public function execute(array $request): void
{
$bridgeClassName = $request['bridgeClassName'] ?? null;

if (!$bridgeClassName || !class_exists($bridgeClassName)) {
$this->sendNotFoundResponse();
}

$cacheKey = $this->buildCacheKey($bridgeClassName);

list(
'imageData' => $imageData,
'mimeType' => $mimeType
) = $this->readCache($cacheKey);

if ($imageData && $mimeType) {
$this->sendImageResponse($mimeType, $imageData);
} elseif ('' === $imageData && '' === $mimeType) {
// empty values means that the image was broken and could not be loaded
$this->sendNotFoundResponse();
}

$bridge = $this->bridgeFactory->create($bridgeClassName);
$imageUrl = $bridge->getIcon();

$imageUrl = $this->cleanImageUrl($imageUrl);

if (!filter_var($imageUrl, FILTER_VALIDATE_URL)) {
//store empty values to prevent further attempts to load broken image
$this->writeCache($cacheKey, '', '');
$this->sendNotFoundResponse();
}

list(
'imageData' => $imageData,
'mimeType' => $mimeType
) = $this->readImageData($imageUrl);

if (false === $imageData || false === $mimeType) {
//store empty values to prevent further attempts to load broken image
$this->writeCache($cacheKey, '', '');
$this->sendNotFoundResponse();
}

$this->writeCache($cacheKey, $mimeType, $imageData);
$this->sendImageResponse($mimeType, $imageData);
}

private function sendNotFoundResponse(): void
{
header('HTTP/1.1 404 Not Found');
exit;
}

private function sendImageResponse(string $mimeType, string $imageData): void
{
header('Content-Type: ' . $mimeType);
echo $imageData;
exit;
}

private function buildCacheKey(string $bridgeClassName): string
{
return md5($bridgeClassName . 'icon');
}

private function cleanImageUrl(string $imageUrl): string
{
//negative look behind: replace duplicate slashes in path, but not in protocol part of uri
$imageUrl = preg_replace('~(?<!:)//~', '/', $imageUrl);
return str_replace(["\r", "\n"], '', $imageUrl);
}

private function writeCache(string $cacheKey, string $mimeType, string $imageData): void
{
$this->cache->set($cacheKey, ['imageData' => $imageData, 'mimeType' => $mimeType]);
}

private function readCache(string $cacheKey): array
{
return (array)$this->cache->get($cacheKey);
}

private function readImageData(string $imageUrl): array
{
$context = stream_context_create([
'http' => [
'timeout' => 5,
'user_agent' => Configuration::getConfig('http', 'useragent'),
],
]);

$handle = @fopen($imageUrl, 'r', false, $context);

if ($handle === false) {
return ['imageData' => false, 'mimeType' => false];
}
$metaData = stream_get_meta_data($handle);
$headers = $metaData['wrapper_data'];

$contentType = false;

foreach ($headers as $header) {
if (stripos($header, 'Content-Type:') === 0) {
$contentType = trim(substr($header, 13)); // 13 is the length of "Content-Type:"
break;
}
}

$imageData = stream_get_contents($handle);

fclose($handle);

return ['imageData' => $imageData, 'mimeType' => $contentType];
}
}
9 changes: 4 additions & 5 deletions actions/FrontpageAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,10 @@ public function execute(array $request)

$body = '';
foreach ($bridgeClassNames as $bridgeClassName) {
if ($bridgeFactory->isEnabled($bridgeClassName)) {
$body .= BridgeCard::displayBridgeCard($bridgeClassName, $formats);
$activeBridges++;
} elseif ($showInactive) {
$body .= BridgeCard::displayBridgeCard($bridgeClassName, $formats, false) . PHP_EOL;
$isEnabled = $bridgeFactory->isEnabled($bridgeClassName);
if ($isEnabled || $showInactive) {
$body .= BridgeCard::displayBridgeCard($bridgeClassName, $formats, $isEnabled) . PHP_EOL;
$activeBridges += (int)$isEnabled;
}
}

Expand Down
7 changes: 7 additions & 0 deletions bridges/BlizzardNewsBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,11 @@ protected function getSourceUrl()
}
return 'https://news.blizzard.com/' . $locale;
}

public function getIcon()
{
return <<<icon
https://blznews.akamaized.net/images/favicon-cb34a003c6f2f637ee8f4f7b406f3b9b120b918c04cabec7f03a760e708977ea9689a1c638f4396def8dce7b202cd007eae91946cc3c4a578aa8b5694226cfc6.ico
icon;
}
}
5 changes: 5 additions & 0 deletions bridges/NiusBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ class NiusBridge extends XPathAbstract
const XPATH_EXPRESSION_ITEM_CATEGORIES = './/div[@class="subtitle"]/text()';
const SETTING_FIX_ENCODING = false;

public function getIcon()
{
return 'https://www.nius.de/favicon.ico';
}

protected function formatItemTitle($value)
{
return strip_tags($value);
Expand Down
14 changes: 12 additions & 2 deletions lib/BridgeCard.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static function displayBridgeCard($bridgeClassName, $formats, $isActive =

$uri = $bridge->getURI();
$name = $bridge->getName();
$icon = $bridge->getIcon();
$icon = self::hasBrokenIcon($bridgeClassName) ? '' : $_SERVER['PHP_SELF'] . '?action=FeedIcon&bridgeClassName=' . $bridgeClassName;
$description = $bridge->getDescription();
$parameters = $bridge->getParameters();
if (Configuration::getConfig('proxy', 'url') && Configuration::getConfig('proxy', 'by_bridge')) {
Expand All @@ -47,7 +47,7 @@ class="bridge-card"
data-short-name="$shortName"
>

<h2><a href="{$uri}">{$name}</a></h2>
<h2><img src="{$icon}" onerror="this.style.display='none'" /><a href="{$uri}">{$name}</a></h2>
<p class="description">{$description}</p>
<input type="checkbox" class="showmore-box" id="showmore-{$bridgeClassName}" />
<label class="showmore" for="showmore-{$bridgeClassName}">Show more</label>
Expand Down Expand Up @@ -371,4 +371,14 @@ private static function getCheckboxInput($entry, $id, $name)
. ' />'
. PHP_EOL;
}

private static function hasBrokenIcon(string $bridgeClassName): bool
{
$logger = RssBridge::getLogger();
$cacheFactory = new CacheFactory($logger);
$cache = $cacheFactory->create();
$cacheKey = md5($bridgeClassName . 'icon');
$cachedImage = (array)$cache->get($cacheKey);
return isset($cachedImage['imageData']) && '' === $cachedImage['imageData'] && isset($cachedImage['mimeType']) && '' === $cachedImage['mimeType'];
}
}
3 changes: 2 additions & 1 deletion lib/Debug.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ class Debug
public static function isEnabled(): bool
{
$ip = $_SERVER['REMOTE_ADDR'] ?? 'x.y.z.1';
$isIconRequest = isset($_REQUEST['action']) && 'FeedIcon' === $_REQUEST['action'];
$enableDebugMode = Configuration::getConfig('system', 'enable_debug_mode');
$debugModeWhitelist = Configuration::getConfig('system', 'debug_mode_whitelist') ?: [];
if ($enableDebugMode && ($debugModeWhitelist === [] || in_array($ip, $debugModeWhitelist))) {
if ($enableDebugMode && !$isIconRequest && ($debugModeWhitelist === [] || in_array($ip, $debugModeWhitelist))) {
return true;
}
return false;
Expand Down
5 changes: 5 additions & 0 deletions static/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,11 @@ section > h2 {
font-weight: bold;
text-align: center;
}
section > h2 img {
height: 1em;
margin-right: 10px;
}

section li {
margin-left: 1em;
}
Expand Down