Moved wfGetIP() to WebRequest::getIP():
[lhc/web/wiklou.git] / includes / ProxyTools.php
1 <?php
2 /**
3 * Functions for dealing with proxies
4 *
5 * @file
6 */
7
8 /**
9 * Extracts the XFF string from the request header
10 * Note: headers are spoofable
11 *
12 * @deprecated in 1.19; use $wgRequest->getHeader( 'X-Forwarded-For' ) instead.
13 * @return string
14 */
15 function wfGetForwardedFor() {
16 global $wgRequest;
17 return $wgRequest->getHeader( 'X-Forwarded-For' );
18 }
19
20 /**
21 * Returns the browser/OS data from the request header
22 * Note: headers are spoofable
23 *
24 * @deprecated in 1.18; use $wgRequest->getHeader( 'User-Agent' ) instead.
25 * @return string
26 */
27 function wfGetAgent() {
28 wfDeprecated( __FUNCTION__ );
29 global $wgRequest;
30 return $wgRequest->getHeader( 'User-Agent' );
31 }
32
33 /**
34 * Work out the IP address based on various globals
35 * For trusted proxies, use the XFF client IP (first of the chain)
36 *
37 * @deprecated in 1.19; call $wgRequest->getIP() directly.
38 * @return string
39 */
40 function wfGetIP() {
41 global $wgRequest;
42 return $wgRequest->getIP();
43 }
44
45 /**
46 * Checks if an IP is a trusted proxy providor
47 * Useful to tell if X-Fowarded-For data is possibly bogus
48 * Squid cache servers for the site and AOL are whitelisted
49 * @param $ip String
50 * @return bool
51 */
52 function wfIsTrustedProxy( $ip ) {
53 global $wgSquidServers, $wgSquidServersNoPurge;
54
55 $trusted = in_array( $ip, $wgSquidServers ) ||
56 in_array( $ip, $wgSquidServersNoPurge );
57 wfRunHooks( 'IsTrustedProxy', array( &$ip, &$trusted ) );
58 return $trusted;
59 }
60
61 /**
62 * Forks processes to scan the originating IP for an open proxy server
63 * MemCached can be used to skip IPs that have already been scanned
64 */
65 function wfProxyCheck() {
66 global $wgBlockOpenProxies, $wgProxyPorts, $wgProxyScriptPath;
67 global $wgMemc, $wgProxyMemcExpiry, $wgRequest;
68 global $wgProxyKey;
69
70 if ( !$wgBlockOpenProxies ) {
71 return;
72 }
73
74 $ip = $wgRequest->getIP();
75
76 # Get MemCached key
77 $mcKey = wfMemcKey( 'proxy', 'ip', $ip );
78 $mcValue = $wgMemc->get( $mcKey );
79 $skip = (bool)$mcValue;
80
81 # Fork the processes
82 if ( !$skip ) {
83 $title = SpecialPage::getTitleFor( 'Blockme' );
84 $iphash = md5( $ip . $wgProxyKey );
85 $url = $title->getFullURL( 'ip='.$iphash );
86
87 foreach ( $wgProxyPorts as $port ) {
88 $params = implode( ' ', array(
89 escapeshellarg( $wgProxyScriptPath ),
90 escapeshellarg( $ip ),
91 escapeshellarg( $port ),
92 escapeshellarg( $url )
93 ));
94 exec( "php $params >" . wfGetNull() . " 2>&1 &" );
95 }
96 # Set MemCached key
97 $wgMemc->set( $mcKey, 1, $wgProxyMemcExpiry );
98 }
99 }