3 * Blocks and bans object
10 define ( 'EB_KEEP_EXPIRED', 1 );
11 define ( 'EB_FOR_UPDATE', 2 );
15 * All the functions in this class assume the object is either explicitly
16 * loaded or filled. It is not load-on-demand. There are no accessors.
18 * To use delete(), you only need to fill $mAddress
19 * Globals used: $wgBlockCache, $wgAutoblockExpiry
21 * @todo This could be used everywhere, but it isn't.
26 /* public*/ var $mAddress, $mUser, $mBy, $mReason, $mTimestamp, $mAuto, $mId, $mExpiry;
27 /* private */ var $mNetworkBits, $mIntegerAddr, $mForUpdate;
29 function Block( $address = '', $user = '', $by = 0, $reason = '',
30 $timestamp = '' , $auto = 0, $expiry = '' )
32 $this->mAddress
= $address;
35 $this->mReason
= $reason;
36 $this->mTimestamp
= wfTimestamp(TS_MW
,$timestamp);
38 if( empty( $expiry ) ) {
39 $this->mExpiry
= $expiry;
41 $this->mExpiry
= wfTimestamp( TS_MW
, $expiry );
44 $this->mForUpdate
= false;
45 $this->initialiseRange();
48 /*static*/ function newFromDB( $address, $user = 0, $killExpired = true )
51 $ban->load( $address, $user, $killExpired );
57 $mAddress = $mReason = $mTimestamp = '';
62 * Get a ban from the DB, with either the given address or the given username
64 function load( $address = '', $user = 0, $killExpired = true )
66 global $wgDBmysql4, $wgAntiLockFlags;
67 $fname = 'Block::load';
68 wfDebug( "Block::load: '$address', '$user', $killExpired\n" );
72 if ( $this->forUpdate() ) {
73 $db =& wfGetDB( DB_MASTER
);
74 if ( $wgAntiLockFlags & ALF_NO_BLOCK_LOCK
) {
77 $options = 'FOR UPDATE';
80 $db =& wfGetDB( DB_SLAVE
);
83 $ipblocks = $db->tableName( 'ipblocks' );
85 if ( 0 == $user && $address=='' ) {
86 $sql = "SELECT * from $ipblocks $options";
87 } elseif ($address=="") {
88 $sql = "SELECT * FROM $ipblocks WHERE ipb_user={$user} $options";
89 } elseif ($user=="") {
90 $sql = "SELECT * FROM $ipblocks WHERE ipb_address='" . $db->strencode( $address ) . "' $options";
91 } elseif ( $options=='' && $wgDBmysql4 ) {
92 # If there are no optiones (e.g. FOR UPDATE), use a UNION
93 # so that the query can make efficient use of indices
94 $sql = "SELECT * FROM $ipblocks WHERE ipb_address='" . $db->strencode( $address ) .
95 "' UNION SELECT * FROM $ipblocks WHERE ipb_user={$user}";
97 # If there are options, a UNION can not be used, use one
98 # SELECT instead. Will do a full table scan.
99 $sql = "SELECT * FROM $ipblocks WHERE (ipb_address='" . $db->strencode( $address ) .
100 "' OR ipb_user={$user}) $options";
103 $res = $db->query( $sql, $fname );
104 if ( 0 == $db->numRows( $res ) ) {
105 # User is not blocked
109 $row = $db->fetchObject( $res );
110 $this->initFromRow( $row );
112 if ( $killExpired ) {
113 # If requested, delete expired rows
115 $killed = $this->deleteIfExpired();
117 $row = $db->fetchObject( $res );
119 $this->initFromRow( $row );
122 } while ( $killed && $row );
124 # If there were any left after the killing finished, return true
135 $db->freeResult( $res );
139 function initFromRow( $row )
141 $this->mAddress
= $row->ipb_address
;
142 $this->mReason
= $row->ipb_reason
;
143 $this->mTimestamp
= wfTimestamp(TS_MW
,$row->ipb_timestamp
);
144 $this->mUser
= $row->ipb_user
;
145 $this->mBy
= $row->ipb_by
;
146 $this->mAuto
= $row->ipb_auto
;
147 $this->mId
= $row->ipb_id
;
148 $this->mExpiry
= $row->ipb_expiry ?
149 wfTimestamp(TS_MW
,$row->ipb_expiry
) :
152 $this->initialiseRange();
155 function initialiseRange()
157 if ( $this->mUser
== 0 ) {
158 $rangeParts = explode( '/', $this->mAddress
);
159 if ( count( $rangeParts ) == 2 ) {
160 $this->mNetworkBits
= $rangeParts[1];
162 $this->mNetworkBits
= 32;
164 $this->mIntegerAddr
= ip2long( $rangeParts[0] );
166 $this->mNetworkBits
= false;
167 $this->mIntegerAddr
= false;
172 * Callback with a Block object for every block
174 /*static*/ function enumBlocks( $callback, $tag, $flags = 0 )
176 global $wgAntiLockFlags;
178 $block = new Block();
179 if ( $flags & EB_FOR_UPDATE
) {
180 $db =& wfGetDB( DB_MASTER
);
181 if ( $wgAntiLockFlags & ALF_NO_BLOCK_LOCK
) {
184 $options = 'FOR UPDATE';
186 $block->forUpdate( true );
188 $db =& wfGetDB( DB_SLAVE
);
191 $ipblocks = $db->tableName( 'ipblocks' );
193 $sql = "SELECT * FROM $ipblocks ORDER BY ipb_timestamp DESC $options";
194 $res = $db->query( $sql, 'Block::enumBans' );
196 while ( $row = $db->fetchObject( $res ) ) {
197 $block->initFromRow( $row );
198 if ( !( $flags & EB_KEEP_EXPIRED
) ) {
199 if ( !$block->deleteIfExpired() ) {
200 $callback( $block, $tag );
203 $callback( $block, $tag );
206 wfFreeResult( $res );
211 $fname = 'Block::delete';
215 $dbw =& wfGetDB( DB_MASTER
);
217 if ( $this->mAddress
== '' ) {
218 $condition = array( 'ipb_id' => $this->mId
);
220 $condition = array( 'ipb_address' => $this->mAddress
);
222 $dbw->delete( 'ipblocks', $condition, $fname );
228 wfDebug( "Block::insert; timestamp {$this->mTimestamp}\n" );
229 $dbw =& wfGetDB( DB_MASTER
);
230 $ipb_id = $dbw->nextSequenceValue('ipblocks_ipb_id_val');
231 $dbw->insert( 'ipblocks',
234 'ipb_address' => $this->mAddress
,
235 'ipb_user' => $this->mUser
,
236 'ipb_by' => $this->mBy
,
237 'ipb_reason' => $this->mReason
,
238 'ipb_timestamp' => $dbw->timestamp($this->mTimestamp
),
239 'ipb_auto' => $this->mAuto
,
240 'ipb_expiry' => $this->mExpiry ?
241 $dbw->timestamp($this->mExpiry
) :
249 function deleteIfExpired()
251 if ( $this->isExpired() ) {
252 wfDebug( "Block::deleteIfExpired() -- deleting\n" );
256 wfDebug( "Block::deleteIfExpired() -- not expired\n" );
263 wfDebug( "Block::isExpired() checking current " . wfTimestampNow() . " vs $this->mExpiry\n" );
264 if ( !$this->mExpiry
) {
267 return wfTimestampNow() > $this->mExpiry
;
273 return $this->mAddress
!= '';
276 function updateTimestamp()
278 if ( $this->mAuto
) {
279 $this->mTimestamp
= wfTimestamp();
280 $this->mExpiry
= Block
::getAutoblockExpiry( $this->mTimestamp
);
282 $dbw =& wfGetDB( DB_MASTER
);
283 $dbw->update( 'ipblocks',
285 'ipb_timestamp' => $dbw->timestamp($this->mTimestamp
),
286 'ipb_expiry' => $dbw->timestamp($this->mExpiry
),
287 ), array( /* WHERE */
288 'ipb_address' => $this->mAddress
289 ), 'Block::updateTimestamp'
296 /* private */ function clearCache()
298 global $wgBlockCache;
299 if ( is_object( $wgBlockCache ) ) {
300 $wgBlockCache->loadFromDB();
304 function getIntegerAddr()
306 return $this->mIntegerAddr
;
309 function getNetworkBits()
311 return $this->mNetworkBits
;
314 function forUpdate( $x = NULL ) {
315 return wfSetVar( $this->mForUpdate
, $x );
318 /* static */ function getAutoblockExpiry( $timestamp )
320 global $wgAutoblockExpiry;
321 return wfTimestamp( TS_MW
, wfTimestamp( TS_UNIX
, $timestamp ) +
$wgAutoblockExpiry );
324 /* static */ function normaliseRange( $range )
326 $parts = explode( '/', $range );
327 if ( count( $parts ) == 2 ) {
328 $shift = 32 - $parts[1];
329 $ipint = ip2long( $parts[0] );
330 $ipint = $ipint >> $shift << $shift;
331 $newip = long2ip( $ipint );
332 $range = "$newip/{$parts[1]}";