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 */
26 global $wgSquidServers, $wgSquidServersNoPurge, $wgIP;
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 # Get list of trusted proxies
44 # Flipped for quicker access
45 $trustedProxies = array_flip( array_merge( $wgSquidServers, $wgSquidServersNoPurge ) );
46 if ( count( $trustedProxies ) ) {
47 # Append XFF on to $ipchain
48 $forwardedFor = wfGetForwardedFor();
49 if ( isset( $forwardedFor ) ) {
50 $xff = array_map( 'trim', explode( ',', $forwardedFor ) );
51 $xff = array_reverse( $xff );
52 $ipchain = array_merge( $ipchain, $xff );
54 # Step through XFF list and find the last address in the list which is a trusted server
55 # Set $ip to the IP address given by that trusted server, unless the address is not sensible (e.g. private)
56 foreach ( $ipchain as $i => $curIP ) {
57 if ( array_key_exists( $curIP, $trustedProxies ) ) {
58 if ( isset( $ipchain[$i +
1] ) && IP
::isPublic( $ipchain[$i +
1] ) ) {
59 $ip = $ipchain[$i +
1];
67 wfDebug( "IP: $ip\n" );
73 * Forks processes to scan the originating IP for an open proxy server
74 * MemCached can be used to skip IPs that have already been scanned
76 function wfProxyCheck() {
77 global $wgBlockOpenProxies, $wgProxyPorts, $wgProxyScriptPath;
78 global $wgUseMemCached, $wgMemc, $wgDBname, $wgProxyMemcExpiry;
81 if ( !$wgBlockOpenProxies ) {
89 if ( $wgUseMemCached ) {
90 $mcKey = "$wgDBname:proxy:ip:$ip";
91 $mcValue = $wgMemc->get( $mcKey );
99 $title = Title
::makeTitle( NS_SPECIAL
, 'Blockme' );
100 $iphash = md5( $ip . $wgProxyKey );
101 $url = $title->getFullURL( 'ip='.$iphash );
103 foreach ( $wgProxyPorts as $port ) {
104 $params = implode( ' ', array(
105 escapeshellarg( $wgProxyScriptPath ),
106 escapeshellarg( $ip ),
107 escapeshellarg( $port ),
108 escapeshellarg( $url )
110 exec( "php $params &>/dev/null &" );
113 if ( $wgUseMemCached ) {
114 $wgMemc->set( $mcKey, 1, $wgProxyMemcExpiry );
120 * Convert a network specification in CIDR notation to an integer network and a number of bits
122 function wfParseCIDR( $range ) {
123 $parts = explode( '/', $range, 2 );
124 if ( count( $parts ) != 2 ) {
125 return array( false, false );
127 $network = IP
::toUnsigned( $parts[0] );
128 if ( $network !== false && is_numeric( $parts[1] ) && $parts[1] >= 0 && $parts[1] <= 32 ) {
134 return array( $network, $bits );
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 );