3 * Functions for dealing with proxies
6 function wfGetForwardedFor() {
7 if( function_exists( 'apache_request_headers' ) ) {
8 // More reliable than $_SERVER due to case and -/_ folding
9 $set = apache_request_headers();
10 $index = 'X-Forwarded-For';
12 // Subject to spoofing with headers like X_Forwarded_For
14 $index = 'HTTP_X_FORWARDED_FOR';
16 if( isset( $set[$index] ) ) {
23 /** Work out the IP address based on various globals */
27 # Return cached result
28 if ( !empty( $wgIP ) ) {
32 /* collect the originating ips */
33 # Client connecting to this webserver
34 if ( isset( $_SERVER['REMOTE_ADDR'] ) ) {
35 $ipchain = array( IP
::canonicalize( $_SERVER['REMOTE_ADDR'] ) );
38 $ipchain = array( '127.0.0.1' );
42 # Append XFF on to $ipchain
43 $forwardedFor = wfGetForwardedFor();
44 if ( isset( $forwardedFor ) ) {
45 $xff = array_map( 'trim', explode( ',', $forwardedFor ) );
46 $xff = array_reverse( $xff );
47 $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 $curIP = IP
::canonicalize( $curIP );
54 if ( wfIsTrustedProxy( $curIP ) ) {
55 if ( isset( $ipchain[$i +
1] ) && IP
::isPublic( $ipchain[$i +
1] ) ) {
56 $ip = $ipchain[$i +
1];
63 wfDebug( "IP: $ip\n" );
68 function wfIsTrustedProxy( $ip ) {
69 global $wgSquidServers, $wgSquidServersNoPurge;
71 if ( in_array( $ip, $wgSquidServers ) ||
72 in_array( $ip, $wgSquidServersNoPurge ) ||
79 wfRunHooks( 'IsTrustedProxy', array( &$ip, &$trusted ) );
84 * Forks processes to scan the originating IP for an open proxy server
85 * MemCached can be used to skip IPs that have already been scanned
87 function wfProxyCheck() {
88 global $wgBlockOpenProxies, $wgProxyPorts, $wgProxyScriptPath;
89 global $wgUseMemCached, $wgMemc, $wgProxyMemcExpiry;
92 if ( !$wgBlockOpenProxies ) {
100 if ( $wgUseMemCached ) {
101 $mcKey = wfMemcKey( 'proxy', 'ip', $ip );
102 $mcValue = $wgMemc->get( $mcKey );
110 $title = SpecialPage
::getTitleFor( 'Blockme' );
111 $iphash = md5( $ip . $wgProxyKey );
112 $url = $title->getFullURL( 'ip='.$iphash );
114 foreach ( $wgProxyPorts as $port ) {
115 $params = implode( ' ', array(
116 escapeshellarg( $wgProxyScriptPath ),
117 escapeshellarg( $ip ),
118 escapeshellarg( $port ),
119 escapeshellarg( $url )
121 exec( "php $params &>/dev/null &" );
124 if ( $wgUseMemCached ) {
125 $wgMemc->set( $mcKey, 1, $wgProxyMemcExpiry );
131 * Convert a network specification in CIDR notation to an integer network and a number of bits
133 function wfParseCIDR( $range ) {
134 return IP
::parseCIDR( $range );
138 * Check if an IP address is in the local proxy list
140 function wfIsLocallyBlockedProxy( $ip ) {
142 $fname = 'wfIsLocallyBlockedProxy';
144 if ( !$wgProxyList ) {
147 wfProfileIn( $fname );
149 if ( !is_array( $wgProxyList ) ) {
150 # Load from the specified file
151 $wgProxyList = array_map( 'trim', file( $wgProxyList ) );
154 if ( !is_array( $wgProxyList ) ) {
156 } elseif ( array_search( $ip, $wgProxyList ) !== false ) {
158 } elseif ( array_key_exists( $ip, $wgProxyList ) ) {
159 # Old-style flipped proxy list
164 wfProfileOut( $fname );
169 * TODO: move this list to the database in a global IP info table incorporating
170 * trusted ISP proxies, blocked IP addresses and open proxies.
172 function wfIsAOLProxy( $ip ) {
193 '205.188.146.144/30',
197 static $parsedRanges;
198 if ( is_null( $parsedRanges ) ) {
199 $parsedRanges = array();
200 foreach ( $ranges as $range ) {
201 $parsedRanges[] = IP
::parseRange( $range );
205 $hex = IP
::toHex( $ip );
206 foreach ( $parsedRanges as $range ) {
207 if ( $hex >= $range[0] && $hex <= $range[1] ) {