Fix regression in even sizing of diff columns; forgot to restore a couple bits I...
[mediawiki.git] / includes / HttpFunctions.php
blob16a6348252e411096431517a1b23af753d9bf35c
1 <?php
3 /**
4 * Various HTTP related functions
5 */
6 class Http {
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 );
15 /**
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 );
39 else
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
46 # referring page.
47 if ( is_object( $wgTitle ) ) {
48 curl_setopt( $c, CURLOPT_REFERER, $wgTitle->getFullURL() );
51 ob_start();
52 curl_exec( $c );
53 $text = ob_get_contents();
54 ob_end_clean();
56 # Don't return the text of error messages, return false on error
57 if ( curl_getinfo( $c, CURLINFO_HTTP_CODE ) != 200 ) {
58 $text = false;
60 curl_close( $c );
61 } else {
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 );
72 return $text;
75 /**
76 * Check if the URL can be served by localhost
78 static function isLocalURL( $url ) {
79 global $wgCommandLineMode, $wgConf;
80 if ( $wgCommandLineMode ) {
81 return false;
84 // Extract host part
85 $matches = array();
86 if ( preg_match( '!^http://([\w.-]+)[/:].*$!', $url, $matches ) ) {
87 $host = $matches[1];
88 // Split up dotwise
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];
94 if ( $i == 0 ) {
95 $domain = $domainPart;
96 } else {
97 $domain = $domainPart . '.' . $domain;
99 if ( $wgConf->isLocalVHost( $domain ) ) {
100 return true;
104 return false;