MediaWiki  1.27.1
HttpFunctions.php
Go to the documentation of this file.
1 <?php
29 
34 class Http {
35  static public $httpEngine = false;
36 
63  public static function request( $method, $url, $options = [], $caller = __METHOD__ ) {
64  wfDebug( "HTTP: $method: $url\n" );
65 
66  $options['method'] = strtoupper( $method );
67 
68  if ( !isset( $options['timeout'] ) ) {
69  $options['timeout'] = 'default';
70  }
71  if ( !isset( $options['connectTimeout'] ) ) {
72  $options['connectTimeout'] = 'default';
73  }
74 
75  $req = MWHttpRequest::factory( $url, $options, $caller );
76  $status = $req->execute();
77 
78  if ( $status->isOK() ) {
79  return $req->getContent();
80  } else {
81  $errors = $status->getErrorsByType( 'error' );
82  $logger = LoggerFactory::getInstance( 'http' );
83  $logger->warning( $status->getWikiText( false, false, 'en' ),
84  [ 'error' => $errors, 'caller' => $caller, 'content' => $req->getContent() ] );
85  return false;
86  }
87  }
88 
100  public static function get( $url, $options = [], $caller = __METHOD__ ) {
101  $args = func_get_args();
102  if ( isset( $args[1] ) && ( is_string( $args[1] ) || is_numeric( $args[1] ) ) ) {
103  // Second was used to be the timeout
104  // And third parameter used to be $options
105  wfWarn( "Second parameter should not be a timeout.", 2 );
106  $options = isset( $args[2] ) && is_array( $args[2] ) ?
107  $args[2] : [];
108  $options['timeout'] = $args[1];
109  $caller = __METHOD__;
110  }
111  return Http::request( 'GET', $url, $options, $caller );
112  }
113 
123  public static function post( $url, $options = [], $caller = __METHOD__ ) {
124  return Http::request( 'POST', $url, $options, $caller );
125  }
126 
133  public static function isLocalURL( $url ) {
134  global $wgCommandLineMode, $wgLocalVirtualHosts;
135 
136  if ( $wgCommandLineMode ) {
137  return false;
138  }
139 
140  // Extract host part
141  $matches = [];
142  if ( preg_match( '!^http://([\w.-]+)[/:].*$!', $url, $matches ) ) {
143  $host = $matches[1];
144  // Split up dotwise
145  $domainParts = explode( '.', $host );
146  // Check if this domain or any superdomain is listed as a local virtual host
147  $domainParts = array_reverse( $domainParts );
148 
149  $domain = '';
150  $countParts = count( $domainParts );
151  for ( $i = 0; $i < $countParts; $i++ ) {
152  $domainPart = $domainParts[$i];
153  if ( $i == 0 ) {
154  $domain = $domainPart;
155  } else {
156  $domain = $domainPart . '.' . $domain;
157  }
158 
159  if ( in_array( $domain, $wgLocalVirtualHosts ) ) {
160  return true;
161  }
162  }
163  }
164 
165  return false;
166  }
167 
172  public static function userAgent() {
174  return "MediaWiki/$wgVersion";
175  }
176 
189  public static function isValidURI( $uri ) {
190  return preg_match(
191  '/^https?:\/\/[^\/\s]\S*$/D',
192  $uri
193  );
194  }
195 
201  public static function getProxy() {
202  global $wgHTTPProxy;
203 
204  if ( $wgHTTPProxy ) {
205  return $wgHTTPProxy;
206  }
207 
208  return "";
209  }
210 }
211 
220  const SUPPORTS_FILE_POSTS = false;
221 
222  protected $content;
223  protected $timeout = 'default';
224  protected $headersOnly = null;
225  protected $postData = null;
226  protected $proxy = null;
227  protected $noProxy = false;
228  protected $sslVerifyHost = true;
229  protected $sslVerifyCert = true;
230  protected $caInfo = null;
231  protected $method = "GET";
232  protected $reqHeaders = [];
233  protected $url;
234  protected $parsedUrl;
235  protected $callback;
236  protected $maxRedirects = 5;
237  protected $followRedirects = false;
238 
242  protected $cookieJar;
243 
244  protected $headerList = [];
245  protected $respVersion = "0.9";
246  protected $respStatus = "200 Ok";
247  protected $respHeaders = [];
248 
249  public $status;
250 
254  protected $profiler;
255 
259  protected $profileName;
260 
267  protected function __construct(
268  $url, $options = [], $caller = __METHOD__, $profiler = null
269  ) {
270  global $wgHTTPTimeout, $wgHTTPConnectTimeout;
271 
272  $this->url = wfExpandUrl( $url, PROTO_HTTP );
273  $this->parsedUrl = wfParseUrl( $this->url );
274 
275  if ( !$this->parsedUrl || !Http::isValidURI( $this->url ) ) {
276  $this->status = Status::newFatal( 'http-invalid-url', $url );
277  } else {
278  $this->status = Status::newGood( 100 ); // continue
279  }
280 
281  if ( isset( $options['timeout'] ) && $options['timeout'] != 'default' ) {
282  $this->timeout = $options['timeout'];
283  } else {
284  $this->timeout = $wgHTTPTimeout;
285  }
286  if ( isset( $options['connectTimeout'] ) && $options['connectTimeout'] != 'default' ) {
287  $this->connectTimeout = $options['connectTimeout'];
288  } else {
289  $this->connectTimeout = $wgHTTPConnectTimeout;
290  }
291  if ( isset( $options['userAgent'] ) ) {
292  $this->setUserAgent( $options['userAgent'] );
293  }
294 
295  $members = [ "postData", "proxy", "noProxy", "sslVerifyHost", "caInfo",
296  "method", "followRedirects", "maxRedirects", "sslVerifyCert", "callback" ];
297 
298  foreach ( $members as $o ) {
299  if ( isset( $options[$o] ) ) {
300  // ensure that MWHttpRequest::method is always
301  // uppercased. Bug 36137
302  if ( $o == 'method' ) {
303  $options[$o] = strtoupper( $options[$o] );
304  }
305  $this->$o = $options[$o];
306  }
307  }
308 
309  if ( $this->noProxy ) {
310  $this->proxy = ''; // noProxy takes precedence
311  }
312 
313  // Profile based on what's calling us
314  $this->profiler = $profiler;
315  $this->profileName = $caller;
316  }
317 
323  public static function canMakeRequests() {
324  return function_exists( 'curl_init' ) || wfIniGetBool( 'allow_url_fopen' );
325  }
326 
336  public static function factory( $url, $options = null, $caller = __METHOD__ ) {
337  if ( !Http::$httpEngine ) {
338  Http::$httpEngine = function_exists( 'curl_init' ) ? 'curl' : 'php';
339  } elseif ( Http::$httpEngine == 'curl' && !function_exists( 'curl_init' ) ) {
340  throw new MWException( __METHOD__ . ': curl (http://php.net/curl) is not installed, but' .
341  ' Http::$httpEngine is set to "curl"' );
342  }
343 
344  switch ( Http::$httpEngine ) {
345  case 'curl':
346  return new CurlHttpRequest( $url, $options, $caller, Profiler::instance() );
347  case 'php':
348  if ( !wfIniGetBool( 'allow_url_fopen' ) ) {
349  throw new MWException( __METHOD__ . ': allow_url_fopen ' .
350  'needs to be enabled for pure PHP http requests to ' .
351  'work. If possible, curl should be used instead. See ' .
352  'http://php.net/curl.'
353  );
354  }
355  return new PhpHttpRequest( $url, $options, $caller, Profiler::instance() );
356  default:
357  throw new MWException( __METHOD__ . ': The setting of Http::$httpEngine is not valid.' );
358  }
359  }
360 
366  public function getContent() {
367  return $this->content;
368  }
369 
376  public function setData( $args ) {
377  $this->postData = $args;
378  }
379 
385  public function proxySetup() {
386  // If there is an explicit proxy set and proxies are not disabled, then use it
387  if ( $this->proxy && !$this->noProxy ) {
388  return;
389  }
390 
391  // Otherwise, fallback to $wgHTTPProxy if this is not a machine
392  // local URL and proxies are not disabled
393  if ( Http::isLocalURL( $this->url ) || $this->noProxy ) {
394  $this->proxy = '';
395  } else {
396  $this->proxy = Http::getProxy();
397  }
398  }
399 
404  public function setUserAgent( $UA ) {
405  $this->setHeader( 'User-Agent', $UA );
406  }
407 
413  public function setHeader( $name, $value ) {
414  // I feel like I should normalize the case here...
415  $this->reqHeaders[$name] = $value;
416  }
417 
422  public function getHeaderList() {
423  $list = [];
424 
425  if ( $this->cookieJar ) {
426  $this->reqHeaders['Cookie'] =
427  $this->cookieJar->serializeToHttpRequest(
428  $this->parsedUrl['path'],
429  $this->parsedUrl['host']
430  );
431  }
432 
433  foreach ( $this->reqHeaders as $name => $value ) {
434  $list[] = "$name: $value";
435  }
436 
437  return $list;
438  }
439 
458  public function setCallback( $callback ) {
459  if ( !is_callable( $callback ) ) {
460  throw new MWException( 'Invalid MwHttpRequest callback' );
461  }
462  $this->callback = $callback;
463  }
464 
473  public function read( $fh, $content ) {
474  $this->content .= $content;
475  return strlen( $content );
476  }
477 
483  public function execute() {
484 
485  $this->content = "";
486 
487  if ( strtoupper( $this->method ) == "HEAD" ) {
488  $this->headersOnly = true;
489  }
490 
491  $this->proxySetup(); // set up any proxy as needed
492 
493  if ( !$this->callback ) {
494  $this->setCallback( [ $this, 'read' ] );
495  }
496 
497  if ( !isset( $this->reqHeaders['User-Agent'] ) ) {
498  $this->setUserAgent( Http::userAgent() );
499  }
500 
501  }
502 
508  protected function parseHeader() {
509 
510  $lastname = "";
511 
512  foreach ( $this->headerList as $header ) {
513  if ( preg_match( "#^HTTP/([0-9.]+) (.*)#", $header, $match ) ) {
514  $this->respVersion = $match[1];
515  $this->respStatus = $match[2];
516  } elseif ( preg_match( "#^[ \t]#", $header ) ) {
517  $last = count( $this->respHeaders[$lastname] ) - 1;
518  $this->respHeaders[$lastname][$last] .= "\r\n$header";
519  } elseif ( preg_match( "#^([^:]*):[\t ]*(.*)#", $header, $match ) ) {
520  $this->respHeaders[strtolower( $match[1] )][] = $match[2];
521  $lastname = strtolower( $match[1] );
522  }
523  }
524 
525  $this->parseCookies();
526 
527  }
528 
537  protected function setStatus() {
538  if ( !$this->respHeaders ) {
539  $this->parseHeader();
540  }
541 
542  if ( (int)$this->respStatus > 399 ) {
543  list( $code, $message ) = explode( " ", $this->respStatus, 2 );
544  $this->status->fatal( "http-bad-status", $code, $message );
545  }
546  }
547 
555  public function getStatus() {
556  if ( !$this->respHeaders ) {
557  $this->parseHeader();
558  }
559 
560  return (int)$this->respStatus;
561  }
562 
568  public function isRedirect() {
569  if ( !$this->respHeaders ) {
570  $this->parseHeader();
571  }
572 
573  $status = (int)$this->respStatus;
574 
575  if ( $status >= 300 && $status <= 303 ) {
576  return true;
577  }
578 
579  return false;
580  }
581 
590  public function getResponseHeaders() {
591  if ( !$this->respHeaders ) {
592  $this->parseHeader();
593  }
594 
595  return $this->respHeaders;
596  }
597 
604  public function getResponseHeader( $header ) {
605  if ( !$this->respHeaders ) {
606  $this->parseHeader();
607  }
608 
609  if ( isset( $this->respHeaders[strtolower( $header )] ) ) {
610  $v = $this->respHeaders[strtolower( $header )];
611  return $v[count( $v ) - 1];
612  }
613 
614  return null;
615  }
616 
622  public function setCookieJar( $jar ) {
623  $this->cookieJar = $jar;
624  }
625 
631  public function getCookieJar() {
632  if ( !$this->respHeaders ) {
633  $this->parseHeader();
634  }
635 
636  return $this->cookieJar;
637  }
638 
648  public function setCookie( $name, $value = null, $attr = null ) {
649  if ( !$this->cookieJar ) {
650  $this->cookieJar = new CookieJar;
651  }
652 
653  $this->cookieJar->setCookie( $name, $value, $attr );
654  }
655 
659  protected function parseCookies() {
660 
661  if ( !$this->cookieJar ) {
662  $this->cookieJar = new CookieJar;
663  }
664 
665  if ( isset( $this->respHeaders['set-cookie'] ) ) {
666  $url = parse_url( $this->getFinalUrl() );
667  foreach ( $this->respHeaders['set-cookie'] as $cookie ) {
668  $this->cookieJar->parseCookieResponseHeader( $cookie, $url['host'] );
669  }
670  }
671 
672  }
673 
690  public function getFinalUrl() {
691  $headers = $this->getResponseHeaders();
692 
693  // return full url (fix for incorrect but handled relative location)
694  if ( isset( $headers['location'] ) ) {
695  $locations = $headers['location'];
696  $domain = '';
697  $foundRelativeURI = false;
698  $countLocations = count( $locations );
699 
700  for ( $i = $countLocations - 1; $i >= 0; $i-- ) {
701  $url = parse_url( $locations[$i] );
702 
703  if ( isset( $url['host'] ) ) {
704  $domain = $url['scheme'] . '://' . $url['host'];
705  break; // found correct URI (with host)
706  } else {
707  $foundRelativeURI = true;
708  }
709  }
710 
711  if ( $foundRelativeURI ) {
712  if ( $domain ) {
713  return $domain . $locations[$countLocations - 1];
714  } else {
715  $url = parse_url( $this->url );
716  if ( isset( $url['host'] ) ) {
717  return $url['scheme'] . '://' . $url['host'] .
718  $locations[$countLocations - 1];
719  }
720  }
721  } else {
722  return $locations[$countLocations - 1];
723  }
724  }
725 
726  return $this->url;
727  }
728 
734  public function canFollowRedirects() {
735  return true;
736  }
737 }
738 
743  const SUPPORTS_FILE_POSTS = true;
744 
745  protected $curlOptions = [];
746  protected $headerText = "";
747 
753  protected function readHeader( $fh, $content ) {
754  $this->headerText .= $content;
755  return strlen( $content );
756  }
757 
758  public function execute() {
759 
760  parent::execute();
761 
762  if ( !$this->status->isOK() ) {
763  return $this->status;
764  }
765 
766  $this->curlOptions[CURLOPT_PROXY] = $this->proxy;
767  $this->curlOptions[CURLOPT_TIMEOUT] = $this->timeout;
768 
769  // Only supported in curl >= 7.16.2
770  if ( defined( 'CURLOPT_CONNECTTIMEOUT_MS' ) ) {
771  $this->curlOptions[CURLOPT_CONNECTTIMEOUT_MS] = $this->connectTimeout * 1000;
772  }
773 
774  $this->curlOptions[CURLOPT_HTTP_VERSION] = CURL_HTTP_VERSION_1_0;
775  $this->curlOptions[CURLOPT_WRITEFUNCTION] = $this->callback;
776  $this->curlOptions[CURLOPT_HEADERFUNCTION] = [ $this, "readHeader" ];
777  $this->curlOptions[CURLOPT_MAXREDIRS] = $this->maxRedirects;
778  $this->curlOptions[CURLOPT_ENCODING] = ""; # Enable compression
779 
780  $this->curlOptions[CURLOPT_USERAGENT] = $this->reqHeaders['User-Agent'];
781 
782  $this->curlOptions[CURLOPT_SSL_VERIFYHOST] = $this->sslVerifyHost ? 2 : 0;
783  $this->curlOptions[CURLOPT_SSL_VERIFYPEER] = $this->sslVerifyCert;
784 
785  if ( $this->caInfo ) {
786  $this->curlOptions[CURLOPT_CAINFO] = $this->caInfo;
787  }
788 
789  if ( $this->headersOnly ) {
790  $this->curlOptions[CURLOPT_NOBODY] = true;
791  $this->curlOptions[CURLOPT_HEADER] = true;
792  } elseif ( $this->method == 'POST' ) {
793  $this->curlOptions[CURLOPT_POST] = true;
795  // Don't interpret POST parameters starting with '@' as file uploads, because this
796  // makes it impossible to POST plain values starting with '@' (and causes security
797  // issues potentially exposing the contents of local files).
798  // The PHP manual says this option was introduced in PHP 5.5 defaults to true in PHP 5.6,
799  // but we support lower versions, and the option doesn't exist in HHVM 5.6.99.
800  if ( defined( 'CURLOPT_SAFE_UPLOAD' ) ) {
801  $this->curlOptions[CURLOPT_SAFE_UPLOAD] = true;
802  } elseif ( is_array( $postData ) ) {
803  // In PHP 5.2 and later, '@' is interpreted as a file upload if POSTFIELDS
804  // is an array, but not if it's a string. So convert $req['body'] to a string
805  // for safety.
807  }
808  $this->curlOptions[CURLOPT_POSTFIELDS] = $postData;
809 
810  // Suppress 'Expect: 100-continue' header, as some servers
811  // will reject it with a 417 and Curl won't auto retry
812  // with HTTP 1.0 fallback
813  $this->reqHeaders['Expect'] = '';
814  } else {
815  $this->curlOptions[CURLOPT_CUSTOMREQUEST] = $this->method;
816  }
817 
818  $this->curlOptions[CURLOPT_HTTPHEADER] = $this->getHeaderList();
819 
820  $curlHandle = curl_init( $this->url );
821 
822  if ( !curl_setopt_array( $curlHandle, $this->curlOptions ) ) {
823  throw new MWException( "Error setting curl options." );
824  }
825 
826  if ( $this->followRedirects && $this->canFollowRedirects() ) {
827  MediaWiki\suppressWarnings();
828  if ( !curl_setopt( $curlHandle, CURLOPT_FOLLOWLOCATION, true ) ) {
829  wfDebug( __METHOD__ . ": Couldn't set CURLOPT_FOLLOWLOCATION. " .
830  "Probably open_basedir is set.\n" );
831  // Continue the processing. If it were in curl_setopt_array,
832  // processing would have halted on its entry
833  }
834  MediaWiki\restoreWarnings();
835  }
836 
837  if ( $this->profiler ) {
838  $profileSection = $this->profiler->scopedProfileIn(
839  __METHOD__ . '-' . $this->profileName
840  );
841  }
842 
843  $curlRes = curl_exec( $curlHandle );
844  if ( curl_errno( $curlHandle ) == CURLE_OPERATION_TIMEOUTED ) {
845  $this->status->fatal( 'http-timed-out', $this->url );
846  } elseif ( $curlRes === false ) {
847  $this->status->fatal( 'http-curl-error', curl_error( $curlHandle ) );
848  } else {
849  $this->headerList = explode( "\r\n", $this->headerText );
850  }
851 
852  curl_close( $curlHandle );
853 
854  if ( $this->profiler ) {
855  $this->profiler->scopedProfileOut( $profileSection );
856  }
857 
858  $this->parseHeader();
859  $this->setStatus();
860 
861  return $this->status;
862  }
863 
867  public function canFollowRedirects() {
868  $curlVersionInfo = curl_version();
869  if ( $curlVersionInfo['version_number'] < 0x071304 ) {
870  wfDebug( "Cannot follow redirects with libcurl < 7.19.4 due to CVE-2009-0037\n" );
871  return false;
872  }
873 
874  if ( version_compare( PHP_VERSION, '5.6.0', '<' ) ) {
875  if ( strval( ini_get( 'open_basedir' ) ) !== '' ) {
876  wfDebug( "Cannot follow redirects when open_basedir is set\n" );
877  return false;
878  }
879  }
880 
881  return true;
882  }
883 }
884 
886 
887  private $fopenErrors = [];
888 
893  protected function urlToTcp( $url ) {
894  $parsedUrl = parse_url( $url );
895 
896  return 'tcp://' . $parsedUrl['host'] . ':' . $parsedUrl['port'];
897  }
898 
908  protected function getCertOptions() {
909  $certOptions = [];
910  $certLocations = [];
911  if ( $this->caInfo ) {
912  $certLocations = [ 'manual' => $this->caInfo ];
913  } elseif ( version_compare( PHP_VERSION, '5.6.0', '<' ) ) {
914  // @codingStandardsIgnoreStart Generic.Files.LineLength
915  // Default locations, based on
916  // https://www.happyassassin.net/2015/01/12/a-note-about-ssltls-trusted-certificate-stores-and-platforms/
917  // PHP 5.5 and older doesn't have any defaults, so we try to guess ourselves.
918  // PHP 5.6+ gets the CA location from OpenSSL as long as it is not set manually,
919  // so we should leave capath/cafile empty there.
920  // @codingStandardsIgnoreEnd
921  $certLocations = array_filter( [
922  getenv( 'SSL_CERT_DIR' ),
923  getenv( 'SSL_CERT_PATH' ),
924  '/etc/pki/tls/certs/ca-bundle.crt', # Fedora et al
925  '/etc/ssl/certs', # Debian et al
926  '/etc/pki/tls/certs/ca-bundle.trust.crt',
927  '/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem',
928  '/System/Library/OpenSSL', # OSX
929  ] );
930  }
931 
932  foreach ( $certLocations as $key => $cert ) {
933  if ( is_dir( $cert ) ) {
934  $certOptions['capath'] = $cert;
935  break;
936  } elseif ( is_file( $cert ) ) {
937  $certOptions['cafile'] = $cert;
938  break;
939  } elseif ( $key === 'manual' ) {
940  // fail more loudly if a cert path was manually configured and it is not valid
941  throw new DomainException( "Invalid CA info passed: $cert" );
942  }
943  }
944 
945  return $certOptions;
946  }
947 
955  public function errorHandler( $errno, $errstr ) {
956  $n = count( $this->fopenErrors ) + 1;
957  $this->fopenErrors += [ "errno$n" => $errno, "errstr$n" => $errstr ];
958  }
959 
960  public function execute() {
961 
962  parent::execute();
963 
964  if ( is_array( $this->postData ) ) {
965  $this->postData = wfArrayToCgi( $this->postData );
966  }
967 
968  if ( $this->parsedUrl['scheme'] != 'http'
969  && $this->parsedUrl['scheme'] != 'https' ) {
970  $this->status->fatal( 'http-invalid-scheme', $this->parsedUrl['scheme'] );
971  }
972 
973  $this->reqHeaders['Accept'] = "*/*";
974  $this->reqHeaders['Connection'] = 'Close';
975  if ( $this->method == 'POST' ) {
976  // Required for HTTP 1.0 POSTs
977  $this->reqHeaders['Content-Length'] = strlen( $this->postData );
978  if ( !isset( $this->reqHeaders['Content-Type'] ) ) {
979  $this->reqHeaders['Content-Type'] = "application/x-www-form-urlencoded";
980  }
981  }
982 
983  // Set up PHP stream context
984  $options = [
985  'http' => [
986  'method' => $this->method,
987  'header' => implode( "\r\n", $this->getHeaderList() ),
988  'protocol_version' => '1.1',
989  'max_redirects' => $this->followRedirects ? $this->maxRedirects : 0,
990  'ignore_errors' => true,
991  'timeout' => $this->timeout,
992  // Curl options in case curlwrappers are installed
993  'curl_verify_ssl_host' => $this->sslVerifyHost ? 2 : 0,
994  'curl_verify_ssl_peer' => $this->sslVerifyCert,
995  ],
996  'ssl' => [
997  'verify_peer' => $this->sslVerifyCert,
998  'SNI_enabled' => true,
999  'ciphers' => 'HIGH:!SSLv2:!SSLv3:-ADH:-kDH:-kECDH:-DSS',
1000  'disable_compression' => true,
1001  ],
1002  ];
1003 
1004  if ( $this->proxy ) {
1005  $options['http']['proxy'] = $this->urlToTcp( $this->proxy );
1006  $options['http']['request_fulluri'] = true;
1007  }
1008 
1009  if ( $this->postData ) {
1010  $options['http']['content'] = $this->postData;
1011  }
1012 
1013  if ( $this->sslVerifyHost ) {
1014  // PHP 5.6.0 deprecates CN_match, in favour of peer_name which
1015  // actually checks SubjectAltName properly.
1016  if ( version_compare( PHP_VERSION, '5.6.0', '>=' ) ) {
1017  $options['ssl']['peer_name'] = $this->parsedUrl['host'];
1018  } else {
1019  $options['ssl']['CN_match'] = $this->parsedUrl['host'];
1020  }
1021  }
1022 
1023  $options['ssl'] += $this->getCertOptions();
1024 
1025  $context = stream_context_create( $options );
1026 
1027  $this->headerList = [];
1028  $reqCount = 0;
1029  $url = $this->url;
1030 
1031  $result = [];
1032 
1033  if ( $this->profiler ) {
1034  $profileSection = $this->profiler->scopedProfileIn(
1035  __METHOD__ . '-' . $this->profileName
1036  );
1037  }
1038  do {
1039  $reqCount++;
1040  $this->fopenErrors = [];
1041  set_error_handler( [ $this, 'errorHandler' ] );
1042  $fh = fopen( $url, "r", false, $context );
1043  restore_error_handler();
1044 
1045  if ( !$fh ) {
1046  // HACK for instant commons.
1047  // If we are contacting (commons|upload).wikimedia.org
1048  // try again with CN_match for en.wikipedia.org
1049  // as php does not handle SubjectAltName properly
1050  // prior to "peer_name" option in php 5.6
1051  if ( isset( $options['ssl']['CN_match'] )
1052  && ( $options['ssl']['CN_match'] === 'commons.wikimedia.org'
1053  || $options['ssl']['CN_match'] === 'upload.wikimedia.org' )
1054  ) {
1055  $options['ssl']['CN_match'] = 'en.wikipedia.org';
1056  $context = stream_context_create( $options );
1057  continue;
1058  }
1059  break;
1060  }
1061 
1062  $result = stream_get_meta_data( $fh );
1063  $this->headerList = $result['wrapper_data'];
1064  $this->parseHeader();
1065 
1066  if ( !$this->followRedirects ) {
1067  break;
1068  }
1069 
1070  # Handle manual redirection
1071  if ( !$this->isRedirect() || $reqCount > $this->maxRedirects ) {
1072  break;
1073  }
1074  # Check security of URL
1075  $url = $this->getResponseHeader( "Location" );
1076 
1077  if ( !Http::isValidURI( $url ) ) {
1078  wfDebug( __METHOD__ . ": insecure redirection\n" );
1079  break;
1080  }
1081  } while ( true );
1082  if ( $this->profiler ) {
1083  $this->profiler->scopedProfileOut( $profileSection );
1084  }
1085 
1086  $this->setStatus();
1087 
1088  if ( $fh === false ) {
1089  if ( $this->fopenErrors ) {
1090  LoggerFactory::getInstance( 'http' )->warning( __CLASS__
1091  . ': error opening connection: {errstr1}', $this->fopenErrors );
1092  }
1093  $this->status->fatal( 'http-request-error' );
1094  return $this->status;
1095  }
1096 
1097  if ( $result['timed_out'] ) {
1098  $this->status->fatal( 'http-timed-out', $this->url );
1099  return $this->status;
1100  }
1101 
1102  // If everything went OK, or we received some error code
1103  // get the response body content.
1104  if ( $this->status->isOK() || (int)$this->respStatus >= 300 ) {
1105  while ( !feof( $fh ) ) {
1106  $buf = fread( $fh, 8192 );
1107 
1108  if ( $buf === false ) {
1109  $this->status->fatal( 'http-read-error' );
1110  break;
1111  }
1112 
1113  if ( strlen( $buf ) ) {
1114  call_user_func( $this->callback, $fh, $buf );
1115  }
1116  }
1117  }
1118  fclose( $fh );
1119 
1120  return $this->status;
1121  }
1122 }
CookieJar $cookieJar
readHeader($fh, $content)
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
Definition: deferred.txt:11
magic word the default is to use $key to get the and $key value or $key value text $key value html to format the value $key
Definition: hooks.txt:2321
proxySetup()
Take care of setting up the proxy (do nothing if "noProxy" is set)
$wgVersion
MediaWiki version number.
$context
Definition: load.php:44
$batch execute()
setCookie($name, $value=null, $attr=null)
Sets a cookie.
static getProxy()
Gets the relevant proxy from $wgHTTPProxy.
execute()
Take care of whatever is necessary to perform the URI request.
per default it will return the text for text based content
canFollowRedirects()
Returns true if the backend can follow redirects.
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
setHeader($name, $value)
Set an arbitrary header.
static instance()
Singleton.
Definition: Profiler.php:60
setCookieJar($jar)
Tells the MWHttpRequest object to use this pre-loaded CookieJar.
isRedirect()
Returns true if the last status code was a redirect.
$value
when a variable name is used in a it is silently declared as a new local masking the global
Definition: design.txt:93
wfExpandUrl($url, $defaultProto=PROTO_CURRENT)
Expand a potentially local URL to a fully-qualified URL.
static newFatal($message)
Factory function for fatal errors.
Definition: Status.php:89
static canMakeRequests()
Simple function to test if we can make any sort of requests at all, using cURL or fopen() ...
MWHttpRequest implemented using internal curl compiled into PHP.
static request($method, $url, $options=[], $caller=__METHOD__)
Perform an HTTP request.
wfDebug($text, $dest= 'all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
static userAgent()
A standard user-agent we can use for external requests.
The index of the header message $result[1]=The index of the body text message $result[2 through n]=Parameters passed to body text message.Please note the header message cannot receive/use parameters. 'ImportHandleLogItemXMLTag':When parsing a XML tag in a log item.Return false to stop further processing of the tag $reader:XMLReader object $logInfo:Array of information 'ImportHandlePageXMLTag':When parsing a XML tag in a page.Return false to stop further processing of the tag $reader:XMLReader object &$pageInfo:Array of information 'ImportHandleRevisionXMLTag':When parsing a XML tag in a page revision.Return false to stop further processing of the tag $reader:XMLReader object $pageInfo:Array of page information $revisionInfo:Array of revision information 'ImportHandleToplevelXMLTag':When parsing a top level XML tag.Return false to stop further processing of the tag $reader:XMLReader object 'ImportHandleUploadXMLTag':When parsing a XML tag in a file upload.Return false to stop further processing of the tag $reader:XMLReader object $revisionInfo:Array of information 'ImportLogInterwikiLink':Hook to change the interwiki link used in log entries and edit summaries for transwiki imports.&$fullInterwikiPrefix:Interwiki prefix, may contain colons.&$pageTitle:String that contains page title. 'ImportSources':Called when reading from the $wgImportSources configuration variable.Can be used to lazy-load the import sources list.&$importSources:The value of $wgImportSources.Modify as necessary.See the comment in DefaultSettings.php for the detail of how to structure this array. 'InfoAction':When building information to display on the action=info page.$context:IContextSource object &$pageInfo:Array of information 'InitializeArticleMaybeRedirect':MediaWiki check to see if title is a redirect.&$title:Title object for the current page &$request:WebRequest &$ignoreRedirect:boolean to skip redirect check &$target:Title/string of redirect target &$article:Article object 'InternalParseBeforeLinks':during Parser's internalParse method before links but after nowiki/noinclude/includeonly/onlyinclude and other processings.&$parser:Parser object &$text:string containing partially parsed text &$stripState:Parser's internal StripState object 'InternalParseBeforeSanitize':during Parser's internalParse method just before the parser removes unwanted/dangerous HTML tags and after nowiki/noinclude/includeonly/onlyinclude and other processings.Ideal for syntax-extensions after template/parser function execution which respect nowiki and HTML-comments.&$parser:Parser object &$text:string containing partially parsed text &$stripState:Parser's internal StripState object 'InterwikiLoadPrefix':When resolving if a given prefix is an interwiki or not.Return true without providing an interwiki to continue interwiki search.$prefix:interwiki prefix we are looking for.&$iwData:output array describing the interwiki with keys iw_url, iw_local, iw_trans and optionally iw_api and iw_wikiid. 'InvalidateEmailComplete':Called after a user's email has been invalidated successfully.$user:user(object) whose email is being invalidated 'IRCLineURL':When constructing the URL to use in an IRC notification.Callee may modify $url and $query, URL will be constructed as $url.$query &$url:URL to index.php &$query:Query string $rc:RecentChange object that triggered url generation 'IsFileCacheable':Override the result of Article::isFileCacheable()(if true) &$article:article(object) being checked 'IsTrustedProxy':Override the result of IP::isTrustedProxy() &$ip:IP being check &$result:Change this value to override the result of IP::isTrustedProxy() 'IsUploadAllowedFromUrl':Override the result of UploadFromUrl::isAllowedUrl() $url:URL used to upload from &$allowed:Boolean indicating if uploading is allowed for given URL 'isValidEmailAddr':Override the result of Sanitizer::validateEmail(), for instance to return false if the domain name doesn't match your organization.$addr:The e-mail address entered by the user &$result:Set this and return false to override the internal checks 'isValidPassword':Override the result of User::isValidPassword() $password:The password entered by the user &$result:Set this and return false to override the internal checks $user:User the password is being validated for 'Language::getMessagesFileName':$code:The language code or the language we're looking for a messages file for &$file:The messages file path, you can override this to change the location. 'LanguageGetMagic':DEPRECATED!Use $magicWords in a file listed in $wgExtensionMessagesFiles instead.Use this to define synonyms of magic words depending of the language &$magicExtensions:associative array of magic words synonyms $lang:language code(string) 'LanguageGetNamespaces':Provide custom ordering for namespaces or remove namespaces.Do not use this hook to add namespaces.Use CanonicalNamespaces for that.&$namespaces:Array of namespaces indexed by their numbers 'LanguageGetSpecialPageAliases':DEPRECATED!Use $specialPageAliases in a file listed in $wgExtensionMessagesFiles instead.Use to define aliases of special pages names depending of the language &$specialPageAliases:associative array of magic words synonyms $lang:language code(string) 'LanguageGetTranslatedLanguageNames':Provide translated language names.&$names:array of language code=> language name $code:language of the preferred translations 'LanguageLinks':Manipulate a page's language links.This is called in various places to allow extensions to define the effective language links for a page.$title:The page's Title.&$links:Associative array mapping language codes to prefixed links of the form"language:title".&$linkFlags:Associative array mapping prefixed links to arrays of flags.Currently unused, but planned to provide support for marking individual language links in the UI, e.g.for featured articles. 'LanguageSelector':Hook to change the language selector available on a page.$out:The output page.$cssClassName:CSS class name of the language selector. 'LinkBegin':Used when generating internal and interwiki links in Linker::link(), before processing starts.Return false to skip default processing and return $ret.See documentation for Linker::link() for details on the expected meanings of parameters.$skin:the Skin object $target:the Title that the link is pointing to &$html:the contents that the< a > tag should have(raw HTML) $result
Definition: hooks.txt:1796
parseCookies()
Parse the cookies in the response headers and store them in the cookie jar.
if($line===false) $args
Definition: cdb.php:64
$last
global $wgCommandLineMode
Definition: Setup.php:513
getCookieJar()
Returns the cookie jar in use.
getHeaderList()
Get an array of the headers.
static $httpEngine
getFinalUrl()
Returns the final URL after all redirections.
Profiler $profiler
wfWarn($msg, $callerOffset=1, $level=E_USER_NOTICE)
Send a warning either to the debug log or in a PHP error depending on $wgDevelopmentWarnings.
wfIniGetBool($setting)
Safety wrapper around ini_get() for boolean settings.
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set and then return false from the hook function Ensure you consume the ChangeTagAfterDelete hook to carry out custom deletion actions as context called by AbstractContent::getParserOutput May be used to override the normal model specific rendering of page content as context as context $options
Definition: hooks.txt:1004
__construct($url, $options=[], $caller=__METHOD__, $profiler=null)
errorHandler($errno, $errstr)
Custom error handler for dealing with fopen() errors.
const SUPPORTS_FILE_POSTS
read($fh, $content)
A generic callback to read the body of the response from a remote server.
getCertOptions()
Returns an array with a 'capath' or 'cafile' key that is suitable to be merged into the 'ssl' sub-arr...
setStatus()
Sets HTTPRequest status member to a fatal value with the error message if the returned integer value ...
MediaWiki has optional support for a high distributed memory object caching system For general information on but for a larger site with heavy like it should help lighten the load on the database servers by caching data and objects in Debian
Definition: memcached.txt:10
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
setCallback($callback)
Set a read callback to accept data read from the HTTP request.
getResponseHeaders()
Returns an associative array of response headers after the request has been executed.
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that probably a stub it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output modifiable & $code
Definition: hooks.txt:762
const PROTO_HTTP
Definition: Defines.php:261
getStatus()
Get the integer value of the HTTP status code (e.g.
parseHeader()
Parses the headers, including the HTTP status code and any Set-Cookie headers.
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
this hook is for auditing only $req
Definition: hooks.txt:965
setUserAgent($UA)
Set the user agent.
wfArrayToCgi($array1, $array2=null, $prefix= '')
This function takes one or two arrays as input, and returns a CGI-style string, e.g.
static post($url, $options=[], $caller=__METHOD__)
Simple wrapper for Http::request( 'POST' )
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set $status
Definition: hooks.txt:1004
MediaWiki Logger LoggerFactory implements a PSR[0] compatible message logging system Named Psr Log LoggerInterface instances can be obtained from the MediaWiki Logger LoggerFactory::getInstance() static method.MediaWiki\Logger\LoggerFactory expects a class implementing the MediaWiki\Logger\Spi interface to act as a factory for new Psr\Log\LoggerInterface instances.The"Spi"in MediaWiki\Logger\Spi stands for"service provider interface".An SPI is an API intended to be implemented or extended by a third party.This software design pattern is intended to enable framework extension and replaceable components.It is specifically used in the MediaWiki\Logger\LoggerFactory service to allow alternate PSR-3 logging implementations to be easily integrated with MediaWiki.The service provider interface allows the backend logging library to be implemented in multiple ways.The $wgMWLoggerDefaultSpi global provides the classname of the default MediaWiki\Logger\Spi implementation to be loaded at runtime.This can either be the name of a class implementing the MediaWiki\Logger\Spi with a zero argument const ructor or a callable that will return an MediaWiki\Logger\Spi instance.Alternately the MediaWiki\Logger\LoggerFactory MediaWiki Logger LoggerFactory
Definition: logger.txt:5
static isLocalURL($url)
Check if the URL can be served by localhost.
static factory($url, $options=null, $caller=__METHOD__)
Generate a new request object.
getContent()
Get the body, or content, of the response to the request.
wfParseUrl($url)
parse_url() work-alike, but non-broken.
setData($args)
Set the parameters of the request.
setCookie($name, $value, $attr)
Set a cookie in the cookie jar.
Definition: CookieJar.php:32
static isValidURI($uri)
Checks that the given URI is a valid one.
getResponseHeader($header)
Returns the value of the given response header.
static newGood($value=null)
Factory function for good results.
Definition: Status.php:101
$matches
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:310