summaryrefslogtreecommitdiffstats
path: root/vendor/fguillot/picofeed/lib/PicoFeed/Reader
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/fguillot/picofeed/lib/PicoFeed/Reader')
m---------vendor/fguillot/picofeed0
-rw-r--r--vendor/fguillot/picofeed/lib/PicoFeed/Reader/Favicon.php216
-rw-r--r--vendor/fguillot/picofeed/lib/PicoFeed/Reader/Reader.php211
-rw-r--r--vendor/fguillot/picofeed/lib/PicoFeed/Reader/ReaderException.php16
-rw-r--r--vendor/fguillot/picofeed/lib/PicoFeed/Reader/SubscriptionNotFoundException.php13
-rw-r--r--vendor/fguillot/picofeed/lib/PicoFeed/Reader/UnsupportedFeedFormatException.php13
6 files changed, 469 insertions, 0 deletions
diff --git a/vendor/fguillot/picofeed b/vendor/fguillot/picofeed
deleted file mode 160000
-Subproject 0a1d0d3950f7f047dc8fb1d80aa6296e15f306d
diff --git a/vendor/fguillot/picofeed/lib/PicoFeed/Reader/Favicon.php b/vendor/fguillot/picofeed/lib/PicoFeed/Reader/Favicon.php
new file mode 100644
index 000000000..6b26400dc
--- /dev/null
+++ b/vendor/fguillot/picofeed/lib/PicoFeed/Reader/Favicon.php
@@ -0,0 +1,216 @@
+<?php
+
+namespace PicoFeed\Reader;
+
+use DOMXpath;
+use PicoFeed\Client\Client;
+use PicoFeed\Client\ClientException;
+use PicoFeed\Client\Url;
+use PicoFeed\Config\Config;
+use PicoFeed\Logging\Logger;
+use PicoFeed\Parser\XmlParser;
+
+/**
+ * Favicon class
+ *
+ * https://en.wikipedia.org/wiki/Favicon
+ *
+ * @author Frederic Guillot
+ * @package Reader
+ */
+class Favicon
+{
+ /**
+ * Valid types for favicon (supported by browsers)
+ *
+ * @access private
+ * @var array
+ */
+ private $types = array(
+ 'image/png',
+ 'image/gif',
+ 'image/x-icon',
+ 'image/jpeg',
+ 'image/jpg',
+ );
+
+ /**
+ * Config class instance
+ *
+ * @access private
+ * @var \PicoFeed\Config\Config
+ */
+ private $config;
+
+ /**
+ * Icon binary content
+ *
+ * @access private
+ * @var string
+ */
+ private $content = '';
+
+ /**
+ * Icon content type
+ *
+ * @access private
+ * @var string
+ */
+ private $content_type = '';
+
+ /**
+ * Constructor
+ *
+ * @access public
+ * @param \PicoFeed\Config\Config $config Config class instance
+ */
+ public function __construct(Config $config = null)
+ {
+ $this->config = $config ?: new Config;
+ }
+
+ /**
+ * Get the icon file content (available only after the download)
+ *
+ * @access public
+ * @return string
+ */
+ public function getContent()
+ {
+ return $this->content;
+ }
+
+ /**
+ * Get the icon file type (available only after the download)
+ *
+ * @access public
+ * @return string
+ */
+ public function getType()
+ {
+ foreach ($this->types as $type) {
+ if (strpos($this->content_type, $type) === 0) {
+ return $type;
+ }
+ }
+
+ return 'image/x-icon';
+ }
+
+ /**
+ * Get data URI (http://en.wikipedia.org/wiki/Data_URI_scheme)
+ *
+ * @access public
+ * @return string
+ */
+ public function getDataUri()
+ {
+ if (empty($this->content)) {
+ return '';
+ }
+
+ return sprintf(
+ 'data:%s;base64,%s',
+ $this->getType(),
+ base64_encode($this->content)
+ );
+ }
+
+ /**
+ * Download and check if a resource exists
+ *
+ * @access public
+ * @param string $url URL
+ * @return \PicoFeed\Client Client instance
+ */
+ public function download($url)
+ {
+ $client = Client::getInstance();
+ $client->setConfig($this->config);
+
+ Logger::setMessage(get_called_class().' Download => '.$url);
+
+ try {
+ $client->execute($url);
+ }
+ catch (ClientException $e) {
+ Logger::setMessage(get_called_class().' Download Failed => '.$e->getMessage());
+ }
+
+ return $client;
+ }
+
+ /**
+ * Check if a remote file exists
+ *
+ * @access public
+ * @param string $url URL
+ * @return boolean
+ */
+ public function exists($url)
+ {
+ return $this->download($url)->getContent() !== '';
+ }
+
+ /**
+ * Get the icon link for a website
+ *
+ * @access public
+ * @param string $website_link URL
+ * @param string $favicon_link optional URL
+ * @return string
+ */
+ public function find($website_link, $favicon_link = '')
+ {
+ $website = new Url($website_link);
+
+ if ($favicon_link !== '') {
+ $icons = array($favicon_link);
+ } else {
+ $icons = $this->extract($this->download($website->getBaseUrl('/'))->getContent());
+ $icons[] = $website->getBaseUrl('/favicon.ico');
+ }
+
+ foreach ($icons as $icon_link) {
+ $icon_link = Url::resolve($icon_link, $website);
+ $resource = $this->download($icon_link);
+ $this->content = $resource->getContent();
+ $this->content_type = $resource->getContentType();
+
+ if ($this->content !== '') {
+ return $icon_link;
+ } elseif ($favicon_link !== '') {
+ return $this->find($website_link);
+ }
+ }
+
+ return '';
+ }
+
+ /**
+ * Extract the icon links from the HTML
+ *
+ * @access public
+ * @param string $html HTML
+ * @return array
+ */
+ public function extract($html)
+ {
+ $icons = array();
+
+ if (empty($html)) {
+ return $icons;
+ }
+
+ $dom = XmlParser::getHtmlDocument($html);
+
+ $xpath = new DOMXpath($dom);
+ $elements = $xpath->query("//link[contains(@rel, 'icon') and not(contains(@rel, 'apple'))]");
+
+ for ($i = 0; $i < $elements->length; $i++) {
+ $icons[] = $elements->item($i)->getAttribute('href');
+ }
+
+ return $icons;
+ }
+}
diff --git a/vendor/fguillot/picofeed/lib/PicoFeed/Reader/Reader.php b/vendor/fguillot/picofeed/lib/PicoFeed/Reader/Reader.php
new file mode 100644
index 000000000..fd629f094
--- /dev/null
+++ b/vendor/fguillot/picofeed/lib/PicoFeed/Reader/Reader.php
@@ -0,0 +1,211 @@
+<?php
+
+namespace PicoFeed\Reader;
+
+use DOMXPath;
+use PicoFeed\Config\Config;
+use PicoFeed\Client\Client;
+use PicoFeed\Client\Url;
+use PicoFeed\Logging\Logger;
+use PicoFeed\Parser\XmlParser;
+
+/**
+ * Reader class
+ *
+ * @author Frederic Guillot
+ * @package Reader
+ */
+class Reader
+{
+ /**
+ * Feed formats for detection
+ *
+ * @access private
+ * @var array
+ */
+ private $formats = array(
+ 'Atom' => '//feed',
+ 'Rss20' => '//rss[@version="2.0"]',
+ 'Rss92' => '//rss[@version="0.92"]',
+ 'Rss91' => '//rss[@version="0.91"]',
+ 'Rss10' => '//rdf',
+ );
+
+ /**
+ * Config class instance
+ *
+ * @access private
+ * @var \PicoFeed\Config\Config
+ */
+ private $config;
+
+ /**
+ * Constructor
+ *
+ * @access public
+ * @param \PicoFeed\Config\Config $config Config class instance
+ */
+ public function __construct(Config $config = null)
+ {
+ $this->config = $config ?: new Config;
+ Logger::setTimezone($this->config->getTimezone());
+ }
+
+ /**
+ * Download a feed (no discovery)
+ *
+ * @access public
+ * @param string $url Feed url
+ * @param string $last_modified Last modified HTTP header
+ * @param string $etag Etag HTTP header
+ * @return \PicoFeed\Client\Client
+ */
+ public function download($url, $last_modified = '', $etag = '')
+ {
+ $url = $this->prependScheme($url);
+
+ return Client::getInstance()
+ ->setConfig($this->config)
+ ->setLastModified($last_modified)
+ ->setEtag($etag)
+ ->execute($url);
+ }
+
+ /**
+ * Discover and download a feed
+ *
+ * @access public
+ * @param string $url Feed or website url
+ * @param string $last_modified Last modified HTTP header
+ * @param string $etag Etag HTTP header
+ * @return \PicoFeed\Client\Client
+ */
+ public function discover($url, $last_modified = '', $etag = '')
+ {
+ $client = $this->download($url, $last_modified, $etag);
+
+ // It's already a feed or the feed was not modified
+ if (! $client->isModified() || $this->detectFormat($client->getContent())) {
+ return $client;
+ }
+
+ // Try to find a subscription
+ $links = $this->find($client->getUrl(), $client->getContent());
+
+ if (empty($links)) {
+ throw new SubscriptionNotFoundException('Unable to find a subscription');
+ }
+
+ return $this->download($links[0], $last_modified, $etag);
+ }
+
+ /**
+ * Find feed urls inside a HTML document
+ *
+ * @access public
+ * @param string $url Website url
+ * @param string $html HTML content
+ * @return array List of feed links
+ */
+ public function find($url, $html)
+ {
+ Logger::setMessage(get_called_class().': Try to discover subscriptions');
+
+ $dom = XmlParser::getHtmlDocument($html);
+ $xpath = new DOMXPath($dom);
+ $links = array();
+
+ $queries = array(
+ '//link[@type="application/rss+xml"]',
+ '//link[@type="application/atom+xml"]',
+ );
+
+ foreach ($queries as $query) {
+
+ $nodes = $xpath->query($query);
+
+ foreach ($nodes as $node) {
+
+ $link = $node->getAttribute('href');
+
+ if (! empty($link)) {
+
+ $feedUrl = new Url($link);
+ $siteUrl = new Url($url);
+
+ $links[] = $feedUrl->getAbsoluteUrl($feedUrl->isRelativeUrl() ? $siteUrl->getBaseUrl() : '');
+ }
+ }
+ }
+
+ Logger::setMessage(get_called_class().': '.implode(', ', $links));
+
+ return $links;
+ }
+
+ /**
+ * Get a parser instance
+ *
+ * @access public
+ * @param string $url Site url
+ * @param string $content Feed content
+ * @param string $encoding HTTP encoding
+ * @return \PicoFeed\Parser\Parser
+ */
+ public function getParser($url, $content, $encoding)
+ {
+ $format = $this->detectFormat($content);
+
+ if (empty($format)) {
+ throw new UnsupportedFeedFormatException('Unable to detect feed format');
+ }
+
+ $className = '\PicoFeed\Parser\\'.$format;
+
+ $parser = new $className($content, $encoding, $url);
+ $parser->setHashAlgo($this->config->getParserHashAlgo());
+ $parser->setTimezone($this->config->getTimezone());
+ $parser->setConfig($this->config);
+
+ return $parser;
+ }
+
+ /**
+ * Detect the feed format
+ *
+ * @access public
+ * @param string $content Feed content
+ * @return string
+ */
+ public function detectFormat($content)
+ {
+ $dom = XmlParser::getHtmlDocument($content);
+ $xpath = new DOMXPath($dom);
+
+ foreach ($this->formats as $parser_name => $query) {
+ $nodes = $xpath->query($query);
+
+ if ($nodes->length === 1) {
+ return $parser_name;
+ }
+ }
+
+ return '';
+ }
+
+ /**
+ * Add the prefix "http://" if the end-user just enter a domain name
+ *
+ * @access public
+ * @param string $url Url
+ * @retunr string
+ */
+ public function prependScheme($url)
+ {
+ if (! preg_match('%^https?://%', $url)) {
+ $url = 'http://' . $url;
+ }
+
+ return $url;
+ }
+}
diff --git a/vendor/fguillot/picofeed/lib/PicoFeed/Reader/ReaderException.php b/vendor/fguillot/picofeed/lib/PicoFeed/Reader/ReaderException.php
new file mode 100644
index 000000000..a8e973f8a
--- /dev/null
+++ b/vendor/fguillot/picofeed/lib/PicoFeed/Reader/ReaderException.php
@@ -0,0 +1,16 @@
+<?php
+
+namespace PicoFeed\Reader;
+
+use PicoFeed\PicoFeedException;
+
+
+/**
+ * ReaderException Exception
+ *
+ * @author Frederic Guillot
+ * @package Reader
+ */
+abstract class ReaderException extends PicoFeedException
+{
+}
diff --git a/vendor/fguillot/picofeed/lib/PicoFeed/Reader/SubscriptionNotFoundException.php b/vendor/fguillot/picofeed/lib/PicoFeed/Reader/SubscriptionNotFoundException.php
new file mode 100644
index 000000000..1121fdf0e
--- /dev/null
+++ b/vendor/fguillot/picofeed/lib/PicoFeed/Reader/SubscriptionNotFoundException.php
@@ -0,0 +1,13 @@
+<?php
+
+namespace PicoFeed\Reader;
+
+/**
+ * SubscriptionNotFoundException Exception
+ *
+ * @author Frederic Guillot
+ * @package Reader
+ */
+class SubscriptionNotFoundException extends ReaderException
+{
+}
diff --git a/vendor/fguillot/picofeed/lib/PicoFeed/Reader/UnsupportedFeedFormatException.php b/vendor/fguillot/picofeed/lib/PicoFeed/Reader/UnsupportedFeedFormatException.php
new file mode 100644
index 000000000..7d4df080e
--- /dev/null
+++ b/vendor/fguillot/picofeed/lib/PicoFeed/Reader/UnsupportedFeedFormatException.php
@@ -0,0 +1,13 @@
+<?php
+
+namespace PicoFeed\Reader;
+
+/**
+ * UnsupportedFeedFormatException Exception
+ *
+ * @author Frederic Guillot
+ * @package Reader
+ */
+class UnsupportedFeedFormatException extends ReaderException
+{
+} \ No newline at end of file