PHP Fatal error: Call to undefined function request() in /usr/local/apache/common...
[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, or its compatibility function from GlobalFunctions.php
63 # This may take 3 minutes to time out, and doesn't have local fetch capabilities
64
65 $opts = array('http' => array( 'method' => $method ) );
66 $ctx = stream_context_create($opts);
67
68 $url_fopen = ini_set( 'allow_url_fopen', 1 );
69 $text = file_get_contents( $url, false, $ctx );
70 ini_set( 'allow_url_fopen', $url_fopen );
71 }
72 return $text;
73 }
74
75 /**
76 * Check if the URL can be served by localhost
77 */
78 static function isLocalURL( $url ) {
79 global $wgCommandLineMode, $wgConf;
80 if ( $wgCommandLineMode ) {
81 return false;
82 }
83
84 // Extract host part
85 $matches = array();
86 if ( preg_match( '!^http://([\w.-]+)[/:].*$!', $url, $matches ) ) {
87 $host = $matches[1];
88 // Split up dotwise
89 $domainParts = explode( '.', $host );
90 // Check if this domain or any superdomain is listed in $wgConf as a local virtual host
91 $domainParts = array_reverse( $domainParts );
92 for ( $i = 0; $i < count( $domainParts ); $i++ ) {
93 $domainPart = $domainParts[$i];
94 if ( $i == 0 ) {
95 $domain = $domainPart;
96 } else {
97 $domain = $domainPart . '.' . $domain;
98 }
99 if ( $wgConf->isLocalVHost( $domain ) ) {
100 return true;
101 }
102 }
103 }
104 return false;
105 }
106 }
107 ?>