2004-02-18 02:15:00 +00:00
|
|
|
<?php
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
|
|
|
|
* Blocks and bans object
|
2005-08-02 13:35:19 +00:00
|
|
|
*
|
2011-03-22 17:18:15 +00:00
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
2004-09-03 23:00:01 +00:00
|
|
|
*
|
2011-03-22 17:18:15 +00:00
|
|
|
* @file
|
2004-09-02 23:28:24 +00:00
|
|
|
*/
|
2016-09-22 02:52:06 +00:00
|
|
|
|
|
|
|
|
use MediaWiki\MediaWikiServices;
|
|
|
|
|
|
2008-09-22 04:52:51 +00:00
|
|
|
class Block {
|
2014-05-12 14:42:51 +00:00
|
|
|
/** @var string */
|
|
|
|
|
public $mReason;
|
2011-06-02 19:32:45 +00:00
|
|
|
|
2014-05-22 14:45:46 +00:00
|
|
|
/** @var string */
|
2014-05-12 14:42:51 +00:00
|
|
|
public $mTimestamp;
|
2011-03-22 17:18:15 +00:00
|
|
|
|
2014-05-22 14:45:46 +00:00
|
|
|
/** @var bool */
|
2014-05-12 14:42:51 +00:00
|
|
|
public $mAuto;
|
|
|
|
|
|
2014-05-22 14:45:46 +00:00
|
|
|
/** @var string */
|
2014-05-12 14:42:51 +00:00
|
|
|
public $mExpiry;
|
|
|
|
|
|
2014-05-22 14:45:46 +00:00
|
|
|
/** @var bool */
|
2014-05-12 14:42:51 +00:00
|
|
|
public $mHideName;
|
|
|
|
|
|
|
|
|
|
/** @var int */
|
|
|
|
|
public $mParentBlockId;
|
|
|
|
|
|
|
|
|
|
/** @var int */
|
|
|
|
|
protected $mId;
|
|
|
|
|
|
|
|
|
|
/** @var bool */
|
|
|
|
|
protected $mFromMaster;
|
|
|
|
|
|
|
|
|
|
/** @var bool */
|
|
|
|
|
protected $mBlockEmail;
|
|
|
|
|
|
|
|
|
|
/** @var bool */
|
|
|
|
|
protected $mDisableUsertalk;
|
|
|
|
|
|
|
|
|
|
/** @var bool */
|
|
|
|
|
protected $mCreateAccount;
|
2011-03-19 23:47:08 +00:00
|
|
|
|
2014-04-23 09:25:56 +00:00
|
|
|
/** @var User|string */
|
2011-03-21 19:12:41 +00:00
|
|
|
protected $target;
|
|
|
|
|
|
2014-04-23 09:25:56 +00:00
|
|
|
/** @var int Hack for foreign blocking (CentralAuth) */
|
2012-04-10 00:12:06 +00:00
|
|
|
protected $forcedTargetID;
|
|
|
|
|
|
2014-04-23 09:25:56 +00:00
|
|
|
/** @var int Block::TYPE_ constant. Can only be USER, IP or RANGE internally */
|
2011-03-21 19:12:41 +00:00
|
|
|
protected $type;
|
|
|
|
|
|
2014-04-06 18:02:32 +00:00
|
|
|
/** @var User */
|
2011-03-21 19:12:41 +00:00
|
|
|
protected $blocker;
|
|
|
|
|
|
2014-04-23 09:25:56 +00:00
|
|
|
/** @var bool */
|
2014-05-22 14:45:46 +00:00
|
|
|
protected $isHardblock;
|
2011-03-22 17:18:15 +00:00
|
|
|
|
2014-04-23 09:25:56 +00:00
|
|
|
/** @var bool */
|
2014-05-22 14:45:46 +00:00
|
|
|
protected $isAutoblocking;
|
2011-03-22 17:18:15 +00:00
|
|
|
|
2011-03-20 17:43:17 +00:00
|
|
|
# TYPE constants
|
2011-03-12 21:54:35 +00:00
|
|
|
const TYPE_USER = 1;
|
|
|
|
|
const TYPE_IP = 2;
|
|
|
|
|
const TYPE_RANGE = 3;
|
2011-03-13 14:47:34 +00:00
|
|
|
const TYPE_AUTO = 4;
|
|
|
|
|
const TYPE_ID = 5;
|
2011-03-12 21:54:35 +00:00
|
|
|
|
2011-03-21 19:12:41 +00:00
|
|
|
/**
|
2014-05-22 14:45:46 +00:00
|
|
|
* Create a new block with specified parameters on a user, IP or IP range.
|
|
|
|
|
*
|
|
|
|
|
* @param array $options Parameters of the block:
|
|
|
|
|
* address string|User Target user name, User object, IP address or IP range
|
|
|
|
|
* user int Override target user ID (for foreign users)
|
|
|
|
|
* by int User ID of the blocker
|
|
|
|
|
* reason string Reason of the block
|
|
|
|
|
* timestamp string The time at which the block comes into effect
|
|
|
|
|
* auto bool Is this an automatic block?
|
|
|
|
|
* expiry string Timestamp of expiration of the block or 'infinity'
|
|
|
|
|
* anonOnly bool Only disallow anonymous actions
|
|
|
|
|
* createAccount bool Disallow creation of new accounts
|
|
|
|
|
* enableAutoblock bool Enable automatic blocking
|
|
|
|
|
* hideName bool Hide the target user name
|
|
|
|
|
* blockEmail bool Disallow sending emails
|
|
|
|
|
* allowUsertalk bool Allow the target to edit its own talk page
|
|
|
|
|
* byText string Username of the blocker (for foreign users)
|
|
|
|
|
*
|
|
|
|
|
* @since 1.26 accepts $options array instead of individual parameters; order
|
|
|
|
|
* of parameters above reflects the original order
|
2011-03-21 19:12:41 +00:00
|
|
|
*/
|
2016-02-17 09:09:32 +00:00
|
|
|
function __construct( $options = [] ) {
|
|
|
|
|
$defaults = [
|
2014-05-22 14:45:46 +00:00
|
|
|
'address' => '',
|
|
|
|
|
'user' => null,
|
|
|
|
|
'by' => null,
|
|
|
|
|
'reason' => '',
|
|
|
|
|
'timestamp' => '',
|
|
|
|
|
'auto' => false,
|
|
|
|
|
'expiry' => '',
|
|
|
|
|
'anonOnly' => false,
|
|
|
|
|
'createAccount' => false,
|
|
|
|
|
'enableAutoblock' => false,
|
|
|
|
|
'hideName' => false,
|
|
|
|
|
'blockEmail' => false,
|
|
|
|
|
'allowUsertalk' => false,
|
|
|
|
|
'byText' => '',
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2011-03-21 19:12:41 +00:00
|
|
|
|
2014-05-22 14:45:46 +00:00
|
|
|
if ( func_num_args() > 1 || !is_array( $options ) ) {
|
|
|
|
|
$options = array_combine(
|
|
|
|
|
array_slice( array_keys( $defaults ), 0, func_num_args() ),
|
|
|
|
|
func_get_args()
|
|
|
|
|
);
|
2015-06-19 18:16:18 +00:00
|
|
|
wfDeprecated( __METHOD__ . ' with multiple arguments', '1.26' );
|
2011-03-22 17:18:15 +00:00
|
|
|
}
|
|
|
|
|
|
2014-05-22 14:45:46 +00:00
|
|
|
$options += $defaults;
|
|
|
|
|
|
|
|
|
|
$this->setTarget( $options['address'] );
|
|
|
|
|
|
|
|
|
|
if ( $this->target instanceof User && $options['user'] ) {
|
|
|
|
|
# Needed for foreign users
|
|
|
|
|
$this->forcedTargetID = $options['user'];
|
2012-02-28 02:04:30 +00:00
|
|
|
}
|
2014-05-22 14:45:46 +00:00
|
|
|
|
|
|
|
|
if ( $options['by'] ) {
|
|
|
|
|
# Local user
|
2016-03-19 00:08:06 +00:00
|
|
|
$this->setBlocker( User::newFromId( $options['by'] ) );
|
2014-05-22 14:45:46 +00:00
|
|
|
} else {
|
|
|
|
|
# Foreign user
|
|
|
|
|
$this->setBlocker( $options['byText'] );
|
2011-11-01 00:11:53 +00:00
|
|
|
}
|
2014-05-22 14:45:46 +00:00
|
|
|
|
|
|
|
|
$this->mReason = $options['reason'];
|
|
|
|
|
$this->mTimestamp = wfTimestamp( TS_MW, $options['timestamp'] );
|
2016-09-05 19:55:19 +00:00
|
|
|
$this->mExpiry = wfGetDB( DB_REPLICA )->decodeExpiry( $options['expiry'] );
|
2014-05-22 14:45:46 +00:00
|
|
|
|
|
|
|
|
# Boolean settings
|
|
|
|
|
$this->mAuto = (bool)$options['auto'];
|
|
|
|
|
$this->mHideName = (bool)$options['hideName'];
|
|
|
|
|
$this->isHardblock( !$options['anonOnly'] );
|
|
|
|
|
$this->isAutoblocking( (bool)$options['enableAutoblock'] );
|
|
|
|
|
|
|
|
|
|
# Prevention measures
|
|
|
|
|
$this->prevents( 'sendemail', (bool)$options['blockEmail'] );
|
|
|
|
|
$this->prevents( 'editownusertalk', !$options['allowUsertalk'] );
|
|
|
|
|
$this->prevents( 'createaccount', (bool)$options['createAccount'] );
|
2011-03-22 17:18:15 +00:00
|
|
|
|
2005-12-01 10:37:47 +00:00
|
|
|
$this->mFromMaster = false;
|
2004-02-14 12:37:25 +00:00
|
|
|
}
|
2005-08-02 13:35:19 +00:00
|
|
|
|
2008-09-21 14:22:23 +00:00
|
|
|
/**
|
|
|
|
|
* Load a blocked user from their block id.
|
2008-09-22 04:52:51 +00:00
|
|
|
*
|
2014-04-23 09:25:56 +00:00
|
|
|
* @param int $id Block id to search for
|
|
|
|
|
* @return Block|null
|
2008-09-21 14:22:23 +00:00
|
|
|
*/
|
2008-11-23 09:52:29 +00:00
|
|
|
public static function newFromID( $id ) {
|
2016-09-05 19:55:19 +00:00
|
|
|
$dbr = wfGetDB( DB_REPLICA );
|
2011-03-22 17:18:15 +00:00
|
|
|
$res = $dbr->selectRow(
|
|
|
|
|
'ipblocks',
|
2012-05-25 14:50:57 +00:00
|
|
|
self::selectFields(),
|
2016-02-17 09:09:32 +00:00
|
|
|
[ 'ipb_id' => $id ],
|
2011-03-22 17:18:15 +00:00
|
|
|
__METHOD__
|
|
|
|
|
);
|
2011-06-15 19:05:25 +00:00
|
|
|
if ( $res ) {
|
2012-05-25 14:50:57 +00:00
|
|
|
return self::newFromRow( $res );
|
2011-06-15 19:05:25 +00:00
|
|
|
} else {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2003-09-01 13:13:56 +00:00
|
|
|
}
|
2009-10-19 11:15:51 +00:00
|
|
|
|
2012-06-05 22:58:54 +00:00
|
|
|
/**
|
|
|
|
|
* Return the list of ipblocks fields that should be selected to create
|
|
|
|
|
* a new block.
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public static function selectFields() {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
2012-05-25 14:50:57 +00:00
|
|
|
'ipb_id',
|
|
|
|
|
'ipb_address',
|
|
|
|
|
'ipb_by',
|
|
|
|
|
'ipb_by_text',
|
|
|
|
|
'ipb_reason',
|
|
|
|
|
'ipb_timestamp',
|
|
|
|
|
'ipb_auto',
|
|
|
|
|
'ipb_anon_only',
|
|
|
|
|
'ipb_create_account',
|
|
|
|
|
'ipb_enable_autoblock',
|
|
|
|
|
'ipb_expiry',
|
|
|
|
|
'ipb_deleted',
|
|
|
|
|
'ipb_block_email',
|
|
|
|
|
'ipb_allow_usertalk',
|
2012-06-05 22:58:54 +00:00
|
|
|
'ipb_parent_block_id',
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2012-05-25 14:50:57 +00:00
|
|
|
}
|
|
|
|
|
|
2008-11-23 09:52:29 +00:00
|
|
|
/**
|
2011-03-22 17:18:15 +00:00
|
|
|
* Check if two blocks are effectively equal. Doesn't check irrelevant things like
|
2012-10-26 16:18:59 +00:00
|
|
|
* the blocking user or the block timestamp, only things which affect the blocked user
|
2011-05-28 18:58:51 +00:00
|
|
|
*
|
2014-04-23 09:25:56 +00:00
|
|
|
* @param Block $block
|
2011-05-28 18:58:51 +00:00
|
|
|
*
|
|
|
|
|
* @return bool
|
2008-11-23 09:52:29 +00:00
|
|
|
*/
|
|
|
|
|
public function equals( Block $block ) {
|
2009-10-19 11:15:51 +00:00
|
|
|
return (
|
2011-03-22 17:18:15 +00:00
|
|
|
(string)$this->target == (string)$block->target
|
|
|
|
|
&& $this->type == $block->type
|
2008-11-23 09:52:29 +00:00
|
|
|
&& $this->mAuto == $block->mAuto
|
2011-03-22 17:18:15 +00:00
|
|
|
&& $this->isHardblock() == $block->isHardblock()
|
|
|
|
|
&& $this->prevents( 'createaccount' ) == $block->prevents( 'createaccount' )
|
2008-11-23 09:52:29 +00:00
|
|
|
&& $this->mExpiry == $block->mExpiry
|
2011-03-22 17:18:15 +00:00
|
|
|
&& $this->isAutoblocking() == $block->isAutoblocking()
|
2008-11-23 09:52:29 +00:00
|
|
|
&& $this->mHideName == $block->mHideName
|
2011-03-22 17:18:15 +00:00
|
|
|
&& $this->prevents( 'sendemail' ) == $block->prevents( 'sendemail' )
|
|
|
|
|
&& $this->prevents( 'editownusertalk' ) == $block->prevents( 'editownusertalk' )
|
2009-02-21 10:15:10 +00:00
|
|
|
&& $this->mReason == $block->mReason
|
2008-11-23 09:52:29 +00:00
|
|
|
);
|
|
|
|
|
}
|
2005-08-02 13:35:19 +00:00
|
|
|
|
2011-03-21 19:12:41 +00:00
|
|
|
/**
|
|
|
|
|
* Load a block from the database which affects the already-set $this->target:
|
|
|
|
|
* 1) A block directly on the given user or IP
|
2013-03-13 07:42:41 +00:00
|
|
|
* 2) A rangeblock encompassing the given IP (smallest first)
|
2011-03-21 19:12:41 +00:00
|
|
|
* 3) An autoblock on the given IP
|
2014-04-23 09:25:56 +00:00
|
|
|
* @param User|string $vagueTarget Also search for blocks affecting this target. Doesn't
|
2011-05-24 21:04:50 +00:00
|
|
|
* make any sense to use TYPE_AUTO / TYPE_ID here. Leave blank to skip IP lookups.
|
2012-10-07 23:35:26 +00:00
|
|
|
* @throws MWException
|
2014-04-23 09:25:56 +00:00
|
|
|
* @return bool Whether a relevant block was found
|
2011-03-21 19:12:41 +00:00
|
|
|
*/
|
|
|
|
|
protected function newLoad( $vagueTarget = null ) {
|
2016-09-05 19:55:19 +00:00
|
|
|
$db = wfGetDB( $this->mFromMaster ? DB_MASTER : DB_REPLICA );
|
2010-05-30 14:48:30 +00:00
|
|
|
|
2013-04-20 22:49:30 +00:00
|
|
|
if ( $this->type !== null ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$conds = [
|
|
|
|
|
'ipb_address' => [ (string)$this->target ],
|
|
|
|
|
];
|
2011-03-21 19:12:41 +00:00
|
|
|
} else {
|
2016-02-17 09:09:32 +00:00
|
|
|
$conds = [ 'ipb_address' => [] ];
|
2003-09-01 13:13:56 +00:00
|
|
|
}
|
|
|
|
|
|
2011-07-11 16:53:31 +00:00
|
|
|
# Be aware that the != '' check is explicit, since empty values will be
|
|
|
|
|
# passed by some callers (bug 29116)
|
2013-04-20 22:49:30 +00:00
|
|
|
if ( $vagueTarget != '' ) {
|
2011-03-21 19:12:41 +00:00
|
|
|
list( $target, $type ) = self::parseTarget( $vagueTarget );
|
2013-04-26 14:42:31 +00:00
|
|
|
switch ( $type ) {
|
2011-03-21 19:12:41 +00:00
|
|
|
case self::TYPE_USER:
|
2013-03-13 07:42:41 +00:00
|
|
|
# Slightly weird, but who are we to argue?
|
2011-03-21 19:12:41 +00:00
|
|
|
$conds['ipb_address'][] = (string)$target;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case self::TYPE_IP:
|
|
|
|
|
$conds['ipb_address'][] = (string)$target;
|
|
|
|
|
$conds[] = self::getRangeCond( IP::toHex( $target ) );
|
|
|
|
|
$conds = $db->makeList( $conds, LIST_OR );
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case self::TYPE_RANGE:
|
|
|
|
|
list( $start, $end ) = IP::parseRange( $target );
|
|
|
|
|
$conds['ipb_address'][] = (string)$target;
|
|
|
|
|
$conds[] = self::getRangeCond( $start, $end );
|
|
|
|
|
$conds = $db->makeList( $conds, LIST_OR );
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
throw new MWException( "Tried to load block with invalid type" );
|
2006-07-10 06:30:03 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-25 14:50:57 +00:00
|
|
|
$res = $db->select( 'ipblocks', self::selectFields(), $conds, __METHOD__ );
|
2010-05-30 14:48:30 +00:00
|
|
|
|
2011-03-21 19:12:41 +00:00
|
|
|
# This result could contain a block on the user, a block on the IP, and a russian-doll
|
|
|
|
|
# set of rangeblocks. We want to choose the most specific one, so keep a leader board.
|
|
|
|
|
$bestRow = null;
|
2006-07-10 06:30:03 +00:00
|
|
|
|
2011-03-21 19:12:41 +00:00
|
|
|
# Lower will be better
|
|
|
|
|
$bestBlockScore = 100;
|
2010-05-30 14:48:30 +00:00
|
|
|
|
2011-03-21 19:12:41 +00:00
|
|
|
# This is begging for $this = $bestBlock, but that's not allowed in PHP :(
|
|
|
|
|
$bestBlockPreventsEdit = null;
|
2006-07-10 06:30:03 +00:00
|
|
|
|
2013-04-20 22:49:30 +00:00
|
|
|
foreach ( $res as $row ) {
|
2012-05-25 14:50:57 +00:00
|
|
|
$block = self::newFromRow( $row );
|
2010-02-14 22:07:30 +00:00
|
|
|
|
2011-03-21 19:12:41 +00:00
|
|
|
# Don't use expired blocks
|
2015-12-30 04:29:10 +00:00
|
|
|
if ( $block->isExpired() ) {
|
2011-03-21 19:12:41 +00:00
|
|
|
continue;
|
2006-07-11 05:30:35 +00:00
|
|
|
}
|
2010-02-14 22:07:30 +00:00
|
|
|
|
2011-03-21 19:12:41 +00:00
|
|
|
# Don't use anon only blocks on users
|
2013-04-20 22:49:30 +00:00
|
|
|
if ( $this->type == self::TYPE_USER && !$block->isHardblock() ) {
|
2011-03-21 19:12:41 +00:00
|
|
|
continue;
|
|
|
|
|
}
|
2010-02-14 22:07:30 +00:00
|
|
|
|
2013-04-20 22:49:30 +00:00
|
|
|
if ( $block->getType() == self::TYPE_RANGE ) {
|
2011-03-21 19:12:41 +00:00
|
|
|
# This is the number of bits that are allowed to vary in the block, give
|
|
|
|
|
# or take some floating point errors
|
2015-11-24 23:40:00 +00:00
|
|
|
$end = Wikimedia\base_convert( $block->getRangeEnd(), 16, 10 );
|
|
|
|
|
$start = Wikimedia\base_convert( $block->getRangeStart(), 16, 10 );
|
2011-03-21 19:12:41 +00:00
|
|
|
$size = log( $end - $start + 1, 2 );
|
|
|
|
|
|
|
|
|
|
# This has the nice property that a /32 block is ranked equally with a
|
|
|
|
|
# single-IP block, which is exactly what it is...
|
2013-03-07 16:50:43 +00:00
|
|
|
$score = self::TYPE_RANGE - 1 + ( $size / 128 );
|
2011-06-02 19:32:45 +00:00
|
|
|
|
2011-03-21 19:12:41 +00:00
|
|
|
} else {
|
|
|
|
|
$score = $block->getType();
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-20 22:49:30 +00:00
|
|
|
if ( $score < $bestBlockScore ) {
|
2011-03-21 19:12:41 +00:00
|
|
|
$bestBlockScore = $score;
|
|
|
|
|
$bestRow = $row;
|
|
|
|
|
$bestBlockPreventsEdit = $block->prevents( 'edit' );
|
2006-07-11 05:30:35 +00:00
|
|
|
}
|
|
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2013-04-20 22:49:30 +00:00
|
|
|
if ( $bestRow !== null ) {
|
2011-03-21 19:12:41 +00:00
|
|
|
$this->initFromRow( $bestRow );
|
|
|
|
|
$this->prevents( 'edit', $bestBlockPreventsEdit );
|
|
|
|
|
return true;
|
|
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2006-07-10 06:30:03 +00:00
|
|
|
}
|
|
|
|
|
|
2011-03-19 23:47:08 +00:00
|
|
|
/**
|
2013-03-13 07:42:41 +00:00
|
|
|
* Get a set of SQL conditions which will select rangeblocks encompassing a given range
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $start Hexadecimal IP representation
|
2013-03-13 07:42:41 +00:00
|
|
|
* @param string $end Hexadecimal IP representation, or null to use $start = $end
|
2014-04-23 09:25:56 +00:00
|
|
|
* @return string
|
2011-03-19 23:47:08 +00:00
|
|
|
*/
|
2011-03-20 17:43:17 +00:00
|
|
|
public static function getRangeCond( $start, $end = null ) {
|
|
|
|
|
if ( $end === null ) {
|
2011-03-19 23:47:08 +00:00
|
|
|
$end = $start;
|
|
|
|
|
}
|
|
|
|
|
# Per bug 14634, we want to include relevant active rangeblocks; for
|
|
|
|
|
# rangeblocks, we want to include larger ranges which enclose the given
|
|
|
|
|
# range. We know that all blocks must be smaller than $wgBlockCIDRLimit,
|
|
|
|
|
# so we can improve performance by filtering on a LIKE clause
|
|
|
|
|
$chunk = self::getIpFragment( $start );
|
2016-09-05 19:55:19 +00:00
|
|
|
$dbr = wfGetDB( DB_REPLICA );
|
2011-03-19 23:47:08 +00:00
|
|
|
$like = $dbr->buildLike( $chunk, $dbr->anyString() );
|
|
|
|
|
|
|
|
|
|
# Fairly hard to make a malicious SQL statement out of hex characters,
|
|
|
|
|
# but stranger things have happened...
|
|
|
|
|
$safeStart = $dbr->addQuotes( $start );
|
|
|
|
|
$safeEnd = $dbr->addQuotes( $end );
|
|
|
|
|
|
|
|
|
|
return $dbr->makeList(
|
2016-02-17 09:09:32 +00:00
|
|
|
[
|
2011-03-19 23:47:08 +00:00
|
|
|
"ipb_range_start $like",
|
|
|
|
|
"ipb_range_start <= $safeStart",
|
|
|
|
|
"ipb_range_end >= $safeEnd",
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
2011-03-19 23:47:08 +00:00
|
|
|
LIST_AND
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the component of an IP address which is certain to be the same between an IP
|
|
|
|
|
* address and a rangeblock containing that IP address.
|
2014-04-23 09:25:56 +00:00
|
|
|
* @param string $hex Hexadecimal IP representation
|
|
|
|
|
* @return string
|
2011-03-19 23:47:08 +00:00
|
|
|
*/
|
2011-03-20 17:43:17 +00:00
|
|
|
protected static function getIpFragment( $hex ) {
|
2011-03-19 23:47:08 +00:00
|
|
|
global $wgBlockCIDRLimit;
|
2011-03-20 17:43:17 +00:00
|
|
|
if ( substr( $hex, 0, 3 ) == 'v6-' ) {
|
2013-01-26 18:32:03 +00:00
|
|
|
return 'v6-' . substr( substr( $hex, 3 ), 0, floor( $wgBlockCIDRLimit['IPv6'] / 4 ) );
|
2011-03-19 23:47:08 +00:00
|
|
|
} else {
|
2013-01-26 18:32:03 +00:00
|
|
|
return substr( $hex, 0, floor( $wgBlockCIDRLimit['IPv4'] / 4 ) );
|
2011-03-19 23:47:08 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2008-09-21 14:22:23 +00:00
|
|
|
/**
|
|
|
|
|
* Given a database row from the ipblocks table, initialize
|
|
|
|
|
* member variables
|
2014-07-13 03:55:43 +00:00
|
|
|
* @param stdClass $row A row from the ipblocks table
|
2008-09-21 14:22:23 +00:00
|
|
|
*/
|
2011-03-19 17:44:01 +00:00
|
|
|
protected function initFromRow( $row ) {
|
2011-03-21 19:12:41 +00:00
|
|
|
$this->setTarget( $row->ipb_address );
|
2011-11-01 00:11:53 +00:00
|
|
|
if ( $row->ipb_by ) { // local user
|
2014-12-02 18:13:06 +00:00
|
|
|
$this->setBlocker( User::newFromId( $row->ipb_by ) );
|
2011-11-01 00:11:53 +00:00
|
|
|
} else { // foreign user
|
|
|
|
|
$this->setBlocker( $row->ipb_by_text );
|
|
|
|
|
}
|
2011-07-18 22:23:42 +00:00
|
|
|
|
2003-09-01 13:13:56 +00:00
|
|
|
$this->mReason = $row->ipb_reason;
|
2010-02-14 22:07:30 +00:00
|
|
|
$this->mTimestamp = wfTimestamp( TS_MW, $row->ipb_timestamp );
|
2003-09-07 13:56:25 +00:00
|
|
|
$this->mAuto = $row->ipb_auto;
|
2007-03-14 05:24:06 +00:00
|
|
|
$this->mHideName = $row->ipb_deleted;
|
2015-05-06 15:33:08 +00:00
|
|
|
$this->mId = (int)$row->ipb_id;
|
2012-03-28 02:44:32 +00:00
|
|
|
$this->mParentBlockId = $row->ipb_parent_block_id;
|
2011-07-18 22:23:42 +00:00
|
|
|
|
2011-07-18 21:48:56 +00:00
|
|
|
// I wish I didn't have to do this
|
2016-09-05 19:55:19 +00:00
|
|
|
$this->mExpiry = wfGetDB( DB_REPLICA )->decodeExpiry( $row->ipb_expiry );
|
2011-03-22 17:18:15 +00:00
|
|
|
|
|
|
|
|
$this->isHardblock( !$row->ipb_anon_only );
|
|
|
|
|
$this->isAutoblocking( $row->ipb_enable_autoblock );
|
|
|
|
|
|
|
|
|
|
$this->prevents( 'createaccount', $row->ipb_create_account );
|
|
|
|
|
$this->prevents( 'sendemail', $row->ipb_block_email );
|
|
|
|
|
$this->prevents( 'editownusertalk', !$row->ipb_allow_usertalk );
|
2005-08-02 13:35:19 +00:00
|
|
|
}
|
2003-09-01 13:13:56 +00:00
|
|
|
|
2011-03-21 19:12:41 +00:00
|
|
|
/**
|
|
|
|
|
* Create a new Block object from a database row
|
2014-07-13 03:55:43 +00:00
|
|
|
* @param stdClass $row Row from the ipblocks table
|
2011-03-21 19:12:41 +00:00
|
|
|
* @return Block
|
|
|
|
|
*/
|
2012-12-20 15:09:25 +00:00
|
|
|
public static function newFromRow( $row ) {
|
2011-03-21 19:12:41 +00:00
|
|
|
$block = new Block;
|
|
|
|
|
$block->initFromRow( $row );
|
|
|
|
|
return $block;
|
|
|
|
|
}
|
|
|
|
|
|
2008-09-21 14:22:23 +00:00
|
|
|
/**
|
|
|
|
|
* Delete the row from the IP blocks table.
|
2008-09-22 04:52:51 +00:00
|
|
|
*
|
2012-10-07 23:35:26 +00:00
|
|
|
* @throws MWException
|
2014-04-23 09:25:56 +00:00
|
|
|
* @return bool
|
2008-09-21 14:22:23 +00:00
|
|
|
*/
|
2008-11-30 13:09:19 +00:00
|
|
|
public function delete() {
|
|
|
|
|
if ( wfReadOnly() ) {
|
2006-07-10 06:30:03 +00:00
|
|
|
return false;
|
2005-03-08 02:45:25 +00:00
|
|
|
}
|
2010-02-14 22:07:30 +00:00
|
|
|
|
2011-03-22 17:18:15 +00:00
|
|
|
if ( !$this->getId() ) {
|
|
|
|
|
throw new MWException( "Block::delete() requires that the mId member be filled\n" );
|
2003-09-07 13:56:25 +00:00
|
|
|
}
|
2006-07-10 06:30:03 +00:00
|
|
|
|
2007-01-22 23:50:42 +00:00
|
|
|
$dbw = wfGetDB( DB_MASTER );
|
2016-02-17 09:09:32 +00:00
|
|
|
$dbw->delete( 'ipblocks', [ 'ipb_parent_block_id' => $this->getId() ], __METHOD__ );
|
|
|
|
|
$dbw->delete( 'ipblocks', [ 'ipb_id' => $this->getId() ], __METHOD__ );
|
2010-05-30 14:48:30 +00:00
|
|
|
|
2006-07-10 06:30:03 +00:00
|
|
|
return $dbw->affectedRows() > 0;
|
2003-09-01 13:13:56 +00:00
|
|
|
}
|
|
|
|
|
|
2006-11-08 09:54:06 +00:00
|
|
|
/**
|
2008-09-22 04:52:51 +00:00
|
|
|
* Insert a block into the block table. Will fail if there is a conflicting
|
|
|
|
|
* block (same name and options) already in the database.
|
|
|
|
|
*
|
2015-10-04 09:07:25 +00:00
|
|
|
* @param IDatabase $dbw If you have one available
|
2014-04-23 09:25:56 +00:00
|
|
|
* @return bool|array False on failure, assoc array on success:
|
2011-03-22 11:51:09 +00:00
|
|
|
* ('id' => block ID, 'autoIds' => array of autoblock IDs)
|
2008-09-22 04:52:51 +00:00
|
|
|
*/
|
2010-04-17 21:16:06 +00:00
|
|
|
public function insert( $dbw = null ) {
|
2016-04-19 14:25:43 +00:00
|
|
|
global $wgBlockDisablesLogin;
|
2005-01-11 09:29:29 +00:00
|
|
|
wfDebug( "Block::insert; timestamp {$this->mTimestamp}\n" );
|
2010-05-30 14:48:30 +00:00
|
|
|
|
2011-03-22 11:51:09 +00:00
|
|
|
if ( $dbw === null ) {
|
2010-04-17 20:59:05 +00:00
|
|
|
$dbw = wfGetDB( DB_MASTER );
|
2011-03-22 11:51:09 +00:00
|
|
|
}
|
2006-07-10 06:30:03 +00:00
|
|
|
|
2015-04-06 22:13:07 +00:00
|
|
|
# Periodic purge via commit hooks
|
|
|
|
|
if ( mt_rand( 0, 9 ) == 0 ) {
|
|
|
|
|
Block::purgeExpired();
|
|
|
|
|
}
|
2006-11-22 11:51:49 +00:00
|
|
|
|
2011-11-04 11:35:10 +00:00
|
|
|
$row = $this->getDatabaseArray();
|
2013-02-03 20:05:24 +00:00
|
|
|
$row['ipb_id'] = $dbw->nextSequenceValue( "ipblocks_ipb_id_seq" );
|
2011-12-15 18:57:53 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$dbw->insert( 'ipblocks', $row, __METHOD__, [ 'IGNORE' ] );
|
2006-07-10 06:30:03 +00:00
|
|
|
$affected = $dbw->affectedRows();
|
2015-04-17 20:04:35 +00:00
|
|
|
$this->mId = $dbw->insertId();
|
2015-04-06 22:13:07 +00:00
|
|
|
|
|
|
|
|
# Don't collide with expired blocks.
|
2015-04-17 20:04:35 +00:00
|
|
|
# Do this after trying to insert to avoid locking.
|
2015-04-06 22:13:07 +00:00
|
|
|
if ( !$affected ) {
|
2015-04-17 20:04:35 +00:00
|
|
|
# T96428: The ipb_address index uses a prefix on a field, so
|
|
|
|
|
# use a standard SELECT + DELETE to avoid annoying gap locks.
|
|
|
|
|
$ids = $dbw->selectFieldValues( 'ipblocks',
|
|
|
|
|
'ipb_id',
|
2016-02-17 09:09:32 +00:00
|
|
|
[
|
2015-04-06 22:13:07 +00:00
|
|
|
'ipb_address' => $row['ipb_address'],
|
|
|
|
|
'ipb_user' => $row['ipb_user'],
|
|
|
|
|
'ipb_expiry < ' . $dbw->addQuotes( $dbw->timestamp() )
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
2015-04-06 22:13:07 +00:00
|
|
|
__METHOD__
|
|
|
|
|
);
|
2015-04-17 20:04:35 +00:00
|
|
|
if ( $ids ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$dbw->delete( 'ipblocks', [ 'ipb_id' => $ids ], __METHOD__ );
|
|
|
|
|
$dbw->insert( 'ipblocks', $row, __METHOD__, [ 'IGNORE' ] );
|
2015-04-17 20:04:35 +00:00
|
|
|
$affected = $dbw->affectedRows();
|
|
|
|
|
$this->mId = $dbw->insertId();
|
|
|
|
|
}
|
2015-04-06 22:13:07 +00:00
|
|
|
}
|
|
|
|
|
|
2011-03-22 11:22:15 +00:00
|
|
|
if ( $affected ) {
|
2011-03-22 11:51:09 +00:00
|
|
|
$auto_ipd_ids = $this->doRetroactiveAutoblock();
|
2016-04-19 14:25:43 +00:00
|
|
|
|
|
|
|
|
if ( $wgBlockDisablesLogin && $this->target instanceof User ) {
|
|
|
|
|
// Change user login token to force them to be logged out.
|
|
|
|
|
$this->target->setToken();
|
|
|
|
|
$this->target->saveSettings();
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
return [ 'id' => $this->mId, 'autoIds' => $auto_ipd_ids ];
|
2011-03-22 11:22:15 +00:00
|
|
|
}
|
2006-11-08 09:54:06 +00:00
|
|
|
|
2011-03-22 11:22:15 +00:00
|
|
|
return false;
|
2003-09-01 13:13:56 +00:00
|
|
|
}
|
|
|
|
|
|
2008-09-21 15:16:32 +00:00
|
|
|
/**
|
2008-09-22 04:52:51 +00:00
|
|
|
* Update a block in the DB with new parameters.
|
|
|
|
|
* The ID field needs to be loaded first.
|
2011-06-26 23:01:29 +00:00
|
|
|
*
|
2014-03-23 01:28:57 +00:00
|
|
|
* @return bool|array False on failure, array on success:
|
|
|
|
|
* ('id' => block ID, 'autoIds' => array of autoblock IDs)
|
2008-09-21 15:16:32 +00:00
|
|
|
*/
|
|
|
|
|
public function update() {
|
|
|
|
|
wfDebug( "Block::update; timestamp {$this->mTimestamp}\n" );
|
|
|
|
|
$dbw = wfGetDB( DB_MASTER );
|
|
|
|
|
|
2013-06-02 17:36:30 +00:00
|
|
|
$dbw->startAtomic( __METHOD__ );
|
|
|
|
|
|
2010-02-14 22:07:30 +00:00
|
|
|
$dbw->update(
|
|
|
|
|
'ipblocks',
|
2011-03-22 17:18:15 +00:00
|
|
|
$this->getDatabaseArray( $dbw ),
|
2016-02-17 09:09:32 +00:00
|
|
|
[ 'ipb_id' => $this->getId() ],
|
2011-03-22 17:18:15 +00:00
|
|
|
__METHOD__
|
2010-02-14 22:07:30 +00:00
|
|
|
);
|
2008-09-21 15:16:32 +00:00
|
|
|
|
2013-06-02 17:36:30 +00:00
|
|
|
$affected = $dbw->affectedRows();
|
|
|
|
|
|
2014-03-23 16:27:02 +00:00
|
|
|
if ( $this->isAutoblocking() ) {
|
|
|
|
|
// update corresponding autoblock(s) (bug 48813)
|
|
|
|
|
$dbw->update(
|
|
|
|
|
'ipblocks',
|
|
|
|
|
$this->getAutoblockUpdateArray(),
|
2016-02-17 09:09:32 +00:00
|
|
|
[ 'ipb_parent_block_id' => $this->getId() ],
|
2014-03-23 16:27:02 +00:00
|
|
|
__METHOD__
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
// autoblock no longer required, delete corresponding autoblock(s)
|
|
|
|
|
$dbw->delete(
|
|
|
|
|
'ipblocks',
|
2016-02-17 09:09:32 +00:00
|
|
|
[ 'ipb_parent_block_id' => $this->getId() ],
|
2014-03-23 16:27:02 +00:00
|
|
|
__METHOD__
|
|
|
|
|
);
|
|
|
|
|
}
|
2013-06-02 17:36:30 +00:00
|
|
|
|
|
|
|
|
$dbw->endAtomic( __METHOD__ );
|
|
|
|
|
|
|
|
|
|
if ( $affected ) {
|
|
|
|
|
$auto_ipd_ids = $this->doRetroactiveAutoblock();
|
2016-02-17 09:09:32 +00:00
|
|
|
return [ 'id' => $this->mId, 'autoIds' => $auto_ipd_ids ];
|
2013-06-02 17:36:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
2008-09-21 15:16:32 +00:00
|
|
|
}
|
2010-02-14 22:07:30 +00:00
|
|
|
|
2008-09-21 15:16:32 +00:00
|
|
|
/**
|
2011-03-22 17:18:15 +00:00
|
|
|
* Get an array suitable for passing to $dbw->insert() or $dbw->update()
|
2015-10-04 09:07:25 +00:00
|
|
|
* @param IDatabase $db
|
2014-04-23 09:25:56 +00:00
|
|
|
* @return array
|
2008-09-21 15:16:32 +00:00
|
|
|
*/
|
2012-12-20 15:09:25 +00:00
|
|
|
protected function getDatabaseArray( $db = null ) {
|
2013-04-26 14:42:31 +00:00
|
|
|
if ( !$db ) {
|
2016-09-05 19:55:19 +00:00
|
|
|
$db = wfGetDB( DB_REPLICA );
|
2009-05-21 20:32:28 +00:00
|
|
|
}
|
2011-07-18 21:48:56 +00:00
|
|
|
$expiry = $db->encodeExpiry( $this->mExpiry );
|
2009-10-19 11:15:51 +00:00
|
|
|
|
2012-04-10 00:12:06 +00:00
|
|
|
if ( $this->forcedTargetID ) {
|
|
|
|
|
$uid = $this->forcedTargetID;
|
|
|
|
|
} else {
|
2016-03-19 00:08:06 +00:00
|
|
|
$uid = $this->target instanceof User ? $this->target->getId() : 0;
|
2012-04-10 00:12:06 +00:00
|
|
|
}
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$a = [
|
2011-03-22 17:18:15 +00:00
|
|
|
'ipb_address' => (string)$this->target,
|
2012-04-10 00:12:06 +00:00
|
|
|
'ipb_user' => $uid,
|
2011-11-01 00:11:53 +00:00
|
|
|
'ipb_by' => $this->getBy(),
|
|
|
|
|
'ipb_by_text' => $this->getByName(),
|
2011-03-22 17:18:15 +00:00
|
|
|
'ipb_reason' => $this->mReason,
|
|
|
|
|
'ipb_timestamp' => $db->timestamp( $this->mTimestamp ),
|
|
|
|
|
'ipb_auto' => $this->mAuto,
|
|
|
|
|
'ipb_anon_only' => !$this->isHardblock(),
|
|
|
|
|
'ipb_create_account' => $this->prevents( 'createaccount' ),
|
|
|
|
|
'ipb_enable_autoblock' => $this->isAutoblocking(),
|
2011-07-18 21:48:56 +00:00
|
|
|
'ipb_expiry' => $expiry,
|
2011-03-22 17:18:15 +00:00
|
|
|
'ipb_range_start' => $this->getRangeStart(),
|
|
|
|
|
'ipb_range_end' => $this->getRangeEnd(),
|
2012-10-26 16:18:59 +00:00
|
|
|
'ipb_deleted' => intval( $this->mHideName ), // typecast required for SQLite
|
2011-03-22 17:18:15 +00:00
|
|
|
'ipb_block_email' => $this->prevents( 'sendemail' ),
|
2012-03-28 02:44:32 +00:00
|
|
|
'ipb_allow_usertalk' => !$this->prevents( 'editownusertalk' ),
|
2012-10-26 16:18:59 +00:00
|
|
|
'ipb_parent_block_id' => $this->mParentBlockId
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2010-05-30 14:48:30 +00:00
|
|
|
|
2011-03-22 17:18:15 +00:00
|
|
|
return $a;
|
2008-09-21 15:16:32 +00:00
|
|
|
}
|
2009-10-19 11:15:51 +00:00
|
|
|
|
2013-06-02 17:36:30 +00:00
|
|
|
/**
|
2014-04-23 09:25:56 +00:00
|
|
|
* @return array
|
2013-06-02 17:36:30 +00:00
|
|
|
*/
|
|
|
|
|
protected function getAutoblockUpdateArray() {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
2013-06-02 17:36:30 +00:00
|
|
|
'ipb_by' => $this->getBy(),
|
|
|
|
|
'ipb_by_text' => $this->getByName(),
|
|
|
|
|
'ipb_reason' => $this->mReason,
|
|
|
|
|
'ipb_create_account' => $this->prevents( 'createaccount' ),
|
|
|
|
|
'ipb_deleted' => (int)$this->mHideName, // typecast required for SQLite
|
|
|
|
|
'ipb_allow_usertalk' => !$this->prevents( 'editownusertalk' ),
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2013-06-02 17:36:30 +00:00
|
|
|
}
|
|
|
|
|
|
2006-11-08 09:54:06 +00:00
|
|
|
/**
|
2009-10-19 11:15:51 +00:00
|
|
|
* Retroactively autoblocks the last IP used by the user (if it is a user)
|
|
|
|
|
* blocked by this Block.
|
|
|
|
|
*
|
2014-04-23 09:25:56 +00:00
|
|
|
* @return array Block IDs of retroactive autoblocks made
|
2009-10-19 11:15:51 +00:00
|
|
|
*/
|
2011-03-19 17:44:01 +00:00
|
|
|
protected function doRetroactiveAutoblock() {
|
2016-02-17 09:09:32 +00:00
|
|
|
$blockIds = [];
|
2011-07-15 00:05:50 +00:00
|
|
|
# If autoblock is enabled, autoblock the LAST IP(s) used
|
2011-03-22 17:18:15 +00:00
|
|
|
if ( $this->isAutoblocking() && $this->getType() == self::TYPE_USER ) {
|
|
|
|
|
wfDebug( "Doing retroactive autoblocks for " . $this->getTarget() . "\n" );
|
2009-10-19 11:15:51 +00:00
|
|
|
|
2014-12-09 07:23:30 +00:00
|
|
|
$continue = Hooks::run(
|
2016-02-17 09:09:32 +00:00
|
|
|
'PerformRetroactiveAutoblock', [ $this, &$blockIds ] );
|
2009-10-19 11:15:51 +00:00
|
|
|
|
2011-07-15 00:48:02 +00:00
|
|
|
if ( $continue ) {
|
|
|
|
|
self::defaultRetroactiveAutoblock( $this, $blockIds );
|
2008-06-27 06:24:42 +00:00
|
|
|
}
|
2011-07-15 00:05:50 +00:00
|
|
|
}
|
|
|
|
|
return $blockIds;
|
|
|
|
|
}
|
2006-11-08 09:54:06 +00:00
|
|
|
|
2011-07-15 00:05:50 +00:00
|
|
|
/**
|
|
|
|
|
* Retroactively autoblocks the last IP used by the user (if it is a user)
|
|
|
|
|
* blocked by this Block. This will use the recentchanges table.
|
|
|
|
|
*
|
2011-07-15 00:48:02 +00:00
|
|
|
* @param Block $block
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param array &$blockIds
|
2011-07-15 00:05:50 +00:00
|
|
|
*/
|
2011-07-15 00:48:02 +00:00
|
|
|
protected static function defaultRetroactiveAutoblock( Block $block, array &$blockIds ) {
|
2013-01-04 19:58:00 +00:00
|
|
|
global $wgPutIPinRC;
|
|
|
|
|
|
|
|
|
|
// No IPs are in recentchanges table, so nothing to select
|
2013-04-20 22:49:30 +00:00
|
|
|
if ( !$wgPutIPinRC ) {
|
2013-01-04 19:58:00 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-05 19:55:19 +00:00
|
|
|
$dbr = wfGetDB( DB_REPLICA );
|
2011-07-15 00:05:50 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$options = [ 'ORDER BY' => 'rc_timestamp DESC' ];
|
|
|
|
|
$conds = [ 'rc_user_text' => (string)$block->getTarget() ];
|
2011-07-15 00:05:50 +00:00
|
|
|
|
|
|
|
|
// Just the last IP used.
|
|
|
|
|
$options['LIMIT'] = 1;
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$res = $dbr->select( 'recentchanges', [ 'rc_ip' ], $conds,
|
2013-01-26 18:32:03 +00:00
|
|
|
__METHOD__, $options );
|
2011-07-15 00:05:50 +00:00
|
|
|
|
2013-01-06 10:52:40 +00:00
|
|
|
if ( !$res->numRows() ) {
|
2011-07-15 00:05:50 +00:00
|
|
|
# No results, don't autoblock anything
|
|
|
|
|
wfDebug( "No IP found to retroactively autoblock\n" );
|
|
|
|
|
} else {
|
|
|
|
|
foreach ( $res as $row ) {
|
|
|
|
|
if ( $row->rc_ip ) {
|
|
|
|
|
$id = $block->doAutoblock( $row->rc_ip );
|
2013-04-20 22:49:30 +00:00
|
|
|
if ( $id ) {
|
|
|
|
|
$blockIds[] = $id;
|
|
|
|
|
}
|
2008-06-27 06:24:42 +00:00
|
|
|
}
|
2006-11-08 09:54:06 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2009-10-19 11:15:51 +00:00
|
|
|
|
2006-11-08 09:54:06 +00:00
|
|
|
/**
|
2008-08-08 05:56:43 +00:00
|
|
|
* Checks whether a given IP is on the autoblock whitelist.
|
2011-03-22 17:18:15 +00:00
|
|
|
* TODO: this probably belongs somewhere else, but not sure where...
|
2008-09-22 04:52:51 +00:00
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $ip The IP to check
|
2014-04-23 09:25:56 +00:00
|
|
|
* @return bool
|
2008-08-08 05:56:43 +00:00
|
|
|
*/
|
2008-11-30 13:09:19 +00:00
|
|
|
public static function isWhitelistedFromAutoblocks( $ip ) {
|
2008-09-21 14:22:23 +00:00
|
|
|
// Try to get the autoblock_whitelist from the cache, as it's faster
|
|
|
|
|
// than getting the msg raw and explode()'ing it.
|
2016-10-14 09:17:25 +00:00
|
|
|
$cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
|
2015-10-19 17:52:19 +00:00
|
|
|
$lines = $cache->getWithSetCallback(
|
2015-10-15 02:45:03 +00:00
|
|
|
wfMemcKey( 'ipb', 'autoblock', 'whitelist' ),
|
2015-10-19 17:52:19 +00:00
|
|
|
$cache::TTL_DAY,
|
2016-10-14 09:17:25 +00:00
|
|
|
function ( $curValue, &$ttl, array &$setOpts ) {
|
|
|
|
|
$setOpts += Database::getCacheSetOptions( wfGetDB( DB_REPLICA ) );
|
|
|
|
|
|
2015-10-15 02:45:03 +00:00
|
|
|
return explode( "\n",
|
|
|
|
|
wfMessage( 'autoblock_whitelist' )->inContentLanguage()->plain() );
|
|
|
|
|
}
|
|
|
|
|
);
|
2006-11-22 23:42:39 +00:00
|
|
|
|
2010-02-14 22:07:30 +00:00
|
|
|
wfDebug( "Checking the autoblock whitelist..\n" );
|
2006-11-22 23:42:39 +00:00
|
|
|
|
2010-02-14 22:07:30 +00:00
|
|
|
foreach ( $lines as $line ) {
|
2006-11-22 23:42:39 +00:00
|
|
|
# List items only
|
|
|
|
|
if ( substr( $line, 0, 1 ) !== '*' ) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2009-10-19 11:15:51 +00:00
|
|
|
$wlEntry = substr( $line, 1 );
|
|
|
|
|
$wlEntry = trim( $wlEntry );
|
2006-11-22 23:42:39 +00:00
|
|
|
|
2010-02-14 22:07:30 +00:00
|
|
|
wfDebug( "Checking $ip against $wlEntry..." );
|
2006-11-22 23:42:39 +00:00
|
|
|
|
|
|
|
|
# Is the IP in this range?
|
2009-10-19 11:15:51 +00:00
|
|
|
if ( IP::isInRange( $ip, $wlEntry ) ) {
|
2010-02-14 22:07:30 +00:00
|
|
|
wfDebug( " IP $ip matches $wlEntry, not autoblocking\n" );
|
2008-08-08 05:56:43 +00:00
|
|
|
return true;
|
2006-12-08 10:30:50 +00:00
|
|
|
} else {
|
|
|
|
|
wfDebug( " No match\n" );
|
2006-11-22 23:42:39 +00:00
|
|
|
}
|
2006-11-22 11:51:49 +00:00
|
|
|
}
|
2009-10-19 11:15:51 +00:00
|
|
|
|
2008-08-08 05:56:43 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2008-09-22 04:52:51 +00:00
|
|
|
* Autoblocks the given IP, referring to this Block.
|
|
|
|
|
*
|
2014-04-23 09:25:56 +00:00
|
|
|
* @param string $autoblockIP The IP to autoblock.
|
|
|
|
|
* @return int|bool Block ID if an autoblock was inserted, false if not.
|
2008-09-22 04:52:51 +00:00
|
|
|
*/
|
2011-03-23 00:10:46 +00:00
|
|
|
public function doAutoblock( $autoblockIP ) {
|
2008-08-08 05:56:43 +00:00
|
|
|
# If autoblocks are disabled, go away.
|
2011-03-22 17:18:15 +00:00
|
|
|
if ( !$this->isAutoblocking() ) {
|
2011-03-22 11:22:15 +00:00
|
|
|
return false;
|
2008-08-08 05:56:43 +00:00
|
|
|
}
|
|
|
|
|
|
2011-06-18 00:29:32 +00:00
|
|
|
# Check for presence on the autoblock whitelist.
|
2011-03-22 17:18:15 +00:00
|
|
|
if ( self::isWhitelistedFromAutoblocks( $autoblockIP ) ) {
|
2011-03-22 11:22:15 +00:00
|
|
|
return false;
|
2008-08-08 05:56:43 +00:00
|
|
|
}
|
2010-02-14 22:07:30 +00:00
|
|
|
|
2011-06-18 00:29:32 +00:00
|
|
|
# Allow hooks to cancel the autoblock.
|
2016-02-17 09:09:32 +00:00
|
|
|
if ( !Hooks::run( 'AbortAutoblock', [ $autoblockIP, &$this ] ) ) {
|
2009-01-13 20:28:54 +00:00
|
|
|
wfDebug( "Autoblock aborted by hook.\n" );
|
2008-05-23 10:34:11 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
2006-11-22 11:51:49 +00:00
|
|
|
|
2011-06-18 00:29:32 +00:00
|
|
|
# It's okay to autoblock. Go ahead and insert/update the block...
|
2006-11-22 11:51:49 +00:00
|
|
|
|
2011-06-18 00:29:32 +00:00
|
|
|
# Do not add a *new* block if the IP is already blocked.
|
2011-03-21 23:03:11 +00:00
|
|
|
$ipblock = Block::newFromTarget( $autoblockIP );
|
2006-11-08 09:54:06 +00:00
|
|
|
if ( $ipblock ) {
|
2011-06-18 00:29:32 +00:00
|
|
|
# Check if the block is an autoblock and would exceed the user block
|
|
|
|
|
# if renewed. If so, do nothing, otherwise prolong the block time...
|
2013-05-15 01:12:35 +00:00
|
|
|
if ( $ipblock->mAuto && // @todo Why not compare $ipblock->mExpiry?
|
2011-06-18 00:29:32 +00:00
|
|
|
$this->mExpiry > Block::getAutoblockExpiry( $ipblock->mTimestamp )
|
2010-05-30 14:48:30 +00:00
|
|
|
) {
|
2011-06-18 00:29:32 +00:00
|
|
|
# Reset block timestamp to now and its expiry to
|
|
|
|
|
# $wgAutoblockExpiry in the future
|
2007-03-31 17:23:10 +00:00
|
|
|
$ipblock->updateTimestamp();
|
|
|
|
|
}
|
2011-03-22 11:22:15 +00:00
|
|
|
return false;
|
2006-11-08 09:54:06 +00:00
|
|
|
}
|
|
|
|
|
|
2011-06-18 00:29:32 +00:00
|
|
|
# Make a new block object with the desired properties.
|
2011-03-23 00:10:46 +00:00
|
|
|
$autoblock = new Block;
|
2011-03-22 17:18:15 +00:00
|
|
|
wfDebug( "Autoblocking {$this->getTarget()}@" . $autoblockIP . "\n" );
|
2011-03-23 00:10:46 +00:00
|
|
|
$autoblock->setTarget( $autoblockIP );
|
|
|
|
|
$autoblock->setBlocker( $this->getBlocker() );
|
2014-03-23 01:28:57 +00:00
|
|
|
$autoblock->mReason = wfMessage( 'autoblocker', $this->getTarget(), $this->mReason )
|
|
|
|
|
->inContentLanguage()->plain();
|
2011-06-24 10:00:35 +00:00
|
|
|
$timestamp = wfTimestampNow();
|
|
|
|
|
$autoblock->mTimestamp = $timestamp;
|
2011-03-23 00:10:46 +00:00
|
|
|
$autoblock->mAuto = 1;
|
|
|
|
|
$autoblock->prevents( 'createaccount', $this->prevents( 'createaccount' ) );
|
2007-03-14 05:24:06 +00:00
|
|
|
# Continue suppressing the name if needed
|
2011-03-23 00:10:46 +00:00
|
|
|
$autoblock->mHideName = $this->mHideName;
|
|
|
|
|
$autoblock->prevents( 'editownusertalk', $this->prevents( 'editownusertalk' ) );
|
2012-03-28 02:44:32 +00:00
|
|
|
$autoblock->mParentBlockId = $this->mId;
|
2010-05-30 14:48:30 +00:00
|
|
|
|
2011-11-10 09:36:18 +00:00
|
|
|
if ( $this->mExpiry == 'infinity' ) {
|
2011-03-23 00:10:46 +00:00
|
|
|
# Original block was indefinite, start an autoblock now
|
2011-06-24 10:00:35 +00:00
|
|
|
$autoblock->mExpiry = Block::getAutoblockExpiry( $timestamp );
|
2006-11-08 09:54:06 +00:00
|
|
|
} else {
|
2011-03-23 00:10:46 +00:00
|
|
|
# If the user is already blocked with an expiry date, we don't
|
|
|
|
|
# want to pile on top of that.
|
2011-06-24 10:00:35 +00:00
|
|
|
$autoblock->mExpiry = min( $this->mExpiry, Block::getAutoblockExpiry( $timestamp ) );
|
2006-11-08 09:54:06 +00:00
|
|
|
}
|
2010-05-30 14:48:30 +00:00
|
|
|
|
2011-06-18 00:29:32 +00:00
|
|
|
# Insert the block...
|
2011-03-23 00:10:46 +00:00
|
|
|
$status = $autoblock->insert();
|
|
|
|
|
return $status
|
|
|
|
|
? $status['id']
|
|
|
|
|
: false;
|
2006-11-08 09:54:06 +00:00
|
|
|
}
|
|
|
|
|
|
2008-09-21 14:22:23 +00:00
|
|
|
/**
|
|
|
|
|
* Check if a block has expired. Delete it if it is.
|
2014-04-23 09:25:56 +00:00
|
|
|
* @return bool
|
2008-09-21 14:22:23 +00:00
|
|
|
*/
|
2008-11-30 13:09:19 +00:00
|
|
|
public function deleteIfExpired() {
|
2010-05-30 14:48:30 +00:00
|
|
|
|
2003-09-01 13:13:56 +00:00
|
|
|
if ( $this->isExpired() ) {
|
2005-01-11 08:05:22 +00:00
|
|
|
wfDebug( "Block::deleteIfExpired() -- deleting\n" );
|
2003-09-01 13:13:56 +00:00
|
|
|
$this->delete();
|
2005-10-22 20:52:30 +00:00
|
|
|
$retVal = true;
|
2003-09-01 13:13:56 +00:00
|
|
|
} else {
|
2005-01-11 08:05:22 +00:00
|
|
|
wfDebug( "Block::deleteIfExpired() -- not expired\n" );
|
2005-10-22 20:52:30 +00:00
|
|
|
$retVal = false;
|
2003-09-01 13:13:56 +00:00
|
|
|
}
|
2010-05-30 14:48:30 +00:00
|
|
|
|
2005-10-22 20:52:30 +00:00
|
|
|
return $retVal;
|
2003-09-01 13:13:56 +00:00
|
|
|
}
|
|
|
|
|
|
2008-09-21 14:22:23 +00:00
|
|
|
/**
|
|
|
|
|
* Has the block expired?
|
2014-04-23 09:25:56 +00:00
|
|
|
* @return bool
|
2008-09-21 14:22:23 +00:00
|
|
|
*/
|
2008-11-30 13:09:19 +00:00
|
|
|
public function isExpired() {
|
2011-06-24 10:00:35 +00:00
|
|
|
$timestamp = wfTimestampNow();
|
|
|
|
|
wfDebug( "Block::isExpired() checking current " . $timestamp . " vs $this->mExpiry\n" );
|
2010-05-30 14:48:30 +00:00
|
|
|
|
2004-02-16 00:05:25 +00:00
|
|
|
if ( !$this->mExpiry ) {
|
|
|
|
|
return false;
|
|
|
|
|
} else {
|
2011-06-24 10:00:35 +00:00
|
|
|
return $timestamp > $this->mExpiry;
|
2004-02-16 00:05:25 +00:00
|
|
|
}
|
2003-09-01 13:13:56 +00:00
|
|
|
}
|
|
|
|
|
|
2008-09-21 14:22:23 +00:00
|
|
|
/**
|
|
|
|
|
* Is the block address valid (i.e. not a null string?)
|
2014-04-23 09:25:56 +00:00
|
|
|
* @return bool
|
2008-09-21 14:22:23 +00:00
|
|
|
*/
|
2008-11-30 13:09:19 +00:00
|
|
|
public function isValid() {
|
2011-03-22 17:18:15 +00:00
|
|
|
return $this->getTarget() != null;
|
2003-09-01 13:13:56 +00:00
|
|
|
}
|
2005-08-02 13:35:19 +00:00
|
|
|
|
2008-09-21 14:22:23 +00:00
|
|
|
/**
|
2010-02-14 22:07:30 +00:00
|
|
|
* Update the timestamp on autoblocks.
|
2008-09-21 14:22:23 +00:00
|
|
|
*/
|
2008-11-30 13:09:19 +00:00
|
|
|
public function updateTimestamp() {
|
2004-02-27 08:25:56 +00:00
|
|
|
if ( $this->mAuto ) {
|
2004-09-07 06:20:51 +00:00
|
|
|
$this->mTimestamp = wfTimestamp();
|
2004-02-27 08:25:56 +00:00
|
|
|
$this->mExpiry = Block::getAutoblockExpiry( $this->mTimestamp );
|
|
|
|
|
|
2007-01-22 23:50:42 +00:00
|
|
|
$dbw = wfGetDB( DB_MASTER );
|
2005-08-02 13:35:19 +00:00
|
|
|
$dbw->update( 'ipblocks',
|
2016-02-17 09:09:32 +00:00
|
|
|
[ /* SET */
|
2009-10-19 11:15:51 +00:00
|
|
|
'ipb_timestamp' => $dbw->timestamp( $this->mTimestamp ),
|
|
|
|
|
'ipb_expiry' => $dbw->timestamp( $this->mExpiry ),
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[ /* WHERE */
|
2016-03-03 02:48:08 +00:00
|
|
|
'ipb_id' => $this->getId(),
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
2011-03-22 17:18:15 +00:00
|
|
|
__METHOD__
|
2004-07-10 03:09:26 +00:00
|
|
|
);
|
2004-02-14 12:37:25 +00:00
|
|
|
}
|
|
|
|
|
}
|
2005-08-02 13:35:19 +00:00
|
|
|
|
2011-03-19 23:47:08 +00:00
|
|
|
/**
|
|
|
|
|
* Get the IP address at the start of the range in Hex form
|
2012-10-07 23:35:26 +00:00
|
|
|
* @throws MWException
|
2014-04-23 09:25:56 +00:00
|
|
|
* @return string IP in Hex form
|
2011-03-19 23:47:08 +00:00
|
|
|
*/
|
2011-03-20 17:43:17 +00:00
|
|
|
public function getRangeStart() {
|
2013-04-26 14:42:31 +00:00
|
|
|
switch ( $this->type ) {
|
2011-03-19 23:47:08 +00:00
|
|
|
case self::TYPE_USER:
|
2011-06-07 18:13:21 +00:00
|
|
|
return '';
|
2011-03-19 23:47:08 +00:00
|
|
|
case self::TYPE_IP:
|
|
|
|
|
return IP::toHex( $this->target );
|
|
|
|
|
case self::TYPE_RANGE:
|
2011-03-22 17:18:15 +00:00
|
|
|
list( $start, /*...*/ ) = IP::parseRange( $this->target );
|
|
|
|
|
return $start;
|
2013-04-11 05:29:05 +00:00
|
|
|
default:
|
|
|
|
|
throw new MWException( "Block with invalid type" );
|
2011-03-19 23:47:08 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2013-06-23 01:01:26 +00:00
|
|
|
* Get the IP address at the end of the range in Hex form
|
2012-10-07 23:35:26 +00:00
|
|
|
* @throws MWException
|
2014-04-23 09:25:56 +00:00
|
|
|
* @return string IP in Hex form
|
2011-03-19 23:47:08 +00:00
|
|
|
*/
|
2011-03-20 17:43:17 +00:00
|
|
|
public function getRangeEnd() {
|
2013-04-26 14:42:31 +00:00
|
|
|
switch ( $this->type ) {
|
2011-03-19 23:47:08 +00:00
|
|
|
case self::TYPE_USER:
|
2011-06-07 18:13:21 +00:00
|
|
|
return '';
|
2011-03-19 23:47:08 +00:00
|
|
|
case self::TYPE_IP:
|
|
|
|
|
return IP::toHex( $this->target );
|
|
|
|
|
case self::TYPE_RANGE:
|
2011-03-22 17:18:15 +00:00
|
|
|
list( /*...*/, $end ) = IP::parseRange( $this->target );
|
|
|
|
|
return $end;
|
2013-04-11 05:29:05 +00:00
|
|
|
default:
|
|
|
|
|
throw new MWException( "Block with invalid type" );
|
2011-03-19 23:47:08 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2006-11-21 18:26:55 +00:00
|
|
|
/**
|
2008-09-21 14:22:23 +00:00
|
|
|
* Get the user id of the blocking sysop
|
2008-09-22 04:52:51 +00:00
|
|
|
*
|
2014-04-23 09:25:56 +00:00
|
|
|
* @return int (0 for foreign users)
|
2006-11-21 18:26:55 +00:00
|
|
|
*/
|
|
|
|
|
public function getBy() {
|
2011-11-01 00:11:53 +00:00
|
|
|
$blocker = $this->getBlocker();
|
|
|
|
|
return ( $blocker instanceof User )
|
|
|
|
|
? $blocker->getId()
|
2011-03-22 17:18:15 +00:00
|
|
|
: 0;
|
2006-11-21 18:26:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2008-09-21 14:22:23 +00:00
|
|
|
* Get the username of the blocking sysop
|
2008-09-22 04:52:51 +00:00
|
|
|
*
|
2014-04-23 09:25:56 +00:00
|
|
|
* @return string
|
2006-11-21 18:26:55 +00:00
|
|
|
*/
|
2008-11-30 13:09:19 +00:00
|
|
|
public function getByName() {
|
2011-11-01 00:11:53 +00:00
|
|
|
$blocker = $this->getBlocker();
|
|
|
|
|
return ( $blocker instanceof User )
|
|
|
|
|
? $blocker->getName()
|
|
|
|
|
: (string)$blocker; // username
|
2005-08-23 16:52:42 +00:00
|
|
|
}
|
|
|
|
|
|
2011-03-21 19:12:41 +00:00
|
|
|
/**
|
|
|
|
|
* Get the block ID
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
|
|
|
|
public function getId() {
|
|
|
|
|
return $this->mId;
|
|
|
|
|
}
|
|
|
|
|
|
2008-09-22 04:52:51 +00:00
|
|
|
/**
|
|
|
|
|
* Get/set a flag determining whether the master is used for reads
|
2011-06-26 23:01:29 +00:00
|
|
|
*
|
2014-09-16 23:00:19 +00:00
|
|
|
* @param bool|null $x
|
2014-04-23 09:25:56 +00:00
|
|
|
* @return bool
|
2008-09-22 04:52:51 +00:00
|
|
|
*/
|
2009-10-19 11:15:51 +00:00
|
|
|
public function fromMaster( $x = null ) {
|
2005-12-01 10:37:47 +00:00
|
|
|
return wfSetVar( $this->mFromMaster, $x );
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-19 23:47:08 +00:00
|
|
|
/**
|
2014-10-25 00:15:12 +00:00
|
|
|
* Get/set whether the Block is a hardblock (affects logged-in users on a given IP/range)
|
2014-09-16 23:00:19 +00:00
|
|
|
* @param bool|null $x
|
2014-04-23 09:25:56 +00:00
|
|
|
* @return bool
|
2011-03-19 23:47:08 +00:00
|
|
|
*/
|
2011-03-20 17:43:17 +00:00
|
|
|
public function isHardblock( $x = null ) {
|
2011-03-22 17:18:15 +00:00
|
|
|
wfSetVar( $this->isHardblock, $x );
|
|
|
|
|
|
|
|
|
|
# You can't *not* hardblock a user
|
|
|
|
|
return $this->getType() == self::TYPE_USER
|
|
|
|
|
? true
|
|
|
|
|
: $this->isHardblock;
|
2011-03-19 23:47:08 +00:00
|
|
|
}
|
|
|
|
|
|
2014-09-16 23:00:19 +00:00
|
|
|
/**
|
|
|
|
|
* @param null|bool $x
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2011-03-21 19:12:41 +00:00
|
|
|
public function isAutoblocking( $x = null ) {
|
2011-03-22 17:18:15 +00:00
|
|
|
wfSetVar( $this->isAutoblocking, $x );
|
|
|
|
|
|
|
|
|
|
# You can't put an autoblock on an IP or range as we don't have any history to
|
|
|
|
|
# look over to get more IPs from
|
|
|
|
|
return $this->getType() == self::TYPE_USER
|
|
|
|
|
? $this->isAutoblocking
|
|
|
|
|
: false;
|
2011-03-21 19:12:41 +00:00
|
|
|
}
|
|
|
|
|
|
2011-03-19 23:47:08 +00:00
|
|
|
/**
|
|
|
|
|
* Get/set whether the Block prevents a given action
|
2016-06-29 14:45:25 +00:00
|
|
|
*
|
|
|
|
|
* @param string $action Action to check
|
|
|
|
|
* @param bool|null $x Value for set, or null to just get value
|
|
|
|
|
* @return bool|null Null for unrecognized rights.
|
2011-03-19 23:47:08 +00:00
|
|
|
*/
|
2011-03-20 17:43:17 +00:00
|
|
|
public function prevents( $action, $x = null ) {
|
2016-06-29 14:45:25 +00:00
|
|
|
global $wgBlockDisablesLogin;
|
|
|
|
|
$res = null;
|
2013-04-26 14:42:31 +00:00
|
|
|
switch ( $action ) {
|
2011-03-19 23:47:08 +00:00
|
|
|
case 'edit':
|
2011-03-21 19:12:41 +00:00
|
|
|
# For now... <evil laugh>
|
2016-06-29 14:45:25 +00:00
|
|
|
$res = true;
|
|
|
|
|
break;
|
2011-03-19 23:47:08 +00:00
|
|
|
case 'createaccount':
|
2016-06-29 14:45:25 +00:00
|
|
|
$res = wfSetVar( $this->mCreateAccount, $x );
|
|
|
|
|
break;
|
2011-03-19 23:47:08 +00:00
|
|
|
case 'sendemail':
|
2016-06-29 14:45:25 +00:00
|
|
|
$res = wfSetVar( $this->mBlockEmail, $x );
|
|
|
|
|
break;
|
2011-03-20 17:43:17 +00:00
|
|
|
case 'editownusertalk':
|
2016-06-29 14:45:25 +00:00
|
|
|
$res = wfSetVar( $this->mDisableUsertalk, $x );
|
|
|
|
|
break;
|
|
|
|
|
case 'read':
|
|
|
|
|
$res = false;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if ( !$res && $wgBlockDisablesLogin ) {
|
|
|
|
|
// If a block would disable login, then it should
|
|
|
|
|
// prevent any action that all users cannot do
|
|
|
|
|
$anon = new User;
|
|
|
|
|
$res = $anon->isAllowed( $action ) ? $res : true;
|
2011-03-19 23:47:08 +00:00
|
|
|
}
|
2016-06-29 14:45:25 +00:00
|
|
|
|
|
|
|
|
return $res;
|
2011-03-19 23:47:08 +00:00
|
|
|
}
|
|
|
|
|
|
2008-09-22 04:52:51 +00:00
|
|
|
/**
|
|
|
|
|
* Get the block name, but with autoblocked IPs hidden as per standard privacy policy
|
2014-04-23 09:25:56 +00:00
|
|
|
* @return string Text is escaped
|
2008-09-22 04:52:51 +00:00
|
|
|
*/
|
2008-11-30 13:09:19 +00:00
|
|
|
public function getRedactedName() {
|
2006-07-10 06:30:03 +00:00
|
|
|
if ( $this->mAuto ) {
|
2011-03-24 21:35:14 +00:00
|
|
|
return Html::rawElement(
|
2011-03-13 15:14:33 +00:00
|
|
|
'span',
|
2016-02-17 09:09:32 +00:00
|
|
|
[ 'class' => 'mw-autoblockid' ],
|
2011-03-13 15:14:33 +00:00
|
|
|
wfMessage( 'autoblockid', $this->mId )
|
|
|
|
|
);
|
2006-07-10 06:30:03 +00:00
|
|
|
} else {
|
2011-03-22 17:18:15 +00:00
|
|
|
return htmlspecialchars( $this->getTarget() );
|
2006-07-10 06:30:03 +00:00
|
|
|
}
|
|
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2008-09-21 14:22:23 +00:00
|
|
|
/**
|
|
|
|
|
* Get a timestamp of the expiry for autoblocks
|
2008-09-22 04:52:51 +00:00
|
|
|
*
|
2014-04-23 09:25:56 +00:00
|
|
|
* @param string|int $timestamp
|
|
|
|
|
* @return string
|
2008-09-21 14:22:23 +00:00
|
|
|
*/
|
2008-11-30 13:09:19 +00:00
|
|
|
public static function getAutoblockExpiry( $timestamp ) {
|
2004-02-14 12:37:25 +00:00
|
|
|
global $wgAutoblockExpiry;
|
2010-05-30 14:48:30 +00:00
|
|
|
|
2004-09-07 06:26:15 +00:00
|
|
|
return wfTimestamp( TS_MW, wfTimestamp( TS_UNIX, $timestamp ) + $wgAutoblockExpiry );
|
2004-02-14 12:37:25 +00:00
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
|
|
|
|
/**
|
2006-07-10 06:30:03 +00:00
|
|
|
* Purge expired blocks from the ipblocks table
|
|
|
|
|
*/
|
2008-11-30 13:09:19 +00:00
|
|
|
public static function purgeExpired() {
|
2013-04-18 04:35:33 +00:00
|
|
|
if ( wfReadOnly() ) {
|
|
|
|
|
return;
|
2013-01-17 22:25:55 +00:00
|
|
|
}
|
2013-04-18 04:35:33 +00:00
|
|
|
|
2016-01-13 16:54:48 +00:00
|
|
|
DeferredUpdates::addUpdate( new AtomicSectionUpdate(
|
|
|
|
|
wfGetDB( DB_MASTER ),
|
|
|
|
|
__METHOD__,
|
|
|
|
|
function ( IDatabase $dbw, $fname ) {
|
|
|
|
|
$dbw->delete(
|
|
|
|
|
'ipblocks',
|
2016-02-17 09:09:32 +00:00
|
|
|
[ 'ipb_expiry < ' . $dbw->addQuotes( $dbw->timestamp() ) ],
|
2016-01-13 16:54:48 +00:00
|
|
|
$fname
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
) );
|
2006-07-10 06:30:03 +00:00
|
|
|
}
|
|
|
|
|
|
2011-03-13 14:47:34 +00:00
|
|
|
/**
|
2011-03-13 21:33:52 +00:00
|
|
|
* Given a target and the target's type, get an existing Block object if possible.
|
2014-04-23 09:25:56 +00:00
|
|
|
* @param string|User|int $specificTarget A block target, which may be one of several types:
|
2011-03-13 14:47:34 +00:00
|
|
|
* * A user to block, in which case $target will be a User
|
|
|
|
|
* * An IP to block, in which case $target will be a User generated by using
|
|
|
|
|
* User::newFromName( $ip, false ) to turn off name validation
|
|
|
|
|
* * An IP range, in which case $target will be a String "123.123.123.123/18" etc
|
2011-03-21 19:12:41 +00:00
|
|
|
* * The ID of an existing block, in the format "#12345" (since pure numbers are valid
|
|
|
|
|
* usernames
|
|
|
|
|
* Calling this with a user, IP address or range will not select autoblocks, and will
|
|
|
|
|
* only select a block where the targets match exactly (so looking for blocks on
|
|
|
|
|
* 1.2.3.4 will not select 1.2.0.0/16 or even 1.2.3.4/32)
|
2014-04-23 09:25:56 +00:00
|
|
|
* @param string|User|int $vagueTarget As above, but we will search for *any* block which
|
2011-03-21 19:12:41 +00:00
|
|
|
* affects that target (so for an IP address, get ranges containing that IP; and also
|
2011-05-24 21:04:50 +00:00
|
|
|
* get any relevant autoblocks). Leave empty or blank to skip IP-based lookups.
|
2014-04-23 09:25:56 +00:00
|
|
|
* @param bool $fromMaster Whether to use the DB_MASTER database
|
2011-03-21 19:12:41 +00:00
|
|
|
* @return Block|null (null if no relevant block could be found). The target and type
|
|
|
|
|
* of the returned Block will refer to the actual block which was found, which might
|
|
|
|
|
* not be the same as the target you gave if you used $vagueTarget!
|
2011-03-13 14:47:34 +00:00
|
|
|
*/
|
2011-03-21 19:12:41 +00:00
|
|
|
public static function newFromTarget( $specificTarget, $vagueTarget = null, $fromMaster = false ) {
|
2011-05-24 19:03:41 +00:00
|
|
|
|
2011-03-21 19:12:41 +00:00
|
|
|
list( $target, $type ) = self::parseTarget( $specificTarget );
|
2013-04-20 22:49:30 +00:00
|
|
|
if ( $type == Block::TYPE_ID || $type == Block::TYPE_AUTO ) {
|
2011-03-21 19:12:41 +00:00
|
|
|
return Block::newFromID( $target );
|
2011-03-13 14:47:34 +00:00
|
|
|
|
2013-04-20 22:49:30 +00:00
|
|
|
} elseif ( $target === null && $vagueTarget == '' ) {
|
2011-03-22 11:45:18 +00:00
|
|
|
# We're not going to find anything useful here
|
2011-06-27 13:48:01 +00:00
|
|
|
# Be aware that the == '' check is explicit, since empty values will be
|
|
|
|
|
# passed by some callers (bug 29116)
|
2011-03-22 11:45:18 +00:00
|
|
|
return null;
|
|
|
|
|
|
2014-03-23 01:28:57 +00:00
|
|
|
} elseif ( in_array(
|
|
|
|
|
$type,
|
2016-02-17 09:09:32 +00:00
|
|
|
[ Block::TYPE_USER, Block::TYPE_IP, Block::TYPE_RANGE, null ] )
|
2014-03-23 01:28:57 +00:00
|
|
|
) {
|
2011-03-21 19:12:41 +00:00
|
|
|
$block = new Block();
|
|
|
|
|
$block->fromMaster( $fromMaster );
|
2011-03-13 14:47:34 +00:00
|
|
|
|
2013-04-20 22:49:30 +00:00
|
|
|
if ( $type !== null ) {
|
2011-03-21 19:12:41 +00:00
|
|
|
$block->setTarget( $target );
|
|
|
|
|
}
|
2011-03-13 14:47:34 +00:00
|
|
|
|
2013-04-20 22:49:30 +00:00
|
|
|
if ( $block->newLoad( $vagueTarget ) ) {
|
2011-03-21 19:12:41 +00:00
|
|
|
return $block;
|
|
|
|
|
}
|
2011-03-13 14:47:34 +00:00
|
|
|
}
|
2011-12-15 18:57:53 +00:00
|
|
|
return null;
|
2011-03-13 14:47:34 +00:00
|
|
|
}
|
|
|
|
|
|
2012-11-17 23:23:09 +00:00
|
|
|
/**
|
|
|
|
|
* Get all blocks that match any IP from an array of IP addresses
|
|
|
|
|
*
|
2014-04-23 09:25:56 +00:00
|
|
|
* @param array $ipChain List of IPs (strings), usually retrieved from the
|
2012-11-17 23:23:09 +00:00
|
|
|
* X-Forwarded-For header of the request
|
2014-04-23 09:25:56 +00:00
|
|
|
* @param bool $isAnon Exclude anonymous-only blocks if false
|
2016-09-05 20:21:26 +00:00
|
|
|
* @param bool $fromMaster Whether to query the master or replica DB
|
2014-04-23 09:25:56 +00:00
|
|
|
* @return array Array of Blocks
|
2013-05-20 05:52:14 +00:00
|
|
|
* @since 1.22
|
2012-11-17 23:23:09 +00:00
|
|
|
*/
|
|
|
|
|
public static function getBlocksForIPList( array $ipChain, $isAnon, $fromMaster = false ) {
|
|
|
|
|
if ( !count( $ipChain ) ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [];
|
2012-11-17 23:23:09 +00:00
|
|
|
}
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$conds = [];
|
2016-09-22 02:52:06 +00:00
|
|
|
$proxyLookup = MediaWikiServices::getInstance()->getProxyLookup();
|
2012-11-17 23:23:09 +00:00
|
|
|
foreach ( array_unique( $ipChain ) as $ipaddr ) {
|
|
|
|
|
# Discard invalid IP addresses. Since XFF can be spoofed and we do not
|
|
|
|
|
# necessarily trust the header given to us, make sure that we are only
|
|
|
|
|
# checking for blocks on well-formatted IP addresses (IPv4 and IPv6).
|
|
|
|
|
# Do not treat private IP spaces as special as it may be desirable for wikis
|
|
|
|
|
# to block those IP ranges in order to stop misbehaving proxies that spoof XFF.
|
|
|
|
|
if ( !IP::isValid( $ipaddr ) ) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
# Don't check trusted IPs (includes local squids which will be in every request)
|
2016-09-22 02:52:06 +00:00
|
|
|
if ( $proxyLookup->isTrustedProxy( $ipaddr ) ) {
|
2012-11-17 23:23:09 +00:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
# Check both the original IP (to check against single blocks), as well as build
|
|
|
|
|
# the clause to check for rangeblocks for the given IP.
|
|
|
|
|
$conds['ipb_address'][] = $ipaddr;
|
|
|
|
|
$conds[] = self::getRangeCond( IP::toHex( $ipaddr ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( !count( $conds ) ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [];
|
2012-11-17 23:23:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $fromMaster ) {
|
|
|
|
|
$db = wfGetDB( DB_MASTER );
|
|
|
|
|
} else {
|
2016-09-05 19:55:19 +00:00
|
|
|
$db = wfGetDB( DB_REPLICA );
|
2012-11-17 23:23:09 +00:00
|
|
|
}
|
|
|
|
|
$conds = $db->makeList( $conds, LIST_OR );
|
|
|
|
|
if ( !$isAnon ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$conds = [ $conds, 'ipb_anon_only' => 0 ];
|
2012-11-17 23:23:09 +00:00
|
|
|
}
|
|
|
|
|
$selectFields = array_merge(
|
2016-02-17 09:09:32 +00:00
|
|
|
[ 'ipb_range_start', 'ipb_range_end' ],
|
2012-11-17 23:23:09 +00:00
|
|
|
Block::selectFields()
|
|
|
|
|
);
|
|
|
|
|
$rows = $db->select( 'ipblocks',
|
|
|
|
|
$selectFields,
|
|
|
|
|
$conds,
|
|
|
|
|
__METHOD__
|
|
|
|
|
);
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$blocks = [];
|
2012-11-17 23:23:09 +00:00
|
|
|
foreach ( $rows as $row ) {
|
|
|
|
|
$block = self::newFromRow( $row );
|
2015-12-30 04:29:10 +00:00
|
|
|
if ( !$block->isExpired() ) {
|
2012-11-17 23:23:09 +00:00
|
|
|
$blocks[] = $block;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $blocks;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* From a list of multiple blocks, find the most exact and strongest Block.
|
2014-10-21 19:39:17 +00:00
|
|
|
*
|
2012-11-17 23:23:09 +00:00
|
|
|
* The logic for finding the "best" block is:
|
|
|
|
|
* - Blocks that match the block's target IP are preferred over ones in a range
|
|
|
|
|
* - Hardblocks are chosen over softblocks that prevent account creation
|
|
|
|
|
* - Softblocks that prevent account creation are chosen over other softblocks
|
|
|
|
|
* - Other softblocks are chosen over autoblocks
|
|
|
|
|
* - If there are multiple exact or range blocks at the same level, the one chosen
|
|
|
|
|
* is random
|
2014-10-21 19:39:17 +00:00
|
|
|
* This should be used when $blocks where retrieved from the user's IP address
|
|
|
|
|
* and $ipChain is populated from the same IP address information.
|
2014-07-24 08:08:16 +00:00
|
|
|
*
|
2014-10-21 19:39:17 +00:00
|
|
|
* @param array $blocks Array of Block objects
|
2014-04-23 09:25:56 +00:00
|
|
|
* @param array $ipChain List of IPs (strings). This is used to determine how "close"
|
2012-11-17 23:23:09 +00:00
|
|
|
* a block is to the server, and if a block matches exactly, or is in a range.
|
|
|
|
|
* The order is furthest from the server to nearest e.g., (Browser, proxy1, proxy2,
|
|
|
|
|
* local-squid, ...)
|
2014-09-16 23:00:19 +00:00
|
|
|
* @throws MWException
|
2014-07-24 17:42:24 +00:00
|
|
|
* @return Block|null The "best" block from the list
|
2012-11-17 23:23:09 +00:00
|
|
|
*/
|
2013-04-26 14:42:31 +00:00
|
|
|
public static function chooseBlock( array $blocks, array $ipChain ) {
|
2012-11-17 23:23:09 +00:00
|
|
|
if ( !count( $blocks ) ) {
|
|
|
|
|
return null;
|
|
|
|
|
} elseif ( count( $blocks ) == 1 ) {
|
|
|
|
|
return $blocks[0];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Sort hard blocks before soft ones and secondarily sort blocks
|
|
|
|
|
// that disable account creation before those that don't.
|
2014-07-20 19:41:41 +00:00
|
|
|
usort( $blocks, function ( Block $a, Block $b ) {
|
2012-11-17 23:23:09 +00:00
|
|
|
$aWeight = (int)$a->isHardblock() . (int)$a->prevents( 'createaccount' );
|
|
|
|
|
$bWeight = (int)$b->isHardblock() . (int)$b->prevents( 'createaccount' );
|
|
|
|
|
return strcmp( $bWeight, $aWeight ); // highest weight first
|
|
|
|
|
} );
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$blocksListExact = [
|
2012-11-17 23:23:09 +00:00
|
|
|
'hard' => false,
|
|
|
|
|
'disable_create' => false,
|
|
|
|
|
'other' => false,
|
|
|
|
|
'auto' => false
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
|
|
|
|
$blocksListRange = [
|
2012-11-17 23:23:09 +00:00
|
|
|
'hard' => false,
|
|
|
|
|
'disable_create' => false,
|
|
|
|
|
'other' => false,
|
|
|
|
|
'auto' => false
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2012-11-17 23:23:09 +00:00
|
|
|
$ipChain = array_reverse( $ipChain );
|
|
|
|
|
|
2014-09-16 23:00:19 +00:00
|
|
|
/** @var Block $block */
|
2012-11-17 23:23:09 +00:00
|
|
|
foreach ( $blocks as $block ) {
|
|
|
|
|
// Stop searching if we have already have a "better" block. This
|
|
|
|
|
// is why the order of the blocks matters
|
|
|
|
|
if ( !$block->isHardblock() && $blocksListExact['hard'] ) {
|
|
|
|
|
break;
|
|
|
|
|
} elseif ( !$block->prevents( 'createaccount' ) && $blocksListExact['disable_create'] ) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach ( $ipChain as $checkip ) {
|
|
|
|
|
$checkipHex = IP::toHex( $checkip );
|
|
|
|
|
if ( (string)$block->getTarget() === $checkip ) {
|
|
|
|
|
if ( $block->isHardblock() ) {
|
|
|
|
|
$blocksListExact['hard'] = $blocksListExact['hard'] ?: $block;
|
|
|
|
|
} elseif ( $block->prevents( 'createaccount' ) ) {
|
|
|
|
|
$blocksListExact['disable_create'] = $blocksListExact['disable_create'] ?: $block;
|
|
|
|
|
} elseif ( $block->mAuto ) {
|
|
|
|
|
$blocksListExact['auto'] = $blocksListExact['auto'] ?: $block;
|
|
|
|
|
} else {
|
|
|
|
|
$blocksListExact['other'] = $blocksListExact['other'] ?: $block;
|
|
|
|
|
}
|
|
|
|
|
// We found closest exact match in the ip list, so go to the next Block
|
|
|
|
|
break;
|
2016-02-17 09:09:32 +00:00
|
|
|
} elseif ( array_filter( $blocksListExact ) == []
|
2012-11-17 23:23:09 +00:00
|
|
|
&& $block->getRangeStart() <= $checkipHex
|
|
|
|
|
&& $block->getRangeEnd() >= $checkipHex
|
|
|
|
|
) {
|
|
|
|
|
if ( $block->isHardblock() ) {
|
|
|
|
|
$blocksListRange['hard'] = $blocksListRange['hard'] ?: $block;
|
|
|
|
|
} elseif ( $block->prevents( 'createaccount' ) ) {
|
|
|
|
|
$blocksListRange['disable_create'] = $blocksListRange['disable_create'] ?: $block;
|
|
|
|
|
} elseif ( $block->mAuto ) {
|
|
|
|
|
$blocksListRange['auto'] = $blocksListRange['auto'] ?: $block;
|
|
|
|
|
} else {
|
|
|
|
|
$blocksListRange['other'] = $blocksListRange['other'] ?: $block;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
if ( array_filter( $blocksListExact ) == [] ) {
|
2012-11-17 23:23:09 +00:00
|
|
|
$blocksList = &$blocksListRange;
|
|
|
|
|
} else {
|
|
|
|
|
$blocksList = &$blocksListExact;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$chosenBlock = null;
|
|
|
|
|
if ( $blocksList['hard'] ) {
|
|
|
|
|
$chosenBlock = $blocksList['hard'];
|
|
|
|
|
} elseif ( $blocksList['disable_create'] ) {
|
|
|
|
|
$chosenBlock = $blocksList['disable_create'];
|
|
|
|
|
} elseif ( $blocksList['other'] ) {
|
|
|
|
|
$chosenBlock = $blocksList['other'];
|
|
|
|
|
} elseif ( $blocksList['auto'] ) {
|
|
|
|
|
$chosenBlock = $blocksList['auto'];
|
|
|
|
|
} else {
|
|
|
|
|
throw new MWException( "Proxy block found, but couldn't be classified." );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $chosenBlock;
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-13 14:47:34 +00:00
|
|
|
/**
|
2013-01-03 10:53:41 +00:00
|
|
|
* From an existing Block, get the target and the type of target.
|
|
|
|
|
* Note that, except for null, it is always safe to treat the target
|
|
|
|
|
* as a string; for User objects this will return User::__toString()
|
|
|
|
|
* which in turn gives User::getName().
|
2011-06-26 23:01:29 +00:00
|
|
|
*
|
2014-04-23 09:25:56 +00:00
|
|
|
* @param string|int|User|null $target
|
2013-01-03 10:53:41 +00:00
|
|
|
* @return array( User|String|null, Block::TYPE_ constant|null )
|
2011-03-13 14:47:34 +00:00
|
|
|
*/
|
2011-03-20 17:43:17 +00:00
|
|
|
public static function parseTarget( $target ) {
|
2011-03-21 19:12:41 +00:00
|
|
|
# We may have been through this before
|
2013-04-20 22:49:30 +00:00
|
|
|
if ( $target instanceof User ) {
|
|
|
|
|
if ( IP::isValid( $target->getName() ) ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [ $target, self::TYPE_IP ];
|
2011-03-21 19:12:41 +00:00
|
|
|
} else {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [ $target, self::TYPE_USER ];
|
2011-03-21 19:12:41 +00:00
|
|
|
}
|
2013-04-20 22:49:30 +00:00
|
|
|
} elseif ( $target === null ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [ null, null ];
|
2011-03-21 19:12:41 +00:00
|
|
|
}
|
|
|
|
|
|
2012-04-17 07:48:58 +00:00
|
|
|
$target = trim( $target );
|
|
|
|
|
|
2011-08-12 14:32:05 +00:00
|
|
|
if ( IP::isValid( $target ) ) {
|
|
|
|
|
# We can still create a User if it's an IP address, but we need to turn
|
|
|
|
|
# off validation checking (which would exclude IP addresses)
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
2011-08-12 14:32:05 +00:00
|
|
|
User::newFromName( IP::sanitizeIP( $target ), false ),
|
|
|
|
|
Block::TYPE_IP
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2011-08-12 14:32:05 +00:00
|
|
|
|
|
|
|
|
} elseif ( IP::isValidBlock( $target ) ) {
|
|
|
|
|
# Can't create a User from an IP range
|
2016-02-17 09:09:32 +00:00
|
|
|
return [ IP::sanitizeRange( $target ), Block::TYPE_RANGE ];
|
2011-08-12 14:32:05 +00:00
|
|
|
}
|
|
|
|
|
|
2011-07-11 16:53:31 +00:00
|
|
|
# Consider the possibility that this is not a username at all
|
|
|
|
|
# but actually an old subpage (bug #29797)
|
2013-04-20 22:49:30 +00:00
|
|
|
if ( strpos( $target, '/' ) !== false ) {
|
2011-07-11 16:53:31 +00:00
|
|
|
# An old subpage, drill down to the user behind it
|
2016-02-17 19:54:59 +00:00
|
|
|
$target = explode( '/', $target )[0];
|
2011-07-11 16:53:31 +00:00
|
|
|
}
|
|
|
|
|
|
2011-03-13 14:47:34 +00:00
|
|
|
$userObj = User::newFromName( $target );
|
2011-03-20 17:43:17 +00:00
|
|
|
if ( $userObj instanceof User ) {
|
2011-03-19 16:50:21 +00:00
|
|
|
# Note that since numbers are valid usernames, a $target of "12345" will be
|
|
|
|
|
# considered a User. If you want to pass a block ID, prepend a hash "#12345",
|
|
|
|
|
# since hash characters are not valid in usernames or titles generally.
|
2016-02-17 09:09:32 +00:00
|
|
|
return [ $userObj, Block::TYPE_USER ];
|
2011-03-13 14:47:34 +00:00
|
|
|
|
2011-03-20 17:43:17 +00:00
|
|
|
} elseif ( preg_match( '/^#\d+$/', $target ) ) {
|
2011-03-13 14:47:34 +00:00
|
|
|
# Autoblock reference in the form "#12345"
|
2016-02-17 09:09:32 +00:00
|
|
|
return [ substr( $target, 1 ), Block::TYPE_AUTO ];
|
2011-03-13 14:47:34 +00:00
|
|
|
|
2011-03-19 16:50:21 +00:00
|
|
|
} else {
|
|
|
|
|
# WTF?
|
2016-02-17 09:09:32 +00:00
|
|
|
return [ null, null ];
|
2011-03-13 14:47:34 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2011-03-21 19:12:41 +00:00
|
|
|
* Get the type of target for this particular block
|
2014-04-23 09:25:56 +00:00
|
|
|
* @return int Block::TYPE_ constant, will never be TYPE_ID
|
2011-03-21 19:12:41 +00:00
|
|
|
*/
|
|
|
|
|
public function getType() {
|
|
|
|
|
return $this->mAuto
|
|
|
|
|
? self::TYPE_AUTO
|
|
|
|
|
: $this->type;
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-21 23:27:08 +00:00
|
|
|
/**
|
|
|
|
|
* Get the target and target type for this particular Block. Note that for autoblocks,
|
|
|
|
|
* this returns the unredacted name; frontend functions need to call $block->getRedactedName()
|
|
|
|
|
* in this situation.
|
|
|
|
|
* @return array( User|String, Block::TYPE_ constant )
|
2011-05-17 22:03:20 +00:00
|
|
|
* @todo FIXME: This should be an integral part of the Block member variables
|
2011-03-21 23:27:08 +00:00
|
|
|
*/
|
|
|
|
|
public function getTargetAndType() {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [ $this->getTarget(), $this->getType() ];
|
2011-03-21 23:27:08 +00:00
|
|
|
}
|
|
|
|
|
|
2011-03-21 19:12:41 +00:00
|
|
|
/**
|
|
|
|
|
* Get the target for this particular Block. Note that for autoblocks,
|
2011-03-13 15:14:33 +00:00
|
|
|
* this returns the unredacted name; frontend functions need to call $block->getRedactedName()
|
|
|
|
|
* in this situation.
|
2014-04-23 09:25:56 +00:00
|
|
|
* @return User|string
|
2011-03-13 14:47:34 +00:00
|
|
|
*/
|
2011-03-21 19:12:41 +00:00
|
|
|
public function getTarget() {
|
|
|
|
|
return $this->target;
|
|
|
|
|
}
|
2011-03-13 15:14:33 +00:00
|
|
|
|
2011-12-06 15:23:21 +00:00
|
|
|
/**
|
|
|
|
|
* @since 1.19
|
|
|
|
|
*
|
2014-04-23 09:25:56 +00:00
|
|
|
* @return mixed|string
|
2011-12-06 15:23:21 +00:00
|
|
|
*/
|
|
|
|
|
public function getExpiry() {
|
|
|
|
|
return $this->mExpiry;
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-21 19:12:41 +00:00
|
|
|
/**
|
|
|
|
|
* Set the target for this block, and update $this->type accordingly
|
2014-04-23 09:25:56 +00:00
|
|
|
* @param mixed $target
|
2011-03-21 19:12:41 +00:00
|
|
|
*/
|
2012-12-20 15:09:25 +00:00
|
|
|
public function setTarget( $target ) {
|
2011-03-21 19:12:41 +00:00
|
|
|
list( $this->target, $this->type ) = self::parseTarget( $target );
|
2011-03-13 14:47:34 +00:00
|
|
|
}
|
2011-03-13 21:33:52 +00:00
|
|
|
|
2011-03-21 19:12:41 +00:00
|
|
|
/**
|
|
|
|
|
* Get the user who implemented this block
|
2011-11-01 00:11:53 +00:00
|
|
|
* @return User|string Local User object or string for a foreign user
|
2011-03-21 19:12:41 +00:00
|
|
|
*/
|
2012-12-20 15:09:25 +00:00
|
|
|
public function getBlocker() {
|
2011-03-21 19:12:41 +00:00
|
|
|
return $this->blocker;
|
2011-03-13 21:33:52 +00:00
|
|
|
}
|
|
|
|
|
|
2011-03-21 19:12:41 +00:00
|
|
|
/**
|
|
|
|
|
* Set the user who implemented (or will implement) this block
|
2014-04-23 09:25:56 +00:00
|
|
|
* @param User|string $user Local User object or username string for foreign users
|
2011-03-21 19:12:41 +00:00
|
|
|
*/
|
2012-12-20 15:09:25 +00:00
|
|
|
public function setBlocker( $user ) {
|
2011-03-21 19:12:41 +00:00
|
|
|
$this->blocker = $user;
|
2011-03-13 21:33:52 +00:00
|
|
|
}
|
2013-04-03 21:44:00 +00:00
|
|
|
|
2013-02-07 21:56:54 +00:00
|
|
|
/**
|
|
|
|
|
* Set the 'BlockID' cookie to this block's ID and expiry time. The cookie's expiry will be
|
|
|
|
|
* the same as the block's, unless it's greater than $wgCookieExpiration in which case
|
|
|
|
|
* $wgCookieExpiration will be used instead (defaults to 30 days).
|
|
|
|
|
*
|
|
|
|
|
* An empty value can also be set, in order to retain the cookie but remove the block ID
|
|
|
|
|
* (e.g. as used in User::getBlockedStatus).
|
|
|
|
|
*
|
|
|
|
|
* @param WebResponse $response The response on which to set the cookie.
|
|
|
|
|
* @param boolean $setEmpty Whether to set the cookie's value to the empty string.
|
|
|
|
|
*/
|
|
|
|
|
public function setCookie( WebResponse $response, $setEmpty = false ) {
|
|
|
|
|
// Calculate the default expiry time.
|
|
|
|
|
$config = RequestContext::getMain()->getConfig();
|
|
|
|
|
$defaultExpiry = wfTimestamp() + $config->get( 'CookieExpiration' );
|
|
|
|
|
|
|
|
|
|
// Use the Block's expiry time only if it's less than the default.
|
|
|
|
|
$expiry = wfTimestamp( TS_UNIX, $this->getExpiry() );
|
|
|
|
|
if ( $expiry > $defaultExpiry ) {
|
|
|
|
|
// The *default* default expiry is 30 days.
|
|
|
|
|
$expiry = $defaultExpiry;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$cookieValue = $setEmpty ? '' : $this->getId();
|
|
|
|
|
$response->setCookie( 'BlockID', $cookieValue, $expiry );
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-03 21:44:00 +00:00
|
|
|
/**
|
|
|
|
|
* Get the key and parameters for the corresponding error message.
|
|
|
|
|
*
|
|
|
|
|
* @since 1.22
|
|
|
|
|
* @param IContextSource $context
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public function getPermissionsError( IContextSource $context ) {
|
|
|
|
|
$blocker = $this->getBlocker();
|
|
|
|
|
if ( $blocker instanceof User ) { // local user
|
|
|
|
|
$blockerUserpage = $blocker->getUserPage();
|
|
|
|
|
$link = "[[{$blockerUserpage->getPrefixedText()}|{$blockerUserpage->getText()}]]";
|
|
|
|
|
} else { // foreign user
|
|
|
|
|
$link = $blocker;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$reason = $this->mReason;
|
|
|
|
|
if ( $reason == '' ) {
|
|
|
|
|
$reason = $context->msg( 'blockednoreason' )->text();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* $ip returns who *is* being blocked, $intended contains who was meant to be blocked.
|
|
|
|
|
* This could be a username, an IP range, or a single IP. */
|
|
|
|
|
$intended = $this->getTarget();
|
|
|
|
|
|
|
|
|
|
$lang = $context->getLanguage();
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
2013-04-03 21:44:00 +00:00
|
|
|
$this->mAuto ? 'autoblockedtext' : 'blockedtext',
|
|
|
|
|
$link,
|
|
|
|
|
$reason,
|
|
|
|
|
$context->getRequest()->getIP(),
|
|
|
|
|
$this->getByName(),
|
|
|
|
|
$this->getId(),
|
|
|
|
|
$lang->formatExpiry( $this->mExpiry ),
|
|
|
|
|
(string)$intended,
|
2014-07-08 20:13:01 +00:00
|
|
|
$lang->userTimeAndDate( $this->mTimestamp, $context->getUser() ),
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2013-04-03 21:44:00 +00:00
|
|
|
}
|
2003-09-01 13:13:56 +00:00
|
|
|
}
|