Remove never used stuff. There is some $rand = wfRandom calls that might
[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;
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 if ( $timeout == 'default' ) {
23 $timeout = $wgHTTPTimeout;
24 }
25 curl_setopt( $c, CURLOPT_TIMEOUT, $timeout );
26 ob_start();
27 curl_exec( $c );
28 $text = ob_get_contents();
29 ob_end_clean();
30
31 # Don't return the text of error messages, return false on error
32 if ( curl_getinfo( $c, CURLINFO_HTTP_CODE ) != 200 ) {
33 $text = false;
34 }
35 curl_close( $c );
36 } else {
37 # Otherwise use file_get_contents, or its compatibility function from GlobalFunctions.php
38 # This may take 3 minutes to time out, and doesn't have local fetch capabilities
39 $url_fopen = ini_set( 'allow_url_fopen', 1 );
40 $text = file_get_contents( $url );
41 ini_set( 'allow_url_fopen', $url_fopen );
42 }
43 return $text;
44 }
45
46 /**
47 * Check if the URL can be served by localhost
48 */
49 function wfIsLocalURL( $url ) {
50 global $wgCommandLineMode, $wgConf;
51 if ( $wgCommandLineMode ) {
52 return false;
53 }
54
55 // Extract host part
56 if ( preg_match( '!^http://([\w.-]+)[/:].*$!', $url, $matches ) ) {
57 $host = $matches[1];
58 // Split up dotwise
59 $domainParts = explode( '.', $host );
60 // Check if this domain or any superdomain is listed in $wgConf as a local virtual host
61 $domainParts = array_reverse( $domainParts );
62 for ( $i = 0; $i < count( $domainParts ); $i++ ) {
63 $domainPart = $domainParts[$i];
64 if ( $i == 0 ) {
65 $domain = $domainPart;
66 } else {
67 $domain = $domainPart . '.' . $domain;
68 }
69 if ( $wgConf->isLocalVHost( $domain ) ) {
70 return true;
71 }
72 }
73 }
74 return false;
75 }
76
77 ?>