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