Tests for wfGetIP() follow up r89407
[lhc/web/wiklou.git] / includes / ProxyTools.php
1 <?php
2 /**
3 * Functions for dealing with proxies
4 *
5 * @file
6 */
7
8 /**
9 * Extracts the XFF string from the request header
10 * Note: headers are spoofable
11 * @return string
12 */
13 function wfGetForwardedFor() {
14 $apacheHeaders = function_exists( 'apache_request_headers' ) ? apache_request_headers() : null;
15 if( is_array( $apacheHeaders ) ) {
16 // More reliable than $_SERVER due to case and -/_ folding
17 $set = array();
18 foreach ( $apacheHeaders as $tempName => $tempValue ) {
19 $set[ strtoupper( $tempName ) ] = $tempValue;
20 }
21 $index = strtoupper ( 'X-Forwarded-For' );
22 } else {
23 // Subject to spoofing with headers like X_Forwarded_For
24 $set = $_SERVER;
25 $index = 'HTTP_X_FORWARDED_FOR';
26 }
27
28 #Try to see if XFF is set
29 if( isset( $set[$index] ) ) {
30 return $set[$index];
31 } else {
32 return null;
33 }
34 }
35
36 /**
37 * Returns the browser/OS data from the request header
38 * Note: headers are spoofable
39 *
40 * @deprecated in 1.18; use $wgRequest->getHeader( 'User-Agent' ) instead.
41 * @return string
42 */
43 function wfGetAgent() {
44 wfDeprecated( __FUNCTION__ );
45 if( function_exists( 'apache_request_headers' ) ) {
46 // More reliable than $_SERVER due to case and -/_ folding
47 $set = array();
48 foreach ( apache_request_headers() as $tempName => $tempValue ) {
49 $set[ strtoupper( $tempName ) ] = $tempValue;
50 }
51 $index = strtoupper ( 'User-Agent' );
52 } else {
53 // Subject to spoofing with headers like X_Forwarded_For
54 $set = $_SERVER;
55 $index = 'HTTP_USER_AGENT';
56 }
57 if( isset( $set[$index] ) ) {
58 return $set[$index];
59 } else {
60 return '';
61 }
62 }
63
64 /**
65 * Work out the IP address based on various globals
66 * For trusted proxies, use the XFF client IP (first of the chain)
67 * @param $reset boolean Used to reset the internal static variable
68 * tracking the IP address. (default: false)
69 * @return string
70 */
71 function wfGetIP( $reset = false ) {
72 global $wgUsePrivateIPs, $wgCommandLineMode;
73 static $ip = false;
74
75 if( $reset ) {
76 $ip = false;
77 }
78
79 # Return cached result
80 if ( !empty( $ip ) ) {
81 return $ip;
82 }
83
84 /* collect the originating ips */
85 # Client connecting to this webserver
86 if ( isset( $_SERVER['REMOTE_ADDR'] ) ) {
87 $ip = IP::canonicalize( $_SERVER['REMOTE_ADDR'] );
88 } elseif( $wgCommandLineMode ) {
89 $ip = '127.0.0.1';
90 }
91
92 # Append XFF
93 $forwardedFor = wfGetForwardedFor();
94 if ( $forwardedFor !== null ) {
95 $ipchain = array_map( 'trim', explode( ',', $forwardedFor ) );
96 $ipchain = array_reverse( $ipchain );
97 if ( $ip ) {
98 array_unshift( $ipchain, $ip );
99 }
100
101 # Step through XFF list and find the last address in the list which is a trusted server
102 # Set $ip to the IP address given by that trusted server, unless the address is not sensible (e.g. private)
103 foreach ( $ipchain as $i => $curIP ) {
104 $curIP = IP::canonicalize( $curIP );
105 if ( wfIsTrustedProxy( $curIP ) ) {
106 if ( isset( $ipchain[$i + 1] ) ) {
107 if( $wgUsePrivateIPs || IP::isPublic( $ipchain[$i + 1 ] ) ) {
108 $ip = $ipchain[$i + 1];
109 }
110 }
111 } else {
112 break;
113 }
114 }
115 }
116
117 # Allow extensions to improve our guess
118 wfRunHooks( 'GetIP', array( &$ip ) );
119
120 if( !$ip ) {
121 throw new MWException( "Unable to determine IP" );
122 }
123
124 wfDebug( "IP: $ip\n" );
125 return $ip;
126 }
127
128 /**
129 * Checks if an IP is a trusted proxy providor
130 * Useful to tell if X-Fowarded-For data is possibly bogus
131 * Squid cache servers for the site and AOL are whitelisted
132 * @param $ip String
133 * @return bool
134 */
135 function wfIsTrustedProxy( $ip ) {
136 global $wgSquidServers, $wgSquidServersNoPurge;
137
138 $trusted = in_array( $ip, $wgSquidServers ) ||
139 in_array( $ip, $wgSquidServersNoPurge );
140 wfRunHooks( 'IsTrustedProxy', array( &$ip, &$trusted ) );
141 return $trusted;
142 }
143
144 /**
145 * Forks processes to scan the originating IP for an open proxy server
146 * MemCached can be used to skip IPs that have already been scanned
147 */
148 function wfProxyCheck() {
149 global $wgBlockOpenProxies, $wgProxyPorts, $wgProxyScriptPath;
150 global $wgMemc, $wgProxyMemcExpiry;
151 global $wgProxyKey;
152
153 if ( !$wgBlockOpenProxies ) {
154 return;
155 }
156
157 $ip = wfGetIP();
158
159 # Get MemCached key
160 $mcKey = wfMemcKey( 'proxy', 'ip', $ip );
161 $mcValue = $wgMemc->get( $mcKey );
162 $skip = (bool)$mcValue;
163
164 # Fork the processes
165 if ( !$skip ) {
166 $title = SpecialPage::getTitleFor( 'Blockme' );
167 $iphash = md5( $ip . $wgProxyKey );
168 $url = $title->getFullURL( 'ip='.$iphash );
169
170 foreach ( $wgProxyPorts as $port ) {
171 $params = implode( ' ', array(
172 escapeshellarg( $wgProxyScriptPath ),
173 escapeshellarg( $ip ),
174 escapeshellarg( $port ),
175 escapeshellarg( $url )
176 ));
177 exec( "php $params >" . wfGetNull() . " 2>&1 &" );
178 }
179 # Set MemCached key
180 $wgMemc->set( $mcKey, 1, $wgProxyMemcExpiry );
181 }
182 }