(bug 27854) Http::isValidURI is way to lax. This is a much simplified regex that...
[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 *
16 * @param $method String: HTTP method. Usually GET/POST
17 * @param $url String: full URL to act on
18 * @param $options Array: options to pass to MWHttpRequest object.
19 * Possible keys for the array:
20 * - timeout Timeout length in seconds
21 * - postData An array of key-value pairs or a url-encoded form data
22 * - proxy The proxy to use.
23 * Will use $wgHTTPProxy (if set) otherwise.
24 * - noProxy Override $wgHTTPProxy (if set) and don't use any proxy at all.
25 * - sslVerifyHost (curl only) Verify hostname against certificate
26 * - sslVerifyCert (curl only) Verify SSL certificate
27 * - caInfo (curl only) Provide CA information
28 * - maxRedirects Maximum number of redirects to follow (defaults to 5)
29 * - followRedirects Whether to follow redirects (defaults to false).
30 * Note: this should only be used when the target URL is trusted,
31 * to avoid attacks on intranet services accessible by HTTP.
32 * @return Mixed: (bool)false on failure or a string on success
33 */
34 public static function request( $method, $url, $options = array() ) {
35 $url = wfExpandUrl( $url );
36 wfDebug( "HTTP: $method: $url\n" );
37 $options['method'] = strtoupper( $method );
38
39 if ( !isset( $options['timeout'] ) ) {
40 $options['timeout'] = 'default';
41 }
42
43 $req = MWHttpRequest::factory( $url, $options );
44 $status = $req->execute();
45
46 if ( $status->isOK() ) {
47 return $req->getContent();
48 } else {
49 return false;
50 }
51 }
52
53 /**
54 * Simple wrapper for Http::request( 'GET' )
55 * @see Http::request()
56 */
57 public static function get( $url, $timeout = 'default', $options = array() ) {
58 $options['timeout'] = $timeout;
59 return Http::request( 'GET', $url, $options );
60 }
61
62 /**
63 * Simple wrapper for Http::request( 'POST' )
64 * @see Http::request()
65 */
66 public static function post( $url, $options = array() ) {
67 return Http::request( 'POST', $url, $options );
68 }
69
70 /**
71 * Check if the URL can be served by localhost
72 *
73 * @param $url String: full url to check
74 * @return Boolean
75 */
76 public static function isLocalURL( $url ) {
77 global $wgCommandLineMode, $wgConf;
78
79 if ( $wgCommandLineMode ) {
80 return false;
81 }
82
83 // Extract host part
84 $matches = array();
85 if ( preg_match( '!^http://([\w.-]+)[/:].*$!', $url, $matches ) ) {
86 $host = $matches[1];
87 // Split up dotwise
88 $domainParts = explode( '.', $host );
89 // Check if this domain or any superdomain is listed in $wgConf as a local virtual host
90 $domainParts = array_reverse( $domainParts );
91
92 $domain = '';
93 for ( $i = 0; $i < count( $domainParts ); $i++ ) {
94 $domainPart = $domainParts[$i];
95 if ( $i == 0 ) {
96 $domain = $domainPart;
97 } else {
98 $domain = $domainPart . '.' . $domain;
99 }
100
101 if ( $wgConf->isLocalVHost( $domain ) ) {
102 return true;
103 }
104 }
105 }
106
107 return false;
108 }
109
110 /**
111 * A standard user-agent we can use for external requests.
112 * @return String
113 */
114 public static function userAgent() {
115 global $wgVersion;
116 return "MediaWiki/$wgVersion";
117 }
118
119 /**
120 * Checks that the given URI is a valid one. Hardcoding the
121 * protocols, because we only want protocols that both cURL
122 * and php support.
123 *
124 * @param $uri Mixed: URI to check for validity
125 * @returns Boolean
126 */
127 public static function isValidURI( $uri ) {
128 return preg_match(
129 '/^(f|ht)tps?:\/\/[^\/\s]\S*$/D',
130 $uri
131 );
132 }
133 }
134
135 /**
136 * This wrapper class will call out to curl (if available) or fallback
137 * to regular PHP if necessary for handling internal HTTP requests.
138 *
139 * Renamed from HttpRequest to MWHttpRequst to avoid conflict with
140 * php's HTTP extension.
141 */
142 class MWHttpRequest {
143 const SUPPORTS_FILE_POSTS = false;
144
145 protected $content;
146 protected $timeout = 'default';
147 protected $headersOnly = null;
148 protected $postData = null;
149 protected $proxy = null;
150 protected $noProxy = false;
151 protected $sslVerifyHost = true;
152 protected $sslVerifyCert = true;
153 protected $caInfo = null;
154 protected $method = "GET";
155 protected $reqHeaders = array();
156 protected $url;
157 protected $parsedUrl;
158 protected $callback;
159 protected $maxRedirects = 5;
160 protected $followRedirects = false;
161
162 /**
163 * @var CookieJar
164 */
165 protected $cookieJar;
166
167 protected $headerList = array();
168 protected $respVersion = "0.9";
169 protected $respStatus = "200 Ok";
170 protected $respHeaders = array();
171
172 public $status;
173
174 /**
175 * @param $url String: url to use
176 * @param $options Array: (optional) extra params to pass (see Http::request())
177 */
178 function __construct( $url, $options = array() ) {
179 global $wgHTTPTimeout;
180
181 $this->url = $url;
182 $this->parsedUrl = parse_url( $url );
183
184 if ( !Http::isValidURI( $this->url ) ) {
185 $this->status = Status::newFatal( 'http-invalid-url' );
186 } else {
187 $this->status = Status::newGood( 100 ); // continue
188 }
189
190 if ( isset( $options['timeout'] ) && $options['timeout'] != 'default' ) {
191 $this->timeout = $options['timeout'];
192 } else {
193 $this->timeout = $wgHTTPTimeout;
194 }
195
196 $members = array( "postData", "proxy", "noProxy", "sslVerifyHost", "caInfo",
197 "method", "followRedirects", "maxRedirects", "sslVerifyCert", "callback" );
198
199 foreach ( $members as $o ) {
200 if ( isset( $options[$o] ) ) {
201 $this->$o = $options[$o];
202 }
203 }
204 }
205
206 /**
207 * Generate a new request object
208 * @param $url String: url to use
209 * @param $options Array: (optional) extra params to pass (see Http::request())
210 * @see MWHttpRequest::__construct
211 */
212 public static function factory( $url, $options = null ) {
213 if ( !Http::$httpEngine ) {
214 Http::$httpEngine = function_exists( 'curl_init' ) ? 'curl' : 'php';
215 } elseif ( Http::$httpEngine == 'curl' && !function_exists( 'curl_init' ) ) {
216 throw new MWException( __METHOD__ . ': curl (http://php.net/curl) is not installed, but' .
217 ' Http::$httpEngine is set to "curl"' );
218 }
219
220 switch( Http::$httpEngine ) {
221 case 'curl':
222 return new CurlHttpRequest( $url, $options );
223 case 'php':
224 if ( !wfIniGetBool( 'allow_url_fopen' ) ) {
225 throw new MWException( __METHOD__ . ': allow_url_fopen needs to be enabled for pure PHP' .
226 ' http requests to work. If possible, curl should be used instead. See http://php.net/curl.' );
227 }
228 return new PhpHttpRequest( $url, $options );
229 default:
230 throw new MWException( __METHOD__ . ': The setting of Http::$httpEngine is not valid.' );
231 }
232 }
233
234 /**
235 * Get the body, or content, of the response to the request
236 *
237 * @return String
238 */
239 public function getContent() {
240 return $this->content;
241 }
242
243 /**
244 * Set the parameters of the request
245
246 * @param $args Array
247 * @todo overload the args param
248 */
249 public function setData( $args ) {
250 $this->postData = $args;
251 }
252
253 /**
254 * Take care of setting up the proxy
255 * (override in subclass)
256 *
257 * @return String
258 */
259 public function proxySetup() {
260 global $wgHTTPProxy;
261
262 if ( $this->proxy ) {
263 return;
264 }
265
266 if ( Http::isLocalURL( $this->url ) ) {
267 $this->proxy = 'http://localhost:80/';
268 } elseif ( $wgHTTPProxy ) {
269 $this->proxy = $wgHTTPProxy ;
270 } elseif ( getenv( "http_proxy" ) ) {
271 $this->proxy = getenv( "http_proxy" );
272 }
273 }
274
275 /**
276 * Set the refererer header
277 */
278 public function setReferer( $url ) {
279 $this->setHeader( 'Referer', $url );
280 }
281
282 /**
283 * Set the user agent
284 */
285 public function setUserAgent( $UA ) {
286 $this->setHeader( 'User-Agent', $UA );
287 }
288
289 /**
290 * Set an arbitrary header
291 */
292 public function setHeader( $name, $value ) {
293 // I feel like I should normalize the case here...
294 $this->reqHeaders[$name] = $value;
295 }
296
297 /**
298 * Get an array of the headers
299 */
300 public function getHeaderList() {
301 $list = array();
302
303 if ( $this->cookieJar ) {
304 $this->reqHeaders['Cookie'] =
305 $this->cookieJar->serializeToHttpRequest(
306 $this->parsedUrl['path'],
307 $this->parsedUrl['host']
308 );
309 }
310
311 foreach ( $this->reqHeaders as $name => $value ) {
312 $list[] = "$name: $value";
313 }
314
315 return $list;
316 }
317
318 /**
319 * Set the callback
320 *
321 * @param $callback Callback
322 */
323 public function setCallback( $callback ) {
324 $this->callback = $callback;
325 }
326
327 /**
328 * A generic callback to read the body of the response from a remote
329 * server.
330 *
331 * @param $fh handle
332 * @param $content String
333 */
334 public function read( $fh, $content ) {
335 $this->content .= $content;
336 return strlen( $content );
337 }
338
339 /**
340 * Take care of whatever is necessary to perform the URI request.
341 *
342 * @return Status
343 */
344 public function execute() {
345 global $wgTitle;
346
347 $this->content = "";
348
349 if ( strtoupper( $this->method ) == "HEAD" ) {
350 $this->headersOnly = true;
351 }
352
353 if ( is_object( $wgTitle ) && !isset( $this->reqHeaders['Referer'] ) ) {
354 $this->setReferer( $wgTitle->getFullURL() );
355 }
356
357 if ( !$this->noProxy ) {
358 $this->proxySetup();
359 }
360
361 if ( !$this->callback ) {
362 $this->setCallback( array( $this, 'read' ) );
363 }
364
365 if ( !isset( $this->reqHeaders['User-Agent'] ) ) {
366 $this->setUserAgent( Http::userAgent() );
367 }
368 }
369
370 /**
371 * Parses the headers, including the HTTP status code and any
372 * Set-Cookie headers. This function expectes the headers to be
373 * found in an array in the member variable headerList.
374 *
375 * @return nothing
376 */
377 protected function parseHeader() {
378 $lastname = "";
379
380 foreach ( $this->headerList as $header ) {
381 if ( preg_match( "#^HTTP/([0-9.]+) (.*)#", $header, $match ) ) {
382 $this->respVersion = $match[1];
383 $this->respStatus = $match[2];
384 } elseif ( preg_match( "#^[ \t]#", $header ) ) {
385 $last = count( $this->respHeaders[$lastname] ) - 1;
386 $this->respHeaders[$lastname][$last] .= "\r\n$header";
387 } elseif ( preg_match( "#^([^:]*):[\t ]*(.*)#", $header, $match ) ) {
388 $this->respHeaders[strtolower( $match[1] )][] = $match[2];
389 $lastname = strtolower( $match[1] );
390 }
391 }
392
393 $this->parseCookies();
394 }
395
396 /**
397 * Sets HTTPRequest status member to a fatal value with the error
398 * message if the returned integer value of the status code was
399 * not successful (< 300) or a redirect (>=300 and < 400). (see
400 * RFC2616, section 10,
401 * http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html for a
402 * list of status codes.)
403 *
404 * @return nothing
405 */
406 protected function setStatus() {
407 if ( !$this->respHeaders ) {
408 $this->parseHeader();
409 }
410
411 if ( (int)$this->respStatus > 399 ) {
412 list( $code, $message ) = explode( " ", $this->respStatus, 2 );
413 $this->status->fatal( "http-bad-status", $code, $message );
414 }
415 }
416
417 /**
418 * Get the integer value of the HTTP status code (e.g. 200 for "200 Ok")
419 * (see RFC2616, section 10, http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
420 * for a list of status codes.)
421 *
422 * @return Integer
423 */
424 public function getStatus() {
425 if ( !$this->respHeaders ) {
426 $this->parseHeader();
427 }
428
429 return (int)$this->respStatus;
430 }
431
432
433 /**
434 * Returns true if the last status code was a redirect.
435 *
436 * @return Boolean
437 */
438 public function isRedirect() {
439 if ( !$this->respHeaders ) {
440 $this->parseHeader();
441 }
442
443 $status = (int)$this->respStatus;
444
445 if ( $status >= 300 && $status <= 303 ) {
446 return true;
447 }
448
449 return false;
450 }
451
452 /**
453 * Returns an associative array of response headers after the
454 * request has been executed. Because some headers
455 * (e.g. Set-Cookie) can appear more than once the, each value of
456 * the associative array is an array of the values given.
457 *
458 * @return Array
459 */
460 public function getResponseHeaders() {
461 if ( !$this->respHeaders ) {
462 $this->parseHeader();
463 }
464
465 return $this->respHeaders;
466 }
467
468 /**
469 * Returns the value of the given response header.
470 *
471 * @param $header String
472 * @return String
473 */
474 public function getResponseHeader( $header ) {
475 if ( !$this->respHeaders ) {
476 $this->parseHeader();
477 }
478
479 if ( isset( $this->respHeaders[strtolower ( $header ) ] ) ) {
480 $v = $this->respHeaders[strtolower ( $header ) ];
481 return $v[count( $v ) - 1];
482 }
483
484 return null;
485 }
486
487 /**
488 * Tells the MWHttpRequest object to use this pre-loaded CookieJar.
489 *
490 * @param $jar CookieJar
491 */
492 public function setCookieJar( $jar ) {
493 $this->cookieJar = $jar;
494 }
495
496 /**
497 * Returns the cookie jar in use.
498 *
499 * @returns CookieJar
500 */
501 public function getCookieJar() {
502 if ( !$this->respHeaders ) {
503 $this->parseHeader();
504 }
505
506 return $this->cookieJar;
507 }
508
509 /**
510 * Sets a cookie. Used before a request to set up any individual
511 * cookies. Used internally after a request to parse the
512 * Set-Cookie headers.
513 * @see Cookie::set
514 */
515 public function setCookie( $name, $value = null, $attr = null ) {
516 if ( !$this->cookieJar ) {
517 $this->cookieJar = new CookieJar;
518 }
519
520 $this->cookieJar->setCookie( $name, $value, $attr );
521 }
522
523 /**
524 * Parse the cookies in the response headers and store them in the cookie jar.
525 */
526 protected function parseCookies() {
527 if ( !$this->cookieJar ) {
528 $this->cookieJar = new CookieJar;
529 }
530
531 if ( isset( $this->respHeaders['set-cookie'] ) ) {
532 $url = parse_url( $this->getFinalUrl() );
533 foreach ( $this->respHeaders['set-cookie'] as $cookie ) {
534 $this->cookieJar->parseCookieResponseHeader( $cookie, $url['host'] );
535 }
536 }
537 }
538
539 /**
540 * Returns the final URL after all redirections.
541 *
542 * @return String
543 */
544 public function getFinalUrl() {
545 $location = $this->getResponseHeader( "Location" );
546
547 if ( $location ) {
548 return $location;
549 }
550
551 return $this->url;
552 }
553
554 /**
555 * Returns true if the backend can follow redirects. Overridden by the
556 * child classes.
557 */
558 public function canFollowRedirects() {
559 return true;
560 }
561 }
562
563
564 class Cookie {
565 protected $name;
566 protected $value;
567 protected $expires;
568 protected $path;
569 protected $domain;
570 protected $isSessionKey = true;
571 // TO IMPLEMENT protected $secure
572 // TO IMPLEMENT? protected $maxAge (add onto expires)
573 // TO IMPLEMENT? protected $version
574 // TO IMPLEMENT? protected $comment
575
576 function __construct( $name, $value, $attr ) {
577 $this->name = $name;
578 $this->set( $value, $attr );
579 }
580
581 /**
582 * Sets a cookie. Used before a request to set up any individual
583 * cookies. Used internally after a request to parse the
584 * Set-Cookie headers.
585 *
586 * @param $value String: the value of the cookie
587 * @param $attr Array: possible key/values:
588 * expires A date string
589 * path The path this cookie is used on
590 * domain Domain this cookie is used on
591 */
592 public function set( $value, $attr ) {
593 $this->value = $value;
594
595 if ( isset( $attr['expires'] ) ) {
596 $this->isSessionKey = false;
597 $this->expires = strtotime( $attr['expires'] );
598 }
599
600 if ( isset( $attr['path'] ) ) {
601 $this->path = $attr['path'];
602 } else {
603 $this->path = "/";
604 }
605
606 if ( isset( $attr['domain'] ) ) {
607 if ( self::validateCookieDomain( $attr['domain'] ) ) {
608 $this->domain = $attr['domain'];
609 }
610 } else {
611 throw new MWException( "You must specify a domain." );
612 }
613 }
614
615 /**
616 * Return the true if the cookie is valid is valid. Otherwise,
617 * false. The uses a method similar to IE cookie security
618 * described here:
619 * http://kuza55.blogspot.com/2008/02/understanding-cookie-security.html
620 * A better method might be to use a blacklist like
621 * http://publicsuffix.org/
622 *
623 * @param $domain String: the domain to validate
624 * @param $originDomain String: (optional) the domain the cookie originates from
625 * @return Boolean
626 */
627 public static function validateCookieDomain( $domain, $originDomain = null ) {
628 // Don't allow a trailing dot
629 if ( substr( $domain, -1 ) == "." ) {
630 return false;
631 }
632
633 $dc = explode( ".", $domain );
634
635 // Only allow full, valid IP addresses
636 if ( preg_match( '/^[0-9.]+$/', $domain ) ) {
637 if ( count( $dc ) != 4 ) {
638 return false;
639 }
640
641 if ( ip2long( $domain ) === false ) {
642 return false;
643 }
644
645 if ( $originDomain == null || $originDomain == $domain ) {
646 return true;
647 }
648
649 }
650
651 // Don't allow cookies for "co.uk" or "gov.uk", etc, but allow "supermarket.uk"
652 if ( strrpos( $domain, "." ) - strlen( $domain ) == -3 ) {
653 if ( ( count( $dc ) == 2 && strlen( $dc[0] ) <= 2 )
654 || ( count( $dc ) == 3 && strlen( $dc[0] ) == "" && strlen( $dc[1] ) <= 2 ) ) {
655 return false;
656 }
657 if ( ( count( $dc ) == 2 || ( count( $dc ) == 3 && $dc[0] == "" ) )
658 && preg_match( '/(com|net|org|gov|edu)\...$/', $domain ) ) {
659 return false;
660 }
661 }
662
663 if ( $originDomain != null ) {
664 if ( substr( $domain, 0, 1 ) != "." && $domain != $originDomain ) {
665 return false;
666 }
667
668 if ( substr( $domain, 0, 1 ) == "."
669 && substr_compare( $originDomain, $domain, -strlen( $domain ),
670 strlen( $domain ), TRUE ) != 0 ) {
671 return false;
672 }
673 }
674
675 return true;
676 }
677
678 /**
679 * Serialize the cookie jar into a format useful for HTTP Request headers.
680 *
681 * @param $path String: the path that will be used. Required.
682 * @param $domain String: the domain that will be used. Required.
683 * @return String
684 */
685 public function serializeToHttpRequest( $path, $domain ) {
686 $ret = "";
687
688 if ( $this->canServeDomain( $domain )
689 && $this->canServePath( $path )
690 && $this->isUnExpired() ) {
691 $ret = $this->name . "=" . $this->value;
692 }
693
694 return $ret;
695 }
696
697 protected function canServeDomain( $domain ) {
698 if ( $domain == $this->domain
699 || ( strlen( $domain ) > strlen( $this->domain )
700 && substr( $this->domain, 0, 1 ) == "."
701 && substr_compare( $domain, $this->domain, -strlen( $this->domain ),
702 strlen( $this->domain ), TRUE ) == 0 ) ) {
703 return true;
704 }
705
706 return false;
707 }
708
709 protected function canServePath( $path ) {
710 if ( $this->path && substr_compare( $this->path, $path, 0, strlen( $this->path ) ) == 0 ) {
711 return true;
712 }
713
714 return false;
715 }
716
717 protected function isUnExpired() {
718 if ( $this->isSessionKey || $this->expires > time() ) {
719 return true;
720 }
721
722 return false;
723 }
724 }
725
726 class CookieJar {
727 private $cookie = array();
728
729 /**
730 * Set a cookie in the cookie jar. Make sure only one cookie per-name exists.
731 * @see Cookie::set()
732 */
733 public function setCookie ( $name, $value, $attr ) {
734 /* cookies: case insensitive, so this should work.
735 * We'll still send the cookies back in the same case we got them, though.
736 */
737 $index = strtoupper( $name );
738
739 if ( isset( $this->cookie[$index] ) ) {
740 $this->cookie[$index]->set( $value, $attr );
741 } else {
742 $this->cookie[$index] = new Cookie( $name, $value, $attr );
743 }
744 }
745
746 /**
747 * @see Cookie::serializeToHttpRequest
748 */
749 public function serializeToHttpRequest( $path, $domain ) {
750 $cookies = array();
751
752 foreach ( $this->cookie as $c ) {
753 $serialized = $c->serializeToHttpRequest( $path, $domain );
754
755 if ( $serialized ) {
756 $cookies[] = $serialized;
757 }
758 }
759
760 return implode( "; ", $cookies );
761 }
762
763 /**
764 * Parse the content of an Set-Cookie HTTP Response header.
765 *
766 * @param $cookie String
767 * @param $domain String: cookie's domain
768 */
769 public function parseCookieResponseHeader ( $cookie, $domain ) {
770 $len = strlen( "Set-Cookie:" );
771
772 if ( substr_compare( "Set-Cookie:", $cookie, 0, $len, TRUE ) === 0 ) {
773 $cookie = substr( $cookie, $len );
774 }
775
776 $bit = array_map( 'trim', explode( ";", $cookie ) );
777
778 if ( count( $bit ) >= 1 ) {
779 list( $name, $value ) = explode( "=", array_shift( $bit ), 2 );
780 $attr = array();
781
782 foreach ( $bit as $piece ) {
783 $parts = explode( "=", $piece );
784 if ( count( $parts ) > 1 ) {
785 $attr[strtolower( $parts[0] )] = $parts[1];
786 } else {
787 $attr[strtolower( $parts[0] )] = true;
788 }
789 }
790
791 if ( !isset( $attr['domain'] ) ) {
792 $attr['domain'] = $domain;
793 } elseif ( !Cookie::validateCookieDomain( $attr['domain'], $domain ) ) {
794 return null;
795 }
796
797 $this->setCookie( $name, $value, $attr );
798 }
799 }
800 }
801
802 /**
803 * MWHttpRequest implemented using internal curl compiled into PHP
804 */
805 class CurlHttpRequest extends MWHttpRequest {
806 const SUPPORTS_FILE_POSTS = true;
807
808 static $curlMessageMap = array(
809 6 => 'http-host-unreachable',
810 28 => 'http-timed-out'
811 );
812
813 protected $curlOptions = array();
814 protected $headerText = "";
815
816 protected function readHeader( $fh, $content ) {
817 $this->headerText .= $content;
818 return strlen( $content );
819 }
820
821 public function execute() {
822 parent::execute();
823
824 if ( !$this->status->isOK() ) {
825 return $this->status;
826 }
827
828 $this->curlOptions[CURLOPT_PROXY] = $this->proxy;
829 $this->curlOptions[CURLOPT_TIMEOUT] = $this->timeout;
830 $this->curlOptions[CURLOPT_HTTP_VERSION] = CURL_HTTP_VERSION_1_0;
831 $this->curlOptions[CURLOPT_WRITEFUNCTION] = $this->callback;
832 $this->curlOptions[CURLOPT_HEADERFUNCTION] = array( $this, "readHeader" );
833 $this->curlOptions[CURLOPT_MAXREDIRS] = $this->maxRedirects;
834 $this->curlOptions[CURLOPT_ENCODING] = ""; # Enable compression
835
836 /* not sure these two are actually necessary */
837 if ( isset( $this->reqHeaders['Referer'] ) ) {
838 $this->curlOptions[CURLOPT_REFERER] = $this->reqHeaders['Referer'];
839 }
840 $this->curlOptions[CURLOPT_USERAGENT] = $this->reqHeaders['User-Agent'];
841
842 if ( isset( $this->sslVerifyHost ) ) {
843 $this->curlOptions[CURLOPT_SSL_VERIFYHOST] = $this->sslVerifyHost;
844 }
845
846 if ( isset( $this->sslVerifyCert ) ) {
847 $this->curlOptions[CURLOPT_SSL_VERIFYPEER] = $this->sslVerifyCert;
848 }
849
850 if ( $this->caInfo ) {
851 $this->curlOptions[CURLOPT_CAINFO] = $this->caInfo;
852 }
853
854 if ( $this->headersOnly ) {
855 $this->curlOptions[CURLOPT_NOBODY] = true;
856 $this->curlOptions[CURLOPT_HEADER] = true;
857 } elseif ( $this->method == 'POST' ) {
858 $this->curlOptions[CURLOPT_POST] = true;
859 $this->curlOptions[CURLOPT_POSTFIELDS] = $this->postData;
860 // Suppress 'Expect: 100-continue' header, as some servers
861 // will reject it with a 417 and Curl won't auto retry
862 // with HTTP 1.0 fallback
863 $this->reqHeaders['Expect'] = '';
864 } else {
865 $this->curlOptions[CURLOPT_CUSTOMREQUEST] = $this->method;
866 }
867
868 $this->curlOptions[CURLOPT_HTTPHEADER] = $this->getHeaderList();
869
870 $curlHandle = curl_init( $this->url );
871
872 if ( !curl_setopt_array( $curlHandle, $this->curlOptions ) ) {
873 throw new MWException( "Error setting curl options." );
874 }
875
876 if ( $this->followRedirects && $this->canFollowRedirects() ) {
877 wfSuppressWarnings();
878 if ( ! curl_setopt( $curlHandle, CURLOPT_FOLLOWLOCATION, true ) ) {
879 wfDebug( __METHOD__ . ": Couldn't set CURLOPT_FOLLOWLOCATION. " .
880 "Probably safe_mode or open_basedir is set.\n" );
881 // Continue the processing. If it were in curl_setopt_array,
882 // processing would have halted on its entry
883 }
884 wfRestoreWarnings();
885 }
886
887 if ( false === curl_exec( $curlHandle ) ) {
888 $code = curl_error( $curlHandle );
889
890 if ( isset( self::$curlMessageMap[$code] ) ) {
891 $this->status->fatal( self::$curlMessageMap[$code] );
892 } else {
893 $this->status->fatal( 'http-curl-error', curl_error( $curlHandle ) );
894 }
895 } else {
896 $this->headerList = explode( "\r\n", $this->headerText );
897 }
898
899 curl_close( $curlHandle );
900
901 $this->parseHeader();
902 $this->setStatus();
903
904 return $this->status;
905 }
906
907 public function canFollowRedirects() {
908 if ( strval( ini_get( 'open_basedir' ) ) !== '' || wfIniGetBool( 'safe_mode' ) ) {
909 wfDebug( "Cannot follow redirects in safe mode\n" );
910 return false;
911 }
912
913 if ( !defined( 'CURLOPT_REDIR_PROTOCOLS' ) ) {
914 wfDebug( "Cannot follow redirects with libcurl < 7.19.4 due to CVE-2009-0037\n" );
915 return false;
916 }
917
918 return true;
919 }
920 }
921
922 class PhpHttpRequest extends MWHttpRequest {
923 protected function urlToTcp( $url ) {
924 $parsedUrl = parse_url( $url );
925
926 return 'tcp://' . $parsedUrl['host'] . ':' . $parsedUrl['port'];
927 }
928
929 public function execute() {
930 parent::execute();
931
932 if ( is_array( $this->postData ) ) {
933 $this->postData = wfArrayToCGI( $this->postData );
934 }
935
936 // At least on Centos 4.8 with PHP 5.1.6, using max_redirects to follow redirects
937 // causes a segfault
938 $manuallyRedirect = version_compare( phpversion(), '5.1.7', '<' );
939
940 if ( $this->parsedUrl['scheme'] != 'http' ) {
941 $this->status->fatal( 'http-invalid-scheme', $this->parsedUrl['scheme'] );
942 }
943
944 $this->reqHeaders['Accept'] = "*/*";
945 if ( $this->method == 'POST' ) {
946 // Required for HTTP 1.0 POSTs
947 $this->reqHeaders['Content-Length'] = strlen( $this->postData );
948 $this->reqHeaders['Content-type'] = "application/x-www-form-urlencoded";
949 }
950
951 $options = array();
952 if ( $this->proxy && !$this->noProxy ) {
953 $options['proxy'] = $this->urlToTCP( $this->proxy );
954 $options['request_fulluri'] = true;
955 }
956
957 if ( !$this->followRedirects || $manuallyRedirect ) {
958 $options['max_redirects'] = 0;
959 } else {
960 $options['max_redirects'] = $this->maxRedirects;
961 }
962
963 $options['method'] = $this->method;
964 $options['header'] = implode( "\r\n", $this->getHeaderList() );
965 // Note that at some future point we may want to support
966 // HTTP/1.1, but we'd have to write support for chunking
967 // in version of PHP < 5.3.1
968 $options['protocol_version'] = "1.0";
969
970 // This is how we tell PHP we want to deal with 404s (for example) ourselves.
971 // Only works on 5.2.10+
972 $options['ignore_errors'] = true;
973
974 if ( $this->postData ) {
975 $options['content'] = $this->postData;
976 }
977
978 $oldTimeout = false;
979 if ( version_compare( '5.2.1', phpversion(), '>' ) ) {
980 $oldTimeout = ini_set( 'default_socket_timeout', $this->timeout );
981 } else {
982 $options['timeout'] = $this->timeout;
983 }
984
985 $context = stream_context_create( array( 'http' => $options ) );
986
987 $this->headerList = array();
988 $reqCount = 0;
989 $url = $this->url;
990
991 $result = array();
992
993 do {
994 $reqCount++;
995 wfSuppressWarnings();
996 $fh = fopen( $url, "r", false, $context );
997 wfRestoreWarnings();
998
999 if ( !$fh ) {
1000 break;
1001 }
1002
1003 $result = stream_get_meta_data( $fh );
1004 $this->headerList = $result['wrapper_data'];
1005 $this->parseHeader();
1006
1007 if ( !$manuallyRedirect || !$this->followRedirects ) {
1008 break;
1009 }
1010
1011 # Handle manual redirection
1012 if ( !$this->isRedirect() || $reqCount > $this->maxRedirects ) {
1013 break;
1014 }
1015 # Check security of URL
1016 $url = $this->getResponseHeader( "Location" );
1017
1018 if ( substr( $url, 0, 7 ) !== 'http://' ) {
1019 wfDebug( __METHOD__ . ": insecure redirection\n" );
1020 break;
1021 }
1022 } while ( true );
1023
1024 if ( $oldTimeout !== false ) {
1025 ini_set( 'default_socket_timeout', $oldTimeout );
1026 }
1027
1028 $this->setStatus();
1029
1030 if ( $fh === false ) {
1031 $this->status->fatal( 'http-request-error' );
1032 return $this->status;
1033 }
1034
1035 if ( $result['timed_out'] ) {
1036 $this->status->fatal( 'http-timed-out', $this->url );
1037 return $this->status;
1038 }
1039
1040 if ( $this->status->isOK() ) {
1041 while ( !feof( $fh ) ) {
1042 $buf = fread( $fh, 8192 );
1043
1044 if ( $buf === false ) {
1045 $this->status->fatal( 'http-read-error' );
1046 break;
1047 }
1048
1049 if ( strlen( $buf ) ) {
1050 call_user_func( $this->callback, $fh, $buf );
1051 }
1052 }
1053 }
1054 fclose( $fh );
1055
1056 return $this->status;
1057 }
1058 }