]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/HttpFunctions.php
MediaWiki 1.14.0
[autoinstalls/mediawiki.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                                 curl_setopt( $c, CURLOPT_POSTFIELDS, '' );
62                         }
63                         else
64                                 curl_setopt( $c, CURLOPT_CUSTOMREQUEST, $method );
65
66                         # Set the referer to $wgTitle, even in command-line mode
67                         # This is useful for interwiki transclusion, where the foreign
68                         # server wants to know what the referring page is.
69                         # $_SERVER['REQUEST_URI'] gives a less reliable indication of the
70                         # referring page.
71                         if ( is_object( $wgTitle ) ) {
72                                 curl_setopt( $c, CURLOPT_REFERER, $wgTitle->getFullURL() );
73                         }
74                         
75                         if ( is_array( $curlOptions ) ) {
76                                 foreach( $curlOptions as $option => $value ) {
77                                         curl_setopt( $c, $option, $value );
78                                 }
79                         }
80
81                         ob_start();
82                         curl_exec( $c );
83                         $text = ob_get_contents();
84                         ob_end_clean();
85
86                         # Don't return the text of error messages, return false on error
87                         $retcode = curl_getinfo( $c, CURLINFO_HTTP_CODE );
88                         if ( $retcode != 200 ) {
89                                 wfDebug( __METHOD__ . ": HTTP return code $retcode\n" );
90                                 $text = false;
91                         }
92                         # Don't return truncated output
93                         $errno = curl_errno( $c );
94                         if ( $errno != CURLE_OK ) {
95                                 $errstr = curl_error( $c );
96                                 wfDebug( __METHOD__ . ": CURL error code $errno: $errstr\n" );
97                                 $text = false;
98                         }
99                         curl_close( $c );
100                 } else {
101                         # Otherwise use file_get_contents...
102                         # This doesn't have local fetch capabilities...
103
104                         $headers = array( "User-Agent: " . self :: userAgent() );
105                         if( strcasecmp( $method, 'post' ) == 0 ) {
106                                 // Required for HTTP 1.0 POSTs
107                                 $headers[] = "Content-Length: 0";
108                         }
109                         $opts = array(
110                                 'http' => array(
111                                         'method' => $method,
112                                         'header' => implode( "\r\n", $headers ),
113                                         'timeout' => $timeout ) );
114                         $ctx = stream_context_create($opts);
115
116                         $text = file_get_contents( $url, false, $ctx );
117                 }
118                 return $text;
119         }
120
121         /**
122          * Check if the URL can be served by localhost
123          * @param $url string Full url to check
124          * @return bool
125          */
126         public static function isLocalURL( $url ) {
127                 global $wgCommandLineMode, $wgConf;
128                 if ( $wgCommandLineMode ) {
129                         return false;
130                 }
131
132                 // Extract host part
133                 $matches = array();
134                 if ( preg_match( '!^http://([\w.-]+)[/:].*$!', $url, $matches ) ) {
135                         $host = $matches[1];
136                         // Split up dotwise
137                         $domainParts = explode( '.', $host );
138                         // Check if this domain or any superdomain is listed in $wgConf as a local virtual host
139                         $domainParts = array_reverse( $domainParts );
140                         for ( $i = 0; $i < count( $domainParts ); $i++ ) {
141                                 $domainPart = $domainParts[$i];
142                                 if ( $i == 0 ) {
143                                         $domain = $domainPart;
144                                 } else {
145                                         $domain = $domainPart . '.' . $domain;
146                                 }
147                                 if ( $wgConf->isLocalVHost( $domain ) ) {
148                                         return true;
149                                 }
150                         }
151                 }
152                 return false;
153         }
154         
155         /**
156          * Return a standard user-agent we can use for external requests.
157          */
158         public static function userAgent() {
159                 global $wgVersion;
160                 return "MediaWiki/$wgVersion";
161         }
162 }