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