Live hack: Skip some work on empty category/link sets
[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, $wgVersion, $wgTitle;
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
23 if ( $timeout == 'default' ) {
24 $timeout = $wgHTTPTimeout;
25 }
26 curl_setopt( $c, CURLOPT_TIMEOUT, $timeout );
27 curl_setopt( $c, CURLOPT_USERAGENT, "MediaWiki/$wgVersion" );
28
29 # Set the referer to $wgTitle, even in command-line mode
30 # This is useful for interwiki transclusion, where the foreign
31 # server wants to know what the referring page is.
32 # $_SERVER['REQUEST_URI'] gives a less reliable indication of the
33 # referring page.
34 if ( is_object( $wgTitle ) ) {
35 curl_setopt( $c, CURLOPT_REFERER, $wgTitle->getFullURL() );
36 }
37
38 ob_start();
39 curl_exec( $c );
40 $text = ob_get_contents();
41 ob_end_clean();
42
43 # Don't return the text of error messages, return false on error
44 if ( curl_getinfo( $c, CURLINFO_HTTP_CODE ) != 200 ) {
45 $text = false;
46 }
47 curl_close( $c );
48 } else {
49 # Otherwise use file_get_contents, or its compatibility function from GlobalFunctions.php
50 # This may take 3 minutes to time out, and doesn't have local fetch capabilities
51 $url_fopen = ini_set( 'allow_url_fopen', 1 );
52 $text = file_get_contents( $url );
53 ini_set( 'allow_url_fopen', $url_fopen );
54 }
55 return $text;
56 }
57
58 /**
59 * Check if the URL can be served by localhost
60 */
61 function wfIsLocalURL( $url ) {
62 global $wgCommandLineMode, $wgConf;
63 if ( $wgCommandLineMode ) {
64 return false;
65 }
66
67 // Extract host part
68 $matches = array();
69 if ( preg_match( '!^http://([\w.-]+)[/:].*$!', $url, $matches ) ) {
70 $host = $matches[1];
71 // Split up dotwise
72 $domainParts = explode( '.', $host );
73 // Check if this domain or any superdomain is listed in $wgConf as a local virtual host
74 $domainParts = array_reverse( $domainParts );
75 for ( $i = 0; $i < count( $domainParts ); $i++ ) {
76 $domainPart = $domainParts[$i];
77 if ( $i == 0 ) {
78 $domain = $domainPart;
79 } else {
80 $domain = $domainPart . '.' . $domain;
81 }
82 if ( $wgConf->isLocalVHost( $domain ) ) {
83 return true;
84 }
85 }
86 }
87 return false;
88 }
89
90 ?>