mirror of
https://github.com/RSS-Bridge/rss-bridge.git
synced 2025-07-30 13:20:12 +02:00
refactor: html format (#3062)
* refactor: html format Fix a few small bugs too * fix * fix * trigger build * striptags instead of encode title
This commit is contained in:
@@ -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('<a href="./?%s"><button class="rss-feed">%s</button></a>', $query, $format) . "\n";
|
||||
|
||||
$mime = $formatFactory->create($format)->getMimeType();
|
||||
$links .= sprintf('<link href="./?%s" title="%s" rel="alternate" type="%s">', $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(
|
||||
'<a href="%s" target="_blank"><button class="highlight">Donate to maintainer</button></a>',
|
||||
$donationUri
|
||||
);
|
||||
$buttons .= $str;
|
||||
$str1 = sprintf(
|
||||
'<link href="%s target="_blank"" title="Donate to Maintainer" rel="alternate">',
|
||||
$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('<br /><p class="author">by: %s</p>', $item->getAuthor());
|
||||
} else {
|
||||
$entryAuthor = '';
|
||||
}
|
||||
$entryTitle = sanitize_html(strip_tags($item->getTitle()));
|
||||
$entryUri = $item->getURI() ?: $uri;
|
||||
|
||||
$entryDate = '';
|
||||
if ($item->getTimestamp()) {
|
||||
$entryDate = sprintf(
|
||||
'<time datetime="%s">%s</time>',
|
||||
date('Y-m-d H:i:s', $item->getTimestamp()),
|
||||
date('Y-m-d H:i:s', $item->getTimestamp())
|
||||
);
|
||||
}
|
||||
|
||||
$entryContent = '';
|
||||
if ($item->getContent()) {
|
||||
$str2 = sprintf('<div class="content">%s</div>', sanitize_html($item->getContent()));
|
||||
$entryContent = $str2;
|
||||
}
|
||||
|
||||
$entryEnclosures = '';
|
||||
if (!empty($item->getEnclosures())) {
|
||||
$entryEnclosures = '<div class="attachments"><p>Attachments:</p>';
|
||||
|
||||
foreach ($item->getEnclosures() as $enclosure) {
|
||||
$template = '<li class="enclosure"><a href="%s" rel="noopener noreferrer nofollow">%s</a></li>';
|
||||
$url = sanitize_html($enclosure);
|
||||
$anchorText = substr($url, strrpos($url, '/') + 1);
|
||||
|
||||
$entryEnclosures .= sprintf($template, $url, $anchorText);
|
||||
}
|
||||
|
||||
$entryEnclosures .= '</div>';
|
||||
}
|
||||
|
||||
$entryCategories = '';
|
||||
if (!empty($item->getCategories())) {
|
||||
$entryCategories = '<div class="categories"><p>Categories:</p>';
|
||||
|
||||
foreach ($item->getCategories() as $category) {
|
||||
$entryCategories .= '<li class="category">'
|
||||
. sanitize_html($category)
|
||||
. '</li>';
|
||||
}
|
||||
|
||||
$entryCategories .= '</div>';
|
||||
}
|
||||
|
||||
$entries .= <<<EOD
|
||||
|
||||
<section class="feeditem">
|
||||
<h2><a class="itemtitle" href="{$entryUri}">{$entryTitle}</a></h2>
|
||||
{$entryDate}
|
||||
{$entryAuthor}
|
||||
{$entryContent}
|
||||
{$entryEnclosures}
|
||||
{$entryCategories}
|
||||
</section>
|
||||
|
||||
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 = <<<EOD
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="{$charset}">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>{$title}</title>
|
||||
<link href="static/HtmlFormat.css" rel="stylesheet">
|
||||
<link rel="icon" type="image/png" href="static/favicon.png">
|
||||
{$links}
|
||||
<meta name="robots" content="noindex, follow">
|
||||
</head>
|
||||
<body>
|
||||
<h1 class="pagetitle"><a href="{$uri}" target="_blank">{$title}</a></h1>
|
||||
<div class="buttons">
|
||||
<a href="./#bridge-{$_GET['bridge']}"><button class="backbutton">← back to rss-bridge</button></a>
|
||||
{$buttons}
|
||||
</div>
|
||||
{$entries}
|
||||
</body>
|
||||
</html>
|
||||
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');
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user