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