* Added temporary special-case AOL proxy detection, they're automatically counted...
[lhc/web/wiklou.git] / includes / ProxyTools.php
1 <?php
2 /**
3 * Functions for dealing with proxies
4 * @package MediaWiki
5 */
6
7 function wfGetForwardedFor() {
8 if( function_exists( 'apache_request_headers' ) ) {
9 // More reliable than $_SERVER due to case and -/_ folding
10 $set = apache_request_headers();
11 $index = 'X-Forwarded-For';
12 } else {
13 // Subject to spoofing with headers like X_Forwarded_For
14 $set = $_SERVER;
15 $index = 'HTTP_X_FORWARDED_FOR';
16 }
17 if( isset( $set[$index] ) ) {
18 return $set[$index];
19 } else {
20 return null;
21 }
22 }
23
24 /** Work out the IP address based on various globals */
25 function wfGetIP() {
26 global $wgIP;
27
28 # Return cached result
29 if ( !empty( $wgIP ) ) {
30 return $wgIP;
31 }
32
33 /* collect the originating ips */
34 # Client connecting to this webserver
35 if ( isset( $_SERVER['REMOTE_ADDR'] ) ) {
36 $ipchain = array( $_SERVER['REMOTE_ADDR'] );
37 } else {
38 # Running on CLI?
39 $ipchain = array( '127.0.0.1' );
40 }
41 $ip = $ipchain[0];
42
43 # Append XFF on to $ipchain
44 $forwardedFor = wfGetForwardedFor();
45 if ( isset( $forwardedFor ) ) {
46 $xff = array_map( 'trim', explode( ',', $forwardedFor ) );
47 $xff = array_reverse( $xff );
48 $ipchain = array_merge( $ipchain, $xff );
49 }
50 # Step through XFF list and find the last address in the list which is a trusted server
51 # Set $ip to the IP address given by that trusted server, unless the address is not sensible (e.g. private)
52 foreach ( $ipchain as $i => $curIP ) {
53 if ( wfIsTrustedProxy( $curIP ) ) {
54 if ( isset( $ipchain[$i + 1] ) && IP::isPublic( $ipchain[$i + 1] ) ) {
55 $ip = $ipchain[$i + 1];
56 }
57 } else {
58 break;
59 }
60 }
61
62 wfDebug( "IP: $ip\n" );
63 $wgIP = $ip;
64 return $ip;
65 }
66
67 function wfIsTrustedProxy( $ip ) {
68 global $wgSquidServers, $wgSquidServersNoPurge;
69
70 if ( in_array( $ip, $wgSquidServers ) ||
71 in_array( $ip, $wgSquidServersNoPurge ) ||
72 wfIsAOLProxy( $ip )
73 ) {
74 $trusted = true;
75 } else {
76 $trusted = false;
77 }
78 wfRunHooks( 'IsTrustedProxy', array( &$ip, &$trusted ) );
79 return $trusted;
80 }
81
82 /**
83 * Forks processes to scan the originating IP for an open proxy server
84 * MemCached can be used to skip IPs that have already been scanned
85 */
86 function wfProxyCheck() {
87 global $wgBlockOpenProxies, $wgProxyPorts, $wgProxyScriptPath;
88 global $wgUseMemCached, $wgMemc, $wgProxyMemcExpiry;
89 global $wgProxyKey;
90
91 if ( !$wgBlockOpenProxies ) {
92 return;
93 }
94
95 $ip = wfGetIP();
96
97 # Get MemCached key
98 $skip = false;
99 if ( $wgUseMemCached ) {
100 $mcKey = wfMemcKey( 'proxy', 'ip', $ip );
101 $mcValue = $wgMemc->get( $mcKey );
102 if ( $mcValue ) {
103 $skip = true;
104 }
105 }
106
107 # Fork the processes
108 if ( !$skip ) {
109 $title = SpecialPage::getTitleFor( 'Blockme' );
110 $iphash = md5( $ip . $wgProxyKey );
111 $url = $title->getFullURL( 'ip='.$iphash );
112
113 foreach ( $wgProxyPorts as $port ) {
114 $params = implode( ' ', array(
115 escapeshellarg( $wgProxyScriptPath ),
116 escapeshellarg( $ip ),
117 escapeshellarg( $port ),
118 escapeshellarg( $url )
119 ));
120 exec( "php $params &>/dev/null &" );
121 }
122 # Set MemCached key
123 if ( $wgUseMemCached ) {
124 $wgMemc->set( $mcKey, 1, $wgProxyMemcExpiry );
125 }
126 }
127 }
128
129 /**
130 * Convert a network specification in CIDR notation to an integer network and a number of bits
131 */
132 function wfParseCIDR( $range ) {
133 return IP::parseCIDR( $range );
134 }
135
136 /**
137 * Check if an IP address is in the local proxy list
138 */
139 function wfIsLocallyBlockedProxy( $ip ) {
140 global $wgProxyList;
141 $fname = 'wfIsLocallyBlockedProxy';
142
143 if ( !$wgProxyList ) {
144 return false;
145 }
146 wfProfileIn( $fname );
147
148 if ( !is_array( $wgProxyList ) ) {
149 # Load from the specified file
150 $wgProxyList = array_map( 'trim', file( $wgProxyList ) );
151 }
152
153 if ( !is_array( $wgProxyList ) ) {
154 $ret = false;
155 } elseif ( array_search( $ip, $wgProxyList ) !== false ) {
156 $ret = true;
157 } elseif ( array_key_exists( $ip, $wgProxyList ) ) {
158 # Old-style flipped proxy list
159 $ret = true;
160 } else {
161 $ret = false;
162 }
163 wfProfileOut( $fname );
164 return $ret;
165 }
166
167 /**
168 * TODO: move this list to the database in a global IP info table incorporating
169 * trusted ISP proxies, blocked IP addresses and open proxies.
170 */
171 function wfIsAOLProxy( $ip ) {
172 $ranges = array(
173 '64.12.96.0/19',
174 '149.174.160.0/20',
175 '152.163.240.0/21',
176 '152.163.248.0/22',
177 '152.163.252.0/23',
178 '152.163.96.0/22',
179 '152.163.100.0/23',
180 '195.93.32.0/22',
181 '195.93.48.0/22',
182 '195.93.64.0/19',
183 '195.93.96.0/19',
184 '195.93.16.0/20',
185 '198.81.0.0/22',
186 '198.81.16.0/20',
187 '198.81.8.0/23',
188 '202.67.64.128/25',
189 '205.188.192.0/20',
190 '205.188.208.0/23',
191 '205.188.112.0/20',
192 '205.188.146.144/30',
193 '207.200.112.0/21',
194 );
195
196 static $parsedRanges;
197 if ( is_null( $parsedRanges ) ) {
198 $parsedRanges = array();
199 foreach ( $ranges as $range ) {
200 $parsedRanges[] = IP::parseRange( $range );
201 }
202 }
203
204 $hex = IP::toHex( $ip );
205 foreach ( $parsedRanges as $range ) {
206 if ( $hex >= $range[0] && $hex <= $range[1] ) {
207 return true;
208 }
209 }
210 return false;
211 }
212
213
214
215 ?>