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