4 * Various HTTP related functions
8 * Get the contents of a file by HTTP
10 * if $timeout is 'default', $wgHTTPTimeout is used
12 static function get( $url, $timeout = 'default' ) {
13 global $wgHTTPTimeout, $wgHTTPProxy, $wgVersion, $wgTitle;
15 # Use curl if available
16 if ( function_exists( 'curl_init' ) ) {
17 $c = curl_init( $url );
18 if ( wfIsLocalURL( $url ) ) {
19 curl_setopt( $c, CURLOPT_PROXY
, 'localhost:80' );
20 } else if ($wgHTTPProxy) {
21 curl_setopt($c, CURLOPT_PROXY
, $wgHTTPProxy);
24 if ( $timeout == 'default' ) {
25 $timeout = $wgHTTPTimeout;
27 curl_setopt( $c, CURLOPT_TIMEOUT
, $timeout );
28 curl_setopt( $c, CURLOPT_USERAGENT
, "MediaWiki/$wgVersion" );
30 # Set the referer to $wgTitle, even in command-line mode
31 # This is useful for interwiki transclusion, where the foreign
32 # server wants to know what the referring page is.
33 # $_SERVER['REQUEST_URI'] gives a less reliable indication of the
35 if ( is_object( $wgTitle ) ) {
36 curl_setopt( $c, CURLOPT_REFERER
, $wgTitle->getFullURL() );
41 $text = ob_get_contents();
44 # Don't return the text of error messages, return false on error
45 if ( curl_getinfo( $c, CURLINFO_HTTP_CODE
) != 200 ) {
50 # Otherwise use file_get_contents, or its compatibility function from GlobalFunctions.php
51 # This may take 3 minutes to time out, and doesn't have local fetch capabilities
52 $url_fopen = ini_set( 'allow_url_fopen', 1 );
53 $text = file_get_contents( $url );
54 ini_set( 'allow_url_fopen', $url_fopen );
60 * Check if the URL can be served by localhost
62 static function isLocalURL( $url ) {
63 global $wgCommandLineMode, $wgConf;
64 if ( $wgCommandLineMode ) {
70 if ( preg_match( '!^http://([\w.-]+)[/:].*$!', $url, $matches ) ) {
73 $domainParts = explode( '.', $host );
74 // Check if this domain or any superdomain is listed in $wgConf as a local virtual host
75 $domainParts = array_reverse( $domainParts );
76 for ( $i = 0; $i < count( $domainParts ); $i++
) {
77 $domainPart = $domainParts[$i];
79 $domain = $domainPart;
81 $domain = $domainPart . '.' . $domain;
83 if ( $wgConf->isLocalVHost( $domain ) ) {