Add domain checking to HTTP cookie checking.
[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'] =
261 $this->cookieJar->serializeToHttpRequest($this->parsedURL['path'],
262 $this->parsedURL['host']);
263 }
264 foreach($this->reqHeaders as $name => $value) {
265 $list[] = "$name: $value";
266 }
267 return $list;
268 }
269
270 /**
271 * Set the callback
272 * @param $callback callback
273 */
274 public function setCallback( $callback ) {
275 $this->callback = $callback;
276 }
277
278 /**
279 * A generic callback to read the body of the response from a remote
280 * server.
281 * @param $fh handle
282 * @param $content string
283 */
284 public function read( $fh, $content ) {
285 $this->content .= $content;
286 return strlen( $content );
287 }
288
289 /**
290 * Take care of whatever is necessary to perform the URI request.
291 * @return Status
292 */
293 public function execute() {
294 global $wgTitle;
295
296 if( strtoupper($this->method) == "HEAD" ) {
297 $this->headersOnly = true;
298 }
299
300 if ( is_array( $this->postData ) ) {
301 $this->postData = wfArrayToCGI( $this->postData );
302 }
303
304 if ( is_object( $wgTitle ) && !isset($this->reqHeaders['Referer']) ) {
305 $this->setReferer( $wgTitle->getFullURL() );
306 }
307
308 if ( !$this->noProxy ) {
309 $this->proxySetup();
310 }
311
312 if ( !$this->callback ) {
313 $this->setCallback( array( $this, 'read' ) );
314 }
315
316 if ( !isset($this->reqHeaders['User-Agent']) ) {
317 $this->setUserAgent(Http::userAgent());
318 }
319 }
320
321 protected function parseHeader() {
322 $lastname = "";
323 foreach( $this->headerList as $header ) {
324 if( preg_match( "#^HTTP/([0-9.]+) (.*)#", $header, $match ) ) {
325 $this->respVersion = $match[1];
326 $this->respStatus = $match[2];
327 } elseif( preg_match( "#^[ \t]#", $header ) ) {
328 $last = count($this->respHeaders[$lastname]) - 1;
329 $this->respHeaders[$lastname][$last] .= "\r\n$header";
330 } elseif( preg_match( "#^([^:]*):[\t ]*(.*)#", $header, $match ) ) {
331 $this->respHeaders[strtolower( $match[1] )][] = $match[2];
332 $lastname = strtolower( $match[1] );
333 }
334 }
335
336 $this->parseCookies();
337 }
338
339 /**
340 * Returns an associative array of response headers after the
341 * request has been executed. Because some headers
342 * (e.g. Set-Cookie) can appear more than once the, each value of
343 * the associative array is an array of the values given.
344 * @return array
345 */
346 public function getResponseHeaders() {
347 if( !$this->respHeaders ) {
348 $this->parseHeader();
349 }
350 return $this->respHeaders;
351 }
352
353 /**
354 * Tells the HttpRequest object to use this pre-loaded CookieJar.
355 * @param $jar CookieJar
356 */
357 public function setCookieJar( $jar ) {
358 $this->cookieJar = $jar;
359 }
360
361 /**
362 * Returns the cookie jar in use.
363 * @returns CookieJar
364 */
365 public function getCookieJar() {
366 if( !$this->respHeaders ) {
367 $this->parseHeader();
368 }
369 return $this->cookieJar;
370 }
371
372 /**
373 * Sets a cookie. Used before a request to set up any individual
374 * cookies. Used internally after a request to parse the
375 * Set-Cookie headers.
376 * @see Cookie::set
377 */
378 public function setCookie( $name, $value = null, $attr = null) {
379 if( !$this->cookieJar ) {
380 $this->cookieJar = new CookieJar;
381 }
382 $this->cookieJar->setCookie($name, $value, $attr);
383 }
384
385 /**
386 * Parse the cookies in the response headers and store them in the cookie jar.
387 */
388 protected function parseCookies() {
389 if( isset( $this->respHeaders['set-cookie'] ) ) {
390 if( !$this->cookieJar ) {
391 $this->cookieJar = new CookieJar;
392 }
393 $url = parse_url( $this->getFinalUrl() );
394 foreach( $this->respHeaders['set-cookie'] as $cookie ) {
395 $this->cookieJar->parseCookieResponseHeader( $cookie, $url['host'] );
396 }
397 }
398 }
399
400 /**
401 * Returns the final URL after all redirections.
402 * @returns string
403 */
404 public function getFinalUrl() {
405 $finalUrl = $this->url;
406 if ( isset( $this->respHeaders['location'] ) ) {
407 $redir = $this->respHeaders['location'];
408 $finalUrl = $redir[count($redir) - 1];
409 }
410
411 return $finalUrl;
412 }
413 }
414
415
416 class Cookie {
417 protected $name;
418 protected $value;
419 protected $expires;
420 protected $path;
421 protected $domain;
422 protected $isSessionKey = true;
423 // TO IMPLEMENT protected $secure
424 // TO IMPLEMENT? protected $maxAge (add onto expires)
425 // TO IMPLEMENT? protected $version
426 // TO IMPLEMENT? protected $comment
427
428 function __construct( $name, $value, $attr ) {
429 $this->name = $name;
430 $this->set( $value, $attr );
431 }
432
433 /**
434 * Sets a cookie. Used before a request to set up any individual
435 * cookies. Used internally after a request to parse the
436 * Set-Cookie headers.
437 * @param $name string the name of the cookie
438 * @param $value string the value of the cookie
439 * @param $attr array possible key/values:
440 * expires A date string
441 * path The path this cookie is used on
442 * domain Domain this cookie is used on
443 */
444 public function set( $value, $attr ) {
445 $this->value = $value;
446 if( isset( $attr['expires'] ) ) {
447 $this->isSessionKey = false;
448 $this->expires = strtotime( $attr['expires'] );
449 }
450 if( isset( $attr['path'] ) ) {
451 $this->path = $attr['path'];
452 } else {
453 $this->path = "/";
454 }
455 if( isset( $attr['domain'] ) ) {
456 $this->domain = $attr['domain'];
457 } else {
458 throw new MWException("You must specify a domain.");
459 }
460 }
461
462 /**
463 * Serialize the cookie jar into a format useful for HTTP Request headers.
464 * @param $path string the path that will be used. Required.
465 * @param $domain string the domain that will be used. Required.
466 * @return string
467 */
468 public function serializeToHttpRequest( $path, $domain ) {
469 $ret = "";
470
471 if( $this->canServeDomain( $domain )
472 && $this->canServePath( $path )
473 && $this->isUnExpired() ) {
474 $ret = $this->name ."=". $this->value;
475 }
476
477 return $ret;
478 }
479
480 protected function canServeDomain( $domain ) {
481 if( $this->domain && substr_compare( $domain, $this->domain, -strlen( $this->domain ),
482 strlen( $this->domain ), TRUE ) == 0 ) {
483 return true;
484 }
485 return false;
486 }
487
488 protected function canServePath( $path ) {
489 if( $this->path && substr_compare( $this->path, $path, 0, strlen( $this->path ) ) == 0 ) {
490 return true;
491 }
492 return false;
493 }
494
495 protected function isUnExpired() {
496 if( $this->isSessionKey || $this->expires > time() ) {
497 return true;
498 }
499 return false;
500 }
501
502 }
503
504 class CookieJar {
505 private $cookie = array();
506
507 /**
508 * Set a cookie in the cookie jar. Make sure only one cookie per-name exists.
509 * @see Cookie::set()
510 */
511 public function setCookie ($name, $value, $attr) {
512 /* cookies: case insensitive, so this should work.
513 * We'll still send the cookies back in the same case we got them, though.
514 */
515 $index = strtoupper($name);
516 if( isset( $this->cookie[$index] ) ) {
517 $this->cookie[$index]->set( $value, $attr );
518 } else {
519 $this->cookie[$index] = new Cookie( $name, $value, $attr );
520 }
521 }
522
523 /**
524 * @see Cookie::serializeToHttpRequest
525 */
526 public function serializeToHttpRequest( $path, $domain ) {
527 $cookies = array();
528
529 foreach( $this->cookie as $c ) {
530 $serialized = $c->serializeToHttpRequest( $path, $domain );
531 if ( $serialized ) $cookies[] = $serialized;
532 }
533
534 return implode("; ", $cookies);
535 }
536
537 /**
538 * Parse the content of an Set-Cookie HTTP Response header.
539 * @param $cookie string
540 */
541 public function parseCookieResponseHeader ( $cookie, $domain ) {
542 $len = strlen( "Set-Cookie:" );
543 if ( substr_compare( "Set-Cookie:", $cookie, 0, $len, TRUE ) === 0 ) {
544 $cookie = substr( $cookie, $len );
545 }
546
547 $bit = array_map( 'trim', explode( ";", $cookie ) );
548 if ( count($bit) >= 1 ) {
549 list($name, $value) = explode( "=", array_shift( $bit ), 2 );
550 $attr = array();
551 foreach( $bit as $piece ) {
552 $parts = explode( "=", $piece );
553 if( count( $parts ) > 1 ) {
554 $attr[strtolower( $parts[0] )] = $parts[1];
555 } else {
556 $attr[strtolower( $parts[0] )] = true;
557 }
558 }
559
560 if( !isset( $attr['domain'] ) ) {
561 $attr['domain'] = $domain;
562 } else {
563 /* If domain is given, it has to contain at least two dots */
564 if ( strrpos( $attr['domain'], '.' ) === false
565 || strrpos( $attr['domain'], '.' ) === strpos( $attr['domain'], '.' ) ) {
566 return;
567 }
568 if ( substr( $attr['domain'], 0, 1 ) === '.' ) {
569 $attr['domain'] = substr( $attr['domain'], 1 );
570 }
571 if ( strlen( $attr['domain'] ) < strlen( $domain )
572 && substr_compare( $domain, $attr['domain'], -strlen( $attr['domain'] ),
573 strlen( $attr['domain'] ), TRUE ) != 0 ) {
574 return; /* silently reject a bad cookie */
575 }
576 }
577 $this->setCookie( $name, $value, $attr );
578 }
579 }
580 }
581
582
583 /**
584 * HttpRequest implemented using internal curl compiled into PHP
585 */
586 class CurlHttpRequest extends HttpRequest {
587 static $curlMessageMap = array(
588 6 => 'http-host-unreachable',
589 28 => 'http-timed-out'
590 );
591
592 protected $curlOptions = array();
593 protected $headerText = "";
594
595 protected function readHeader( $fh, $content ) {
596 $this->headerText .= $content;
597 return strlen( $content );
598 }
599
600 public function execute() {
601 parent::execute();
602 if ( !$this->status->isOK() ) {
603 return $this->status;
604 }
605 $this->curlOptions[CURLOPT_PROXY] = $this->proxy;
606 $this->curlOptions[CURLOPT_TIMEOUT] = $this->timeout;
607 $this->curlOptions[CURLOPT_HTTP_VERSION] = CURL_HTTP_VERSION_1_0;
608 $this->curlOptions[CURLOPT_WRITEFUNCTION] = $this->callback;
609 $this->curlOptions[CURLOPT_HEADERFUNCTION] = array($this, "readHeader");
610 $this->curlOptions[CURLOPT_FOLLOWLOCATION] = $this->followRedirects;
611 $this->curlOptions[CURLOPT_MAXREDIRS] = $this->maxRedirects;
612
613 /* not sure these two are actually necessary */
614 if(isset($this->reqHeaders['Referer'])) {
615 $this->curlOptions[CURLOPT_REFERER] = $this->reqHeaders['Referer'];
616 }
617 $this->curlOptions[CURLOPT_USERAGENT] = $this->reqHeaders['User-Agent'];
618
619 if ( $this->sslVerifyHost ) {
620 $this->curlOptions[CURLOPT_SSL_VERIFYHOST] = $this->sslVerifyHost;
621 }
622
623 if ( $this->caInfo ) {
624 $this->curlOptions[CURLOPT_CAINFO] = $this->caInfo;
625 }
626
627 if ( $this->headersOnly ) {
628 $this->curlOptions[CURLOPT_NOBODY] = true;
629 $this->curlOptions[CURLOPT_HEADER] = true;
630 } elseif ( $this->method == 'POST' ) {
631 $this->curlOptions[CURLOPT_POST] = true;
632 $this->curlOptions[CURLOPT_POSTFIELDS] = $this->postData;
633 // Suppress 'Expect: 100-continue' header, as some servers
634 // will reject it with a 417 and Curl won't auto retry
635 // with HTTP 1.0 fallback
636 $this->reqHeaders['Expect'] = '';
637 } else {
638 $this->curlOptions[CURLOPT_CUSTOMREQUEST] = $this->method;
639 }
640
641 $this->curlOptions[CURLOPT_HTTPHEADER] = $this->getHeaderList();
642
643 $curlHandle = curl_init( $this->url );
644 curl_setopt_array( $curlHandle, $this->curlOptions );
645
646 if ( false === curl_exec( $curlHandle ) ) {
647 $code = curl_error( $curlHandle );
648
649 if ( isset( self::$curlMessageMap[$code] ) ) {
650 $this->status->fatal( self::$curlMessageMap[$code] );
651 } else {
652 $this->status->fatal( 'http-curl-error', curl_error( $curlHandle ) );
653 }
654 } else {
655 $this->headerList = explode("\r\n", $this->headerText);
656 }
657
658 curl_close( $curlHandle );
659
660 return $this->status;
661 }
662 }
663
664 class PhpHttpRequest extends HttpRequest {
665 protected function urlToTcp( $url ) {
666 $parsedUrl = parse_url( $url );
667
668 return 'tcp://' . $parsedUrl['host'] . ':' . $parsedUrl['port'];
669 }
670
671 public function execute() {
672 if ( $this->parsedUrl['scheme'] != 'http' ) {
673 $this->status->fatal( 'http-invalid-scheme', $this->parsedURL['scheme'] );
674 }
675
676 parent::execute();
677 if ( !$this->status->isOK() ) {
678 return $this->status;
679 }
680
681 $this->reqHeaders['Accept'] = "*/*";
682 if ( $this->method == 'POST' ) {
683 // Required for HTTP 1.0 POSTs
684 $this->reqHeaders['Content-Length'] = strlen( $this->postData );
685 $this->reqHeaders['Content-type'] = "application/x-www-form-urlencoded";
686 }
687
688 $options = array();
689 if ( $this->proxy && !$this->noProxy ) {
690 $options['proxy'] = $this->urlToTCP( $this->proxy );
691 $options['request_fulluri'] = true;
692 }
693
694 if ( !$this->followRedirects ) {
695 $options['max_redirects'] = 0;
696 } else {
697 $options['max_redirects'] = $this->maxRedirects;
698 }
699
700 $options['method'] = $this->method;
701 $options['timeout'] = $this->timeout;
702 $options['header'] = implode("\r\n", $this->getHeaderList());
703 // Note that at some future point we may want to support
704 // HTTP/1.1, but we'd have to write support for chunking
705 // in version of PHP < 5.3.1
706 $options['protocol_version'] = "1.0";
707
708 if ( $this->postData ) {
709 $options['content'] = $this->postData;
710 }
711
712 $oldTimeout = false;
713 if ( version_compare( '5.2.1', phpversion(), '>' ) ) {
714 $oldTimeout = ini_set('default_socket_timeout', $this->timeout);
715 }
716
717 $context = stream_context_create( array( 'http' => $options ) );
718 wfSuppressWarnings();
719 $fh = fopen( $this->url, "r", false, $context );
720 wfRestoreWarnings();
721 if ( $oldTimeout !== false ) {
722 ini_set('default_socket_timeout', $oldTimeout);
723 }
724 if ( $fh === false ) {
725 $this->status->fatal( 'http-request-error' );
726 return $this->status;
727 }
728
729 $result = stream_get_meta_data( $fh );
730 if ( $result['timed_out'] ) {
731 $this->status->fatal( 'http-timed-out', $this->url );
732 return $this->status;
733 }
734 $this->headerList = $result['wrapper_data'];
735
736 while ( !feof( $fh ) ) {
737 $buf = fread( $fh, 8192 );
738 if ( $buf === false ) {
739 $this->status->fatal( 'http-read-error' );
740 break;
741 }
742 if ( strlen( $buf ) ) {
743 call_user_func( $this->callback, $fh, $buf );
744 }
745 }
746 fclose( $fh );
747
748 return $this->status;
749 }
750 }