* (bug 15497) Removed encoding attribute from <?xml ?> tags
[mediawiki.git] / includes / HttpFunctions.php
blob8ae4d2dfa13552005f9e224e9c3b8a907c2d7f70
1 <?php
3 /**
4 * Various HTTP related functions
5 * @ingroup HTTP
6 */
7 class Http {
8 static function get( $url, $timeout = 'default' ) {
9 return Http::request( "GET", $url, $timeout );
12 static function post( $url, $timeout = 'default' ) {
13 return Http::request( "POST", $url, $timeout );
16 /**
17 * Get the contents of a file by HTTP
19 * if $timeout is 'default', $wgHTTPTimeout is used
21 static function request( $method, $url, $timeout = 'default' ) {
22 global $wgHTTPTimeout, $wgHTTPProxy, $wgVersion, $wgTitle;
24 wfDebug( __METHOD__ . ": $method $url\n" );
25 # Use curl if available
26 if ( function_exists( 'curl_init' ) ) {
27 $c = curl_init( $url );
28 if ( self::isLocalURL( $url ) ) {
29 curl_setopt( $c, CURLOPT_PROXY, 'localhost:80' );
30 } else if ($wgHTTPProxy) {
31 curl_setopt($c, CURLOPT_PROXY, $wgHTTPProxy);
34 if ( $timeout == 'default' ) {
35 $timeout = $wgHTTPTimeout;
37 curl_setopt( $c, CURLOPT_TIMEOUT, $timeout );
38 curl_setopt( $c, CURLOPT_USERAGENT, "MediaWiki/$wgVersion" );
39 if ( $method == 'POST' )
40 curl_setopt( $c, CURLOPT_POST, true );
41 else
42 curl_setopt( $c, CURLOPT_CUSTOMREQUEST, $method );
44 # Set the referer to $wgTitle, even in command-line mode
45 # This is useful for interwiki transclusion, where the foreign
46 # server wants to know what the referring page is.
47 # $_SERVER['REQUEST_URI'] gives a less reliable indication of the
48 # referring page.
49 if ( is_object( $wgTitle ) ) {
50 curl_setopt( $c, CURLOPT_REFERER, $wgTitle->getFullURL() );
53 ob_start();
54 curl_exec( $c );
55 $text = ob_get_contents();
56 ob_end_clean();
58 # Don't return the text of error messages, return false on error
59 if ( curl_getinfo( $c, CURLINFO_HTTP_CODE ) != 200 ) {
60 $text = false;
62 # Don't return truncated output
63 if ( curl_errno( $c ) != CURLE_OK ) {
64 $text = false;
66 curl_close( $c );
67 } else {
68 # Otherwise use file_get_contents...
69 # This may take 3 minutes to time out, and doesn't have local fetch capabilities
71 global $wgVersion;
72 $headers = array( "User-Agent: MediaWiki/$wgVersion" );
73 if( strcasecmp( $method, 'post' ) == 0 ) {
74 // Required for HTTP 1.0 POSTs
75 $headers[] = "Content-Length: 0";
77 $opts = array(
78 'http' => array(
79 'method' => $method,
80 'header' => implode( "\r\n", $headers ) ) );
81 $ctx = stream_context_create($opts);
83 $url_fopen = ini_set( 'allow_url_fopen', 1 );
84 $text = file_get_contents( $url, false, $ctx );
85 ini_set( 'allow_url_fopen', $url_fopen );
87 return $text;
90 /**
91 * Check if the URL can be served by localhost
93 static function isLocalURL( $url ) {
94 global $wgCommandLineMode, $wgConf;
95 if ( $wgCommandLineMode ) {
96 return false;
99 // Extract host part
100 $matches = array();
101 if ( preg_match( '!^http://([\w.-]+)[/:].*$!', $url, $matches ) ) {
102 $host = $matches[1];
103 // Split up dotwise
104 $domainParts = explode( '.', $host );
105 // Check if this domain or any superdomain is listed in $wgConf as a local virtual host
106 $domainParts = array_reverse( $domainParts );
107 for ( $i = 0; $i < count( $domainParts ); $i++ ) {
108 $domainPart = $domainParts[$i];
109 if ( $i == 0 ) {
110 $domain = $domainPart;
111 } else {
112 $domain = $domainPart . '.' . $domain;
114 if ( $wgConf->isLocalVHost( $domain ) ) {
115 return true;
119 return false;