diff --git a/formats/AtomFormat.php b/formats/AtomFormat.php index 56c36963..238dc884 100644 --- a/formats/AtomFormat.php +++ b/formats/AtomFormat.php @@ -6,8 +6,7 @@ class AtomFormat extends FormatAbstract{ public function stringify(){ - /* Datas preparation */ - $https = ( isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' ? 's' : '' ); + $https = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' ? 's' : ''; $httpHost = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : ''; $httpInfo = isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : ''; @@ -20,12 +19,12 @@ class AtomFormat extends FormatAbstract{ $uri = $this->xml_encode($uri); $entries = ''; - foreach($this->getDatas() as $data){ - $entryAuthor = is_null($data['author']) ? '' : $this->xml_encode($data['author']); - $entryTitle = is_null($data['title']) ? '' : $this->xml_encode($data['title']); - $entryUri = is_null($data['uri']) ? '' : $this->xml_encode($data['uri']); - $entryTimestamp = is_null($data['timestamp']) ? '' : $this->xml_encode(date(DATE_ATOM, $data['timestamp'])); - $entryContent = is_null($data['content']) ? '' : $this->xml_encode($this->sanitizeHtml($data['content'])); + foreach($this->getItems() as $item){ + $entryAuthor = isset($item['author']) ? $this->xml_encode($item['author']) : ''; + $entryTitle = isset($item['title']) ? $this->xml_encode($item['title']) : ''; + $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'])) : ''; $entries .= << @@ -42,16 +41,7 @@ class AtomFormat extends FormatAbstract{ EOD; } - /* - TODO : - - Security: Disable Javascript ? - - : Define new extra info ? - - : RFC look with xhtml, keep this in spite of ? - */ - -// #### TEMPORARY FIX ### -$feedTimestamp = date(DATE_ATOM, time()); -// ################ + $feedTimestamp = date(DATE_ATOM, time()); /* Data are prepared, now let's begin the "MAGIE !!!" */ $toReturn = ''; @@ -70,11 +60,6 @@ $feedTimestamp = date(DATE_ATOM, time()); EOD; // Remove invalid non-UTF8 characters - - // We cannot use iconv because of a bug in some versions of iconv. - // See http://www.php.net/manual/fr/function.iconv.php#108643 - //$toReturn = iconv("UTF-8", "UTF-8//IGNORE", $toReturn); - // So we use mb_convert_encoding instead: ini_set('mbstring.substitute_character', 'none'); $toReturn= mb_convert_encoding($toReturn, 'UTF-8', 'UTF-8'); return $toReturn; @@ -82,7 +67,7 @@ EOD; public function display(){ $this - ->setContentType('application/atom+xml; charset=UTF-8') // We force UTF-8 in ATOM output. + ->setContentType('application/atom+xml; charset=UTF-8') ->callContentType(); return parent::display(); diff --git a/formats/HtmlFormat.php b/formats/HtmlFormat.php index 4a9aab01..d7c927b0 100644 --- a/formats/HtmlFormat.php +++ b/formats/HtmlFormat.php @@ -2,7 +2,6 @@ class HtmlFormat extends FormatAbstract{ public function stringify(){ - /* Datas preparation */ $extraInfos = $this->getExtraInfos(); $title = htmlspecialchars($extraInfos['name']); $uri = htmlspecialchars($extraInfos['uri']); @@ -10,12 +9,12 @@ class HtmlFormat extends FormatAbstract{ $mrssquery = str_replace('format=Html', 'format=Mrss', htmlentities($_SERVER['QUERY_STRING'])); $entries = ''; - foreach($this->getDatas() as $data){ - $entryAuthor = isset($data['author']) ? '

by: ' . $data['author'] . '

' : ''; - $entryTitle = isset($data['title']) ? $this->sanitizeHtml(strip_tags($data['title'])) : ''; - $entryUri = isset($data['uri']) ? $data['uri'] : $uri; - $entryTimestamp = isset($data['timestamp']) ? '' : ''; - $entryContent = isset($data['content']) ? '
' . $this->sanitizeHtml($data['content']). '
' : ''; + foreach($this->getItems() as $item){ + $entryAuthor = isset($item['author']) ? '

by: ' . $item['author'] . '

' : ''; + $entryTitle = isset($item['title']) ? $this->sanitizeHtml(strip_tags($item['title'])) : ''; + $entryUri = isset($item['uri']) ? $item['uri'] : $uri; + $entryTimestamp = isset($item['timestamp']) ? '' : ''; + $entryContent = isset($item['content']) ? '
' . $this->sanitizeHtml($item['content']). '
' : ''; $entries .= << diff --git a/formats/JsonFormat.php b/formats/JsonFormat.php index c1b076ee..e173f230 100644 --- a/formats/JsonFormat.php +++ b/formats/JsonFormat.php @@ -6,10 +6,9 @@ class JsonFormat extends FormatAbstract{ public function stringify(){ - // FIXME : sometime content can be null, transform to empty string - $datas = $this->getDatas(); + $items = $this->getItems(); - return json_encode($datas, JSON_PRETTY_PRINT); + return json_encode($items, JSON_PRETTY_PRINT); } public function display(){ diff --git a/formats/MrssFormat.php b/formats/MrssFormat.php index d7cb682e..ddbd5d35 100644 --- a/formats/MrssFormat.php +++ b/formats/MrssFormat.php @@ -6,8 +6,7 @@ class MrssFormat extends FormatAbstract{ public function stringify(){ - /* Datas preparation */ - $https = ( isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' ? 's' : '' ); + $https = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' ? 's' : ''; $httpHost = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : ''; $httpInfo = isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : ''; @@ -16,14 +15,15 @@ class MrssFormat extends FormatAbstract{ $extraInfos = $this->getExtraInfos(); $title = $this->xml_encode($extraInfos['name']); $uri = $this->xml_encode(!empty($extraInfos['uri']) ? $extraInfos['uri'] : 'https://github.com/sebsauvage/rss-bridge'); + $icon = $this->xml_encode('http://icons.better-idea.org/icon?url='. $uri .'&size=64'); $items = ''; - foreach($this->getDatas() as $data){ - $itemAuthor = is_null($data['author']) ? '' : $this->xml_encode($data['author']); - $itemTitle = strip_tags(is_null($data['title']) ? '' : $this->xml_encode($data['title'])); - $itemUri = is_null($data['uri']) ? '' : $this->xml_encode($data['uri']); - $itemTimestamp = is_null($data['timestamp']) ? '' : $this->xml_encode(date(DATE_RFC2822, $data['timestamp'])); - $itemContent = is_null($data['content']) ? '' : $this->xml_encode($this->sanitizeHtml($data['content'])); + foreach($this->getItems() as $item){ + $itemAuthor = isset($item['author']) ? $this->xml_encode($item['author']) : ''; + $itemTitle = strip_tags(isset($item['title']) ? $this->xml_encode($item['title']) : ''); + $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'])) : ''; $items .= << @@ -38,13 +38,6 @@ class MrssFormat extends FormatAbstract{ EOD; } - /* - TODO : - - Security: Disable Javascript ? - - : Define new extra info ? - - : RFC look with xhtml, keep this in spite of ? - */ - /* Data are prepared, now let's begin the "MAGIE !!!" */ $toReturn = ''; $toReturn .= <<{$title} http{$https}://{$httpHost}{$httpInfo}/ {$title} + {$items} @@ -61,11 +55,6 @@ EOD; EOD; // Remove invalid non-UTF8 characters - - // We cannot use iconv because of a bug in some versions of iconv. - // See http://www.php.net/manual/fr/function.iconv.php#108643 - //$toReturn = iconv("UTF-8", "UTF-8//IGNORE", $toReturn); - // So we use mb_convert_encoding instead: ini_set('mbstring.substitute_character', 'none'); $toReturn= mb_convert_encoding($toReturn, 'UTF-8', 'UTF-8'); return $toReturn; @@ -73,7 +62,7 @@ EOD; public function display(){ $this - ->setContentType('application/rss+xml; charset=UTF-8') // We force UTF-8 in RSS output. + ->setContentType('application/rss+xml; charset=UTF-8') ->callContentType(); return parent::display(); diff --git a/formats/PlaintextFormat.php b/formats/PlaintextFormat.php index 7916bc61..e2cf0b94 100644 --- a/formats/PlaintextFormat.php +++ b/formats/PlaintextFormat.php @@ -6,8 +6,8 @@ class PlaintextFormat extends FormatAbstract{ public function stringify(){ - $datas = $this->getDatas(); - return print_r($datas, true); + $items = $this->getItems(); + return print_r($items, true); } public function display(){ diff --git a/index.php b/index.php index 610c19a8..10a13e0b 100644 --- a/index.php +++ b/index.php @@ -137,7 +137,7 @@ try{ try { $format = Format::create($format); $format - ->setDatas($bridge->getDatas()) + ->setItems($bridge->getItems()) ->setExtraInfos(array( 'name' => $bridge->getName(), 'uri' => $bridge->getURI(), diff --git a/lib/Bridge.php b/lib/Bridge.php index c8f5cbac..583598d8 100644 --- a/lib/Bridge.php +++ b/lib/Bridge.php @@ -139,7 +139,7 @@ abstract class BridgeAbstract implements BridgeInterface { * Return items stored in the bridge * @return mixed */ - public function getDatas(){ + public function getItems(){ return $this->items; } @@ -267,7 +267,7 @@ abstract class BridgeAbstract implements BridgeInterface { $this->collectData(); if(!is_null($this->cache)){ - $this->cache->saveData($this->getDatas()); + $this->cache->saveData($this->getItems()); } return; } @@ -360,7 +360,7 @@ abstract class BridgeAbstract implements BridgeInterface { $this->collectData(); if(!is_null($this->cache)){ - $this->cache->saveData($this->getDatas()); + $this->cache->saveData($this->getItems()); } } diff --git a/lib/Format.php b/lib/Format.php index 486ae849..8053d36d 100644 --- a/lib/Format.php +++ b/lib/Format.php @@ -7,7 +7,7 @@ interface FormatInterface{ public function stringify(); public function display(); - public function setDatas(array $bridge); + public function setItems(array $bridges); } abstract class FormatAbstract implements FormatInterface{ @@ -16,7 +16,7 @@ abstract class FormatAbstract implements FormatInterface{ protected $contentType, $charset, - $datas, + $items, $extraInfos ; @@ -48,18 +48,17 @@ abstract class FormatAbstract implements FormatInterface{ return $this; } - public function setDatas(array $datas){ - $this->datas = $datas; + public function setItems(array $items){ + $this->items = array_map(array($this, 'array_trim'), $items); return $this; } - public function getDatas(){ - if( !is_array($this->datas) ){ - throw new \LogicException('Feed the ' . get_class($this) . ' with "setDatas" method before !'); - } + public function getItems(){ + if(!is_array($this->items)) + throw new \LogicException('Feed the ' . get_class($this) . ' with "setItems" method before !'); - return $this->datas; + return $this->items; } /** @@ -99,7 +98,7 @@ abstract class FormatAbstract implements FormatInterface{ * Maybe we'll switch to http://htmlpurifier.org/ * or http://www.bioinformatics.org/phplabware/internal_utilities/htmLawed/index.php */ - public function sanitizeHtml($html) + protected function sanitizeHtml($html) { $html = str_replace(' $value){ + if(is_string($value)) + $elements[$key] = trim($value); + } + return $elements; + } } class Format{ diff --git a/lib/RssBridge.php b/lib/RssBridge.php index 7e175017..4b0aab52 100644 --- a/lib/RssBridge.php +++ b/lib/RssBridge.php @@ -32,7 +32,7 @@ require_once $vendorLibSimpleHtmlDom; Format::setDir(__DIR__ . '/formats/'); $format = Format::create('Atom'); $format - ->setDatas($bridge->getDatas()) + ->setItems($bridge->getItems()) ->setExtraInfos(array( 'name' => $bridge->getName(), 'uri' => $bridge->getURI(),