e690ea932d2f9887d08495575b5da7bfa97336e2
6 * Various HTTP related functions
10 static function get( $url, $timeout = 'default', $opts = array() ) {
11 return Http
::request( "GET", $url, $timeout, $opts );
14 static function post( $url, $timeout = 'default', $opts = array() ) {
15 return Http
::request( "POST", $url, $timeout, $opts );
19 * Get the contents of a file by HTTP
21 * if $timeout is 'default', $wgHTTPTimeout is used
23 static function request( $method, $url, $timeout = 'default', $curlOptions = array() ) {
24 global $wgHTTPTimeout, $wgHTTPProxy, $wgVersion, $wgTitle;
26 wfDebug( __METHOD__
. ": $method $url\n" );
27 # Use curl if available
28 if ( function_exists( 'curl_init' ) ) {
29 $c = curl_init( $url );
30 if ( self
::isLocalURL( $url ) ) {
31 curl_setopt( $c, CURLOPT_PROXY
, 'localhost:80' );
32 } else if ($wgHTTPProxy) {
33 curl_setopt($c, CURLOPT_PROXY
, $wgHTTPProxy);
36 if ( $timeout == 'default' ) {
37 $timeout = $wgHTTPTimeout;
39 curl_setopt( $c, CURLOPT_TIMEOUT
, $timeout );
40 curl_setopt( $c, CURLOPT_USERAGENT
, "MediaWiki/$wgVersion" );
41 if ( $method == 'POST' )
42 curl_setopt( $c, CURLOPT_POST
, true );
44 curl_setopt( $c, CURLOPT_CUSTOMREQUEST
, $method );
46 # Set the referer to $wgTitle, even in command-line mode
47 # This is useful for interwiki transclusion, where the foreign
48 # server wants to know what the referring page is.
49 # $_SERVER['REQUEST_URI'] gives a less reliable indication of the
51 if ( is_object( $wgTitle ) ) {
52 curl_setopt( $c, CURLOPT_REFERER
, $wgTitle->getFullURL() );
55 if ( is_array( $curlOptions ) ) {
56 foreach( $curlOptions as $option => $value ) {
57 curl_setopt( $c, $option, $value );
63 $text = ob_get_contents();
66 # Don't return the text of error messages, return false on error
67 if ( curl_getinfo( $c, CURLINFO_HTTP_CODE
) != 200 ) {
70 # Don't return truncated output
71 if ( curl_errno( $c ) != CURLE_OK
) {
76 # Otherwise use file_get_contents...
77 # This may take 3 minutes to time out, and doesn't have local fetch capabilities
80 $headers = array( "User-Agent: MediaWiki/$wgVersion" );
81 if( strcasecmp( $method, 'post' ) == 0 ) {
82 // Required for HTTP 1.0 POSTs
83 $headers[] = "Content-Length: 0";
88 'header' => implode( "\r\n", $headers ) ) );
89 $ctx = stream_context_create($opts);
91 $url_fopen = ini_set( 'allow_url_fopen', 1 );
92 $text = file_get_contents( $url, false, $ctx );
93 ini_set( 'allow_url_fopen', $url_fopen );
99 * Check if the URL can be served by localhost
101 static function isLocalURL( $url ) {
102 global $wgCommandLineMode, $wgConf;
103 if ( $wgCommandLineMode ) {
109 if ( preg_match( '!^http://([\w.-]+)[/:].*$!', $url, $matches ) ) {
112 $domainParts = explode( '.', $host );
113 // Check if this domain or any superdomain is listed in $wgConf as a local virtual host
114 $domainParts = array_reverse( $domainParts );
115 for ( $i = 0; $i < count( $domainParts ); $i++
) {
116 $domainPart = $domainParts[$i];
118 $domain = $domainPart;
120 $domain = $domainPart . '.' . $domain;
122 if ( $wgConf->isLocalVHost( $domain ) ) {