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