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( $_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 );
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 if ( wfIsTrustedProxy( $curIP ) ) {
54 if ( isset( $ipchain[$i +
1] ) && IP
::isPublic( $ipchain[$i +
1] ) ) {
55 $ip = $ipchain[$i +
1];
62 wfDebug( "IP: $ip\n" );
67 function wfIsTrustedProxy( $ip ) {
68 global $wgSquidServers, $wgSquidServersNoPurge;
70 if ( in_array( $ip, $wgSquidServers ) ||
71 in_array( $ip, $wgSquidServersNoPurge ) ||
78 wfRunHooks( 'IsTrustedProxy', array( &$ip, &$trusted ) );
83 * Forks processes to scan the originating IP for an open proxy server
84 * MemCached can be used to skip IPs that have already been scanned
86 function wfProxyCheck() {
87 global $wgBlockOpenProxies, $wgProxyPorts, $wgProxyScriptPath;
88 global $wgUseMemCached, $wgMemc, $wgProxyMemcExpiry;
91 if ( !$wgBlockOpenProxies ) {
99 if ( $wgUseMemCached ) {
100 $mcKey = wfMemcKey( 'proxy', 'ip', $ip );
101 $mcValue = $wgMemc->get( $mcKey );
109 $title = SpecialPage
::getTitleFor( 'Blockme' );
110 $iphash = md5( $ip . $wgProxyKey );
111 $url = $title->getFullURL( 'ip='.$iphash );
113 foreach ( $wgProxyPorts as $port ) {
114 $params = implode( ' ', array(
115 escapeshellarg( $wgProxyScriptPath ),
116 escapeshellarg( $ip ),
117 escapeshellarg( $port ),
118 escapeshellarg( $url )
120 exec( "php $params &>/dev/null &" );
123 if ( $wgUseMemCached ) {
124 $wgMemc->set( $mcKey, 1, $wgProxyMemcExpiry );
130 * Convert a network specification in CIDR notation to an integer network and a number of bits
132 function wfParseCIDR( $range ) {
133 return IP
::parseCIDR( $range );
137 * Check if an IP address is in the local proxy list
139 function wfIsLocallyBlockedProxy( $ip ) {
141 $fname = 'wfIsLocallyBlockedProxy';
143 if ( !$wgProxyList ) {
146 wfProfileIn( $fname );
148 if ( !is_array( $wgProxyList ) ) {
149 # Load from the specified file
150 $wgProxyList = array_map( 'trim', file( $wgProxyList ) );
153 if ( !is_array( $wgProxyList ) ) {
155 } elseif ( array_search( $ip, $wgProxyList ) !== false ) {
157 } elseif ( array_key_exists( $ip, $wgProxyList ) ) {
158 # Old-style flipped proxy list
163 wfProfileOut( $fname );
168 * TODO: move this list to the database in a global IP info table incorporating
169 * trusted ISP proxies, blocked IP addresses and open proxies.
171 function wfIsAOLProxy( $ip ) {
192 '205.188.146.144/30',
196 static $parsedRanges;
197 if ( is_null( $parsedRanges ) ) {
198 $parsedRanges = array();
199 foreach ( $ranges as $range ) {
200 $parsedRanges[] = IP
::parseRange( $range );
204 $hex = IP
::toHex( $ip );
205 foreach ( $parsedRanges as $range ) {
206 if ( $hex >= $range[0] && $hex <= $range[1] ) {