Added some GPL headers
[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 } elseif( 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 *
45 * @deprecated in 1.19; use $wgRequest->getHeader( 'User-Agent' ) instead.
46 * @return string
47 */
48 function wfGetAgent() {
49 wfDeprecated( __FUNCTION__ );
50 if( function_exists( 'apache_request_headers' ) ) {
51 // More reliable than $_SERVER due to case and -/_ folding
52 $set = array();
53 foreach ( apache_request_headers() as $tempName => $tempValue ) {
54 $set[ strtoupper( $tempName ) ] = $tempValue;
55 }
56 $index = strtoupper ( 'User-Agent' );
57 } else {
58 // Subject to spoofing with headers like X_Forwarded_For
59 $set = $_SERVER;
60 $index = 'HTTP_USER_AGENT';
61 }
62 if( isset( $set[$index] ) ) {
63 return $set[$index];
64 } else {
65 return '';
66 }
67 }
68
69 /**
70 * Work out the IP address based on various globals
71 * For trusted proxies, use the XFF client IP (first of the chain)
72 * @return string
73 */
74 function wfGetIP() {
75 global $wgUsePrivateIPs, $wgCommandLineMode;
76 static $ip = false;
77
78 # Return cached result
79 if ( !empty( $ip ) ) {
80 return $ip;
81 }
82
83 /* collect the originating ips */
84 # Client connecting to this webserver
85 if ( isset( $_SERVER['REMOTE_ADDR'] ) ) {
86 $ip = IP::canonicalize( $_SERVER['REMOTE_ADDR'] );
87 } elseif( $wgCommandLineMode ) {
88 $ip = '127.0.0.1';
89 }
90
91 # Append XFF
92 $forwardedFor = wfGetForwardedFor();
93 if ( $forwardedFor !== null ) {
94 $ipchain = array_map( 'trim', explode( ',', $forwardedFor ) );
95 $ipchain = array_reverse( $ipchain );
96 if ( $ip ) {
97 array_unshift( $ipchain, $ip );
98 }
99
100 # Step through XFF list and find the last address in the list which is a trusted server
101 # Set $ip to the IP address given by that trusted server, unless the address is not sensible (e.g. private)
102 foreach ( $ipchain as $i => $curIP ) {
103 $curIP = IP::canonicalize( $curIP );
104 if ( wfIsTrustedProxy( $curIP ) ) {
105 if ( isset( $ipchain[$i + 1] ) ) {
106 if( $wgUsePrivateIPs || IP::isPublic( $ipchain[$i + 1 ] ) ) {
107 $ip = $ipchain[$i + 1];
108 }
109 }
110 } else {
111 break;
112 }
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 }