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 = '';
61 # Get a ban from the DB, with either the given address or the given username
62 function load( $address = '', $user = 0, $killExpired = true )
64 $fname = 'Block::load';
65 wfDebug( "Block::load: '$address', '$user', $killExpired\n" );
69 if ( $this->forUpdate() ) {
70 $db =& wfGetDB( DB_MASTER
);
71 $options = 'FOR UPDATE';
73 $db =& wfGetDB( DB_SLAVE
);
76 $ipblocks = $db->tableName( 'ipblocks' );
78 if ( 0 == $user && $address=='' ) {
79 $sql = "SELECT * from $ipblocks $options";
80 } elseif ($address=="") {
81 $sql = "SELECT * FROM $ipblocks WHERE ipb_user={$user} $options";
82 } elseif ($user=="") {
83 $sql = "SELECT * FROM $ipblocks WHERE ipb_address='" . $db->strencode( $address ) . "' $options";
84 } elseif ( $options=='' ) {
85 # If there are no optiones (e.g. FOR UPDATE), use a UNION
86 # so that the query can make efficient use of indices
87 $sql = "SELECT * FROM $ipblocks WHERE ipb_address='" . $db->strencode( $address ) .
88 "' UNION SELECT * FROM $ipblocks WHERE ipb_user={$user}";
90 # If there are options, a UNION can not be used, use one
91 # SELECT instead. Will do a full table scan.
92 $sql = "SELECT * FROM $ipblocks WHERE (ipb_address='" . $db->strencode( $address ) .
93 "' OR ipb_user={$user}) $options";
96 $res = $db->query( $sql, $fname );
97 if ( 0 == $db->numRows( $res ) ) {
102 $row = $db->fetchObject( $res );
103 $this->initFromRow( $row );
105 if ( $killExpired ) {
106 # If requested, delete expired rows
108 $killed = $this->deleteIfExpired();
110 $row = $db->fetchObject( $res );
112 $this->initFromRow( $row );
115 } while ( $killed && $row );
117 # If there were any left after the killing finished, return true
128 $db->freeResult( $res );
132 function initFromRow( $row )
134 $this->mAddress
= $row->ipb_address
;
135 $this->mReason
= $row->ipb_reason
;
136 $this->mTimestamp
= wfTimestamp(TS_MW
,$row->ipb_timestamp
);
137 $this->mUser
= $row->ipb_user
;
138 $this->mBy
= $row->ipb_by
;
139 $this->mAuto
= $row->ipb_auto
;
140 $this->mId
= $row->ipb_id
;
141 $this->mExpiry
= $row->ipb_expiry ?
142 wfTimestamp(TS_MW
,$row->ipb_expiry
) :
145 $this->initialiseRange();
148 function initialiseRange()
150 if ( $this->mUser
== 0 ) {
151 $rangeParts = explode( '/', $this->mAddress
);
152 if ( count( $rangeParts ) == 2 ) {
153 $this->mNetworkBits
= $rangeParts[1];
155 $this->mNetworkBits
= 32;
157 $this->mIntegerAddr
= ip2long( $rangeParts[0] );
159 $this->mNetworkBits
= false;
160 $this->mIntegerAddr
= false;
164 # Callback with a Block object for every block
165 /*static*/ function enumBlocks( $callback, $tag, $flags = 0 )
167 $block = new Block();
168 if ( $flags & EB_FOR_UPDATE
) {
169 $db =& wfGetDB( DB_MASTER
);
170 $options = 'FOR UPDATE';
171 $block->forUpdate( true );
173 $db =& wfGetDB( DB_SLAVE
);
176 $ipblocks = $db->tableName( 'ipblocks' );
178 $sql = "SELECT * FROM $ipblocks ORDER BY ipb_timestamp DESC $options";
179 $res = $db->query( $sql, 'Block::enumBans' );
181 while ( $row = $db->fetchObject( $res ) ) {
182 $block->initFromRow( $row );
183 if ( !( $flags & EB_KEEP_EXPIRED
) ) {
184 if ( !$block->deleteIfExpired() ) {
185 $callback( $block, $tag );
188 $callback( $block, $tag );
191 wfFreeResult( $res );
196 $fname = 'Block::delete';
197 $dbw =& wfGetDB( DB_MASTER
);
199 if ( $this->mAddress
== '' ) {
200 $condition = array( 'ipb_id' => $this->mId
);
202 $condition = array( 'ipb_address' => $this->mAddress
);
204 $dbw->delete( 'ipblocks', $condition, $fname );
210 wfDebug( "Block::insert; timestamp {$this->mTimestamp}\n" );
211 $dbw =& wfGetDB( DB_MASTER
);
212 $dbw->insert( 'ipblocks',
214 'ipb_address' => $this->mAddress
,
215 'ipb_user' => $this->mUser
,
216 'ipb_by' => $this->mBy
,
217 'ipb_reason' => $this->mReason
,
218 'ipb_timestamp' => $dbw->timestamp($this->mTimestamp
),
219 'ipb_auto' => $this->mAuto
,
220 'ipb_expiry' => $this->mExpiry ?
221 $dbw->timestamp($this->mExpiry
) :
229 function deleteIfExpired()
231 if ( $this->isExpired() ) {
232 wfDebug( "Block::deleteIfExpired() -- deleting\n" );
236 wfDebug( "Block::deleteIfExpired() -- not expired\n" );
243 wfDebug( "Block::isExpired() checking current " . wfTimestampNow() . " vs $this->mExpiry\n" );
244 if ( !$this->mExpiry
) {
247 return wfTimestampNow() > $this->mExpiry
;
253 return $this->mAddress
!= '';
256 function updateTimestamp()
258 if ( $this->mAuto
) {
259 $this->mTimestamp
= wfTimestamp();
260 $this->mExpiry
= Block
::getAutoblockExpiry( $this->mTimestamp
);
262 $dbw =& wfGetDB( DB_MASTER
);
263 $dbw->update( 'ipblocks',
265 'ipb_timestamp' => $dbw->timestamp($this->mTimestamp
),
266 'ipb_expiry' => $dbw->timestamp($this->mExpiry
),
267 ), array( /* WHERE */
268 'ipb_address' => $this->mAddress
269 ), 'Block::updateTimestamp'
276 /* private */ function clearCache()
278 global $wgBlockCache;
279 if ( is_object( $wgBlockCache ) ) {
280 $wgBlockCache->loadFromDB();
284 function getIntegerAddr()
286 return $this->mIntegerAddr
;
289 function getNetworkBits()
291 return $this->mNetworkBits
;
294 function forUpdate( $x = NULL ) {
295 return wfSetVar( $this->mForUpdate
, $x );
298 /* static */ function getAutoblockExpiry( $timestamp )
300 global $wgAutoblockExpiry;
301 return wfTimestamp( TS_MW
, wfTimestamp( TS_UNIX
, $timestamp ) +
$wgAutoblockExpiry );
304 /* static */ function normaliseRange( $range )
306 $parts = explode( '/', $range );
307 if ( count( $parts ) == 2 ) {
308 $shift = 32 - $parts[1];
309 $ipint = ip2long( $parts[0] );
310 $ipint = $ipint >> $shift << $shift;
311 $newip = long2ip( $ipint );
312 $range = "$newip/{$parts[1]}";