Fix some doxygen stuff.
[lhc/web/wiklou.git] / includes / HttpFunctions.php
1 <?php
2
3 /**
4 * @defgroup HTTP HTTP
5 *
6 * Various HTTP related functions
7 * @ingroup HTTP
8 */
9 class Http {
10 static function get( $url, $timeout = 'default', $opts = array() ) {
11 return Http::request( "GET", $url, $timeout, $opts );
12 }
13
14 static function post( $url, $timeout = 'default', $opts = array() ) {
15 return Http::request( "POST", $url, $timeout, $opts );
16 }
17
18 /**
19 * Get the contents of a file by HTTP
20 *
21 * if $timeout is 'default', $wgHTTPTimeout is used
22 */
23 static function request( $method, $url, $timeout = 'default', $curlOptions = array() ) {
24 global $wgHTTPTimeout, $wgHTTPProxy, $wgVersion, $wgTitle;
25
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);
34 }
35
36 if ( $timeout == 'default' ) {
37 $timeout = $wgHTTPTimeout;
38 }
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 );
43 else
44 curl_setopt( $c, CURLOPT_CUSTOMREQUEST, $method );
45
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
50 # referring page.
51 if ( is_object( $wgTitle ) ) {
52 curl_setopt( $c, CURLOPT_REFERER, $wgTitle->getFullURL() );
53 }
54
55 if ( is_array( $curlOptions ) ) {
56 foreach( $curlOptions as $option => $value ) {
57 curl_setopt( $c, $option, $value );
58 }
59 }
60
61 ob_start();
62 curl_exec( $c );
63 $text = ob_get_contents();
64 ob_end_clean();
65
66 # Don't return the text of error messages, return false on error
67 if ( curl_getinfo( $c, CURLINFO_HTTP_CODE ) != 200 ) {
68 $text = false;
69 }
70 # Don't return truncated output
71 if ( curl_errno( $c ) != CURLE_OK ) {
72 $text = false;
73 }
74 curl_close( $c );
75 } else {
76 # Otherwise use file_get_contents...
77 # This may take 3 minutes to time out, and doesn't have local fetch capabilities
78
79 global $wgVersion;
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";
84 }
85 $opts = array(
86 'http' => array(
87 'method' => $method,
88 'header' => implode( "\r\n", $headers ) ) );
89 $ctx = stream_context_create($opts);
90
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 );
94 }
95 return $text;
96 }
97
98 /**
99 * Check if the URL can be served by localhost
100 */
101 static function isLocalURL( $url ) {
102 global $wgCommandLineMode, $wgConf;
103 if ( $wgCommandLineMode ) {
104 return false;
105 }
106
107 // Extract host part
108 $matches = array();
109 if ( preg_match( '!^http://([\w.-]+)[/:].*$!', $url, $matches ) ) {
110 $host = $matches[1];
111 // Split up dotwise
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];
117 if ( $i == 0 ) {
118 $domain = $domainPart;
119 } else {
120 $domain = $domainPart . '.' . $domain;
121 }
122 if ( $wgConf->isLocalVHost( $domain ) ) {
123 return true;
124 }
125 }
126 }
127 return false;
128 }
129 }