2018-10-09 19:58:58 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Friendica\Network;
|
|
|
|
|
2018-10-29 22:20:46 +01:00
|
|
|
use Friendica\Core\Logger;
|
2018-10-10 21:08:43 +02:00
|
|
|
use Friendica\Network\HTTPException\InternalServerErrorException;
|
2018-11-24 01:27:00 +01:00
|
|
|
use Friendica\Util\Network;
|
2018-10-10 21:08:43 +02:00
|
|
|
|
2018-10-09 19:58:58 +02:00
|
|
|
/**
|
|
|
|
* A content class for Curl call results
|
|
|
|
*/
|
2018-10-10 22:08:13 +02:00
|
|
|
class CurlResult
|
2018-10-09 19:58:58 +02:00
|
|
|
{
|
|
|
|
/**
|
2018-10-10 21:08:43 +02:00
|
|
|
* @var int HTTP return code or 0 if timeout or failure
|
2018-10-09 19:58:58 +02:00
|
|
|
*/
|
2018-10-10 21:08:43 +02:00
|
|
|
private $returnCode;
|
2018-10-09 19:58:58 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string the content type of the Curl call
|
|
|
|
*/
|
|
|
|
private $contentType;
|
|
|
|
|
|
|
|
/**
|
2018-10-10 21:08:43 +02:00
|
|
|
* @var string the HTTP headers of the Curl call
|
2018-10-09 19:58:58 +02:00
|
|
|
*/
|
2018-10-10 21:08:43 +02:00
|
|
|
private $header;
|
2018-10-09 19:58:58 +02:00
|
|
|
|
2018-10-10 21:08:43 +02:00
|
|
|
/**
|
|
|
|
* @var boolean true (if HTTP 2xx result) or false
|
|
|
|
*/
|
|
|
|
private $isSuccess;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string the URL which was called
|
|
|
|
*/
|
|
|
|
private $url;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string in case of redirect, content was finally retrieved from this URL
|
|
|
|
*/
|
|
|
|
private $redirectUrl;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string fetched content
|
|
|
|
*/
|
|
|
|
private $body;
|
2018-10-09 19:58:58 +02:00
|
|
|
|
|
|
|
/**
|
2018-10-10 21:08:43 +02:00
|
|
|
* @var array some informations about the fetched data
|
|
|
|
*/
|
|
|
|
private $info;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var boolean true if the URL has a redirect
|
|
|
|
*/
|
|
|
|
private $isRedirectUrl;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var boolean true if the curl request timed out
|
|
|
|
*/
|
|
|
|
private $isTimeout;
|
|
|
|
|
|
|
|
/**
|
2018-10-10 21:20:30 +02:00
|
|
|
* @var int the error number or 0 (zero) if no error
|
2018-10-10 21:08:43 +02:00
|
|
|
*/
|
|
|
|
private $errorNumber;
|
|
|
|
|
|
|
|
/**
|
2018-10-10 21:20:30 +02:00
|
|
|
* @var string the error message or '' (the empty string) if no
|
2018-10-10 21:08:43 +02:00
|
|
|
*/
|
|
|
|
private $error;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates an errored CURL response
|
2018-10-09 19:58:58 +02:00
|
|
|
*
|
2018-10-10 21:08:43 +02:00
|
|
|
* @param string $url optional URL
|
|
|
|
*
|
2018-10-10 22:08:13 +02:00
|
|
|
* @return CurlResult a CURL with error response
|
2019-01-06 22:06:53 +01:00
|
|
|
* @throws InternalServerErrorException
|
2018-10-09 19:58:58 +02:00
|
|
|
*/
|
2018-10-10 21:08:43 +02:00
|
|
|
public static function createErrorCurl($url = '')
|
2018-10-09 19:58:58 +02:00
|
|
|
{
|
2018-10-10 22:51:26 +02:00
|
|
|
return new CurlResult($url, '', ['http_code' => 0]);
|
2018-10-09 19:58:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-10-10 21:08:43 +02:00
|
|
|
* Curl constructor.
|
|
|
|
* @param string $url the URL which was called
|
|
|
|
* @param string $result the result of the curl execution
|
|
|
|
* @param array $info an additional info array
|
|
|
|
* @param int $errorNumber the error number or 0 (zero) if no error
|
|
|
|
* @param string $error the error message or '' (the empty string) if no
|
2018-10-09 19:58:58 +02:00
|
|
|
*
|
2018-10-10 21:08:43 +02:00
|
|
|
* @throws InternalServerErrorException when HTTP code of the CURL response is missing
|
2018-10-09 19:58:58 +02:00
|
|
|
*/
|
2018-10-10 21:08:43 +02:00
|
|
|
public function __construct($url, $result, $info, $errorNumber = 0, $error = '')
|
|
|
|
{
|
2018-10-10 21:20:30 +02:00
|
|
|
if (!array_key_exists('http_code', $info)) {
|
2018-10-10 21:08:43 +02:00
|
|
|
throw new InternalServerErrorException('CURL response doesn\'t contains a response HTTP code');
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->returnCode = $info['http_code'];
|
|
|
|
$this->url = $url;
|
|
|
|
$this->info = $info;
|
|
|
|
$this->errorNumber = $errorNumber;
|
|
|
|
$this->error = $error;
|
|
|
|
|
2018-10-30 14:58:45 +01:00
|
|
|
Logger::log($url . ': ' . $this->returnCode . " " . $result, Logger::DATA);
|
2018-10-10 21:08:43 +02:00
|
|
|
|
|
|
|
$this->parseBodyHeader($result);
|
|
|
|
$this->checkSuccess();
|
|
|
|
$this->checkRedirect();
|
|
|
|
$this->checkInfo();
|
|
|
|
}
|
|
|
|
|
|
|
|
private function parseBodyHeader($result)
|
|
|
|
{
|
|
|
|
// Pull out multiple headers, e.g. proxy and continuation headers
|
|
|
|
// allow for HTTP/2.x without fixing code
|
|
|
|
|
|
|
|
$header = '';
|
|
|
|
$base = $result;
|
2018-10-11 22:18:27 +02:00
|
|
|
while (preg_match('/^HTTP\/.+? \d+/', $base)) {
|
2018-10-10 21:08:43 +02:00
|
|
|
$chunk = substr($base, 0, strpos($base, "\r\n\r\n") + 4);
|
|
|
|
$header .= $chunk;
|
|
|
|
$base = substr($base, strlen($chunk));
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->body = substr($result, strlen($header));
|
|
|
|
$this->header = $header;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function checkSuccess()
|
2018-10-09 19:58:58 +02:00
|
|
|
{
|
2018-10-10 21:20:30 +02:00
|
|
|
$this->isSuccess = ($this->returnCode >= 200 && $this->returnCode <= 299) || $this->errorNumber == 0;
|
2018-10-10 21:08:43 +02:00
|
|
|
|
2018-11-12 05:12:36 +01:00
|
|
|
// Everything higher or equal 400 is not a success
|
|
|
|
if ($this->returnCode >= 400) {
|
2018-11-11 22:54:50 +01:00
|
|
|
$this->isSuccess = false;
|
|
|
|
}
|
|
|
|
|
2018-10-10 21:08:43 +02:00
|
|
|
if (!$this->isSuccess) {
|
2018-10-30 14:58:45 +01:00
|
|
|
Logger::log('error: ' . $this->url . ': ' . $this->returnCode . ' - ' . $this->error, Logger::INFO);
|
|
|
|
Logger::log('debug: ' . print_r($this->info, true), Logger::DATA);
|
2018-10-10 21:08:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!$this->isSuccess && $this->errorNumber == CURLE_OPERATION_TIMEDOUT) {
|
|
|
|
$this->isTimeout = true;
|
|
|
|
} else {
|
|
|
|
$this->isTimeout = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function checkRedirect()
|
|
|
|
{
|
2018-10-10 21:20:30 +02:00
|
|
|
if (!array_key_exists('url', $this->info)) {
|
2018-10-10 21:08:43 +02:00
|
|
|
$this->redirectUrl = '';
|
|
|
|
} else {
|
|
|
|
$this->redirectUrl = $this->info['url'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->returnCode == 301 || $this->returnCode == 302 || $this->returnCode == 303 || $this->returnCode== 307) {
|
2018-11-24 10:01:10 +01:00
|
|
|
$redirect_parts = parse_url(defaults($this->info, 'redirect_url', ''));
|
2018-12-05 07:01:58 +01:00
|
|
|
if (empty($redirect_parts)) {
|
|
|
|
$redirect_parts = [];
|
|
|
|
}
|
|
|
|
|
2018-11-24 01:27:00 +01:00
|
|
|
if (preg_match('/(Location:|URI:)(.*?)\n/i', $this->header, $matches)) {
|
2018-12-05 07:01:58 +01:00
|
|
|
$redirect_parts2 = parse_url(trim(array_pop($matches)));
|
|
|
|
if (!empty($redirect_parts2)) {
|
|
|
|
$redirect_parts = array_merge($redirect_parts, $redirect_parts2);
|
|
|
|
}
|
2018-10-10 21:08:43 +02:00
|
|
|
}
|
|
|
|
|
2018-11-24 10:01:10 +01:00
|
|
|
$parts = parse_url(defaults($this->info, 'url', ''));
|
2018-12-05 07:01:58 +01:00
|
|
|
if (empty($parts)) {
|
|
|
|
$parts = [];
|
|
|
|
}
|
2018-10-10 21:08:43 +02:00
|
|
|
|
2018-11-24 10:01:10 +01:00
|
|
|
/// @todo Checking the corresponding RFC which parts of a redirect can be ommitted.
|
|
|
|
$components = ['scheme', 'host', 'path', 'query', 'fragment'];
|
|
|
|
foreach ($components as $component) {
|
|
|
|
if (empty($redirect_parts[$component]) && !empty($parts[$component])) {
|
|
|
|
$redirect_parts[$component] = $parts[$component];
|
|
|
|
}
|
2018-10-10 21:08:43 +02:00
|
|
|
}
|
|
|
|
|
2018-11-24 01:27:00 +01:00
|
|
|
$this->redirectUrl = Network::unparseURL($redirect_parts);
|
|
|
|
|
2018-12-04 08:12:55 +01:00
|
|
|
$this->isRedirectUrl = true;
|
2018-10-10 21:08:43 +02:00
|
|
|
} else {
|
|
|
|
$this->isRedirectUrl = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function checkInfo()
|
|
|
|
{
|
|
|
|
if (isset($this->info['content_type'])) {
|
|
|
|
$this->contentType = $this->info['content_type'];
|
|
|
|
} else {
|
|
|
|
$this->contentType = '';
|
|
|
|
}
|
2018-10-09 19:58:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-10-10 21:08:43 +02:00
|
|
|
* Gets the Curl Code
|
2018-10-09 19:58:58 +02:00
|
|
|
*
|
2018-10-10 21:08:43 +02:00
|
|
|
* @return string The Curl Code
|
2018-10-09 19:58:58 +02:00
|
|
|
*/
|
2018-10-10 21:08:43 +02:00
|
|
|
public function getReturnCode()
|
2018-10-09 19:58:58 +02:00
|
|
|
{
|
2018-10-10 21:08:43 +02:00
|
|
|
return $this->returnCode;
|
2018-10-09 19:58:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the Curl Content Type
|
|
|
|
*
|
|
|
|
* @return string the Curl Content Type
|
|
|
|
*/
|
|
|
|
public function getContentType()
|
|
|
|
{
|
|
|
|
return $this->contentType;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-10-10 21:08:43 +02:00
|
|
|
* Returns the Curl headers
|
2018-10-09 19:58:58 +02:00
|
|
|
*
|
2019-10-01 18:33:11 +02:00
|
|
|
* @param string $field optional header field. Return all fields if empty
|
|
|
|
*
|
2019-10-01 21:02:26 +02:00
|
|
|
* @return string the Curl headers or the specified content of the header variable
|
2018-10-09 19:58:58 +02:00
|
|
|
*/
|
2019-10-01 20:22:33 +02:00
|
|
|
public function getHeader(string $field = '')
|
2018-10-09 19:58:58 +02:00
|
|
|
{
|
2019-10-01 18:33:11 +02:00
|
|
|
if (empty($field)) {
|
|
|
|
return $this->header;
|
|
|
|
}
|
|
|
|
|
2019-10-01 23:46:18 +02:00
|
|
|
$field = strtolower(trim($field));
|
|
|
|
|
|
|
|
$headers = self::getHeaderArray();
|
|
|
|
|
|
|
|
if (isset($headers[$field])) {
|
|
|
|
return $headers[$field];
|
2019-10-01 18:33:11 +02:00
|
|
|
}
|
2019-10-01 23:46:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if a specified header exists
|
|
|
|
*
|
|
|
|
* @param string $field header field
|
|
|
|
*
|
|
|
|
* @return boolean "true" if header exists
|
|
|
|
*/
|
|
|
|
public function headerExists(string $field)
|
|
|
|
{
|
|
|
|
$field = strtolower(trim($field));
|
|
|
|
|
|
|
|
$headers = self::getHeaderArray();
|
2019-10-01 18:33:11 +02:00
|
|
|
|
2019-10-01 23:46:18 +02:00
|
|
|
return array_key_exists($field, $headers);
|
2018-10-09 19:58:58 +02:00
|
|
|
}
|
|
|
|
|
2019-10-01 20:22:33 +02:00
|
|
|
/**
|
|
|
|
* Returns the Curl headers as an associated array
|
|
|
|
*
|
|
|
|
* @return array associated header array
|
|
|
|
*/
|
|
|
|
public function getHeaderArray()
|
|
|
|
{
|
|
|
|
$headers = [];
|
|
|
|
|
|
|
|
$lines = explode("\n", $this->header);
|
|
|
|
foreach ($lines as $line) {
|
|
|
|
$parts = explode(':', $line);
|
|
|
|
$headerfield = strtolower(trim(array_shift($parts)));
|
|
|
|
$headerdata = trim(implode(':', $parts));
|
|
|
|
|
|
|
|
if (!empty($headerdata)) {
|
|
|
|
$headers[$headerfield] = $headerdata;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $headers;
|
|
|
|
}
|
|
|
|
|
2018-10-09 19:58:58 +02:00
|
|
|
/**
|
2018-10-10 21:08:43 +02:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isSuccess()
|
|
|
|
{
|
|
|
|
return $this->isSuccess;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getUrl()
|
|
|
|
{
|
|
|
|
return $this->url;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getRedirectUrl()
|
|
|
|
{
|
|
|
|
return $this->redirectUrl;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getBody()
|
|
|
|
{
|
|
|
|
return $this->body;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getInfo()
|
|
|
|
{
|
|
|
|
return $this->info;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isRedirectUrl()
|
|
|
|
{
|
|
|
|
return $this->isRedirectUrl;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function getErrorNumber()
|
|
|
|
{
|
|
|
|
return $this->errorNumber;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getError()
|
|
|
|
{
|
|
|
|
return $this->error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return bool
|
2018-10-09 19:58:58 +02:00
|
|
|
*/
|
2018-10-10 21:08:43 +02:00
|
|
|
public function isTimeout()
|
2018-10-09 19:58:58 +02:00
|
|
|
{
|
2018-10-10 21:08:43 +02:00
|
|
|
return $this->isTimeout;
|
2018-10-09 19:58:58 +02:00
|
|
|
}
|
|
|
|
}
|