Merge "Revert "Add maxlength of 6 for time correction in preferences""
[lhc/web/wiklou.git] / includes / HttpFunctions.php
1 <?php
2 /**
3 * Various HTTP related functions.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup HTTP
22 */
23
24 /**
25 * @defgroup HTTP HTTP
26 */
27
28 /**
29 * Various HTTP related functions
30 * @ingroup HTTP
31 */
32 class Http {
33 static public $httpEngine = false;
34
35 /**
36 * Perform an HTTP request
37 *
38 * @param string $method HTTP method. Usually GET/POST
39 * @param string $url Full URL to act on. If protocol-relative, will be expanded to an http:// URL
40 * @param array $options Options to pass to MWHttpRequest object.
41 * Possible keys for the array:
42 * - timeout Timeout length in seconds
43 * - connectTimeout Timeout for connection, in seconds (curl only)
44 * - postData An array of key-value pairs or a url-encoded form data
45 * - proxy The proxy to use.
46 * Otherwise it will use $wgHTTPProxy (if set)
47 * Otherwise it will use the environment variable "http_proxy" (if set)
48 * - noProxy Don't use any proxy at all. Takes precedence over proxy value(s).
49 * - sslVerifyHost Verify hostname against certificate
50 * - sslVerifyCert Verify SSL certificate
51 * - caInfo Provide CA information
52 * - maxRedirects Maximum number of redirects to follow (defaults to 5)
53 * - followRedirects Whether to follow redirects (defaults to false).
54 * Note: this should only be used when the target URL is trusted,
55 * to avoid attacks on intranet services accessible by HTTP.
56 * - userAgent A user agent, if you want to override the default
57 * MediaWiki/$wgVersion
58 * @return string|bool (bool)false on failure or a string on success
59 */
60 public static function request( $method, $url, $options = array() ) {
61 wfDebug( "HTTP: $method: $url\n" );
62 wfProfileIn( __METHOD__ . "-$method" );
63
64 $options['method'] = strtoupper( $method );
65
66 if ( !isset( $options['timeout'] ) ) {
67 $options['timeout'] = 'default';
68 }
69 if ( !isset( $options['connectTimeout'] ) ) {
70 $options['connectTimeout'] = 'default';
71 }
72
73 $req = MWHttpRequest::factory( $url, $options );
74 $status = $req->execute();
75
76 $content = false;
77 if ( $status->isOK() ) {
78 $content = $req->getContent();
79 }
80 wfProfileOut( __METHOD__ . "-$method" );
81 return $content;
82 }
83
84 /**
85 * Simple wrapper for Http::request( 'GET' )
86 * @see Http::request()
87 *
88 * @param string $url
89 * @param string $timeout
90 * @param array $options
91 * @return string
92 */
93 public static function get( $url, $timeout = 'default', $options = array() ) {
94 $options['timeout'] = $timeout;
95 return Http::request( 'GET', $url, $options );
96 }
97
98 /**
99 * Simple wrapper for Http::request( 'POST' )
100 * @see Http::request()
101 *
102 * @param string $url
103 * @param array $options
104 * @return string
105 */
106 public static function post( $url, $options = array() ) {
107 return Http::request( 'POST', $url, $options );
108 }
109
110 /**
111 * Check if the URL can be served by localhost
112 *
113 * @param string $url Full url to check
114 * @return bool
115 */
116 public static function isLocalURL( $url ) {
117 global $wgCommandLineMode, $wgLocalVirtualHosts, $wgConf;
118
119 if ( $wgCommandLineMode ) {
120 return false;
121 }
122
123 // Extract host part
124 $matches = array();
125 if ( preg_match( '!^http://([\w.-]+)[/:].*$!', $url, $matches ) ) {
126 $host = $matches[1];
127 // Split up dotwise
128 $domainParts = explode( '.', $host );
129 // Check if this domain or any superdomain is listed as a local virtual host
130 $domainParts = array_reverse( $domainParts );
131
132 $domain = '';
133 $countParts = count( $domainParts );
134 for ( $i = 0; $i < $countParts; $i++ ) {
135 $domainPart = $domainParts[$i];
136 if ( $i == 0 ) {
137 $domain = $domainPart;
138 } else {
139 $domain = $domainPart . '.' . $domain;
140 }
141
142 if ( in_array( $domain, $wgLocalVirtualHosts )
143 || $wgConf->isLocalVHost( $domain )
144 ) {
145 return true;
146 }
147 }
148 }
149
150 return false;
151 }
152
153 /**
154 * A standard user-agent we can use for external requests.
155 * @return string
156 */
157 public static function userAgent() {
158 global $wgVersion;
159 return "MediaWiki/$wgVersion";
160 }
161
162 /**
163 * Checks that the given URI is a valid one. Hardcoding the
164 * protocols, because we only want protocols that both cURL
165 * and php support.
166 *
167 * file:// should not be allowed here for security purpose (r67684)
168 *
169 * @todo FIXME this is wildly inaccurate and fails to actually check most stuff
170 *
171 * @param string $uri URI to check for validity
172 * @return bool
173 */
174 public static function isValidURI( $uri ) {
175 return preg_match(
176 '/^https?:\/\/[^\/\s]\S*$/D',
177 $uri
178 );
179 }
180 }
181
182 /**
183 * This wrapper class will call out to curl (if available) or fallback
184 * to regular PHP if necessary for handling internal HTTP requests.
185 *
186 * Renamed from HttpRequest to MWHttpRequest to avoid conflict with
187 * PHP's HTTP extension.
188 */
189 class MWHttpRequest {
190 const SUPPORTS_FILE_POSTS = false;
191
192 protected $content;
193 protected $timeout = 'default';
194 protected $headersOnly = null;
195 protected $postData = null;
196 protected $proxy = null;
197 protected $noProxy = false;
198 protected $sslVerifyHost = true;
199 protected $sslVerifyCert = true;
200 protected $caInfo = null;
201 protected $method = "GET";
202 protected $reqHeaders = array();
203 protected $url;
204 protected $parsedUrl;
205 protected $callback;
206 protected $maxRedirects = 5;
207 protected $followRedirects = false;
208
209 /**
210 * @var CookieJar
211 */
212 protected $cookieJar;
213
214 protected $headerList = array();
215 protected $respVersion = "0.9";
216 protected $respStatus = "200 Ok";
217 protected $respHeaders = array();
218
219 public $status;
220
221 /**
222 * @param string $url Url to use. If protocol-relative, will be expanded to an http:// URL
223 * @param array $options (optional) extra params to pass (see Http::request())
224 */
225 protected function __construct( $url, $options = array() ) {
226 global $wgHTTPTimeout, $wgHTTPConnectTimeout;
227
228 $this->url = wfExpandUrl( $url, PROTO_HTTP );
229 $this->parsedUrl = wfParseUrl( $this->url );
230
231 if ( !$this->parsedUrl || !Http::isValidURI( $this->url ) ) {
232 $this->status = Status::newFatal( 'http-invalid-url' );
233 } else {
234 $this->status = Status::newGood( 100 ); // continue
235 }
236
237 if ( isset( $options['timeout'] ) && $options['timeout'] != 'default' ) {
238 $this->timeout = $options['timeout'];
239 } else {
240 $this->timeout = $wgHTTPTimeout;
241 }
242 if ( isset( $options['connectTimeout'] ) && $options['connectTimeout'] != 'default' ) {
243 $this->connectTimeout = $options['connectTimeout'];
244 } else {
245 $this->connectTimeout = $wgHTTPConnectTimeout;
246 }
247 if ( isset( $options['userAgent'] ) ) {
248 $this->setUserAgent( $options['userAgent'] );
249 }
250
251 $members = array( "postData", "proxy", "noProxy", "sslVerifyHost", "caInfo",
252 "method", "followRedirects", "maxRedirects", "sslVerifyCert", "callback" );
253
254 foreach ( $members as $o ) {
255 if ( isset( $options[$o] ) ) {
256 // ensure that MWHttpRequest::method is always
257 // uppercased. Bug 36137
258 if ( $o == 'method' ) {
259 $options[$o] = strtoupper( $options[$o] );
260 }
261 $this->$o = $options[$o];
262 }
263 }
264
265 if ( $this->noProxy ) {
266 $this->proxy = ''; // noProxy takes precedence
267 }
268 }
269
270 /**
271 * Simple function to test if we can make any sort of requests at all, using
272 * cURL or fopen()
273 * @return bool
274 */
275 public static function canMakeRequests() {
276 return function_exists( 'curl_init' ) || wfIniGetBool( 'allow_url_fopen' );
277 }
278
279 /**
280 * Generate a new request object
281 * @param string $url Url to use
282 * @param array $options (optional) extra params to pass (see Http::request())
283 * @throws MWException
284 * @return CurlHttpRequest|PhpHttpRequest
285 * @see MWHttpRequest::__construct
286 */
287 public static function factory( $url, $options = null ) {
288 if ( !Http::$httpEngine ) {
289 Http::$httpEngine = function_exists( 'curl_init' ) ? 'curl' : 'php';
290 } elseif ( Http::$httpEngine == 'curl' && !function_exists( 'curl_init' ) ) {
291 throw new MWException( __METHOD__ . ': curl (http://php.net/curl) is not installed, but' .
292 ' Http::$httpEngine is set to "curl"' );
293 }
294
295 switch ( Http::$httpEngine ) {
296 case 'curl':
297 return new CurlHttpRequest( $url, $options );
298 case 'php':
299 if ( !wfIniGetBool( 'allow_url_fopen' ) ) {
300 throw new MWException( __METHOD__ . ': allow_url_fopen ' .
301 'needs to be enabled for pure PHP http requests to ' .
302 'work. If possible, curl should be used instead. See ' .
303 'http://php.net/curl.'
304 );
305 }
306 return new PhpHttpRequest( $url, $options );
307 default:
308 throw new MWException( __METHOD__ . ': The setting of Http::$httpEngine is not valid.' );
309 }
310 }
311
312 /**
313 * Get the body, or content, of the response to the request
314 *
315 * @return string
316 */
317 public function getContent() {
318 return $this->content;
319 }
320
321 /**
322 * Set the parameters of the request
323 *
324 * @param array $args
325 * @todo overload the args param
326 */
327 public function setData( $args ) {
328 $this->postData = $args;
329 }
330
331 /**
332 * Take care of setting up the proxy (do nothing if "noProxy" is set)
333 *
334 * @return void
335 */
336 public function proxySetup() {
337 global $wgHTTPProxy;
338
339 // If there is an explicit proxy set and proxies are not disabled, then use it
340 if ( $this->proxy && !$this->noProxy ) {
341 return;
342 }
343
344 // Otherwise, fallback to $wgHTTPProxy/http_proxy (when set) if this is not a machine
345 // local URL and proxies are not disabled
346 if ( Http::isLocalURL( $this->url ) || $this->noProxy ) {
347 $this->proxy = '';
348 } elseif ( $wgHTTPProxy ) {
349 $this->proxy = $wgHTTPProxy;
350 } elseif ( getenv( "http_proxy" ) ) {
351 $this->proxy = getenv( "http_proxy" );
352 }
353 }
354
355 /**
356 * Set the user agent
357 * @param string $UA
358 */
359 public function setUserAgent( $UA ) {
360 $this->setHeader( 'User-Agent', $UA );
361 }
362
363 /**
364 * Set an arbitrary header
365 * @param string $name
366 * @param string $value
367 */
368 public function setHeader( $name, $value ) {
369 // I feel like I should normalize the case here...
370 $this->reqHeaders[$name] = $value;
371 }
372
373 /**
374 * Get an array of the headers
375 * @return array
376 */
377 public function getHeaderList() {
378 $list = array();
379
380 if ( $this->cookieJar ) {
381 $this->reqHeaders['Cookie'] =
382 $this->cookieJar->serializeToHttpRequest(
383 $this->parsedUrl['path'],
384 $this->parsedUrl['host']
385 );
386 }
387
388 foreach ( $this->reqHeaders as $name => $value ) {
389 $list[] = "$name: $value";
390 }
391
392 return $list;
393 }
394
395 /**
396 * Set a read callback to accept data read from the HTTP request.
397 * By default, data is appended to an internal buffer which can be
398 * retrieved through $req->getContent().
399 *
400 * To handle data as it comes in -- especially for large files that
401 * would not fit in memory -- you can instead set your own callback,
402 * in the form function($resource, $buffer) where the first parameter
403 * is the low-level resource being read (implementation specific),
404 * and the second parameter is the data buffer.
405 *
406 * You MUST return the number of bytes handled in the buffer; if fewer
407 * bytes are reported handled than were passed to you, the HTTP fetch
408 * will be aborted.
409 *
410 * @param callable $callback
411 * @throws MWException
412 */
413 public function setCallback( $callback ) {
414 if ( !is_callable( $callback ) ) {
415 throw new MWException( 'Invalid MwHttpRequest callback' );
416 }
417 $this->callback = $callback;
418 }
419
420 /**
421 * A generic callback to read the body of the response from a remote
422 * server.
423 *
424 * @param resource $fh
425 * @param string $content
426 * @return int
427 */
428 public function read( $fh, $content ) {
429 $this->content .= $content;
430 return strlen( $content );
431 }
432
433 /**
434 * Take care of whatever is necessary to perform the URI request.
435 *
436 * @return Status
437 */
438 public function execute() {
439 wfProfileIn( __METHOD__ );
440
441 $this->content = "";
442
443 if ( strtoupper( $this->method ) == "HEAD" ) {
444 $this->headersOnly = true;
445 }
446
447 $this->proxySetup(); // set up any proxy as needed
448
449 if ( !$this->callback ) {
450 $this->setCallback( array( $this, 'read' ) );
451 }
452
453 if ( !isset( $this->reqHeaders['User-Agent'] ) ) {
454 $this->setUserAgent( Http::userAgent() );
455 }
456
457 wfProfileOut( __METHOD__ );
458 }
459
460 /**
461 * Parses the headers, including the HTTP status code and any
462 * Set-Cookie headers. This function expects the headers to be
463 * found in an array in the member variable headerList.
464 */
465 protected function parseHeader() {
466 wfProfileIn( __METHOD__ );
467
468 $lastname = "";
469
470 foreach ( $this->headerList as $header ) {
471 if ( preg_match( "#^HTTP/([0-9.]+) (.*)#", $header, $match ) ) {
472 $this->respVersion = $match[1];
473 $this->respStatus = $match[2];
474 } elseif ( preg_match( "#^[ \t]#", $header ) ) {
475 $last = count( $this->respHeaders[$lastname] ) - 1;
476 $this->respHeaders[$lastname][$last] .= "\r\n$header";
477 } elseif ( preg_match( "#^([^:]*):[\t ]*(.*)#", $header, $match ) ) {
478 $this->respHeaders[strtolower( $match[1] )][] = $match[2];
479 $lastname = strtolower( $match[1] );
480 }
481 }
482
483 $this->parseCookies();
484
485 wfProfileOut( __METHOD__ );
486 }
487
488 /**
489 * Sets HTTPRequest status member to a fatal value with the error
490 * message if the returned integer value of the status code was
491 * not successful (< 300) or a redirect (>=300 and < 400). (see
492 * RFC2616, section 10,
493 * http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html for a
494 * list of status codes.)
495 */
496 protected function setStatus() {
497 if ( !$this->respHeaders ) {
498 $this->parseHeader();
499 }
500
501 if ( (int)$this->respStatus > 399 ) {
502 list( $code, $message ) = explode( " ", $this->respStatus, 2 );
503 $this->status->fatal( "http-bad-status", $code, $message );
504 }
505 }
506
507 /**
508 * Get the integer value of the HTTP status code (e.g. 200 for "200 Ok")
509 * (see RFC2616, section 10, http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
510 * for a list of status codes.)
511 *
512 * @return int
513 */
514 public function getStatus() {
515 if ( !$this->respHeaders ) {
516 $this->parseHeader();
517 }
518
519 return (int)$this->respStatus;
520 }
521
522 /**
523 * Returns true if the last status code was a redirect.
524 *
525 * @return bool
526 */
527 public function isRedirect() {
528 if ( !$this->respHeaders ) {
529 $this->parseHeader();
530 }
531
532 $status = (int)$this->respStatus;
533
534 if ( $status >= 300 && $status <= 303 ) {
535 return true;
536 }
537
538 return false;
539 }
540
541 /**
542 * Returns an associative array of response headers after the
543 * request has been executed. Because some headers
544 * (e.g. Set-Cookie) can appear more than once the, each value of
545 * the associative array is an array of the values given.
546 *
547 * @return array
548 */
549 public function getResponseHeaders() {
550 if ( !$this->respHeaders ) {
551 $this->parseHeader();
552 }
553
554 return $this->respHeaders;
555 }
556
557 /**
558 * Returns the value of the given response header.
559 *
560 * @param string $header
561 * @return string
562 */
563 public function getResponseHeader( $header ) {
564 if ( !$this->respHeaders ) {
565 $this->parseHeader();
566 }
567
568 if ( isset( $this->respHeaders[strtolower( $header )] ) ) {
569 $v = $this->respHeaders[strtolower( $header )];
570 return $v[count( $v ) - 1];
571 }
572
573 return null;
574 }
575
576 /**
577 * Tells the MWHttpRequest object to use this pre-loaded CookieJar.
578 *
579 * @param CookieJar $jar
580 */
581 public function setCookieJar( $jar ) {
582 $this->cookieJar = $jar;
583 }
584
585 /**
586 * Returns the cookie jar in use.
587 *
588 * @return CookieJar
589 */
590 public function getCookieJar() {
591 if ( !$this->respHeaders ) {
592 $this->parseHeader();
593 }
594
595 return $this->cookieJar;
596 }
597
598 /**
599 * Sets a cookie. Used before a request to set up any individual
600 * cookies. Used internally after a request to parse the
601 * Set-Cookie headers.
602 * @see Cookie::set
603 * @param string $name
604 * @param mixed $value
605 * @param array $attr
606 */
607 public function setCookie( $name, $value = null, $attr = null ) {
608 if ( !$this->cookieJar ) {
609 $this->cookieJar = new CookieJar;
610 }
611
612 $this->cookieJar->setCookie( $name, $value, $attr );
613 }
614
615 /**
616 * Parse the cookies in the response headers and store them in the cookie jar.
617 */
618 protected function parseCookies() {
619 wfProfileIn( __METHOD__ );
620
621 if ( !$this->cookieJar ) {
622 $this->cookieJar = new CookieJar;
623 }
624
625 if ( isset( $this->respHeaders['set-cookie'] ) ) {
626 $url = parse_url( $this->getFinalUrl() );
627 foreach ( $this->respHeaders['set-cookie'] as $cookie ) {
628 $this->cookieJar->parseCookieResponseHeader( $cookie, $url['host'] );
629 }
630 }
631
632 wfProfileOut( __METHOD__ );
633 }
634
635 /**
636 * Returns the final URL after all redirections.
637 *
638 * Relative values of the "Location" header are incorrect as
639 * stated in RFC, however they do happen and modern browsers
640 * support them. This function loops backwards through all
641 * locations in order to build the proper absolute URI - Marooned
642 * at wikia-inc.com
643 *
644 * Note that the multiple Location: headers are an artifact of
645 * CURL -- they shouldn't actually get returned this way. Rewrite
646 * this when bug 29232 is taken care of (high-level redirect
647 * handling rewrite).
648 *
649 * @return string
650 */
651 public function getFinalUrl() {
652 $headers = $this->getResponseHeaders();
653
654 //return full url (fix for incorrect but handled relative location)
655 if ( isset( $headers['location'] ) ) {
656 $locations = $headers['location'];
657 $domain = '';
658 $foundRelativeURI = false;
659 $countLocations = count( $locations );
660
661 for ( $i = $countLocations - 1; $i >= 0; $i-- ) {
662 $url = parse_url( $locations[$i] );
663
664 if ( isset( $url['host'] ) ) {
665 $domain = $url['scheme'] . '://' . $url['host'];
666 break; //found correct URI (with host)
667 } else {
668 $foundRelativeURI = true;
669 }
670 }
671
672 if ( $foundRelativeURI ) {
673 if ( $domain ) {
674 return $domain . $locations[$countLocations - 1];
675 } else {
676 $url = parse_url( $this->url );
677 if ( isset( $url['host'] ) ) {
678 return $url['scheme'] . '://' . $url['host'] .
679 $locations[$countLocations - 1];
680 }
681 }
682 } else {
683 return $locations[$countLocations - 1];
684 }
685 }
686
687 return $this->url;
688 }
689
690 /**
691 * Returns true if the backend can follow redirects. Overridden by the
692 * child classes.
693 * @return bool
694 */
695 public function canFollowRedirects() {
696 return true;
697 }
698 }
699
700 /**
701 * MWHttpRequest implemented using internal curl compiled into PHP
702 */
703 class CurlHttpRequest extends MWHttpRequest {
704 const SUPPORTS_FILE_POSTS = true;
705
706 protected $curlOptions = array();
707 protected $headerText = "";
708
709 /**
710 * @param resource $fh
711 * @param string $content
712 * @return int
713 */
714 protected function readHeader( $fh, $content ) {
715 $this->headerText .= $content;
716 return strlen( $content );
717 }
718
719 public function execute() {
720 wfProfileIn( __METHOD__ );
721
722 parent::execute();
723
724 if ( !$this->status->isOK() ) {
725 wfProfileOut( __METHOD__ );
726 return $this->status;
727 }
728
729 $this->curlOptions[CURLOPT_PROXY] = $this->proxy;
730 $this->curlOptions[CURLOPT_TIMEOUT] = $this->timeout;
731
732 // Only supported in curl >= 7.16.2
733 if ( defined( 'CURLOPT_CONNECTTIMEOUT_MS' ) ) {
734 $this->curlOptions[CURLOPT_CONNECTTIMEOUT_MS] = $this->connectTimeout * 1000;
735 }
736
737 $this->curlOptions[CURLOPT_HTTP_VERSION] = CURL_HTTP_VERSION_1_0;
738 $this->curlOptions[CURLOPT_WRITEFUNCTION] = $this->callback;
739 $this->curlOptions[CURLOPT_HEADERFUNCTION] = array( $this, "readHeader" );
740 $this->curlOptions[CURLOPT_MAXREDIRS] = $this->maxRedirects;
741 $this->curlOptions[CURLOPT_ENCODING] = ""; # Enable compression
742
743 $this->curlOptions[CURLOPT_USERAGENT] = $this->reqHeaders['User-Agent'];
744
745 $this->curlOptions[CURLOPT_SSL_VERIFYHOST] = $this->sslVerifyHost ? 2 : 0;
746 $this->curlOptions[CURLOPT_SSL_VERIFYPEER] = $this->sslVerifyCert;
747
748 if ( $this->caInfo ) {
749 $this->curlOptions[CURLOPT_CAINFO] = $this->caInfo;
750 }
751
752 if ( $this->headersOnly ) {
753 $this->curlOptions[CURLOPT_NOBODY] = true;
754 $this->curlOptions[CURLOPT_HEADER] = true;
755 } elseif ( $this->method == 'POST' ) {
756 $this->curlOptions[CURLOPT_POST] = true;
757 $this->curlOptions[CURLOPT_POSTFIELDS] = $this->postData;
758 // Suppress 'Expect: 100-continue' header, as some servers
759 // will reject it with a 417 and Curl won't auto retry
760 // with HTTP 1.0 fallback
761 $this->reqHeaders['Expect'] = '';
762 } else {
763 $this->curlOptions[CURLOPT_CUSTOMREQUEST] = $this->method;
764 }
765
766 $this->curlOptions[CURLOPT_HTTPHEADER] = $this->getHeaderList();
767
768 $curlHandle = curl_init( $this->url );
769
770 if ( !curl_setopt_array( $curlHandle, $this->curlOptions ) ) {
771 wfProfileOut( __METHOD__ );
772 throw new MWException( "Error setting curl options." );
773 }
774
775 if ( $this->followRedirects && $this->canFollowRedirects() ) {
776 wfSuppressWarnings();
777 if ( !curl_setopt( $curlHandle, CURLOPT_FOLLOWLOCATION, true ) ) {
778 wfDebug( __METHOD__ . ": Couldn't set CURLOPT_FOLLOWLOCATION. " .
779 "Probably safe_mode or open_basedir is set.\n" );
780 // Continue the processing. If it were in curl_setopt_array,
781 // processing would have halted on its entry
782 }
783 wfRestoreWarnings();
784 }
785
786 $curlRes = curl_exec( $curlHandle );
787 if ( curl_errno( $curlHandle ) == CURLE_OPERATION_TIMEOUTED ) {
788 $this->status->fatal( 'http-timed-out', $this->url );
789 } elseif ( $curlRes === false ) {
790 $this->status->fatal( 'http-curl-error', curl_error( $curlHandle ) );
791 } else {
792 $this->headerList = explode( "\r\n", $this->headerText );
793 }
794
795 curl_close( $curlHandle );
796
797 $this->parseHeader();
798 $this->setStatus();
799
800 wfProfileOut( __METHOD__ );
801
802 return $this->status;
803 }
804
805 /**
806 * @return bool
807 */
808 public function canFollowRedirects() {
809 if ( strval( ini_get( 'open_basedir' ) ) !== '' || wfIniGetBool( 'safe_mode' ) ) {
810 wfDebug( "Cannot follow redirects in safe mode\n" );
811 return false;
812 }
813
814 $curlVersionInfo = curl_version();
815 if ( $curlVersionInfo['version_number'] < 0x071304 ) {
816 wfDebug( "Cannot follow redirects with libcurl < 7.19.4 due to CVE-2009-0037\n" );
817 return false;
818 }
819
820 return true;
821 }
822 }
823
824 class PhpHttpRequest extends MWHttpRequest {
825
826 /**
827 * @param string $url
828 * @return string
829 */
830 protected function urlToTcp( $url ) {
831 $parsedUrl = parse_url( $url );
832
833 return 'tcp://' . $parsedUrl['host'] . ':' . $parsedUrl['port'];
834 }
835
836 public function execute() {
837 wfProfileIn( __METHOD__ );
838
839 parent::execute();
840
841 if ( is_array( $this->postData ) ) {
842 $this->postData = wfArrayToCgi( $this->postData );
843 }
844
845 if ( $this->parsedUrl['scheme'] != 'http'
846 && $this->parsedUrl['scheme'] != 'https' ) {
847 $this->status->fatal( 'http-invalid-scheme', $this->parsedUrl['scheme'] );
848 }
849
850 $this->reqHeaders['Accept'] = "*/*";
851 $this->reqHeaders['Connection'] = 'Close';
852 if ( $this->method == 'POST' ) {
853 // Required for HTTP 1.0 POSTs
854 $this->reqHeaders['Content-Length'] = strlen( $this->postData );
855 if ( !isset( $this->reqHeaders['Content-Type'] ) ) {
856 $this->reqHeaders['Content-Type'] = "application/x-www-form-urlencoded";
857 }
858 }
859
860 // Set up PHP stream context
861 $options = array(
862 'http' => array(
863 'method' => $this->method,
864 'header' => implode( "\r\n", $this->getHeaderList() ),
865 'protocol_version' => '1.1',
866 'max_redirects' => $this->followRedirects ? $this->maxRedirects : 0,
867 'ignore_errors' => true,
868 'timeout' => $this->timeout,
869 // Curl options in case curlwrappers are installed
870 'curl_verify_ssl_host' => $this->sslVerifyHost ? 2 : 0,
871 'curl_verify_ssl_peer' => $this->sslVerifyCert,
872 ),
873 'ssl' => array(
874 'verify_peer' => $this->sslVerifyCert,
875 'SNI_enabled' => true,
876 ),
877 );
878
879 if ( $this->proxy ) {
880 $options['http']['proxy'] = $this->urlToTCP( $this->proxy );
881 $options['http']['request_fulluri'] = true;
882 }
883
884 if ( $this->postData ) {
885 $options['http']['content'] = $this->postData;
886 }
887
888 if ( $this->sslVerifyHost ) {
889 $options['ssl']['CN_match'] = $this->parsedUrl['host'];
890 }
891
892 if ( is_dir( $this->caInfo ) ) {
893 $options['ssl']['capath'] = $this->caInfo;
894 } elseif ( is_file( $this->caInfo ) ) {
895 $options['ssl']['cafile'] = $this->caInfo;
896 } elseif ( $this->caInfo ) {
897 throw new MWException( "Invalid CA info passed: {$this->caInfo}" );
898 }
899
900 $context = stream_context_create( $options );
901
902 $this->headerList = array();
903 $reqCount = 0;
904 $url = $this->url;
905
906 $result = array();
907
908 do {
909 $reqCount++;
910 wfSuppressWarnings();
911 $fh = fopen( $url, "r", false, $context );
912 wfRestoreWarnings();
913
914 if ( !$fh ) {
915 break;
916 }
917
918 $result = stream_get_meta_data( $fh );
919 $this->headerList = $result['wrapper_data'];
920 $this->parseHeader();
921
922 if ( !$this->followRedirects ) {
923 break;
924 }
925
926 # Handle manual redirection
927 if ( !$this->isRedirect() || $reqCount > $this->maxRedirects ) {
928 break;
929 }
930 # Check security of URL
931 $url = $this->getResponseHeader( "Location" );
932
933 if ( !Http::isValidURI( $url ) ) {
934 wfDebug( __METHOD__ . ": insecure redirection\n" );
935 break;
936 }
937 } while ( true );
938
939 $this->setStatus();
940
941 if ( $fh === false ) {
942 $this->status->fatal( 'http-request-error' );
943 wfProfileOut( __METHOD__ );
944 return $this->status;
945 }
946
947 if ( $result['timed_out'] ) {
948 $this->status->fatal( 'http-timed-out', $this->url );
949 wfProfileOut( __METHOD__ );
950 return $this->status;
951 }
952
953 // If everything went OK, or we received some error code
954 // get the response body content.
955 if ( $this->status->isOK() || (int)$this->respStatus >= 300 ) {
956 while ( !feof( $fh ) ) {
957 $buf = fread( $fh, 8192 );
958
959 if ( $buf === false ) {
960 $this->status->fatal( 'http-read-error' );
961 break;
962 }
963
964 if ( strlen( $buf ) ) {
965 call_user_func( $this->callback, $fh, $buf );
966 }
967 }
968 }
969 fclose( $fh );
970
971 wfProfileOut( __METHOD__ );
972
973 return $this->status;
974 }
975 }