2 if ( ! defined( 'MEDIAWIKI' ) )
5 * Functions for dealing with proxies
9 function wfGetForwardedFor() {
10 if( function_exists( 'apache_request_headers' ) ) {
11 // More reliable than $_SERVER due to case and -/_ folding
12 $set = apache_request_headers();
13 $index = 'X-Forwarded-For';
15 // Subject to spoofing with headers like X_Forwarded_For
17 $index = 'HTTP_X_FORWARDED_FOR';
19 if( isset( $set[$index] ) ) {
26 /** Work out the IP address based on various globals */
28 global $wgSquidServers, $wgSquidServersNoPurge, $wgIP;
30 # Return cached result
31 if ( !empty( $wgIP ) ) {
35 /* collect the originating ips */
36 # Client connecting to this webserver
37 if ( isset( $_SERVER['REMOTE_ADDR'] ) ) {
38 $ipchain = array( $_SERVER['REMOTE_ADDR'] );
41 $ipchain = array( '127.0.0.1' );
45 # Get list of trusted proxies
46 # Flipped for quicker access
47 $trustedProxies = array_flip( array_merge( $wgSquidServers, $wgSquidServersNoPurge ) );
48 if ( count( $trustedProxies ) ) {
49 # Append XFF on to $ipchain
50 $forwardedFor = wfGetForwardedFor();
51 if ( isset( $forwardedFor ) ) {
52 $xff = array_map( 'trim', explode( ',', $forwardedFor ) );
53 $xff = array_reverse( $xff );
54 $ipchain = array_merge( $ipchain, $xff );
56 # Step through XFF list and find the last address in the list which is a trusted server
57 # Set $ip to the IP address given by that trusted server, unless the address is not sensible (e.g. private)
58 foreach ( $ipchain as $i => $curIP ) {
59 if ( array_key_exists( $curIP, $trustedProxies ) ) {
60 if ( isset( $ipchain[$i +
1] ) && wfIsIPPublic( $ipchain[$i +
1] ) ) {
61 $ip = $ipchain[$i +
1];
69 wfDebug( "IP: $ip\n" );
75 * Given an IP address in dotted-quad notation, returns an unsigned integer.
76 * Like ip2long() except that it actually works and has a consistent error return value.
78 function wfIP2Unsigned( $ip ) {
80 if ( $n == -1 ||
$n === false ) { # Return value on error depends on PHP version
89 * Return a zero-padded hexadecimal representation of an IP address
91 function wfIP2Hex( $ip ) {
92 $n = wfIP2Unsigned( $ip );
94 $n = sprintf( '%08X', $n );
100 * Determine if an IP address really is an IP address, and if it is public,
101 * i.e. not RFC 1918 or similar
103 function wfIsIPPublic( $ip ) {
104 $n = wfIP2Unsigned( $ip );
109 static $privateRanges = false;
110 if ( !$privateRanges ) {
111 $privateRanges = array(
112 array( '10.0.0.0', '10.255.255.255' ), # RFC 1918 (private)
113 array( '172.16.0.0', '172.31.255.255' ), # "
114 array( '192.168.0.0', '192.168.255.255' ), # "
115 array( '0.0.0.0', '0.255.255.255' ), # this network
116 array( '127.0.0.0', '127.255.255.255' ), # loopback
120 foreach ( $privateRanges as $r ) {
121 $start = wfIP2Unsigned( $r[0] );
122 $end = wfIP2Unsigned( $r[1] );
123 if ( $n >= $start && $n <= $end ) {
131 * Forks processes to scan the originating IP for an open proxy server
132 * MemCached can be used to skip IPs that have already been scanned
134 function wfProxyCheck() {
135 global $wgBlockOpenProxies, $wgProxyPorts, $wgProxyScriptPath;
136 global $wgUseMemCached, $wgMemc, $wgDBname, $wgProxyMemcExpiry;
139 if ( !$wgBlockOpenProxies ) {
147 if ( $wgUseMemCached ) {
148 $mcKey = "$wgDBname:proxy:ip:$ip";
149 $mcValue = $wgMemc->get( $mcKey );
157 $title = Title
::makeTitle( NS_SPECIAL
, '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 &" );
171 if ( $wgUseMemCached ) {
172 $wgMemc->set( $mcKey, 1, $wgProxyMemcExpiry );
178 * Convert a network specification in CIDR notation to an integer network and a number of bits
180 function wfParseCIDR( $range ) {
181 $parts = explode( '/', $range, 2 );
182 if ( count( $parts ) != 2 ) {
183 return array( false, false );
185 $network = wfIP2Unsigned( $parts[0] );
186 if ( $network !== false && is_numeric( $parts[1] ) && $parts[1] >= 0 && $parts[1] <= 32 ) {
192 return array( $network, $bits );
196 * Check if an IP address is in the local proxy list
198 function wfIsLocallyBlockedProxy( $ip ) {
200 $fname = 'wfIsLocallyBlockedProxy';
202 if ( !$wgProxyList ) {
205 wfProfileIn( $fname );
207 if ( !is_array( $wgProxyList ) ) {
208 # Load from the specified file
209 $wgProxyList = array_map( 'trim', file( $wgProxyList ) );
212 if ( !is_array( $wgProxyList ) ) {
214 } elseif ( array_search( $ip, $wgProxyList ) !== false ) {
216 } elseif ( array_key_exists( $ip, $wgProxyList ) ) {
217 # Old-style flipped proxy list
222 wfProfileOut( $fname );