deleted svn:executable property
[mediawiki.git] / includes / ProxyTools.php
blob0f0104218a2ed113d25430937ba53e2f0dca1bc4
1 <?php
2 /**
3 * Functions for dealing with proxies
4 * @file
5 */
7 /**
8 * Extracts the XFF string from the request header
9 * Checks first for "X-Forwarded-For", then "Client-ip"
10 * Note: headers are spoofable
11 * @return string
13 function wfGetForwardedFor() {
14 if( function_exists( 'apache_request_headers' ) ) {
15 // More reliable than $_SERVER due to case and -/_ folding
16 $set = array ();
17 foreach ( apache_request_headers() as $tempName => $tempValue ) {
18 $set[ strtoupper( $tempName ) ] = $tempValue;
20 $index = strtoupper ( 'X-Forwarded-For' );
21 $index2 = strtoupper ( 'Client-ip' );
22 } else {
23 // Subject to spoofing with headers like X_Forwarded_For
24 $set = $_SERVER;
25 $index = 'HTTP_X_FORWARDED_FOR';
26 $index2 = 'CLIENT-IP';
29 #Try a couple of headers
30 if( isset( $set[$index] ) ) {
31 return $set[$index];
32 } else if( isset( $set[$index2] ) ) {
33 return $set[$index2];
34 } else {
35 return null;
39 /**
40 * Returns the browser/OS data from the request header
41 * Note: headers are spoofable
42 * @return string
44 function wfGetAgent() {
45 if( function_exists( 'apache_request_headers' ) ) {
46 // More reliable than $_SERVER due to case and -/_ folding
47 $set = array ();
48 foreach ( apache_request_headers() as $tempName => $tempValue ) {
49 $set[ strtoupper( $tempName ) ] = $tempValue;
51 $index = strtoupper ( 'User-Agent' );
52 } else {
53 // Subject to spoofing with headers like X_Forwarded_For
54 $set = $_SERVER;
55 $index = 'HTTP_USER_AGENT';
57 if( isset( $set[$index] ) ) {
58 return $set[$index];
59 } else {
60 return '';
64 /**
65 * Work out the IP address based on various globals
66 * For trusted proxies, use the XFF client IP (first of the chain)
67 * @return string
69 function wfGetIP() {
70 global $wgIP;
72 # Return cached result
73 if ( !empty( $wgIP ) ) {
74 return $wgIP;
77 /* collect the originating ips */
78 # Client connecting to this webserver
79 if ( isset( $_SERVER['REMOTE_ADDR'] ) ) {
80 $ipchain = array( IP::canonicalize( $_SERVER['REMOTE_ADDR'] ) );
81 } else {
82 # Running on CLI?
83 $ipchain = array( '127.0.0.1' );
85 $ip = $ipchain[0];
87 # Append XFF on to $ipchain
88 $forwardedFor = wfGetForwardedFor();
89 if ( isset( $forwardedFor ) ) {
90 $xff = array_map( 'trim', explode( ',', $forwardedFor ) );
91 $xff = array_reverse( $xff );
92 $ipchain = array_merge( $ipchain, $xff );
95 # Step through XFF list and find the last address in the list which is a trusted server
96 # Set $ip to the IP address given by that trusted server, unless the address is not sensible (e.g. private)
97 foreach ( $ipchain as $i => $curIP ) {
98 $curIP = IP::canonicalize( $curIP );
99 if ( wfIsTrustedProxy( $curIP ) ) {
100 if ( isset( $ipchain[$i + 1] ) && IP::isPublic( $ipchain[$i + 1] ) ) {
101 $ip = $ipchain[$i + 1];
103 } else {
104 break;
108 wfDebug( "IP: $ip\n" );
109 $wgIP = $ip;
110 return $ip;
114 * Checks if an IP is a trusted proxy providor
115 * Useful to tell if X-Fowarded-For data is possibly bogus
116 * Squid cache servers for the site and AOL are whitelisted
117 * @param string $ip
118 * @return bool
120 function wfIsTrustedProxy( $ip ) {
121 global $wgSquidServers, $wgSquidServersNoPurge;
123 if ( in_array( $ip, $wgSquidServers ) ||
124 in_array( $ip, $wgSquidServersNoPurge ) ||
125 wfIsAOLProxy( $ip )
127 $trusted = true;
128 } else {
129 $trusted = false;
131 wfRunHooks( 'IsTrustedProxy', array( &$ip, &$trusted ) );
132 return $trusted;
136 * Forks processes to scan the originating IP for an open proxy server
137 * MemCached can be used to skip IPs that have already been scanned
139 function wfProxyCheck() {
140 global $wgBlockOpenProxies, $wgProxyPorts, $wgProxyScriptPath;
141 global $wgMemc, $wgProxyMemcExpiry;
142 global $wgProxyKey;
144 if ( !$wgBlockOpenProxies ) {
145 return;
148 $ip = wfGetIP();
150 # Get MemCached key
151 $mcKey = wfMemcKey( 'proxy', 'ip', $ip );
152 $mcValue = $wgMemc->get( $mcKey );
153 $skip = (bool)$mcValue;
155 # Fork the processes
156 if ( !$skip ) {
157 $title = SpecialPage::getTitleFor( 'Blockme' );
158 $iphash = md5( $ip . $wgProxyKey );
159 $url = $title->getFullURL( 'ip='.$iphash );
161 foreach ( $wgProxyPorts as $port ) {
162 $params = implode( ' ', array(
163 escapeshellarg( $wgProxyScriptPath ),
164 escapeshellarg( $ip ),
165 escapeshellarg( $port ),
166 escapeshellarg( $url )
168 exec( "php $params &>/dev/null &" );
170 # Set MemCached key
171 $wgMemc->set( $mcKey, 1, $wgProxyMemcExpiry );
176 * Convert a network specification in CIDR notation to an integer network and a number of bits
177 * @return array(string, int)
179 function wfParseCIDR( $range ) {
180 return IP::parseCIDR( $range );
184 * Check if an IP address is in the local proxy list
185 * @return bool
187 function wfIsLocallyBlockedProxy( $ip ) {
188 global $wgProxyList;
189 $fname = 'wfIsLocallyBlockedProxy';
191 if ( !$wgProxyList ) {
192 return false;
194 wfProfileIn( $fname );
196 if ( !is_array( $wgProxyList ) ) {
197 # Load from the specified file
198 $wgProxyList = array_map( 'trim', file( $wgProxyList ) );
201 if ( !is_array( $wgProxyList ) ) {
202 $ret = false;
203 } elseif ( array_search( $ip, $wgProxyList ) !== false ) {
204 $ret = true;
205 } elseif ( array_key_exists( $ip, $wgProxyList ) ) {
206 # Old-style flipped proxy list
207 $ret = true;
208 } else {
209 $ret = false;
211 wfProfileOut( $fname );
212 return $ret;
216 * TODO: move this list to the database in a global IP info table incorporating
217 * trusted ISP proxies, blocked IP addresses and open proxies.
218 * @return bool
220 function wfIsAOLProxy( $ip ) {
221 # From http://webmaster.info.aol.com/proxyinfo.html
222 $ranges = array(
223 '64.12.96.0/19',
224 '149.174.160.0/20',
225 '152.163.240.0/21',
226 '152.163.248.0/22',
227 '152.163.252.0/23',
228 '152.163.96.0/22',
229 '152.163.100.0/23',
230 '195.93.32.0/22',
231 '195.93.48.0/22',
232 '195.93.64.0/19',
233 '195.93.96.0/19',
234 '195.93.16.0/20',
235 '198.81.0.0/22',
236 '198.81.16.0/20',
237 '198.81.8.0/23',
238 '202.67.64.128/25',
239 '205.188.192.0/20',
240 '205.188.208.0/23',
241 '205.188.112.0/20',
242 '205.188.146.144/30',
243 '207.200.112.0/21',
246 static $parsedRanges;
247 if ( is_null( $parsedRanges ) ) {
248 $parsedRanges = array();
249 foreach ( $ranges as $range ) {
250 $parsedRanges[] = IP::parseRange( $range );
254 $hex = IP::toHex( $ip );
255 foreach ( $parsedRanges as $range ) {
256 if ( $hex >= $range[0] && $hex <= $range[1] ) {
257 return true;
260 return false;