some useless calls / unitialized $matches arrays
[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 $matches = array();
57 if ( preg_match( '!^http://([\w.-]+)[/:].*$!', $url, $matches ) ) {
58 $host = $matches[1];
59 // Split up dotwise
60 $domainParts = explode( '.', $host );
61 // Check if this domain or any superdomain is listed in $wgConf as a local virtual host
62 $domainParts = array_reverse( $domainParts );
63 for ( $i = 0; $i < count( $domainParts ); $i++ ) {
64 $domainPart = $domainParts[$i];
65 if ( $i == 0 ) {
66 $domain = $domainPart;
67 } else {
68 $domain = $domainPart . '.' . $domain;
69 }
70 if ( $wgConf->isLocalVHost( $domain ) ) {
71 return true;
72 }
73 }
74 }
75 return false;
76 }
77
78 ?>