3 * Various HTTP related functions
7 * Get the contents of a file by HTTP
9 * if $timeout is 'default', $wgHTTPTimeout is used
11 function wfGetHTTP( $url, $timeout = 'default' ) {
12 global $wgHTTPTimeout, $wgHTTPProxy;
14 # Use curl if available
15 if ( function_exists( 'curl_init' ) ) {
16 $c = curl_init( $url );
17 if ( wfIsLocalURL( $url ) ) {
18 curl_setopt( $c, CURLOPT_PROXY
, 'localhost:80' );
19 } else if ($wgHTTPProxy)
20 curl_setopt($c, CURLOPT_PROXY
, $wgHTTPProxy);
22 if ( $timeout == 'default' ) {
23 $timeout = $wgHTTPTimeout;
25 curl_setopt( $c, CURLOPT_TIMEOUT
, $timeout );
28 $text = ob_get_contents();
31 # Don't return the text of error messages, return false on error
32 if ( curl_getinfo( $c, CURLINFO_HTTP_CODE
) != 200 ) {
37 # Otherwise use file_get_contents, or its compatibility function from GlobalFunctions.php
38 # This may take 3 minutes to time out, and doesn't have local fetch capabilities
39 $url_fopen = ini_set( 'allow_url_fopen', 1 );
40 $text = file_get_contents( $url );
41 ini_set( 'allow_url_fopen', $url_fopen );
47 * Check if the URL can be served by localhost
49 function wfIsLocalURL( $url ) {
50 global $wgCommandLineMode, $wgConf;
51 if ( $wgCommandLineMode ) {
57 if ( preg_match( '!^http://([\w.-]+)[/:].*$!', $url, $matches ) ) {
60 $domainParts = explode( '.', $host );
61 // Check if this domain or any superdomain is listed in $wgConf as a local virtual host
62 $domainParts = array_reverse( $domainParts );
63 for ( $i = 0; $i < count( $domainParts ); $i++
) {
64 $domainPart = $domainParts[$i];
66 $domain = $domainPart;
68 $domain = $domainPart . '.' . $domain;
70 if ( $wgConf->isLocalVHost( $domain ) ) {