3 * Blocks and bans object
10 define ( 'EB_KEEP_EXPIRED', 1 );
11 define ( 'EB_FOR_UPDATE', 2 );
12 define ( 'EB_RANGE_ONLY', 4 );
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.
19 * To use delete(), you only need to fill $mAddress
20 * Globals used: $wgAutoblockExpiry, $wgAntiLockFlags
22 * @todo This could be used everywhere, but it isn't.
27 /* public*/ var $mAddress, $mUser, $mBy, $mReason, $mTimestamp, $mAuto, $mId, $mExpiry,
28 $mRangeStart, $mRangeEnd;
29 /* private */ var $mNetworkBits, $mIntegerAddr, $mForUpdate, $mFromMaster, $mByName;
31 function Block( $address = '', $user = '', $by = 0, $reason = '',
32 $timestamp = '' , $auto = 0, $expiry = '' )
34 $this->mAddress
= $address;
37 $this->mReason
= $reason;
38 $this->mTimestamp
= wfTimestamp(TS_MW
,$timestamp);
40 if( empty( $expiry ) ) {
41 $this->mExpiry
= $expiry;
43 $this->mExpiry
= wfTimestamp( TS_MW
, $expiry );
46 $this->mForUpdate
= false;
47 $this->mFromMaster
= false;
48 $this->mByName
= false;
49 $this->initialiseRange();
52 /*static*/ function newFromDB( $address, $user = 0, $killExpired = true )
55 $ban->load( $address, $user, $killExpired );
61 $this->mAddress
= $this->mReason
= $this->mTimestamp
= '';
62 $this->mUser
= $this->mBy
= 0;
63 $this->mByName
= false;
68 * Get the DB object and set the reference parameter to the query options
70 function &getDBOptions( &$options )
72 global $wgAntiLockFlags;
73 if ( $this->mForUpdate ||
$this->mFromMaster
) {
74 $db =& wfGetDB( DB_MASTER
);
75 if ( !$this->mForUpdate ||
($wgAntiLockFlags & ALF_NO_BLOCK_LOCK
) ) {
78 $options = 'FOR UPDATE';
81 $db =& wfGetDB( DB_SLAVE
);
88 * Get a ban from the DB, with either the given address or the given username
90 function load( $address = '', $user = 0, $killExpired = true )
92 $fname = 'Block::load';
93 wfDebug( "Block::load: '$address', '$user', $killExpired\n" );
96 $db =& $this->getDBOptions( $options );
100 $ipblocks = $db->tableName( 'ipblocks' );
102 if ( 0 == $user && $address == '' ) {
103 # Invalid user specification, not blocked
106 } elseif ( $address == '' ) {
107 $sql = "SELECT * FROM $ipblocks WHERE ipb_user={$user} $options";
108 } elseif ( $user == '' ) {
109 $sql = "SELECT * FROM $ipblocks WHERE ipb_address=" . $db->addQuotes( $address ) . " $options";
110 } elseif ( $options == '' ) {
111 # If there are no options (e.g. FOR UPDATE), use a UNION
112 # so that the query can make efficient use of indices
113 $sql = "SELECT * FROM $ipblocks WHERE ipb_address='" . $db->strencode( $address ) .
114 "' UNION SELECT * FROM $ipblocks WHERE ipb_user={$user}";
116 # If there are options, a UNION can not be used, use one
117 # SELECT instead. Will do a full table scan.
118 $sql = "SELECT * FROM $ipblocks WHERE (ipb_address='" . $db->strencode( $address ) .
119 "' OR ipb_user={$user}) $options";
122 $res = $db->query( $sql, $fname );
123 if ( 0 != $db->numRows( $res ) ) {
125 $row = $db->fetchObject( $res );
126 $this->initFromRow( $row );
128 if ( $killExpired ) {
129 # If requested, delete expired rows
131 $killed = $this->deleteIfExpired();
133 $row = $db->fetchObject( $res );
135 $this->initFromRow( $row );
138 } while ( $killed && $row );
140 # If there were any left after the killing finished, return true
151 $db->freeResult( $res );
153 # No blocks found yet? Try looking for range blocks
154 if ( !$ret && $address != '' ) {
155 $ret = $this->loadRange( $address, $killExpired );
165 * Search the database for any range blocks matching the given address, and
166 * load the row if one is found.
168 function loadRange( $address, $killExpired = true )
170 $fname = 'Block::loadRange';
172 $iaddr = wfIP2Hex( $address );
173 if ( $iaddr === false ) {
178 # Only scan ranges which start in this /16, this improves search speed
179 # Blocks should not cross a /16 boundary.
180 $range = substr( $iaddr, 0, 4 );
183 $db =& $this->getDBOptions( $options );
184 $ipblocks = $db->tableName( 'ipblocks' );
185 $sql = "SELECT * FROM $ipblocks WHERE ipb_range_start LIKE '$range%' ".
186 "AND ipb_range_start <= '$iaddr' AND ipb_range_end >= '$iaddr' $options";
187 $res = $db->query( $sql, $fname );
188 $row = $db->fetchObject( $res );
192 # Found a row, initialise this object
193 $this->initFromRow( $row );
196 if ( !$killExpired ||
!$this->deleteIfExpired() ) {
202 $db->freeResult( $res );
207 * Determine if a given integer IPv4 address is in a given CIDR network
209 function isAddressInRange( $addr, $range ) {
210 list( $network, $bits ) = wfParseCIDR( $range );
211 if ( $network !== false && $addr >> ( 32 - $bits ) == $network >> ( 32 - $bits ) ) {
218 function initFromRow( $row )
220 $this->mAddress
= $row->ipb_address
;
221 $this->mReason
= $row->ipb_reason
;
222 $this->mTimestamp
= wfTimestamp(TS_MW
,$row->ipb_timestamp
);
223 $this->mUser
= $row->ipb_user
;
224 $this->mBy
= $row->ipb_by
;
225 $this->mAuto
= $row->ipb_auto
;
226 $this->mId
= $row->ipb_id
;
227 $this->mExpiry
= $row->ipb_expiry ?
228 wfTimestamp(TS_MW
,$row->ipb_expiry
) :
230 if ( isset( $row->user_name
) ) {
231 $this->mByName
= $row->user_name
;
233 $this->mByName
= false;
235 $this->mRangeStart
= $row->ipb_range_start
;
236 $this->mRangeEnd
= $row->ipb_range_end
;
239 function initialiseRange()
241 $this->mRangeStart
= '';
242 $this->mRangeEnd
= '';
243 if ( $this->mUser
== 0 ) {
244 list( $network, $bits ) = wfParseCIDR( $this->mAddress
);
245 if ( $network !== false ) {
246 $this->mRangeStart
= sprintf( '%08X', $network );
247 $this->mRangeEnd
= sprintf( '%08X', $network +
(1 << (32 - $bits)) - 1 );
253 * Callback with a Block object for every block
254 * @return integer number of blocks;
256 /*static*/ function enumBlocks( $callback, $tag, $flags = 0 )
258 global $wgAntiLockFlags;
260 $block = new Block();
261 if ( $flags & EB_FOR_UPDATE
) {
262 $db =& wfGetDB( DB_MASTER
);
263 if ( $wgAntiLockFlags & ALF_NO_BLOCK_LOCK
) {
266 $options = 'FOR UPDATE';
268 $block->forUpdate( true );
270 $db =& wfGetDB( DB_SLAVE
);
273 if ( $flags & EB_RANGE_ONLY
) {
274 $cond = " AND ipb_range_start <> ''";
279 $now = wfTimestampNow();
281 extract( $db->tableNames( 'ipblocks', 'user' ) );
283 $sql = "SELECT $ipblocks.*,user_name FROM $ipblocks,$user " .
284 "WHERE user_id=ipb_by $cond ORDER BY ipb_timestamp DESC $options";
285 $res = $db->query( $sql, 'Block::enumBlocks' );
286 $num_rows = $db->numRows( $res );
288 while ( $row = $db->fetchObject( $res ) ) {
289 $block->initFromRow( $row );
290 if ( ( $flags & EB_RANGE_ONLY
) && $block->mRangeStart
== '' ) {
294 if ( !( $flags & EB_KEEP_EXPIRED
) ) {
295 if ( $block->mExpiry
&& $now > $block->mExpiry
) {
298 call_user_func( $callback, $block, $tag );
301 call_user_func( $callback, $block, $tag );
304 wfFreeResult( $res );
310 $fname = 'Block::delete';
314 $dbw =& wfGetDB( DB_MASTER
);
316 if ( $this->mAddress
== '' ) {
317 $condition = array( 'ipb_id' => $this->mId
);
319 $condition = array( 'ipb_address' => $this->mAddress
);
321 return( $dbw->delete( 'ipblocks', $condition, $fname ) > 0 ?
true : false );
326 wfDebug( "Block::insert; timestamp {$this->mTimestamp}\n" );
327 $dbw =& wfGetDB( DB_MASTER
);
328 $ipb_id = $dbw->nextSequenceValue('ipblocks_ipb_id_val');
329 $dbw->insert( 'ipblocks',
332 'ipb_address' => $this->mAddress
,
333 'ipb_user' => $this->mUser
,
334 'ipb_by' => $this->mBy
,
335 'ipb_reason' => $this->mReason
,
336 'ipb_timestamp' => $dbw->timestamp($this->mTimestamp
),
337 'ipb_auto' => $this->mAuto
,
338 'ipb_expiry' => $this->mExpiry ?
339 $dbw->timestamp($this->mExpiry
) :
341 'ipb_range_start' => $this->mRangeStart
,
342 'ipb_range_end' => $this->mRangeEnd
,
347 function deleteIfExpired()
349 $fname = 'Block::deleteIfExpired';
350 wfProfileIn( $fname );
351 if ( $this->isExpired() ) {
352 wfDebug( "Block::deleteIfExpired() -- deleting\n" );
356 wfDebug( "Block::deleteIfExpired() -- not expired\n" );
359 wfProfileOut( $fname );
365 wfDebug( "Block::isExpired() checking current " . wfTimestampNow() . " vs $this->mExpiry\n" );
366 if ( !$this->mExpiry
) {
369 return wfTimestampNow() > $this->mExpiry
;
375 return $this->mAddress
!= '';
378 function updateTimestamp()
380 if ( $this->mAuto
) {
381 $this->mTimestamp
= wfTimestamp();
382 $this->mExpiry
= Block
::getAutoblockExpiry( $this->mTimestamp
);
384 $dbw =& wfGetDB( DB_MASTER
);
385 $dbw->update( 'ipblocks',
387 'ipb_timestamp' => $dbw->timestamp($this->mTimestamp
),
388 'ipb_expiry' => $dbw->timestamp($this->mExpiry
),
389 ), array( /* WHERE */
390 'ipb_address' => $this->mAddress
391 ), 'Block::updateTimestamp'
397 function getIntegerAddr()
399 return $this->mIntegerAddr;
402 function getNetworkBits()
404 return $this->mNetworkBits;
409 if ( $this->mByName
=== false ) {
410 $this->mByName
= User
::whoIs( $this->mBy
);
412 return $this->mByName
;
415 function forUpdate( $x = NULL ) {
416 return wfSetVar( $this->mForUpdate
, $x );
419 function fromMaster( $x = NULL ) {
420 return wfSetVar( $this->mFromMaster
, $x );
423 /* static */ function getAutoblockExpiry( $timestamp )
425 global $wgAutoblockExpiry;
426 return wfTimestamp( TS_MW
, wfTimestamp( TS_UNIX
, $timestamp ) +
$wgAutoblockExpiry );
429 /* static */ function normaliseRange( $range )
431 $parts = explode( '/', $range );
432 if ( count( $parts ) == 2 ) {
433 $shift = 32 - $parts[1];
434 $ipint = wfIP2Unsigned( $parts[0] );
435 $ipint = $ipint >> $shift << $shift;
436 $newip = long2ip( $ipint );
437 $range = "$newip/{$parts[1]}";