miniflux-legacy/vendor/miniflux/picofeed/lib/PicoFeed/Client/Client.php

713 lines
15 KiB
PHP
Raw Permalink Normal View History

2013-07-17 01:54:44 +02:00
<?php
namespace PicoFeed\Client;
2013-07-17 01:54:44 +02:00
use DateTime;
2017-04-05 22:05:48 +02:00
use Exception;
2014-05-20 20:20:27 +02:00
use LogicException;
use PicoFeed\Logging\Logger;
use PicoFeed\Config\Config;
2014-05-20 20:20:27 +02:00
/**
* Client class.
2014-05-20 20:20:27 +02:00
*
* @author Frederic Guillot
*/
2013-07-17 01:54:44 +02:00
abstract class Client
{
2014-05-20 20:20:27 +02:00
/**
* Flag that say if the resource have been modified.
2014-05-20 20:20:27 +02:00
*
* @var bool
*/
private $is_modified = true;
2014-10-19 20:42:31 +02:00
/**
* HTTP Content-Type.
2014-10-19 20:42:31 +02:00
*
* @var string
2014-10-19 20:42:31 +02:00
*/
private $content_type = '';
2014-10-19 20:42:31 +02:00
2014-05-20 20:20:27 +02:00
/**
* HTTP encoding.
2014-05-20 20:20:27 +02:00
*
* @var string
*/
private $encoding = '';
2015-04-11 02:34:48 +02:00
/**
* HTTP request headers.
2015-04-11 02:34:48 +02:00
*
* @var array
*/
protected $request_headers = array();
2014-05-20 20:20:27 +02:00
/**
* HTTP Etag header.
2014-05-20 20:20:27 +02:00
*
* @var string
*/
protected $etag = '';
/**
* HTTP Last-Modified header.
2014-05-20 20:20:27 +02:00
*
* @var string
*/
protected $last_modified = '';
/**
* Expiration DateTime
*
* @var DateTime
*/
protected $expiration = null;
2014-05-20 20:20:27 +02:00
/**
* Proxy hostname.
2014-05-20 20:20:27 +02:00
*
* @var string
*/
protected $proxy_hostname = '';
/**
* Proxy port.
2014-05-20 20:20:27 +02:00
*
* @var int
2014-05-20 20:20:27 +02:00
*/
protected $proxy_port = 3128;
/**
* Proxy username.
2014-05-20 20:20:27 +02:00
*
* @var string
*/
protected $proxy_username = '';
/**
* Proxy password.
2014-05-20 20:20:27 +02:00
*
* @var string
*/
protected $proxy_password = '';
2014-12-24 19:50:20 +01:00
/**
* Basic auth username.
2014-12-24 19:50:20 +01:00
*
* @var string
*/
protected $username = '';
/**
* Basic auth password.
2014-12-24 19:50:20 +01:00
*
* @var string
*/
protected $password = '';
2017-06-21 01:41:55 +02:00
/**
* CURL options.
*
* @var array
*/
protected $additional_curl_options = array();
2014-05-20 20:20:27 +02:00
/**
* Client connection timeout.
2014-05-20 20:20:27 +02:00
*
* @var int
2014-05-20 20:20:27 +02:00
*/
protected $timeout = 10;
/**
* User-agent.
2014-05-20 20:20:27 +02:00
*
* @var string
*/
2017-06-12 03:08:00 +02:00
protected $user_agent = 'PicoFeed (https://github.com/miniflux/picoFeed)';
2014-05-20 20:20:27 +02:00
/**
* Real URL used (can be changed after a HTTP redirect).
2014-05-20 20:20:27 +02:00
*
* @var string
*/
protected $url = '';
/**
* Page/Feed content.
2014-05-20 20:20:27 +02:00
*
* @var string
*/
protected $content = '';
/**
* Number maximum of HTTP redirections to avoid infinite loops.
2014-05-20 20:20:27 +02:00
*
* @var int
2014-05-20 20:20:27 +02:00
*/
protected $max_redirects = 5;
/**
* Maximum size of the HTTP body response.
2014-05-20 20:20:27 +02:00
*
* @var int
2014-05-20 20:20:27 +02:00
*/
protected $max_body_size = 2097152; // 2MB
2014-12-24 19:50:20 +01:00
/**
* HTTP response status code.
2014-12-24 19:50:20 +01:00
*
* @var int
2014-12-24 19:50:20 +01:00
*/
protected $status_code = 0;
2015-02-06 03:16:34 +01:00
/**
* Enables direct passthrough to requesting client.
2015-02-06 03:16:34 +01:00
*
* @var bool
*/
protected $passthrough = false;
/**
* Do the HTTP request.
*
* @abstract
*
* @return array
*/
2015-02-01 20:48:05 +01:00
abstract public function doRequest();
2014-05-20 20:20:27 +02:00
/**
* Get client instance: curl or stream driver.
2014-05-20 20:20:27 +02:00
*
* @static
*
* @return \PicoFeed\Client\Client
2014-05-20 20:20:27 +02:00
*/
public static function getInstance()
2013-07-17 01:54:44 +02:00
{
if (function_exists('curl_init')) {
return new Curl();
} elseif (ini_get('allow_url_fopen')) {
return new Stream();
2013-07-17 01:54:44 +02:00
}
2014-05-20 20:20:27 +02:00
throw new LogicException('You must have "allow_url_fopen=1" or curl extension installed');
2013-07-17 01:54:44 +02:00
}
2015-04-11 02:34:48 +02:00
/**
* Add HTTP Header to the request.
2015-04-11 02:34:48 +02:00
*
* @param array $headers
*/
public function setHeaders($headers)
{
2015-04-11 02:34:48 +02:00
$this->request_headers = $headers;
}
2014-05-20 20:20:27 +02:00
/**
* Perform the HTTP request.
*
* @param string $url URL
2014-05-20 20:20:27 +02:00
*
* @return Client
2014-05-20 20:20:27 +02:00
*/
public function execute($url = '')
2013-07-17 01:54:44 +02:00
{
2014-05-20 20:20:27 +02:00
if ($url !== '') {
$this->url = $url;
2013-07-17 01:54:44 +02:00
}
Logger::setMessage(get_called_class().' Fetch URL: '.$this->url);
Logger::setMessage(get_called_class().' Etag provided: '.$this->etag);
Logger::setMessage(get_called_class().' Last-Modified provided: '.$this->last_modified);
2013-07-17 01:54:44 +02:00
$response = $this->doRequest();
2014-12-24 19:50:20 +01:00
$this->status_code = $response['status'];
$this->handleNotModifiedResponse($response);
2016-03-31 04:43:08 +02:00
$this->handleErrorResponse($response);
$this->handleNormalResponse($response);
2014-10-19 20:42:31 +02:00
$this->expiration = $this->parseExpiration($response['headers']);
Logger::setMessage(get_called_class().' Expiration: '.$this->expiration->format(DATE_ISO8601));
return $this;
2014-10-19 20:42:31 +02:00
}
/**
* Handle not modified response.
2014-10-19 20:42:31 +02:00
*
* @param array $response Client response
2014-10-19 20:42:31 +02:00
*/
2016-03-31 04:43:08 +02:00
protected function handleNotModifiedResponse(array $response)
2014-10-19 20:42:31 +02:00
{
if ($response['status'] == 304) {
$this->is_modified = false;
} elseif ($response['status'] == 200) {
$this->is_modified = $this->hasBeenModified($response, $this->etag, $this->last_modified);
$this->etag = $this->getHeader($response, 'ETag');
$this->last_modified = $this->getHeader($response, 'Last-Modified');
2013-07-17 01:54:44 +02:00
}
2014-10-19 20:42:31 +02:00
if ($this->is_modified === false) {
Logger::setMessage(get_called_class().' Resource not modified');
2014-10-19 20:42:31 +02:00
}
}
/**
2016-03-31 04:43:08 +02:00
* Handle Http Error codes
2014-10-19 20:42:31 +02:00
*
* @param array $response Client response
* @throws ForbiddenException
* @throws InvalidUrlException
* @throws UnauthorizedException
2014-10-19 20:42:31 +02:00
*/
2016-03-31 04:43:08 +02:00
protected function handleErrorResponse(array $response)
2014-10-19 20:42:31 +02:00
{
2016-03-31 04:43:08 +02:00
$status = $response['status'];
if ($status == 401) {
throw new UnauthorizedException('Wrong or missing credentials');
} else if ($status == 403) {
throw new ForbiddenException('Not allowed to access resource');
} else if ($status == 404) {
throw new InvalidUrlException('Resource not found');
2014-10-19 20:42:31 +02:00
}
}
/**
* Handle normal response.
2014-10-19 20:42:31 +02:00
*
* @param array $response Client response
2014-10-19 20:42:31 +02:00
*/
2016-03-31 04:43:08 +02:00
protected function handleNormalResponse(array $response)
2014-10-19 20:42:31 +02:00
{
if ($response['status'] == 200) {
$this->content = $response['body'];
$this->content_type = $this->findContentType($response);
$this->encoding = $this->findCharset();
2014-10-19 20:42:31 +02:00
}
}
/**
* Check if a request has been modified according to the parameters.
*
* @param array $response
* @param string $etag
* @param string $lastModified
2014-10-19 20:42:31 +02:00
*
* @return bool
2014-10-19 20:42:31 +02:00
*/
private function hasBeenModified($response, $etag, $lastModified)
2014-10-19 20:42:31 +02:00
{
$headers = array(
'Etag' => $etag,
'Last-Modified' => $lastModified,
);
// Compare the values for each header that is present
$presentCacheHeaderCount = 0;
foreach ($headers as $key => $value) {
if (isset($response['headers'][$key])) {
if ($response['headers'][$key] !== $value) {
return true;
}
++$presentCacheHeaderCount;
}
}
// If at least one header is present and the values match, the response
// was not modified
if ($presentCacheHeaderCount > 0) {
return false;
}
return true;
2014-10-19 20:42:31 +02:00
}
/**
* Find content type from response headers.
*
* @param array $response Client response
* @return string
2014-10-19 20:42:31 +02:00
*/
public function findContentType(array $response)
2014-10-19 20:42:31 +02:00
{
return strtolower($this->getHeader($response, 'Content-Type'));
}
/**
* Find charset from response headers.
*
* @return string
*/
public function findCharset()
{
$result = explode('charset=', $this->content_type);
2014-10-19 20:42:31 +02:00
return isset($result[1]) ? $result[1] : '';
}
/**
* Get header value from a client response.
*
* @param array $response Client response
* @param string $header Header name
2014-10-19 20:42:31 +02:00
* @return string
*/
public function getHeader(array $response, $header)
{
return isset($response['headers'][$header]) ? $response['headers'][$header] : '';
2014-05-20 20:20:27 +02:00
}
2013-07-17 01:54:44 +02:00
2014-05-20 20:20:27 +02:00
/**
* Set the Last-Modified HTTP header.
*
* @param string $last_modified Header value
2017-06-21 01:41:55 +02:00
* @return $this
2014-05-20 20:20:27 +02:00
*/
2013-07-17 01:54:44 +02:00
public function setLastModified($last_modified)
{
$this->last_modified = $last_modified;
return $this;
}
2014-05-20 20:20:27 +02:00
/**
* Get the value of the Last-Modified HTTP header.
2014-05-20 20:20:27 +02:00
*
* @return string
*/
2013-07-17 01:54:44 +02:00
public function getLastModified()
{
return $this->last_modified;
}
2014-05-20 20:20:27 +02:00
/**
* Set the value of the Etag HTTP header.
*
* @param string $etag Etag HTTP header value
2017-06-21 01:41:55 +02:00
* @return $this
2014-05-20 20:20:27 +02:00
*/
2013-07-17 01:54:44 +02:00
public function setEtag($etag)
{
$this->etag = $etag;
return $this;
}
2014-05-20 20:20:27 +02:00
/**
* Get the Etag HTTP header value.
2014-05-20 20:20:27 +02:00
*
* @return string
*/
2013-07-17 01:54:44 +02:00
public function getEtag()
{
return $this->etag;
}
2014-05-20 20:20:27 +02:00
/**
* Get the final url value.
2014-05-20 20:20:27 +02:00
*
* @return string
*/
2013-07-17 01:54:44 +02:00
public function getUrl()
{
return $this->url;
}
2014-05-20 20:20:27 +02:00
/**
* Set the url.
2014-05-20 20:20:27 +02:00
*
* @param $url
2014-05-20 20:20:27 +02:00
* @return string
*/
public function setUrl($url)
{
$this->url = $url;
return $this;
}
2013-07-17 01:54:44 +02:00
2014-12-24 19:50:20 +01:00
/**
* Get the HTTP response status code.
2014-12-24 19:50:20 +01:00
*
* @return int
2014-12-24 19:50:20 +01:00
*/
public function getStatusCode()
{
return $this->status_code;
}
2014-05-20 20:20:27 +02:00
/**
* Get the body of the HTTP response.
2014-05-20 20:20:27 +02:00
*
* @return string
*/
2013-07-17 01:54:44 +02:00
public function getContent()
{
return $this->content;
}
2014-05-20 20:20:27 +02:00
/**
* Get the content type value from HTTP headers.
2014-05-20 20:20:27 +02:00
*
* @return string
*/
public function getContentType()
2013-10-04 05:14:39 +02:00
{
return $this->content_type;
2013-10-04 05:14:39 +02:00
}
2014-05-20 20:20:27 +02:00
/**
* Get the encoding value from HTTP headers.
2014-05-20 20:20:27 +02:00
*
* @return string
2014-05-20 20:20:27 +02:00
*/
public function getEncoding()
2013-07-17 01:54:44 +02:00
{
return $this->encoding;
2013-07-17 01:54:44 +02:00
}
2014-05-20 20:20:27 +02:00
2014-10-19 20:42:31 +02:00
/**
* Return true if the remote resource has changed.
2014-10-19 20:42:31 +02:00
*
* @return bool
*/
public function isModified()
2014-10-19 20:42:31 +02:00
{
return $this->is_modified;
2014-10-19 20:42:31 +02:00
}
2015-02-06 03:16:34 +01:00
/**
* return true if passthrough mode is enabled.
2015-02-06 03:16:34 +01:00
*
* @return bool
*/
public function isPassthroughEnabled()
{
return $this->passthrough;
}
2014-05-20 20:20:27 +02:00
/**
* Set connection timeout.
*
* @param int $timeout Connection timeout
2017-06-21 01:41:55 +02:00
* @return $this
2014-05-20 20:20:27 +02:00
*/
public function setTimeout($timeout)
{
$this->timeout = $timeout ?: $this->timeout;
return $this;
}
/**
* Set a custom user agent.
*
* @param string $user_agent User Agent
2017-06-21 01:41:55 +02:00
* @return $this
2014-05-20 20:20:27 +02:00
*/
public function setUserAgent($user_agent)
{
$this->user_agent = $user_agent ?: $this->user_agent;
return $this;
}
/**
2015-12-16 01:26:15 +01:00
* Set the maximum number of HTTP redirections.
*
* @param int $max Maximum
2017-06-21 01:41:55 +02:00
* @return $this
2014-05-20 20:20:27 +02:00
*/
public function setMaxRedirections($max)
{
$this->max_redirects = $max ?: $this->max_redirects;
return $this;
}
/**
* Set the maximum size of the HTTP body.
*
* @param int $max Maximum
2017-06-21 01:41:55 +02:00
* @return $this
2014-05-20 20:20:27 +02:00
*/
public function setMaxBodySize($max)
{
$this->max_body_size = $max ?: $this->max_body_size;
return $this;
}
/**
* Set the proxy hostname.
*
* @param string $hostname Proxy hostname
2017-06-21 01:41:55 +02:00
* @return $this
2014-05-20 20:20:27 +02:00
*/
public function setProxyHostname($hostname)
{
$this->proxy_hostname = $hostname ?: $this->proxy_hostname;
return $this;
}
/**
* Set the proxy port.
*
* @param int $port Proxy port
2017-06-21 01:41:55 +02:00
* @return $this
2014-05-20 20:20:27 +02:00
*/
public function setProxyPort($port)
{
$this->proxy_port = $port ?: $this->proxy_port;
return $this;
}
/**
* Set the proxy username.
*
* @param string $username Proxy username
2017-06-21 01:41:55 +02:00
* @return $this
2014-05-20 20:20:27 +02:00
*/
public function setProxyUsername($username)
{
$this->proxy_username = $username ?: $this->proxy_username;
return $this;
}
/**
* Set the proxy password.
*
* @param string $password Password
2017-06-21 01:41:55 +02:00
* @return $this
2014-05-20 20:20:27 +02:00
*/
public function setProxyPassword($password)
{
$this->proxy_password = $password ?: $this->proxy_password;
return $this;
}
2014-12-24 19:50:20 +01:00
/**
* Set the username.
*
* @param string $username Basic Auth username
2014-12-24 19:50:20 +01:00
*
2017-06-21 01:41:55 +02:00
* @return $this
2014-12-24 19:50:20 +01:00
*/
public function setUsername($username)
{
$this->username = $username ?: $this->username;
return $this;
}
/**
* Set the password.
*
* @param string $password Basic Auth Password
2014-12-24 19:50:20 +01:00
*
2017-06-21 01:41:55 +02:00
* @return $this
2014-12-24 19:50:20 +01:00
*/
public function setPassword($password)
{
$this->password = $password ?: $this->password;
2017-06-21 01:41:55 +02:00
return $this;
}
2017-06-21 01:41:55 +02:00
/**
* Set the CURL options.
*
* @param array $options
* @return $this
*/
public function setAdditionalCurlOptions(array $options)
{
$this->additional_curl_options = $options ?: $this->additional_curl_options;
2014-12-24 19:50:20 +01:00
return $this;
}
2017-06-21 01:41:55 +02:00
2015-02-06 03:16:34 +01:00
/**
* Enable the passthrough mode.
2015-02-06 03:16:34 +01:00
*
2017-06-21 01:41:55 +02:00
* @return $this
2015-02-06 03:16:34 +01:00
*/
public function enablePassthroughMode()
{
$this->passthrough = true;
return $this;
}
/**
* Disable the passthrough mode.
2015-02-06 03:16:34 +01:00
*
2017-06-21 01:41:55 +02:00
* @return $this
2015-02-06 03:16:34 +01:00
*/
public function disablePassthroughMode()
{
$this->passthrough = false;
return $this;
}
2014-05-20 20:20:27 +02:00
/**
* Set config object.
*
* @param \PicoFeed\Config\Config $config Config instance
2017-06-21 01:41:55 +02:00
* @return $this
2014-05-20 20:20:27 +02:00
*/
public function setConfig(Config $config)
2014-05-20 20:20:27 +02:00
{
2014-10-19 20:42:31 +02:00
if ($config !== null) {
2015-04-11 02:34:48 +02:00
$this->setTimeout($config->getClientTimeout());
$this->setUserAgent($config->getClientUserAgent());
2014-10-19 20:42:31 +02:00
$this->setMaxRedirections($config->getMaxRedirections());
$this->setMaxBodySize($config->getMaxBodySize());
$this->setProxyHostname($config->getProxyHostname());
$this->setProxyPort($config->getProxyPort());
$this->setProxyUsername($config->getProxyUsername());
$this->setProxyPassword($config->getProxyPassword());
2017-06-21 01:41:55 +02:00
$this->setAdditionalCurlOptions($config->getAdditionalCurlOptions() ?: array());
2014-10-19 20:42:31 +02:00
}
2014-05-20 20:20:27 +02:00
return $this;
}
/**
* Return true if the HTTP status code is a redirection
*
* @access protected
* @param integer $code
* @return boolean
*/
public function isRedirection($code)
{
return $code == 301 || $code == 302 || $code == 303 || $code == 307;
}
public function parseExpiration(HttpHeaders $headers)
{
2017-04-05 22:05:48 +02:00
try {
if (isset($headers['Cache-Control'])) {
if (preg_match('/s-maxage=(\d+)/', $headers['Cache-Control'], $matches)) {
return new DateTime('+' . $matches[1] . ' seconds');
} else if (preg_match('/max-age=(\d+)/', $headers['Cache-Control'], $matches)) {
return new DateTime('+' . $matches[1] . ' seconds');
}
}
2017-04-05 22:05:48 +02:00
if (! empty($headers['Expires'])) {
return new DateTime($headers['Expires']);
}
} catch (Exception $e) {
Logger::setMessage('Unable to parse expiration date: '.$e->getMessage());
}
return new DateTime();
}
/**
* Get expiration date time from "Expires" or "Cache-Control" headers
*
* @return DateTime
*/
public function getExpiration()
{
return $this->expiration ?: new DateTime();
}
2014-05-20 20:20:27 +02:00
}