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