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