diff --git a/bridges/FilterBridge.php b/bridges/FilterBridge.php index ef739de3..1d920f90 100644 --- a/bridges/FilterBridge.php +++ b/bridges/FilterBridge.php @@ -73,6 +73,15 @@ class FilterBridge extends FeedExpander ], ]]; + public function collectData() + { + $url = $this->getInput('url'); + if (!Url::validate($url)) { + returnClientError('The url parameter must either refer to http or https protocol.'); + } + $this->collectExpandableDatas($this->getURI()); + } + protected function parseItem($newItem) { $item = parent::parseItem($newItem); @@ -158,13 +167,4 @@ class FilterBridge extends FeedExpander return $url; } - - public function collectData() - { - if ($this->getInput('url') && substr($this->getInput('url'), 0, 4) !== 'http') { - // just in case someone finds a way to access local files by playing with the url - returnClientError('The url parameter must either refer to http or https protocol.'); - } - $this->collectExpandableDatas($this->getURI()); - } } diff --git a/formats/AtomFormat.php b/formats/AtomFormat.php index 1b9053fa..9886e4b7 100644 --- a/formats/AtomFormat.php +++ b/formats/AtomFormat.php @@ -14,8 +14,6 @@ class AtomFormat extends FormatAbstract protected const ATOM_NS = 'http://www.w3.org/2005/Atom'; protected const MRSS_NS = 'http://search.yahoo.com/mrss/'; - const LIMIT_TITLE = 140; - public function stringify() { $feedUrl = get_current_url(); @@ -109,8 +107,8 @@ class AtomFormat extends FormatAbstract if (empty($entryTitle)) { $entryTitle = str_replace("\n", ' ', strip_tags($entryContent)); - if (strlen($entryTitle) > self::LIMIT_TITLE) { - $wrapPos = strpos(wordwrap($entryTitle, self::LIMIT_TITLE), "\n"); + if (strlen($entryTitle) > 140) { + $wrapPos = strpos(wordwrap($entryTitle, 140), "\n"); $entryTitle = substr($entryTitle, 0, $wrapPos) . '...'; } } @@ -182,11 +180,11 @@ class AtomFormat extends FormatAbstract } } - $toReturn = $document->saveXML(); + $xml = $document->saveXML(); // Remove invalid characters ini_set('mbstring.substitute_character', 'none'); - $toReturn = mb_convert_encoding($toReturn, $this->getCharset(), 'UTF-8'); - return $toReturn; + $xml = mb_convert_encoding($xml, $this->getCharset(), 'UTF-8'); + return $xml; } } diff --git a/formats/HtmlFormat.php b/formats/HtmlFormat.php index 9ee4aeea..4933af8d 100644 --- a/formats/HtmlFormat.php +++ b/formats/HtmlFormat.php @@ -6,6 +6,8 @@ class HtmlFormat extends FormatAbstract public function stringify() { + $queryString = $_SERVER['QUERY_STRING']; + $extraInfos = $this->getExtraInfos(); $formatFactory = new FormatFactory(); $buttons = []; @@ -15,7 +17,7 @@ class HtmlFormat extends FormatAbstract if ($format === 'Html') { continue; } - $formatUrl = '?' . str_ireplace('format=Html', 'format=' . $format, htmlentities($_SERVER['QUERY_STRING'])); + $formatUrl = '?' . str_ireplace('format=Html', 'format=' . $format, htmlentities($queryString)); $buttons[] = [ 'href' => $formatUrl, 'value' => $format, @@ -57,6 +59,7 @@ class HtmlFormat extends FormatAbstract ]); // Remove invalid characters ini_set('mbstring.substitute_character', 'none'); - return mb_convert_encoding($html, $this->getCharset(), 'UTF-8'); + $html = mb_convert_encoding($html, $this->getCharset(), 'UTF-8'); + return $html; } } diff --git a/formats/JsonFormat.php b/formats/JsonFormat.php index 5bb1a525..dd61da41 100644 --- a/formats/JsonFormat.php +++ b/formats/JsonFormat.php @@ -110,13 +110,8 @@ class JsonFormat extends FormatAbstract } $data['items'] = $items; - /** - * The intention here is to discard non-utf8 byte sequences. - * But the JSON_PARTIAL_OUTPUT_ON_ERROR also discards lots of other errors. - * So consider this a hack. - * Switch to JSON_INVALID_UTF8_IGNORE when PHP 7.2 is the latest platform requirement. - */ - $json = json_encode($data, \JSON_PRETTY_PRINT | \JSON_PARTIAL_OUTPUT_ON_ERROR); + // Ignoring invalid json + $json = json_encode($data, \JSON_PRETTY_PRINT | \JSON_INVALID_UTF8_IGNORE); return $json; } diff --git a/formats/MrssFormat.php b/formats/MrssFormat.php index 3c3dce5a..984611c7 100644 --- a/formats/MrssFormat.php +++ b/formats/MrssFormat.php @@ -32,12 +32,6 @@ class MrssFormat extends FormatAbstract protected const ATOM_NS = 'http://www.w3.org/2005/Atom'; protected const MRSS_NS = 'http://search.yahoo.com/mrss/'; - const ALLOWED_IMAGE_EXT = [ - '.gif', - '.jpg', - '.png', - ]; - public function stringify() { $feedUrl = get_current_url(); @@ -72,8 +66,13 @@ class MrssFormat extends FormatAbstract $channel->appendChild($description); $description->appendChild($document->createTextNode($extraInfos['name'])); + $allowedIconExtensions = [ + '.gif', + '.jpg', + '.png', + ]; $icon = $extraInfos['icon']; - if (!empty($icon) && in_array(substr($icon, -4), self::ALLOWED_IMAGE_EXT)) { + if (!empty($icon) && in_array(substr($icon, -4), $allowedIconExtensions)) { $feedImage = $document->createElement('image'); $channel->appendChild($feedImage); $iconUrl = $document->createElement('url'); @@ -164,11 +163,10 @@ class MrssFormat extends FormatAbstract } } - $toReturn = $document->saveXML(); - + $xml = $document->saveXML(); // Remove invalid non-UTF8 characters ini_set('mbstring.substitute_character', 'none'); - $toReturn = mb_convert_encoding($toReturn, $this->getCharset(), 'UTF-8'); - return $toReturn; + $xml = mb_convert_encoding($xml, $this->getCharset(), 'UTF-8'); + return $xml; } } diff --git a/formats/PlaintextFormat.php b/formats/PlaintextFormat.php index c8c4e9d6..0a9237d0 100644 --- a/formats/PlaintextFormat.php +++ b/formats/PlaintextFormat.php @@ -6,18 +6,14 @@ class PlaintextFormat extends FormatAbstract public function stringify() { - $items = $this->getItems(); $data = []; - - foreach ($items as $item) { + foreach ($this->getItems() as $item) { $data[] = $item->toArray(); } - - $toReturn = print_r($data, true); - + $text = print_r($data, true); // Remove invalid non-UTF8 characters ini_set('mbstring.substitute_character', 'none'); - $toReturn = mb_convert_encoding($toReturn, $this->getCharset(), 'UTF-8'); - return $toReturn; + $text = mb_convert_encoding($text, $this->getCharset(), 'UTF-8'); + return $text; } } diff --git a/formats/SfeedFormat.php b/formats/SfeedFormat.php index 85d5f608..33740aaa 100644 --- a/formats/SfeedFormat.php +++ b/formats/SfeedFormat.php @@ -4,6 +4,38 @@ class SfeedFormat extends FormatAbstract { const MIME_TYPE = 'text/plain'; + public function stringify() + { + $text = ''; + foreach ($this->getItems() as $item) { + $text .= sprintf( + "%s\t%s\t%s\t%s\thtml\t\t%s\t%s\t%s\n", + $item->toArray()['timestamp'], + preg_replace('/\s/', ' ', $item->toArray()['title']), + $item->toArray()['uri'], + $this->escape($item->toArray()['content']), + $item->toArray()['author'], + $this->getFirstEnclosure( + $item->toArray()['enclosures'] + ), + $this->escape( + $this->getCategories( + $item->toArray()['categories'] + ) + ) + ); + } + + // Remove invalid non-UTF8 characters + ini_set('mbstring.substitute_character', 'none'); + $text = mb_convert_encoding( + $text, + $this->getCharset(), + 'UTF-8' + ); + return $text; + } + private function escape(string $str) { $str = str_replace('\\', '\\\\', $str); @@ -31,39 +63,4 @@ class SfeedFormat extends FormatAbstract } return $toReturn; } - - public function stringify() - { - $items = $this->getItems(); - - $toReturn = ''; - foreach ($items as $item) { - $toReturn .= sprintf( - "%s\t%s\t%s\t%s\thtml\t\t%s\t%s\t%s\n", - $item->toArray()['timestamp'], - preg_replace('/\s/', ' ', $item->toArray()['title']), - $item->toArray()['uri'], - $this->escape($item->toArray()['content']), - $item->toArray()['author'], - $this->getFirstEnclosure( - $item->toArray()['enclosures'] - ), - $this->escape( - $this->getCategories( - $item->toArray()['categories'] - ) - ) - ); - } - - // Remove invalid non-UTF8 characters - ini_set('mbstring.substitute_character', 'none'); - $toReturn = mb_convert_encoding( - $toReturn, - $this->getCharset(), - 'UTF-8' - ); - return $toReturn; - } } -// vi: expandtab diff --git a/lib/contents.php b/lib/contents.php index e173b542..432d9139 100644 --- a/lib/contents.php +++ b/lib/contents.php @@ -15,6 +15,7 @@ function getContents( bool $returnFull = false ) { $httpClient = RssBridge::getHttpClient(); + $cache = RssBridge::getCache(); $httpHeadersNormalized = []; foreach ($httpHeaders as $httpHeader) { @@ -51,7 +52,6 @@ function getContents( $config['proxy'] = Configuration::getConfig('proxy', 'url'); } - $cache = RssBridge::getCache(); $cacheKey = 'server_' . $url; /** @var Response $cachedResponse */ diff --git a/lib/logger.php b/lib/logger.php index 5d95e673..e41de34b 100644 --- a/lib/logger.php +++ b/lib/logger.php @@ -148,7 +148,7 @@ final class StreamHandler $context ); error_log($text); - if (Debug::isEnabled()) { + if ($record['level'] < Logger::ERROR && Debug::isEnabled()) { print sprintf("
%s\n", e($text)); } //$bytes = file_put_contents('/tmp/rss-bridge.log', $text, FILE_APPEND | LOCK_EX); diff --git a/tests/FeedItemTest.php b/tests/FeedItemTest.php new file mode 100644 index 00000000..92833753 --- /dev/null +++ b/tests/FeedItemTest.php @@ -0,0 +1,30 @@ +setTitle('hello'); + $this->assertSame('hello', $item->getTitle()); + + $item = FeedItem::fromArray(['title' => 'hello2']); + $this->assertSame('hello2', $item->getTitle()); + + $item = new FeedItem(); + $item->setAuthor('123'); + $this->assertSame('123', $item->getAuthor()); + + $item = new FeedItem(); + $item->title = 'aa'; + $this->assertSame('aa', $item->getTitle()); + $this->assertSame('aa', $item->title); + } +}