721b1f7fa3e31c02acbc2bf6575266c90d5fc54f
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: $wgBlockCache, $wgAutoblockExpiry
22 * @todo This could be used everywhere, but it isn't.
27 /* public*/ var $mAddress, $mUser, $mBy, $mReason, $mTimestamp, $mAuto, $mId, $mExpiry;
28 /* private */ var $mNetworkBits, $mIntegerAddr, $mForUpdate, $mByName;
30 function Block( $address = '', $user = '', $by = 0, $reason = '',
31 $timestamp = '' , $auto = 0, $expiry = '' )
33 $this->mAddress
= $address;
36 $this->mReason
= $reason;
37 $this->mTimestamp
= wfTimestamp(TS_MW
,$timestamp);
39 if( empty( $expiry ) ) {
40 $this->mExpiry
= $expiry;
42 $this->mExpiry
= wfTimestamp( TS_MW
, $expiry );
45 $this->mForUpdate
= false;
46 $this->mByName
= false;
47 $this->initialiseRange();
50 /*static*/ function newFromDB( $address, $user = 0, $killExpired = true )
53 $ban->load( $address, $user, $killExpired );
59 $this->mAddress
= $this->mReason
= $this->mTimestamp
= '';
60 $this->mUser
= $this->mBy
= 0;
61 $this->mByName
= false;
66 * Get a ban from the DB, with either the given address or the given username
68 function load( $address = '', $user = 0, $killExpired = true )
70 global $wgDBmysql4, $wgAntiLockFlags;
71 $fname = 'Block::load';
72 wfDebug( "Block::load: '$address', '$user', $killExpired\n" );
76 if ( $this->forUpdate() ) {
77 $db =& wfGetDB( DB_MASTER
);
78 if ( $wgAntiLockFlags & ALF_NO_BLOCK_LOCK
) {
81 $options = 'FOR UPDATE';
84 $db =& wfGetDB( DB_SLAVE
);
87 $ipblocks = $db->tableName( 'ipblocks' );
89 if ( 0 == $user && $address=='' ) {
90 $sql = "SELECT * from $ipblocks $options";
91 } elseif ($address=="") {
92 $sql = "SELECT * FROM $ipblocks WHERE ipb_user={$user} $options";
93 } elseif ($user=="") {
94 $sql = "SELECT * FROM $ipblocks WHERE ipb_address='" . $db->strencode( $address ) . "' $options";
95 } elseif ( $options=='' && $wgDBmysql4 ) {
96 # If there are no optiones (e.g. FOR UPDATE), use a UNION
97 # so that the query can make efficient use of indices
98 $sql = "SELECT * FROM $ipblocks WHERE ipb_address='" . $db->strencode( $address ) .
99 "' UNION SELECT * FROM $ipblocks WHERE ipb_user={$user}";
101 # If there are options, a UNION can not be used, use one
102 # SELECT instead. Will do a full table scan.
103 $sql = "SELECT * FROM $ipblocks WHERE (ipb_address='" . $db->strencode( $address ) .
104 "' OR ipb_user={$user}) $options";
107 $res = $db->query( $sql, $fname );
108 if ( 0 == $db->numRows( $res ) ) {
109 # User is not blocked
113 $row = $db->fetchObject( $res );
114 $this->initFromRow( $row );
116 if ( $killExpired ) {
117 # If requested, delete expired rows
119 $killed = $this->deleteIfExpired();
121 $row = $db->fetchObject( $res );
123 $this->initFromRow( $row );
126 } while ( $killed && $row );
128 # If there were any left after the killing finished, return true
139 $db->freeResult( $res );
143 function initFromRow( $row )
145 $this->mAddress
= $row->ipb_address
;
146 $this->mReason
= $row->ipb_reason
;
147 $this->mTimestamp
= wfTimestamp(TS_MW
,$row->ipb_timestamp
);
148 $this->mUser
= $row->ipb_user
;
149 $this->mBy
= $row->ipb_by
;
150 $this->mAuto
= $row->ipb_auto
;
151 $this->mId
= $row->ipb_id
;
152 $this->mExpiry
= $row->ipb_expiry ?
153 wfTimestamp(TS_MW
,$row->ipb_expiry
) :
155 if ( isset( $row->user_name
) ) {
156 $this->mByName
= $row->user_name
;
158 $this->mByName
= false;
161 $this->initialiseRange();
164 function initialiseRange()
166 if ( $this->mUser
== 0 ) {
167 $rangeParts = explode( '/', $this->mAddress
);
168 if ( count( $rangeParts ) == 2 ) {
169 $this->mNetworkBits
= $rangeParts[1];
171 $this->mNetworkBits
= 32;
173 $this->mIntegerAddr
= ip2long( $rangeParts[0] );
175 $this->mNetworkBits
= false;
176 $this->mIntegerAddr
= false;
181 * Callback with a Block object for every block
182 * @return integer number of blocks;
184 /*static*/ function enumBlocks( $callback, $tag, $flags = 0 )
186 global $wgAntiLockFlags;
188 $block = new Block();
189 if ( $flags & EB_FOR_UPDATE
) {
190 $db =& wfGetDB( DB_MASTER
);
191 if ( $wgAntiLockFlags & ALF_NO_BLOCK_LOCK
) {
194 $options = 'FOR UPDATE';
196 $block->forUpdate( true );
198 $db =& wfGetDB( DB_SLAVE
);
201 if ( $flags & EB_RANGE_ONLY
) {
202 $cond = " AND ipb_address LIKE '%/%'";
207 extract( $db->tableNames( 'ipblocks', 'user' ) );
209 $sql = "SELECT $ipblocks.*,user_name FROM $ipblocks,$user " .
210 "WHERE user_id=ipb_by $cond ORDER BY ipb_timestamp DESC $options";
211 $res = $db->query( $sql, 'Block::enumBans' );
212 $num_rows = $db->numRows( $res );
214 while ( $row = $db->fetchObject( $res ) ) {
215 $block->initFromRow( $row );
216 if ( ( $flags & EB_RANGE_ONLY
) && $block->getNetworkBits() == 32 ) {
220 if ( !( $flags & EB_KEEP_EXPIRED
) ) {
221 if ( !$block->deleteIfExpired() ) {
222 $callback( $block, $tag );
225 $callback( $block, $tag );
228 wfFreeResult( $res );
234 $fname = 'Block::delete';
238 $dbw =& wfGetDB( DB_MASTER
);
240 if ( $this->mAddress
== '' ) {
241 $condition = array( 'ipb_id' => $this->mId
);
243 $condition = array( 'ipb_address' => $this->mAddress
);
245 $dbw->delete( 'ipblocks', $condition, $fname );
251 wfDebug( "Block::insert; timestamp {$this->mTimestamp}\n" );
252 $dbw =& wfGetDB( DB_MASTER
);
253 $ipb_id = $dbw->nextSequenceValue('ipblocks_ipb_id_val');
254 $dbw->insert( 'ipblocks',
257 'ipb_address' => $this->mAddress
,
258 'ipb_user' => $this->mUser
,
259 'ipb_by' => $this->mBy
,
260 'ipb_reason' => $this->mReason
,
261 'ipb_timestamp' => $dbw->timestamp($this->mTimestamp
),
262 'ipb_auto' => $this->mAuto
,
263 'ipb_expiry' => $this->mExpiry ?
264 $dbw->timestamp($this->mExpiry
) :
272 function deleteIfExpired()
274 if ( $this->isExpired() ) {
275 wfDebug( "Block::deleteIfExpired() -- deleting\n" );
279 wfDebug( "Block::deleteIfExpired() -- not expired\n" );
286 wfDebug( "Block::isExpired() checking current " . wfTimestampNow() . " vs $this->mExpiry\n" );
287 if ( !$this->mExpiry
) {
290 return wfTimestampNow() > $this->mExpiry
;
296 return $this->mAddress
!= '';
299 function updateTimestamp()
301 if ( $this->mAuto
) {
302 $this->mTimestamp
= wfTimestamp();
303 $this->mExpiry
= Block
::getAutoblockExpiry( $this->mTimestamp
);
305 $dbw =& wfGetDB( DB_MASTER
);
306 $dbw->update( 'ipblocks',
308 'ipb_timestamp' => $dbw->timestamp($this->mTimestamp
),
309 'ipb_expiry' => $dbw->timestamp($this->mExpiry
),
310 ), array( /* WHERE */
311 'ipb_address' => $this->mAddress
312 ), 'Block::updateTimestamp'
319 /* private */ function clearCache()
321 global $wgBlockCache;
322 if ( is_object( $wgBlockCache ) ) {
323 $wgBlockCache->loadFromDB();
327 function getIntegerAddr()
329 return $this->mIntegerAddr
;
332 function getNetworkBits()
334 return $this->mNetworkBits
;
339 if ( $this->mByName
=== false ) {
340 $this->mByName
= User
::whoIs( $this->mBy
);
342 return $this->mByName
;
345 function forUpdate( $x = NULL ) {
346 return wfSetVar( $this->mForUpdate
, $x );
349 /* static */ function getAutoblockExpiry( $timestamp )
351 global $wgAutoblockExpiry;
352 return wfTimestamp( TS_MW
, wfTimestamp( TS_UNIX
, $timestamp ) +
$wgAutoblockExpiry );
355 /* static */ function normaliseRange( $range )
357 $parts = explode( '/', $range );
358 if ( count( $parts ) == 2 ) {
359 $shift = 32 - $parts[1];
360 $ipint = ip2long( $parts[0] );
361 $ipint = $ipint >> $shift << $shift;
362 $newip = long2ip( $ipint );
363 $range = "$newip/{$parts[1]}";