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