diff options
Diffstat (limited to '3rdparty/fguillot/picofeed/lib')
44 files changed, 951 insertions, 669 deletions
diff --git a/3rdparty/fguillot/picofeed/lib/PicoFeed/Client.php b/3rdparty/fguillot/picofeed/lib/PicoFeed/Client/Client.php index 59e9aa9ce..7328b2c75 100644 --- a/3rdparty/fguillot/picofeed/lib/PicoFeed/Client.php +++ b/3rdparty/fguillot/picofeed/lib/PicoFeed/Client/Client.php @@ -1,10 +1,9 @@ <?php -namespace PicoFeed; +namespace PicoFeed\Client; use LogicException; -use Clients\Curl; -use Clients\Stream; +use PicoFeed\Logging\Logging; /** * Client class @@ -23,14 +22,6 @@ abstract class Client private $is_modified = true; /** - * Flag that say if the resource is a 404 - * - * @access private - * @var bool - */ - private $is_not_found = false; - - /** * HTTP encoding * * @access private @@ -135,23 +126,28 @@ abstract class Client protected $max_body_size = 2097152; // 2MB /** + * Do the HTTP request + * + * @abstract + * @access public + * @return array + */ + abstract public function doRequest(); + + /** * Get client instance: curl or stream driver * * @static * @access public - * @return \PicoFeed\Client + * @return \PicoFeed\Client\Client */ public static function getInstance() { if (function_exists('curl_init')) { - - require_once __DIR__.'/Clients/Curl.php'; - return new Clients\Curl; + return new Curl; } else if (ini_get('allow_url_fopen')) { - - require_once __DIR__.'/Clients/Stream.php'; - return new Clients\Stream; + return new Stream; } throw new LogicException('You must have "allow_url_fopen=1" or curl extension installed'); @@ -162,7 +158,7 @@ abstract class Client * * @access public * @param string $url URL - * @return bool + * @return Client */ public function execute($url = '') { @@ -176,14 +172,11 @@ abstract class Client $response = $this->doRequest(); - if (is_array($response)) { - $this->handleNotModifiedResponse($response); - $this->handleNotFoundResponse($response); - $this->handleNormalResponse($response); - return true; - } + $this->handleNotModifiedResponse($response); + $this->handleNotFoundResponse($response); + $this->handleNormalResponse($response); - return false; + return $this; } /** @@ -224,8 +217,7 @@ abstract class Client public function handleNotFoundResponse(array $response) { if ($response['status'] == 404) { - $this->is_not_found = true; - Logging::setMessage(get_called_class().' Resource not found'); + throw new InvalidUrlException('Resource not found'); } } @@ -319,7 +311,7 @@ abstract class Client * * @access public * @param string $last_modified Header value - * @return \PicoFeed\Client + * @return \PicoFeed\Client\Client */ public function setLastModified($last_modified) { @@ -343,7 +335,7 @@ abstract class Client * * @access public * @param string $etag Etag HTTP header value - * @return \PicoFeed\Client + * @return \PicoFeed\Client\Client */ public function setEtag($etag) { @@ -378,7 +370,7 @@ abstract class Client * * @access public * @return string - * @return \PicoFeed\Client + * @return \PicoFeed\Client\Client */ public function setUrl($url) { @@ -420,22 +412,11 @@ abstract class Client } /** - * Return true if the remote resource is not found - * - * @access public - * @return bool - */ - public function isNotFound() - { - return $this->is_not_found; - } - - /** * Set connection timeout * * @access public * @param integer $timeout Connection timeout - * @return \PicoFeed\Client + * @return \PicoFeed\Client\Client */ public function setTimeout($timeout) { @@ -448,7 +429,7 @@ abstract class Client * * @access public * @param string $user_agent User Agent - * @return \PicoFeed\Client + * @return \PicoFeed\Client\Client */ public function setUserAgent($user_agent) { @@ -461,7 +442,7 @@ abstract class Client * * @access public * @param integer $max Maximum - * @return \PicoFeed\Client + * @return \PicoFeed\Client\Client */ public function setMaxRedirections($max) { @@ -474,7 +455,7 @@ abstract class Client * * @access public * @param integer $max Maximum - * @return \PicoFeed\Client + * @return \PicoFeed\Client\Client */ public function setMaxBodySize($max) { @@ -487,7 +468,7 @@ abstract class Client * * @access public * @param string $hostname Proxy hostname - * @return \PicoFeed\Client + * @return \PicoFeed\Client\Client */ public function setProxyHostname($hostname) { @@ -500,7 +481,7 @@ abstract class Client * * @access public * @param integer $port Proxy port - * @return \PicoFeed\Client + * @return \PicoFeed\Client\Client */ public function setProxyPort($port) { @@ -513,7 +494,7 @@ abstract class Client * * @access public * @param string $username Proxy username - * @return \PicoFeed\Client + * @return \PicoFeed\Client\Client */ public function setProxyUsername($username) { @@ -526,7 +507,7 @@ abstract class Client * * @access public * @param string $password Password - * @return \PicoFeed\Client + * @return \PicoFeed\Client\Client */ public function setProxyPassword($password) { @@ -538,8 +519,8 @@ abstract class Client * Set config object * * @access public - * @param \PicoFeed\Config $config Config instance - * @return \PicoFeed\Client + * @param \PicoFeed\Config\Config $config Config instance + * @return \PicoFeed\Config\Config */ public function setConfig($config) { diff --git a/3rdparty/fguillot/picofeed/lib/PicoFeed/Client/ClientException.php b/3rdparty/fguillot/picofeed/lib/PicoFeed/Client/ClientException.php new file mode 100644 index 000000000..0e27452ed --- /dev/null +++ b/3rdparty/fguillot/picofeed/lib/PicoFeed/Client/ClientException.php @@ -0,0 +1,16 @@ +<?php + +namespace PicoFeed\Client; + +use PicoFeed\PicoFeedException; + + +/** + * ClientException Exception + * + * @author Frederic Guillot + * @package Client + */ +abstract class ClientException extends PicoFeedException +{ +} diff --git a/3rdparty/fguillot/picofeed/lib/PicoFeed/Clients/Curl.php b/3rdparty/fguillot/picofeed/lib/PicoFeed/Client/Curl.php index 055f72e4f..9cf3eb6f4 100644 --- a/3rdparty/fguillot/picofeed/lib/PicoFeed/Clients/Curl.php +++ b/3rdparty/fguillot/picofeed/lib/PicoFeed/Client/Curl.php @@ -1,15 +1,14 @@ <?php -namespace PicoFeed\Clients; +namespace PicoFeed\Client; -use \PicoFeed\Logging; -use \PicoFeed\Client; +use PicoFeed\Logging\Logging; /** * cURL HTTP client * * @author Frederic Guillot - * @package client + * @package Client */ class Curl extends Client { @@ -159,8 +158,6 @@ class Curl extends Client $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $this->url); - curl_setopt($ch, CURLOPT_FRESH_CONNECT, true); - curl_setopt($ch, CURLOPT_FORBID_REUSE, true); curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->timeout); curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout); @@ -168,7 +165,6 @@ class Curl extends Client curl_setopt($ch, CURLOPT_FOLLOWLOCATION, ini_get('open_basedir') === ''); curl_setopt($ch, CURLOPT_MAXREDIRS, $this->max_redirects); curl_setopt($ch, CURLOPT_ENCODING, ''); - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // For auto-signed certificates... curl_setopt($ch, CURLOPT_WRITEFUNCTION, array($this, 'readBody')); curl_setopt($ch, CURLOPT_HEADERFUNCTION, array($this, 'readHeaders')); curl_setopt($ch, CURLOPT_COOKIEJAR, 'php://memory'); @@ -183,7 +179,6 @@ class Curl extends Client * Execute curl context * * @access private - * @return resource */ private function executeContext() { @@ -196,15 +191,16 @@ class Curl extends Client Logging::setMessage(get_called_class().' cURL speed download: '.curl_getinfo($ch, CURLINFO_SPEED_DOWNLOAD)); Logging::setMessage(get_called_class().' cURL effective url: '.curl_getinfo($ch, CURLINFO_EFFECTIVE_URL)); - if (curl_errno($ch)) { + $curl_errno = curl_errno($ch); + + if ($curl_errno) { Logging::setMessage(get_called_class().' cURL error: '.curl_error($ch)); curl_close($ch); - return false; + + $this->handleError($curl_errno); } curl_close($ch); - - return true; } /** @@ -216,9 +212,7 @@ class Curl extends Client */ public function doRequest($follow_location = true) { - if (! $this->executeContext()) { - return false; - } + $this->executeContext(); list($status, $headers) = $this->parseHeaders(explode("\r\n", $this->headers[$this->headers_counter - 1])); @@ -287,4 +281,44 @@ class Curl extends Client return false; } + + /** + * Handle cURL errors (throw individual exceptions) + * + * We don't use constants because they are not necessary always available + * (depends of the version of libcurl linked to php) + * + * @see http://curl.haxx.se/libcurl/c/libcurl-errors.html + * @access private + * @param integer $errno cURL error code + */ + private function handleError($errno) + { + switch ($errno) { + case 78: // CURLE_REMOTE_FILE_NOT_FOUND + throw new InvalidUrlException('Resource not found'); + case 6: // CURLE_COULDNT_RESOLVE_HOST + throw new InvalidUrlException('Unable to resolve hostname'); + case 7: // CURLE_COULDNT_CONNECT + throw new InvalidUrlException('Unable to connect to the remote host'); + case 28: // CURLE_OPERATION_TIMEDOUT + throw new TimeoutException('Operation timeout'); + case 35: // CURLE_SSL_CONNECT_ERROR + case 51: // CURLE_PEER_FAILED_VERIFICATION + case 58: // CURLE_SSL_CERTPROBLEM + case 60: // CURLE_SSL_CACERT + case 59: // CURLE_SSL_CIPHER + case 64: // CURLE_USE_SSL_FAILED + case 66: // CURLE_SSL_ENGINE_INITFAILED + case 77: // CURLE_SSL_CACERT_BADFILE + case 83: // CURLE_SSL_ISSUER_ERROR + throw new InvalidCertificateException('Invalid SSL certificate'); + case 47: // CURLE_TOO_MANY_REDIRECTS + throw new MaxRedirectException('Maximum number of redirections reached'); + case 63: // CURLE_FILESIZE_EXCEEDED + throw new MaxSizeException('Maximum response size exceeded'); + default: + throw new InvalidUrlException('Unable to fetch the URL'); + } + } } diff --git a/3rdparty/fguillot/picofeed/lib/PicoFeed/Favicon.php b/3rdparty/fguillot/picofeed/lib/PicoFeed/Client/Favicon.php index ec8753107..b6d3b6d26 100644 --- a/3rdparty/fguillot/picofeed/lib/PicoFeed/Favicon.php +++ b/3rdparty/fguillot/picofeed/lib/PicoFeed/Client/Favicon.php @@ -1,16 +1,20 @@ <?php -namespace PicoFeed; +namespace PicoFeed\Client; use DOMXpath; +use PicoFeed\Config\Config; +use PicoFeed\Logging\Logging; +use PicoFeed\Parser\XmlParser; + /** * Favicon class * * https://en.wikipedia.org/wiki/Favicon * * @author Frederic Guillot - * @package picofeed + * @package Client */ class Favicon { @@ -18,9 +22,9 @@ class Favicon * Config class instance * * @access private - * @var \PicoFeed\Config + * @var \PicoFeed\Config\Config */ - private $config = null; + private $config; /** * Icon content @@ -34,7 +38,7 @@ class Favicon * Constructor * * @access public - * @param \PicoFeed\Config $config Config class instance + * @param \PicoFeed\Config\Config $config Config class instance */ public function __construct(Config $config = null) { @@ -61,16 +65,19 @@ class Favicon */ public function download($url) { - Logging::setMessage(get_called_class().' Download => '.$url); + try { + + Logging::setMessage(get_called_class().' Download => '.$url); - $client = Client::getInstance(); - $client->setConfig($this->config); + $client = Client::getInstance(); + $client->setConfig($this->config); + $client->execute($url); - if ($client->execute($url) && ! $client->isNotFound()) { return $client->getContent(); } - - return ''; + catch (ClientException $e) { + return ''; + } } /** @@ -116,8 +123,8 @@ class Favicon * Convert icon links to absolute url * * @access public - * @param \PicoFeed\Url $website Website url - * @param \PicoFeed\Url $icon Icon url + * @param \PicoFeed\Client\Url $website Website url + * @param \PicoFeed\Client\Url $icon Icon url * @return string */ public function convertLink(Url $website, Url $icon) diff --git a/3rdparty/fguillot/picofeed/lib/PicoFeed/Grabber.php b/3rdparty/fguillot/picofeed/lib/PicoFeed/Client/Grabber.php index 97f1e0574..a072fc805 100644 --- a/3rdparty/fguillot/picofeed/lib/PicoFeed/Grabber.php +++ b/3rdparty/fguillot/picofeed/lib/PicoFeed/Client/Grabber.php @@ -1,14 +1,19 @@ <?php -namespace PicoFeed; +namespace PicoFeed\Client; use DOMXPath; +use PicoFeed\Encoding\Encoding; +use PicoFeed\Logging\Logging; +use PicoFeed\Filter\Filter; +use PicoFeed\Parser\XmlParser; + /** * Grabber class * * @author Frederic Guillot - * @package picofeed + * @package Client */ class Grabber { @@ -119,9 +124,9 @@ class Grabber * Config object * * @access private - * @var \PicoFeed\Config + * @var \PicoFeed\Config\Config */ - private $config = null; + private $config; /** * Constructor @@ -259,7 +264,7 @@ class Grabber foreach ($files as $file) { - $filename = __DIR__.'/Rules/'.$file.'.php'; + $filename = __DIR__.'/../Rules/'.$file.'.php'; if (file_exists($filename)) { Logging::setMessage(get_called_class().' Load rule: '.$file); diff --git a/3rdparty/fguillot/picofeed/lib/PicoFeed/Client/InvalidCertificateException.php b/3rdparty/fguillot/picofeed/lib/PicoFeed/Client/InvalidCertificateException.php new file mode 100644 index 000000000..ece3f303f --- /dev/null +++ b/3rdparty/fguillot/picofeed/lib/PicoFeed/Client/InvalidCertificateException.php @@ -0,0 +1,13 @@ +<?php + +namespace PicoFeed\Client; + +/** + * InvalidCertificateException Exception + * + * @author Frederic Guillot + * @package Client + */ +class InvalidCertificateException extends ClientException +{ +} diff --git a/3rdparty/fguillot/picofeed/lib/PicoFeed/Client/InvalidUrlException.php b/3rdparty/fguillot/picofeed/lib/PicoFeed/Client/InvalidUrlException.php new file mode 100644 index 000000000..0298f0dc8 --- /dev/null +++ b/3rdparty/fguillot/picofeed/lib/PicoFeed/Client/InvalidUrlException.php @@ -0,0 +1,13 @@ +<?php + +namespace PicoFeed\Client; + +/** + * InvalidUrlException Exception + * + * @author Frederic Guillot + * @package Client + */ +class InvalidUrlException extends ClientException +{ +} diff --git a/3rdparty/fguillot/picofeed/lib/PicoFeed/Client/MaxRedirectException.php b/3rdparty/fguillot/picofeed/lib/PicoFeed/Client/MaxRedirectException.php new file mode 100644 index 000000000..1651d7f6b --- /dev/null +++ b/3rdparty/fguillot/picofeed/lib/PicoFeed/Client/MaxRedirectException.php @@ -0,0 +1,13 @@ +<?php + +namespace PicoFeed\Client; + +/** + * MaxRedirectException Exception + * + * @author Frederic Guillot + * @package Client + */ +class MaxRedirectException extends ClientException +{ +} diff --git a/3rdparty/fguillot/picofeed/lib/PicoFeed/Client/MaxSizeException.php b/3rdparty/fguillot/picofeed/lib/PicoFeed/Client/MaxSizeException.php new file mode 100644 index 000000000..60bb9f132 --- /dev/null +++ b/3rdparty/fguillot/picofeed/lib/PicoFeed/Client/MaxSizeException.php @@ -0,0 +1,13 @@ +<?php + +namespace PicoFeed\Client; + +/** + * MaxSizeException Exception + * + * @author Frederic Guillot + * @package Client + */ +class MaxSizeException extends ClientException +{ +} diff --git a/3rdparty/fguillot/picofeed/lib/PicoFeed/Clients/Stream.php b/3rdparty/fguillot/picofeed/lib/PicoFeed/Client/Stream.php index f16952fc7..bc9809c4d 100644 --- a/3rdparty/fguillot/picofeed/lib/PicoFeed/Clients/Stream.php +++ b/3rdparty/fguillot/picofeed/lib/PicoFeed/Client/Stream.php @@ -1,15 +1,14 @@ <?php -namespace PicoFeed\Clients; +namespace PicoFeed\Client; -use \PicoFeed\Logging; -use \PicoFeed\Client; +use PicoFeed\Logging\Logging; /** * Stream context HTTP client * * @author Frederic Guillot - * @package client + * @package Client */ class Stream extends Client { @@ -96,7 +95,7 @@ class Stream extends Client // Make HTTP request $stream = @fopen($this->url, 'r', false, $context); if (! is_resource($stream)) { - return false; + throw new InvalidUrlException('Unable to establish a connection'); |