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';
11 $index2 = 'Client-ip';
13 // Subject to spoofing with headers like X_Forwarded_For
15 $index = 'HTTP_X_FORWARDED_FOR';
16 $index2 = 'CLIENT-IP';
18 #Try a couple of headers
19 if( isset( $set[$index] ) ) {
21 } else if( isset( $set[$index2] ) ) {
29 * Locates the client IP within a given XFF string
33 function wfGetClientIPfromXFF( $xff ) {
34 if ( !$xff ) return null;
35 $xff = substr( $xff, 0, 511 );
36 // Just look for the first IP match
37 // We might have a mix of IPv4/IPv6
38 if ( preg_match('#\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}|:(:[0-9A-Fa-f]{1,4}){1,7}|[0-9A-Fa-f]{1,4}(:{1,2}[0-9A-Fa-f]{1,4}|::$){1,7}#', $xff, $ip) ) {
39 // Check if the numbers are valid
40 if ( IP
::isIPAddress($ip) )
47 function wfGetAgent() {
48 if( function_exists( 'apache_request_headers' ) ) {
49 // More reliable than $_SERVER due to case and -/_ folding
50 $set = apache_request_headers();
51 $index = 'User-Agent';
53 // Subject to spoofing with headers like X_Forwarded_For
55 $index = 'HTTP_USER_AGENT';
57 if( isset( $set[$index] ) ) {
64 /** Work out the IP address based on various globals */
68 # Return cached result
69 if ( !empty( $wgIP ) ) {
73 /* collect the originating ips */
74 # Client connecting to this webserver
75 if ( isset( $_SERVER['REMOTE_ADDR'] ) ) {
76 $ipchain = array( IP
::canonicalize( $_SERVER['REMOTE_ADDR'] ) );
79 $ipchain = array( '127.0.0.1' );
83 # Append XFF on to $ipchain
84 $forwardedFor = wfGetForwardedFor();
85 if ( isset( $forwardedFor ) ) {
86 $xff = array_map( 'trim', explode( ',', $forwardedFor ) );
87 $xff = array_reverse( $xff );
88 $ipchain = array_merge( $ipchain, $xff );
91 # Step through XFF list and find the last address in the list which is a trusted server
92 # Set $ip to the IP address given by that trusted server, unless the address is not sensible (e.g. private)
93 foreach ( $ipchain as $i => $curIP ) {
94 $curIP = IP
::canonicalize( $curIP );
95 if ( wfIsTrustedProxy( $curIP ) ) {
96 if ( isset( $ipchain[$i +
1] ) && IP
::isPublic( $ipchain[$i +
1] ) ) {
97 $ip = $ipchain[$i +
1];
104 wfDebug( "IP: $ip\n" );
109 function wfIsTrustedProxy( $ip ) {
110 global $wgSquidServers, $wgSquidServersNoPurge;
112 if ( in_array( $ip, $wgSquidServers ) ||
113 in_array( $ip, $wgSquidServersNoPurge ) ||
120 wfRunHooks( 'IsTrustedProxy', array( &$ip, &$trusted ) );
125 * Forks processes to scan the originating IP for an open proxy server
126 * MemCached can be used to skip IPs that have already been scanned
128 function wfProxyCheck() {
129 global $wgBlockOpenProxies, $wgProxyPorts, $wgProxyScriptPath;
130 global $wgUseMemCached, $wgMemc, $wgProxyMemcExpiry;
133 if ( !$wgBlockOpenProxies ) {
141 if ( $wgUseMemCached ) {
142 $mcKey = wfMemcKey( 'proxy', 'ip', $ip );
143 $mcValue = $wgMemc->get( $mcKey );
151 $title = SpecialPage
::getTitleFor( 'Blockme' );
152 $iphash = md5( $ip . $wgProxyKey );
153 $url = $title->getFullURL( 'ip='.$iphash );
155 foreach ( $wgProxyPorts as $port ) {
156 $params = implode( ' ', array(
157 escapeshellarg( $wgProxyScriptPath ),
158 escapeshellarg( $ip ),
159 escapeshellarg( $port ),
160 escapeshellarg( $url )
162 exec( "php $params &>/dev/null &" );
165 if ( $wgUseMemCached ) {
166 $wgMemc->set( $mcKey, 1, $wgProxyMemcExpiry );
172 * Convert a network specification in CIDR notation to an integer network and a number of bits
174 function wfParseCIDR( $range ) {
175 return IP
::parseCIDR( $range );
179 * Check if an IP address is in the local proxy list
181 function wfIsLocallyBlockedProxy( $ip ) {
183 $fname = 'wfIsLocallyBlockedProxy';
185 if ( !$wgProxyList ) {
188 wfProfileIn( $fname );
190 if ( !is_array( $wgProxyList ) ) {
191 # Load from the specified file
192 $wgProxyList = array_map( 'trim', file( $wgProxyList ) );
195 if ( !is_array( $wgProxyList ) ) {
197 } elseif ( array_search( $ip, $wgProxyList ) !== false ) {
199 } elseif ( array_key_exists( $ip, $wgProxyList ) ) {
200 # Old-style flipped proxy list
205 wfProfileOut( $fname );
210 * TODO: move this list to the database in a global IP info table incorporating
211 * trusted ISP proxies, blocked IP addresses and open proxies.
213 function wfIsAOLProxy( $ip ) {
234 '205.188.146.144/30',
238 static $parsedRanges;
239 if ( is_null( $parsedRanges ) ) {
240 $parsedRanges = array();
241 foreach ( $ranges as $range ) {
242 $parsedRanges[] = IP
::parseRange( $range );
246 $hex = IP
::toHex( $ip );
247 foreach ( $parsedRanges as $range ) {
248 if ( $hex >= $range[0] && $hex <= $range[1] ) {