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