replace TYPE= with ENGINE=, (supported since 4.0, TYPE deprecated since 4.1)
[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 $wgSquidServers, $wgSquidServersNoPurge, $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 # Get list of trusted proxies
44 # Flipped for quicker access
45 $trustedProxies = array_flip( array_merge( $wgSquidServers, $wgSquidServersNoPurge ) );
46 if ( count( $trustedProxies ) ) {
47 # Append XFF on to $ipchain
48 $forwardedFor = wfGetForwardedFor();
49 if ( isset( $forwardedFor ) ) {
50 $xff = array_map( 'trim', explode( ',', $forwardedFor ) );
51 $xff = array_reverse( $xff );
52 $ipchain = array_merge( $ipchain, $xff );
53 }
54 # Step through XFF list and find the last address in the list which is a trusted server
55 # Set $ip to the IP address given by that trusted server, unless the address is not sensible (e.g. private)
56 foreach ( $ipchain as $i => $curIP ) {
57 if ( array_key_exists( $curIP, $trustedProxies ) ) {
58 if ( isset( $ipchain[$i + 1] ) && wfIsIPPublic( $ipchain[$i + 1] ) ) {
59 $ip = $ipchain[$i + 1];
60 }
61 } else {
62 break;
63 }
64 }
65 }
66
67 wfDebug( "IP: $ip\n" );
68 $wgIP = $ip;
69 return $ip;
70 }
71
72 /**
73 * Given an IP address in dotted-quad notation, returns an unsigned integer.
74 * Like ip2long() except that it actually works and has a consistent error return value.
75 */
76 function wfIP2Unsigned( $ip ) {
77 $n = ip2long( $ip );
78 if ( $n == -1 || $n === false ) { # Return value on error depends on PHP version
79 $n = false;
80 } elseif ( $n < 0 ) {
81 $n += pow( 2, 32 );
82 }
83 return $n;
84 }
85
86 /**
87 * Return a zero-padded hexadecimal representation of an IP address
88 */
89 function wfIP2Hex( $ip ) {
90 $n = wfIP2Unsigned( $ip );
91 if ( $n !== false ) {
92 $n = sprintf( '%08X', $n );
93 }
94 return $n;
95 }
96
97 /**
98 * Determine if an IP address really is an IP address, and if it is public,
99 * i.e. not RFC 1918 or similar
100 */
101 function wfIsIPPublic( $ip ) {
102 $n = wfIP2Unsigned( $ip );
103 if ( !$n ) {
104 return false;
105 }
106
107 static $privateRanges = false;
108 if ( !$privateRanges ) {
109 $privateRanges = array(
110 array( '10.0.0.0', '10.255.255.255' ), # RFC 1918 (private)
111 array( '172.16.0.0', '172.31.255.255' ), # "
112 array( '192.168.0.0', '192.168.255.255' ), # "
113 array( '0.0.0.0', '0.255.255.255' ), # this network
114 array( '127.0.0.0', '127.255.255.255' ), # loopback
115 );
116 }
117
118 foreach ( $privateRanges as $r ) {
119 $start = wfIP2Unsigned( $r[0] );
120 $end = wfIP2Unsigned( $r[1] );
121 if ( $n >= $start && $n <= $end ) {
122 return false;
123 }
124 }
125 return true;
126 }
127
128 /**
129 * Forks processes to scan the originating IP for an open proxy server
130 * MemCached can be used to skip IPs that have already been scanned
131 */
132 function wfProxyCheck() {
133 global $wgBlockOpenProxies, $wgProxyPorts, $wgProxyScriptPath;
134 global $wgUseMemCached, $wgMemc, $wgDBname, $wgProxyMemcExpiry;
135 global $wgProxyKey;
136
137 if ( !$wgBlockOpenProxies ) {
138 return;
139 }
140
141 $ip = wfGetIP();
142
143 # Get MemCached key
144 $skip = false;
145 if ( $wgUseMemCached ) {
146 $mcKey = "$wgDBname:proxy:ip:$ip";
147 $mcValue = $wgMemc->get( $mcKey );
148 if ( $mcValue ) {
149 $skip = true;
150 }
151 }
152
153 # Fork the processes
154 if ( !$skip ) {
155 $title = Title::makeTitle( NS_SPECIAL, 'Blockme' );
156 $iphash = md5( $ip . $wgProxyKey );
157 $url = $title->getFullURL( 'ip='.$iphash );
158
159 foreach ( $wgProxyPorts as $port ) {
160 $params = implode( ' ', array(
161 escapeshellarg( $wgProxyScriptPath ),
162 escapeshellarg( $ip ),
163 escapeshellarg( $port ),
164 escapeshellarg( $url )
165 ));
166 exec( "php $params &>/dev/null &" );
167 }
168 # Set MemCached key
169 if ( $wgUseMemCached ) {
170 $wgMemc->set( $mcKey, 1, $wgProxyMemcExpiry );
171 }
172 }
173 }
174
175 /**
176 * Convert a network specification in CIDR notation to an integer network and a number of bits
177 */
178 function wfParseCIDR( $range ) {
179 $parts = explode( '/', $range, 2 );
180 if ( count( $parts ) != 2 ) {
181 return array( false, false );
182 }
183 $network = wfIP2Unsigned( $parts[0] );
184 if ( $network !== false && is_numeric( $parts[1] ) && $parts[1] >= 0 && $parts[1] <= 32 ) {
185 $bits = $parts[1];
186 } else {
187 $network = false;
188 $bits = false;
189 }
190 return array( $network, $bits );
191 }
192
193 /**
194 * Check if an IP address is in the local proxy list
195 */
196 function wfIsLocallyBlockedProxy( $ip ) {
197 global $wgProxyList;
198 $fname = 'wfIsLocallyBlockedProxy';
199
200 if ( !$wgProxyList ) {
201 return false;
202 }
203 wfProfileIn( $fname );
204
205 if ( !is_array( $wgProxyList ) ) {
206 # Load from the specified file
207 $wgProxyList = array_map( 'trim', file( $wgProxyList ) );
208 }
209
210 if ( !is_array( $wgProxyList ) ) {
211 $ret = false;
212 } elseif ( array_search( $ip, $wgProxyList ) !== false ) {
213 $ret = true;
214 } elseif ( array_key_exists( $ip, $wgProxyList ) ) {
215 # Old-style flipped proxy list
216 $ret = true;
217 } else {
218 $ret = false;
219 }
220 wfProfileOut( $fname );
221 return $ret;
222 }
223
224
225
226
227 ?>