4 * Various HTTP related functions
7 static function get( $url, $timeout = 'default' ) {
8 return Http
::request( "GET", $url, $timeout );
11 static function post( $url, $timeout = 'default' ) {
12 return Http
::request( "POST", $url, $timeout );
16 * Get the contents of a file by HTTP
18 * if $timeout is 'default', $wgHTTPTimeout is used
20 static function request( $method, $url, $timeout = 'default' ) {
21 global $wgHTTPTimeout, $wgHTTPProxy, $wgVersion, $wgTitle;
23 # Use curl if available
24 if ( function_exists( 'curl_init' ) ) {
25 $c = curl_init( $url );
26 if ( wfIsLocalURL( $url ) ) {
27 curl_setopt( $c, CURLOPT_PROXY
, 'localhost:80' );
28 } else if ($wgHTTPProxy) {
29 curl_setopt($c, CURLOPT_PROXY
, $wgHTTPProxy);
32 if ( $timeout == 'default' ) {
33 $timeout = $wgHTTPTimeout;
35 curl_setopt( $c, CURLOPT_TIMEOUT
, $timeout );
36 curl_setopt( $c, CURLOPT_USERAGENT
, "MediaWiki/$wgVersion" );
37 if ( $method == 'POST' )
38 curl_setopt( $c, CURLOPT_POST
, true );
40 curl_setopt( $c, CURLOPT_CUSTOMREQUEST
, $method );
42 # Set the referer to $wgTitle, even in command-line mode
43 # This is useful for interwiki transclusion, where the foreign
44 # server wants to know what the referring page is.
45 # $_SERVER['REQUEST_URI'] gives a less reliable indication of the
47 if ( is_object( $wgTitle ) ) {
48 curl_setopt( $c, CURLOPT_REFERER
, $wgTitle->getFullURL() );
53 $text = ob_get_contents();
56 # Don't return the text of error messages, return false on error
57 if ( curl_getinfo( $c, CURLINFO_HTTP_CODE
) != 200 ) {
62 # Otherwise use file_get_contents, or its compatibility function from GlobalFunctions.php
63 # This may take 3 minutes to time out, and doesn't have local fetch capabilities
65 $opts = array('http' => array( 'method' => $method ) );
66 $ctx = stream_context_create($opts);
68 $url_fopen = ini_set( 'allow_url_fopen', 1 );
69 $text = file_get_contents( $url, false, $ctx );
70 ini_set( 'allow_url_fopen', $url_fopen );
76 * Check if the URL can be served by localhost
78 static function isLocalURL( $url ) {
79 global $wgCommandLineMode, $wgConf;
80 if ( $wgCommandLineMode ) {
86 if ( preg_match( '!^http://([\w.-]+)[/:].*$!', $url, $matches ) ) {
89 $domainParts = explode( '.', $host );
90 // Check if this domain or any superdomain is listed in $wgConf as a local virtual host
91 $domainParts = array_reverse( $domainParts );
92 for ( $i = 0; $i < count( $domainParts ); $i++
) {
93 $domainPart = $domainParts[$i];
95 $domain = $domainPart;
97 $domain = $domainPart . '.' . $domain;
99 if ( $wgConf->isLocalVHost( $domain ) ) {