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 )
67 $fname = 'Block::load';
68 wfDebug( "Block::load: '$address', '$user', $killExpired\n" );
72 if ( $this->forUpdate() ) {
73 $db =& wfGetDB( DB_MASTER
);
74 $options = 'FOR UPDATE';
76 $db =& wfGetDB( DB_SLAVE
);
79 $ipblocks = $db->tableName( 'ipblocks' );
81 if ( 0 == $user && $address=='' ) {
82 $sql = "SELECT * from $ipblocks $options";
83 } elseif ($address=="") {
84 $sql = "SELECT * FROM $ipblocks WHERE ipb_user={$user} $options";
85 } elseif ($user=="") {
86 $sql = "SELECT * FROM $ipblocks WHERE ipb_address='" . $db->strencode( $address ) . "' $options";
87 } elseif ( $options=='' && $wgDBmysql4 ) {
88 # If there are no optiones (e.g. FOR UPDATE), use a UNION
89 # so that the query can make efficient use of indices
90 $sql = "SELECT * FROM $ipblocks WHERE ipb_address='" . $db->strencode( $address ) .
91 "' UNION SELECT * FROM $ipblocks WHERE ipb_user={$user}";
93 # If there are options, a UNION can not be used, use one
94 # SELECT instead. Will do a full table scan.
95 $sql = "SELECT * FROM $ipblocks WHERE (ipb_address='" . $db->strencode( $address ) .
96 "' OR ipb_user={$user}) $options";
99 $res = $db->query( $sql, $fname );
100 if ( 0 == $db->numRows( $res ) ) {
101 # User is not blocked
105 $row = $db->fetchObject( $res );
106 $this->initFromRow( $row );
108 if ( $killExpired ) {
109 # If requested, delete expired rows
111 $killed = $this->deleteIfExpired();
113 $row = $db->fetchObject( $res );
115 $this->initFromRow( $row );
118 } while ( $killed && $row );
120 # If there were any left after the killing finished, return true
131 $db->freeResult( $res );
135 function initFromRow( $row )
137 $this->mAddress
= $row->ipb_address
;
138 $this->mReason
= $row->ipb_reason
;
139 $this->mTimestamp
= wfTimestamp(TS_MW
,$row->ipb_timestamp
);
140 $this->mUser
= $row->ipb_user
;
141 $this->mBy
= $row->ipb_by
;
142 $this->mAuto
= $row->ipb_auto
;
143 $this->mId
= $row->ipb_id
;
144 $this->mExpiry
= $row->ipb_expiry ?
145 wfTimestamp(TS_MW
,$row->ipb_expiry
) :
148 $this->initialiseRange();
151 function initialiseRange()
153 if ( $this->mUser
== 0 ) {
154 $rangeParts = explode( '/', $this->mAddress
);
155 if ( count( $rangeParts ) == 2 ) {
156 $this->mNetworkBits
= $rangeParts[1];
158 $this->mNetworkBits
= 32;
160 $this->mIntegerAddr
= ip2long( $rangeParts[0] );
162 $this->mNetworkBits
= false;
163 $this->mIntegerAddr
= false;
168 * Callback with a Block object for every block
170 /*static*/ function enumBlocks( $callback, $tag, $flags = 0 )
172 $block = new Block();
173 if ( $flags & EB_FOR_UPDATE
) {
174 $db =& wfGetDB( DB_MASTER
);
175 $options = 'FOR UPDATE';
176 $block->forUpdate( true );
178 $db =& wfGetDB( DB_SLAVE
);
181 $ipblocks = $db->tableName( 'ipblocks' );
183 $sql = "SELECT * FROM $ipblocks ORDER BY ipb_timestamp DESC $options";
184 $res = $db->query( $sql, 'Block::enumBans' );
186 while ( $row = $db->fetchObject( $res ) ) {
187 $block->initFromRow( $row );
188 if ( !( $flags & EB_KEEP_EXPIRED
) ) {
189 if ( !$block->deleteIfExpired() ) {
190 $callback( $block, $tag );
193 $callback( $block, $tag );
196 wfFreeResult( $res );
201 $fname = 'Block::delete';
205 $dbw =& wfGetDB( DB_MASTER
);
207 if ( $this->mAddress
== '' ) {
208 $condition = array( 'ipb_id' => $this->mId
);
210 $condition = array( 'ipb_address' => $this->mAddress
);
212 $dbw->delete( 'ipblocks', $condition, $fname );
218 wfDebug( "Block::insert; timestamp {$this->mTimestamp}\n" );
219 $dbw =& wfGetDB( DB_MASTER
);
220 $dbw->insert( 'ipblocks',
222 'ipb_address' => $this->mAddress
,
223 'ipb_user' => $this->mUser
,
224 'ipb_by' => $this->mBy
,
225 'ipb_reason' => $this->mReason
,
226 'ipb_timestamp' => $dbw->timestamp($this->mTimestamp
),
227 'ipb_auto' => $this->mAuto
,
228 'ipb_expiry' => $this->mExpiry ?
229 $dbw->timestamp($this->mExpiry
) :
237 function deleteIfExpired()
239 if ( $this->isExpired() ) {
240 wfDebug( "Block::deleteIfExpired() -- deleting\n" );
244 wfDebug( "Block::deleteIfExpired() -- not expired\n" );
251 wfDebug( "Block::isExpired() checking current " . wfTimestampNow() . " vs $this->mExpiry\n" );
252 if ( !$this->mExpiry
) {
255 return wfTimestampNow() > $this->mExpiry
;
261 return $this->mAddress
!= '';
264 function updateTimestamp()
266 if ( $this->mAuto
) {
267 $this->mTimestamp
= wfTimestamp();
268 $this->mExpiry
= Block
::getAutoblockExpiry( $this->mTimestamp
);
270 $dbw =& wfGetDB( DB_MASTER
);
271 $dbw->update( 'ipblocks',
273 'ipb_timestamp' => $dbw->timestamp($this->mTimestamp
),
274 'ipb_expiry' => $dbw->timestamp($this->mExpiry
),
275 ), array( /* WHERE */
276 'ipb_address' => $this->mAddress
277 ), 'Block::updateTimestamp'
284 /* private */ function clearCache()
286 global $wgBlockCache;
287 if ( is_object( $wgBlockCache ) ) {
288 $wgBlockCache->loadFromDB();
292 function getIntegerAddr()
294 return $this->mIntegerAddr
;
297 function getNetworkBits()
299 return $this->mNetworkBits
;
302 function forUpdate( $x = NULL ) {
303 return wfSetVar( $this->mForUpdate
, $x );
306 /* static */ function getAutoblockExpiry( $timestamp )
308 global $wgAutoblockExpiry;
309 return wfTimestamp( TS_MW
, wfTimestamp( TS_UNIX
, $timestamp ) +
$wgAutoblockExpiry );
312 /* static */ function normaliseRange( $range )
314 $parts = explode( '/', $range );
315 if ( count( $parts ) == 2 ) {
316 $shift = 32 - $parts[1];
317 $ipint = ip2long( $parts[0] );
318 $ipint = $ipint >> $shift << $shift;
319 $newip = long2ip( $ipint );
320 $range = "$newip/{$parts[1]}";