Core changes for AbuseFilter extension.
[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->mReason = wfMsgForContent( 'autoblocker', $this->mAddress, $this->mReason );
536 $ipblock->mTimestamp = wfTimestampNow();
537 $ipblock->mAuto = 1;
538 $ipblock->mCreateAccount = $this->mCreateAccount;
539 # Continue suppressing the name if needed
540 $ipblock->mHideName = $this->mHideName;
541
542 # If the user is already blocked with an expiry date, we don't
543 # want to pile on top of that!
544 if($this->mExpiry) {
545 $ipblock->mExpiry = min ( $this->mExpiry, Block::getAutoblockExpiry( $this->mTimestamp ));
546 } else {
547 $ipblock->mExpiry = Block::getAutoblockExpiry( $this->mTimestamp );
548 }
549 # Insert it
550 return $ipblock->insert();
551 }
552
553 function deleteIfExpired()
554 {
555 $fname = 'Block::deleteIfExpired';
556 wfProfileIn( $fname );
557 if ( $this->isExpired() ) {
558 wfDebug( "Block::deleteIfExpired() -- deleting\n" );
559 $this->delete();
560 $retVal = true;
561 } else {
562 wfDebug( "Block::deleteIfExpired() -- not expired\n" );
563 $retVal = false;
564 }
565 wfProfileOut( $fname );
566 return $retVal;
567 }
568
569 function isExpired()
570 {
571 wfDebug( "Block::isExpired() checking current " . wfTimestampNow() . " vs $this->mExpiry\n" );
572 if ( !$this->mExpiry ) {
573 return false;
574 } else {
575 return wfTimestampNow() > $this->mExpiry;
576 }
577 }
578
579 function isValid()
580 {
581 return $this->mAddress != '';
582 }
583
584 function updateTimestamp()
585 {
586 if ( $this->mAuto ) {
587 $this->mTimestamp = wfTimestamp();
588 $this->mExpiry = Block::getAutoblockExpiry( $this->mTimestamp );
589
590 $dbw = wfGetDB( DB_MASTER );
591 $dbw->update( 'ipblocks',
592 array( /* SET */
593 'ipb_timestamp' => $dbw->timestamp($this->mTimestamp),
594 'ipb_expiry' => $dbw->timestamp($this->mExpiry),
595 ), array( /* WHERE */
596 'ipb_address' => $this->mAddress
597 ), 'Block::updateTimestamp'
598 );
599 }
600 }
601
602 /*
603 function getIntegerAddr()
604 {
605 return $this->mIntegerAddr;
606 }
607
608 function getNetworkBits()
609 {
610 return $this->mNetworkBits;
611 }*/
612
613 /**
614 * @return The blocker user ID.
615 */
616 public function getBy() {
617 return $this->mBy;
618 }
619
620 /**
621 * @return The blocker user name.
622 */
623 function getByName()
624 {
625 return $this->mByName;
626 }
627
628 function forUpdate( $x = NULL ) {
629 return wfSetVar( $this->mForUpdate, $x );
630 }
631
632 function fromMaster( $x = NULL ) {
633 return wfSetVar( $this->mFromMaster, $x );
634 }
635
636 function getRedactedName() {
637 if ( $this->mAuto ) {
638 return '#' . $this->mId;
639 } else {
640 return $this->mAddress;
641 }
642 }
643
644 /**
645 * Encode expiry for DB
646 */
647 static function encodeExpiry( $expiry, $db ) {
648 if ( $expiry == '' || $expiry == Block::infinity() ) {
649 return Block::infinity();
650 } else {
651 return $db->timestamp( $expiry );
652 }
653 }
654
655 /**
656 * Decode expiry which has come from the DB
657 */
658 static function decodeExpiry( $expiry, $timestampType = TS_MW ) {
659 if ( $expiry == '' || $expiry == Block::infinity() ) {
660 return Block::infinity();
661 } else {
662 return wfTimestamp( $timestampType, $expiry );
663 }
664 }
665
666 static function getAutoblockExpiry( $timestamp )
667 {
668 global $wgAutoblockExpiry;
669 return wfTimestamp( TS_MW, wfTimestamp( TS_UNIX, $timestamp ) + $wgAutoblockExpiry );
670 }
671
672 /**
673 * Gets rid of uneeded numbers in quad-dotted/octet IP strings
674 * For example, 127.111.113.151/24 -> 127.111.113.0/24
675 */
676 static function normaliseRange( $range ) {
677 $parts = explode( '/', $range );
678 if ( count( $parts ) == 2 ) {
679 // IPv6
680 if ( IP::isIPv6($range) && $parts[1] >= 64 && $parts[1] <= 128 ) {
681 $bits = $parts[1];
682 $ipint = IP::toUnsigned6( $parts[0] );
683 # Native 32 bit functions WONT work here!!!
684 # Convert to a padded binary number
685 $network = wfBaseConvert( $ipint, 10, 2, 128 );
686 # Truncate the last (128-$bits) bits and replace them with zeros
687 $network = str_pad( substr( $network, 0, $bits ), 128, 0, STR_PAD_RIGHT );
688 # Convert back to an integer
689 $network = wfBaseConvert( $network, 2, 10 );
690 # Reform octet address
691 $newip = IP::toOctet( $network );
692 $range = "$newip/{$parts[1]}";
693 } // IPv4
694 else if ( IP::isIPv4($range) && $parts[1] >= 16 && $parts[1] <= 32 ) {
695 $shift = 32 - $parts[1];
696 $ipint = IP::toUnsigned( $parts[0] );
697 $ipint = $ipint >> $shift << $shift;
698 $newip = long2ip( $ipint );
699 $range = "$newip/{$parts[1]}";
700 }
701 }
702 return $range;
703 }
704
705 /**
706 * Purge expired blocks from the ipblocks table
707 */
708 static function purgeExpired() {
709 $dbw = wfGetDB( DB_MASTER );
710 $dbw->delete( 'ipblocks', array( 'ipb_expiry < ' . $dbw->addQuotes( $dbw->timestamp() ) ), __METHOD__ );
711 }
712
713 static function infinity() {
714 # This is a special keyword for timestamps in PostgreSQL, and
715 # works with CHAR(14) as well because "i" sorts after all numbers.
716 return 'infinity';
717
718 /*
719 static $infinity;
720 if ( !isset( $infinity ) ) {
721 $dbr = wfGetDB( DB_SLAVE );
722 $infinity = $dbr->bigTimestamp();
723 }
724 return $infinity;
725 */
726 }
727
728 /**
729 * Convert a DB-encoded expiry into a real string that humans can read.
730 */
731 static function formatExpiry( $encoded_expiry ) {
732
733 static $msg = null;
734
735 if( is_null( $msg ) ) {
736 $msg = array();
737 $keys = array( 'infiniteblock', 'expiringblock' );
738 foreach( $keys as $key ) {
739 $msg[$key] = wfMsgHtml( $key );
740 }
741 }
742
743 $expiry = Block::decodeExpiry( $encoded_expiry );
744 if ($expiry == 'infinity') {
745 $expirystr = $msg['infiniteblock'];
746 } else {
747 global $wgLang;
748 $expiretimestr = $wgLang->timeanddate( $expiry, true );
749 $expirystr = wfMsgReplaceArgs( $msg['expiringblock'], array($expiretimestr) );
750 }
751
752 return $expirystr;
753 }
754
755 /**
756 * Convert a typed-in expiry time into something we can put into the database.
757 */
758 static function parseExpiryInput( $expiry_input ) {
759 if ( $expiry_input == 'infinite' || $expiry_input == 'indefinite' ) {
760 $expiry = 'infinity';
761 } else {
762 $expiry = strtotime( $expiry_input );
763 if ($expiry < 0 || $expiry === false) {
764 return false;
765 }
766 }
767
768 return $expiry;
769 }
770
771 }