2005-06-26 06:34:13 +00:00
|
|
|
<?php
|
2009-11-26 12:00:36 +00:00
|
|
|
/**
|
2009-11-28 03:49:29 +00:00
|
|
|
* @defgroup HTTP HTTP
|
2009-12-05 17:07:40 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Various HTTP related functions
|
2009-11-26 12:00:36 +00:00
|
|
|
* @ingroup HTTP
|
|
|
|
|
*/
|
2006-07-02 15:57:59 +00:00
|
|
|
class Http {
|
2010-01-07 21:57:33 +00:00
|
|
|
/**
|
2010-01-22 02:17:58 +00:00
|
|
|
* Perform an HTTP request
|
2010-01-07 21:57:33 +00:00
|
|
|
* @param $method string HTTP method. Usually GET/POST
|
|
|
|
|
* @param $url string Full URL to act on
|
2010-01-22 02:17:58 +00:00
|
|
|
* @param $opts options to pass to HttpRequest object
|
|
|
|
|
* @returns mixed (bool)false on failure or a string on success
|
2010-01-07 21:57:33 +00:00
|
|
|
*/
|
|
|
|
|
public static function request( $method, $url, $opts = array() ) {
|
2010-01-22 02:17:58 +00:00
|
|
|
$opts['method'] = strtoupper( $method );
|
|
|
|
|
if ( !array_key_exists( 'timeout', $opts ) ) {
|
|
|
|
|
$opts['timeout'] = 'default';
|
|
|
|
|
}
|
|
|
|
|
$req = HttpRequest::factory( $url, $opts );
|
|
|
|
|
$status = $req->execute();
|
|
|
|
|
if ( $status->isOK() ) {
|
|
|
|
|
return $req;
|
2010-01-07 21:57:33 +00:00
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2009-07-25 20:05:41 +00:00
|
|
|
|
2008-09-29 06:12:47 +00:00
|
|
|
/**
|
|
|
|
|
* Simple wrapper for Http::request( 'GET' )
|
2009-11-26 12:00:36 +00:00
|
|
|
* @see Http::request()
|
2008-09-29 06:12:47 +00:00
|
|
|
*/
|
2010-01-22 02:17:58 +00:00
|
|
|
public static function get( $url, $timeout = 'default', $opts = array() ) {
|
|
|
|
|
$opts['timeout'] = $timeout;
|
2010-01-07 21:57:33 +00:00
|
|
|
return Http::request( 'GET', $url, $opts );
|
2007-05-10 19:13:02 +00:00
|
|
|
}
|
2009-07-15 00:55:58 +00:00
|
|
|
|
2008-09-29 06:12:47 +00:00
|
|
|
/**
|
|
|
|
|
* Simple wrapper for Http::request( 'POST' )
|
2009-11-26 12:00:36 +00:00
|
|
|
* @see Http::request()
|
2008-09-29 06:12:47 +00:00
|
|
|
*/
|
2010-01-07 21:57:33 +00:00
|
|
|
public static function post( $url, $opts = array() ) {
|
|
|
|
|
return Http::request( 'POST', $url, $opts );
|
|
|
|
|
}
|
|
|
|
|
|
2006-07-02 15:57:59 +00:00
|
|
|
/**
|
2010-01-15 18:57:29 +00:00
|
|
|
* Check if the URL can be served by localhost
|
|
|
|
|
* @param $url string Full url to check
|
|
|
|
|
* @return bool
|
2006-07-02 15:57:59 +00:00
|
|
|
*/
|
2010-01-15 18:57:29 +00:00
|
|
|
public static function isLocalURL( $url ) {
|
|
|
|
|
global $wgCommandLineMode, $wgConf;
|
|
|
|
|
if ( $wgCommandLineMode ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2005-11-01 22:26:11 +00:00
|
|
|
|
2010-01-15 18:57:29 +00:00
|
|
|
// Extract host part
|
|
|
|
|
$matches = array();
|
|
|
|
|
if ( preg_match( '!^http://([\w.-]+)[/:].*$!', $url, $matches ) ) {
|
|
|
|
|
$host = $matches[1];
|
|
|
|
|
// Split up dotwise
|
|
|
|
|
$domainParts = explode( '.', $host );
|
|
|
|
|
// Check if this domain or any superdomain is listed in $wgConf as a local virtual host
|
|
|
|
|
$domainParts = array_reverse( $domainParts );
|
|
|
|
|
for ( $i = 0; $i < count( $domainParts ); $i++ ) {
|
|
|
|
|
$domainPart = $domainParts[$i];
|
|
|
|
|
if ( $i == 0 ) {
|
|
|
|
|
$domain = $domainPart;
|
|
|
|
|
} else {
|
|
|
|
|
$domain = $domainPart . '.' . $domain;
|
|
|
|
|
}
|
|
|
|
|
if ( $wgConf->isLocalVHost( $domain ) ) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2010-01-07 21:57:33 +00:00
|
|
|
|
2010-01-15 18:57:29 +00:00
|
|
|
/**
|
2010-01-22 02:17:58 +00:00
|
|
|
* A standard user-agent we can use for external requests.
|
|
|
|
|
* @returns string
|
2010-01-15 18:57:29 +00:00
|
|
|
*/
|
|
|
|
|
public static function userAgent() {
|
|
|
|
|
global $wgVersion;
|
|
|
|
|
return "MediaWiki/$wgVersion";
|
|
|
|
|
}
|
2010-01-07 21:57:33 +00:00
|
|
|
|
2010-01-15 18:57:29 +00:00
|
|
|
/**
|
|
|
|
|
* Checks that the given URI is a valid one
|
|
|
|
|
* @param $uri Mixed: URI to check for validity
|
2010-01-22 02:17:58 +00:00
|
|
|
* @returns bool
|
2010-01-15 18:57:29 +00:00
|
|
|
*/
|
2010-01-22 02:17:58 +00:00
|
|
|
public static function isValidURI( $uri ) {
|
2010-01-15 18:57:29 +00:00
|
|
|
return preg_match(
|
|
|
|
|
'/(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/',
|
|
|
|
|
$uri,
|
|
|
|
|
$matches
|
|
|
|
|
);
|
2010-01-07 21:57:33 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-01-22 02:17:58 +00:00
|
|
|
/**
|
|
|
|
|
* This wrapper class will call out to curl (if available) or fallback
|
|
|
|
|
* to regular PHP if necessary for handling internal HTTP requests.
|
|
|
|
|
*/
|
2010-01-07 21:57:33 +00:00
|
|
|
class HttpRequest {
|
2010-01-22 02:17:58 +00:00
|
|
|
protected $content;
|
|
|
|
|
protected $timeout = 'default';
|
|
|
|
|
protected $headersOnly = null;
|
|
|
|
|
protected $postdata = null;
|
|
|
|
|
protected $proxy = null;
|
|
|
|
|
protected $no_proxy = false;
|
|
|
|
|
protected $sslVerifyHost = true;
|
|
|
|
|
protected $caInfo = null;
|
|
|
|
|
protected $method = "GET";
|
|
|
|
|
protected $url;
|
|
|
|
|
protected $parsed_url;
|
|
|
|
|
public $status;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $url string url to use
|
|
|
|
|
* @param $options array (optional) extra params to pass
|
|
|
|
|
* Possible keys for the array:
|
|
|
|
|
* method
|
|
|
|
|
* timeout
|
|
|
|
|
* targetFilePath
|
|
|
|
|
* requestKey
|
|
|
|
|
* headersOnly
|
|
|
|
|
* postdata
|
|
|
|
|
* proxy
|
|
|
|
|
* no_proxy
|
|
|
|
|
* sslVerifyHost
|
|
|
|
|
* caInfo
|
|
|
|
|
*/
|
2010-01-22 03:01:48 +00:00
|
|
|
function __construct( $url = null, $opt = array() ) {
|
2010-01-22 02:17:58 +00:00
|
|
|
global $wgHTTPTimeout, $wgTitle;
|
2010-01-07 21:57:33 +00:00
|
|
|
|
|
|
|
|
$this->url = $url;
|
2010-01-22 03:01:48 +00:00
|
|
|
$this->parsed_url = parse_url( $url );
|
2010-01-22 02:17:58 +00:00
|
|
|
|
|
|
|
|
if ( !ini_get( 'allow_url_fopen' ) ) {
|
|
|
|
|
throw new MWException( 'allow_url_fopen needs to be enabled for http requests to work' );
|
|
|
|
|
} elseif ( !Http::isValidURI( $this->url ) ) {
|
2010-01-22 03:01:48 +00:00
|
|
|
throw new MWException( 'Invalid URL' );
|
2010-01-22 02:17:58 +00:00
|
|
|
} else {
|
|
|
|
|
$this->status = Status::newGood( 100 ); // continue
|
2010-01-15 05:56:57 +00:00
|
|
|
}
|
2010-01-07 21:57:33 +00:00
|
|
|
|
2010-01-22 02:17:58 +00:00
|
|
|
if ( array_key_exists( 'timeout', $opt ) && $opt['timeout'] != 'default' ) {
|
|
|
|
|
$this->timeout = $opt['timeout'];
|
|
|
|
|
} else {
|
|
|
|
|
$this->timeout = $wgHTTPTimeout;
|
|
|
|
|
}
|
2010-01-07 21:57:33 +00:00
|
|
|
|
2010-01-22 02:17:58 +00:00
|
|
|
$members = array( "targetFilePath", "requestKey", "headersOnly", "postdata",
|
2010-01-22 03:01:48 +00:00
|
|
|
"proxy", "no_proxy", "sslVerifyHost", "caInfo", "method" );
|
2010-01-22 02:17:58 +00:00
|
|
|
foreach ( $members as $o ) {
|
|
|
|
|
if ( array_key_exists( $o, $opt ) ) {
|
|
|
|
|
$this->$o = $opt[$o];
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-01-07 21:57:33 +00:00
|
|
|
|
2010-01-22 02:17:58 +00:00
|
|
|
if ( is_array( $this->postdata ) ) {
|
|
|
|
|
$this->postdata = wfArrayToCGI( $this->postdata );
|
|
|
|
|
}
|
2010-01-07 21:57:33 +00:00
|
|
|
|
2010-01-22 02:17:58 +00:00
|
|
|
$this->initRequest();
|
2010-01-07 21:57:33 +00:00
|
|
|
|
2010-01-22 02:17:58 +00:00
|
|
|
if ( !$this->no_proxy ) {
|
|
|
|
|
$this->proxySetup();
|
|
|
|
|
}
|
2010-01-15 05:56:57 +00:00
|
|
|
|
2010-01-22 02:17:58 +00:00
|
|
|
# Set the referer to $wgTitle, even in command-line mode
|
|
|
|
|
# This is useful for interwiki transclusion, where the foreign
|
|
|
|
|
# server wants to know what the referring page is.
|
|
|
|
|
# $_SERVER['REQUEST_URI'] gives a less reliable indication of the
|
|
|
|
|
# referring page.
|
|
|
|
|
if ( is_object( $wgTitle ) ) {
|
|
|
|
|
$this->setReferrer( $wgTitle->getFullURL() );
|
2010-01-07 21:57:33 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-01-15 18:57:29 +00:00
|
|
|
/**
|
2010-01-22 02:17:58 +00:00
|
|
|
* For backwards compatibility, we provide a __toString method so
|
|
|
|
|
* that any code that expects a string result from Http::Get()
|
|
|
|
|
* will see the content of the request.
|
2010-01-15 18:57:29 +00:00
|
|
|
*/
|
2010-01-22 02:17:58 +00:00
|
|
|
function __toString() {
|
|
|
|
|
return $this->content;
|
2010-01-07 21:57:33 +00:00
|
|
|
}
|
2010-01-22 02:17:58 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Generate a new request object
|
|
|
|
|
* @see HttpRequest::__construct
|
|
|
|
|
*/
|
|
|
|
|
public static function factory( $url, $opt ) {
|
|
|
|
|
global $wgHTTPEngine;
|
|
|
|
|
$engine = $wgHTTPEngine;
|
|
|
|
|
|
|
|
|
|
if ( !$wgHTTPEngine ) {
|
|
|
|
|
$wgHTTPEngine = function_exists( 'curl_init' ) ? 'curl' : 'php';
|
|
|
|
|
} elseif ( $wgHTTPEngine == 'curl' && !function_exists( 'curl_init' ) ) {
|
|
|
|
|
throw new MWException( 'FIXME' );
|
2010-01-07 21:57:33 +00:00
|
|
|
}
|
|
|
|
|
|
2010-01-22 02:17:58 +00:00
|
|
|
switch( $wgHTTPEngine ) {
|
|
|
|
|
case 'curl':
|
|
|
|
|
return new CurlHttpRequest( $url, $opt );
|
|
|
|
|
case 'php':
|
|
|
|
|
return new PhpHttpRequest( $url, $opt );
|
|
|
|
|
default:
|
2010-01-22 03:01:48 +00:00
|
|
|
throw new MWException( 'The setting of $wgHTTPEngine is not valid.' );
|
2010-01-22 02:17:58 +00:00
|
|
|
}
|
|
|
|
|
}
|
2010-01-07 21:57:33 +00:00
|
|
|
|
2010-01-22 02:17:58 +00:00
|
|
|
public function getContent() {
|
|
|
|
|
return $this->content;
|
|
|
|
|
}
|
2010-01-07 21:57:33 +00:00
|
|
|
|
2010-01-22 03:01:48 +00:00
|
|
|
public function initRequest() { }
|
|
|
|
|
public function proxySetup() { }
|
|
|
|
|
public function setReferrer( $url ) { }
|
|
|
|
|
public function setCallback( $cb ) { }
|
|
|
|
|
public function read( $fh, $content ) { }
|
|
|
|
|
public function getCode() { }
|
|
|
|
|
public function execute() { }
|
2010-01-22 02:17:58 +00:00
|
|
|
}
|
2010-01-07 21:57:33 +00:00
|
|
|
|
2010-01-22 02:17:58 +00:00
|
|
|
/**
|
|
|
|
|
* HttpRequest implemented using internal curl compiled into PHP
|
|
|
|
|
*/
|
|
|
|
|
class CurlHttpRequest extends HttpRequest {
|
|
|
|
|
protected $curlHandle;
|
|
|
|
|
protected $curlCBSet;
|
|
|
|
|
|
|
|
|
|
public function initRequest() {
|
|
|
|
|
$this->curlHandle = curl_init( $this->url );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function proxySetup() {
|
|
|
|
|
global $wgHTTPProxy;
|
|
|
|
|
|
|
|
|
|
if ( is_string( $this->proxy ) ) {
|
|
|
|
|
curl_setopt( $this->curlHandle, CURLOPT_PROXY, $this->proxy );
|
|
|
|
|
} else if ( Http::isLocalURL( $this->url ) ) { /* Not sure this makes any sense. */
|
|
|
|
|
curl_setopt( $this->curlHandle, CURLOPT_PROXY, 'localhost:80' );
|
|
|
|
|
} else if ( $wgHTTPProxy ) {
|
|
|
|
|
curl_setopt( $this->curlHandle, CURLOPT_PROXY, $wgHTTPProxy );
|
2010-01-07 21:57:33 +00:00
|
|
|
}
|
2010-01-22 02:17:58 +00:00
|
|
|
}
|
2010-01-07 21:57:33 +00:00
|
|
|
|
2010-01-22 02:17:58 +00:00
|
|
|
public function setCallback( $cb ) {
|
|
|
|
|
if ( !$this->curlCBSet ) {
|
|
|
|
|
$this->curlCBSet = true;
|
|
|
|
|
curl_setopt( $this->curlHandle, CURLOPT_WRITEFUNCTION, $cb );
|
2010-01-07 21:57:33 +00:00
|
|
|
}
|
2010-01-22 02:17:58 +00:00
|
|
|
}
|
2010-01-07 21:57:33 +00:00
|
|
|
|
2010-01-22 02:17:58 +00:00
|
|
|
public function execute() {
|
2010-01-22 03:01:48 +00:00
|
|
|
if ( !$this->status->isOK() ) {
|
2010-01-22 02:17:58 +00:00
|
|
|
return $this->status;
|
2010-01-15 05:56:57 +00:00
|
|
|
}
|
|
|
|
|
|
2010-01-22 03:01:48 +00:00
|
|
|
$this->setCallback( array( $this, 'read' ) );
|
2010-01-15 05:56:57 +00:00
|
|
|
|
2010-01-22 02:17:58 +00:00
|
|
|
curl_setopt( $this->curlHandle, CURLOPT_TIMEOUT, $this->timeout );
|
|
|
|
|
curl_setopt( $this->curlHandle, CURLOPT_USERAGENT, Http::userAgent() );
|
|
|
|
|
curl_setopt( $this->curlHandle, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0 );
|
|
|
|
|
|
|
|
|
|
if ( $this->sslVerifyHost ) {
|
|
|
|
|
curl_setopt( $this->curlHandle, CURLOPT_SSL_VERIFYHOST, $this->sslVerifyHost );
|
2010-01-07 21:57:33 +00:00
|
|
|
}
|
2010-01-22 02:17:58 +00:00
|
|
|
|
|
|
|
|
if ( $this->caInfo ) {
|
|
|
|
|
curl_setopt( $this->curlHandle, CURLOPT_CAINFO, $this->caInfo );
|
2010-01-07 21:57:33 +00:00
|
|
|
}
|
|
|
|
|
|
2010-01-22 02:17:58 +00:00
|
|
|
if ( $this->headersOnly ) {
|
|
|
|
|
curl_setopt( $this->curlHandle, CURLOPT_NOBODY, true );
|
|
|
|
|
curl_setopt( $this->curlHandle, CURLOPT_HEADER, true );
|
|
|
|
|
} elseif ( $this->method == 'POST' ) {
|
|
|
|
|
curl_setopt( $this->curlHandle, CURLOPT_POST, true );
|
|
|
|
|
curl_setopt( $this->curlHandle, CURLOPT_POSTFIELDS, $this->postdata );
|
|
|
|
|
// Suppress 'Expect: 100-continue' header, as some servers
|
|
|
|
|
// will reject it with a 417 and Curl won't auto retry
|
|
|
|
|
// with HTTP 1.0 fallback
|
|
|
|
|
curl_setopt( $this->curlHandle, CURLOPT_HTTPHEADER, array( 'Expect:' ) );
|
2010-01-07 21:57:33 +00:00
|
|
|
} else {
|
2010-01-22 02:17:58 +00:00
|
|
|
curl_setopt( $this->curlHandle, CURLOPT_CUSTOMREQUEST, $this->method );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
if ( false === curl_exec( $this->curlHandle ) ) {
|
2010-01-22 03:01:48 +00:00
|
|
|
$this->status->fatal( 'Error sending request (#$1): $2',
|
|
|
|
|
curl_errno( $this->curlHandle ),
|
|
|
|
|
curl_error( $this->curlHandle ) );
|
2010-01-07 21:57:33 +00:00
|
|
|
}
|
2010-01-22 02:17:58 +00:00
|
|
|
} catch ( Exception $e ) {
|
|
|
|
|
$errno = curl_errno( $this->curlHandle );
|
2010-01-07 21:57:33 +00:00
|
|
|
if ( $errno != CURLE_OK ) {
|
2010-01-22 02:17:58 +00:00
|
|
|
$errstr = curl_error( $this->curlHandle );
|
2010-01-22 03:01:48 +00:00
|
|
|
$this->status->fatal( 'CURL error code $1: $2', $errno, $errstr );
|
2010-01-07 21:57:33 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-01-22 02:17:58 +00:00
|
|
|
curl_close( $this->curlHandle );
|
|
|
|
|
|
|
|
|
|
return $this->status;
|
2010-01-15 05:56:57 +00:00
|
|
|
}
|
2010-01-07 21:57:33 +00:00
|
|
|
|
2010-01-22 02:17:58 +00:00
|
|
|
public function read( $curlH, $content ) {
|
|
|
|
|
$this->content .= $content;
|
|
|
|
|
return strlen( $content );
|
|
|
|
|
}
|
2010-01-07 21:57:33 +00:00
|
|
|
|
2010-01-22 02:17:58 +00:00
|
|
|
public function getCode() {
|
|
|
|
|
# Don't return truncated output
|
|
|
|
|
$code = curl_getinfo( $this->curlHandle, CURLINFO_HTTP_CODE );
|
|
|
|
|
if ( $code < 400 ) {
|
|
|
|
|
$this->status->setResult( true, $code );
|
|
|
|
|
} else {
|
|
|
|
|
$this->status->setResult( false, $code );
|
2010-01-07 21:57:33 +00:00
|
|
|
}
|
2010-01-22 02:17:58 +00:00
|
|
|
}
|
|
|
|
|
}
|
2010-01-07 21:57:33 +00:00
|
|
|
|
2010-01-22 02:17:58 +00:00
|
|
|
class PhpHttpRequest extends HttpRequest {
|
|
|
|
|
private $reqHeaders;
|
|
|
|
|
private $callback;
|
|
|
|
|
private $fh;
|
|
|
|
|
|
|
|
|
|
public function initRequest() {
|
|
|
|
|
$this->setCallback( array( $this, 'read' ) );
|
2010-01-15 05:56:57 +00:00
|
|
|
|
2010-01-22 02:17:58 +00:00
|
|
|
$this->reqHeaders[] = "User-Agent: " . Http::userAgent();
|
|
|
|
|
$this->reqHeaders[] = "Accept: */*";
|
|
|
|
|
if ( $this->method == 'POST' ) {
|
2010-01-15 18:57:29 +00:00
|
|
|
// Required for HTTP 1.0 POSTs
|
2010-01-22 02:17:58 +00:00
|
|
|
$this->reqHeaders[] = "Content-Length: " . strlen( $this->postdata );
|
|
|
|
|
$this->reqHeaders[] = "Content-type: application/x-www-form-urlencoded";
|
2010-01-15 05:56:57 +00:00
|
|
|
}
|
|
|
|
|
|
2010-01-22 03:01:48 +00:00
|
|
|
if ( $this->parsed_url['scheme'] != 'http' ) {
|
2010-01-22 02:17:58 +00:00
|
|
|
$this->status->fatal( "Only http:// is supported currently." );
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-01-15 05:56:57 +00:00
|
|
|
|
2010-01-22 03:01:48 +00:00
|
|
|
protected function urlToTcp( $url ) {
|
|
|
|
|
$parsed_url = parse_url( $url );
|
2010-01-15 05:56:57 +00:00
|
|
|
|
2010-01-22 03:01:48 +00:00
|
|
|
return 'tcp://' . $parsed_url['host'] . ':' . $parsed_url['port'];
|
2010-01-22 02:17:58 +00:00
|
|
|
}
|
2010-01-07 21:57:33 +00:00
|
|
|
|
2010-01-22 02:17:58 +00:00
|
|
|
public function proxySetup() {
|
|
|
|
|
global $wgHTTPProxy;
|
2010-01-15 05:56:57 +00:00
|
|
|
|
2010-01-22 02:17:58 +00:00
|
|
|
if ( Http::isLocalURL( $this->url ) ) {
|
|
|
|
|
$this->proxy = 'http://localhost:80/';
|
|
|
|
|
} elseif ( $wgHTTPProxy ) {
|
|
|
|
|
$this->proxy = $wgHTTPProxy ;
|
2010-01-15 05:56:57 +00:00
|
|
|
}
|
2010-01-22 02:17:58 +00:00
|
|
|
}
|
2010-01-15 05:56:57 +00:00
|
|
|
|
2010-01-22 02:17:58 +00:00
|
|
|
public function setReferrer( $url ) {
|
|
|
|
|
$this->reqHeaders[] = "Referer: $url";
|
2010-01-15 05:56:57 +00:00
|
|
|
}
|
2010-01-15 18:57:29 +00:00
|
|
|
|
2010-01-22 02:17:58 +00:00
|
|
|
public function setCallback( $cb ) {
|
|
|
|
|
$this->callback = $cb;
|
|
|
|
|
}
|
2010-01-07 21:57:33 +00:00
|
|
|
|
2010-01-22 02:17:58 +00:00
|
|
|
public function read( $fh, $contents ) {
|
|
|
|
|
if ( $this->headersOnly ) {
|
|
|
|
|
return false;
|
2010-01-07 21:57:33 +00:00
|
|
|
}
|
2010-01-22 02:17:58 +00:00
|
|
|
$this->content .= $contents;
|
|
|
|
|
|
|
|
|
|
return strlen( $contents );
|
2010-01-07 21:57:33 +00:00
|
|
|
}
|
|
|
|
|
|
2010-01-22 02:17:58 +00:00
|
|
|
public function execute() {
|
2010-01-22 03:01:48 +00:00
|
|
|
if ( !$this->status->isOK() ) {
|
2010-01-22 02:17:58 +00:00
|
|
|
return $this->status;
|
|
|
|
|
}
|
2010-01-07 21:57:33 +00:00
|
|
|
|
2010-01-22 02:17:58 +00:00
|
|
|
$opts = array();
|
|
|
|
|
if ( $this->proxy && !$this->no_proxy ) {
|
2010-01-22 03:01:48 +00:00
|
|
|
$opts['proxy'] = $this->urlToTCP( $this->proxy );
|
2010-01-22 02:17:58 +00:00
|
|
|
$opts['request_fulluri'] = true;
|
2010-01-07 21:57:33 +00:00
|
|
|
}
|
|
|
|
|
|
2010-01-22 02:17:58 +00:00
|
|
|
$opts['method'] = $this->method;
|
|
|
|
|
$opts['timeout'] = $this->timeout;
|
|
|
|
|
$opts['header'] = implode( "\r\n", $this->reqHeaders );
|
|
|
|
|
// FOR NOW: Force everyone to HTTP 1.0
|
|
|
|
|
/* if ( version_compare( "5.3.0", phpversion(), ">" ) ) { */
|
|
|
|
|
$opts['protocol_version'] = "1.0";
|
|
|
|
|
/* } else { */
|
|
|
|
|
/* $opts['protocol_version'] = "1.1"; */
|
|
|
|
|
/* } */
|
2010-01-15 18:57:29 +00:00
|
|
|
|
2010-01-22 02:17:58 +00:00
|
|
|
if ( $this->postdata ) {
|
|
|
|
|
$opts['content'] = $this->postdata;
|
2010-01-07 21:57:33 +00:00
|
|
|
}
|
|
|
|
|
|
2010-01-22 02:17:58 +00:00
|
|
|
$context = stream_context_create( array( 'http' => $opts ) );
|
|
|
|
|
try {
|
|
|
|
|
$this->fh = fopen( $this->url, "r", false, $context );
|
2010-01-22 03:01:48 +00:00
|
|
|
} catch ( Exception $e ) {
|
|
|
|
|
$this->status->fatal( $e->getMessage() );
|
2010-01-22 02:17:58 +00:00
|
|
|
return $this->status;
|
2010-01-07 21:57:33 +00:00
|
|
|
}
|
2010-01-22 02:17:58 +00:00
|
|
|
|
|
|
|
|
$result = stream_get_meta_data( $this->fh );
|
|
|
|
|
if ( $result['timed_out'] ) {
|
2010-01-22 03:01:48 +00:00
|
|
|
$this->status->error( 'The request timed out' );
|
2010-01-07 21:57:33 +00:00
|
|
|
}
|
|
|
|
|
|
2010-01-22 02:17:58 +00:00
|
|
|
$this->headers = $result['wrapper_data'];
|
2010-01-07 21:57:33 +00:00
|
|
|
|
2010-01-22 02:17:58 +00:00
|
|
|
$end = false;
|
|
|
|
|
while ( !$end ) {
|
|
|
|
|
$contents = fread( $this->fh, 8192 );
|
|
|
|
|
$size = call_user_func_array( $this->callback, array( $this->fh, $contents ) );
|
|
|
|
|
$end = ( $size == 0 ) || feof( $this->fh );
|
2010-01-07 21:57:33 +00:00
|
|
|
}
|
2010-01-22 02:17:58 +00:00
|
|
|
fclose( $this->fh );
|
2010-01-07 21:57:33 +00:00
|
|
|
|
2010-01-22 02:17:58 +00:00
|
|
|
return $this->status;
|
|
|
|
|
}
|
2009-07-25 00:19:36 +00:00
|
|
|
}
|