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