Hooks! Hoooooks!
[lhc/web/wiklou.git] / includes / ProxyTools.php
1 <?php
2 /**
3 * Functions for dealing with proxies
4 * @package MediaWiki
5 */
6
7 if ( !defined( 'MEDIAWIKI' ) ) {
8 die();
9 }
10
11 /** Work out the IP address based on various globals */
12 function wfGetIP() {
13 global $wgSquidServers, $wgSquidServersNoPurge, $wgIP;
14
15 # Return cached result
16 if ( !empty( $wgIP ) ) {
17 return $wgIP;
18 }
19
20 /* collect the originating ips */
21 # Client connecting to this webserver
22 if ( isset( $_SERVER['REMOTE_ADDR'] ) ) {
23 $ipchain = array( $_SERVER['REMOTE_ADDR'] );
24 } else {
25 # Running on CLI?
26 $ipchain = array( '127.0.0.1' );
27 }
28 $ip = $ipchain[0];
29
30 # Get list of trusted proxies
31 # Flipped for quicker access
32 $trustedProxies = array_flip( array_merge( $wgSquidServers, $wgSquidServersNoPurge ) );
33 if ( count( $trustedProxies ) ) {
34 # Append XFF on to $ipchain
35 if ( isset( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) {
36 $xff = array_map( 'trim', explode( ',', $_SERVER['HTTP_X_FORWARDED_FOR'] ) );
37 $xff = array_reverse( $xff );
38 $ipchain = array_merge( $ipchain, $xff );
39 }
40 # Step through XFF list and find the last address in the list which is a trusted server
41 # Set $ip to the IP address given by that trusted server, unless the address is not sensible (e.g. private)
42 foreach ( $ipchain as $i => $curIP ) {
43 if ( array_key_exists( $curIP, $trustedProxies ) ) {
44 if ( isset( $ipchain[$i + 1] ) && wfIsIPPublic( $ipchain[$i + 1] ) ) {
45 $ip = $ipchain[$i + 1];
46 }
47 } else {
48 break;
49 }
50 }
51 }
52
53 wfDebug( "IP: $ip\n" );
54 $wgIP = $ip;
55 return $ip;
56 }
57
58 /**
59 * Given an IP address in dotted-quad notation, returns an unsigned integer.
60 * Like ip2long() except that it actually works and has a consistent error return value.
61 */
62 function wfIP2Unsigned( $ip ) {
63 $n = ip2long( $ip );
64 if ( $n == -1 || $n === false ) { # Return value on error depends on PHP version
65 $n = false;
66 } elseif ( $n < 0 ) {
67 $n += pow( 2, 32 );
68 }
69 return $n;
70 }
71
72 /**
73 * Return a zero-padded hexadecimal representation of an IP address
74 */
75 function wfIP2Hex( $ip ) {
76 $n = wfIP2Unsigned( $ip );
77 if ( $n !== false ) {
78 $n = sprintf( '%08X', $n );
79 }
80 return $n;
81 }
82
83 /**
84 * Determine if an IP address really is an IP address, and if it is public,
85 * i.e. not RFC 1918 or similar
86 */
87 function wfIsIPPublic( $ip ) {
88 $n = wfIP2Unsigned( $ip );
89 if ( !$n ) {
90 return false;
91 }
92
93 static $privateRanges = false;
94 if ( !$privateRanges ) {
95 $privateRanges = array(
96 array( '10.0.0.0', '10.255.255.255' ), # RFC 1918 (private)
97 array( '172.16.0.0', '172.31.255.255' ), # "
98 array( '192.168.0.0', '192.168.255.255' ), # "
99 array( '0.0.0.0', '0.255.255.255' ), # this network
100 array( '127.0.0.0', '127.255.255.255' ), # loopback
101 );
102 }
103
104 foreach ( $privateRanges as $r ) {
105 $start = wfIP2Unsigned( $r[0] );
106 $end = wfIP2Unsigned( $r[1] );
107 if ( $n >= $start && $n <= $end ) {
108 return false;
109 }
110 }
111 return true;
112 }
113
114 /**
115 * Forks processes to scan the originating IP for an open proxy server
116 * MemCached can be used to skip IPs that have already been scanned
117 */
118 function wfProxyCheck() {
119 global $wgBlockOpenProxies, $wgProxyPorts, $wgProxyScriptPath;
120 global $wgUseMemCached, $wgMemc, $wgDBname, $wgProxyMemcExpiry;
121
122 if ( !$wgBlockOpenProxies ) {
123 return;
124 }
125
126 $ip = wfGetIP();
127
128 # Get MemCached key
129 $skip = false;
130 if ( $wgUseMemCached ) {
131 $mcKey = "$wgDBname:proxy:ip:$ip";
132 $mcValue = $wgMemc->get( $mcKey );
133 if ( $mcValue ) {
134 $skip = true;
135 }
136 }
137
138 # Fork the processes
139 if ( !$skip ) {
140 $title = Title::makeTitle( NS_SPECIAL, 'Blockme' );
141 $iphash = md5( $ip . $wgProxyKey );
142 $url = $title->getFullURL( 'ip='.$iphash );
143
144 foreach ( $wgProxyPorts as $port ) {
145 $params = implode( ' ', array(
146 escapeshellarg( $wgProxyScriptPath ),
147 escapeshellarg( $ip ),
148 escapeshellarg( $port ),
149 escapeshellarg( $url )
150 ));
151 exec( "php $params &>/dev/null &" );
152 }
153 # Set MemCached key
154 if ( $wgUseMemCached ) {
155 $wgMemc->set( $mcKey, 1, $wgProxyMemcExpiry );
156 }
157 }
158 }
159
160 /**
161 * Convert a network specification in CIDR notation to an integer network and a number of bits
162 */
163 function wfParseCIDR( $range ) {
164 $parts = explode( '/', $range, 2 );
165 if ( count( $parts ) != 2 ) {
166 return array( false, false );
167 }
168 $network = wfIP2Unsigned( $parts[0] );
169 if ( $network !== false && is_numeric( $parts[1] ) && $parts[1] >= 0 && $parts[1] <= 32 ) {
170 $bits = $parts[1];
171 } else {
172 $network = false;
173 $bits = false;
174 }
175 return array( $network, $bits );
176 }
177
178 /**
179 * Check if an IP address is in the local proxy list
180 */
181 function wfIsLocallyBlockedProxy( $ip ) {
182 global $wgProxyList;
183 $fname = 'wfIsLocallyBlockedProxy';
184
185 if ( !$wgProxyList ) {
186 return false;
187 }
188 wfProfileIn( $fname );
189
190 if ( !is_array( $wgProxyList ) ) {
191 # Load from the specified file
192 $wgProxyList = array_map( 'trim', file( $wgProxyList ) );
193 }
194
195 if ( !is_array( $wgProxyList ) ) {
196 $ret = false;
197 } elseif ( array_search( $ip, $wgProxyList ) !== false ) {
198 $ret = true;
199 } elseif ( array_key_exists( $ip, $wgProxyList ) ) {
200 # Old-style flipped proxy list
201 $ret = true;
202 } else {
203 $ret = false;
204 }
205 wfProfileOut( $fname );
206 return $ret;
207 }
208
209
210
211
212 ?>