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