0a5f044374ee03d919ada92b1e84cd2af08451c8
[lhc/web/wiklou.git] / includes / Block.php
1 <?php
2 /**
3 * Blocks and bans object
4 * @package MediaWiki
5 */
6
7 /**
8 * Some globals
9 */
10 define ( 'EB_KEEP_EXPIRED', 1 );
11 define ( 'EB_FOR_UPDATE', 2 );
12 define ( 'EB_RANGE_ONLY', 4 );
13
14 /**
15 * The block class
16 * All the functions in this class assume the object is either explicitly
17 * loaded or filled. It is not load-on-demand. There are no accessors.
18 *
19 * To use delete(), you only need to fill $mAddress
20 * Globals used: $wgAutoblockExpiry, $wgAntiLockFlags
21 *
22 * @todo This could be used everywhere, but it isn't.
23 * @package MediaWiki
24 */
25 class Block
26 {
27 public
28 $mAddress,
29 $mAuto,
30 $mBy,
31 $mExpiry,
32 $mId,
33 $mRangeEnd,
34 $mRangeStart,
35 $mReason,
36 $mTimestamp,
37 $mUser ;
38
39 private
40 $mByName,
41 $mForUpdate,
42 $mFromMaster,
43 $mIntegerAddr,
44 $mNetworkBits ;
45
46 function Block( $address = '', $user = '', $by = 0, $reason = '',
47 $timestamp = '' , $auto = 0, $expiry = '' )
48 {
49 $this->mAddress = $address;
50 $this->mUser = $user;
51 $this->mBy = $by;
52 $this->mReason = $reason;
53 $this->mTimestamp = wfTimestamp(TS_MW,$timestamp);
54 $this->mAuto = $auto;
55 if( empty( $expiry ) ) {
56 $this->mExpiry = $expiry;
57 } else {
58 $this->mExpiry = wfTimestamp( TS_MW, $expiry );
59 }
60
61 $this->mForUpdate = false;
62 $this->mFromMaster = false;
63 $this->mByName = false;
64 $this->initialiseRange();
65 }
66
67 /*static*/ function newFromDB( $address, $user = 0, $killExpired = true )
68 {
69 $ban = new Block();
70 $ban->load( $address, $user, $killExpired );
71 return $ban;
72 }
73
74 function clear()
75 {
76 $this->mAddress = $this->mReason = $this->mTimestamp = '';
77 $this->mUser = $this->mBy = 0;
78 $this->mByName = false;
79
80 }
81
82 /**
83 * Get the DB object and set the reference parameter to the query options
84 */
85 function &getDBOptions( &$options )
86 {
87 global $wgAntiLockFlags;
88 if ( $this->mForUpdate || $this->mFromMaster ) {
89 $db =& wfGetDB( DB_MASTER );
90 if ( !$this->mForUpdate || ($wgAntiLockFlags & ALF_NO_BLOCK_LOCK) ) {
91 $options = '';
92 } else {
93 $options = 'FOR UPDATE';
94 }
95 } else {
96 $db =& wfGetDB( DB_SLAVE );
97 $options = '';
98 }
99 return $db;
100 }
101
102 /**
103 * Get a ban from the DB, with either the given address or the given username
104 */
105 function load( $address = '', $user = 0, $killExpired = true )
106 {
107 $fname = 'Block::load';
108 wfDebug( "Block::load: '$address', '$user', $killExpired\n" );
109
110 $options = '';
111 $db =& $this->getDBOptions( $options );
112
113 $ret = false;
114 $killed = false;
115 $ipblocks = $db->tableName( 'ipblocks' );
116
117 if ( 0 == $user && $address == '' ) {
118 # Invalid user specification, not blocked
119 $this->clear();
120 return false;
121 } elseif ( $address == '' ) {
122 $sql = "SELECT * FROM $ipblocks WHERE ipb_user={$user} $options";
123 } elseif ( $user == '' ) {
124 $sql = "SELECT * FROM $ipblocks WHERE ipb_address=" . $db->addQuotes( $address ) . " $options";
125 } elseif ( $options == '' ) {
126 # If there are no options (e.g. FOR UPDATE), use a UNION
127 # so that the query can make efficient use of indices
128 $sql = "SELECT * FROM $ipblocks WHERE ipb_address='" . $db->strencode( $address ) .
129 "' UNION SELECT * FROM $ipblocks WHERE ipb_user={$user}";
130 } else {
131 # If there are options, a UNION can not be used, use one
132 # SELECT instead. Will do a full table scan.
133 $sql = "SELECT * FROM $ipblocks WHERE (ipb_address='" . $db->strencode( $address ) .
134 "' OR ipb_user={$user}) $options";
135 }
136
137 $res = $db->query( $sql, $fname );
138 if ( 0 != $db->numRows( $res ) ) {
139 # Get first block
140 $row = $db->fetchObject( $res );
141 $this->initFromRow( $row );
142
143 if ( $killExpired ) {
144 # If requested, delete expired rows
145 do {
146 $killed = $this->deleteIfExpired();
147 if ( $killed ) {
148 $row = $db->fetchObject( $res );
149 if ( $row ) {
150 $this->initFromRow( $row );
151 }
152 }
153 } while ( $killed && $row );
154
155 # If there were any left after the killing finished, return true
156 if ( !$row ) {
157 $ret = false;
158 $this->clear();
159 } else {
160 $ret = true;
161 }
162 } else {
163 $ret = true;
164 }
165 }
166 $db->freeResult( $res );
167
168 # No blocks found yet? Try looking for range blocks
169 if ( !$ret && $address != '' ) {
170 $ret = $this->loadRange( $address, $killExpired );
171 }
172 if ( !$ret ) {
173 $this->clear();
174 }
175
176 return $ret;
177 }
178
179 /**
180 * Search the database for any range blocks matching the given address, and
181 * load the row if one is found.
182 */
183 function loadRange( $address, $killExpired = true )
184 {
185 $fname = 'Block::loadRange';
186
187 $iaddr = wfIP2Hex( $address );
188 if ( $iaddr === false ) {
189 # Invalid address
190 return false;
191 }
192
193 # Only scan ranges which start in this /16, this improves search speed
194 # Blocks should not cross a /16 boundary.
195 $range = substr( $iaddr, 0, 4 );
196
197 $options = '';
198 $db =& $this->getDBOptions( $options );
199 $ipblocks = $db->tableName( 'ipblocks' );
200 $sql = "SELECT * FROM $ipblocks WHERE ipb_range_start LIKE '$range%' ".
201 "AND ipb_range_start <= '$iaddr' AND ipb_range_end >= '$iaddr' $options";
202 $res = $db->query( $sql, $fname );
203 $row = $db->fetchObject( $res );
204
205 $success = false;
206 if ( $row ) {
207 # Found a row, initialise this object
208 $this->initFromRow( $row );
209
210 # Is it expired?
211 if ( !$killExpired || !$this->deleteIfExpired() ) {
212 # No, return true
213 $success = true;
214 }
215 }
216
217 $db->freeResult( $res );
218 return $success;
219 }
220
221 /**
222 * Determine if a given integer IPv4 address is in a given CIDR network
223 */
224 function isAddressInRange( $addr, $range ) {
225 list( $network, $bits ) = wfParseCIDR( $range );
226 if ( $network !== false && $addr >> ( 32 - $bits ) == $network >> ( 32 - $bits ) ) {
227 return true;
228 } else {
229 return false;
230 }
231 }
232
233 function initFromRow( $row )
234 {
235 $this->mAddress = $row->ipb_address;
236 $this->mReason = $row->ipb_reason;
237 $this->mTimestamp = wfTimestamp(TS_MW,$row->ipb_timestamp);
238 $this->mUser = $row->ipb_user;
239 $this->mBy = $row->ipb_by;
240 $this->mAuto = $row->ipb_auto;
241 $this->mId = $row->ipb_id;
242 $this->mExpiry = $row->ipb_expiry ?
243 wfTimestamp(TS_MW,$row->ipb_expiry) :
244 $row->ipb_expiry;
245 if ( isset( $row->user_name ) ) {
246 $this->mByName = $row->user_name;
247 } else {
248 $this->mByName = false;
249 }
250 $this->mRangeStart = $row->ipb_range_start;
251 $this->mRangeEnd = $row->ipb_range_end;
252 }
253
254 function initialiseRange()
255 {
256 $this->mRangeStart = '';
257 $this->mRangeEnd = '';
258 if ( $this->mUser == 0 ) {
259 list( $network, $bits ) = wfParseCIDR( $this->mAddress );
260 if ( $network !== false ) {
261 $this->mRangeStart = sprintf( '%08X', $network );
262 $this->mRangeEnd = sprintf( '%08X', $network + (1 << (32 - $bits)) - 1 );
263 }
264 }
265 }
266
267 /**
268 * Callback with a Block object for every block
269 * @return integer number of blocks;
270 */
271 /*static*/ function enumBlocks( $callback, $tag, $flags = 0 )
272 {
273 global $wgAntiLockFlags;
274
275 $block = new Block();
276 if ( $flags & EB_FOR_UPDATE ) {
277 $db =& wfGetDB( DB_MASTER );
278 if ( $wgAntiLockFlags & ALF_NO_BLOCK_LOCK ) {
279 $options = '';
280 } else {
281 $options = 'FOR UPDATE';
282 }
283 $block->forUpdate( true );
284 } else {
285 $db =& wfGetDB( DB_SLAVE );
286 $options = '';
287 }
288 if ( $flags & EB_RANGE_ONLY ) {
289 $cond = " AND ipb_range_start <> ''";
290 } else {
291 $cond = '';
292 }
293
294 $now = wfTimestampNow();
295
296 extract( $db->tableNames( 'ipblocks', 'user' ) );
297
298 $sql = "SELECT $ipblocks.*,user_name FROM $ipblocks,$user " .
299 "WHERE user_id=ipb_by $cond ORDER BY ipb_timestamp DESC $options";
300 $res = $db->query( $sql, 'Block::enumBlocks' );
301 $num_rows = $db->numRows( $res );
302
303 while ( $row = $db->fetchObject( $res ) ) {
304 $block->initFromRow( $row );
305 if ( ( $flags & EB_RANGE_ONLY ) && $block->mRangeStart == '' ) {
306 continue;
307 }
308
309 if ( !( $flags & EB_KEEP_EXPIRED ) ) {
310 if ( $block->mExpiry && $now > $block->mExpiry ) {
311 $block->delete();
312 } else {
313 call_user_func( $callback, $block, $tag );
314 }
315 } else {
316 call_user_func( $callback, $block, $tag );
317 }
318 }
319 wfFreeResult( $res );
320 return $num_rows;
321 }
322
323 function delete()
324 {
325 $fname = 'Block::delete';
326 if (wfReadOnly()) {
327 return;
328 }
329 $dbw =& wfGetDB( DB_MASTER );
330
331 if ( $this->mAddress == '' ) {
332 $condition = array( 'ipb_id' => $this->mId );
333 } else {
334 $condition = array( 'ipb_address' => $this->mAddress );
335 }
336 return( $dbw->delete( 'ipblocks', $condition, $fname ) > 0 ? true : false );
337 }
338
339 function insert()
340 {
341 wfDebug( "Block::insert; timestamp {$this->mTimestamp}\n" );
342 $dbw =& wfGetDB( DB_MASTER );
343 $ipb_id = $dbw->nextSequenceValue('ipblocks_ipb_id_val');
344 $dbw->insert( 'ipblocks',
345 array(
346 'ipb_id' => $ipb_id,
347 'ipb_address' => $this->mAddress,
348 'ipb_user' => $this->mUser,
349 'ipb_by' => $this->mBy,
350 'ipb_reason' => $this->mReason,
351 'ipb_timestamp' => $dbw->timestamp($this->mTimestamp),
352 'ipb_auto' => $this->mAuto,
353 'ipb_expiry' => $this->mExpiry ?
354 $dbw->timestamp($this->mExpiry) :
355 $this->mExpiry,
356 'ipb_range_start' => $this->mRangeStart,
357 'ipb_range_end' => $this->mRangeEnd,
358 ), 'Block::insert'
359 );
360 }
361
362 function deleteIfExpired()
363 {
364 $fname = 'Block::deleteIfExpired';
365 wfProfileIn( $fname );
366 if ( $this->isExpired() ) {
367 wfDebug( "Block::deleteIfExpired() -- deleting\n" );
368 $this->delete();
369 $retVal = true;
370 } else {
371 wfDebug( "Block::deleteIfExpired() -- not expired\n" );
372 $retVal = false;
373 }
374 wfProfileOut( $fname );
375 return $retVal;
376 }
377
378 function isExpired()
379 {
380 wfDebug( "Block::isExpired() checking current " . wfTimestampNow() . " vs $this->mExpiry\n" );
381 if ( !$this->mExpiry ) {
382 return false;
383 } else {
384 return wfTimestampNow() > $this->mExpiry;
385 }
386 }
387
388 function isValid()
389 {
390 return $this->mAddress != '';
391 }
392
393 function updateTimestamp()
394 {
395 if ( $this->mAuto ) {
396 $this->mTimestamp = wfTimestamp();
397 $this->mExpiry = Block::getAutoblockExpiry( $this->mTimestamp );
398
399 $dbw =& wfGetDB( DB_MASTER );
400 $dbw->update( 'ipblocks',
401 array( /* SET */
402 'ipb_timestamp' => $dbw->timestamp($this->mTimestamp),
403 'ipb_expiry' => $dbw->timestamp($this->mExpiry),
404 ), array( /* WHERE */
405 'ipb_address' => $this->mAddress
406 ), 'Block::updateTimestamp'
407 );
408 }
409 }
410
411 /*
412 function getIntegerAddr()
413 {
414 return $this->mIntegerAddr;
415 }
416
417 function getNetworkBits()
418 {
419 return $this->mNetworkBits;
420 }*/
421
422 function getByName()
423 {
424 if ( $this->mByName === false ) {
425 $this->mByName = User::whoIs( $this->mBy );
426 }
427 return $this->mByName;
428 }
429
430 function forUpdate( $x = NULL ) {
431 return wfSetVar( $this->mForUpdate, $x );
432 }
433
434 function fromMaster( $x = NULL ) {
435 return wfSetVar( $this->mFromMaster, $x );
436 }
437
438 /* static */ function getAutoblockExpiry( $timestamp )
439 {
440 global $wgAutoblockExpiry;
441 return wfTimestamp( TS_MW, wfTimestamp( TS_UNIX, $timestamp ) + $wgAutoblockExpiry );
442 }
443
444 /* static */ function normaliseRange( $range )
445 {
446 $parts = explode( '/', $range );
447 if ( count( $parts ) == 2 ) {
448 $shift = 32 - $parts[1];
449 $ipint = wfIP2Unsigned( $parts[0] );
450 $ipint = $ipint >> $shift << $shift;
451 $newip = long2ip( $ipint );
452 $range = "$newip/{$parts[1]}";
453 }
454 return $range;
455 }
456
457 }
458 ?>