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