3 * Blocks and bans object
11 define ( 'EB_KEEP_EXPIRED', 1 );
12 define ( 'EB_FOR_UPDATE', 2 );
16 * All the functions in this class assume the object is either explicitly
17 * loaded or filled. It is not load-on-demand. There are no accessors.
19 * To use delete(), you only need to fill $mAddress
20 * Globals used: $wgBlockCache, $wgAutoblockExpiry
22 * @todo This could be used everywhere, but it isn't.
27 /* public*/ var $mAddress, $mUser, $mBy, $mReason, $mTimestamp, $mAuto, $mId, $mExpiry;
28 /* private */ var $mNetworkBits, $mIntegerAddr, $mForUpdate;
30 function Block( $address = '', $user = '', $by = 0, $reason = '',
31 $timestamp = '' , $auto = 0, $expiry = '' )
33 $this->mAddress
= $address;
36 $this->mReason
= $reason;
37 $this->mTimestamp
= wfTimestamp(TS_MW
,$timestamp);
39 $this->mExpiry
= wfTimestamp(TS_MW
,$expiry);
41 $this->mForUpdate
= false;
42 $this->initialiseRange();
45 /*static*/ function newFromDB( $address, $user = 0, $killExpired = true )
48 $ban->load( $address, $user, $killExpired );
54 $mAddress = $mReason = $mTimestamp = '';
58 # Get a ban from the DB, with either the given address or the given username
59 function load( $address = '', $user = 0, $killExpired = true )
61 $fname = 'Block::load';
65 if ( $this->forUpdate() ) {
66 $db =& wfGetDB( DB_MASTER
);
67 $options = 'FOR UPDATE';
69 $db =& wfGetDB( DB_SLAVE
);
72 $ipblocks = $db->tableName( 'ipblocks' );
74 if ( 0 == $user && $address=='' ) {
75 $sql = "SELECT * from $ipblocks $options";
76 } elseif ($address=="") {
77 $sql = "SELECT * FROM $ipblocks WHERE ipb_user={$user} $options";
78 } elseif ($user=="") {
79 $sql = "SELECT * FROM $ipblocks WHERE ipb_address='" . $db->strencode( $address ) . "' $options";
81 $sql = "SELECT * FROM $ipblocks WHERE (ipb_address='" . $db->strencode( $address ) .
82 "' OR ipb_user={$user}) $options";
85 $res = $db->query( $sql, $fname );
86 if ( 0 == $db->numRows( $res ) ) {
91 $row = $db->fetchObject( $res );
92 $this->initFromRow( $row );
95 # If requested, delete expired rows
97 $killed = $this->deleteIfExpired();
99 $row = $db->fetchObject( $res );
101 $this->initFromRow( $row );
104 } while ( $killed && $row );
106 # If there were any left after the killing finished, return true
117 $db->freeResult( $res );
121 function initFromRow( $row )
123 $this->mAddress
= $row->ipb_address
;
124 $this->mReason
= $row->ipb_reason
;
125 $this->mTimestamp
= wfTimestamp(TS_MW
,$row->ipb_timestamp
);
126 $this->mUser
= $row->ipb_user
;
127 $this->mBy
= $row->ipb_by
;
128 $this->mAuto
= $row->ipb_auto
;
129 $this->mId
= $row->ipb_id
;
130 $this->mExpiry
= wfTimestamp(TS_MW
,$row->ipb_expiry
);
132 $this->initialiseRange();
135 function initialiseRange()
137 if ( $this->mUser
== 0 ) {
138 $rangeParts = explode( '/', $this->mAddress
);
139 if ( count( $rangeParts ) == 2 ) {
140 $this->mNetworkBits
= $rangeParts[1];
142 $this->mNetworkBits
= 32;
144 $this->mIntegerAddr
= ip2long( $rangeParts[0] );
146 $this->mNetworkBits
= false;
147 $this->mIntegerAddr
= false;
151 # Callback with a Block object for every block
152 /*static*/ function enumBlocks( $callback, $tag, $flags = 0 )
154 $block = new Block();
155 if ( $flags & EB_FOR_UPDATE
) {
156 $db =& wfGetDB( DB_MASTER
);
157 $options = 'FOR UPDATE';
158 $block->forUpdate( true );
160 $db =& wfGetDB( DB_SLAVE
);
163 $ipblocks = $db->tableName( 'ipblocks' );
165 $sql = "SELECT * FROM $ipblocks ORDER BY ipb_timestamp DESC $options";
166 $res = $db->query( $sql, 'Block::enumBans' );
168 while ( $row = $db->fetchObject( $res ) ) {
169 $block->initFromRow( $row );
170 if ( !( $flags & EB_KEEP_EXPIRED
) ) {
171 if ( !$block->deleteIfExpired() ) {
172 $callback( $block, $tag );
175 $callback( $block, $tag );
178 wfFreeResult( $res );
183 $fname = 'Block::delete';
184 $dbw =& wfGetDB( DB_MASTER
);
186 if ( $this->mAddress
== '' ) {
187 $condition = array( 'ipb_id' => $this->mId
);
189 $condition = array( 'ipb_address' => $this->mAddress
);
191 $dbw->delete( 'ipblocks', $condition, $fname );
197 $dbw =& wfGetDB( DB_MASTER
);
198 $dbw->insertArray( 'ipblocks',
200 'ipb_address' => $this->mAddress
,
201 'ipb_user' => $this->mUser
,
202 'ipb_by' => $this->mBy
,
203 'ipb_reason' => $this->mReason
,
204 'ipb_timestamp' => $dbw->timestamp($this->mTimestamp
),
205 'ipb_auto' => $this->mAuto
,
206 'ipb_expiry' => $dbw->timestamp($this->mExpiry
),
213 function deleteIfExpired()
215 if ( $this->isExpired() ) {
225 if ( !$this->mExpiry
) {
228 return wfTimestamp() > $this->mExpiry
;
234 return $this->mAddress
!= '';
237 function updateTimestamp()
239 if ( $this->mAuto
) {
240 $this->mTimestamp
= wfTimestamp();
241 $this->mExpiry
= Block
::getAutoblockExpiry( $this->mTimestamp
);
243 $dbw =& wfGetDB( DB_MASTER
);
244 $dbw->updateArray( 'ipblocks',
246 'ipb_timestamp' => $dbw->timestamp($this->mTimestamp
),
247 'ipb_expiry' => $dbw->timestamp($this->mExpiry
),
248 ), array( /* WHERE */
249 'ipb_address' => $this->mAddress
250 ), 'Block::updateTimestamp'
257 /* private */ function clearCache()
259 global $wgBlockCache;
260 if ( is_object( $wgBlockCache ) ) {
261 $wgBlockCache->loadFromDB();
265 function getIntegerAddr()
267 return $this->mIntegerAddr
;
270 function getNetworkBits()
272 return $this->mNetworkBits
;
275 function forUpdate( $x = NULL ) {
276 return wfSetVar( $this->mForUpdate
, $x );
279 /* static */ function getAutoblockExpiry( $timestamp )
281 global $wgAutoblockExpiry;
282 return wfTimestamp( TS_MW
, wfTimestamp( TS_UNIX
, $timestamp ) +
$wgAutoblockExpiry );
285 /* static */ function normaliseRange( $range )
287 $parts = explode( '/', $range );
288 if ( count( $parts ) == 2 ) {
289 $shift = 32 - $parts[1];
290 $ipint = ip2long( $parts[0] );
291 $ipint = $ipint >> $shift << $shift;
292 $newip = long2ip( $ipint );
293 $range = "$newip/{$parts[1]}";