2 if ( ! defined( 'MEDIAWIKI' ) )
5 * Functions for dealing with proxies
9 /** Work out the IP address based on various globals */
11 global $wgSquidServers, $wgSquidServersNoPurge, $wgIP;
13 # Return cached result
14 if ( !empty( $wgIP ) ) {
18 /* collect the originating ips */
19 # Client connecting to this webserver
20 if ( isset( $_SERVER['REMOTE_ADDR'] ) ) {
21 $ipchain = array( $_SERVER['REMOTE_ADDR'] );
24 $ipchain = array( '127.0.0.1' );
28 # Get list of trusted proxies
29 # Flipped for quicker access
30 $trustedProxies = array_flip( array_merge( $wgSquidServers, $wgSquidServersNoPurge ) );
31 if ( count( $trustedProxies ) ) {
32 # Append XFF on to $ipchain
33 if ( isset( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) {
34 $xff = array_map( 'trim', explode( ',', $_SERVER['HTTP_X_FORWARDED_FOR'] ) );
35 $xff = array_reverse( $xff );
36 $ipchain = array_merge( $ipchain, $xff );
38 # Step through XFF list and find the last address in the list which is a trusted server
39 # Set $ip to the IP address given by that trusted server, unless the address is not sensible (e.g. private)
40 foreach ( $ipchain as $i => $curIP ) {
41 if ( array_key_exists( $curIP, $trustedProxies ) ) {
42 if ( isset( $ipchain[$i +
1] ) && wfIsIPPublic( $ipchain[$i +
1] ) ) {
43 $ip = $ipchain[$i +
1];
51 wfDebug( "IP: $ip\n" );
57 * Given an IP address in dotted-quad notation, returns an unsigned integer.
58 * Like ip2long() except that it actually works and has a consistent error return value.
60 function wfIP2Unsigned( $ip ) {
62 if ( $n == -1 ||
$n === false ) { # Return value on error depends on PHP version
71 * Return a zero-padded hexadecimal representation of an IP address
73 function wfIP2Hex( $ip ) {
74 $n = wfIP2Unsigned( $ip );
76 $n = sprintf( '%08X', $n );
82 * Determine if an IP address really is an IP address, and if it is public,
83 * i.e. not RFC 1918 or similar
85 function wfIsIPPublic( $ip ) {
86 $n = wfIP2Unsigned( $ip );
91 static $privateRanges = false;
92 if ( !$privateRanges ) {
93 $privateRanges = array(
94 array( '10.0.0.0', '10.255.255.255' ), # RFC 1918 (private)
95 array( '172.16.0.0', '172.31.255.255' ), # "
96 array( '192.168.0.0', '192.168.255.255' ), # "
97 array( '0.0.0.0', '0.255.255.255' ), # this network
98 array( '127.0.0.0', '127.255.255.255' ), # loopback
102 foreach ( $privateRanges as $r ) {
103 $start = wfIP2Unsigned( $r[0] );
104 $end = wfIP2Unsigned( $r[1] );
105 if ( $n >= $start && $n <= $end ) {
113 * Forks processes to scan the originating IP for an open proxy server
114 * MemCached can be used to skip IPs that have already been scanned
116 function wfProxyCheck() {
117 global $wgBlockOpenProxies, $wgProxyPorts, $wgProxyScriptPath;
118 global $wgUseMemCached, $wgMemc, $wgDBname, $wgProxyMemcExpiry;
120 if ( !$wgBlockOpenProxies ) {
128 if ( $wgUseMemCached ) {
129 $mcKey = "$wgDBname:proxy:ip:$ip";
130 $mcValue = $wgMemc->get( $mcKey );
138 $title = Title
::makeTitle( NS_SPECIAL
, 'Blockme' );
139 $iphash = md5( $ip . $wgProxyKey );
140 $url = $title->getFullURL( 'ip='.$iphash );
142 foreach ( $wgProxyPorts as $port ) {
143 $params = implode( ' ', array(
144 escapeshellarg( $wgProxyScriptPath ),
145 escapeshellarg( $ip ),
146 escapeshellarg( $port ),
147 escapeshellarg( $url )
149 exec( "php $params &>/dev/null &" );
152 if ( $wgUseMemCached ) {
153 $wgMemc->set( $mcKey, 1, $wgProxyMemcExpiry );
159 * Convert a network specification in CIDR notation to an integer network and a number of bits
161 function wfParseCIDR( $range ) {
162 $parts = explode( '/', $range, 2 );
163 if ( count( $parts ) != 2 ) {
164 return array( false, false );
166 $network = wfIP2Unsigned( $parts[0] );
167 if ( $network !== false && is_numeric( $parts[1] ) && $parts[1] >= 0 && $parts[1] <= 32 ) {
173 return array( $network, $bits );
177 * Check if an IP address is in the local proxy list
179 function wfIsLocallyBlockedProxy( $ip ) {
181 $fname = 'wfIsLocallyBlockedProxy';
183 if ( !$wgProxyList ) {
186 wfProfileIn( $fname );
188 if ( !is_array( $wgProxyList ) ) {
189 # Load from the specified file
190 $wgProxyList = array_map( 'trim', file( $wgProxyList ) );
193 if ( !is_array( $wgProxyList ) ) {
195 } elseif ( array_search( $ip, $wgProxyList ) !== false ) {
197 } elseif ( array_key_exists( $ip, $wgProxyList ) ) {
198 # Old-style flipped proxy list
203 wfProfileOut( $fname );