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 $this->mExpiry
= wfTimestamp(TS_MW
,$expiry);
40 $this->mForUpdate
= false;
41 $this->initialiseRange();
44 /*static*/ function newFromDB( $address, $user = 0, $killExpired = true )
47 $ban->load( $address, $user, $killExpired );
53 $mAddress = $mReason = $mTimestamp = '';
57 # Get a ban from the DB, with either the given address or the given username
58 function load( $address = '', $user = 0, $killExpired = true )
60 $fname = 'Block::load';
64 if ( $this->forUpdate() ) {
65 $db =& wfGetDB( DB_MASTER
);
66 $options = 'FOR UPDATE';
68 $db =& wfGetDB( DB_SLAVE
);
71 $ipblocks = $db->tableName( 'ipblocks' );
73 if ( 0 == $user && $address=='' ) {
74 $sql = "SELECT * from $ipblocks $options";
75 } elseif ($address=="") {
76 $sql = "SELECT * FROM $ipblocks WHERE ipb_user={$user} $options";
77 } elseif ($user=="") {
78 $sql = "SELECT * FROM $ipblocks WHERE ipb_address='" . $db->strencode( $address ) . "' $options";
80 $sql = "SELECT * FROM $ipblocks WHERE (ipb_address='" . $db->strencode( $address ) .
81 "' OR ipb_user={$user}) $options";
84 $res = $db->query( $sql, $fname );
85 if ( 0 == $db->numRows( $res ) ) {
90 $row = $db->fetchObject( $res );
91 $this->initFromRow( $row );
94 # If requested, delete expired rows
96 $killed = $this->deleteIfExpired();
98 $row = $db->fetchObject( $res );
100 $this->initFromRow( $row );
103 } while ( $killed && $row );
105 # If there were any left after the killing finished, return true
116 $db->freeResult( $res );
120 function initFromRow( $row )
122 $this->mAddress
= $row->ipb_address
;
123 $this->mReason
= $row->ipb_reason
;
124 $this->mTimestamp
= wfTimestamp(TS_MW
,$row->ipb_timestamp
);
125 $this->mUser
= $row->ipb_user
;
126 $this->mBy
= $row->ipb_by
;
127 $this->mAuto
= $row->ipb_auto
;
128 $this->mId
= $row->ipb_id
;
129 $this->mExpiry
= wfTimestamp(TS_MW
,$row->ipb_expiry
);
131 $this->initialiseRange();
134 function initialiseRange()
136 if ( $this->mUser
== 0 ) {
137 $rangeParts = explode( '/', $this->mAddress
);
138 if ( count( $rangeParts ) == 2 ) {
139 $this->mNetworkBits
= $rangeParts[1];
141 $this->mNetworkBits
= 32;
143 $this->mIntegerAddr
= ip2long( $rangeParts[0] );
145 $this->mNetworkBits
= false;
146 $this->mIntegerAddr
= false;
150 # Callback with a Block object for every block
151 /*static*/ function enumBlocks( $callback, $tag, $flags = 0 )
153 $block = new Block();
154 if ( $flags & EB_FOR_UPDATE
) {
155 $db =& wfGetDB( DB_MASTER
);
156 $options = 'FOR UPDATE';
157 $block->forUpdate( true );
159 $db =& wfGetDB( DB_SLAVE
);
162 $ipblocks = $db->tableName( 'ipblocks' );
164 $sql = "SELECT * FROM $ipblocks ORDER BY ipb_timestamp DESC $options";
165 $res = $db->query( $sql, 'Block::enumBans' );
167 while ( $row = $db->fetchObject( $res ) ) {
168 $block->initFromRow( $row );
169 if ( !( $flags & EB_KEEP_EXPIRED
) ) {
170 if ( !$block->deleteIfExpired() ) {
171 $callback( $block, $tag );
174 $callback( $block, $tag );
177 wfFreeResult( $res );
182 $fname = 'Block::delete';
183 $dbw =& wfGetDB( DB_MASTER
);
185 if ( $this->mAddress
== '' ) {
186 $condition = array( 'ipb_id' => $this->mId
);
188 $condition = array( 'ipb_address' => $this->mAddress
);
190 $dbw->delete( 'ipblocks', $condition, $fname );
196 $dbw =& wfGetDB( DB_MASTER
);
197 $dbw->insert( 'ipblocks',
199 'ipb_address' => $this->mAddress
,
200 'ipb_user' => $this->mUser
,
201 'ipb_by' => $this->mBy
,
202 'ipb_reason' => $this->mReason
,
203 'ipb_timestamp' => $dbw->timestamp($this->mTimestamp
),
204 'ipb_auto' => $this->mAuto
,
205 'ipb_expiry' => $dbw->timestamp($this->mExpiry
),
212 function deleteIfExpired()
214 if ( $this->isExpired() ) {
224 if ( !$this->mExpiry
) {
227 return wfTimestamp() > $this->mExpiry
;
233 return $this->mAddress
!= '';
236 function updateTimestamp()
238 if ( $this->mAuto
) {
239 $this->mTimestamp
= wfTimestamp();
240 $this->mExpiry
= Block
::getAutoblockExpiry( $this->mTimestamp
);
242 $dbw =& wfGetDB( DB_MASTER
);
243 $dbw->update( 'ipblocks',
245 'ipb_timestamp' => $dbw->timestamp($this->mTimestamp
),
246 'ipb_expiry' => $dbw->timestamp($this->mExpiry
),
247 ), array( /* WHERE */
248 'ipb_address' => $this->mAddress
249 ), 'Block::updateTimestamp'
256 /* private */ function clearCache()
258 global $wgBlockCache;
259 if ( is_object( $wgBlockCache ) ) {
260 $wgBlockCache->loadFromDB();
264 function getIntegerAddr()
266 return $this->mIntegerAddr
;
269 function getNetworkBits()
271 return $this->mNetworkBits
;
274 function forUpdate( $x = NULL ) {
275 return wfSetVar( $this->mForUpdate
, $x );
278 /* static */ function getAutoblockExpiry( $timestamp )
280 global $wgAutoblockExpiry;
281 return wfTimestamp( TS_MW
, wfTimestamp( TS_UNIX
, $timestamp ) +
$wgAutoblockExpiry );
284 /* static */ function normaliseRange( $range )
286 $parts = explode( '/', $range );
287 if ( count( $parts ) == 2 ) {
288 $shift = 32 - $parts[1];
289 $ipint = ip2long( $parts[0] );
290 $ipint = $ipint >> $shift << $shift;
291 $newip = long2ip( $ipint );
292 $range = "$newip/{$parts[1]}";