3 * Functions for dealing with proxies
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';
13 // Subject to spoofing with headers like X_Forwarded_For
15 $index = 'HTTP_X_FORWARDED_FOR';
17 if( isset( $set[$index] ) ) {
24 /** Work out the IP address based on various globals */
28 # Return cached result
29 if ( !empty( $wgIP ) ) {
33 /* collect the originating ips */
34 # Client connecting to this webserver
35 if ( isset( $_SERVER['REMOTE_ADDR'] ) ) {
36 $ipchain = array( IP
::canonicalize( $_SERVER['REMOTE_ADDR'] ) );
39 $ipchain = array( '127.0.0.1' );
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 );
51 # Step through XFF list and find the last address in the list which is a trusted server
52 # Set $ip to the IP address given by that trusted server, unless the address is not sensible (e.g. private)
53 foreach ( $ipchain as $i => $curIP ) {
54 $curIP = IP
::canonicalize( $curIP );
55 if ( wfIsTrustedProxy( $curIP ) ) {
56 if ( isset( $ipchain[$i +
1] ) && IP
::isPublic( $ipchain[$i +
1] ) ) {
57 $ip = $ipchain[$i +
1];
64 wfDebug( "IP: $ip\n" );
69 function wfIsTrustedProxy( $ip ) {
70 global $wgSquidServers, $wgSquidServersNoPurge;
72 if ( in_array( $ip, $wgSquidServers ) ||
73 in_array( $ip, $wgSquidServersNoPurge ) ||
80 wfRunHooks( 'IsTrustedProxy', array( &$ip, &$trusted ) );
85 * Forks processes to scan the originating IP for an open proxy server
86 * MemCached can be used to skip IPs that have already been scanned
88 function wfProxyCheck() {
89 global $wgBlockOpenProxies, $wgProxyPorts, $wgProxyScriptPath;
90 global $wgUseMemCached, $wgMemc, $wgProxyMemcExpiry;
93 if ( !$wgBlockOpenProxies ) {
101 if ( $wgUseMemCached ) {
102 $mcKey = wfMemcKey( 'proxy', 'ip', $ip );
103 $mcValue = $wgMemc->get( $mcKey );
111 $title = SpecialPage
::getTitleFor( 'Blockme' );
112 $iphash = md5( $ip . $wgProxyKey );
113 $url = $title->getFullURL( 'ip='.$iphash );
115 foreach ( $wgProxyPorts as $port ) {
116 $params = implode( ' ', array(
117 escapeshellarg( $wgProxyScriptPath ),
118 escapeshellarg( $ip ),
119 escapeshellarg( $port ),
120 escapeshellarg( $url )
122 exec( "php $params &>/dev/null &" );
125 if ( $wgUseMemCached ) {
126 $wgMemc->set( $mcKey, 1, $wgProxyMemcExpiry );
132 * Convert a network specification in CIDR notation to an integer network and a number of bits
134 function wfParseCIDR( $range ) {
135 return IP
::parseCIDR( $range );
139 * Check if an IP address is in the local proxy list
141 function wfIsLocallyBlockedProxy( $ip ) {
143 $fname = 'wfIsLocallyBlockedProxy';
145 if ( !$wgProxyList ) {
148 wfProfileIn( $fname );
150 if ( !is_array( $wgProxyList ) ) {
151 # Load from the specified file
152 $wgProxyList = array_map( 'trim', file( $wgProxyList ) );
155 if ( !is_array( $wgProxyList ) ) {
157 } elseif ( array_search( $ip, $wgProxyList ) !== false ) {
159 } elseif ( array_key_exists( $ip, $wgProxyList ) ) {
160 # Old-style flipped proxy list
165 wfProfileOut( $fname );
170 * TODO: move this list to the database in a global IP info table incorporating
171 * trusted ISP proxies, blocked IP addresses and open proxies.
173 function wfIsAOLProxy( $ip ) {
194 '205.188.146.144/30',
198 static $parsedRanges;
199 if ( is_null( $parsedRanges ) ) {
200 $parsedRanges = array();
201 foreach ( $ranges as $range ) {
202 $parsedRanges[] = IP
::parseRange( $range );
206 $hex = IP
::toHex( $ip );
207 foreach ( $parsedRanges as $range ) {
208 if ( $hex >= $range[0] && $hex <= $range[1] ) {