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