rss-bridge/formats/HtmlFormat.php

149 lines
3.8 KiB
PHP
Raw Normal View History

2013-08-11 15:30:41 +04:00
<?php
class HtmlFormat extends FormatAbstract {
const MIME_TYPE = 'text/html';
public function stringify(){
$extraInfos = $this->getExtraInfos();
$title = htmlspecialchars($extraInfos['name']);
$uri = htmlspecialchars($extraInfos['uri']);
// Dynamically build buttons for all formats (except HTML)
$formatFac = new FormatFactory();
$formatFac->setWorkingDir(PATH_LIB_FORMATS);
$buttons = '';
$links = '';
foreach($formatFac->getFormatNames() as $format) {
if(strcasecmp($format, 'HTML') === 0) {
continue;
}
$query = str_ireplace('format=Html', 'format=' . $format, htmlentities($_SERVER['QUERY_STRING']));
$buttons .= $this->buildButton($format, $query) . PHP_EOL;
$mime = $formatFac->create($format)->getMimeType();
$links .= $this->buildLink($format, $query, $mime) . PHP_EOL;
}
$entries = '';
foreach($this->getItems() as $item) {
$entryAuthor = $item->getAuthor() ? '<br /><p class="author">by: ' . $item->getAuthor() . '</p>' : '';
$entryTitle = $this->sanitizeHtml(strip_tags($item->getTitle()));
$entryUri = $item->getURI() ?: $uri;
2016-09-10 22:01:02 +03:00
$entryTimestamp = '';
if($item->getTimestamp()) {
2016-09-10 22:01:02 +03:00
$entryTimestamp = '<time datetime="'
. date(DATE_ATOM, $item->getTimestamp())
2016-09-10 22:01:02 +03:00
. '">'
. date(DATE_ATOM, $item->getTimestamp())
2016-09-10 22:01:02 +03:00
. '</time>';
}
$entryContent = '';
if($item->getContent()) {
2016-09-10 22:01:02 +03:00
$entryContent = '<div class="content">'
. $this->sanitizeHtml($item->getContent())
2016-09-10 22:01:02 +03:00
. '</div>';
}
$entryEnclosures = '';
if(!empty($item->getEnclosures())) {
$entryEnclosures = '<div class="attachments"><p>Attachments:</p>';
foreach($item->getEnclosures() as $enclosure) {
$url = $this->sanitizeHtml($enclosure);
$entryEnclosures .= '<li class="enclosure"><a href="'
. $url
. '">'
. substr($url, strrpos($url, '/') + 1)
. '</a></li>';
}
$entryEnclosures .= '</div>';
}
$entryCategories = '';
if(!empty($item->getCategories())) {
$entryCategories = '<div class="categories"><p>Categories:</p>';
foreach($item->getCategories() as $category) {
$entryCategories .= '<li class="category">'
. $this->sanitizeHtml($category)
. '</li>';
}
$entryCategories .= '</div>';
}
$entries .= <<<EOD
2013-08-11 15:30:41 +04:00
2016-08-03 21:07:49 +03:00
<section class="feeditem">
<h2><a class="itemtitle" href="{$entryUri}">{$entryTitle}</a></h2>
{$entryTimestamp}
{$entryAuthor}
{$entryContent}
{$entryEnclosures}
{$entryCategories}
2016-08-03 21:07:49 +03:00
</section>
2013-08-11 15:30:41 +04:00
EOD;
}
2013-08-11 15:30:41 +04:00
$charset = $this->getCharset();
/* Data are prepared, now let's begin the "MAGIE !!!" */
$toReturn = <<<EOD
2014-05-28 18:57:30 +04:00
<!DOCTYPE html>
2013-08-11 15:30:41 +04:00
<html>
2014-05-28 18:57:30 +04:00
<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">
2014-05-28 18:57:30 +04:00
</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>
2013-08-11 15:30:41 +04:00
{$entries}
2014-05-28 18:57:30 +04:00
</body>
2013-08-11 15:30:41 +04:00
</html>
EOD;
// Remove invalid characters
ini_set('mbstring.substitute_character', 'none');
$toReturn = mb_convert_encoding($toReturn, $this->getCharset(), 'UTF-8');
return $toReturn;
}
2013-08-11 15:30:41 +04:00
public function display() {
$this
->setContentType(self::MIME_TYPE . '; charset=' . $this->getCharset())
->callContentType();
2013-08-11 15:30:41 +04:00
return parent::display();
}
private function buildButton($format, $query) {
return <<<EOD
<a href="./?{$query}"><button class="rss-feed">{$format}</button></a>
EOD;
}
private function buildLink($format, $query, $mime) {
return <<<EOD
<link href="./?{$query}" title="{$format}" rel="alternate" type="{$mime}">
EOD;
}
2014-05-28 18:57:30 +04:00
}