* Fix for r57997 and bug 21222: move math, gallery, pre and nowiki to a new module...
[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 static $httpEngine = false;
12
13 /**
14 * Perform an HTTP request
15 * @param $method string HTTP method. Usually GET/POST
16 * @param $url string Full URL to act on
17 * @param $options options to pass to HttpRequest object
18 * Possible keys for the array:
19 * timeout Timeout length in seconds
20 * postData An array of key-value pairs or a url-encoded form data
21 * proxy The proxy to use. Will use $wgHTTPProxy (if set) otherwise.
22 * noProxy Override $wgHTTPProxy (if set) and don't use any proxy at all.
23 * sslVerifyHost (curl only) Verify the SSL certificate
24 * caInfo (curl only) Provide CA information
25 * maxRedirects Maximum number of redirects to follow (defaults to 5)
26 * followRedirects Whether to follow redirects (defaults to true)
27 * @returns mixed (bool)false on failure or a string on success
28 */
29 public static function request( $method, $url, $options = array() ) {
30 wfDebug( "HTTP: $method: $url" );
31 $options['method'] = strtoupper( $method );
32 if ( !isset( $options['timeout'] ) ) {
33 $options['timeout'] = 'default';
34 }
35 $req = HttpRequest::factory( $url, $options );
36 $status = $req->execute();
37 if ( $status->isOK() ) {
38 return $req->getContent();
39 } else {
40 return false;
41 }
42 }
43
44 /**
45 * Simple wrapper for Http::request( 'GET' )
46 * @see Http::request()
47 */
48 public static function get( $url, $timeout = 'default', $options = array() ) {
49 $options['timeout'] = $timeout;
50 return Http::request( 'GET', $url, $options );
51 }
52
53 /**
54 * Simple wrapper for Http::request( 'POST' )
55 * @see Http::request()
56 */
57 public static function post( $url, $options = array() ) {
58 return Http::request( 'POST', $url, $options );
59 }
60
61 /**
62 * Check if the URL can be served by localhost
63 * @param $url string Full url to check
64 * @return bool
65 */
66 public static function isLocalURL( $url ) {
67 global $wgCommandLineMode, $wgConf;
68 if ( $wgCommandLineMode ) {
69 return false;
70 }
71
72 // Extract host part
73 $matches = array();
74 if ( preg_match( '!^http://([\w.-]+)[/:].*$!', $url, $matches ) ) {
75 $host = $matches[1];
76 // Split up dotwise
77 $domainParts = explode( '.', $host );
78 // Check if this domain or any superdomain is listed in $wgConf as a local virtual host
79 $domainParts = array_reverse( $domainParts );
80 for ( $i = 0; $i < count( $domainParts ); $i++ ) {
81 $domainPart = $domainParts[$i];
82 if ( $i == 0 ) {
83 $domain = $domainPart;
84 } else {
85 $domain = $domainPart . '.' . $domain;
86 }
87 if ( $wgConf->isLocalVHost( $domain ) ) {
88 return true;
89 }
90 }
91 }
92 return false;
93 }
94
95 /**
96 * A standard user-agent we can use for external requests.
97 * @returns string
98 */
99 public static function userAgent() {
100 global $wgVersion;
101 return "MediaWiki/$wgVersion";
102 }
103
104 /**
105 * Checks that the given URI is a valid one
106 * @param $uri Mixed: URI to check for validity
107 * @returns bool
108 */
109 public static function isValidURI( $uri ) {
110 return preg_match(
111 '/(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/',
112 $uri,
113 $matches
114 );
115 }
116 }
117
118 /**
119 * This wrapper class will call out to curl (if available) or fallback
120 * to regular PHP if necessary for handling internal HTTP requests.
121 */
122 class HttpRequest {
123 protected $content;
124 protected $timeout = 'default';
125 protected $headersOnly = null;
126 protected $postData = null;
127 protected $proxy = null;
128 protected $noProxy = false;
129 protected $sslVerifyHost = true;
130 protected $caInfo = null;
131 protected $method = "GET";
132 protected $reqHeaders = array();
133 protected $url;
134 protected $parsedUrl;
135 protected $callback;
136 protected $maxRedirects = 5;
137 protected $followRedirects = true;
138
139 protected $cookieJar;
140
141 protected $headerList = array();
142 protected $respVersion = "0.9";
143 protected $respStatus = "0.1";
144 protected $respHeaders = array();
145
146 public $status;
147
148 /**
149 * @param $url string url to use
150 * @param $options array (optional) extra params to pass (see Http::request())
151 */
152 function __construct( $url, $options = array() ) {
153 global $wgHTTPTimeout;
154
155 $this->url = $url;
156 $this->parsedUrl = parse_url( $url );
157
158 if ( !Http::isValidURI( $this->url ) ) {
159 $this->status = Status::newFromFatal('http-invalid-url');
160 } else {
161 $this->status = Status::newGood( 100 ); // continue
162 }
163
164 if ( isset($options['timeout']) && $options['timeout'] != 'default' ) {
165 $this->timeout = $options['timeout'];
166 } else {
167 $this->timeout = $wgHTTPTimeout;
168 }
169
170 $members = array( "postData", "proxy", "noProxy", "sslVerifyHost", "caInfo",
171 "method", "followRedirects", "maxRedirects" );
172 foreach ( $members as $o ) {
173 if ( isset($options[$o]) ) {
174 $this->$o = $options[$o];
175 }
176 }
177 }
178
179 /**
180 * Generate a new request object
181 * @see HttpRequest::__construct
182 */
183 public static function factory( $url, $options = null ) {
184 if ( !Http::$httpEngine ) {
185 Http::$httpEngine = function_exists( 'curl_init' ) ? 'curl' : 'php';
186 } elseif ( Http::$httpEngine == 'curl' && !function_exists( 'curl_init' ) ) {
187 throw new MWException( __METHOD__.': curl (http://php.net/curl) is not installed, but'.
188 ' Http::$httpEngine is set to "curl"' );
189 }
190
191 switch( Http::$httpEngine ) {
192 case 'curl':
193 return new CurlHttpRequest( $url, $options );
194 case 'php':
195 if ( !wfIniGetBool( 'allow_url_fopen' ) ) {
196 throw new MWException( __METHOD__.': allow_url_fopen needs to be enabled for pure PHP'.
197 ' http requests to work. If possible, curl should be used instead. See http://php.net/curl.' );
198 }
199 return new PhpHttpRequest( $url, $options );
200 default:
201 throw new MWException( __METHOD__.': The setting of Http::$httpEngine is not valid.' );
202 }
203 }
204
205 /**
206 * Get the body, or content, of the response to the request
207 * @return string
208 */
209 public function getContent() {
210 return $this->content;
211 }
212
213 /**
214 * Take care of setting up the proxy
215 * (override in subclass)
216 * @return string
217 */
218 public function proxySetup() {
219 global $wgHTTPProxy;
220
221 if ( $this->proxy ) {
222 return;
223 }
224 if ( Http::isLocalURL( $this->url ) ) {
225 $this->proxy = 'http://localhost:80/';
226 } elseif ( $wgHTTPProxy ) {
227 $this->proxy = $wgHTTPProxy ;
228 }
229 }
230
231 /**
232 * Set the refererer header
233 */
234 public function setReferer( $url ) {
235 $this->setHeader('Referer', $url);
236 }
237
238 /**
239 * Set the user agent
240 */
241 public function setUserAgent( $UA ) {
242 $this->setHeader('User-Agent', $UA);
243 }
244
245 /**
246 * Set an arbitrary header
247 */
248 public function setHeader($name, $value) {
249 // I feel like I should normalize the case here...
250 $this->reqHeaders[$name] = $value;
251 }
252
253 /**
254 * Get an array of the headers
255 */
256 public function getHeaderList() {
257 $list = array();
258
259 if( $this->cookieJar ) {
260 $this->reqHeaders['Cookie'] = $this->cookieJar->serializeToHttpRequest();
261 }
262 foreach($this->reqHeaders as $name => $value) {
263 $list[] = "$name: $value";
264 }
265 return $list;
266 }
267
268 /**
269 * Set the callback
270 * @param $callback callback
271 */
272 public function setCallback( $callback ) {
273 $this->callback = $callback;
274 }
275
276 /**
277 * A generic callback to read the body of the response from a remote
278 * server.
279 * @param $fh handle
280 * @param $content string
281 */
282 public function read( $fh, $content ) {
283 $this->content .= $content;
284 return strlen( $content );
285 }
286
287 /**
288 * Take care of whatever is necessary to perform the URI request.
289 * @return Status
290 */
291 public function execute() {
292 global $wgTitle;
293
294 if( strtoupper($this->method) == "HEAD" ) {
295 $this->headersOnly = true;
296 }
297
298 if ( is_array( $this->postData ) ) {
299 $this->postData = wfArrayToCGI( $this->postData );
300 }
301
302 if ( is_object( $wgTitle ) && !isset($this->reqHeaders['Referer']) ) {
303 $this->setReferer( $wgTitle->getFullURL() );
304 }
305
306 if ( !$this->noProxy ) {
307 $this->proxySetup();
308 }
309
310 if ( !$this->callback ) {
311 $this->setCallback( array( $this, 'read' ) );
312 }
313
314 if ( !isset($this->reqHeaders['User-Agent']) ) {
315 $this->setUserAgent(Http::userAgent());
316 }
317 }
318
319 protected function parseHeader() {
320 $lastname = "";
321 foreach( $this->headerList as $header ) {
322 if( preg_match( "#^HTTP/([0-9.]+) (.*)#", $header, $match ) ) {
323 $this->respVersion = $match[1];
324 $this->respStatus = $match[2];
325 } elseif( preg_match( "#^[ \t]#", $header ) ) {
326 $last = count($this->respHeaders[$lastname]) - 1;
327 $this->respHeaders[$lastname][$last] .= "\r\n$header";
328 } elseif( preg_match( "#^([^:]*):[\t ]*(.*)#", $header, $match ) ) {
329 $this->respHeaders[strtolower( $match[1] )][] = $match[2];
330 $lastname = strtolower( $match[1] );
331 }
332 }
333
334 $this->parseCookies();
335 }
336
337 /**
338 * Returns an associative array of response headers after the
339 * request has been executed. Because some headers
340 * (e.g. Set-Cookie) can appear more than once the, each value of
341 * the associative array is an array of the values given.
342 * @return array
343 */
344 public function getResponseHeaders() {
345 if( !$this->respHeaders ) {
346 $this->parseHeader();
347 }
348 return $this->respHeaders;
349 }
350
351 /**
352 * Tells the HttpRequest object to use this pre-loaded CookieJar.
353 * @param $jar CookieJar
354 */
355 public function setCookieJar( $jar ) {
356 $this->cookieJar = $jar;
357 }
358
359 /**
360 * Returns the cookie jar in use.
361 * @returns CookieJar
362 */
363 public function getCookieJar() {
364 if( !$this->respHeaders ) {
365 $this->parseHeader();
366 }
367 return $this->cookieJar;
368 }
369
370 /**
371 * Sets a cookie. Used before a request to set up any individual
372 * cookies. Used internally after a request to parse the
373 * Set-Cookie headers.
374 * @see Cookie::set
375 */
376 public function setCookie( $name, $value = null, $attr = null) {
377 if( !$this->cookieJar ) {
378 $this->cookieJar = new CookieJar;
379 }
380 $this->cookieJar->setCookie($name, $value, $attr);
381 }
382
383 /**
384 * Parse the cookies in the response headers and store them in the cookie jar.
385 */
386 protected function parseCookies() {
387 if( isset( $this->respHeaders['set-cookie'] ) ) {
388 if( !$this->cookieJar ) {
389 $this->cookieJar = new CookieJar;
390 }
391 $url = parse_url( $this->getFinalUrl() );
392 foreach( $this->respHeaders['set-cookie'] as $cookie ) {
393 $this->cookieJar->parseCookieResponseHeader( $cookie, $url['host'] );
394 }
395 }
396 }
397
398 /**
399 * Returns the final URL after all redirections.
400 * @returns string
401 */
402 public function getFinalUrl() {
403 $finalUrl = $this->url;
404 if ( isset( $this->respHeaders['location'] ) ) {
405 $redir = $this->respHeaders['location'];
406 $finalUrl = $redir[count($redir) - 1];
407 }
408
409 return $finalUrl;
410 }
411 }
412
413
414 class Cookie {
415 protected $name;
416 protected $value;
417 protected $expires;
418 protected $path;
419 protected $domain;
420 protected $isSessionKey = true;
421 // TO IMPLEMENT protected $secure
422 // TO IMPLEMENT? protected $maxAge (add onto expires)
423 // TO IMPLEMENT? protected $version
424 // TO IMPLEMENT? protected $comment
425
426 function __construct( $name, $value, $attr ) {
427 $this->name = $name;
428 $this->set( $value, $attr );
429 }
430
431 /**
432 * Sets a cookie. Used before a request to set up any individual
433 * cookies. Used internally after a request to parse the
434 * Set-Cookie headers.
435 * @param $name string the name of the cookie
436 * @param $value string the value of the cookie
437 * @param $attr array possible key/values:
438 * expires A date string
439 * path The path this cookie is used on
440 * domain Domain this cookie is used on
441 */
442 public function set( $value, $attr ) {
443 $this->value = $value;
444 if( isset( $attr['expires'] ) ) {
445 $this->isSessionKey = false;
446 $this->expires = strtotime( $attr['expires'] );
447 }
448 if( isset( $attr['path'] ) ) {
449 $this->path = $attr['path'];
450 } else {
451 $this->path = "/";
452 }
453 if( isset( $attr['domain'] ) ) {
454 $this->domain = $attr['domain'];
455 } else {
456 throw new MWException("You must specify a domain.");
457 }
458 }
459
460 /**
461 * Serialize the cookie jar into a format useful for HTTP Request headers.
462 * @param $path string the path that will be used. Required.
463 * @param $domain string the domain that will be used. Required.
464 * @return string
465 */
466 public function serializeToHttpRequest( $path, $domain ) {
467 $ret = "";
468
469 if( $this->canServeDomain( $domain )
470 && $this->canServePath( $path )
471 && $this->isUnExpired() ) {
472 $ret = $this->name ."=". $this->value;
473 }
474
475 return $ret;
476 }
477
478 protected function canServeDomain( $domain ) {
479 if( $this->domain && substr_compare( $domain, $this->domain, -strlen( $this->domain ),
480 strlen( $this->domain ), TRUE ) == 0 ) {
481 return true;
482 }
483 return false;
484 }
485
486 protected function canServePath( $path ) {
487 if( $this->path && substr_compare( $this->path, $path, 0, strlen( $this->path ) ) == 0 ) {
488 return true;
489 }
490 return false;
491 }
492
493 protected function isUnExpired() {
494 if( $this->isSessionKey || $this->expires > time() ) {
495 return true;
496 }
497 return false;
498 }
499
500 }
501
502 class CookieJar {
503 private $cookie;
504
505 /**
506 * Set a cookie in the cookie jar. Make sure only one cookie per-name exists.
507 * @see Cookie::set()
508 */
509 public function setCookie ($name, $value, $attr) {
510 /* cookies: case insensitive, so this should work.
511 * We'll still send the cookies back in the same case we got them, though.
512 */
513 $index = strtoupper($name);
514 if( isset( $this->cookie[$index] ) ) {
515 $this->cookie[$index]->set( $value, $attr );
516 } else {
517 $this->cookie[$index] = new Cookie( $name, $value, $attr );
518 }
519 }
520
521 /**
522 * @see Cookie::serializeToHttpRequest
523 */
524 public function serializeToHttpRequest( $path, $domain ) {
525 $cookies = array();
526
527 foreach( $this->cookie as $c ) {
528 $serialized = $c->serializeToHttpRequest( $path, $domain );
529 if ( $serialized ) $cookies[] = $serialized;
530 }
531
532 return implode("; ", $cookies);
533 }
534
535 /**
536 * Parse the content of an Set-Cookie HTTP Response header.
537 * @param $cookie string
538 */
539 public function parseCookieResponseHeader ( $cookie, $domain = null ) {
540 $len = strlen( "Set-Cookie:" );
541 if ( substr_compare( "Set-Cookie:", $cookie, 0, $len, TRUE ) === 0 ) {
542 $cookie = substr( $cookie, $len );
543 }
544
545 $bit = array_map( 'trim', explode( ";", $cookie ) );
546 list($name, $value) = explode( "=", array_shift( $bit ), 2 );
547 $attr = array();
548 foreach( $bit as $piece ) {
549 $parts = explode( "=", $piece );
550 if( count( $parts ) > 1 ) {
551 $attr[strtolower( $parts[0] )] = $parts[1];
552 } else {
553 $attr[strtolower( $parts[0] )] = true;
554 }
555 }
556 $this->setCookie( $name, $value, $attr );
557 }
558 }
559
560
561 /**
562 * HttpRequest implemented using internal curl compiled into PHP
563 */
564 class CurlHttpRequest extends HttpRequest {
565 static $curlMessageMap = array(
566 6 => 'http-host-unreachable',
567 28 => 'http-timed-out'
568 );
569
570 protected $curlOptions = array();
571 protected $headerText = "";
572
573 protected function readHeader( $fh, $content ) {
574 $this->headerText .= $content;
575 return strlen( $content );
576 }
577
578 public function execute() {
579 parent::execute();
580 if ( !$this->status->isOK() ) {
581 return $this->status;
582 }
583 $this->curlOptions[CURLOPT_PROXY] = $this->proxy;
584 $this->curlOptions[CURLOPT_TIMEOUT] = $this->timeout;
585 $this->curlOptions[CURLOPT_HTTP_VERSION] = CURL_HTTP_VERSION_1_0;
586 $this->curlOptions[CURLOPT_WRITEFUNCTION] = $this->callback;
587 $this->curlOptions[CURLOPT_HEADERFUNCTION] = array($this, "readHeader");
588 $this->curlOptions[CURLOPT_FOLLOWLOCATION] = $this->followRedirects;
589 $this->curlOptions[CURLOPT_MAXREDIRS] = $this->maxRedirects;
590
591 /* not sure these two are actually necessary */
592 if(isset($this->reqHeaders['Referer'])) {
593 $this->curlOptions[CURLOPT_REFERER] = $this->reqHeaders['Referer'];
594 }
595 $this->curlOptions[CURLOPT_USERAGENT] = $this->reqHeaders['User-Agent'];
596
597 if ( $this->sslVerifyHost ) {
598 $this->curlOptions[CURLOPT_SSL_VERIFYHOST] = $this->sslVerifyHost;
599 }
600
601 if ( $this->caInfo ) {
602 $this->curlOptions[CURLOPT_CAINFO] = $this->caInfo;
603 }
604
605 if ( $this->headersOnly ) {
606 $this->curlOptions[CURLOPT_NOBODY] = true;
607 $this->curlOptions[CURLOPT_HEADER] = true;
608 } elseif ( $this->method == 'POST' ) {
609 $this->curlOptions[CURLOPT_POST] = true;
610 $this->curlOptions[CURLOPT_POSTFIELDS] = $this->postData;
611 // Suppress 'Expect: 100-continue' header, as some servers
612 // will reject it with a 417 and Curl won't auto retry
613 // with HTTP 1.0 fallback
614 $this->reqHeaders['Expect'] = '';
615 } else {
616 $this->curlOptions[CURLOPT_CUSTOMREQUEST] = $this->method;
617 }
618
619 $this->curlOptions[CURLOPT_HTTPHEADER] = $this->getHeaderList();
620
621 $curlHandle = curl_init( $this->url );
622 curl_setopt_array( $curlHandle, $this->curlOptions );
623
624 if ( false === curl_exec( $curlHandle ) ) {
625 $code = curl_error( $curlHandle );
626
627 if ( isset( self::$curlMessageMap[$code] ) ) {
628 $this->status->fatal( self::$curlMessageMap[$code] );
629 } else {
630 $this->status->fatal( 'http-curl-error', curl_error( $curlHandle ) );
631 }
632 } else {
633 $this->headerList = explode("\r\n", $this->headerText);
634 }
635
636 curl_close( $curlHandle );
637
638 return $this->status;
639 }
640 }
641
642 class PhpHttpRequest extends HttpRequest {
643 protected function urlToTcp( $url ) {
644 $parsedUrl = parse_url( $url );
645
646 return 'tcp://' . $parsedUrl['host'] . ':' . $parsedUrl['port'];
647 }
648
649 public function execute() {
650 if ( $this->parsedUrl['scheme'] != 'http' ) {
651 $this->status->fatal( 'http-invalid-scheme', $this->parsedURL['scheme'] );
652 }
653
654 parent::execute();
655 if ( !$this->status->isOK() ) {
656 return $this->status;
657 }
658
659 $this->reqHeaders['Accept'] = "*/*";
660 if ( $this->method == 'POST' ) {
661 // Required for HTTP 1.0 POSTs
662 $this->reqHeaders['Content-Length'] = strlen( $this->postData );
663 $this->reqHeaders['Content-type'] = "application/x-www-form-urlencoded";
664 }
665
666 $options = array();
667 if ( $this->proxy && !$this->noProxy ) {
668 $options['proxy'] = $this->urlToTCP( $this->proxy );
669 $options['request_fulluri'] = true;
670 }
671
672 if ( !$this->followRedirects ) {
673 $options['max_redirects'] = 0;
674 } else {
675 $options['max_redirects'] = $this->maxRedirects;
676 }
677
678 $options['method'] = $this->method;
679 $options['timeout'] = $this->timeout;
680 $options['header'] = implode("\r\n", $this->getHeaderList());
681 // Note that at some future point we may want to support
682 // HTTP/1.1, but we'd have to write support for chunking
683 // in version of PHP < 5.3.1
684 $options['protocol_version'] = "1.0";
685
686 if ( $this->postData ) {
687 $options['content'] = $this->postData;
688 }
689
690 $oldTimeout = false;
691 if ( version_compare( '5.2.1', phpversion(), '>' ) ) {
692 $oldTimeout = ini_set('default_socket_timeout', $this->timeout);
693 }
694
695 $context = stream_context_create( array( 'http' => $options ) );
696 wfSuppressWarnings();
697 $fh = fopen( $this->url, "r", false, $context );
698 wfRestoreWarnings();
699 if ( $oldTimeout !== false ) {
700 ini_set('default_socket_timeout', $oldTimeout);
701 }
702 if ( $fh === false ) {
703 $this->status->fatal( 'http-request-error' );
704 return $this->status;
705 }
706
707 $result = stream_get_meta_data( $fh );
708 if ( $result['timed_out'] ) {
709 $this->status->fatal( 'http-timed-out', $this->url );
710 return $this->status;
711 }
712 $this->headerList = $result['wrapper_data'];
713
714 while ( !feof( $fh ) ) {
715 $buf = fread( $fh, 8192 );
716 if ( $buf === false ) {
717 $this->status->fatal( 'http-read-error' );
718 break;
719 }
720 if ( strlen( $buf ) ) {
721 call_user_func( $this->callback, $fh, $buf );
722 }
723 }
724 fclose( $fh );
725
726 return $this->status;
727 }
728 }