2011-01-11 18:06:12 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Humble HTTP Agent
|
|
|
|
*
|
|
|
|
* This class is designed to take advantage of parallel HTTP requests
|
2011-11-04 17:10:31 +00:00
|
|
|
* offered by PHP's PECL HTTP extension or the curl_multi_* functions.
|
|
|
|
* For environments which do not have these options, it reverts to standard sequential
|
2011-01-11 18:06:12 +00:00
|
|
|
* requests (using file_get_contents())
|
|
|
|
*
|
2019-04-04 21:23:27 +00:00
|
|
|
* @version 1.7
|
|
|
|
* @date 2016-11-28
|
2017-02-18 15:06:19 +00:00
|
|
|
* @see http://devel-m6w6.rhcloud.com/mdref/http
|
2011-01-11 18:06:12 +00:00
|
|
|
* @author Keyvan Minoukadeh
|
2019-04-04 21:23:27 +00:00
|
|
|
* @copyright 2011-2016 Keyvan Minoukadeh
|
2011-01-11 18:06:12 +00:00
|
|
|
* @license http://www.gnu.org/licenses/agpl-3.0.html AGPL v3
|
|
|
|
*/
|
|
|
|
|
|
|
|
class HumbleHttpAgent
|
|
|
|
{
|
2011-11-04 17:10:31 +00:00
|
|
|
const METHOD_REQUEST_POOL = 1;
|
|
|
|
const METHOD_CURL_MULTI = 2;
|
|
|
|
const METHOD_FILE_GET_CONTENTS = 4;
|
2013-04-18 14:11:06 +00:00
|
|
|
//const UA_BROWSER = 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:2.0.1) Gecko/20100101 Firefox/4.0.1';
|
2019-04-04 21:23:27 +00:00
|
|
|
const UA_BROWSER = 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36';
|
|
|
|
const UA_PHP = 'PHP/5.6';
|
2013-04-18 14:11:06 +00:00
|
|
|
const REF_GOOGLE = 'http://www.google.co.uk/url?sa=t&source=web&cd=1';
|
2011-11-04 17:10:31 +00:00
|
|
|
|
2011-01-11 18:06:12 +00:00
|
|
|
protected $requests = array();
|
2011-11-04 17:10:31 +00:00
|
|
|
protected $redirectQueue = array();
|
2011-01-11 18:06:12 +00:00
|
|
|
protected $requestOptions;
|
|
|
|
protected $maxParallelRequests = 5;
|
2011-11-04 17:10:31 +00:00
|
|
|
protected $cache = null; //TODO
|
2011-01-11 18:06:12 +00:00
|
|
|
protected $httpContext;
|
2014-09-15 20:24:06 +00:00
|
|
|
protected $curlOptions;
|
2011-11-04 17:10:31 +00:00
|
|
|
protected $minimiseMemoryUse = false; //TODO
|
|
|
|
protected $method;
|
2015-06-14 00:28:37 +00:00
|
|
|
protected $cookieJar = array();
|
2014-05-15 20:49:16 +00:00
|
|
|
public $debug = false;
|
|
|
|
public $debugVerbose = false;
|
2011-11-04 17:10:31 +00:00
|
|
|
public $rewriteHashbangFragment = true; // see http://code.google.com/web/ajaxcrawling/docs/specification.html
|
2017-02-18 15:06:19 +00:00
|
|
|
public $siteConfigBuilder = null; // can be set to an instance of ContentExtractor to have site config files used for custom HTTP headers
|
2011-11-04 17:10:31 +00:00
|
|
|
public $maxRedirects = 5;
|
2013-04-18 14:11:06 +00:00
|
|
|
public $userAgentMap = array();
|
|
|
|
public $rewriteUrls = array();
|
|
|
|
public $userAgentDefault;
|
|
|
|
public $referer;
|
|
|
|
//public $userAgent = 'Mozilla/5.0';
|
|
|
|
|
|
|
|
// Prevent certain file/mime types
|
|
|
|
// HTTP responses which match these content types will
|
|
|
|
// be returned without body.
|
|
|
|
public $headerOnlyTypes = array();
|
|
|
|
// URLs ending with one of these extensions will
|
|
|
|
// prompt Humble HTTP Agent to send a HEAD request first
|
|
|
|
// to see if returned content type matches $headerOnlyTypes.
|
2014-05-15 20:49:16 +00:00
|
|
|
public $headerOnlyClues = array('pdf','mp3','zip','exe','gif','gzip','gz','jpeg','jpg','mpg','mpeg','png','ppt','mov');
|
|
|
|
// AJAX triggers to search for.
|
|
|
|
// for AJAX sites, e.g. Blogger with its dynamic views templates.
|
|
|
|
public $ajaxTriggers = array("<meta name='fragment' content='!'",'<meta name="fragment" content="!"',"<meta content='!' name='fragment'",'<meta content="!" name="fragment"');
|
2011-01-11 18:06:12 +00:00
|
|
|
|
|
|
|
//TODO: set max file size
|
|
|
|
//TODO: normalise headers
|
|
|
|
|
2011-11-04 17:10:31 +00:00
|
|
|
function __construct($requestOptions=null, $method=null) {
|
2013-04-18 14:11:06 +00:00
|
|
|
$this->userAgentDefault = self::UA_BROWSER;
|
|
|
|
$this->referer = self::REF_GOOGLE;
|
2011-11-04 17:10:31 +00:00
|
|
|
// set the request method
|
|
|
|
if (in_array($method, array(1,2,4))) {
|
|
|
|
$this->method = $method;
|
|
|
|
} else {
|
2017-02-18 15:06:19 +00:00
|
|
|
if (class_exists('http\Client\Request')) {
|
2011-11-04 17:10:31 +00:00
|
|
|
$this->method = self::METHOD_REQUEST_POOL;
|
|
|
|
} elseif (function_exists('curl_multi_init')) {
|
|
|
|
$this->method = self::METHOD_CURL_MULTI;
|
|
|
|
} else {
|
|
|
|
$this->method = self::METHOD_FILE_GET_CONTENTS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($this->method == self::METHOD_CURL_MULTI) {
|
|
|
|
require_once(dirname(__FILE__).'/RollingCurl.php');
|
|
|
|
}
|
2011-11-04 17:40:29 +00:00
|
|
|
// create cookie jar
|
2015-06-14 00:28:37 +00:00
|
|
|
// $this->cookieJar = new CookieJar();
|
2011-11-04 17:40:29 +00:00
|
|
|
// set request options (redirect must be 0)
|
2014-09-15 20:24:06 +00:00
|
|
|
// HTTP PECL (http://php.net/manual/en/http.request.options.php)
|
2011-01-11 18:06:12 +00:00
|
|
|
$this->requestOptions = array(
|
2013-04-18 14:11:06 +00:00
|
|
|
'timeout' => 15,
|
2014-05-15 21:03:31 +00:00
|
|
|
'connecttimeout' => 15,
|
|
|
|
'dns_cache_timeout' => 300,
|
2011-11-04 17:10:31 +00:00
|
|
|
'redirect' => 0 // we handle redirects manually so we can rewrite the new hashbang URLs that are creeping up over the web
|
2011-01-11 18:06:12 +00:00
|
|
|
// TODO: test onprogress?
|
|
|
|
);
|
|
|
|
if (is_array($requestOptions)) {
|
|
|
|
$this->requestOptions = array_merge($this->requestOptions, $requestOptions);
|
|
|
|
}
|
2014-09-15 20:24:06 +00:00
|
|
|
// HTTP file_get_contents
|
2011-11-04 17:40:29 +00:00
|
|
|
$this->httpContext = array(
|
2011-01-11 18:06:12 +00:00
|
|
|
'http' => array(
|
2011-11-04 17:10:31 +00:00
|
|
|
'ignore_errors' => true,
|
2011-01-11 18:06:12 +00:00
|
|
|
'timeout' => $this->requestOptions['timeout'],
|
2011-03-23 22:39:01 +00:00
|
|
|
'max_redirects' => $this->requestOptions['redirect'],
|
2013-04-18 14:11:06 +00:00
|
|
|
'header' => "Accept: */*\r\n"
|
2011-01-11 18:06:12 +00:00
|
|
|
)
|
2011-11-04 17:40:29 +00:00
|
|
|
);
|
2014-09-15 20:24:06 +00:00
|
|
|
// HTTP cURL
|
2019-04-04 21:23:27 +00:00
|
|
|
if ($this->method === self::METHOD_CURL_MULTI) {
|
|
|
|
$this->curlOptions = array(
|
|
|
|
CURLOPT_CONNECTTIMEOUT => $this->requestOptions['timeout'],
|
|
|
|
CURLOPT_TIMEOUT => $this->requestOptions['timeout']
|
2014-09-15 20:24:06 +00:00
|
|
|
);
|
2019-04-04 21:23:27 +00:00
|
|
|
}
|
2014-09-15 20:24:06 +00:00
|
|
|
// Use proxy?
|
2019-04-04 21:23:27 +00:00
|
|
|
if (isset($this->requestOptions['proxyhost']) && $this->requestOptions['proxyhost']) {
|
2014-09-15 20:24:06 +00:00
|
|
|
// For file_get_contents (see http://stackoverflow.com/a/1336419/407938)
|
|
|
|
$this->httpContext['http']['proxy'] = 'tcp://'.$this->requestOptions['proxyhost'];
|
|
|
|
$this->httpContext['http']['request_fulluri'] = true;
|
|
|
|
// For cURL (see http://stackoverflow.com/a/9247672/407938)
|
2019-04-04 21:23:27 +00:00
|
|
|
if ($this->method === self::METHOD_CURL_MULTI) {
|
|
|
|
$this->curlOptions[CURLOPT_PROXY] = $this->requestOptions['proxyhost'];
|
|
|
|
}
|
2014-09-15 20:24:06 +00:00
|
|
|
if (isset($this->requestOptions['proxyauth'])) {
|
|
|
|
$this->httpContext['http']['header'] .= "Proxy-Authorization: Basic ".base64_encode($this->requestOptions['proxyauth'])."\r\n";
|
2019-04-04 21:23:27 +00:00
|
|
|
if ($this->method === self::METHOD_CURL_MULTI) {
|
|
|
|
$this->curlOptions[CURLOPT_PROXYUSERPWD] = $this->requestOptions['proxyauth'];
|
|
|
|
}
|
2014-09-15 20:24:06 +00:00
|
|
|
}
|
|
|
|
}
|
2011-01-11 18:06:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function debug($msg) {
|
|
|
|
if ($this->debug) {
|
|
|
|
$mem = round(memory_get_usage()/1024, 2);
|
|
|
|
$memPeak = round(memory_get_peak_usage()/1024, 2);
|
|
|
|
echo '* ',$msg;
|
2014-05-15 20:49:16 +00:00
|
|
|
if ($this->debugVerbose) echo ' - mem used: ',$mem," (peak: $memPeak)";
|
|
|
|
echo "\n";
|
2011-01-11 18:06:12 +00:00
|
|
|
ob_flush();
|
|
|
|
flush();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-18 14:11:06 +00:00
|
|
|
protected function getUserAgent($url, $asArray=false) {
|
|
|
|
$host = @parse_url($url, PHP_URL_HOST);
|
|
|
|
if (strtolower(substr($host, 0, 4)) == 'www.') {
|
|
|
|
$host = substr($host, 4);
|
|
|
|
}
|
|
|
|
if ($host) {
|
|
|
|
$try = array($host);
|
|
|
|
$split = explode('.', $host);
|
|
|
|
if (count($split) > 1) {
|
|
|
|
array_shift($split);
|
|
|
|
$try[] = '.'.implode('.', $split);
|
|
|
|
}
|
|
|
|
foreach ($try as $h) {
|
|
|
|
if (isset($this->userAgentMap[$h])) {
|
|
|
|
$ua = $this->userAgentMap[$h];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!isset($ua)) $ua = $this->userAgentDefault;
|
|
|
|
if ($asArray) {
|
|
|
|
return array('User-Agent' => $ua);
|
|
|
|
} else {
|
|
|
|
return 'User-Agent: '.$ua;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-04 17:10:31 +00:00
|
|
|
public function rewriteHashbangFragment($url) {
|
|
|
|
// return $url if there's no '#!'
|
|
|
|
if (strpos($url, '#!') === false) return $url;
|
|
|
|
// split $url and rewrite
|
2013-04-18 14:11:06 +00:00
|
|
|
// TODO: is SimplePie_IRI included?
|
|
|
|
$iri = new SimplePie_IRI($url);
|
|
|
|
$fragment = substr($iri->fragment, 1); // strip '!'
|
2011-11-04 17:10:31 +00:00
|
|
|
$iri->fragment = null;
|
2013-04-18 14:11:06 +00:00
|
|
|
if (isset($iri->query)) {
|
|
|
|
parse_str($iri->query, $query);
|
2011-11-04 17:10:31 +00:00
|
|
|
} else {
|
|
|
|
$query = array();
|
|
|
|
}
|
|
|
|
$query['_escaped_fragment_'] = (string)$fragment;
|
|
|
|
$iri->query = str_replace('%2F', '/', http_build_query($query)); // needed for some sites
|
2013-04-18 14:11:06 +00:00
|
|
|
return $iri->get_iri();
|
2011-11-04 17:10:31 +00:00
|
|
|
}
|
|
|
|
|
2014-05-15 21:03:31 +00:00
|
|
|
public function getRedirectURLfromHTML($url, $html) {
|
|
|
|
$redirect_url = $this->getMetaRefreshURL($url, $html);
|
|
|
|
if (!$redirect_url) {
|
|
|
|
$redirect_url = $this->getUglyURL($url, $html);
|
|
|
|
}
|
|
|
|
return $redirect_url;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getMetaRefreshURL($url, $html) {
|
|
|
|
if ($html == '') return false;
|
|
|
|
// <meta HTTP-EQUIV="REFRESH" content="0; url=http://www.bernama.com/bernama/v6/newsindex.php?id=943513">
|
2014-09-15 20:24:06 +00:00
|
|
|
if (!preg_match('!<meta http-equiv=["\']?refresh["\']? content=["\']?[0-9];\s*url=["\']?([^"\'>]+)["\']?!i', $html, $match)) {
|
2014-05-15 21:03:31 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$redirect_url = $match[1];
|
2017-02-18 15:06:19 +00:00
|
|
|
$redirect_url = htmlspecialchars_decode($redirect_url); // For Facebook!
|
2014-05-15 21:03:31 +00:00
|
|
|
if (preg_match('!^https?://!i', $redirect_url)) {
|
|
|
|
// already absolute
|
|
|
|
$this->debug('Meta refresh redirect found (http-equiv="refresh"), new URL: '.$redirect_url);
|
|
|
|
return $redirect_url;
|
|
|
|
}
|
|
|
|
// absolutize redirect URL
|
|
|
|
$base = new SimplePie_IRI($url);
|
|
|
|
// remove '//' in URL path (causes URLs not to resolve properly)
|
|
|
|
if (isset($base->path)) $base->path = preg_replace('!//+!', '/', $base->path);
|
|
|
|
if ($absolute = SimplePie_IRI::absolutize($base, $redirect_url)) {
|
|
|
|
$this->debug('Meta refresh redirect found (http-equiv="refresh"), new URL: '.$absolute);
|
2017-02-18 15:06:19 +00:00
|
|
|
return $absolute->get_iri();
|
2014-05-15 21:03:31 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-05-15 20:49:16 +00:00
|
|
|
public function getUglyURL($url, $html) {
|
|
|
|
if ($html == '') return false;
|
|
|
|
$found = false;
|
|
|
|
foreach ($this->ajaxTriggers as $string) {
|
|
|
|
if (stripos($html, $string)) {
|
|
|
|
$found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!$found) return false;
|
|
|
|
$iri = new SimplePie_IRI($url);
|
|
|
|
if (isset($iri->query)) {
|
|
|
|
parse_str($iri->query, $query);
|
|
|
|
} else {
|
|
|
|
$query = array();
|
|
|
|
}
|
|
|
|
$query['_escaped_fragment_'] = '';
|
|
|
|
$iri->query = str_replace('%2F', '/', http_build_query($query)); // needed for some sites
|
2014-05-15 21:03:31 +00:00
|
|
|
$ugly_url = $iri->get_iri();
|
|
|
|
$this->debug('AJAX trigger (meta name="fragment" content="!") found, new URL: '.$ugly_url);
|
|
|
|
return $ugly_url;
|
2014-05-15 20:49:16 +00:00
|
|
|
}
|
|
|
|
|
2012-04-30 22:51:43 +00:00
|
|
|
public function removeFragment($url) {
|
|
|
|
$pos = strpos($url, '#');
|
|
|
|
if ($pos === false) {
|
|
|
|
return $url;
|
|
|
|
} else {
|
|
|
|
return substr($url, 0, $pos);
|
|
|
|
}
|
2013-04-18 14:11:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function rewriteUrls($url) {
|
|
|
|
foreach ($this->rewriteUrls as $find => $action) {
|
|
|
|
if (strpos($url, $find) !== false) {
|
|
|
|
if (is_array($action)) {
|
|
|
|
return strtr($url, $action);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $url;
|
|
|
|
}
|
2012-04-30 22:51:43 +00:00
|
|
|
|
2011-01-11 18:06:12 +00:00
|
|
|
public function enableDebug($bool=true) {
|
|
|
|
$this->debug = (bool)$bool;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function minimiseMemoryUse($bool = true) {
|
|
|
|
$this->minimiseMemoryUse = $bool;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setMaxParallelRequests($max) {
|
|
|
|
$this->maxParallelRequests = $max;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function validateUrl($url) {
|
2011-11-04 17:10:31 +00:00
|
|
|
$url = filter_var($url, FILTER_SANITIZE_URL);
|
2011-03-23 22:39:01 +00:00
|
|
|
$test = filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_SCHEME_REQUIRED);
|
|
|
|
// deal with bug http://bugs.php.net/51192 (present in PHP 5.2.13 and PHP 5.3.2)
|
|
|
|
if ($test === false) {
|
|
|
|
$test = filter_var(strtr($url, '-', '_'), FILTER_VALIDATE_URL, FILTER_FLAG_SCHEME_REQUIRED);
|
|
|
|
}
|
|
|
|
if ($test !== false && $test !== null && preg_match('!^https?://!', $url)) {
|
2011-11-04 17:10:31 +00:00
|
|
|
return $url;
|
2011-01-11 18:06:12 +00:00
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function fetchAll(array $urls) {
|
2011-11-04 17:10:31 +00:00
|
|
|
$this->fetchAllOnce($urls, $isRedirect=false);
|
|
|
|
$redirects = 0;
|
|
|
|
while (!empty($this->redirectQueue) && ++$redirects <= $this->maxRedirects) {
|
|
|
|
$this->debug("Following redirects #$redirects...");
|
|
|
|
$this->fetchAllOnce($this->redirectQueue, $isRedirect=true);
|
|
|
|
}
|
2015-06-14 00:28:37 +00:00
|
|
|
$this->deleteCookies();
|
2011-11-04 17:10:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// fetch all URLs without following redirects
|
|
|
|
public function fetchAllOnce(array $urls, $isRedirect=false) {
|
|
|
|
if (!$isRedirect) $urls = array_unique($urls);
|
|
|
|
if (empty($urls)) return;
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////
|
2017-02-18 15:06:19 +00:00
|
|
|
// parallel (HTTP extension)
|
2011-11-04 17:10:31 +00:00
|
|
|
if ($this->method == self::METHOD_REQUEST_POOL) {
|
2017-02-18 15:06:19 +00:00
|
|
|
$this->debug('Starting parallel fetch (HTTP Extension)');
|
2011-01-11 18:06:12 +00:00
|
|
|
try {
|
|
|
|
while (count($urls) > 0) {
|
2011-11-04 17:10:31 +00:00
|
|
|
$this->debug('Processing set of '.min($this->maxParallelRequests, count($urls)));
|
2011-01-11 18:06:12 +00:00
|
|
|
$subset = array_splice($urls, 0, $this->maxParallelRequests);
|
2017-02-18 15:06:19 +00:00
|
|
|
//$pool = new HttpRequestPool();
|
|
|
|
$pool = new http\Client;
|
|
|
|
$pool->setOptions($this->requestOptions);
|
2011-11-04 17:10:31 +00:00
|
|
|
foreach ($subset as $orig => $url) {
|
|
|
|
if (!$isRedirect) $orig = $url;
|
|
|
|
unset($this->redirectQueue[$orig]);
|
2011-01-11 18:06:12 +00:00
|
|
|
$this->debug("...$url");
|
2011-11-04 17:10:31 +00:00
|
|
|
if (!$isRedirect && isset($this->requests[$url])) {
|
2011-01-11 18:06:12 +00:00
|
|
|
$this->debug("......in memory");
|
2011-11-04 17:10:31 +00:00
|
|
|
/*
|
2011-01-11 18:06:12 +00:00
|
|
|
} elseif ($this->isCached($url)) {
|
|
|
|
$this->debug("......is cached");
|
|
|
|
if (!$this->minimiseMemoryUse) {
|
|
|
|
$this->requests[$url] = $this->getCached($url);
|
|
|
|
}
|
2011-11-04 17:10:31 +00:00
|
|
|
*/
|
2011-01-11 18:06:12 +00:00
|
|
|
} else {
|
|
|
|
$this->debug("......adding to pool");
|
2013-04-18 14:11:06 +00:00
|
|
|
$req_url = $this->rewriteUrls($url);
|
|
|
|
$req_url = ($this->rewriteHashbangFragment) ? $this->rewriteHashbangFragment($req_url) : $req_url;
|
2012-04-30 22:51:43 +00:00
|
|
|
$req_url = $this->removeFragment($req_url);
|
2013-04-18 14:11:06 +00:00
|
|
|
if (!empty($this->headerOnlyTypes) && !isset($this->requests[$orig]['wrongGuess']) && $this->possibleUnsupportedType($req_url)) {
|
2017-02-18 15:06:19 +00:00
|
|
|
$_meth = "HEAD";
|
2013-04-18 14:11:06 +00:00
|
|
|
} else {
|
2017-02-18 15:06:19 +00:00
|
|
|
$_meth = "GET";
|
2013-04-18 14:11:06 +00:00
|
|
|
unset($this->requests[$orig]['wrongGuess']);
|
|
|
|
}
|
2017-02-18 15:06:19 +00:00
|
|
|
//$httpRequest = new HttpRequest($req_url, $_meth, $this->requestOptions);
|
|
|
|
$httpRequest = new http\Client\Request($_meth, $req_url);
|
|
|
|
$httpRequest->setOptions($this->requestOptions);
|
|
|
|
|
|
|
|
// check site config for additional http headers
|
|
|
|
$scHeaders = array();
|
|
|
|
if (isset($this->siteConfigBuilder)) {
|
|
|
|
$scHeaders = $this->siteConfigBuilder->buildSiteConfig($req_url)->http_header;
|
|
|
|
}
|
|
|
|
|
2011-11-04 17:40:29 +00:00
|
|
|
// send cookies, if we have any
|
2017-02-18 15:06:19 +00:00
|
|
|
$_cookies = null;
|
|
|
|
if (isset($scHeaders['cookie'])) {
|
|
|
|
$_cookies = $scHeaders['cookie'];
|
|
|
|
} else {
|
|
|
|
//$_cookies = $this->cookieJar->getMatchingCookies($req_url);
|
|
|
|
$_cookies = $this->getCookies($orig, $req_url);
|
|
|
|
}
|
|
|
|
if ($_cookies) {
|
|
|
|
$this->debug("......sending cookies: $_cookies");
|
|
|
|
$httpRequest->addHeaders(array('Cookie' => $_cookies));
|
|
|
|
}
|
|
|
|
|
|
|
|
// send user agent
|
|
|
|
$_ua = null;
|
|
|
|
if (isset($scHeaders['user-agent'])) {
|
|
|
|
$_ua = $scHeaders['user-agent'];
|
|
|
|
} else {
|
|
|
|
$_ua = $this->getUserAgent($req_url, true);
|
|
|
|
$_ua = $_ua['User-Agent'];
|
|
|
|
}
|
|
|
|
if ($_ua) {
|
|
|
|
$this->debug("......user-agent set to: $_ua");
|
|
|
|
$httpRequest->addHeaders(array('User-Agent' => $_ua));
|
2011-11-04 17:40:29 +00:00
|
|
|
}
|
2017-02-18 15:06:19 +00:00
|
|
|
|
2013-04-18 14:11:06 +00:00
|
|
|
// add referer for picky sites
|
2017-02-18 15:06:19 +00:00
|
|
|
$_referer = null;
|
|
|
|
if (isset($scHeaders['referer'])) {
|
|
|
|
$_referer = $scHeaders['referer'];
|
|
|
|
} else {
|
|
|
|
$_referer = $this->referer;
|
|
|
|
}
|
|
|
|
if ($_referer) {
|
|
|
|
$this->debug("......referer set to: $_referer");
|
|
|
|
$httpRequest->addheaders(array('Referer'=>$_referer));
|
|
|
|
}
|
|
|
|
|
2011-11-04 17:10:31 +00:00
|
|
|
$this->requests[$orig] = array('headers'=>null, 'body'=>null, 'httpRequest'=>$httpRequest);
|
|
|
|
$this->requests[$orig]['original_url'] = $orig;
|
2017-02-18 15:06:19 +00:00
|
|
|
$pool->enqueue($httpRequest);
|
2011-01-11 18:06:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// did we get anything into the pool?
|
|
|
|
if (count($pool) > 0) {
|
|
|
|
$this->debug('Sending request...');
|
2012-04-30 22:51:43 +00:00
|
|
|
try {
|
|
|
|
$pool->send();
|
2017-02-18 15:06:19 +00:00
|
|
|
} catch (http\Exception $e) {
|
2012-04-30 22:51:43 +00:00
|
|
|
// do nothing
|
|
|
|
}
|
2011-01-11 18:06:12 +00:00
|
|
|
$this->debug('Received responses');
|
2011-11-04 17:10:31 +00:00
|
|
|
foreach($subset as $orig => $url) {
|
|
|
|
if (!$isRedirect) $orig = $url;
|
2013-04-18 14:11:06 +00:00
|
|
|
$request = $this->requests[$orig]['httpRequest'];
|
2017-02-18 15:06:19 +00:00
|
|
|
$response = $pool->getResponse($request);
|
2013-04-18 14:11:06 +00:00
|
|
|
//$this->requests[$orig]['headers'] = $this->headersToString($request->getResponseHeader());
|
|
|
|
// getResponseHeader() doesn't return status line, so, for consistency...
|
2017-02-18 15:06:19 +00:00
|
|
|
//$headers = $response->toString();
|
|
|
|
$this->requests[$orig]['headers'] = $response->getInfo()."\n".$this->headersToString($response->getHeaders(), true);
|
|
|
|
// v1 HTTP extension code
|
|
|
|
//$this->requests[$orig]['headers'] = substr($request->getRawResponseMessage(), 0, $request->getResponseInfo('header_size'));
|
2013-04-18 14:11:06 +00:00
|
|
|
// check content type
|
|
|
|
// TODO: use getResponseHeader('content-type') or getResponseInfo()
|
|
|
|
if ($this->headerOnlyType($this->requests[$orig]['headers'])) {
|
|
|
|
$this->requests[$orig]['body'] = '';
|
|
|
|
$_header_only_type = true;
|
|
|
|
$this->debug('Header only type returned');
|
|
|
|
} else {
|
2017-02-18 15:06:19 +00:00
|
|
|
$this->requests[$orig]['body'] = $response->getBody()->toString();
|
|
|
|
//var_dump($this->requests[$orig]['body']);exit;
|
|
|
|
// v1 HTTP ext. code
|
|
|
|
//$this->requests[$orig]['body'] = $request->getResponseBody();
|
2013-04-18 14:11:06 +00:00
|
|
|
$_header_only_type = false;
|
|
|
|
}
|
2017-02-18 15:06:19 +00:00
|
|
|
$this->requests[$orig]['effective_url'] = $response->getTransferInfo('effective_url');
|
|
|
|
$this->requests[$orig]['status_code'] = $status_code = $response->getResponseCode();
|
|
|
|
// v1 HTTP ext. code
|
|
|
|
//$this->requests[$orig]['effective_url'] = $request->getResponseInfo('effective_url');
|
|
|
|
//$this->requests[$orig]['status_code'] = $status_code = $request->getResponseCode();
|
2013-04-18 14:11:06 +00:00
|
|
|
// is redirect?
|
2017-02-18 15:06:19 +00:00
|
|
|
if ((in_array($status_code, array(300, 301, 302, 303, 307)) || $status_code > 307 && $status_code < 400) && $response->getHeader('location')) {
|
|
|
|
// v1 HTTP ext. code
|
|
|
|
//if ((in_array($status_code, array(300, 301, 302, 303, 307)) || $status_code > 307 && $status_code < 400) && $request->getResponseHeader('location')) {
|
|
|
|
$redirectURL = $response->getHeader('location');
|
2013-04-18 14:11:06 +00:00
|
|
|
if (!preg_match('!^https?://!i', $redirectURL)) {
|
|
|
|
$redirectURL = SimplePie_Misc::absolutize_url($redirectURL, $url);
|
2011-11-04 17:10:31 +00:00
|
|
|
}
|
2013-04-18 14:11:06 +00:00
|
|
|
if ($this->validateURL($redirectURL)) {
|
|
|
|
$this->debug('Redirect detected. Valid URL: '.$redirectURL);
|
2017-02-18 15:06:19 +00:00
|
|
|
// store any cookies
|
|
|
|
//$cookies = $request->getResponseHeader('set-cookie');
|
|
|
|
//if ($cookies && !is_array($cookies)) $cookies = array($cookies);
|
|
|
|
//if ($cookies) $this->cookieJar->storeCookies($url, $cookies);
|
2015-06-14 00:28:37 +00:00
|
|
|
$this->storeCookies($orig, $url);
|
2013-04-18 14:11:06 +00:00
|
|
|
$this->redirectQueue[$orig] = $redirectURL;
|
|
|
|
} else {
|
|
|
|
$this->debug('Redirect detected. Invalid URL: '.$redirectURL);
|
2011-01-11 18:06:12 +00:00
|
|
|
}
|
2017-02-18 15:06:19 +00:00
|
|
|
} elseif (!$_header_only_type && $request->getRequestMethod() == "HEAD") {
|
2013-04-18 14:11:06 +00:00
|
|
|
// the response content-type did not match our 'header only' types,
|
|
|
|
// but we'd issues a HEAD request because we assumed it would. So
|
|
|
|
// let's queue a proper GET request for this item...
|
|
|
|
$this->debug('Wrong guess at content-type, queing GET request');
|
|
|
|
$this->requests[$orig]['wrongGuess'] = true;
|
|
|
|
$this->redirectQueue[$orig] = $this->requests[$orig]['effective_url'];
|
2014-05-15 20:49:16 +00:00
|
|
|
} elseif (strpos($this->requests[$orig]['effective_url'], '_escaped_fragment_') === false) {
|
|
|
|
// check for <meta name='fragment' content='!'/>
|
|
|
|
// for AJAX sites, e.g. Blogger with its dynamic views templates.
|
|
|
|
// Based on Google's spec: https://developers.google.com/webmasters/ajax-crawling/docs/specification
|
|
|
|
if (isset($this->requests[$orig]['body'])) {
|
2015-06-14 00:03:20 +00:00
|
|
|
$redirectURL = $this->getRedirectURLfromHTML($this->requests[$orig]['effective_url'], substr($this->requests[$orig]['body'], 0, 150000));
|
2014-05-15 20:49:16 +00:00
|
|
|
if ($redirectURL) {
|
|
|
|
$this->redirectQueue[$orig] = $redirectURL;
|
|
|
|
}
|
|
|
|
}
|
2013-04-18 14:11:06 +00:00
|
|
|
}
|
|
|
|
//die($url.' -multi- '.$request->getResponseInfo('effective_url'));
|
2017-02-18 15:06:19 +00:00
|
|
|
$pool->dequeue($request);
|
2013-04-18 14:11:06 +00:00
|
|
|
unset($this->requests[$orig]['httpRequest'], $request);
|
|
|
|
/*
|
|
|
|
if ($this->minimiseMemoryUse) {
|
|
|
|
if ($this->cache($url)) {
|
|
|
|
unset($this->requests[$url]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
2011-01-11 18:06:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-02-18 15:06:19 +00:00
|
|
|
} catch (http\Exception $e) {
|
2011-01-11 18:06:12 +00:00
|
|
|
$this->debug($e);
|
|
|
|
return false;
|
|
|
|
}
|
2011-11-04 17:10:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////
|
|
|
|
// parallel (curl_multi_*)
|
|
|
|
elseif ($this->method == self::METHOD_CURL_MULTI) {
|
|
|
|
$this->debug('Starting parallel fetch (curl_multi_*)');
|
|
|
|
while (count($urls) > 0) {
|
|
|
|
$this->debug('Processing set of '.min($this->maxParallelRequests, count($urls)));
|
|
|
|
$subset = array_splice($urls, 0, $this->maxParallelRequests);
|
|
|
|
$pool = new RollingCurl(array($this, 'handleCurlResponse'));
|
|
|
|
$pool->window_size = count($subset);
|
|
|
|
|
|
|
|
foreach ($subset as $orig => $url) {
|
|
|
|
if (!$isRedirect) $orig = $url;
|
|
|
|
unset($this->redirectQueue[$orig]);
|
|
|
|
$this->debug("...$url");
|
|
|
|
if (!$isRedirect && isset($this->requests[$url])) {
|
|
|
|
$this->debug("......in memory");
|
|
|
|
/*
|
|
|
|
} elseif ($this->isCached($url)) {
|
|
|
|
$this->debug("......is cached");
|
|
|
|
if (!$this->minimiseMemoryUse) {
|
|
|
|
$this->requests[$url] = $this->getCached($url);
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
} else {
|
|
|
|
$this->debug("......adding to pool");
|
2013-04-18 14:11:06 +00:00
|
|
|
$req_url = $this->rewriteUrls($url);
|
|
|
|
$req_url = ($this->rewriteHashbangFragment) ? $this->rewriteHashbangFragment($req_url) : $req_url;
|
2012-04-30 22:51:43 +00:00
|
|
|
$req_url = $this->removeFragment($req_url);
|
2013-04-18 14:11:06 +00:00
|
|
|
if (!empty($this->headerOnlyTypes) && !isset($this->requests[$orig]['wrongGuess']) && $this->possibleUnsupportedType($req_url)) {
|
|
|
|
$_meth = 'HEAD';
|
|
|
|
} else {
|
|
|
|
$_meth = 'GET';
|
|
|
|
unset($this->requests[$orig]['wrongGuess']);
|
2017-02-18 15:06:19 +00:00
|
|
|
}
|
2011-11-04 17:40:29 +00:00
|
|
|
$headers = array();
|
2017-02-18 15:06:19 +00:00
|
|
|
|
|
|
|
// check site config for additional http headers
|
|
|
|
$scHeaders = array();
|
|
|
|
if (isset($this->siteConfigBuilder)) {
|
|
|
|
$scHeaders = $this->siteConfigBuilder->buildSiteConfig($req_url)->http_header;
|
|
|
|
}
|
|
|
|
|
2011-11-04 17:40:29 +00:00
|
|
|
// send cookies, if we have any
|
2017-02-18 15:06:19 +00:00
|
|
|
$_cookies = null;
|
|
|
|
if (isset($scHeaders['cookie'])) {
|
|
|
|
$_cookies = $scHeaders['cookie'];
|
|
|
|
} else {
|
|
|
|
//$_cookies = $this->cookieJar->getMatchingCookies($req_url);
|
|
|
|
$_cookies = $this->getCookies($orig, $req_url);
|
2011-11-04 17:40:29 +00:00
|
|
|
}
|
2017-02-18 15:06:19 +00:00
|
|
|
if ($_cookies) {
|
|
|
|
$this->debug("......sending cookies: $_cookies");
|
|
|
|
$headers[] = 'Cookie: '.$_cookies;
|
|
|
|
}
|
|
|
|
|
|
|
|
// send user agent
|
|
|
|
$_ua = null;
|
|
|
|
if (isset($scHeaders['user-agent'])) {
|
|
|
|
$_ua = $scHeaders['user-agent'];
|
|
|
|
} else {
|
|
|
|
$_ua = $this->getUserAgent($req_url, true);
|
|
|
|
$_ua = $_ua['User-Agent'];
|
|
|
|
}
|
|
|
|
if ($_ua) {
|
|
|
|
$this->debug("......user-agent set to: $_ua");
|
|
|
|
$headers[] = 'User-Agent: '.$_ua;
|
|
|
|
}
|
|
|
|
|
|
|
|
// add referer for picky sites
|
|
|
|
$_referer = null;
|
|
|
|
if (isset($scHeaders['referer'])) {
|
|
|
|
$_referer = $scHeaders['referer'];
|
|
|
|
} else {
|
|
|
|
$_referer = $this->referer;
|
|
|
|
}
|
|
|
|
if ($_referer) {
|
|
|
|
$this->debug("......referer set to: $_referer");
|
|
|
|
$headers[] = 'Referer: '.$_referer;
|
|
|
|
}
|
|
|
|
|
2014-09-15 20:24:06 +00:00
|
|
|
$httpRequest = new RollingCurlRequest($req_url, $_meth, null, $headers, $this->curlOptions);
|
2011-11-04 17:10:31 +00:00
|
|
|
$httpRequest->set_original_url($orig);
|
|
|
|
$this->requests[$orig] = array('headers'=>null, 'body'=>null, 'httpRequest'=>$httpRequest);
|
|
|
|
$this->requests[$orig]['original_url'] = $orig; // TODO: is this needed anymore?
|
|
|
|
$pool->add($httpRequest);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// did we get anything into the pool?
|
|
|
|
if (count($pool) > 0) {
|
|
|
|
$this->debug('Sending request...');
|
|
|
|
$pool->execute(); // this will call handleCurlResponse() and populate $this->requests[$orig]
|
|
|
|
$this->debug('Received responses');
|
|
|
|
foreach($subset as $orig => $url) {
|
|
|
|
if (!$isRedirect) $orig = $url;
|
|
|
|
// $this->requests[$orig]['headers']
|
|
|
|
// $this->requests[$orig]['body']
|
|
|
|
// $this->requests[$orig]['effective_url']
|
2013-04-18 14:11:06 +00:00
|
|
|
// check content type
|
|
|
|
if ($this->headerOnlyType($this->requests[$orig]['headers'])) {
|
|
|
|
$this->requests[$orig]['body'] = '';
|
|
|
|
$_header_only_type = true;
|
|
|
|
$this->debug('Header only type returned');
|
|
|
|
} else {
|
|
|
|
$_header_only_type = false;
|
|
|
|
}
|
2011-11-04 17:10:31 +00:00
|
|
|
$status_code = $this->requests[$orig]['status_code'];
|
|
|
|
if ((in_array($status_code, array(300, 301, 302, 303, 307)) || $status_code > 307 && $status_code < 400) && isset($this->requests[$orig]['location'])) {
|
|
|
|
$redirectURL = $this->requests[$orig]['location'];
|
2012-04-30 22:51:43 +00:00
|
|
|
if (!preg_match('!^https?://!i', $redirectURL)) {
|
|
|
|
$redirectURL = SimplePie_Misc::absolutize_url($redirectURL, $url);
|
|
|
|
}
|
2011-11-04 17:10:31 +00:00
|
|
|
if ($this->validateURL($redirectURL)) {
|
|
|
|
$this->debug('Redirect detected. Valid URL: '.$redirectURL);
|
2017-02-18 15:06:19 +00:00
|
|
|
// store any cookies
|
|
|
|
//$cookies = $this->cookieJar->extractCookies($this->requests[$orig]['headers']);
|
|
|
|
//if (!empty($cookies)) $this->cookieJar->storeCookies($url, $cookies);
|
|
|
|
$this->storeCookies($orig, $url);
|
2011-11-04 17:40:29 +00:00
|
|
|
$this->redirectQueue[$orig] = $redirectURL;
|
2011-11-04 17:10:31 +00:00
|
|
|
} else {
|
|
|
|
$this->debug('Redirect detected. Invalid URL: '.$redirectURL);
|
|
|
|
}
|
2013-04-18 14:11:06 +00:00
|
|
|
} elseif (!$_header_only_type && $this->requests[$orig]['method'] == 'HEAD') {
|
2014-05-15 20:49:16 +00:00
|
|
|
// the response content-type did not match our 'header only' types,
|
|
|
|
// but we'd issues a HEAD request because we assumed it would. So
|
|
|
|
// let's queue a proper GET request for this item...
|
|
|
|
$this->debug('Wrong guess at content-type, queing GET request');
|
|
|
|
$this->requests[$orig]['wrongGuess'] = true;
|
|
|
|
$this->redirectQueue[$orig] = $this->requests[$orig]['effective_url'];
|
|
|
|
} elseif (strpos($this->requests[$orig]['effective_url'], '_escaped_fragment_') === false) {
|
|
|
|
// check for <meta name='fragment' content='!'/>
|
|
|
|
// for AJAX sites, e.g. Blogger with its dynamic views templates.
|
|
|
|
// Based on Google's spec: https://developers.google.com/webmasters/ajax-crawling/docs/specification
|
|
|
|
if (isset($this->requests[$orig]['body'])) {
|
2015-06-14 00:03:20 +00:00
|
|
|
$redirectURL = $this->getRedirectURLfromHTML($this->requests[$orig]['effective_url'], substr($this->requests[$orig]['body'], 0, 150000));
|
2014-05-15 20:49:16 +00:00
|
|
|
if ($redirectURL) {
|
|
|
|
$this->redirectQueue[$orig] = $redirectURL;
|
|
|
|
}
|
|
|
|
}
|
2011-11-04 17:10:31 +00:00
|
|
|
}
|
|
|
|
// die($url.' -multi- '.$request->getResponseInfo('effective_url'));
|
2013-04-18 14:11:06 +00:00
|
|
|
unset($this->requests[$orig]['httpRequest'], $this->requests[$orig]['method']);
|
2011-11-04 17:10:31 +00:00
|
|
|
}
|
|
|
|
}
|
2011-01-11 18:06:12 +00:00
|
|
|
}
|
|
|
|
}
|
2011-11-04 17:10:31 +00:00
|
|
|
|
|
|
|
//////////////////////////////////////////////////////
|
|
|
|
// sequential (file_get_contents)
|
|
|
|
else {
|
|
|
|
$this->debug('Starting sequential fetch (file_get_contents)');
|
|
|
|
$this->debug('Processing set of '.count($urls));
|
|
|
|
foreach ($urls as $orig => $url) {
|
|
|
|
if (!$isRedirect) $orig = $url;
|
|
|
|
unset($this->redirectQueue[$orig]);
|
|
|
|
$this->debug("...$url");
|
|
|
|
if (!$isRedirect && isset($this->requests[$url])) {
|
|
|
|
$this->debug("......in memory");
|
|
|
|
/*
|
|
|
|
} elseif ($this->isCached($url)) {
|
|
|
|
$this->debug("......is cached");
|
|
|
|
if (!$this->minimiseMemoryUse) {
|
|
|
|
$this->requests[$url] = $this->getCached($url);
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
} else {
|
|
|
|
$this->debug("Sending request for $url");
|
2013-04-18 14:11:06 +00:00
|
|
|
$this->requests[$orig]['original_url'] = $orig;
|
|
|
|
$req_url = $this->rewriteUrls($url);
|
|
|
|
$req_url = ($this->rewriteHashbangFragment) ? $this->rewriteHashbangFragment($req_url) : $req_url;
|
2012-04-30 22:51:43 +00:00
|
|
|
$req_url = $this->removeFragment($req_url);
|
2011-11-04 17:40:29 +00:00
|
|
|
$httpContext = $this->httpContext;
|
2017-02-18 15:06:19 +00:00
|
|
|
|
|
|
|
// check site config for additional http headers
|
|
|
|
$scHeaders = array();
|
|
|
|
if (isset($this->siteConfigBuilder)) {
|
|
|
|
$scHeaders = $this->siteConfigBuilder->buildSiteConfig($req_url)->http_header;
|
|
|
|
}
|
|
|
|
|
|
|
|
// send cookies, if we have any
|
|
|
|
$_cookies = null;
|
|
|
|
if (isset($scHeaders['cookie'])) {
|
|
|
|
$_cookies = $scHeaders['cookie'];
|
|
|
|
} else {
|
|
|
|
//$_cookies = $this->cookieJar->getMatchingCookies($req_url);
|
|
|
|
$_cookies = $this->getCookies($orig, $req_url);
|
|
|
|
}
|
|
|
|
if ($_cookies) {
|
|
|
|
$this->debug("......sending cookies: $_cookies");
|
|
|
|
$httpContext['http']['header'] .= 'Cookie: '.$_cookies."\r\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
// send user agent
|
|
|
|
$_ua = null;
|
|
|
|
if (isset($scHeaders['user-agent'])) {
|
|
|
|
$_ua = $scHeaders['user-agent'];
|
|
|
|
} else {
|
|
|
|
$_ua = $this->getUserAgent($req_url, true);
|
|
|
|
$_ua = $_ua['User-Agent'];
|
|
|
|
}
|
|
|
|
if ($_ua) {
|
|
|
|
$this->debug("......user-agent set to: $_ua");
|
|
|
|
$httpContext['http']['header'] .= 'User-Agent: '.$_ua."\r\n";
|
|
|
|
}
|
|
|
|
|
2013-04-18 14:11:06 +00:00
|
|
|
// add referer for picky sites
|
2017-02-18 15:06:19 +00:00
|
|
|
$_referer = null;
|
|
|
|
if (isset($scHeaders['referer'])) {
|
|
|
|
$_referer = $scHeaders['referer'];
|
|
|
|
} else {
|
|
|
|
$_referer = $this->referer;
|
2011-11-04 17:40:29 +00:00
|
|
|
}
|
2017-02-18 15:06:19 +00:00
|
|
|
if ($_referer) {
|
|
|
|
$this->debug("......referer set to: $_referer");
|
|
|
|
$httpContext['http']['header'] .= 'Referer: '.$_referer."\r\n";
|
|
|
|
}
|
|
|
|
|
2011-11-04 17:40:29 +00:00
|
|
|
if (false !== ($html = @file_get_contents($req_url, false, stream_context_create($httpContext)))) {
|
2011-11-04 17:10:31 +00:00
|
|
|
$this->debug('Received response');
|
|
|
|
// get status code
|
|
|
|
if (!isset($http_response_header[0]) || !preg_match('!^HTTP/\d+\.\d+\s+(\d+)!', trim($http_response_header[0]), $match)) {
|
|
|
|
$this->debug('Error: no status code found');
|
|
|
|
// TODO: handle error - no status code
|
|
|
|
} else {
|
|
|
|
$this->requests[$orig]['headers'] = $this->headersToString($http_response_header, false);
|
2013-04-18 14:11:06 +00:00
|
|
|
// check content type
|
|
|
|
if ($this->headerOnlyType($this->requests[$orig]['headers'])) {
|
|
|
|
$this->requests[$orig]['body'] = '';
|
|
|
|
} else {
|
|
|
|
$this->requests[$orig]['body'] = $html;
|
|
|
|
}
|
2011-11-04 17:10:31 +00:00
|
|
|
$this->requests[$orig]['effective_url'] = $req_url;
|
|
|
|
$this->requests[$orig]['status_code'] = $status_code = (int)$match[1];
|
|
|
|
unset($match);
|
|
|
|
// handle redirect
|
2014-05-15 20:49:16 +00:00
|
|
|
if (preg_match('/^Location:(.*?)$/mi', $this->requests[$orig]['headers'], $match)) {
|
2011-11-04 17:10:31 +00:00
|
|
|
$this->requests[$orig]['location'] = trim($match[1]);
|
|
|
|
}
|
|
|
|
if ((in_array($status_code, array(300, 301, 302, 303, 307)) || $status_code > 307 && $status_code < 400) && isset($this->requests[$orig]['location'])) {
|
|
|
|
$redirectURL = $this->requests[$orig]['location'];
|
2012-04-30 22:51:43 +00:00
|
|
|
if (!preg_match('!^https?://!i', $redirectURL)) {
|
|
|
|
$redirectURL = SimplePie_Misc::absolutize_url($redirectURL, $url);
|
|
|
|
}
|
2011-11-04 17:10:31 +00:00
|
|
|
if ($this->validateURL($redirectURL)) {
|
|
|
|
$this->debug('Redirect detected. Valid URL: '.$redirectURL);
|
2017-02-18 15:06:19 +00:00
|
|
|
// store any cookies
|
|
|
|
//$cookies = $this->cookieJar->extractCookies($this->requests[$orig]['headers']);
|
|
|
|
//if (!empty($cookies)) $this->cookieJar->storeCookies($url, $cookies);
|
2015-06-14 00:28:37 +00:00
|
|
|
$this->storeCookies($orig, $url);
|
2011-11-04 17:40:29 +00:00
|
|
|
$this->redirectQueue[$orig] = $redirectURL;
|
2011-11-04 17:10:31 +00:00
|
|
|
} else {
|
|
|
|
$this->debug('Redirect detected. Invalid URL: '.$redirectURL);
|
|
|
|
}
|
2014-05-15 20:49:16 +00:00
|
|
|
} elseif (strpos($this->requests[$orig]['effective_url'], '_escaped_fragment_') === false) {
|
|
|
|
// check for <meta name='fragment' content='!'/>
|
|
|
|
// for AJAX sites, e.g. Blogger with its dynamic views templates.
|
|
|
|
// Based on Google's spec: https://developers.google.com/webmasters/ajax-crawling/docs/specification
|
|
|
|
if (isset($this->requests[$orig]['body'])) {
|
2015-06-14 00:03:20 +00:00
|
|
|
$redirectURL = $this->getRedirectURLfromHTML($this->requests[$orig]['effective_url'], substr($this->requests[$orig]['body'], 0, 150000));
|
2014-05-15 20:49:16 +00:00
|
|
|
if ($redirectURL) {
|
|
|
|
$this->redirectQueue[$orig] = $redirectURL;
|
|
|
|
}
|
|
|
|
}
|
2011-11-04 17:10:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$this->debug('Error retrieving URL');
|
|
|
|
//print_r($req_url);
|
|
|
|
//print_r($http_response_header);
|
|
|
|
//print_r($html);
|
|
|
|
|
|
|
|
// TODO: handle error - failed to retrieve URL
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function handleCurlResponse($response, $info, $request) {
|
|
|
|
$orig = $request->url_original;
|
|
|
|
$this->requests[$orig]['headers'] = substr($response, 0, $info['header_size']);
|
|
|
|
$this->requests[$orig]['body'] = substr($response, $info['header_size']);
|
2013-04-18 14:11:06 +00:00
|
|
|
$this->requests[$orig]['method'] = $request->method;
|
2011-11-04 17:10:31 +00:00
|
|
|
$this->requests[$orig]['effective_url'] = $info['url'];
|
|
|
|
$this->requests[$orig]['status_code'] = (int)$info['http_code'];
|
2014-05-15 20:49:16 +00:00
|
|
|
if (preg_match('/^Location:(.*?)$/mi', $this->requests[$orig]['headers'], $match)) {
|
2011-11-04 17:10:31 +00:00
|
|
|
$this->requests[$orig]['location'] = trim($match[1]);
|
|
|
|
}
|
2011-01-11 18:06:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function headersToString(array $headers, $associative=true) {
|
|
|
|
if (!$associative) {
|
|
|
|
return implode("\n", $headers);
|
|
|
|
} else {
|
|
|
|
$str = '';
|
|
|
|
foreach ($headers as $key => $val) {
|
|
|
|
if (is_array($val)) {
|
|
|
|
foreach ($val as $v) $str .= "$key: $v\n";
|
|
|
|
} else {
|
|
|
|
$str .= "$key: $val\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rtrim($str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-18 14:11:06 +00:00
|
|
|
public function get($url, $remove=false, $gzdecode=true) {
|
2012-04-30 22:51:43 +00:00
|
|
|
$url = "$url";
|
2011-01-11 18:06:12 +00:00
|
|
|
if (isset($this->requests[$url]) && isset($this->requests[$url]['body'])) {
|
2011-11-04 17:10:31 +00:00
|
|
|
$this->debug("URL already fetched - in memory ($url, effective: {$this->requests[$url]['effective_url']})");
|
2011-01-11 18:06:12 +00:00
|
|
|
$response = $this->requests[$url];
|
2011-11-04 17:10:31 +00:00
|
|
|
/*
|
2011-01-11 18:06:12 +00:00
|
|
|
} elseif ($this->isCached($url)) {
|
|
|
|
$this->debug("URL already fetched - in disk cache ($url)");
|
|
|
|
$response = $this->getCached($url);
|
|
|
|
$this->requests[$url] = $response;
|
2011-11-04 17:10:31 +00:00
|
|
|
*/
|
2011-01-11 18:06:12 +00:00
|
|
|
} else {
|
|
|
|
$this->debug("Fetching URL ($url)");
|
2011-11-04 17:10:31 +00:00
|
|
|
$this->fetchAll(array($url));
|
|
|
|
if (isset($this->requests[$url]) && isset($this->requests[$url]['body'])) {
|
|
|
|
$response = $this->requests[$url];
|
2011-01-11 18:06:12 +00:00
|
|
|
} else {
|
2011-11-04 17:10:31 +00:00
|
|
|
$this->debug("Request failed");
|
2011-01-11 18:06:12 +00:00
|
|
|
$response = false;
|
|
|
|
}
|
|
|
|
}
|
2011-11-04 17:10:31 +00:00
|
|
|
/*
|
2011-01-11 18:06:12 +00:00
|
|
|
if ($this->minimiseMemoryUse && $response) {
|
|
|
|
$this->cache($url);
|
|
|
|
unset($this->requests[$url]);
|
|
|
|
}
|
2011-11-04 17:10:31 +00:00
|
|
|
*/
|
|
|
|
if ($remove && $response) unset($this->requests[$url]);
|
2013-04-18 14:11:06 +00:00
|
|
|
if ($gzdecode && stripos($response['headers'], 'Content-Encoding: gzip')) {
|
2014-09-15 20:24:06 +00:00
|
|
|
if ($html = @gzdecode($response['body'])) {
|
2013-04-18 14:11:06 +00:00
|
|
|
$response['body'] = $html;
|
|
|
|
}
|
|
|
|
}
|
2011-01-11 18:06:12 +00:00
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function parallelSupport() {
|
2017-02-18 15:06:19 +00:00
|
|
|
return class_exists('http\Client') || function_exists('curl_multi_init');
|
2011-01-11 18:06:12 +00:00
|
|
|
}
|
2013-04-18 14:11:06 +00:00
|
|
|
|
|
|
|
private function headerOnlyType($headers) {
|
|
|
|
if (preg_match('!^Content-Type:\s*(([a-z-]+)/([^;\r\n ]+))!im', $headers, $match)) {
|
|
|
|
// look for full mime type (e.g. image/jpeg) or just type (e.g. image)
|
|
|
|
$match[1] = strtolower(trim($match[1]));
|
|
|
|
$match[2] = strtolower(trim($match[2]));
|
|
|
|
foreach (array($match[1], $match[2]) as $mime) {
|
|
|
|
if (in_array($mime, $this->headerOnlyTypes)) return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function possibleUnsupportedType($url) {
|
|
|
|
$path = @parse_url($url, PHP_URL_PATH);
|
|
|
|
if ($path && strpos($path, '.') !== false) {
|
|
|
|
$ext = strtolower(trim(pathinfo($path, PATHINFO_EXTENSION)));
|
|
|
|
return in_array($ext, $this->headerOnlyClues);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2017-02-18 15:06:19 +00:00
|
|
|
|
2015-06-14 00:28:37 +00:00
|
|
|
protected function getCookies($orig, $req_url) {
|
2019-04-04 21:23:27 +00:00
|
|
|
if (!isset($this->cookieJar[$orig])) return null;
|
2015-06-14 00:28:37 +00:00
|
|
|
$jar = $this->cookieJar[$orig];
|
|
|
|
if (!isset($jar)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return $jar->getMatchingCookies($req_url);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function storeCookies($orig, $url) {
|
|
|
|
$headers = $this->requests[$orig]['headers'];
|
|
|
|
$cookies = CookieJar::extractCookies($headers);
|
|
|
|
if (empty($cookies)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!isset($this->cookieJar[$orig])) {
|
|
|
|
$this->cookieJar[$orig] = new CookieJar();
|
|
|
|
}
|
|
|
|
$this->cookieJar[$orig]->storeCookies($url, $cookies);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function deleteCookies() {
|
|
|
|
$this->cookieJar = array();
|
|
|
|
}
|
2017-02-18 15:06:19 +00:00
|
|
|
|
2013-04-18 14:11:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// gzdecode from http://www.php.net/manual/en/function.gzdecode.php#82930
|
|
|
|
if (!function_exists('gzdecode')) {
|
|
|
|
function gzdecode($data,&$filename='',&$error='',$maxlength=null)
|
|
|
|
{
|
|
|
|
$len = strlen($data);
|
|
|
|
if ($len < 18 || strcmp(substr($data,0,2),"\x1f\x8b")) {
|
|
|
|
$error = "Not in GZIP format.";
|
|
|
|
return null; // Not GZIP format (See RFC 1952)
|
|
|
|
}
|
|
|
|
$method = ord(substr($data,2,1)); // Compression method
|
|
|
|
$flags = ord(substr($data,3,1)); // Flags
|
|
|
|
if ($flags & 31 != $flags) {
|
|
|
|
$error = "Reserved bits not allowed.";
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
// NOTE: $mtime may be negative (PHP integer limitations)
|
|
|
|
$mtime = unpack("V", substr($data,4,4));
|
|
|
|
$mtime = $mtime[1];
|
|
|
|
$xfl = substr($data,8,1);
|
|
|
|
$os = substr($data,8,1);
|
|
|
|
$headerlen = 10;
|
|
|
|
$extralen = 0;
|
|
|
|
$extra = "";
|
|
|
|
if ($flags & 4) {
|
|
|
|
// 2-byte length prefixed EXTRA data in header
|
|
|
|
if ($len - $headerlen - 2 < 8) {
|
|
|
|
return false; // invalid
|
|
|
|
}
|
|
|
|
$extralen = unpack("v",substr($data,8,2));
|
|
|
|
$extralen = $extralen[1];
|
|
|
|
if ($len - $headerlen - 2 - $extralen < 8) {
|
|
|
|
return false; // invalid
|
|
|
|
}
|
|
|
|
$extra = substr($data,10,$extralen);
|
|
|
|
$headerlen += 2 + $extralen;
|
|
|
|
}
|
|
|
|
$filenamelen = 0;
|
|
|
|
$filename = "";
|
|
|
|
if ($flags & 8) {
|
|
|
|
// C-style string
|
|
|
|
if ($len - $headerlen - 1 < 8) {
|
|
|
|
return false; // invalid
|
|
|
|
}
|
|
|
|
$filenamelen = strpos(substr($data,$headerlen),chr(0));
|
|
|
|
if ($filenamelen === false || $len - $headerlen - $filenamelen - 1 < 8) {
|
|
|
|
return false; // invalid
|
|
|
|
}
|
|
|
|
$filename = substr($data,$headerlen,$filenamelen);
|
|
|
|
$headerlen += $filenamelen + 1;
|
|
|
|
}
|
|
|
|
$commentlen = 0;
|
|
|
|
$comment = "";
|
|
|
|
if ($flags & 16) {
|
|
|
|
// C-style string COMMENT data in header
|
|
|
|
if ($len - $headerlen - 1 < 8) {
|
|
|
|
return false; // invalid
|
|
|
|
}
|
|
|
|
$commentlen = strpos(substr($data,$headerlen),chr(0));
|
|
|
|
if ($commentlen === false || $len - $headerlen - $commentlen - 1 < 8) {
|
|
|
|
return false; // Invalid header format
|
|
|
|
}
|
|
|
|
$comment = substr($data,$headerlen,$commentlen);
|
|
|
|
$headerlen += $commentlen + 1;
|
|
|
|
}
|
|
|
|
$headercrc = "";
|
|
|
|
if ($flags & 2) {
|
|
|
|
// 2-bytes (lowest order) of CRC32 on header present
|
|
|
|
if ($len - $headerlen - 2 < 8) {
|
|
|
|
return false; // invalid
|
|
|
|
}
|
|
|
|
$calccrc = crc32(substr($data,0,$headerlen)) & 0xffff;
|
|
|
|
$headercrc = unpack("v", substr($data,$headerlen,2));
|
|
|
|
$headercrc = $headercrc[1];
|
|
|
|
if ($headercrc != $calccrc) {
|
|
|
|
$error = "Header checksum failed.";
|
|
|
|
return false; // Bad header CRC
|
|
|
|
}
|
|
|
|
$headerlen += 2;
|
|
|
|
}
|
|
|
|
// GZIP FOOTER
|
|
|
|
$datacrc = unpack("V",substr($data,-8,4));
|
|
|
|
$datacrc = sprintf('%u',$datacrc[1] & 0xFFFFFFFF);
|
|
|
|
$isize = unpack("V",substr($data,-4));
|
|
|
|
$isize = $isize[1];
|
|
|
|
// decompression:
|
|
|
|
$bodylen = $len-$headerlen-8;
|
|
|
|
if ($bodylen < 1) {
|
|
|
|
// IMPLEMENTATION BUG!
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$body = substr($data,$headerlen,$bodylen);
|
|
|
|
$data = "";
|
|
|
|
if ($bodylen > 0) {
|
|
|
|
switch ($method) {
|
|
|
|
case 8:
|
|
|
|
// Currently the only supported compression method:
|
|
|
|
$data = gzinflate($body,$maxlength);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$error = "Unknown compression method.";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} // zero-byte body content is allowed
|
|
|
|
// Verifiy CRC32
|
|
|
|
$crc = sprintf("%u",crc32($data));
|
|
|
|
$crcOK = $crc == $datacrc;
|
|
|
|
$lenOK = $isize == strlen($data);
|
|
|
|
if (!$lenOK || !$crcOK) {
|
|
|
|
$error = ( $lenOK ? '' : 'Length check FAILED. ') . ( $crcOK ? '' : 'Checksum FAILED.');
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return $data;
|
|
|
|
}
|
2014-05-15 21:03:31 +00:00
|
|
|
}
|