Follow-up r104051: fix tests
[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 are whitelisted.
49 *
50 * @param $ip String
51 * @return bool
52 */
53 function wfIsTrustedProxy( $ip ) {
54 global $wgSquidServers, $wgSquidServersNoPurge;
55
56 $trusted = in_array( $ip, $wgSquidServers ) ||
57 in_array( $ip, $wgSquidServersNoPurge );
58 wfRunHooks( 'IsTrustedProxy', array( &$ip, &$trusted ) );
59 return $trusted;
60 }
61
62 /**
63 * Forks processes to scan the originating IP for an open proxy server
64 * MemCached can be used to skip IPs that have already been scanned
65 */
66 function wfProxyCheck() {
67 global $wgBlockOpenProxies, $wgProxyPorts, $wgProxyScriptPath;
68 global $wgMemc, $wgProxyMemcExpiry, $wgRequest;
69 global $wgProxyKey;
70
71 if ( !$wgBlockOpenProxies ) {
72 return;
73 }
74
75 $ip = $wgRequest->getIP();
76
77 # Get MemCached key
78 $mcKey = wfMemcKey( 'proxy', 'ip', $ip );
79 $mcValue = $wgMemc->get( $mcKey );
80 $skip = (bool)$mcValue;
81
82 # Fork the processes
83 if ( !$skip ) {
84 $title = SpecialPage::getTitleFor( 'Blockme' );
85 $iphash = md5( $ip . $wgProxyKey );
86 $url = wfExpandUrl( $title->getFullURL( 'ip='.$iphash ), PROTO_HTTP );
87
88 foreach ( $wgProxyPorts as $port ) {
89 $params = implode( ' ', array(
90 escapeshellarg( $wgProxyScriptPath ),
91 escapeshellarg( $ip ),
92 escapeshellarg( $port ),
93 escapeshellarg( $url )
94 ));
95 exec( "php $params >" . wfGetNull() . " 2>&1 &" );
96 }
97 # Set MemCached key
98 $wgMemc->set( $mcKey, 1, $wgProxyMemcExpiry );
99 }
100 }