Revert to r14165 . Did too many changes, didnt even run parserTests (i am bad)
[mediawiki.git] / includes / Block.php
blob42969b606c136e510b139f8a62d806a7f1586848
1 <?php
2 /**
3 * Blocks and bans object
4 * @package MediaWiki
5 */
7 /**
8 * Some globals
9 */
10 define ( 'EB_KEEP_EXPIRED', 1 );
11 define ( 'EB_FOR_UPDATE', 2 );
12 define ( 'EB_RANGE_ONLY', 4 );
14 /**
15 * The block class
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: $wgAutoblockExpiry, $wgAntiLockFlags
22 * @todo This could be used everywhere, but it isn't.
23 * @package MediaWiki
25 class Block
27 /* public*/ var $mAddress, $mUser, $mBy, $mReason, $mTimestamp, $mAuto, $mId, $mExpiry,
28 $mRangeStart, $mRangeEnd;
29 /* private */ var $mNetworkBits, $mIntegerAddr, $mForUpdate, $mFromMaster, $mByName;
31 function Block( $address = '', $user = '', $by = 0, $reason = '',
32 $timestamp = '' , $auto = 0, $expiry = '' )
34 $this->mAddress = $address;
35 $this->mUser = $user;
36 $this->mBy = $by;
37 $this->mReason = $reason;
38 $this->mTimestamp = wfTimestamp(TS_MW,$timestamp);
39 $this->mAuto = $auto;
40 if( empty( $expiry ) ) {
41 $this->mExpiry = $expiry;
42 } else {
43 $this->mExpiry = wfTimestamp( TS_MW, $expiry );
46 $this->mForUpdate = false;
47 $this->mFromMaster = false;
48 $this->mByName = false;
49 $this->initialiseRange();
52 /*static*/ function newFromDB( $address, $user = 0, $killExpired = true )
54 $ban = new Block();
55 $ban->load( $address, $user, $killExpired );
56 return $ban;
59 function clear()
61 $this->mAddress = $this->mReason = $this->mTimestamp = '';
62 $this->mUser = $this->mBy = 0;
63 $this->mByName = false;
67 /**
68 * Get the DB object and set the reference parameter to the query options
70 function &getDBOptions( &$options )
72 global $wgAntiLockFlags;
73 if ( $this->mForUpdate || $this->mFromMaster ) {
74 $db =& wfGetDB( DB_MASTER );
75 if ( !$this->mForUpdate || ($wgAntiLockFlags & ALF_NO_BLOCK_LOCK) ) {
76 $options = '';
77 } else {
78 $options = 'FOR UPDATE';
80 } else {
81 $db =& wfGetDB( DB_SLAVE );
82 $options = '';
84 return $db;
87 /**
88 * Get a ban from the DB, with either the given address or the given username
90 function load( $address = '', $user = 0, $killExpired = true )
92 $fname = 'Block::load';
93 wfDebug( "Block::load: '$address', '$user', $killExpired\n" );
95 $options = '';
96 $db =& $this->getDBOptions( $options );
98 $ret = false;
99 $killed = false;
100 $ipblocks = $db->tableName( 'ipblocks' );
102 if ( 0 == $user && $address == '' ) {
103 # Invalid user specification, not blocked
104 $this->clear();
105 return false;
106 } elseif ( $address == '' ) {
107 $sql = "SELECT * FROM $ipblocks WHERE ipb_user={$user} $options";
108 } elseif ( $user == '' ) {
109 $sql = "SELECT * FROM $ipblocks WHERE ipb_address=" . $db->addQuotes( $address ) . " $options";
110 } elseif ( $options == '' ) {
111 # If there are no options (e.g. FOR UPDATE), use a UNION
112 # so that the query can make efficient use of indices
113 $sql = "SELECT * FROM $ipblocks WHERE ipb_address='" . $db->strencode( $address ) .
114 "' UNION SELECT * FROM $ipblocks WHERE ipb_user={$user}";
115 } else {
116 # If there are options, a UNION can not be used, use one
117 # SELECT instead. Will do a full table scan.
118 $sql = "SELECT * FROM $ipblocks WHERE (ipb_address='" . $db->strencode( $address ) .
119 "' OR ipb_user={$user}) $options";
122 $res = $db->query( $sql, $fname );
123 if ( 0 != $db->numRows( $res ) ) {
124 # Get first block
125 $row = $db->fetchObject( $res );
126 $this->initFromRow( $row );
128 if ( $killExpired ) {
129 # If requested, delete expired rows
130 do {
131 $killed = $this->deleteIfExpired();
132 if ( $killed ) {
133 $row = $db->fetchObject( $res );
134 if ( $row ) {
135 $this->initFromRow( $row );
138 } while ( $killed && $row );
140 # If there were any left after the killing finished, return true
141 if ( !$row ) {
142 $ret = false;
143 $this->clear();
144 } else {
145 $ret = true;
147 } else {
148 $ret = true;
151 $db->freeResult( $res );
153 # No blocks found yet? Try looking for range blocks
154 if ( !$ret && $address != '' ) {
155 $ret = $this->loadRange( $address, $killExpired );
157 if ( !$ret ) {
158 $this->clear();
161 return $ret;
165 * Search the database for any range blocks matching the given address, and
166 * load the row if one is found.
168 function loadRange( $address, $killExpired = true )
170 $fname = 'Block::loadRange';
172 $iaddr = wfIP2Hex( $address );
173 if ( $iaddr === false ) {
174 # Invalid address
175 return false;
178 # Only scan ranges which start in this /16, this improves search speed
179 # Blocks should not cross a /16 boundary.
180 $range = substr( $iaddr, 0, 4 );
182 $options = '';
183 $db =& $this->getDBOptions( $options );
184 $ipblocks = $db->tableName( 'ipblocks' );
185 $sql = "SELECT * FROM $ipblocks WHERE ipb_range_start LIKE '$range%' ".
186 "AND ipb_range_start <= '$iaddr' AND ipb_range_end >= '$iaddr' $options";
187 $res = $db->query( $sql, $fname );
188 $row = $db->fetchObject( $res );
190 $success = false;
191 if ( $row ) {
192 # Found a row, initialise this object
193 $this->initFromRow( $row );
195 # Is it expired?
196 if ( !$killExpired || !$this->deleteIfExpired() ) {
197 # No, return true
198 $success = true;
202 $db->freeResult( $res );
203 return $success;
207 * Determine if a given integer IPv4 address is in a given CIDR network
209 function isAddressInRange( $addr, $range ) {
210 list( $network, $bits ) = wfParseCIDR( $range );
211 if ( $network !== false && $addr >> ( 32 - $bits ) == $network >> ( 32 - $bits ) ) {
212 return true;
213 } else {
214 return false;
218 function initFromRow( $row )
220 $this->mAddress = $row->ipb_address;
221 $this->mReason = $row->ipb_reason;
222 $this->mTimestamp = wfTimestamp(TS_MW,$row->ipb_timestamp);
223 $this->mUser = $row->ipb_user;
224 $this->mBy = $row->ipb_by;
225 $this->mAuto = $row->ipb_auto;
226 $this->mId = $row->ipb_id;
227 $this->mExpiry = $row->ipb_expiry ?
228 wfTimestamp(TS_MW,$row->ipb_expiry) :
229 $row->ipb_expiry;
230 if ( isset( $row->user_name ) ) {
231 $this->mByName = $row->user_name;
232 } else {
233 $this->mByName = false;
235 $this->mRangeStart = $row->ipb_range_start;
236 $this->mRangeEnd = $row->ipb_range_end;
239 function initialiseRange()
241 $this->mRangeStart = '';
242 $this->mRangeEnd = '';
243 if ( $this->mUser == 0 ) {
244 list( $network, $bits ) = wfParseCIDR( $this->mAddress );
245 if ( $network !== false ) {
246 $this->mRangeStart = sprintf( '%08X', $network );
247 $this->mRangeEnd = sprintf( '%08X', $network + (1 << (32 - $bits)) - 1 );
253 * Callback with a Block object for every block
254 * @return integer number of blocks;
256 /*static*/ function enumBlocks( $callback, $tag, $flags = 0 )
258 global $wgAntiLockFlags;
260 $block = new Block();
261 if ( $flags & EB_FOR_UPDATE ) {
262 $db =& wfGetDB( DB_MASTER );
263 if ( $wgAntiLockFlags & ALF_NO_BLOCK_LOCK ) {
264 $options = '';
265 } else {
266 $options = 'FOR UPDATE';
268 $block->forUpdate( true );
269 } else {
270 $db =& wfGetDB( DB_SLAVE );
271 $options = '';
273 if ( $flags & EB_RANGE_ONLY ) {
274 $cond = " AND ipb_range_start <> ''";
275 } else {
276 $cond = '';
279 $now = wfTimestampNow();
281 extract( $db->tableNames( 'ipblocks', 'user' ) );
283 $sql = "SELECT $ipblocks.*,user_name FROM $ipblocks,$user " .
284 "WHERE user_id=ipb_by $cond ORDER BY ipb_timestamp DESC $options";
285 $res = $db->query( $sql, 'Block::enumBlocks' );
286 $num_rows = $db->numRows( $res );
288 while ( $row = $db->fetchObject( $res ) ) {
289 $block->initFromRow( $row );
290 if ( ( $flags & EB_RANGE_ONLY ) && $block->mRangeStart == '' ) {
291 continue;
294 if ( !( $flags & EB_KEEP_EXPIRED ) ) {
295 if ( $block->mExpiry && $now > $block->mExpiry ) {
296 $block->delete();
297 } else {
298 call_user_func( $callback, $block, $tag );
300 } else {
301 call_user_func( $callback, $block, $tag );
304 wfFreeResult( $res );
305 return $num_rows;
308 function delete()
310 $fname = 'Block::delete';
311 if (wfReadOnly()) {
312 return;
314 $dbw =& wfGetDB( DB_MASTER );
316 if ( $this->mAddress == '' ) {
317 $condition = array( 'ipb_id' => $this->mId );
318 } else {
319 $condition = array( 'ipb_address' => $this->mAddress );
321 return( $dbw->delete( 'ipblocks', $condition, $fname ) > 0 ? true : false );
324 function insert()
326 wfDebug( "Block::insert; timestamp {$this->mTimestamp}\n" );
327 $dbw =& wfGetDB( DB_MASTER );
328 $ipb_id = $dbw->nextSequenceValue('ipblocks_ipb_id_val');
329 $dbw->insert( 'ipblocks',
330 array(
331 'ipb_id' => $ipb_id,
332 'ipb_address' => $this->mAddress,
333 'ipb_user' => $this->mUser,
334 'ipb_by' => $this->mBy,
335 'ipb_reason' => $this->mReason,
336 'ipb_timestamp' => $dbw->timestamp($this->mTimestamp),
337 'ipb_auto' => $this->mAuto,
338 'ipb_expiry' => $this->mExpiry ?
339 $dbw->timestamp($this->mExpiry) :
340 $this->mExpiry,
341 'ipb_range_start' => $this->mRangeStart,
342 'ipb_range_end' => $this->mRangeEnd,
343 ), 'Block::insert'
347 function deleteIfExpired()
349 $fname = 'Block::deleteIfExpired';
350 wfProfileIn( $fname );
351 if ( $this->isExpired() ) {
352 wfDebug( "Block::deleteIfExpired() -- deleting\n" );
353 $this->delete();
354 $retVal = true;
355 } else {
356 wfDebug( "Block::deleteIfExpired() -- not expired\n" );
357 $retVal = false;
359 wfProfileOut( $fname );
360 return $retVal;
363 function isExpired()
365 wfDebug( "Block::isExpired() checking current " . wfTimestampNow() . " vs $this->mExpiry\n" );
366 if ( !$this->mExpiry ) {
367 return false;
368 } else {
369 return wfTimestampNow() > $this->mExpiry;
373 function isValid()
375 return $this->mAddress != '';
378 function updateTimestamp()
380 if ( $this->mAuto ) {
381 $this->mTimestamp = wfTimestamp();
382 $this->mExpiry = Block::getAutoblockExpiry( $this->mTimestamp );
384 $dbw =& wfGetDB( DB_MASTER );
385 $dbw->update( 'ipblocks',
386 array( /* SET */
387 'ipb_timestamp' => $dbw->timestamp($this->mTimestamp),
388 'ipb_expiry' => $dbw->timestamp($this->mExpiry),
389 ), array( /* WHERE */
390 'ipb_address' => $this->mAddress
391 ), 'Block::updateTimestamp'
397 function getIntegerAddr()
399 return $this->mIntegerAddr;
402 function getNetworkBits()
404 return $this->mNetworkBits;
407 function getByName()
409 if ( $this->mByName === false ) {
410 $this->mByName = User::whoIs( $this->mBy );
412 return $this->mByName;
415 function forUpdate( $x = NULL ) {
416 return wfSetVar( $this->mForUpdate, $x );
419 function fromMaster( $x = NULL ) {
420 return wfSetVar( $this->mFromMaster, $x );
423 /* static */ function getAutoblockExpiry( $timestamp )
425 global $wgAutoblockExpiry;
426 return wfTimestamp( TS_MW, wfTimestamp( TS_UNIX, $timestamp ) + $wgAutoblockExpiry );
429 /* static */ function normaliseRange( $range )
431 $parts = explode( '/', $range );
432 if ( count( $parts ) == 2 ) {
433 $shift = 32 - $parts[1];
434 $ipint = wfIP2Unsigned( $parts[0] );
435 $ipint = $ipint >> $shift << $shift;
436 $newip = long2ip( $ipint );
437 $range = "$newip/{$parts[1]}";
439 return $range;