If there is no $oldtext, don't bother prepending it
[lhc/web/wiklou.git] / includes / ProxyTools.php
1 <?php
2 /**
3 * Functions for dealing with proxies
4 * @package MediaWiki
5 */
6
7 function wfGetForwardedFor() {
8 if( function_exists( 'apache_request_headers' ) ) {
9 // More reliable than $_SERVER due to case and -/_ folding
10 $set = apache_request_headers();
11 $index = 'X-Forwarded-For';
12 } else {
13 // Subject to spoofing with headers like X_Forwarded_For
14 $set = $_SERVER;
15 $index = 'HTTP_X_FORWARDED_FOR';
16 }
17 if( isset( $set[$index] ) ) {
18 return $set[$index];
19 } else {
20 return null;
21 }
22 }
23
24 /** Work out the IP address based on various globals */
25 function wfGetIP() {
26 global $wgIP;
27
28 # Return cached result
29 if ( !empty( $wgIP ) ) {
30 return $wgIP;
31 }
32
33 /* collect the originating ips */
34 # Client connecting to this webserver
35 if ( isset( $_SERVER['REMOTE_ADDR'] ) ) {
36 $ipchain = array( IP::canonicalize( $_SERVER['REMOTE_ADDR'] ) );
37 } else {
38 # Running on CLI?
39 $ipchain = array( '127.0.0.1' );
40 }
41 $ip = $ipchain[0];
42
43 # Append XFF on to $ipchain
44 $forwardedFor = wfGetForwardedFor();
45 if ( isset( $forwardedFor ) ) {
46 $xff = array_map( 'trim', explode( ',', $forwardedFor ) );
47 $xff = array_reverse( $xff );
48 $ipchain = array_merge( $ipchain, $xff );
49 }
50
51 # Step through XFF list and find the last address in the list which is a trusted server
52 # Set $ip to the IP address given by that trusted server, unless the address is not sensible (e.g. private)
53 foreach ( $ipchain as $i => $curIP ) {
54 $curIP = IP::canonicalize( $curIP );
55 if ( wfIsTrustedProxy( $curIP ) ) {
56 if ( isset( $ipchain[$i + 1] ) && IP::isPublic( $ipchain[$i + 1] ) ) {
57 $ip = $ipchain[$i + 1];
58 }
59 } else {
60 break;
61 }
62 }
63
64 wfDebug( "IP: $ip\n" );
65 $wgIP = $ip;
66 return $ip;
67 }
68
69 function wfIsTrustedProxy( $ip ) {
70 global $wgSquidServers, $wgSquidServersNoPurge;
71
72 if ( in_array( $ip, $wgSquidServers ) ||
73 in_array( $ip, $wgSquidServersNoPurge ) ||
74 wfIsAOLProxy( $ip )
75 ) {
76 $trusted = true;
77 } else {
78 $trusted = false;
79 }
80 wfRunHooks( 'IsTrustedProxy', array( &$ip, &$trusted ) );
81 return $trusted;
82 }
83
84 /**
85 * Forks processes to scan the originating IP for an open proxy server
86 * MemCached can be used to skip IPs that have already been scanned
87 */
88 function wfProxyCheck() {
89 global $wgBlockOpenProxies, $wgProxyPorts, $wgProxyScriptPath;
90 global $wgUseMemCached, $wgMemc, $wgProxyMemcExpiry;
91 global $wgProxyKey;
92
93 if ( !$wgBlockOpenProxies ) {
94 return;
95 }
96
97 $ip = wfGetIP();
98
99 # Get MemCached key
100 $skip = false;
101 if ( $wgUseMemCached ) {
102 $mcKey = wfMemcKey( 'proxy', 'ip', $ip );
103 $mcValue = $wgMemc->get( $mcKey );
104 if ( $mcValue ) {
105 $skip = true;
106 }
107 }
108
109 # Fork the processes
110 if ( !$skip ) {
111 $title = SpecialPage::getTitleFor( 'Blockme' );
112 $iphash = md5( $ip . $wgProxyKey );
113 $url = $title->getFullURL( 'ip='.$iphash );
114
115 foreach ( $wgProxyPorts as $port ) {
116 $params = implode( ' ', array(
117 escapeshellarg( $wgProxyScriptPath ),
118 escapeshellarg( $ip ),
119 escapeshellarg( $port ),
120 escapeshellarg( $url )
121 ));
122 exec( "php $params &>/dev/null &" );
123 }
124 # Set MemCached key
125 if ( $wgUseMemCached ) {
126 $wgMemc->set( $mcKey, 1, $wgProxyMemcExpiry );
127 }
128 }
129 }
130
131 /**
132 * Convert a network specification in CIDR notation to an integer network and a number of bits
133 */
134 function wfParseCIDR( $range ) {
135 return IP::parseCIDR( $range );
136 }
137
138 /**
139 * Check if an IP address is in the local proxy list
140 */
141 function wfIsLocallyBlockedProxy( $ip ) {
142 global $wgProxyList;
143 $fname = 'wfIsLocallyBlockedProxy';
144
145 if ( !$wgProxyList ) {
146 return false;
147 }
148 wfProfileIn( $fname );
149
150 if ( !is_array( $wgProxyList ) ) {
151 # Load from the specified file
152 $wgProxyList = array_map( 'trim', file( $wgProxyList ) );
153 }
154
155 if ( !is_array( $wgProxyList ) ) {
156 $ret = false;
157 } elseif ( array_search( $ip, $wgProxyList ) !== false ) {
158 $ret = true;
159 } elseif ( array_key_exists( $ip, $wgProxyList ) ) {
160 # Old-style flipped proxy list
161 $ret = true;
162 } else {
163 $ret = false;
164 }
165 wfProfileOut( $fname );
166 return $ret;
167 }
168
169 /**
170 * TODO: move this list to the database in a global IP info table incorporating
171 * trusted ISP proxies, blocked IP addresses and open proxies.
172 */
173 function wfIsAOLProxy( $ip ) {
174 $ranges = array(
175 '64.12.96.0/19',
176 '149.174.160.0/20',
177 '152.163.240.0/21',
178 '152.163.248.0/22',
179 '152.163.252.0/23',
180 '152.163.96.0/22',
181 '152.163.100.0/23',
182 '195.93.32.0/22',
183 '195.93.48.0/22',
184 '195.93.64.0/19',
185 '195.93.96.0/19',
186 '195.93.16.0/20',
187 '198.81.0.0/22',
188 '198.81.16.0/20',
189 '198.81.8.0/23',
190 '202.67.64.128/25',
191 '205.188.192.0/20',
192 '205.188.208.0/23',
193 '205.188.112.0/20',
194 '205.188.146.144/30',
195 '207.200.112.0/21',
196 );
197
198 static $parsedRanges;
199 if ( is_null( $parsedRanges ) ) {
200 $parsedRanges = array();
201 foreach ( $ranges as $range ) {
202 $parsedRanges[] = IP::parseRange( $range );
203 }
204 }
205
206 $hex = IP::toHex( $ip );
207 foreach ( $parsedRanges as $range ) {
208 if ( $hex >= $range[0] && $hex <= $range[1] ) {
209 return true;
210 }
211 }
212 return false;
213 }
214
215
216
217 ?>