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