diff --git a/bridges/TelegramBridge.php b/bridges/TelegramBridge.php index d650cb31..511f0263 100644 --- a/bridges/TelegramBridge.php +++ b/bridges/TelegramBridge.php @@ -24,7 +24,7 @@ class TelegramBridge extends BridgeAbstract 'http://telegram.me/rssbridge' => ['username' => 'rssbridge'], ]; - const CACHE_TIMEOUT = 900; // 15 mins + const CACHE_TIMEOUT = 60 * 15; // 15 mins private $feedName = ''; private $enclosures = []; diff --git a/formats/HtmlFormat.php b/formats/HtmlFormat.php index 6c916de6..9ee4aeea 100644 --- a/formats/HtmlFormat.php +++ b/formats/HtmlFormat.php @@ -7,137 +7,56 @@ class HtmlFormat extends FormatAbstract public function stringify() { $extraInfos = $this->getExtraInfos(); - $title = e($extraInfos['name']); - $uri = e($extraInfos['uri']); - $donationUri = e($extraInfos['donationUri']); - $donationsAllowed = Configuration::getConfig('admin', 'donations'); - - // Dynamically build buttons for all formats (except HTML) $formatFactory = new FormatFactory(); - - $buttons = ''; - $links = ''; - + $buttons = []; + $linkTags = []; foreach ($formatFactory->getFormatNames() as $format) { + // Dynamically build buttons for all formats (except HTML) if ($format === 'Html') { continue; } - - $queryString = $_SERVER['QUERY_STRING']; - $query = str_ireplace('format=Html', 'format=' . $format, htmlentities($queryString)); - $buttons .= sprintf('', $query, $format) . "\n"; - - $mime = $formatFactory->create($format)->getMimeType(); - $links .= sprintf('', $query, $format, $mime) . "\n"; + $formatUrl = '?' . str_ireplace('format=Html', 'format=' . $format, htmlentities($_SERVER['QUERY_STRING'])); + $buttons[] = [ + 'href' => $formatUrl, + 'value' => $format, + ]; + $linkTags[] = [ + 'href' => $formatUrl, + 'title' => $format, + 'type' => $formatFactory->create($format)->getMimeType(), + ]; } - if ($donationUri !== '' && $donationsAllowed) { - $str = sprintf( - '', - $donationUri - ); - $buttons .= $str; - $str1 = sprintf( - '', - $donationUri - ); - $links .= $str1; + if (Configuration::getConfig('admin', 'donations') && $extraInfos['donationUri'] !== '') { + $buttons[] = [ + 'href' => e($extraInfos['donationUri']), + 'value' => 'Donate to maintainer', + ]; } - $entries = ''; + $items = []; foreach ($this->getItems() as $item) { - if ($item->getAuthor()) { - $entryAuthor = sprintf('

by: %s

', $item->getAuthor()); - } else { - $entryAuthor = ''; - } - $entryTitle = sanitize_html(strip_tags($item->getTitle())); - $entryUri = $item->getURI() ?: $uri; - - $entryDate = ''; - if ($item->getTimestamp()) { - $entryDate = sprintf( - '', - date('Y-m-d H:i:s', $item->getTimestamp()), - date('Y-m-d H:i:s', $item->getTimestamp()) - ); - } - - $entryContent = ''; - if ($item->getContent()) { - $str2 = sprintf('
%s
', sanitize_html($item->getContent())); - $entryContent = $str2; - } - - $entryEnclosures = ''; - if (!empty($item->getEnclosures())) { - $entryEnclosures = '

Attachments:

'; - - foreach ($item->getEnclosures() as $enclosure) { - $template = '
  • %s
  • '; - $url = sanitize_html($enclosure); - $anchorText = substr($url, strrpos($url, '/') + 1); - - $entryEnclosures .= sprintf($template, $url, $anchorText); - } - - $entryEnclosures .= '
    '; - } - - $entryCategories = ''; - if (!empty($item->getCategories())) { - $entryCategories = '

    Categories:

    '; - - foreach ($item->getCategories() as $category) { - $entryCategories .= '
  • ' - . sanitize_html($category) - . '
  • '; - } - - $entryCategories .= '
    '; - } - - $entries .= << -

    {$entryTitle}

    - {$entryDate} - {$entryAuthor} - {$entryContent} - {$entryEnclosures} - {$entryCategories} - - -EOD; + $items[] = [ + 'url' => $item->getURI() ?: $extraInfos['uri'], + 'title' => $item->getTitle() ?? '(no title)', + 'timestamp' => $item->getTimestamp(), + 'author' => $item->getAuthor(), + 'content' => $item->getContent() ?? '', + 'enclosures' => $item->getEnclosures(), + 'categories' => $item->getCategories(), + ]; } - $charset = $this->getCharset(); - $toReturn = << - - - - - {$title} - - - {$links} - - - -

    {$title}

    -
    - - {$buttons} -
    -{$entries} - - -EOD; - + $html = render_template(__DIR__ . '/../templates/html-format.html.php', [ + 'charset' => $this->getCharset(), + 'title' => $extraInfos['name'], + 'linkTags' => $linkTags, + 'uri' => $extraInfos['uri'], + 'buttons' => $buttons, + 'items' => $items, + ]); // Remove invalid characters ini_set('mbstring.substitute_character', 'none'); - $toReturn = mb_convert_encoding($toReturn, $this->getCharset(), 'UTF-8'); - return $toReturn; + return mb_convert_encoding($html, $this->getCharset(), 'UTF-8'); } } diff --git a/lib/FormatInterface.php b/lib/FormatInterface.php index 8f98d6e4..c0355804 100644 --- a/lib/FormatInterface.php +++ b/lib/FormatInterface.php @@ -42,7 +42,7 @@ interface FormatInterface * Return items * * @throws \LogicException if the items are not set - * @return array The items + * @return FeedItem[] The items */ public function getItems(); diff --git a/lib/html.php b/lib/html.php index 693504b0..1e852928 100644 --- a/lib/html.php +++ b/lib/html.php @@ -18,15 +18,26 @@ function render(string $template, array $context = []): string return render_template('base.html.php', $context); } +/** + * Render template as absolute path or relative to templates folder. + * Do not pass user input in $template + */ function render_template(string $template, array $context = []): string { if (isset($context['template'])) { throw new \Exception("Don't use `template` as a context key"); } + $templateFilepath = __DIR__ . '/../templates/' . $template; extract($context); ob_start(); try { - require __DIR__ . '/../templates/' . $template; + if (is_file($template)) { + require $template; + } elseif (is_file($templateFilepath)) { + require $templateFilepath; + } else { + throw new \Exception(sprintf('Unable to find template `%s`', $template)); + } } catch (\Throwable $e) { ob_end_clean(); throw $e; diff --git a/templates/html-format.html.php b/templates/html-format.html.php new file mode 100644 index 00000000..fc24f759 --- /dev/null +++ b/templates/html-format.html.php @@ -0,0 +1,90 @@ + + + + + + <?= e($title) ?> + + + + + + + + + + +

    + + + +

    + + + + +
    +

    + +

    + + + + + + +
    +

    by:

    + + +
    + +
    + + +
    +

    Attachments:

    + +
  • + + + +
  • + +
    + + + +
    +

    Categories:

    + +
  • + +
    + +
    + + + +