Follow up 61352, address TimStarling's comments.
[lhc/web/wiklou.git] / includes / HttpFunctions.php
1 <?php
2 /**
3 * @defgroup HTTP HTTP
4 */
5
6 /**
7 * Various HTTP related functions
8 * @ingroup HTTP
9 */
10 class Http {
11 /**
12 * Perform an HTTP request
13 * @param $method string HTTP method. Usually GET/POST
14 * @param $url string Full URL to act on
15 * @param $options options to pass to HttpRequest object
16 * @returns mixed (bool)false on failure or a string on success
17 */
18 public static function request( $method, $url, $options = array() ) {
19 $options['method'] = strtoupper( $method );
20 if ( !isset( $options['timeout'] ) ) {
21 $options['timeout'] = 'default';
22 }
23 $req = HttpRequest::factory( $url, $options );
24 $status = $req->execute();
25 if ( $status->isOK() ) {
26 return $req->getContent();
27 } else {
28 return false;
29 }
30 }
31
32 /**
33 * Simple wrapper for Http::request( 'GET' )
34 * @see Http::request()
35 */
36 public static function get( $url, $timeout = 'default', $options = array() ) {
37 $options['timeout'] = $timeout;
38 return Http::request( 'GET', $url, $options );
39 }
40
41 /**
42 * Simple wrapper for Http::request( 'POST' )
43 * @see Http::request()
44 */
45 public static function post( $url, $options = array() ) {
46 return Http::request( 'POST', $url, $options );
47 }
48
49 /**
50 * Check if the URL can be served by localhost
51 * @param $url string Full url to check
52 * @return bool
53 */
54 public static function isLocalURL( $url ) {
55 global $wgCommandLineMode, $wgConf;
56 if ( $wgCommandLineMode ) {
57 return false;
58 }
59
60 // Extract host part
61 $matches = array();
62 if ( preg_match( '!^http://([\w.-]+)[/:].*$!', $url, $matches ) ) {
63 $host = $matches[1];
64 // Split up dotwise
65 $domainParts = explode( '.', $host );
66 // Check if this domain or any superdomain is listed in $wgConf as a local virtual host
67 $domainParts = array_reverse( $domainParts );
68 for ( $i = 0; $i < count( $domainParts ); $i++ ) {
69 $domainPart = $domainParts[$i];
70 if ( $i == 0 ) {
71 $domain = $domainPart;
72 } else {
73 $domain = $domainPart . '.' . $domain;
74 }
75 if ( $wgConf->isLocalVHost( $domain ) ) {
76 return true;
77 }
78 }
79 }
80 return false;
81 }
82
83 /**
84 * A standard user-agent we can use for external requests.
85 * @returns string
86 */
87 public static function userAgent() {
88 global $wgVersion;
89 return "MediaWiki/$wgVersion";
90 }
91
92 /**
93 * Checks that the given URI is a valid one
94 * @param $uri Mixed: URI to check for validity
95 * @returns bool
96 */
97 public static function isValidURI( $uri ) {
98 return preg_match(
99 '/(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/',
100 $uri,
101 $matches
102 );
103 }
104 }
105
106 /**
107 * This wrapper class will call out to curl (if available) or fallback
108 * to regular PHP if necessary for handling internal HTTP requests.
109 */
110 class HttpRequest {
111 protected $content;
112 protected $timeout = 'default';
113 protected $headersOnly = null;
114 protected $postData = null;
115 protected $proxy = null;
116 protected $noProxy = false;
117 protected $sslVerifyHost = true;
118 protected $caInfo = null;
119 protected $method = "GET";
120 protected $reqHeaders = array();
121 protected $url;
122 protected $parsedUrl;
123 protected $callback;
124 public $status;
125
126 /**
127 * @param $url string url to use
128 * @param $options array (optional) extra params to pass
129 * Possible keys for the array:
130 * method
131 * timeout
132 * targetFilePath
133 * requestKey
134 * postData
135 * proxy
136 * noProxy
137 * sslVerifyHost
138 * caInfo
139 */
140 function __construct( $url, $options = array() ) {
141 global $wgHTTPTimeout;
142
143 $this->url = $url;
144 $this->parsedUrl = parse_url( $url );
145
146 if ( !Http::isValidURI( $this->url ) ) {
147 $this->status = Status::newFromFatal('http-invalid-url');
148 } else {
149 $this->status = Status::newGood( 100 ); // continue
150 }
151
152 if ( isset($options['timeout']) && $options['timeout'] != 'default' ) {
153 $this->timeout = $options['timeout'];
154 } else {
155 $this->timeout = $wgHTTPTimeout;
156 }
157
158 $members = array( "targetFilePath", "requestKey", "postData",
159 "proxy", "noProxy", "sslVerifyHost", "caInfo", "method" );
160 foreach ( $members as $o ) {
161 if ( isset($options[$o]) ) {
162 $this->$o = $options[$o];
163 }
164 }
165 }
166
167 /**
168 * Generate a new request object
169 * @see HttpRequest::__construct
170 */
171 public static function factory( $url, $options ) {
172 global $wgHTTPEngine;
173 $engine = $wgHTTPEngine;
174
175 if ( !$wgHTTPEngine ) {
176 $wgHTTPEngine = function_exists( 'curl_init' ) ? 'curl' : 'php';
177 } elseif ( $wgHTTPEngine == 'curl' && !function_exists( 'curl_init' ) ) {
178 throw new MWException( __METHOD__.': curl (http://php.net/curl) is not installed, but $wgHTTPEngine is set to "curl"' );
179 }
180
181 switch( $wgHTTPEngine ) {
182 case 'curl':
183 return new CurlHttpRequest( $url, $options );
184 case 'php':
185 if ( !wfIniGetBool( 'allow_url_fopen' ) ) {
186 throw new MWException( __METHOD__.': allow_url_fopen needs to be enabled for pure PHP http requests to work. '.
187 'If possible, curl should be used instead. See http://php.net/curl.' );
188 }
189 return new PhpHttpRequest( $url, $options );
190 default:
191 throw new MWException( __METHOD__.': The setting of $wgHTTPEngine is not valid.' );
192 }
193 }
194
195 /**
196 * Get the body, or content, of the response to the request
197 * @return string
198 */
199 public function getContent() {
200 return $this->content;
201 }
202
203 /**
204 * Take care of setting up the proxy
205 * (override in subclass)
206 * @return string
207 */
208 public function proxySetup() {
209 global $wgHTTPProxy;
210
211
212 if ( $this->proxy ) {
213 return;
214 }
215 if ( Http::isLocalURL( $this->url ) ) {
216 $this->proxy = 'http://localhost:80/';
217 } elseif ( $wgHTTPProxy ) {
218 $this->proxy = $wgHTTPProxy ;
219 }
220 }
221
222 /**
223 * Set the refererer header
224 */
225 public function setReferer( $url ) {
226 $this->setHeader('Referer', $url);
227 }
228
229 /**
230 * Set the user agent
231 */
232 public function setUserAgent( $UA ) {
233 $this->setHeader('User-Agent', $UA);
234 }
235
236 /**
237 * Set an arbitrary header
238 */
239 public function setHeader($name, $value) {
240 // I feel like I should normalize the case here...
241 $this->reqHeaders[$name] = $value;
242 }
243
244 /**
245 * Get an array of the headers
246 */
247 public function getHeaderList() {
248 $list = array();
249
250 foreach($this->reqHeaders as $name => $value) {
251 $list[] = "$name: $value";
252 }
253 return $list;
254 }
255
256 /**
257 * Set the callback
258 * @param $callback callback
259 */
260 public function setCallback( $callback ) {
261 $this->callback = $callback;
262 }
263
264 /**
265 * A generic callback to read in the response from a remote server
266 * @param $fh handle
267 * @param $content string
268 */
269 public function read( $fh, $content ) {
270 $this->content .= $content;
271 return strlen( $content );
272 }
273
274 /**
275 * Take care of whatever is necessary to perform the URI request.
276 * @return Status
277 */
278 public function execute() {
279 global $wgTitle;
280
281 if( strtoupper($this->method) == "HEAD" ) {
282 $this->headersOnly = true;
283 }
284
285 if ( is_array( $this->postData ) ) {
286 $this->postData = wfArrayToCGI( $this->postData );
287 }
288
289 if ( is_object( $wgTitle ) && !isset($this->reqHeaders['Referer']) ) {
290 $this->setReferer( $wgTitle->getFullURL() );
291 }
292
293 if ( !$this->noProxy ) {
294 $this->proxySetup();
295 }
296
297 if ( !$this->callback ) {
298 $this->setCallback( array( $this, 'read' ) );
299 }
300
301 if ( !isset($this->reqHeaders['User-Agent']) ) {
302 $this->setUserAgent(Http::userAgent());
303 }
304 }
305 }
306
307 /**
308 * HttpRequest implemented using internal curl compiled into PHP
309 */
310 class CurlHttpRequest extends HttpRequest {
311 protected $curlOptions = array();
312
313 public function execute() {
314 parent::execute();
315 if ( !$this->status->isOK() ) {
316 return $this->status;
317 }
318
319 // A lot of the action up front should probably be in
320 // set* methods, but we'll leave that for another time.
321
322 $this->curlOptions[CURLOPT_PROXY] = $this->proxy;
323 $this->curlOptions[CURLOPT_TIMEOUT] = $this->timeout;
324 $this->curlOptions[CURLOPT_HTTP_VERSION] = CURL_HTTP_VERSION_1_0;
325 $this->curlOptions[CURLOPT_WRITEFUNCTION] = $this->callback;
326
327 /* not sure these two are actually necessary */
328 if(isset($this->reqHeaders['Referer'])) {
329 $this->curlOptions[CURLOPT_REFERER] = $this->reqHeaders['Referer'];
330 }
331 $this->curlOptions[CURLOPT_USERAGENT] = $this->reqHeaders['User-Agent'];
332
333 if ( $this->sslVerifyHost ) {
334 $this->curlOptions[CURLOPT_SSL_VERIFYHOST] = $this->sslVerifyHost;
335 }
336
337 if ( $this->caInfo ) {
338 $this->curlOptions[CURLOPT_CAINFO] = $this->caInfo;
339 }
340
341 if ( $this->headersOnly ) {
342 $this->curlOptions[CURLOPT_NOBODY] = true;
343 $this->curlOptions[CURLOPT_HEADER] = true;
344 } elseif ( $this->method == 'POST' ) {
345 $this->curlOptions[CURLOPT_POST] = true;
346 $this->curlOptions[CURLOPT_POSTFIELDS] = $this->postData;
347 // Suppress 'Expect: 100-continue' header, as some servers
348 // will reject it with a 417 and Curl won't auto retry
349 // with HTTP 1.0 fallback
350 $this->reqHeaders['Expect'] = '';
351 } else {
352 $this->curlOptions[CURLOPT_CUSTOMREQUEST] = $this->method;
353 }
354
355 $this->curlOptions[CURLOPT_HTTPHEADER] = $this->getHeaderList();
356
357 $curlHandle = curl_init( $this->url );
358 curl_setopt_array( $curlHandle, $this->curlOptions );
359
360 if ( false === curl_exec( $curlHandle ) ) {
361 // re-using already translated error messages
362 $this->status->fatal( 'upload-curl-error'.curl_errno( $curlHandle ).'-text' );
363 }
364
365 curl_close( $curlHandle );
366
367 return $this->status;
368 }
369 }
370
371 class PhpHttpRequest extends HttpRequest {
372 private $fh;
373
374 protected function urlToTcp( $url ) {
375 $parsedUrl = parse_url( $url );
376
377 return 'tcp://' . $parsedUrl['host'] . ':' . $parsedUrl['port'];
378 }
379
380 public function execute() {
381 if ( $this->parsedUrl['scheme'] != 'http' ) {
382 $this->status->fatal( 'http-invalid-scheme', $this->parsedURL['scheme'] );
383 }
384
385 parent::execute();
386 if ( !$this->status->isOK() ) {
387 return $this->status;
388 }
389
390 // A lot of the action up front should probably be in
391 // set* methods, but we'll leave that for another time.
392
393 $this->reqHeaders['Accept'] = "*/*";
394 if ( $this->method == 'POST' ) {
395 // Required for HTTP 1.0 POSTs
396 $this->reqHeaders['Content-Length'] = strlen( $this->postData );
397 $this->reqHeaders['Content-type'] = "application/x-www-form-urlencoded";
398 }
399
400 $options = array();
401 if ( $this->proxy && !$this->noProxy ) {
402 $options['proxy'] = $this->urlToTCP( $this->proxy );
403 $options['request_fulluri'] = true;
404 }
405
406 $options['method'] = $this->method;
407 $options['timeout'] = $this->timeout;
408 $options['header'] = implode("\r\n", $this->getHeaderList());
409 // FOR NOW: Force everyone to HTTP 1.0
410 /* if ( version_compare( "5.3.0", phpversion(), ">" ) ) { */
411 $options['protocol_version'] = "1.0";
412 /* } else { */
413 /* $options['protocol_version'] = "1.1"; */
414 /* } */
415
416 if ( $this->postData ) {
417 $options['content'] = $this->postData;
418 }
419
420 $context = stream_context_create( array( 'http' => $options ) );
421 try {
422 $this->fh = fopen( $this->url, "r", false, $context );
423 } catch ( Exception $e ) {
424 $this->status->fatal( $e->getMessage() ); /* need some l10n help */
425 return $this->status;
426 }
427
428 $result = stream_get_meta_data( $this->fh );
429 if ( $result['timed_out'] ) {
430 $this->status->fatal( 'http-timed-out', $this->url );
431 return $this->status;
432 }
433
434 $this->headers = $result['wrapper_data'];
435
436 $end = false;
437 while ( !$end ) {
438 $contents = fread( $this->fh, 8192 );
439 $size = 0;
440 if ( $contents ) {
441 $size = call_user_func_array( $this->callback, array( $this->fh, $contents ) );
442 }
443 $end = ( $size == 0 ) || feof( $this->fh );
444 }
445 fclose( $this->fh );
446
447 return $this->status;
448 }
449 }