Add a way for packagers to override some installation details
[lhc/web/wiklou.git] / includes / ProxyTools.php
1 <?php
2 /**
3 * Functions for dealing with proxies.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22
23 /**
24 * Extracts the XFF string from the request header
25 * Note: headers are spoofable
26 *
27 * @deprecated in 1.19; use $wgRequest->getHeader( 'X-Forwarded-For' ) instead.
28 * @return string
29 */
30 function wfGetForwardedFor() {
31 wfDeprecated( __METHOD__, '1.19' );
32 global $wgRequest;
33 return $wgRequest->getHeader( 'X-Forwarded-For' );
34 }
35
36 /**
37 * Returns the browser/OS data from the request header
38 * Note: headers are spoofable
39 *
40 * @deprecated in 1.18; use $wgRequest->getHeader( 'User-Agent' ) instead.
41 * @return string
42 */
43 function wfGetAgent() {
44 wfDeprecated( __METHOD__, '1.18' );
45 global $wgRequest;
46 return $wgRequest->getHeader( 'User-Agent' );
47 }
48
49 /**
50 * Work out the IP address based on various globals
51 * For trusted proxies, use the XFF client IP (first of the chain)
52 *
53 * @deprecated in 1.19; call $wgRequest->getIP() directly.
54 * @return string
55 */
56 function wfGetIP() {
57 wfDeprecated( __METHOD__, '1.19' );
58 global $wgRequest;
59 return $wgRequest->getIP();
60 }
61
62 /**
63 * Checks if an IP is a trusted proxy providor.
64 * Useful to tell if X-Fowarded-For data is possibly bogus.
65 * Squid cache servers for the site are whitelisted.
66 *
67 * @param $ip String
68 * @return bool
69 */
70 function wfIsTrustedProxy( $ip ) {
71 $trusted = wfIsConfiguredProxy( $ip );
72 wfRunHooks( 'IsTrustedProxy', array( &$ip, &$trusted ) );
73 return $trusted;
74 }
75
76 /**
77 * Checks if an IP matches a proxy we've configured.
78 * @param $ip String
79 * @return bool
80 */
81 function wfIsConfiguredProxy( $ip ) {
82 global $wgSquidServers, $wgSquidServersNoPurge;
83 $trusted = in_array( $ip, $wgSquidServers ) ||
84 in_array( $ip, $wgSquidServersNoPurge );
85 return $trusted;
86 }
87
88 /**
89 * Forks processes to scan the originating IP for an open proxy server
90 * MemCached can be used to skip IPs that have already been scanned
91 */
92 function wfProxyCheck() {
93 global $wgBlockOpenProxies, $wgProxyPorts, $wgProxyScriptPath;
94 global $wgMemc, $wgProxyMemcExpiry, $wgRequest;
95 global $wgProxyKey;
96
97 if ( !$wgBlockOpenProxies ) {
98 return;
99 }
100
101 $ip = $wgRequest->getIP();
102
103 # Get MemCached key
104 $mcKey = wfMemcKey( 'proxy', 'ip', $ip );
105 $mcValue = $wgMemc->get( $mcKey );
106 $skip = (bool)$mcValue;
107
108 # Fork the processes
109 if ( !$skip ) {
110 $title = SpecialPage::getTitleFor( 'Blockme' );
111 $iphash = md5( $ip . $wgProxyKey );
112 $url = wfExpandUrl( $title->getFullURL( 'ip='.$iphash ), PROTO_HTTP );
113
114 foreach ( $wgProxyPorts as $port ) {
115 $params = implode( ' ', array(
116 escapeshellarg( $wgProxyScriptPath ),
117 escapeshellarg( $ip ),
118 escapeshellarg( $port ),
119 escapeshellarg( $url )
120 ));
121 exec( "php $params >" . wfGetNull() . " 2>&1 &" );
122 }
123 # Set MemCached key
124 $wgMemc->set( $mcKey, 1, $wgProxyMemcExpiry );
125 }
126 }