(bug 28868) Include the number of pages in the default getLongDesc for multipaged...
[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 * Checks first for "X-Forwarded-For", then "Client-ip"
11 * Note: headers are spoofable
12 * @return string
13 */
14 function wfGetForwardedFor() {
15 $apacheHeaders = function_exists( 'apache_request_headers' ) ? apache_request_headers() : null;
16 if( is_array( $apacheHeaders ) ) {
17 // More reliable than $_SERVER due to case and -/_ folding
18 $set = array();
19 foreach ( $apacheHeaders as $tempName => $tempValue ) {
20 $set[ strtoupper( $tempName ) ] = $tempValue;
21 }
22 $index = strtoupper ( 'X-Forwarded-For' );
23 $index2 = strtoupper ( 'Client-ip' );
24 } else {
25 // Subject to spoofing with headers like X_Forwarded_For
26 $set = $_SERVER;
27 $index = 'HTTP_X_FORWARDED_FOR';
28 $index2 = 'CLIENT-IP';
29 }
30
31 #Try a couple of headers
32 if( isset( $set[$index] ) ) {
33 return $set[$index];
34 } else if( isset( $set[$index2] ) ) {
35 return $set[$index2];
36 } else {
37 return null;
38 }
39 }
40
41 /**
42 * Returns the browser/OS data from the request header
43 * Note: headers are spoofable
44 * @return string
45 */
46 function wfGetAgent() {
47 if( function_exists( 'apache_request_headers' ) ) {
48 // More reliable than $_SERVER due to case and -/_ folding
49 $set = array();
50 foreach ( apache_request_headers() as $tempName => $tempValue ) {
51 $set[ strtoupper( $tempName ) ] = $tempValue;
52 }
53 $index = strtoupper ( 'User-Agent' );
54 } else {
55 // Subject to spoofing with headers like X_Forwarded_For
56 $set = $_SERVER;
57 $index = 'HTTP_USER_AGENT';
58 }
59 if( isset( $set[$index] ) ) {
60 return $set[$index];
61 } else {
62 return '';
63 }
64 }
65
66 /**
67 * Work out the IP address based on various globals
68 * For trusted proxies, use the XFF client IP (first of the chain)
69 * @return string
70 */
71 function wfGetIP() {
72 global $wgUsePrivateIPs, $wgCommandLineMode;
73 static $ip = false;
74
75 # Return cached result
76 if ( !empty( $ip ) ) {
77 return $ip;
78 }
79
80 $ipchain = array();
81
82 /* collect the originating ips */
83 # Client connecting to this webserver
84 if ( isset( $_SERVER['REMOTE_ADDR'] ) ) {
85 $ip = IP::canonicalize( $_SERVER['REMOTE_ADDR'] );
86 } elseif( $wgCommandLineMode ) {
87 $ip = '127.0.0.1';
88 }
89 if( $ip ) {
90 $ipchain[] = $ip;
91 }
92
93 # Append XFF on to $ipchain
94 $forwardedFor = wfGetForwardedFor();
95 if ( isset( $forwardedFor ) ) {
96 $xff = array_map( 'trim', explode( ',', $forwardedFor ) );
97 $xff = array_reverse( $xff );
98 $ipchain = array_merge( $ipchain, $xff );
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 # Allow extensions to improve our guess
117 wfRunHooks( 'GetIP', array( &$ip ) );
118
119 if( !$ip ) {
120 throw new MWException( "Unable to determine IP" );
121 }
122
123 wfDebug( "IP: $ip\n" );
124 return $ip;
125 }
126
127 /**
128 * Checks if an IP is a trusted proxy providor
129 * Useful to tell if X-Fowarded-For data is possibly bogus
130 * Squid cache servers for the site and AOL are whitelisted
131 * @param $ip String
132 * @return bool
133 */
134 function wfIsTrustedProxy( $ip ) {
135 global $wgSquidServers, $wgSquidServersNoPurge;
136
137 $trusted = in_array( $ip, $wgSquidServers ) ||
138 in_array( $ip, $wgSquidServersNoPurge );
139 wfRunHooks( 'IsTrustedProxy', array( &$ip, &$trusted ) );
140 return $trusted;
141 }
142
143 /**
144 * Forks processes to scan the originating IP for an open proxy server
145 * MemCached can be used to skip IPs that have already been scanned
146 */
147 function wfProxyCheck() {
148 global $wgBlockOpenProxies, $wgProxyPorts, $wgProxyScriptPath;
149 global $wgMemc, $wgProxyMemcExpiry;
150 global $wgProxyKey;
151
152 if ( !$wgBlockOpenProxies ) {
153 return;
154 }
155
156 $ip = wfGetIP();
157
158 # Get MemCached key
159 $mcKey = wfMemcKey( 'proxy', 'ip', $ip );
160 $mcValue = $wgMemc->get( $mcKey );
161 $skip = (bool)$mcValue;
162
163 # Fork the processes
164 if ( !$skip ) {
165 $title = SpecialPage::getTitleFor( 'Blockme' );
166 $iphash = md5( $ip . $wgProxyKey );
167 $url = $title->getFullURL( 'ip='.$iphash );
168
169 foreach ( $wgProxyPorts as $port ) {
170 $params = implode( ' ', array(
171 escapeshellarg( $wgProxyScriptPath ),
172 escapeshellarg( $ip ),
173 escapeshellarg( $port ),
174 escapeshellarg( $url )
175 ));
176 exec( "php $params >" . wfGetNull() . " 2>&1 &" );
177 }
178 # Set MemCached key
179 $wgMemc->set( $mcKey, 1, $wgProxyMemcExpiry );
180 }
181 }
182
183 /**
184 * Check if an IP address is in the local proxy list
185 * @return bool
186 */
187 function wfIsLocallyBlockedProxy( $ip ) {
188 global $wgProxyList;
189
190 if ( !$wgProxyList ) {
191 return false;
192 }
193 wfProfileIn( __METHOD__ );
194
195 if ( !is_array( $wgProxyList ) ) {
196 # Load from the specified file
197 $wgProxyList = array_map( 'trim', file( $wgProxyList ) );
198 }
199
200 if ( !is_array( $wgProxyList ) ) {
201 $ret = false;
202 } elseif ( array_search( $ip, $wgProxyList ) !== false ) {
203 $ret = true;
204 } elseif ( array_key_exists( $ip, $wgProxyList ) ) {
205 # Old-style flipped proxy list
206 $ret = true;
207 } else {
208 $ret = false;
209 }
210 wfProfileOut( __METHOD__ );
211 return $ret;
212 }
213