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