- proxy support for wfGetHTTP()
[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, $wgHTTPProxy;
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 } else if ($wgHTTPProxy)
18 curl_setopt($c, CURLOPT_PROXY, $wgHTTPProxy);
19
20 if ( $timeout == 'default' ) {
21 $timeout = $wgHTTPTimeout;
22 }
23 curl_setopt( $c, CURLOPT_TIMEOUT, $timeout );
24 ob_start();
25 curl_exec( $c );
26 $text = ob_get_contents();
27 ob_end_clean();
28 curl_close( $c );
29 } else {
30 # Otherwise use file_get_contents, or its compatibility function from GlobalFunctions.php
31 # This may take 3 minutes to time out, and doesn't have local fetch capabilities
32 $url_fopen = ini_set( 'allow_url_fopen', 1 );
33 $text = file_get_contents( $url );
34 ini_set( 'allow_url_fopen', $url_fopen );
35 }
36 return $text;
37 }
38
39 /**
40 * Check if the URL can be served by localhost
41 */
42 function wfIsLocalURL( $url ) {
43 global $wgConf;
44 // Extract host part
45 if ( preg_match( '!^http://([\w.-]+)[/:].*$!', $url, $matches ) ) {
46 $host = $matches[1];
47 // Split up dotwise
48 $domainParts = explode( '.', $host );
49 // Check if this domain or any superdomain is listed in $wgConf as a local virtual host
50 $domainParts = array_reverse( $domainParts );
51 for ( $i = 0; $i < count( $domainParts ); $i++ ) {
52 $domainPart = $domainParts[$i];
53 if ( $i == 0 ) {
54 $domain = $domainPart;
55 } else {
56 $domain = $domainPart . '.' . $domain;
57 }
58 if ( $wgConf->isLocalVHost( $domain ) ) {
59 return true;
60 }
61 }
62 }
63 return false;
64 }
65
66 ?>