Use AutoLoader to load classes:
[lhc/web/wiklou.git] / includes / HttpFunctions.php
1 <?php
2 /**
3 * Various HTTP related functions
4 */
5
6 class HttpFunctions {
7 /**
8 * Get the contents of a file by HTTP
9 *
10 * if $timeout is 'default', $wgHTTPTimeout is used
11 */
12 function getHTTP( $url, $timeout = 'default' ) {
13 global $wgHTTPTimeout, $wgHTTPProxy, $wgVersion, $wgTitle;
14
15 # Use curl if available
16 if ( function_exists( 'curl_init' ) ) {
17 $c = curl_init( $url );
18 if ( HttpFunctions::isLocalURL( $url ) ) {
19 curl_setopt( $c, CURLOPT_PROXY, 'localhost:80' );
20 } else if ($wgHTTPProxy) {
21 curl_setopt($c, CURLOPT_PROXY, $wgHTTPProxy);
22 }
23
24 if ( $timeout == 'default' ) {
25 $timeout = $wgHTTPTimeout;
26 }
27 curl_setopt( $c, CURLOPT_TIMEOUT, $timeout );
28 curl_setopt( $c, CURLOPT_USERAGENT, "MediaWiki/$wgVersion" );
29
30 # Set the referer to $wgTitle, even in command-line mode
31 # This is useful for interwiki transclusion, where the foreign
32 # server wants to know what the referring page is.
33 # $_SERVER['REQUEST_URI'] gives a less reliable indication of the
34 # referring page.
35 if ( is_object( $wgTitle ) ) {
36 curl_setopt( $c, CURLOPT_REFERER, $wgTitle->getFullURL() );
37 }
38
39 ob_start();
40 curl_exec( $c );
41 $text = ob_get_contents();
42 ob_end_clean();
43
44 # Don't return the text of error messages, return false on error
45 if ( curl_getinfo( $c, CURLINFO_HTTP_CODE ) != 200 ) {
46 $text = false;
47 }
48 curl_close( $c );
49 } else {
50 # Otherwise use file_get_contents, or its compatibility function from GlobalFunctions.php
51 # This may take 3 minutes to time out, and doesn't have local fetch capabilities
52 $url_fopen = ini_set( 'allow_url_fopen', 1 );
53 $text = file_get_contents( $url );
54 ini_set( 'allow_url_fopen', $url_fopen );
55 }
56 return $text;
57 }
58
59 /**
60 * Check if the URL can be served by localhost
61 */
62 function isLocalURL( $url ) {
63 global $wgCommandLineMode, $wgConf;
64 if ( $wgCommandLineMode ) {
65 return false;
66 }
67
68 // Extract host part
69 $matches = array();
70 if ( preg_match( '!^http://([\w.-]+)[/:].*$!', $url, $matches ) ) {
71 $host = $matches[1];
72 // Split up dotwise
73 $domainParts = explode( '.', $host );
74 // Check if this domain or any superdomain is listed in $wgConf as a local virtual host
75 $domainParts = array_reverse( $domainParts );
76 for ( $i = 0; $i < count( $domainParts ); $i++ ) {
77 $domainPart = $domainParts[$i];
78 if ( $i == 0 ) {
79 $domain = $domainPart;
80 } else {
81 $domain = $domainPart . '.' . $domain;
82 }
83 if ( $wgConf->isLocalVHost( $domain ) ) {
84 return true;
85 }
86 }
87 }
88 return false;
89 }
90 }
91 ?>