0a5f044374ee03d919ada92b1e84cd2af08451c8
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.
46 function Block( $address = '', $user = '', $by = 0, $reason = '',
47 $timestamp = '' , $auto = 0, $expiry = '' )
49 $this->mAddress
= $address;
52 $this->mReason
= $reason;
53 $this->mTimestamp
= wfTimestamp(TS_MW
,$timestamp);
55 if( empty( $expiry ) ) {
56 $this->mExpiry
= $expiry;
58 $this->mExpiry
= wfTimestamp( TS_MW
, $expiry );
61 $this->mForUpdate
= false;
62 $this->mFromMaster
= false;
63 $this->mByName
= false;
64 $this->initialiseRange();
67 /*static*/ function newFromDB( $address, $user = 0, $killExpired = true )
70 $ban->load( $address, $user, $killExpired );
76 $this->mAddress
= $this->mReason
= $this->mTimestamp
= '';
77 $this->mUser
= $this->mBy
= 0;
78 $this->mByName
= false;
83 * Get the DB object and set the reference parameter to the query options
85 function &getDBOptions( &$options )
87 global $wgAntiLockFlags;
88 if ( $this->mForUpdate ||
$this->mFromMaster
) {
89 $db =& wfGetDB( DB_MASTER
);
90 if ( !$this->mForUpdate ||
($wgAntiLockFlags & ALF_NO_BLOCK_LOCK
) ) {
93 $options = 'FOR UPDATE';
96 $db =& wfGetDB( DB_SLAVE
);
103 * Get a ban from the DB, with either the given address or the given username
105 function load( $address = '', $user = 0, $killExpired = true )
107 $fname = 'Block::load';
108 wfDebug( "Block::load: '$address', '$user', $killExpired\n" );
111 $db =& $this->getDBOptions( $options );
115 $ipblocks = $db->tableName( 'ipblocks' );
117 if ( 0 == $user && $address == '' ) {
118 # Invalid user specification, not blocked
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}";
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";
137 $res = $db->query( $sql, $fname );
138 if ( 0 != $db->numRows( $res ) ) {
140 $row = $db->fetchObject( $res );
141 $this->initFromRow( $row );
143 if ( $killExpired ) {
144 # If requested, delete expired rows
146 $killed = $this->deleteIfExpired();
148 $row = $db->fetchObject( $res );
150 $this->initFromRow( $row );
153 } while ( $killed && $row );
155 # If there were any left after the killing finished, return true
166 $db->freeResult( $res );
168 # No blocks found yet? Try looking for range blocks
169 if ( !$ret && $address != '' ) {
170 $ret = $this->loadRange( $address, $killExpired );
180 * Search the database for any range blocks matching the given address, and
181 * load the row if one is found.
183 function loadRange( $address, $killExpired = true )
185 $fname = 'Block::loadRange';
187 $iaddr = wfIP2Hex( $address );
188 if ( $iaddr === false ) {
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 );
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 );
207 # Found a row, initialise this object
208 $this->initFromRow( $row );
211 if ( !$killExpired ||
!$this->deleteIfExpired() ) {
217 $db->freeResult( $res );
222 * Determine if a given integer IPv4 address is in a given CIDR network
224 function isAddressInRange( $addr, $range ) {
225 list( $network, $bits ) = wfParseCIDR( $range );
226 if ( $network !== false && $addr >> ( 32 - $bits ) == $network >> ( 32 - $bits ) ) {
233 function initFromRow( $row )
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
) :
245 if ( isset( $row->user_name
) ) {
246 $this->mByName
= $row->user_name
;
248 $this->mByName
= false;
250 $this->mRangeStart
= $row->ipb_range_start
;
251 $this->mRangeEnd
= $row->ipb_range_end
;
254 function initialiseRange()
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 );
268 * Callback with a Block object for every block
269 * @return integer number of blocks;
271 /*static*/ function enumBlocks( $callback, $tag, $flags = 0 )
273 global $wgAntiLockFlags;
275 $block = new Block();
276 if ( $flags & EB_FOR_UPDATE
) {
277 $db =& wfGetDB( DB_MASTER
);
278 if ( $wgAntiLockFlags & ALF_NO_BLOCK_LOCK
) {
281 $options = 'FOR UPDATE';
283 $block->forUpdate( true );
285 $db =& wfGetDB( DB_SLAVE
);
288 if ( $flags & EB_RANGE_ONLY
) {
289 $cond = " AND ipb_range_start <> ''";
294 $now = wfTimestampNow();
296 extract( $db->tableNames( 'ipblocks', 'user' ) );
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 );
303 while ( $row = $db->fetchObject( $res ) ) {
304 $block->initFromRow( $row );
305 if ( ( $flags & EB_RANGE_ONLY
) && $block->mRangeStart
== '' ) {
309 if ( !( $flags & EB_KEEP_EXPIRED
) ) {
310 if ( $block->mExpiry
&& $now > $block->mExpiry
) {
313 call_user_func( $callback, $block, $tag );
316 call_user_func( $callback, $block, $tag );
319 wfFreeResult( $res );
325 $fname = 'Block::delete';
329 $dbw =& wfGetDB( DB_MASTER
);
331 if ( $this->mAddress
== '' ) {
332 $condition = array( 'ipb_id' => $this->mId
);
334 $condition = array( 'ipb_address' => $this->mAddress
);
336 return( $dbw->delete( 'ipblocks', $condition, $fname ) > 0 ?
true : false );
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',
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
) :
356 'ipb_range_start' => $this->mRangeStart
,
357 'ipb_range_end' => $this->mRangeEnd
,
362 function deleteIfExpired()
364 $fname = 'Block::deleteIfExpired';
365 wfProfileIn( $fname );
366 if ( $this->isExpired() ) {
367 wfDebug( "Block::deleteIfExpired() -- deleting\n" );
371 wfDebug( "Block::deleteIfExpired() -- not expired\n" );
374 wfProfileOut( $fname );
380 wfDebug( "Block::isExpired() checking current " . wfTimestampNow() . " vs $this->mExpiry\n" );
381 if ( !$this->mExpiry
) {
384 return wfTimestampNow() > $this->mExpiry
;
390 return $this->mAddress
!= '';
393 function updateTimestamp()
395 if ( $this->mAuto
) {
396 $this->mTimestamp
= wfTimestamp();
397 $this->mExpiry
= Block
::getAutoblockExpiry( $this->mTimestamp
);
399 $dbw =& wfGetDB( DB_MASTER
);
400 $dbw->update( 'ipblocks',
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'
412 function getIntegerAddr()
414 return $this->mIntegerAddr;
417 function getNetworkBits()
419 return $this->mNetworkBits;
424 if ( $this->mByName
=== false ) {
425 $this->mByName
= User
::whoIs( $this->mBy
);
427 return $this->mByName
;
430 function forUpdate( $x = NULL ) {
431 return wfSetVar( $this->mForUpdate
, $x );
434 function fromMaster( $x = NULL ) {
435 return wfSetVar( $this->mFromMaster
, $x );
438 /* static */ function getAutoblockExpiry( $timestamp )
440 global $wgAutoblockExpiry;
441 return wfTimestamp( TS_MW
, wfTimestamp( TS_UNIX
, $timestamp ) +
$wgAutoblockExpiry );
444 /* static */ function normaliseRange( $range )
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]}";