Fixes and improvements to interwiki transclusion:
[lhc/web/wiklou.git] / includes / HttpFunctions.php
1 <?php
2 /**
3 * Various HTTP related functions
4 */
5
6 /**
7 * Get the contents of a file by HTTP
8 *
9 * if $timeout is 'default', $wgHTTPTimeout is used
10 */
11 function wfGetHTTP( $url, $timeout = 'default' ) {
12 global $wgHTTPTimeout, $wgHTTPProxy, $wgVersion;
13
14 # Use curl if available
15 if ( function_exists( 'curl_init' ) ) {
16 $c = curl_init( $url );
17 if ( wfIsLocalURL( $url ) ) {
18 curl_setopt( $c, CURLOPT_PROXY, 'localhost:80' );
19 } else if ($wgHTTPProxy) {
20 curl_setopt($c, CURLOPT_PROXY, $wgHTTPProxy);
21 }
22
23 if ( $timeout == 'default' ) {
24 $timeout = $wgHTTPTimeout;
25 }
26 curl_setopt( $c, CURLOPT_TIMEOUT, $timeout );
27 curl_setopt( $c, CURLOPT_USERAGENT, "MediaWiki/$wgVersion" );
28 ob_start();
29 curl_exec( $c );
30 $text = ob_get_contents();
31 ob_end_clean();
32
33 # Don't return the text of error messages, return false on error
34 if ( curl_getinfo( $c, CURLINFO_HTTP_CODE ) != 200 ) {
35 $text = false;
36 }
37 curl_close( $c );
38 } else {
39 # Otherwise use file_get_contents, or its compatibility function from GlobalFunctions.php
40 # This may take 3 minutes to time out, and doesn't have local fetch capabilities
41 $url_fopen = ini_set( 'allow_url_fopen', 1 );
42 $text = file_get_contents( $url );
43 ini_set( 'allow_url_fopen', $url_fopen );
44 }
45 return $text;
46 }
47
48 /**
49 * Check if the URL can be served by localhost
50 */
51 function wfIsLocalURL( $url ) {
52 global $wgCommandLineMode, $wgConf;
53 if ( $wgCommandLineMode ) {
54 return false;
55 }
56
57 // Extract host part
58 $matches = array();
59 if ( preg_match( '!^http://([\w.-]+)[/:].*$!', $url, $matches ) ) {
60 $host = $matches[1];
61 // Split up dotwise
62 $domainParts = explode( '.', $host );
63 // Check if this domain or any superdomain is listed in $wgConf as a local virtual host
64 $domainParts = array_reverse( $domainParts );
65 for ( $i = 0; $i < count( $domainParts ); $i++ ) {
66 $domainPart = $domainParts[$i];
67 if ( $i == 0 ) {
68 $domain = $domainPart;
69 } else {
70 $domain = $domainPart . '.' . $domain;
71 }
72 if ( $wgConf->isLocalVHost( $domain ) ) {
73 return true;
74 }
75 }
76 }
77 return false;
78 }
79
80 ?>