Replacing the fix for bug 12716.
[lhc/web/wiklou.git] / includes / Block.php
1 <?php
2 /**
3 * @file
4 * Blocks and bans object
5 */
6
7 /**
8 * The block class
9 * All the functions in this class assume the object is either explicitly
10 * loaded or filled. It is not load-on-demand. There are no accessors.
11 *
12 * Globals used: $wgAutoblockExpiry, $wgAntiLockFlags
13 *
14 * @todo This could be used everywhere, but it isn't.
15 */
16 class Block
17 {
18 /* public*/ var $mAddress, $mUser, $mBy, $mReason, $mTimestamp, $mAuto, $mId, $mExpiry,
19 $mRangeStart, $mRangeEnd, $mAnonOnly, $mEnableAutoblock, $mHideName,
20 $mBlockEmail, $mByName, $mAngryAutoblock;
21 /* private */ var $mNetworkBits, $mIntegerAddr, $mForUpdate, $mFromMaster;
22
23 const EB_KEEP_EXPIRED = 1;
24 const EB_FOR_UPDATE = 2;
25 const EB_RANGE_ONLY = 4;
26
27 function __construct( $address = '', $user = 0, $by = 0, $reason = '',
28 $timestamp = '' , $auto = 0, $expiry = '', $anonOnly = 0, $createAccount = 0, $enableAutoblock = 0,
29 $hideName = 0, $blockEmail = 0 )
30 {
31 $this->mId = 0;
32 # Expand valid IPv6 addresses
33 $address = IP::sanitizeIP( $address );
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 $this->mAnonOnly = $anonOnly;
41 $this->mCreateAccount = $createAccount;
42 $this->mExpiry = self::decodeExpiry( $expiry );
43 $this->mEnableAutoblock = $enableAutoblock;
44 $this->mHideName = $hideName;
45 $this->mBlockEmail = $blockEmail;
46 $this->mForUpdate = false;
47 $this->mFromMaster = false;
48 $this->mByName = false;
49 $this->mAngryAutoblock = false;
50 $this->initialiseRange();
51 }
52
53 static function newFromDB( $address, $user = 0, $killExpired = true )
54 {
55 $block = new Block();
56 $block->load( $address, $user, $killExpired );
57 if ( $block->isValid() ) {
58 return $block;
59 } else {
60 return null;
61 }
62 }
63
64 static function newFromID( $id )
65 {
66 $dbr = wfGetDB( DB_SLAVE );
67 $res = $dbr->resultObject( $dbr->select( 'ipblocks', '*',
68 array( 'ipb_id' => $id ), __METHOD__ ) );
69 $block = new Block;
70 if ( $block->loadFromResult( $res ) ) {
71 return $block;
72 } else {
73 return null;
74 }
75 }
76
77 function clear()
78 {
79 $this->mAddress = $this->mReason = $this->mTimestamp = '';
80 $this->mId = $this->mAnonOnly = $this->mCreateAccount =
81 $this->mEnableAutoblock = $this->mAuto = $this->mUser =
82 $this->mBy = $this->mHideName = $this->mBlockEmail = 0;
83 $this->mByName = false;
84 }
85
86 /**
87 * Get the DB object and set the reference parameter to the query options
88 */
89 function &getDBOptions( &$options )
90 {
91 global $wgAntiLockFlags;
92 if ( $this->mForUpdate || $this->mFromMaster ) {
93 $db = wfGetDB( DB_MASTER );
94 if ( !$this->mForUpdate || ($wgAntiLockFlags & ALF_NO_BLOCK_LOCK) ) {
95 $options = array();
96 } else {
97 $options = array( 'FOR UPDATE' );
98 }
99 } else {
100 $db = wfGetDB( DB_SLAVE );
101 $options = array();
102 }
103 return $db;
104 }
105
106 /**
107 * Get a ban from the DB, with either the given address or the given username
108 *
109 * @param string $address The IP address of the user, or blank to skip IP blocks
110 * @param integer $user The user ID, or zero for anonymous users
111 * @param bool $killExpired Whether to delete expired rows while loading
112 *
113 */
114 function load( $address = '', $user = 0, $killExpired = true )
115 {
116 wfDebug( "Block::load: '$address', '$user', $killExpired\n" );
117
118 $options = array();
119 $db =& $this->getDBOptions( $options );
120
121 if ( 0 == $user && $address == '' ) {
122 # Invalid user specification, not blocked
123 $this->clear();
124 return false;
125 }
126
127 # Try user block
128 if ( $user ) {
129 $res = $db->resultObject( $db->select( 'ipblocks', '*', array( 'ipb_user' => $user ),
130 __METHOD__, $options ) );
131 if ( $this->loadFromResult( $res, $killExpired ) ) {
132 return true;
133 }
134 }
135
136 # Try IP block
137 # TODO: improve performance by merging this query with the autoblock one
138 # Slightly tricky while handling killExpired as well
139 if ( $address ) {
140 $conds = array( 'ipb_address' => $address, 'ipb_auto' => 0 );
141 $res = $db->resultObject( $db->select( 'ipblocks', '*', $conds, __METHOD__, $options ) );
142 if ( $this->loadFromResult( $res, $killExpired ) ) {
143 if ( $user && $this->mAnonOnly ) {
144 # Block is marked anon-only
145 # Whitelist this IP address against autoblocks and range blocks
146 $this->clear();
147 return false;
148 } else {
149 return true;
150 }
151 }
152 }
153
154 # Try range block
155 if ( $this->loadRange( $address, $killExpired, $user ) ) {
156 if ( $user && $this->mAnonOnly ) {
157 $this->clear();
158 return false;
159 } else {
160 return true;
161 }
162 }
163
164 # Try autoblock
165 if ( $address ) {
166 $conds = array( 'ipb_address' => $address, 'ipb_auto' => 1 );
167 if ( $user ) {
168 $conds['ipb_anon_only'] = 0;
169 }
170 $res = $db->resultObject( $db->select( 'ipblocks', '*', $conds, __METHOD__, $options ) );
171 if ( $this->loadFromResult( $res, $killExpired ) ) {
172 return true;
173 }
174 }
175
176 # Give up
177 $this->clear();
178 return false;
179 }
180
181 /**
182 * Fill in member variables from a result wrapper
183 */
184 function loadFromResult( ResultWrapper $res, $killExpired = true )
185 {
186 $ret = false;
187 if ( 0 != $res->numRows() ) {
188 # Get first block
189 $row = $res->fetchObject();
190 $this->initFromRow( $row );
191
192 if ( $killExpired ) {
193 # If requested, delete expired rows
194 do {
195 $killed = $this->deleteIfExpired();
196 if ( $killed ) {
197 $row = $res->fetchObject();
198 if ( $row ) {
199 $this->initFromRow( $row );
200 }
201 }
202 } while ( $killed && $row );
203
204 # If there were any left after the killing finished, return true
205 if ( $row ) {
206 $ret = true;
207 }
208 } else {
209 $ret = true;
210 }
211 }
212 $res->free();
213 return $ret;
214 }
215
216 /**
217 * Search the database for any range blocks matching the given address, and
218 * load the row if one is found.
219 */
220 function loadRange( $address, $killExpired = true, $user = 0 )
221 {
222 $iaddr = IP::toHex( $address );
223 if ( $iaddr === false ) {
224 # Invalid address
225 return false;
226 }
227
228 # Only scan ranges which start in this /16, this improves search speed
229 # Blocks should not cross a /16 boundary.
230 $range = substr( $iaddr, 0, 4 );
231
232 $options = array();
233 $db =& $this->getDBOptions( $options );
234 $conds = array(
235 "ipb_range_start LIKE '$range%'",
236 "ipb_range_start <= '$iaddr'",
237 "ipb_range_end >= '$iaddr'"
238 );
239
240 if ( $user ) {
241 $conds['ipb_anon_only'] = 0;
242 }
243
244 $res = $db->resultObject( $db->select( 'ipblocks', '*', $conds, __METHOD__, $options ) );
245 $success = $this->loadFromResult( $res, $killExpired );
246 return $success;
247 }
248
249 /**
250 * Determine if a given integer IPv4 address is in a given CIDR network
251 * @deprecated Use IP::isInRange
252 */
253 function isAddressInRange( $addr, $range ) {
254 return IP::isInRange( $addr, $range );
255 }
256
257 function initFromRow( $row )
258 {
259 $this->mAddress = $row->ipb_address;
260 $this->mReason = $row->ipb_reason;
261 $this->mTimestamp = wfTimestamp(TS_MW,$row->ipb_timestamp);
262 $this->mUser = $row->ipb_user;
263 $this->mBy = $row->ipb_by;
264 $this->mAuto = $row->ipb_auto;
265 $this->mAnonOnly = $row->ipb_anon_only;
266 $this->mCreateAccount = $row->ipb_create_account;
267 $this->mEnableAutoblock = $row->ipb_enable_autoblock;
268 $this->mBlockEmail = $row->ipb_block_email;
269 $this->mHideName = $row->ipb_deleted;
270 $this->mId = $row->ipb_id;
271 $this->mExpiry = self::decodeExpiry( $row->ipb_expiry );
272 if ( isset( $row->user_name ) ) {
273 $this->mByName = $row->user_name;
274 } else {
275 $this->mByName = $row->ipb_by_text;
276 }
277 $this->mRangeStart = $row->ipb_range_start;
278 $this->mRangeEnd = $row->ipb_range_end;
279 }
280
281 function initialiseRange()
282 {
283 $this->mRangeStart = '';
284 $this->mRangeEnd = '';
285
286 if ( $this->mUser == 0 ) {
287 list( $this->mRangeStart, $this->mRangeEnd ) = IP::parseRange( $this->mAddress );
288 }
289 }
290
291 /**
292 * Callback with a Block object for every block
293 * @return integer number of blocks;
294 */
295 /*static*/ function enumBlocks( $callback, $tag, $flags = 0 )
296 {
297 global $wgAntiLockFlags;
298
299 $block = new Block();
300 if ( $flags & Block::EB_FOR_UPDATE ) {
301 $db = wfGetDB( DB_MASTER );
302 if ( $wgAntiLockFlags & ALF_NO_BLOCK_LOCK ) {
303 $options = '';
304 } else {
305 $options = 'FOR UPDATE';
306 }
307 $block->forUpdate( true );
308 } else {
309 $db = wfGetDB( DB_SLAVE );
310 $options = '';
311 }
312 if ( $flags & Block::EB_RANGE_ONLY ) {
313 $cond = " AND ipb_range_start <> ''";
314 } else {
315 $cond = '';
316 }
317
318 $now = wfTimestampNow();
319
320 list( $ipblocks, $user ) = $db->tableNamesN( 'ipblocks', 'user' );
321
322 $sql = "SELECT $ipblocks.*,user_name FROM $ipblocks,$user " .
323 "WHERE user_id=ipb_by $cond ORDER BY ipb_timestamp DESC $options";
324 $res = $db->query( $sql, 'Block::enumBlocks' );
325 $num_rows = $db->numRows( $res );
326
327 while ( $row = $db->fetchObject( $res ) ) {
328 $block->initFromRow( $row );
329 if ( ( $flags & Block::EB_RANGE_ONLY ) && $block->mRangeStart == '' ) {
330 continue;
331 }
332
333 if ( !( $flags & Block::EB_KEEP_EXPIRED ) ) {
334 if ( $block->mExpiry && $now > $block->mExpiry ) {
335 $block->delete();
336 } else {
337 call_user_func( $callback, $block, $tag );
338 }
339 } else {
340 call_user_func( $callback, $block, $tag );
341 }
342 }
343 $db->freeResult( $res );
344 return $num_rows;
345 }
346
347 function delete()
348 {
349 if (wfReadOnly()) {
350 return false;
351 }
352 if ( !$this->mId ) {
353 throw new MWException( "Block::delete() now requires that the mId member be filled\n" );
354 }
355
356 $dbw = wfGetDB( DB_MASTER );
357 $dbw->delete( 'ipblocks', array( 'ipb_id' => $this->mId ), __METHOD__ );
358 return $dbw->affectedRows() > 0;
359 }
360
361 /**
362 * Insert a block into the block table.
363 * @return Whether or not the insertion was successful.
364 */
365 function insert()
366 {
367 wfDebug( "Block::insert; timestamp {$this->mTimestamp}\n" );
368 $dbw = wfGetDB( DB_MASTER );
369
370 # Unset ipb_anon_only for user blocks, makes no sense
371 if ( $this->mUser ) {
372 $this->mAnonOnly = 0;
373 }
374
375 # Unset ipb_enable_autoblock for IP blocks, makes no sense
376 if ( !$this->mUser ) {
377 $this->mEnableAutoblock = 0;
378 $this->mBlockEmail = 0; //Same goes for email...
379 }
380
381 if( !$this->mByName ) {
382 if( $this->mBy ) {
383 $this->mByName = User::whoIs( $this->mBy );
384 } else {
385 global $wgUser;
386 $this->mByName = $wgUser->getName();
387 }
388 }
389
390 # Don't collide with expired blocks
391 Block::purgeExpired();
392
393 $ipb_id = $dbw->nextSequenceValue('ipblocks_ipb_id_val');
394 $dbw->insert( 'ipblocks',
395 array(
396 'ipb_id' => $ipb_id,
397 'ipb_address' => $this->mAddress,
398 'ipb_user' => $this->mUser,
399 'ipb_by' => $this->mBy,
400 'ipb_by_text' => $this->mByName,
401 'ipb_reason' => $this->mReason,
402 'ipb_timestamp' => $dbw->timestamp($this->mTimestamp),
403 'ipb_auto' => $this->mAuto,
404 'ipb_anon_only' => $this->mAnonOnly,
405 'ipb_create_account' => $this->mCreateAccount,
406 'ipb_enable_autoblock' => $this->mEnableAutoblock,
407 'ipb_expiry' => self::encodeExpiry( $this->mExpiry, $dbw ),
408 'ipb_range_start' => $this->mRangeStart,
409 'ipb_range_end' => $this->mRangeEnd,
410 'ipb_deleted' => $this->mHideName,
411 'ipb_block_email' => $this->mBlockEmail
412 ), 'Block::insert', array( 'IGNORE' )
413 );
414 $affected = $dbw->affectedRows();
415
416 if ($affected)
417 $this->doRetroactiveAutoblock();
418
419 return $affected;
420 }
421
422 /**
423 * Retroactively autoblocks the last IP used by the user (if it is a user)
424 * blocked by this Block.
425 *@return Whether or not a retroactive autoblock was made.
426 */
427 function doRetroactiveAutoblock() {
428 $dbr = wfGetDB( DB_SLAVE );
429 #If autoblock is enabled, autoblock the LAST IP used
430 # - stolen shamelessly from CheckUser_body.php
431
432 if ($this->mEnableAutoblock && $this->mUser) {
433 wfDebug("Doing retroactive autoblocks for " . $this->mAddress . "\n");
434
435 $options = array( 'ORDER BY' => 'rc_timestamp DESC' );
436 $conds = array( 'rc_user_text' => $this->mAddress );
437
438 if ($this->mAngryAutoblock) {
439 // Block any IP used in the last 7 days. Up to five IPs.
440 $conds[] = 'rc_timestamp < ' . $dbr->addQuotes( $dbr->timestamp( time() - (7*86400) ) );
441 $options['LIMIT'] = 5;
442 } else {
443 // Just the last IP used.
444 $options['LIMIT'] = 1;
445 }
446
447 $res = $dbr->select( 'recentchanges', array( 'rc_ip' ), $conds,
448 __METHOD__ , $options);
449
450 if ( !$dbr->numRows( $res ) ) {
451 #No results, don't autoblock anything
452 wfDebug("No IP found to retroactively autoblock\n");
453 } else {
454 while ( $row = $dbr->fetchObject( $res ) ) {
455 if ( $row->rc_ip )
456 $this->doAutoblock( $row->rc_ip );
457 }
458 }
459 }
460 }
461
462 /**
463 * Autoblocks the given IP, referring to this Block.
464 * @param string $autoblockip The IP to autoblock.
465 * @param bool $justInserted The main block was just inserted
466 * @return bool Whether or not an autoblock was inserted.
467 */
468 function doAutoblock( $autoblockip, $justInserted = false ) {
469 # If autoblocks are disabled, go away.
470 if ( !$this->mEnableAutoblock ) {
471 return;
472 }
473
474 # Check for presence on the autoblock whitelist
475 # TODO cache this?
476 $lines = explode( "\n", wfMsgForContentNoTrans( 'autoblock_whitelist' ) );
477
478 $ip = $autoblockip;
479
480 wfDebug("Checking the autoblock whitelist..\n");
481
482 foreach( $lines as $line ) {
483 # List items only
484 if ( substr( $line, 0, 1 ) !== '*' ) {
485 continue;
486 }
487
488 $wlEntry = substr($line, 1);
489 $wlEntry = trim($wlEntry);
490
491 wfDebug("Checking $ip against $wlEntry...");
492
493 # Is the IP in this range?
494 if (IP::isInRange( $ip, $wlEntry )) {
495 wfDebug(" IP $ip matches $wlEntry, not autoblocking\n");
496 #$autoblockip = null; # Don't autoblock a whitelisted IP.
497 return; #This /SHOULD/ introduce a dummy block - but
498 # I don't know a safe way to do so. -werdna
499 } else {
500 wfDebug( " No match\n" );
501 }
502 }
503
504 ## Allow hooks to cancel the autoblock.
505 if (!wfRunHooks( 'AbortAutoblock', array( $autoblockip, &$this ) )) {
506 wfDebug( "Autoblock aborted by hook." );
507 return false;
508 }
509
510 # It's okay to autoblock. Go ahead and create/insert the block.
511
512 $ipblock = Block::newFromDB( $autoblockip );
513 if ( $ipblock ) {
514 # If the user is already blocked. Then check if the autoblock would
515 # exceed the user block. If it would exceed, then do nothing, else
516 # prolong block time
517 if ($this->mExpiry &&
518 ($this->mExpiry < Block::getAutoblockExpiry($ipblock->mTimestamp))) {
519 return;
520 }
521 # Just update the timestamp
522 if ( !$justInserted ) {
523 $ipblock->updateTimestamp();
524 }
525 return;
526 } else {
527 $ipblock = new Block;
528 }
529
530 # Make a new block object with the desired properties
531 wfDebug( "Autoblocking {$this->mAddress}@" . $autoblockip . "\n" );
532 $ipblock->mAddress = $autoblockip;
533 $ipblock->mUser = 0;
534 $ipblock->mBy = $this->mBy;
535 $ipblock->mByName = $this->mByName;
536 $ipblock->mReason = wfMsgForContent( 'autoblocker', $this->mAddress, $this->mReason );
537 $ipblock->mTimestamp = wfTimestampNow();
538 $ipblock->mAuto = 1;
539 $ipblock->mCreateAccount = $this->mCreateAccount;
540 # Continue suppressing the name if needed
541 $ipblock->mHideName = $this->mHideName;
542
543 # If the user is already blocked with an expiry date, we don't
544 # want to pile on top of that!
545 if($this->mExpiry) {
546 $ipblock->mExpiry = min ( $this->mExpiry, Block::getAutoblockExpiry( $this->mTimestamp ));
547 } else {
548 $ipblock->mExpiry = Block::getAutoblockExpiry( $this->mTimestamp );
549 }
550 # Insert it
551 return $ipblock->insert();
552 }
553
554 function deleteIfExpired()
555 {
556 $fname = 'Block::deleteIfExpired';
557 wfProfileIn( $fname );
558 if ( $this->isExpired() ) {
559 wfDebug( "Block::deleteIfExpired() -- deleting\n" );
560 $this->delete();
561 $retVal = true;
562 } else {
563 wfDebug( "Block::deleteIfExpired() -- not expired\n" );
564 $retVal = false;
565 }
566 wfProfileOut( $fname );
567 return $retVal;
568 }
569
570 function isExpired()
571 {
572 wfDebug( "Block::isExpired() checking current " . wfTimestampNow() . " vs $this->mExpiry\n" );
573 if ( !$this->mExpiry ) {
574 return false;
575 } else {
576 return wfTimestampNow() > $this->mExpiry;
577 }
578 }
579
580 function isValid()
581 {
582 return $this->mAddress != '';
583 }
584
585 function updateTimestamp()
586 {
587 if ( $this->mAuto ) {
588 $this->mTimestamp = wfTimestamp();
589 $this->mExpiry = Block::getAutoblockExpiry( $this->mTimestamp );
590
591 $dbw = wfGetDB( DB_MASTER );
592 $dbw->update( 'ipblocks',
593 array( /* SET */
594 'ipb_timestamp' => $dbw->timestamp($this->mTimestamp),
595 'ipb_expiry' => $dbw->timestamp($this->mExpiry),
596 ), array( /* WHERE */
597 'ipb_address' => $this->mAddress
598 ), 'Block::updateTimestamp'
599 );
600 }
601 }
602
603 /*
604 function getIntegerAddr()
605 {
606 return $this->mIntegerAddr;
607 }
608
609 function getNetworkBits()
610 {
611 return $this->mNetworkBits;
612 }*/
613
614 /**
615 * @return The blocker user ID.
616 */
617 public function getBy() {
618 return $this->mBy;
619 }
620
621 /**
622 * @return The blocker user name.
623 */
624 function getByName()
625 {
626 return $this->mByName;
627 }
628
629 function forUpdate( $x = NULL ) {
630 return wfSetVar( $this->mForUpdate, $x );
631 }
632
633 function fromMaster( $x = NULL ) {
634 return wfSetVar( $this->mFromMaster, $x );
635 }
636
637 function getRedactedName() {
638 if ( $this->mAuto ) {
639 return '#' . $this->mId;
640 } else {
641 return $this->mAddress;
642 }
643 }
644
645 /**
646 * Encode expiry for DB
647 */
648 static function encodeExpiry( $expiry, $db ) {
649 if ( $expiry == '' || $expiry == Block::infinity() ) {
650 return Block::infinity();
651 } else {
652 return $db->timestamp( $expiry );
653 }
654 }
655
656 /**
657 * Decode expiry which has come from the DB
658 */
659 static function decodeExpiry( $expiry, $timestampType = TS_MW ) {
660 if ( $expiry == '' || $expiry == Block::infinity() ) {
661 return Block::infinity();
662 } else {
663 return wfTimestamp( $timestampType, $expiry );
664 }
665 }
666
667 static function getAutoblockExpiry( $timestamp )
668 {
669 global $wgAutoblockExpiry;
670 return wfTimestamp( TS_MW, wfTimestamp( TS_UNIX, $timestamp ) + $wgAutoblockExpiry );
671 }
672
673 /**
674 * Gets rid of uneeded numbers in quad-dotted/octet IP strings
675 * For example, 127.111.113.151/24 -> 127.111.113.0/24
676 */
677 static function normaliseRange( $range ) {
678 $parts = explode( '/', $range );
679 if ( count( $parts ) == 2 ) {
680 // IPv6
681 if ( IP::isIPv6($range) && $parts[1] >= 64 && $parts[1] <= 128 ) {
682 $bits = $parts[1];
683 $ipint = IP::toUnsigned6( $parts[0] );
684 # Native 32 bit functions WONT work here!!!
685 # Convert to a padded binary number
686 $network = wfBaseConvert( $ipint, 10, 2, 128 );
687 # Truncate the last (128-$bits) bits and replace them with zeros
688 $network = str_pad( substr( $network, 0, $bits ), 128, 0, STR_PAD_RIGHT );
689 # Convert back to an integer
690 $network = wfBaseConvert( $network, 2, 10 );
691 # Reform octet address
692 $newip = IP::toOctet( $network );
693 $range = "$newip/{$parts[1]}";
694 } // IPv4
695 else if ( IP::isIPv4($range) && $parts[1] >= 16 && $parts[1] <= 32 ) {
696 $shift = 32 - $parts[1];
697 $ipint = IP::toUnsigned( $parts[0] );
698 $ipint = $ipint >> $shift << $shift;
699 $newip = long2ip( $ipint );
700 $range = "$newip/{$parts[1]}";
701 }
702 }
703 return $range;
704 }
705
706 /**
707 * Purge expired blocks from the ipblocks table
708 */
709 static function purgeExpired() {
710 $dbw = wfGetDB( DB_MASTER );
711 $dbw->delete( 'ipblocks', array( 'ipb_expiry < ' . $dbw->addQuotes( $dbw->timestamp() ) ), __METHOD__ );
712 }
713
714 static function infinity() {
715 # This is a special keyword for timestamps in PostgreSQL, and
716 # works with CHAR(14) as well because "i" sorts after all numbers.
717 return 'infinity';
718
719 /*
720 static $infinity;
721 if ( !isset( $infinity ) ) {
722 $dbr = wfGetDB( DB_SLAVE );
723 $infinity = $dbr->bigTimestamp();
724 }
725 return $infinity;
726 */
727 }
728
729 /**
730 * Convert a DB-encoded expiry into a real string that humans can read.
731 */
732 static function formatExpiry( $encoded_expiry ) {
733
734 static $msg = null;
735
736 if( is_null( $msg ) ) {
737 $msg = array();
738 $keys = array( 'infiniteblock', 'expiringblock' );
739 foreach( $keys as $key ) {
740 $msg[$key] = wfMsgHtml( $key );
741 }
742 }
743
744 $expiry = Block::decodeExpiry( $encoded_expiry );
745 if ($expiry == 'infinity') {
746 $expirystr = $msg['infiniteblock'];
747 } else {
748 global $wgLang;
749 $expiretimestr = $wgLang->timeanddate( $expiry, true );
750 $expirystr = wfMsgReplaceArgs( $msg['expiringblock'], array($expiretimestr) );
751 }
752
753 return $expirystr;
754 }
755
756 /**
757 * Convert a typed-in expiry time into something we can put into the database.
758 */
759 static function parseExpiryInput( $expiry_input ) {
760 if ( $expiry_input == 'infinite' || $expiry_input == 'indefinite' ) {
761 $expiry = 'infinity';
762 } else {
763 $expiry = strtotime( $expiry_input );
764 if ($expiry < 0 || $expiry === false) {
765 return false;
766 }
767 }
768
769 return $expiry;
770 }
771
772 }