3 * Blocks and bans object
9 define ( 'EB_KEEP_EXPIRED', 1 );
10 define ( 'EB_FOR_UPDATE', 2 );
14 * All the functions in this class assume the object is either explicitly
15 * loaded or filled. It is not load-on-demand. There are no accessors.
17 * To use delete(), you only need to fill $mAddress
18 * Globals used: $wgBlockCache, $wgAutoblockExpiry
19 * @todo This could be used everywhere, but it isn't.
23 /* public*/ var $mAddress, $mUser, $mBy, $mReason, $mTimestamp, $mAuto, $mId, $mExpiry;
24 /* private */ var $mNetworkBits, $mIntegerAddr, $mForUpdate;
26 function Block( $address = '', $user = '', $by = 0, $reason = '',
27 $timestamp = '' , $auto = 0, $expiry = '' )
29 $this->mAddress
= $address;
32 $this->mReason
= $reason;
33 $this->mTimestamp
= $timestamp;
35 $this->mExpiry
= $expiry;
37 $this->mForUpdate
= false;
38 $this->initialiseRange();
41 /*static*/ function newFromDB( $address, $user = 0, $killExpired = true )
44 $ban->load( $address, $user, $killExpired );
50 $mAddress = $mReason = $mTimestamp = '';
54 # Get a ban from the DB, with either the given address or the given username
55 function load( $address = '', $user = 0, $killExpired = true )
57 $fname = 'Block::load';
61 if ( $this->forUpdate() ) {
62 $db =& wfGetDB( DB_MASTER
);
63 $options = 'FOR UPDATE';
65 $db =& wfGetDB( DB_SLAVE
);
68 $ipblocks = $db->tableName( 'ipblocks' );
70 if ( 0 == $user && $address=='' ) {
71 $sql = "SELECT * from $ipblocks $options";
72 } elseif ($address=="") {
73 $sql = "SELECT * FROM $ipblocks WHERE ipb_user={$user} $options";
74 } elseif ($user=="") {
75 $sql = "SELECT * FROM $ipblocks WHERE ipb_address='" . $db->strencode( $address ) . "' $options";
77 $sql = "SELECT * FROM $ipblocks WHERE (ipb_address='" . $db->strencode( $address ) .
78 "' OR ipb_user={$user}) $options";
81 $res = $db->query( $sql, $fname );
82 if ( 0 == $db->numRows( $res ) ) {
87 $row = $db->fetchObject( $res );
88 $this->initFromRow( $row );
91 # If requested, delete expired rows
93 $killed = $this->deleteIfExpired();
95 $row = $db->fetchObject( $res );
97 $this->initFromRow( $row );
100 } while ( $killed && $row );
102 # If there were any left after the killing finished, return true
113 $db->freeResult( $res );
117 function initFromRow( $row )
119 $this->mAddress
= $row->ipb_address
;
120 $this->mReason
= $row->ipb_reason
;
121 $this->mTimestamp
= $row->ipb_timestamp
;
122 $this->mUser
= $row->ipb_user
;
123 $this->mBy
= $row->ipb_by
;
124 $this->mAuto
= $row->ipb_auto
;
125 $this->mId
= $row->ipb_id
;
126 $this->mExpiry
= $row->ipb_expiry
;
128 $this->initialiseRange();
131 function initialiseRange()
133 if ( $this->mUser
== 0 ) {
134 $rangeParts = explode( '/', $this->mAddress
);
135 if ( count( $rangeParts ) == 2 ) {
136 $this->mNetworkBits
= $rangeParts[1];
138 $this->mNetworkBits
= 32;
140 $this->mIntegerAddr
= ip2long( $rangeParts[0] );
142 $this->mNetworkBits
= false;
143 $this->mIntegerAddr
= false;
147 # Callback with a Block object for every block
148 /*static*/ function enumBlocks( $callback, $tag, $flags = 0 )
150 $block = new Block();
151 if ( $flags & EB_FOR_UPDATE
) {
152 $db =& wfGetDB( DB_MASTER
);
153 $options = 'FOR UPDATE';
154 $block->forUpdate( true );
156 $db =& wfGetDB( DB_SLAVE
);
159 $ipblocks = $db->tableName( 'ipblocks' );
161 $sql = "SELECT * FROM $ipblocks ORDER BY ipb_timestamp DESC $options";
162 $res = $db->query( $sql, 'Block::enumBans' );
164 while ( $row = $db->fetchObject( $res ) ) {
165 $block->initFromRow( $row );
166 if ( !( $flags & EB_KEEP_EXPIRED
) ) {
167 if ( !$block->deleteIfExpired() ) {
168 $callback( $block, $tag );
171 $callback( $block, $tag );
174 wfFreeResult( $res );
179 $fname = 'Block::delete';
180 $dbw =& wfGetDB( DB_MASTER
);
182 if ( $this->mAddress
== '' ) {
183 $condition = array( 'ipb_id' => $this->mId
);
185 $condition = array( 'ipb_address' => $this->mAddress
);
187 $dbw->delete( 'ipblocks', $condition, $fname );
193 $dbw =& wfGetDB( DB_MASTER
);
194 $dbw->insertArray( 'ipblocks',
196 'ipb_address' => $this->mAddress
,
197 'ipb_user' => $this->mUser
,
198 'ipb_by' => $this->mBy
,
199 'ipb_reason' => $this->mReason
,
200 'ipb_timestamp' => $this->mTimestamp
,
201 'ipb_auto' => $this->mAuto
,
202 'ipb_expiry' => $this->mExpiry
,
209 function deleteIfExpired()
211 if ( $this->isExpired() ) {
221 if ( !$this->mExpiry
) {
224 return wfTimestampNow() > $this->mExpiry
;
230 return $this->mAddress
!= '';
233 function updateTimestamp()
235 if ( $this->mAuto
) {
236 $this->mTimestamp
= wfTimestampNow();
237 $this->mExpiry
= Block
::getAutoblockExpiry( $this->mTimestamp
);
239 $dbw =& wfGetDB( DB_MASTER
);
240 $dbw->updateArray( 'ipblocks',
242 'ipb_timestamp' => $this->mTimestamp
,
243 'ipb_expiry' => $this->mExpiry
,
244 ), array( /* WHERE */
245 'ipb_address' => $this->mAddress
246 ), 'Block::updateTimestamp'
253 /* private */ function clearCache()
255 global $wgBlockCache;
256 if ( is_object( $wgBlockCache ) ) {
257 $wgBlockCache->loadFromDB();
261 function getIntegerAddr()
263 return $this->mIntegerAddr
;
266 function getNetworkBits()
268 return $this->mNetworkBits
;
271 function forUpdate( $x = NULL ) {
272 return wfSetVar( $this->mForUpdate
, $x );
275 /* static */ function getAutoblockExpiry( $timestamp )
277 global $wgAutoblockExpiry;
278 return wfUnix2Timestamp( wfTimestamp2Unix( $timestamp ) +
$wgAutoblockExpiry );
281 /* static */ function normaliseRange( $range )
283 $parts = explode( '/', $range );
284 if ( count( $parts ) == 2 ) {
285 $shift = 32 - $parts[1];
286 $ipint = ip2long( $parts[0] );
287 $ipint = $ipint >> $shift << $shift;
288 $newip = long2ip( $ipint );
289 $range = "$newip/{$parts[1]}";