10 * Various HTTP related functions
14 static function get( $url, $timeout = 'default', $opts = array() ) {
15 return Http
::request( "GET", $url, $timeout, $opts );
18 static function post( $url, $timeout = 'default', $opts = array() ) {
19 return Http
::request( "POST", $url, $timeout, $opts );
23 * Get the contents of a file by HTTP
25 * if $timeout is 'default', $wgHTTPTimeout is used
27 static function request( $method, $url, $timeout = 'default', $curlOptions = array() ) {
28 global $wgHTTPTimeout, $wgHTTPProxy, $wgVersion, $wgTitle;
30 // Go ahead and set the timeout if not otherwise specified
31 if ( $timeout == 'default' ) {
32 $timeout = $wgHTTPTimeout;
35 wfDebug( __METHOD__
. ": $method $url\n" );
36 # Use curl if available
37 if ( function_exists( 'curl_init' ) ) {
38 $c = curl_init( $url );
39 if ( self
::isLocalURL( $url ) ) {
40 curl_setopt( $c, CURLOPT_PROXY
, 'localhost:80' );
41 } else if ($wgHTTPProxy) {
42 curl_setopt($c, CURLOPT_PROXY
, $wgHTTPProxy);
45 curl_setopt( $c, CURLOPT_TIMEOUT
, $timeout );
46 curl_setopt( $c, CURLOPT_USERAGENT
, "MediaWiki/$wgVersion" );
47 if ( $method == 'POST' )
48 curl_setopt( $c, CURLOPT_POST
, true );
50 curl_setopt( $c, CURLOPT_CUSTOMREQUEST
, $method );
52 # Set the referer to $wgTitle, even in command-line mode
53 # This is useful for interwiki transclusion, where the foreign
54 # server wants to know what the referring page is.
55 # $_SERVER['REQUEST_URI'] gives a less reliable indication of the
57 if ( is_object( $wgTitle ) ) {
58 curl_setopt( $c, CURLOPT_REFERER
, $wgTitle->getFullURL() );
61 if ( is_array( $curlOptions ) ) {
62 foreach( $curlOptions as $option => $value ) {
63 curl_setopt( $c, $option, $value );
69 $text = ob_get_contents();
72 # Don't return the text of error messages, return false on error
73 if ( curl_getinfo( $c, CURLINFO_HTTP_CODE
) != 200 ) {
76 # Don't return truncated output
77 if ( curl_errno( $c ) != CURLE_OK
) {
82 # Otherwise use file_get_contents...
83 # This doesn't have local fetch capabilities...
86 $headers = array( "User-Agent: MediaWiki/$wgVersion" );
87 if( strcasecmp( $method, 'post' ) == 0 ) {
88 // Required for HTTP 1.0 POSTs
89 $headers[] = "Content-Length: 0";
94 'header' => implode( "\r\n", $headers ),
95 'timeout' => $timeout ) );
96 $ctx = stream_context_create($opts);
98 $url_fopen = ini_set( 'allow_url_fopen', 1 );
99 $text = file_get_contents( $url, false, $ctx );
100 ini_set( 'allow_url_fopen', $url_fopen );
106 * Check if the URL can be served by localhost
108 static function isLocalURL( $url ) {
109 global $wgCommandLineMode, $wgConf;
110 if ( $wgCommandLineMode ) {
116 if ( preg_match( '!^http://([\w.-]+)[/:].*$!', $url, $matches ) ) {
119 $domainParts = explode( '.', $host );
120 // Check if this domain or any superdomain is listed in $wgConf as a local virtual host
121 $domainParts = array_reverse( $domainParts );
122 for ( $i = 0; $i < count( $domainParts ); $i++
) {
123 $domainPart = $domainParts[$i];
125 $domain = $domainPart;
127 $domain = $domainPart . '.' . $domain;
129 if ( $wgConf->isLocalVHost( $domain ) ) {