fix some doxygen warnings
[lhc/web/wiklou.git] / includes / ProxyTools.php
1 <?php
2 /**
3 * Functions for dealing with proxies
4 * @file
5 */
6
7 /**
8 * Extracts the XFF string from the request header
9 * Checks first for "X-Forwarded-For", then "Client-ip"
10 * Note: headers are spoofable
11 * @return string
12 */
13 function wfGetForwardedFor() {
14 if( function_exists( 'apache_request_headers' ) ) {
15 // More reliable than $_SERVER due to case and -/_ folding
16 $set = array ();
17 foreach ( apache_request_headers() as $tempName => $tempValue ) {
18 $set[ strtoupper( $tempName ) ] = $tempValue;
19 }
20 $index = strtoupper ( 'X-Forwarded-For' );
21 $index2 = strtoupper ( 'Client-ip' );
22 } else {
23 // Subject to spoofing with headers like X_Forwarded_For
24 $set = $_SERVER;
25 $index = 'HTTP_X_FORWARDED_FOR';
26 $index2 = 'CLIENT-IP';
27 }
28
29 #Try a couple of headers
30 if( isset( $set[$index] ) ) {
31 return $set[$index];
32 } else if( isset( $set[$index2] ) ) {
33 return $set[$index2];
34 } else {
35 return null;
36 }
37 }
38
39 /**
40 * Returns the browser/OS data from the request header
41 * Note: headers are spoofable
42 * @return string
43 */
44 function wfGetAgent() {
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 * @return string
68 */
69 function wfGetIP() {
70 global $wgIP;
71
72 # Return cached result
73 if ( !empty( $wgIP ) ) {
74 return $wgIP;
75 }
76
77 /* collect the originating ips */
78 # Client connecting to this webserver
79 if ( isset( $_SERVER['REMOTE_ADDR'] ) ) {
80 $ipchain = array( IP::canonicalize( $_SERVER['REMOTE_ADDR'] ) );
81 } else {
82 # Running on CLI?
83 $ipchain = array( '127.0.0.1' );
84 }
85 $ip = $ipchain[0];
86
87 # Append XFF on to $ipchain
88 $forwardedFor = wfGetForwardedFor();
89 if ( isset( $forwardedFor ) ) {
90 $xff = array_map( 'trim', explode( ',', $forwardedFor ) );
91 $xff = array_reverse( $xff );
92 $ipchain = array_merge( $ipchain, $xff );
93 }
94
95 # Step through XFF list and find the last address in the list which is a trusted server
96 # Set $ip to the IP address given by that trusted server, unless the address is not sensible (e.g. private)
97 foreach ( $ipchain as $i => $curIP ) {
98 $curIP = IP::canonicalize( $curIP );
99 if ( wfIsTrustedProxy( $curIP ) ) {
100 if ( isset( $ipchain[$i + 1] ) && IP::isPublic( $ipchain[$i + 1] ) ) {
101 $ip = $ipchain[$i + 1];
102 }
103 } else {
104 break;
105 }
106 }
107
108 wfDebug( "IP: $ip\n" );
109 $wgIP = $ip;
110 return $ip;
111 }
112
113 /**
114 * Checks if an IP is a trusted proxy providor
115 * Useful to tell if X-Fowarded-For data is possibly bogus
116 * Squid cache servers for the site and AOL are whitelisted
117 * @param string $ip
118 * @return bool
119 */
120 function wfIsTrustedProxy( $ip ) {
121 global $wgSquidServers, $wgSquidServersNoPurge;
122
123 if ( in_array( $ip, $wgSquidServers ) ||
124 in_array( $ip, $wgSquidServersNoPurge )
125 ) {
126 $trusted = true;
127 } else {
128 $trusted = false;
129 }
130 wfRunHooks( 'IsTrustedProxy', array( &$ip, &$trusted ) );
131 return $trusted;
132 }
133
134 /**
135 * Forks processes to scan the originating IP for an open proxy server
136 * MemCached can be used to skip IPs that have already been scanned
137 */
138 function wfProxyCheck() {
139 global $wgBlockOpenProxies, $wgProxyPorts, $wgProxyScriptPath;
140 global $wgMemc, $wgProxyMemcExpiry;
141 global $wgProxyKey;
142
143 if ( !$wgBlockOpenProxies ) {
144 return;
145 }
146
147 $ip = wfGetIP();
148
149 # Get MemCached key
150 $mcKey = wfMemcKey( 'proxy', 'ip', $ip );
151 $mcValue = $wgMemc->get( $mcKey );
152 $skip = (bool)$mcValue;
153
154 # Fork the processes
155 if ( !$skip ) {
156 $title = SpecialPage::getTitleFor( 'Blockme' );
157 $iphash = md5( $ip . $wgProxyKey );
158 $url = $title->getFullURL( 'ip='.$iphash );
159
160 foreach ( $wgProxyPorts as $port ) {
161 $params = implode( ' ', array(
162 escapeshellarg( $wgProxyScriptPath ),
163 escapeshellarg( $ip ),
164 escapeshellarg( $port ),
165 escapeshellarg( $url )
166 ));
167 exec( "php $params &>/dev/null &" );
168 }
169 # Set MemCached key
170 $wgMemc->set( $mcKey, 1, $wgProxyMemcExpiry );
171 }
172 }
173
174 /**
175 * Convert a network specification in CIDR notation to an integer network and a number of bits
176 * @return array(string, int)
177 */
178 function wfParseCIDR( $range ) {
179 return IP::parseCIDR( $range );
180 }
181
182 /**
183 * Check if an IP address is in the local proxy list
184 * @return bool
185 */
186 function wfIsLocallyBlockedProxy( $ip ) {
187 global $wgProxyList;
188 $fname = 'wfIsLocallyBlockedProxy';
189
190 if ( !$wgProxyList ) {
191 return false;
192 }
193 wfProfileIn( $fname );
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( $fname );
211 return $ret;
212 }
213