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