From 28e813620f0ca81171ed657902e5151d1d678cda Mon Sep 17 00:00:00 2001 From: logmanoriginal Date: Mon, 7 Nov 2016 19:58:41 +0100 Subject: [PATCH 1/4] [FormatInterface] Add missing public functions This commit adds all missing functions to the interface that are defined and implemented as public functions in FormatAbstract. --- lib/FormatInterface.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/FormatInterface.php b/lib/FormatInterface.php index 2fac3fcc..f99d214f 100644 --- a/lib/FormatInterface.php +++ b/lib/FormatInterface.php @@ -1,6 +1,11 @@ Date: Mon, 7 Nov 2016 20:20:52 +0100 Subject: [PATCH 2/4] [FormatAbstract] Allow child classes to overwrite DEFAULT_CHARSET By using 'static' instead of 'self' the constant may be overridden by child classes. --- lib/FormatAbstract.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/FormatAbstract.php b/lib/FormatAbstract.php index 6bcc2f94..c359026b 100644 --- a/lib/FormatAbstract.php +++ b/lib/FormatAbstract.php @@ -18,7 +18,7 @@ abstract class FormatAbstract implements FormatInterface { public function getCharset(){ $charset = $this->charset; - return is_null($charset) ? self::DEFAULT_CHARSET : $charset; + return is_null($charset) ? static::DEFAULT_CHARSET : $charset; } protected function setContentType($contentType){ From 3a2cb9ea1e41515348c7cb7e21832adbc66a65d8 Mon Sep 17 00:00:00 2001 From: logmanoriginal Date: Mon, 7 Nov 2016 20:49:44 +0100 Subject: [PATCH 3/4] [formats] Use custom characterset in all formats The specified characterset will now apply to all formats thus allowing other charactersets than 'UTF-8' --- formats/AtomFormat.php | 11 ++++++----- formats/HtmlFormat.php | 7 ++++++- formats/JsonFormat.php | 9 +++++++-- formats/MrssFormat.php | 16 +++++++++------- formats/PlaintextFormat.php | 9 +++++++-- 5 files changed, 35 insertions(+), 17 deletions(-) diff --git a/formats/AtomFormat.php b/formats/AtomFormat.php index eca9f83f..4168ff98 100644 --- a/formats/AtomFormat.php +++ b/formats/AtomFormat.php @@ -47,10 +47,11 @@ EOD; } $feedTimestamp = date(DATE_ATOM, time()); + $charset = $this->getCharset(); /* Data are prepared, now let's begin the "MAGIE !!!" */ - $toReturn = ''; - $toReturn .= << {$title} @@ -64,15 +65,15 @@ EOD; EOD; - // Remove invalid non-UTF8 characters + // Remove invalid characters 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; } public function display(){ $this - ->setContentType('application/atom+xml; charset=UTF-8') + ->setContentType('application/atom+xml; charset=' . $this->getCharset()) ->callContentType(); return parent::display(); diff --git a/formats/HtmlFormat.php b/formats/HtmlFormat.php index c32c3a1c..b928d139 100644 --- a/formats/HtmlFormat.php +++ b/formats/HtmlFormat.php @@ -42,12 +42,14 @@ class HtmlFormat extends FormatAbstract { EOD; } + $charset = $this->getCharset(); + /* Data are prepared, now let's begin the "MAGIE !!!" */ $toReturn = << - + {$title} @@ -64,6 +66,9 @@ EOD; EOD; + // Remove invalid characters + ini_set('mbstring.substitute_character', 'none'); + $toReturn = mb_convert_encoding($toReturn, $this->getCharset(), 'UTF-8'); return $toReturn; } diff --git a/formats/JsonFormat.php b/formats/JsonFormat.php index ac6e450a..a60601a0 100644 --- a/formats/JsonFormat.php +++ b/formats/JsonFormat.php @@ -7,12 +7,17 @@ class JsonFormat extends FormatAbstract { public function stringify(){ $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(){ $this - ->setContentType('application/json') + ->setContentType('application/json; charset=' . $this->getCharset()) ->callContentType(); return parent::display(); diff --git a/formats/MrssFormat.php b/formats/MrssFormat.php index 52ee86fb..da75b77f 100644 --- a/formats/MrssFormat.php +++ b/formats/MrssFormat.php @@ -48,12 +48,14 @@ class MrssFormat extends FormatAbstract { EOD; } + $charset = $this->getCharset(); + /* Data are prepared, now let's begin the "MAGIE !!!" */ - $toReturn = ''; - $toReturn .= << + {$title} @@ -69,13 +71,13 @@ EOD; // Remove invalid non-UTF8 characters 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; } public function display(){ $this - ->setContentType('application/rss+xml; charset=UTF-8') + ->setContentType('application/rss+xml; charset=' . $this->getCharset()) ->callContentType(); return parent::display(); diff --git a/formats/PlaintextFormat.php b/formats/PlaintextFormat.php index 593e938d..c9691d0f 100644 --- a/formats/PlaintextFormat.php +++ b/formats/PlaintextFormat.php @@ -7,12 +7,17 @@ class PlaintextFormat extends FormatAbstract { public function stringify(){ $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(){ $this - ->setContentType('text/plain;charset=' . $this->getCharset()) + ->setContentType('text/plain; charset=' . $this->getCharset()) ->callContentType(); return parent::display(); From 42cbc2e8890f17fe7bb0c155d83c0558ac341a28 Mon Sep 17 00:00:00 2001 From: logmanoriginal Date: Wed, 9 Nov 2016 18:59:17 +0100 Subject: [PATCH 4/4] [formats] Fix enclosures All bridges failed due to missing 'enclosures' element in the items array. With this commit all formats (ATOM, RSS and HTML) provide support for a single 'enclosure' element --- formats/AtomFormat.php | 11 +++++++---- formats/HtmlFormat.php | 8 ++++++++ formats/MrssFormat.php | 11 +++++++---- 3 files changed, 22 insertions(+), 8 deletions(-) diff --git a/formats/AtomFormat.php b/formats/AtomFormat.php index 4168ff98..1944905f 100644 --- a/formats/AtomFormat.php +++ b/formats/AtomFormat.php @@ -26,9 +26,12 @@ class AtomFormat extends FormatAbstract{ $entryUri = isset($item['uri']) ? $this->xml_encode($item['uri']) : ''; $entryTimestamp = isset($item['timestamp']) ? $this->xml_encode(date(DATE_ATOM, $item['timestamp'])) : ''; $entryContent = isset($item['content']) ? $this->xml_encode($this->sanitizeHtml($item['content'])) : ''; - $entryEnclosures = ""; - foreach($item['enclosures'] as $enclosure) - $entryEnclosures .= "xml_encode($enclosure)."\"/>"; + + $entryEnclosure = ''; + if(isset($item['enclosure'])){ + $entryEnclosure = ''; + } + $entries .= << @@ -40,7 +43,7 @@ class AtomFormat extends FormatAbstract{ {$entryUri} {$entryTimestamp} {$entryContent} - {$entryEnclosures} + {$entryEnclosure} EOD; diff --git a/formats/HtmlFormat.php b/formats/HtmlFormat.php index b928d139..8f96466b 100644 --- a/formats/HtmlFormat.php +++ b/formats/HtmlFormat.php @@ -30,6 +30,13 @@ class HtmlFormat extends FormatAbstract { . ''; } + $entryEnclosure = ''; + if(isset($item['enclosure'])){ + $entryEnclosure = '
enclosure
'; + } + $entries .= << @@ -37,6 +44,7 @@ class HtmlFormat extends FormatAbstract { {$entryTimestamp} {$entryAuthor} {$entryContent} + {$entryEnclosure} EOD; diff --git a/formats/MrssFormat.php b/formats/MrssFormat.php index da75b77f..19c703ea 100644 --- a/formats/MrssFormat.php +++ b/formats/MrssFormat.php @@ -30,9 +30,12 @@ class MrssFormat extends FormatAbstract { $itemUri = isset($item['uri']) ? $this->xml_encode($item['uri']) : ''; $itemTimestamp = isset($item['timestamp']) ? $this->xml_encode(date(DATE_RFC2822, $item['timestamp'])) : ''; $itemContent = isset($item['content']) ? $this->xml_encode($this->sanitizeHtml($item['content'])) : ''; - $entryEnclosures = ""; - foreach($item['enclosures'] as $enclosure) - $entryEnclosures .= "xml_encode($enclosure)."\"/>"; + + $entryEnclosure = ''; + if(isset($item['enclosure'])){ + $entryEnclosure = ''; + } + $items .= << @@ -42,7 +45,7 @@ class MrssFormat extends FormatAbstract { {$itemTimestamp} {$itemContent} {$itemAuthor} - {$entryEnclosures} + {$entryEnclosure} EOD;