This commit is contained in:
logmanoriginal 2016-11-09 19:01:17 +01:00
commit 84bc9d2da6
7 changed files with 64 additions and 27 deletions

View file

@ -26,9 +26,12 @@ class AtomFormat extends FormatAbstract{
$entryUri = isset($item['uri']) ? $this->xml_encode($item['uri']) : ''; $entryUri = isset($item['uri']) ? $this->xml_encode($item['uri']) : '';
$entryTimestamp = isset($item['timestamp']) ? $this->xml_encode(date(DATE_ATOM, $item['timestamp'])) : ''; $entryTimestamp = isset($item['timestamp']) ? $this->xml_encode(date(DATE_ATOM, $item['timestamp'])) : '';
$entryContent = isset($item['content']) ? $this->xml_encode($this->sanitizeHtml($item['content'])) : ''; $entryContent = isset($item['content']) ? $this->xml_encode($this->sanitizeHtml($item['content'])) : '';
$entryEnclosures = "";
foreach($item['enclosures'] as $enclosure) $entryEnclosure = '';
$entryEnclosures .= "<link rel=\"enclosure\" href=\"".$this->xml_encode($enclosure)."\"/>"; if(isset($item['enclosure'])){
$entryEnclosure = '<link rel="enclosure" href="' . $this->xml_encode($item['enclosure']) . '"/>';
}
$entries .= <<<EOD $entries .= <<<EOD
<entry> <entry>
@ -40,17 +43,18 @@ class AtomFormat extends FormatAbstract{
<id>{$entryUri}</id> <id>{$entryUri}</id>
<updated>{$entryTimestamp}</updated> <updated>{$entryTimestamp}</updated>
<content type="html">{$entryContent}</content> <content type="html">{$entryContent}</content>
{$entryEnclosures} {$entryEnclosure}
</entry> </entry>
EOD; EOD;
} }
$feedTimestamp = date(DATE_ATOM, time()); $feedTimestamp = date(DATE_ATOM, time());
$charset = $this->getCharset();
/* Data are prepared, now let's begin the "MAGIE !!!" */ /* Data are prepared, now let's begin the "MAGIE !!!" */
$toReturn = '<?xml version="1.0" encoding="UTF-8"?>'; $toReturn = <<<EOD
$toReturn .= <<<EOD <?xml version="1.0" encoding="{$charset}"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:thr="http://purl.org/syndication/thread/1.0"> <feed xmlns="http://www.w3.org/2005/Atom" xmlns:thr="http://purl.org/syndication/thread/1.0">
<title type="text">{$title}</title> <title type="text">{$title}</title>
@ -64,15 +68,15 @@ EOD;
</feed> </feed>
EOD; EOD;
// Remove invalid non-UTF8 characters // Remove invalid characters
ini_set('mbstring.substitute_character', 'none'); ini_set('mbstring.substitute_character', 'none');
$toReturn = mb_convert_encoding($toReturn, 'UTF-8', 'UTF-8'); $toReturn = mb_convert_encoding($toReturn, $this->getCharset(), 'UTF-8');
return $toReturn; return $toReturn;
} }
public function display(){ public function display(){
$this $this
->setContentType('application/atom+xml; charset=UTF-8') ->setContentType('application/atom+xml; charset=' . $this->getCharset())
->callContentType(); ->callContentType();
return parent::display(); return parent::display();

View file

@ -30,6 +30,13 @@ class HtmlFormat extends FormatAbstract {
. '</div>'; . '</div>';
} }
$entryEnclosure = '';
if(isset($item['enclosure'])){
$entryEnclosure = '<div class="enclosure"><a href="'
. $this->sanitizeHtml($item['enclosure'])
. '">enclosure</a><div>';
}
$entries .= <<<EOD $entries .= <<<EOD
<section class="feeditem"> <section class="feeditem">
@ -37,17 +44,20 @@ class HtmlFormat extends FormatAbstract {
{$entryTimestamp} {$entryTimestamp}
{$entryAuthor} {$entryAuthor}
{$entryContent} {$entryContent}
{$entryEnclosure}
</section> </section>
EOD; EOD;
} }
$charset = $this->getCharset();
/* Data are prepared, now let's begin the "MAGIE !!!" */ /* Data are prepared, now let's begin the "MAGIE !!!" */
$toReturn = <<<EOD $toReturn = <<<EOD
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
<head> <head>
<meta charset="UTF-8"> <meta charset="{$charset}">
<title>{$title}</title> <title>{$title}</title>
<link href="css/HtmlFormat.css" rel="stylesheet"> <link href="css/HtmlFormat.css" rel="stylesheet">
<meta name="robots" content="noindex, follow"> <meta name="robots" content="noindex, follow">
@ -64,6 +74,9 @@ EOD;
</html> </html>
EOD; EOD;
// Remove invalid characters
ini_set('mbstring.substitute_character', 'none');
$toReturn = mb_convert_encoding($toReturn, $this->getCharset(), 'UTF-8');
return $toReturn; return $toReturn;
} }

View file

@ -7,12 +7,17 @@ class JsonFormat extends FormatAbstract {
public function stringify(){ public function stringify(){
$items = $this->getItems(); $items = $this->getItems();
return json_encode($items, JSON_PRETTY_PRINT); $toReturn = json_encode($items, JSON_PRETTY_PRINT);
// Remove invalid non-UTF8 characters
ini_set('mbstring.substitute_character', 'none');
$toReturn = mb_convert_encoding($toReturn, $this->getCharset(), 'UTF-8');
return $toReturn;
} }
public function display(){ public function display(){
$this $this
->setContentType('application/json') ->setContentType('application/json; charset=' . $this->getCharset())
->callContentType(); ->callContentType();
return parent::display(); return parent::display();

View file

@ -30,9 +30,12 @@ class MrssFormat extends FormatAbstract {
$itemUri = isset($item['uri']) ? $this->xml_encode($item['uri']) : ''; $itemUri = isset($item['uri']) ? $this->xml_encode($item['uri']) : '';
$itemTimestamp = isset($item['timestamp']) ? $this->xml_encode(date(DATE_RFC2822, $item['timestamp'])) : ''; $itemTimestamp = isset($item['timestamp']) ? $this->xml_encode(date(DATE_RFC2822, $item['timestamp'])) : '';
$itemContent = isset($item['content']) ? $this->xml_encode($this->sanitizeHtml($item['content'])) : ''; $itemContent = isset($item['content']) ? $this->xml_encode($this->sanitizeHtml($item['content'])) : '';
$entryEnclosures = "";
foreach($item['enclosures'] as $enclosure) $entryEnclosure = '';
$entryEnclosures .= "<enclosure url=\"".$this->xml_encode($enclosure)."\"/>"; if(isset($item['enclosure'])){
$entryEnclosure = '<enclosure url="' . $this->xml_encode($item['enclosure']) . '"/>';
}
$items .= <<<EOD $items .= <<<EOD
<item> <item>
@ -42,18 +45,20 @@ class MrssFormat extends FormatAbstract {
<pubDate>{$itemTimestamp}</pubDate> <pubDate>{$itemTimestamp}</pubDate>
<description>{$itemContent}</description> <description>{$itemContent}</description>
<author>{$itemAuthor}</author> <author>{$itemAuthor}</author>
{$entryEnclosures} {$entryEnclosure}
</item> </item>
EOD; EOD;
} }
$charset = $this->getCharset();
/* Data are prepared, now let's begin the "MAGIE !!!" */ /* Data are prepared, now let's begin the "MAGIE !!!" */
$toReturn = '<?xml version="1.0" encoding="UTF-8"?>'; $toReturn = <<<EOD
$toReturn .= <<<EOD <?xml version="1.0" encoding="{$charset}"?>
<rss version="2.0" <rss version="2.0"
xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:media="http://search.yahoo.com/mrss/" xmlns:media="http://search.yahoo.com/mrss/"
xmlns:atom="http://www.w3.org/2005/Atom"> xmlns:atom="http://www.w3.org/2005/Atom">
<channel> <channel>
<title>{$title}</title> <title>{$title}</title>
@ -69,13 +74,13 @@ EOD;
// Remove invalid non-UTF8 characters // Remove invalid non-UTF8 characters
ini_set('mbstring.substitute_character', 'none'); ini_set('mbstring.substitute_character', 'none');
$toReturn = mb_convert_encoding($toReturn, 'UTF-8', 'UTF-8'); $toReturn = mb_convert_encoding($toReturn, $this->getCharset(), 'UTF-8');
return $toReturn; return $toReturn;
} }
public function display(){ public function display(){
$this $this
->setContentType('application/rss+xml; charset=UTF-8') ->setContentType('application/rss+xml; charset=' . $this->getCharset())
->callContentType(); ->callContentType();
return parent::display(); return parent::display();

View file

@ -7,12 +7,17 @@ class PlaintextFormat extends FormatAbstract {
public function stringify(){ public function stringify(){
$items = $this->getItems(); $items = $this->getItems();
return print_r($items, true); $toReturn = print_r($items, true);
// Remove invalid non-UTF8 characters
ini_set('mbstring.substitute_character', 'none');
$toReturn = mb_convert_encoding($toReturn, $this->getCharset(), 'UTF-8');
return $toReturn;
} }
public function display(){ public function display(){
$this $this
->setContentType('text/plain;charset=' . $this->getCharset()) ->setContentType('text/plain; charset=' . $this->getCharset())
->callContentType(); ->callContentType();
return parent::display(); return parent::display();

View file

@ -18,7 +18,7 @@ abstract class FormatAbstract implements FormatInterface {
public function getCharset(){ public function getCharset(){
$charset = $this->charset; $charset = $this->charset;
return is_null($charset) ? self::DEFAULT_CHARSET : $charset; return is_null($charset) ? static::DEFAULT_CHARSET : $charset;
} }
protected function setContentType($contentType){ protected function setContentType($contentType){

View file

@ -1,6 +1,11 @@
<?php <?php
interface FormatInterface{ interface FormatInterface {
public function stringify(); public function stringify();
public function display(); public function display();
public function setItems(array $bridges); public function setItems(array $bridges);
public function getItems();
public function setExtraInfos(array $infos);
public function getExtraInfos();
public function setCharset($charset);
public function getCharset();
} }