00001 <?php
00002
00013 class Http {
00014
00019 public static function get( $url, $timeout = 'default', $opts = array() ) {
00020 return Http::request( "GET", $url, $timeout, $opts );
00021 }
00022
00027 public static function post( $url, $timeout = 'default', $opts = array() ) {
00028 return Http::request( "POST", $url, $timeout, $opts );
00029 }
00030
00039 public static function request( $method, $url, $timeout = 'default', $curlOptions = array() ) {
00040 global $wgHTTPTimeout, $wgHTTPProxy, $wgTitle;
00041
00042
00043 if ( $timeout == 'default' ) {
00044 $timeout = $wgHTTPTimeout;
00045 }
00046
00047 wfDebug( __METHOD__ . ": $method $url\n" );
00048 # Use curl if available
00049 if ( function_exists( 'curl_init' ) ) {
00050 $c = curl_init( $url );
00051 if ( self::isLocalURL( $url ) ) {
00052 curl_setopt( $c, CURLOPT_PROXY, 'localhost:80' );
00053 } else if ($wgHTTPProxy) {
00054 curl_setopt($c, CURLOPT_PROXY, $wgHTTPProxy);
00055 }
00056
00057 curl_setopt( $c, CURLOPT_TIMEOUT, $timeout );
00058 curl_setopt( $c, CURLOPT_USERAGENT, self :: userAgent() );
00059 if ( $method == 'POST' ) {
00060 curl_setopt( $c, CURLOPT_POST, true );
00061 curl_setopt( $c, CURLOPT_POSTFIELDS, '' );
00062 }
00063 else
00064 curl_setopt( $c, CURLOPT_CUSTOMREQUEST, $method );
00065
00066 # Set the referer to $wgTitle, even in command-line mode
00067 # This is useful for interwiki transclusion, where the foreign
00068 # server wants to know what the referring page is.
00069 # $_SERVER['REQUEST_URI'] gives a less reliable indication of the
00070 # referring page.
00071 if ( is_object( $wgTitle ) ) {
00072 curl_setopt( $c, CURLOPT_REFERER, $wgTitle->getFullURL() );
00073 }
00074
00075 if ( is_array( $curlOptions ) ) {
00076 foreach( $curlOptions as $option => $value ) {
00077 curl_setopt( $c, $option, $value );
00078 }
00079 }
00080
00081 ob_start();
00082 curl_exec( $c );
00083 $text = ob_get_contents();
00084 ob_end_clean();
00085
00086 # Don't return the text of error messages, return false on error
00087 $retcode = curl_getinfo( $c, CURLINFO_HTTP_CODE );
00088 if ( $retcode != 200 ) {
00089 wfDebug( __METHOD__ . ": HTTP return code $retcode\n" );
00090 $text = false;
00091 }
00092 # Don't return truncated output
00093 $errno = curl_errno( $c );
00094 if ( $errno != CURLE_OK ) {
00095 $errstr = curl_error( $c );
00096 wfDebug( __METHOD__ . ": CURL error code $errno: $errstr\n" );
00097 $text = false;
00098 }
00099 curl_close( $c );
00100 } else {
00101 # Otherwise use file_get_contents...
00102 # This doesn't have local fetch capabilities...
00103
00104 $headers = array( "User-Agent: " . self :: userAgent() );
00105 if( strcasecmp( $method, 'post' ) == 0 ) {
00106
00107 $headers[] = "Content-Length: 0";
00108 }
00109 $opts = array(
00110 'http' => array(
00111 'method' => $method,
00112 'header' => implode( "\r\n", $headers ),
00113 'timeout' => $timeout ) );
00114 $ctx = stream_context_create($opts);
00115
00116 $text = file_get_contents( $url, false, $ctx );
00117 }
00118 return $text;
00119 }
00120
00126 public static function isLocalURL( $url ) {
00127 global $wgCommandLineMode, $wgConf;
00128 if ( $wgCommandLineMode ) {
00129 return false;
00130 }
00131
00132
00133 $matches = array();
00134 if ( preg_match( '!^http://([\w.-]+)[/:].*$!', $url, $matches ) ) {
00135 $host = $matches[1];
00136
00137 $domainParts = explode( '.', $host );
00138
00139 $domainParts = array_reverse( $domainParts );
00140 for ( $i = 0; $i < count( $domainParts ); $i++ ) {
00141 $domainPart = $domainParts[$i];
00142 if ( $i == 0 ) {
00143 $domain = $domainPart;
00144 } else {
00145 $domain = $domainPart . '.' . $domain;
00146 }
00147 if ( $wgConf->isLocalVHost( $domain ) ) {
00148 return true;
00149 }
00150 }
00151 }
00152 return false;
00153 }
00154
00158 public static function userAgent() {
00159 global $wgVersion;
00160 return "MediaWiki/$wgVersion";
00161 }
00162 }