4 * Blocks and bans object
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.
12 * Globals used: $wgAutoblockExpiry, $wgAntiLockFlags
14 * @todo This could be used everywhere, but it isn't.
15 * FIXME: this whole class is a cesspit, needs a complete rewrite
18 /* public*/ var $mAddress, $mUser, $mBy, $mReason, $mTimestamp, $mAuto, $mId, $mExpiry,
19 $mEnableAutoblock, $mHideName,
20 $mByName, $mAngryAutoblock;
37 function __construct( $address = '', $user = 0, $by = 0, $reason = '',
38 $timestamp = 0, $auto = 0, $expiry = '', $anonOnly = 0, $createAccount = 0, $enableAutoblock = 0,
39 $hideName = 0, $blockEmail = 0, $allowUsertalk = 0, $byName = false )
42 # Expand valid IPv6 addresses
43 $address = IP
::sanitizeIP( $address );
44 $this->mAddress
= $address;
47 $this->mReason
= $reason;
48 $this->mTimestamp
= wfTimestamp( TS_MW
, $timestamp );
50 $this->mAnonOnly
= $anonOnly;
51 $this->mCreateAccount
= $createAccount;
52 $this->mExpiry
= $expiry;
53 $this->mEnableAutoblock
= $enableAutoblock;
54 $this->mHideName
= $hideName;
55 $this->mBlockEmail
= $blockEmail;
56 $this->mAllowUsertalk
= $allowUsertalk;
57 $this->mFromMaster
= false;
58 $this->mByName
= $byName;
59 $this->mAngryAutoblock
= false;
60 $this->initialiseRange();
64 * Load a block from the database, using either the IP address or
65 * user ID. Tries the user ID first, and if that doesn't work, tries
68 * @param $address String: IP address of user/anon
69 * @param $user Integer: user id of user
70 * @param $killExpired Boolean: delete expired blocks on load
71 * @return Block Object
73 public static function newFromDB( $address, $user = 0, $killExpired = true ) {
75 $block->load( $address, $user, $killExpired );
76 if ( $block->isValid() ) {
84 * Load a blocked user from their block id.
86 * @param $id Integer: Block id to search for
87 * @return Block object
89 public static function newFromID( $id ) {
90 $dbr = wfGetDB( DB_SLAVE
);
91 $res = $dbr->resultObject( $dbr->select( 'ipblocks', '*',
92 array( 'ipb_id' => $id ), __METHOD__
) );
95 if ( $block->loadFromResult( $res ) ) {
103 * Check if two blocks are effectively equal
107 public function equals( Block
$block ) {
109 $this->mAddress
== $block->mAddress
110 && $this->mUser
== $block->mUser
111 && $this->mAuto
== $block->mAuto
112 && $this->mAnonOnly
== $block->mAnonOnly
113 && $this->mCreateAccount
== $block->mCreateAccount
114 && $this->mExpiry
== $block->mExpiry
115 && $this->mEnableAutoblock
== $block->mEnableAutoblock
116 && $this->mHideName
== $block->mHideName
117 && $this->mBlockEmail
== $block->mBlockEmail
118 && $this->mAllowUsertalk
== $block->mAllowUsertalk
119 && $this->mReason
== $block->mReason
124 * Clear all member variables in the current object. Does not clear
125 * the block from the DB.
127 public function clear() {
128 $this->mAddress
= $this->mReason
= $this->mTimestamp
= '';
129 $this->mId
= $this->mAnonOnly
= $this->mCreateAccount
=
130 $this->mEnableAutoblock
= $this->mAuto
= $this->mUser
=
131 $this->mBy
= $this->mHideName
= $this->mBlockEmail
= $this->mAllowUsertalk
= 0;
132 $this->mByName
= false;
136 * Get a block from the DB, with either the given address or the given username
138 * @param $address string The IP address of the user, or blank to skip IP blocks
139 * @param $user int The user ID, or zero for anonymous users
140 * @param $killExpired bool Whether to delete expired rows while loading
141 * @return Boolean: the user is blocked from editing
144 public function load( $address = '', $user = 0, $killExpired = true ) {
145 wfDebug( "Block::load: '$address', '$user', $killExpired\n" );
147 $db = wfGetDB( $this->mFromMaster ? DB_MASTER
: DB_SLAVE
);
149 if ( 0 == $user && $address === '' ) {
150 # Invalid user specification, not blocked
158 $res = $db->resultObject( $db->select(
161 array( 'ipb_user' => $user ),
165 if ( $this->loadFromResult( $res, $killExpired ) ) {
171 # TODO: improve performance by merging this query with the autoblock one
172 # Slightly tricky while handling killExpired as well
173 if ( $address !== '' ) {
174 $conds = array( 'ipb_address' => $address, 'ipb_auto' => 0 );
175 $res = $db->resultObject( $db->select(
182 if ( $this->loadFromResult( $res, $killExpired ) ) {
183 if ( $user && $this->mAnonOnly
) {
184 # Block is marked anon-only
185 # Whitelist this IP address against autoblocks and range blocks
186 # (but not account creation blocks -- bug 13611)
187 if ( !$this->mCreateAccount
) {
199 if ( $this->loadRange( $address, $killExpired, $user ) ) {
200 if ( $user && $this->mAnonOnly
) {
201 # Respect account creation blocks on logged-in users -- bug 13611
202 if ( !$this->mCreateAccount
) {
214 $conds = array( 'ipb_address' => $address, 'ipb_auto' => 1 );
217 $conds['ipb_anon_only'] = 0;
220 $res = $db->resultObject( $db->select(
227 if ( $this->loadFromResult( $res, $killExpired ) ) {
238 * Get a set of SQL conditions which will select rangeblocks encompasing a given range
239 * @param $start String Hexadecimal IP representation
240 * @param $end String Hexadecimal IP represenation, or null to use $start = $end
243 public static function getRangeCond( $start, $end = null ){
247 # Per bug 14634, we want to include relevant active rangeblocks; for
248 # rangeblocks, we want to include larger ranges which enclose the given
249 # range. We know that all blocks must be smaller than $wgBlockCIDRLimit,
250 # so we can improve performance by filtering on a LIKE clause
251 $chunk = self
::getIpFragment( $start );
252 $dbr = wfGetDB( DB_SLAVE
);
253 $like = $dbr->buildLike( $chunk, $dbr->anyString() );
255 # Fairly hard to make a malicious SQL statement out of hex characters,
256 # but stranger things have happened...
257 $safeStart = $dbr->addQuotes( $start );
258 $safeEnd = $dbr->addQuotes( $end );
260 return $dbr->makeList(
262 "ipb_range_start $like",
263 "ipb_range_start <= $safeStart",
264 "ipb_range_end >= $safeEnd",
271 * Get the component of an IP address which is certain to be the same between an IP
272 * address and a rangeblock containing that IP address.
273 * @param $hex String Hexadecimal IP representation
276 protected static function getIpFragment( $hex ){
277 global $wgBlockCIDRLimit;
278 if( substr( $hex, 0, 3 ) == 'v6-' ){
279 return 'v6-' . substr( substr( $hex, 3 ), 0, floor( $wgBlockCIDRLimit['IPv6'] / 4 ) );
281 return substr( $hex, 0, floor( $wgBlockCIDRLimit['IPv4'] / 4 ) );
286 * Fill in member variables from a result wrapper
288 * @param $res ResultWrapper: row from the ipblocks table
289 * @param $killExpired Boolean: whether to delete expired rows while loading
292 protected function loadFromResult( ResultWrapper
$res, $killExpired = true ) {
295 if ( 0 != $res->numRows() ) {
297 $row = $res->fetchObject();
298 $this->initFromRow( $row );
300 if ( $killExpired ) {
301 # If requested, delete expired rows
303 $killed = $this->deleteIfExpired();
305 $row = $res->fetchObject();
307 $this->initFromRow( $row );
310 } while ( $killed && $row );
312 # If there were any left after the killing finished, return true
326 * Search the database for any range blocks matching the given address, and
327 * load the row if one is found.
329 * @param $address String: IP address range
330 * @param $killExpired Boolean: whether to delete expired rows while loading
331 * @param $user Integer: if not 0, then sets ipb_anon_only
334 protected function loadRange( $address, $killExpired = true, $user = 0 ) {
335 $iaddr = IP
::toHex( $address );
337 if ( $iaddr === false ) {
342 # Only scan ranges which start in this /16, this improves search speed
343 # Blocks should not cross a /16 boundary.
344 $range = substr( $iaddr, 0, 4 );
346 $db = wfGetDB( $this->mFromMaster ? DB_MASTER
: DB_SLAVE
);
348 'ipb_range_start' . $db->buildLike( $range, $db->anyString() ),
349 "ipb_range_start <= '$iaddr'",
350 "ipb_range_end >= '$iaddr'"
354 $conds['ipb_anon_only'] = 0;
357 $res = $db->resultObject( $db->select( 'ipblocks', '*', $conds, __METHOD__
) );
358 $success = $this->loadFromResult( $res, $killExpired );
364 * Given a database row from the ipblocks table, initialize
367 * @param $row ResultWrapper: a row from the ipblocks table
369 protected function initFromRow( $row ) {
370 $this->mAddress
= $row->ipb_address
;
371 $this->mReason
= $row->ipb_reason
;
372 $this->mTimestamp
= wfTimestamp( TS_MW
, $row->ipb_timestamp
);
373 $this->mUser
= $row->ipb_user
;
374 $this->mBy
= $row->ipb_by
;
375 $this->mAuto
= $row->ipb_auto
;
376 $this->mAnonOnly
= $row->ipb_anon_only
;
377 $this->mCreateAccount
= $row->ipb_create_account
;
378 $this->mEnableAutoblock
= $row->ipb_enable_autoblock
;
379 $this->mBlockEmail
= $row->ipb_block_email
;
380 $this->mAllowUsertalk
= $row->ipb_allow_usertalk
;
381 $this->mHideName
= $row->ipb_deleted
;
382 $this->mId
= $row->ipb_id
;
383 $this->mExpiry
= $row->ipb_expiry
;
385 if ( isset( $row->user_name
) ) {
386 $this->mByName
= $row->user_name
;
388 $this->mByName
= $row->ipb_by_text
;
391 $this->mRangeStart
= $row->ipb_range_start
;
392 $this->mRangeEnd
= $row->ipb_range_end
;
396 * Once $mAddress has been set, get the range they came from.
397 * Wrapper for IP::parseRange
399 protected function initialiseRange() {
400 $this->mRangeStart
= '';
401 $this->mRangeEnd
= '';
403 if ( $this->mUser
== 0 ) {
404 list( $this->mRangeStart
, $this->mRangeEnd
) = IP
::parseRange( $this->mAddress
);
409 * Delete the row from the IP blocks table.
413 public function delete() {
414 if ( wfReadOnly() ) {
419 throw new MWException( "Block::delete() now requires that the mId member be filled\n" );
422 $dbw = wfGetDB( DB_MASTER
);
423 $dbw->delete( 'ipblocks', array( 'ipb_id' => $this->mId
), __METHOD__
);
425 return $dbw->affectedRows() > 0;
429 * Insert a block into the block table. Will fail if there is a conflicting
430 * block (same name and options) already in the database.
432 * @return Boolean: whether or not the insertion was successful.
434 public function insert( $dbw = null ) {
435 wfDebug( "Block::insert; timestamp {$this->mTimestamp}\n" );
438 $dbw = wfGetDB( DB_MASTER
);
440 $this->validateBlockParams();
441 $this->initialiseRange();
443 # Don't collide with expired blocks
444 Block
::purgeExpired();
446 $ipb_id = $dbw->nextSequenceValue( 'ipblocks_ipb_id_seq' );
451 'ipb_address' => $this->mAddress
,
452 'ipb_user' => $this->mUser
,
453 'ipb_by' => $this->mBy
,
454 'ipb_by_text' => $this->mByName
,
455 'ipb_reason' => $this->mReason
,
456 'ipb_timestamp' => $dbw->timestamp( $this->mTimestamp
),
457 'ipb_auto' => $this->mAuto
,
458 'ipb_anon_only' => $this->mAnonOnly
,
459 'ipb_create_account' => $this->mCreateAccount
,
460 'ipb_enable_autoblock' => $this->mEnableAutoblock
,
461 'ipb_expiry' => $dbw->encodeExpiry( $this->mExpiry
),
462 'ipb_range_start' => $this->mRangeStart
,
463 'ipb_range_end' => $this->mRangeEnd
,
464 'ipb_deleted' => intval( $this->mHideName
), // typecast required for SQLite
465 'ipb_block_email' => $this->mBlockEmail
,
466 'ipb_allow_usertalk' => $this->mAllowUsertalk
471 $affected = $dbw->affectedRows();
474 $this->doRetroactiveAutoblock();
476 return (bool)$affected;
480 * Update a block in the DB with new parameters.
481 * The ID field needs to be loaded first.
483 public function update() {
484 wfDebug( "Block::update; timestamp {$this->mTimestamp}\n" );
485 $dbw = wfGetDB( DB_MASTER
);
487 $this->validateBlockParams();
492 'ipb_user' => $this->mUser
,
493 'ipb_by' => $this->mBy
,
494 'ipb_by_text' => $this->mByName
,
495 'ipb_reason' => $this->mReason
,
496 'ipb_timestamp' => $dbw->timestamp( $this->mTimestamp
),
497 'ipb_auto' => $this->mAuto
,
498 'ipb_anon_only' => $this->mAnonOnly
,
499 'ipb_create_account' => $this->mCreateAccount
,
500 'ipb_enable_autoblock' => $this->mEnableAutoblock
,
501 'ipb_expiry' => $dbw->encodeExpiry( $this->mExpiry
),
502 'ipb_range_start' => $this->mRangeStart
,
503 'ipb_range_end' => $this->mRangeEnd
,
504 'ipb_deleted' => $this->mHideName
,
505 'ipb_block_email' => $this->mBlockEmail
,
506 'ipb_allow_usertalk' => $this->mAllowUsertalk
508 array( 'ipb_id' => $this->mId
),
512 return $dbw->affectedRows();
516 * Make sure all the proper members are set to sane values
517 * before adding/updating a block
519 protected function validateBlockParams() {
520 # Unset ipb_anon_only for user blocks, makes no sense
521 if ( $this->mUser
) {
522 $this->mAnonOnly
= 0;
525 # Unset ipb_enable_autoblock for IP blocks, makes no sense
526 if ( !$this->mUser
) {
527 $this->mEnableAutoblock
= 0;
530 # bug 18860: non-anon-only IP blocks should be allowed to block email
531 if ( !$this->mUser
&& $this->mAnonOnly
) {
532 $this->mBlockEmail
= 0;
535 if ( !$this->mByName
) {
537 $this->mByName
= User
::whoIs( $this->mBy
);
540 $this->mByName
= $wgUser->getName();
546 * Retroactively autoblocks the last IP used by the user (if it is a user)
547 * blocked by this Block.
549 * @return Boolean: whether or not a retroactive autoblock was made.
551 protected function doRetroactiveAutoblock() {
552 $dbr = wfGetDB( DB_SLAVE
);
553 # If autoblock is enabled, autoblock the LAST IP used
554 # - stolen shamelessly from CheckUser_body.php
556 if ( $this->mEnableAutoblock
&& $this->mUser
) {
557 wfDebug( "Doing retroactive autoblocks for " . $this->mAddress
. "\n" );
559 $options = array( 'ORDER BY' => 'rc_timestamp DESC' );
560 $conds = array( 'rc_user_text' => $this->mAddress
);
562 if ( $this->mAngryAutoblock
) {
563 // Block any IP used in the last 7 days. Up to five IPs.
564 $conds[] = 'rc_timestamp < ' . $dbr->addQuotes( $dbr->timestamp( time() - ( 7 * 86400 ) ) );
565 $options['LIMIT'] = 5;
567 // Just the last IP used.
568 $options['LIMIT'] = 1;
571 $res = $dbr->select( 'recentchanges', array( 'rc_ip' ), $conds,
572 __METHOD__
, $options );
574 if ( !$dbr->numRows( $res ) ) {
575 # No results, don't autoblock anything
576 wfDebug( "No IP found to retroactively autoblock\n" );
578 foreach ( $res as $row ) {
580 $this->doAutoblock( $row->rc_ip
);
588 * Checks whether a given IP is on the autoblock whitelist.
590 * @param $ip String: The IP to check
593 public static function isWhitelistedFromAutoblocks( $ip ) {
596 // Try to get the autoblock_whitelist from the cache, as it's faster
597 // than getting the msg raw and explode()'ing it.
598 $key = wfMemcKey( 'ipb', 'autoblock', 'whitelist' );
599 $lines = $wgMemc->get( $key );
601 $lines = explode( "\n", wfMsgForContentNoTrans( 'autoblock_whitelist' ) );
602 $wgMemc->set( $key, $lines, 3600 * 24 );
605 wfDebug( "Checking the autoblock whitelist..\n" );
607 foreach ( $lines as $line ) {
609 if ( substr( $line, 0, 1 ) !== '*' ) {
613 $wlEntry = substr( $line, 1 );
614 $wlEntry = trim( $wlEntry );
616 wfDebug( "Checking $ip against $wlEntry..." );
618 # Is the IP in this range?
619 if ( IP
::isInRange( $ip, $wlEntry ) ) {
620 wfDebug( " IP $ip matches $wlEntry, not autoblocking\n" );
623 wfDebug( " No match\n" );
631 * Autoblocks the given IP, referring to this Block.
633 * @param $autoblockIP String: the IP to autoblock.
634 * @param $justInserted Boolean: the main block was just inserted
635 * @return Boolean: whether or not an autoblock was inserted.
637 public function doAutoblock( $autoblockIP, $justInserted = false ) {
638 # If autoblocks are disabled, go away.
639 if ( !$this->mEnableAutoblock
) {
643 # Check for presence on the autoblock whitelist
644 if ( Block
::isWhitelistedFromAutoblocks( $autoblockIP ) ) {
648 # # Allow hooks to cancel the autoblock.
649 if ( !wfRunHooks( 'AbortAutoblock', array( $autoblockIP, &$this ) ) ) {
650 wfDebug( "Autoblock aborted by hook.\n" );
654 # It's okay to autoblock. Go ahead and create/insert the block.
656 $ipblock = Block
::newFromDB( $autoblockIP );
658 # If the user is already blocked. Then check if the autoblock would
659 # exceed the user block. If it would exceed, then do nothing, else
661 if ( $this->mExpiry
&&
662 ( $this->mExpiry
< Block
::getAutoblockExpiry( $ipblock->mTimestamp
) )
667 # Just update the timestamp
668 if ( !$justInserted ) {
669 $ipblock->updateTimestamp();
674 $ipblock = new Block
;
677 # Make a new block object with the desired properties
678 wfDebug( "Autoblocking {$this->mAddress}@" . $autoblockIP . "\n" );
679 $ipblock->mAddress
= $autoblockIP;
681 $ipblock->mBy
= $this->mBy
;
682 $ipblock->mByName
= $this->mByName
;
683 $ipblock->mReason
= wfMsgForContent( 'autoblocker', $this->mAddress
, $this->mReason
);
684 $ipblock->mTimestamp
= wfTimestampNow();
686 $ipblock->mCreateAccount
= $this->mCreateAccount
;
687 # Continue suppressing the name if needed
688 $ipblock->mHideName
= $this->mHideName
;
689 $ipblock->mAllowUsertalk
= $this->mAllowUsertalk
;
691 # If the user is already blocked with an expiry date, we don't
692 # want to pile on top of that!
693 if ( $this->mExpiry
) {
694 $ipblock->mExpiry
= min( $this->mExpiry
, Block
::getAutoblockExpiry( $this->mTimestamp
) );
696 $ipblock->mExpiry
= Block
::getAutoblockExpiry( $this->mTimestamp
);
700 return $ipblock->insert();
704 * Check if a block has expired. Delete it if it is.
707 public function deleteIfExpired() {
708 wfProfileIn( __METHOD__
);
710 if ( $this->isExpired() ) {
711 wfDebug( "Block::deleteIfExpired() -- deleting\n" );
715 wfDebug( "Block::deleteIfExpired() -- not expired\n" );
719 wfProfileOut( __METHOD__
);
724 * Has the block expired?
727 public function isExpired() {
728 wfDebug( "Block::isExpired() checking current " . wfTimestampNow() . " vs $this->mExpiry\n" );
730 if ( !$this->mExpiry
) {
733 return wfTimestampNow() > $this->mExpiry
;
738 * Is the block address valid (i.e. not a null string?)
741 public function isValid() {
742 return $this->mAddress
!= '';
746 * Update the timestamp on autoblocks.
748 public function updateTimestamp() {
749 if ( $this->mAuto
) {
750 $this->mTimestamp
= wfTimestamp();
751 $this->mExpiry
= Block
::getAutoblockExpiry( $this->mTimestamp
);
753 $dbw = wfGetDB( DB_MASTER
);
754 $dbw->update( 'ipblocks',
756 'ipb_timestamp' => $dbw->timestamp( $this->mTimestamp
),
757 'ipb_expiry' => $dbw->timestamp( $this->mExpiry
),
758 ), array( /* WHERE */
759 'ipb_address' => $this->mAddress
760 ), 'Block::updateTimestamp'
766 * Get the IP address at the start of the range in Hex form
767 * @return String IP in Hex form
769 public function getRangeStart(){
770 switch( $this->type
){
771 case self
::TYPE_USER
:
774 return IP
::toHex( $this->target
);
775 case self
::TYPE_RANGE
:
776 return $this->mRangeStart
;
777 default: throw new MWException( "Block with invalid type" );
782 * Get the IP address at the start of the range in Hex form
783 * @return String IP in Hex form
785 public function getRangeEnd(){
786 switch( $this->type
){
787 case self
::TYPE_USER
:
790 return IP
::toHex( $this->target
);
791 case self
::TYPE_RANGE
:
792 return $this->mRangeEnd
;
793 default: throw new MWException( "Block with invalid type" );
798 * Get the user id of the blocking sysop
802 public function getBy() {
807 * Get the username of the blocking sysop
811 public function getByName() {
812 return $this->mByName
;
816 * Get/set the SELECT ... FOR UPDATE flag
817 * @deprecated since 1.18
819 public function forUpdate( $x = null ) {
824 * Get/set a flag determining whether the master is used for reads
826 public function fromMaster( $x = null ) {
827 return wfSetVar( $this->mFromMaster
, $x );
831 * Get/set whether the Block is a hardblock (affects logged-in users on a given IP/range
835 public function isHardblock( $x = null ){
836 $y = $this->mAnonOnly
;
838 $this->mAnonOnly
= !$x;
844 * Get/set whether the Block prevents a given action
845 * @param $action String
849 public function prevents( $action, $x = null ){
852 # TODO Not actually quite this simple (bug 13611 etc)
855 case 'createaccount':
856 return wfSetVar( $this->mCreateAccount
, $x );
859 return wfSetVar( $this->mBlockEmail
, $x );
862 $y = $this->mAllowUsertalk
;
864 $this->mAllowUsertalk
= !$x;
874 * Get the block name, but with autoblocked IPs hidden as per standard privacy policy
875 * @return String, text is escaped
877 public function getRedactedName() {
878 if ( $this->mAuto
) {
879 return HTML
::rawElement(
881 array( 'class' => 'mw-autoblockid' ),
882 wfMessage( 'autoblockid', $this->mId
)
885 return htmlspecialchars( $this->mAddress
);
890 * Encode expiry for DB
892 * @param $expiry String: timestamp for expiry, or
893 * @param $db Database object
895 * @deprecated since 1.18; use $dbw->encodeExpiry() instead
897 public static function encodeExpiry( $expiry, $db ) {
898 return $db->encodeExpiry( $expiry );
902 * Decode expiry which has come from the DB
904 * @param $expiry String: Database expiry format
905 * @param $timestampType Requested timestamp format
907 * @deprecated since 1.18; use $wgLang->decodeExpiry() instead
909 public static function decodeExpiry( $expiry, $timestampType = TS_MW
) {
911 return $wgContLang->formatExpiry( $expiry, $timestampType );
915 * Get a timestamp of the expiry for autoblocks
919 public static function getAutoblockExpiry( $timestamp ) {
920 global $wgAutoblockExpiry;
922 return wfTimestamp( TS_MW
, wfTimestamp( TS_UNIX
, $timestamp ) +
$wgAutoblockExpiry );
926 * Gets rid of uneeded numbers in quad-dotted/octet IP strings
927 * For example, 127.111.113.151/24 -> 127.111.113.0/24
928 * @param $range String: IP address to normalize
930 * @deprecated since 1.18, call IP::sanitizeRange() directly
932 public static function normaliseRange( $range ) {
933 return IP
::sanitizeRange( $range );
937 * Purge expired blocks from the ipblocks table
939 public static function purgeExpired() {
940 $dbw = wfGetDB( DB_MASTER
);
941 $dbw->delete( 'ipblocks', array( 'ipb_expiry < ' . $dbw->addQuotes( $dbw->timestamp() ) ), __METHOD__
);
945 * Get a value to insert into expiry field of the database when infinite expiry
947 * @deprecated since 1.18, call $dbr->getInfinity() directly
950 public static function infinity() {
951 return wfGetDB( DB_SLAVE
)->getInfinity();
955 * Convert a DB-encoded expiry into a real string that humans can read.
957 * @param $encoded_expiry String: Database encoded expiry time
958 * @return Html-escaped String
959 * @deprecated since 1.18; use $wgLang->formatExpiry() instead
961 public static function formatExpiry( $encoded_expiry ) {
965 if ( is_null( $msg ) ) {
967 $keys = array( 'infiniteblock', 'expiringblock' );
969 foreach ( $keys as $key ) {
970 $msg[$key] = wfMsgHtml( $key );
974 $expiry = $wgContLang->formatExpiry( $encoded_expiry, TS_MW
);
975 if ( $expiry == wfGetDB( DB_SLAVE
)->getInfinity() ) {
976 $expirystr = $msg['infiniteblock'];
979 $expiredatestr = htmlspecialchars( $wgLang->date( $expiry, true ) );
980 $expiretimestr = htmlspecialchars( $wgLang->time( $expiry, true ) );
981 $expirystr = wfMsgReplaceArgs( $msg['expiringblock'], array( $expiredatestr, $expiretimestr ) );
987 # FIXME: everything above here is a mess, needs much cleaning up
990 * Convert a submitted expiry time, which may be relative ("2 weeks", etc) or absolute
991 * ("24 May 2034"), into an absolute timestamp we can put into the database.
992 * @param $expiry String: whatever was typed into the form
993 * @return String: timestamp or "infinity" string for th DB implementation
994 * @deprecated since 1.18 moved to SpecialBlock::parseExpiryInput()
996 public static function parseExpiryInput( $expiry ) {
997 wfDeprecated( __METHOD__
);
998 return SpecialBlock
::parseExpiryInput( $expiry );
1002 * Given a target and the target's type, get an existing Block object if possible.
1003 * Note that passing an IP address will get an applicable rangeblock if the IP is
1004 * not individually blocked but falls within that range
1005 * TODO: check that that fallback handles nested rangeblocks nicely (should return
1007 * @param $target String|User|Int a block target, which may be one of several types:
1008 * * A user to block, in which case $target will be a User
1009 * * An IP to block, in which case $target will be a User generated by using
1010 * User::newFromName( $ip, false ) to turn off name validation
1011 * * An IP range, in which case $target will be a String "123.123.123.123/18" etc
1012 * * The ID of an existing block, in which case $target will be an Int
1013 * @param $type Block::TYPE_ constant the type of block as described above
1014 * @return Block|null (null if the target is not blocked)
1016 public static function newFromTargetAndType( $target, $type ){
1017 if( $target instanceof User
){
1018 if( $type == Block
::TYPE_IP
){
1019 return Block
::newFromDB( $target->getName(), 0 );
1020 } elseif( $type == Block
::TYPE_USER
) {
1021 return Block
::newFromDB( '', $target->getId() );
1023 # Should be unreachable;
1027 } elseif( $type == Block
::TYPE_RANGE
){
1028 return Block
::newFromDB( $target, 0 );
1030 } elseif( $type == Block
::TYPE_ID ||
$type == Block
::TYPE_AUTO
){
1031 return Block
::newFromID( $target );
1038 public static function newFromTarget( $target ){
1039 list( $target, $type ) = self
::parseTarget( $target );
1040 return self
::newFromTargetAndType( $target, $type );
1044 * From an existing Block, get the target and the type of target. Note that it is
1045 * always safe to treat the target as a string; for User objects this will return
1046 * User::__toString() which in turn gives User::getName().
1047 * @return array( User|String, Block::TYPE_ constant )
1049 public static function parseTarget( $target ){
1050 $target = trim( $target );
1052 $userObj = User
::newFromName( $target );
1053 if( $userObj instanceof User
){
1054 # Note that since numbers are valid usernames, a $target of "12345" will be
1055 # considered a User. If you want to pass a block ID, prepend a hash "#12345",
1056 # since hash characters are not valid in usernames or titles generally.
1057 return array( $userObj, Block
::TYPE_USER
);
1059 } elseif( IP
::isValid( $target ) ){
1060 # We can still create a User if it's an IP address, but we need to turn
1061 # off validation checking (which would exclude IP addresses)
1063 User
::newFromName( IP
::sanitizeIP( $target ), false ),
1067 } elseif( IP
::isValidBlock( $target ) ){
1068 # Can't create a User from an IP range
1069 return array( Block
::normaliseRange( $target ), Block
::TYPE_RANGE
);
1071 } elseif( preg_match( '/^#\d+$/', $target ) ){
1072 # Autoblock reference in the form "#12345"
1073 return array( substr( $target, 1 ), Block
::TYPE_AUTO
);
1077 return array( null, null );
1082 * Get the target and target type for this particular Block. Note that for autoblocks,
1083 * this returns the unredacted name; frontend functions need to call $block->getRedactedName()
1084 * in this situation.
1085 * @return array( User|String, Block::TYPE_ constant )
1086 * FIXME: this should be an integral part of the Block member variables
1088 public function getTargetAndType(){
1089 list( $target, $type ) = self
::parseTarget( $this->mAddress
);
1091 # Check whether it's an autoblock
1093 $type = self
::TYPE_AUTO
;
1096 return array( $target, $type );
1099 public function getType(){
1100 list( /*...*/, $type ) = $this->getTargetAndType();
1104 public function getTarget(){
1105 list( $target, /*...*/ ) = $this->getTargetAndType();