3 * Implements Special:Blockip
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @ingroup SpecialPage
25 * A special page that allows users with 'block' right to block users from
26 * editing pages and other actions
28 * @ingroup SpecialPage
30 class IPBlockForm
extends SpecialPage
{
31 var $BlockAddress, $BlockExpiry, $BlockReason;
32 // The maximum number of edits a user can have and still be hidden
33 const HIDEUSER_CONTRIBLIMIT
= 1000;
35 public function __construct() {
36 parent
::__construct( 'Blockip', 'block' );
39 public function execute( $par ) {
40 global $wgUser, $wgOut, $wgRequest;
42 # Can't block when the database is locked
44 $wgOut->readOnlyPage();
48 if( !$this->userCanExecute( $wgUser ) ) {
49 $wgOut->permissionRequired( 'block' );
55 # bug 15810: blocked admins should have limited access here
56 if ( $wgUser->isBlocked() ) {
57 $status = IPBlockForm
::checkUnblockSelf( $ipb->BlockAddress
);
58 if ( $status !== true ) {
59 throw new ErrorPageError( 'badaccess', $status );
63 $action = $wgRequest->getVal( 'action' );
64 if( 'success' == $action ) {
66 } elseif( $wgRequest->wasPosted() && 'submit' == $action &&
67 $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ) ) ) {
70 $this->showForm( '' );
74 private function setup( $par ) {
75 global $wgRequest, $wgUser, $wgBlockAllowsUTEdit;
77 $this->BlockAddress
= $wgRequest->getVal( 'wpBlockAddress', $wgRequest->getVal( 'ip', $par ) );
78 $this->BlockAddress
= strtr( $this->BlockAddress
, '_', ' ' );
79 $this->BlockReason
= $wgRequest->getText( 'wpBlockReason' );
80 $this->BlockReasonList
= $wgRequest->getText( 'wpBlockReasonList' );
81 $this->BlockExpiry
= $wgRequest->getVal( 'wpBlockExpiry', wfMsg( 'ipbotheroption' ) );
82 $this->BlockOther
= $wgRequest->getVal( 'wpBlockOther', '' );
84 # Unchecked checkboxes are not included in the form data at all, so having one
85 # that is true by default is a bit tricky
86 $byDefault = !$wgRequest->wasPosted();
87 $this->BlockAnonOnly
= $wgRequest->getBool( 'wpAnonOnly', $byDefault );
88 $this->BlockCreateAccount
= $wgRequest->getBool( 'wpCreateAccount', $byDefault );
89 $this->BlockEnableAutoblock
= $wgRequest->getBool( 'wpEnableAutoblock', $byDefault );
90 $this->BlockEmail
= false;
91 if( self
::canBlockEmail( $wgUser ) ) {
92 $this->BlockEmail
= $wgRequest->getBool( 'wpEmailBan', false );
94 $this->BlockWatchUser
= $wgRequest->getBool( 'wpWatchUser', false ) && $wgUser->isLoggedIn();
95 # Re-check user's rights to hide names, very serious, defaults to null
96 if( $wgUser->isAllowed( 'hideuser' ) ) {
97 $this->BlockHideName
= $wgRequest->getBool( 'wpHideName', null );
99 $this->BlockHideName
= false;
101 $this->BlockAllowUsertalk
= ( $wgRequest->getBool( 'wpAllowUsertalk', $byDefault ) && $wgBlockAllowsUTEdit );
102 $this->BlockReblock
= $wgRequest->getBool( 'wpChangeBlock', false );
104 $this->wasPosted
= $wgRequest->wasPosted();
107 public function showForm( $err ) {
108 global $wgOut, $wgUser, $wgSysopUserBans;
110 $wgOut->setPageTitle( wfMsg( 'blockip-title' ) );
111 $wgOut->addWikiMsg( 'blockiptext' );
113 if( $wgSysopUserBans ) {
114 $mIpaddress = Xml
::label( wfMsg( 'ipadressorusername' ), 'mw-bi-target' );
116 $mIpaddress = Xml
::label( wfMsg( 'ipaddress' ), 'mw-bi-target' );
118 $mIpbexpiry = Xml
::label( wfMsg( 'ipbexpiry' ), 'wpBlockExpiry' );
119 $mIpbother = Xml
::label( wfMsg( 'ipbother' ), 'mw-bi-other' );
120 $mIpbreasonother = Xml
::label( wfMsg( 'ipbreason' ), 'wpBlockReasonList' );
121 $mIpbreason = Xml
::label( wfMsg( 'ipbotherreason' ), 'mw-bi-reason' );
123 $titleObj = SpecialPage
::getTitleFor( 'Blockip' );
124 $user = User
::newFromName( $this->BlockAddress
);
126 $alreadyBlocked = false;
127 $otherBlockedMsgs = array();
128 if( $err && $err[0] != 'ipb_already_blocked' ) {
129 $key = array_shift( $err );
130 $msg = wfMsgReal( $key, $err );
131 $wgOut->setSubtitle( wfMsgHtml( 'formerror' ) );
132 $wgOut->addHTML( Xml
::tags( 'p', array( 'class' => 'error' ), $msg ) );
133 } elseif( $this->BlockAddress
!== null ) {
134 # Get other blocks, i.e. from GlobalBlocking or TorBlock extension
135 wfRunHooks( 'OtherBlockLogLink', array( &$otherBlockedMsgs, $this->BlockAddress
) );
137 $userId = is_object( $user ) ?
$user->getId() : 0;
138 $currentBlock = Block
::newFromDB( $this->BlockAddress
, $userId );
139 if( !is_null( $currentBlock ) && !$currentBlock->mAuto
&& # The block exists and isn't an autoblock
140 ( $currentBlock->mRangeStart
== $currentBlock->mRangeEnd ||
# The block isn't a rangeblock
141 # or if it is, the range is what we're about to block
142 ( $currentBlock->mAddress
== $this->BlockAddress
) )
144 $alreadyBlocked = true;
145 # Set the block form settings to the existing block
146 if( !$this->wasPosted
) {
147 $this->BlockAnonOnly
= $currentBlock->mAnonOnly
;
148 $this->BlockCreateAccount
= $currentBlock->mCreateAccount
;
149 $this->BlockEnableAutoblock
= $currentBlock->mEnableAutoblock
;
150 $this->BlockEmail
= $currentBlock->mBlockEmail
;
151 $this->BlockHideName
= $currentBlock->mHideName
;
152 $this->BlockAllowUsertalk
= $currentBlock->mAllowUsertalk
;
153 if( $currentBlock->mExpiry
== 'infinity' ) {
154 $this->BlockOther
= 'indefinite';
156 $this->BlockOther
= wfTimestamp( TS_ISO_8601
, $currentBlock->mExpiry
);
158 $this->BlockReason
= $currentBlock->mReason
;
163 # Show other blocks from extensions, i.e. GlockBlocking and TorBlock
164 if( count( $otherBlockedMsgs ) ) {
166 Html
::rawElement( 'h2', array(), wfMsgExt( 'ipb-otherblocks-header', 'parseinline', count( $otherBlockedMsgs ) ) ) . "\n"
169 foreach( $otherBlockedMsgs as $link ) {
170 $list .= Html
::rawElement( 'li', array(), $link ) . "\n";
172 $wgOut->addHTML( Html
::rawElement( 'ul', array( 'class' => 'mw-blockip-alreadyblocked' ), $list ) . "\n" );
175 # Username/IP is blocked already locally
176 if( $alreadyBlocked ) {
177 $wgOut->wrapWikiMsg( "<div class='mw-ipb-needreblock'>\n$1\n</div>", array( 'ipb-needreblock', $this->BlockAddress
) );
180 $scBlockExpiryOptions = wfMsgForContent( 'ipboptions' );
182 $showblockoptions = $scBlockExpiryOptions != '-';
183 if( !$showblockoptions ) $mIpbother = $mIpbexpiry;
185 $blockExpiryFormOptions = Xml
::option( wfMsg( 'ipbotheroption' ), 'other' );
186 foreach( explode( ',', $scBlockExpiryOptions ) as $option ) {
187 if( strpos( $option, ':' ) === false ) $option = "$option:$option";
188 list( $show, $value ) = explode( ':', $option );
189 $show = htmlspecialchars( $show );
190 $value = htmlspecialchars( $value );
191 $blockExpiryFormOptions .= Xml
::option( $show, $value, $this->BlockExpiry
=== $value ?
true : false ) . "\n";
194 $reasonDropDown = Xml
::listDropDown( 'wpBlockReasonList',
195 wfMsgForContent( 'ipbreason-dropdown' ),
196 wfMsgForContent( 'ipbreasonotherlist' ), $this->BlockReasonList
, 'wpBlockDropDown', 4 );
198 global $wgStylePath, $wgStyleVersion;
200 Xml
::tags( 'script', array( 'type' => 'text/javascript', 'src' => "$wgStylePath/common/block.js?$wgStyleVersion" ), '' ) .
201 Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $titleObj->getLocalURL( 'action=submit' ), 'id' => 'blockip' ) ) .
202 Xml
::openElement( 'fieldset' ) .
203 Xml
::element( 'legend', null, wfMsg( 'blockip-legend' ) ) .
204 Xml
::openElement( 'table', array( 'border' => '0', 'id' => 'mw-blockip-table' ) ) .
206 <td class='mw-label'>
209 <td class='mw-input'>" .
210 Html
::input( 'wpBlockAddress', $this->BlockAddress
, 'text', array(
212 'id' => 'mw-bi-target',
213 'onchange' => 'updateBlockOptions()',
216 ) +
( $this->BlockAddress ?
array() : array( 'autofocus' ) ) ). "
221 if( $showblockoptions ) {
223 <td class='mw-label'>
226 <td class='mw-input'>" .
229 'id' => 'wpBlockExpiry',
230 'name' => 'wpBlockExpiry',
231 'onchange' => 'considerChangingExpiryFocus()',
233 $blockExpiryFormOptions ) .
239 <tr id='wpBlockOther'>
240 <td class='mw-label'>
243 <td class='mw-input'>" .
244 Xml
::input( 'wpBlockOther', 45, $this->BlockOther
,
245 array( 'tabindex' => '3', 'id' => 'mw-bi-other' ) ) . "
249 <td class='mw-label'>
252 <td class='mw-input'>
256 <tr id=\"wpBlockReason\">
257 <td class='mw-label'>
260 <td class='mw-input'>" .
261 Html
::input( 'wpBlockReason', $this->BlockReason
, 'text', array(
263 'id' => 'mw-bi-reason',
264 'maxlength' => '200',
266 ) +
( $this->BlockAddress ?
array( 'autofocus' ) : array() ) ) . "
269 <tr id='wpAnonOnlyRow'>
271 <td class='mw-input'>" .
272 Xml
::checkLabel( wfMsg( 'ipbanononly' ),
273 'wpAnonOnly', 'wpAnonOnly', $this->BlockAnonOnly
,
274 array( 'tabindex' => '6' ) ) . "
277 <tr id='wpCreateAccountRow'>
279 <td class='mw-input'>" .
280 Xml
::checkLabel( wfMsg( 'ipbcreateaccount' ),
281 'wpCreateAccount', 'wpCreateAccount', $this->BlockCreateAccount
,
282 array( 'tabindex' => '7' ) ) . "
285 <tr id='wpEnableAutoblockRow'>
287 <td class='mw-input'>" .
288 Xml
::checkLabel( wfMsg( 'ipbenableautoblock' ),
289 'wpEnableAutoblock', 'wpEnableAutoblock', $this->BlockEnableAutoblock
,
290 array( 'tabindex' => '8' ) ) . "
295 if( self
::canBlockEmail( $wgUser ) ) {
297 <tr id='wpEnableEmailBan'>
299 <td class='mw-input'>" .
300 Xml
::checkLabel( wfMsg( 'ipbemailban' ),
301 'wpEmailBan', 'wpEmailBan', $this->BlockEmail
,
302 array( 'tabindex' => '9' ) ) . "
308 // Allow some users to hide name from block log, blocklist and listusers
309 if( $wgUser->isAllowed( 'hideuser' ) ) {
311 <tr id='wpEnableHideUser'>
313 <td class='mw-input'><strong>" .
314 Xml
::checkLabel( wfMsg( 'ipbhidename' ),
315 'wpHideName', 'wpHideName', $this->BlockHideName
,
316 array( 'tabindex' => '10' )
323 # Watchlist their user page? (Only if user is logged in)
324 if( $wgUser->isLoggedIn() ) {
326 <tr id='wpEnableWatchUser'>
328 <td class='mw-input'>" .
329 Xml
::checkLabel( wfMsg( 'ipbwatchuser' ),
330 'wpWatchUser', 'wpWatchUser', $this->BlockWatchUser
,
331 array( 'tabindex' => '11' ) ) . "
337 # Can we explicitly disallow the use of user_talk?
338 global $wgBlockAllowsUTEdit;
339 if( $wgBlockAllowsUTEdit ){
341 <tr id='wpAllowUsertalkRow'>
343 <td class='mw-input'>" .
344 Xml
::checkLabel( wfMsg( 'ipballowusertalk' ),
345 'wpAllowUsertalk', 'wpAllowUsertalk', $this->BlockAllowUsertalk
,
346 array( 'tabindex' => '12' ) ) . "
354 <td style='padding-top: 1em'> </td>
355 <td class='mw-submit' style='padding-top: 1em'>" .
356 Xml
::submitButton( wfMsg( $alreadyBlocked ?
'ipb-change-block' : 'ipbsubmit' ),
357 array( 'name' => 'wpBlock', 'tabindex' => '13' )
358 +
$wgUser->getSkin()->tooltipAndAccessKeyAttribs( 'blockip-block' ) ). "
361 Xml
::closeElement( 'table' ) .
362 Xml
::hidden( 'wpEditToken', $wgUser->editToken() ) .
363 ( $alreadyBlocked ? Xml
::hidden( 'wpChangeBlock', 1 ) : "" ) .
364 Xml
::closeElement( 'fieldset' ) .
365 Xml
::closeElement( 'form' ) .
366 Xml
::tags( 'script', array( 'type' => 'text/javascript' ), 'updateBlockOptions()' ) . "\n"
369 $wgOut->addHTML( $this->getConvenienceLinks() );
371 if( is_object( $user ) ) {
372 $this->showLogFragment( $wgOut, $user->getUserPage() );
373 } elseif( preg_match( '/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/', $this->BlockAddress
) ) {
374 $this->showLogFragment( $wgOut, Title
::makeTitle( NS_USER
, $this->BlockAddress
) );
375 } elseif( preg_match( '/^\w{1,4}:\w{1,4}:\w{1,4}:\w{1,4}:\w{1,4}:\w{1,4}:\w{1,4}:\w{1,4}/', $this->BlockAddress
) ) {
376 $this->showLogFragment( $wgOut, Title
::makeTitle( NS_USER
, $this->BlockAddress
) );
381 * Can we do an email block?
382 * @param $user User: the sysop wanting to make a block
385 public static function canBlockEmail( $user ) {
386 global $wgEnableUserEmail, $wgSysopEmailBans;
387 return ( $wgEnableUserEmail && $wgSysopEmailBans && $user->isAllowed( 'blockemail' ) );
391 * bug 15810: blocked admins should not be able to block/unblock
392 * others, and probably shouldn't be able to unblock themselves
394 * @param $user User, Int or String
396 public static function checkUnblockSelf( $user ) {
398 if ( is_int( $user ) ) {
399 $user = User
::newFromId( $user );
400 } elseif ( is_string( $user ) ) {
401 $user = User
::newFromName( $user );
403 if( $user instanceof User
&& $user->getId() == $wgUser->getId() ) {
404 # User is trying to unblock themselves
405 if ( $wgUser->isAllowed( 'unblockself' ) ) {
408 return 'ipbnounblockself';
411 # User is trying to block/unblock someone else
417 * Backend block code.
418 * $userID and $expiry will be filled accordingly
419 * @return array(message key, arguments) on failure, empty array on success
421 function doBlock( &$userId = null, &$expiry = null ) {
422 global $wgUser, $wgSysopUserBans, $wgSysopRangeBans, $wgBlockAllowsUTEdit, $wgBlockCIDRLimit;
425 # Expand valid IPv6 addresses, usernames are left as is
426 $this->BlockAddress
= IP
::sanitizeIP( $this->BlockAddress
);
427 # isIPv4() and IPv6() are used for final validation
428 $rxIP4 = '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}';
429 $rxIP6 = '\w{1,4}:\w{1,4}:\w{1,4}:\w{1,4}:\w{1,4}:\w{1,4}:\w{1,4}:\w{1,4}';
430 $rxIP = "($rxIP4|$rxIP6)";
432 # Check for invalid specifications
433 if( !preg_match( "/^$rxIP$/", $this->BlockAddress
) ) {
435 if( preg_match( "/^($rxIP4)\\/(\\d{1,2})$/", $this->BlockAddress
, $matches ) ) {
437 if( $wgSysopRangeBans ) {
438 if( !IP
::isIPv4( $this->BlockAddress
) ||
$matches[2] > 32 ) {
439 return array( 'ip_range_invalid' );
440 } elseif ( $matches[2] < $wgBlockCIDRLimit['IPv4'] ) {
441 return array( 'ip_range_toolarge', $wgBlockCIDRLimit['IPv4'] );
443 $this->BlockAddress
= Block
::normaliseRange( $this->BlockAddress
);
445 # Range block illegal
446 return array( 'range_block_disabled' );
448 } elseif( preg_match( "/^($rxIP6)\\/(\\d{1,3})$/", $this->BlockAddress
, $matches ) ) {
450 if( $wgSysopRangeBans ) {
451 if( !IP
::isIPv6( $this->BlockAddress
) ||
$matches[2] > 128 ) {
452 return array( 'ip_range_invalid' );
453 } elseif( $matches[2] < $wgBlockCIDRLimit['IPv6'] ) {
454 return array( 'ip_range_toolarge', $wgBlockCIDRLimit['IPv6'] );
456 $this->BlockAddress
= Block
::normaliseRange( $this->BlockAddress
);
458 # Range block illegal
459 return array('range_block_disabled');
463 if( $wgSysopUserBans ) {
464 $user = User
::newFromName( $this->BlockAddress
);
465 if( $user instanceof User
&& $user->getId() ) {
467 $userId = $user->getId();
468 $this->BlockAddress
= $user->getName();
470 return array( 'nosuchusershort', htmlspecialchars( $user ?
$user->getName() : $this->BlockAddress
) );
473 return array( 'badipaddress' );
478 if( $wgUser->isBlocked() && ( $wgUser->getId() !== $userId ) ) {
479 return array( 'cant-block-while-blocked' );
482 $reasonstr = $this->BlockReasonList
;
483 if( $reasonstr != 'other' && $this->BlockReason
!= '' ) {
484 // Entry from drop down menu + additional comment
485 $reasonstr .= wfMsgForContent( 'colon-separator' ) . $this->BlockReason
;
486 } elseif( $reasonstr == 'other' ) {
487 $reasonstr = $this->BlockReason
;
490 $expirestr = $this->BlockExpiry
;
491 if( $expirestr == 'other' )
492 $expirestr = $this->BlockOther
;
494 if( ( strlen( $expirestr ) == 0) ||
( strlen( $expirestr ) > 50 ) ) {
495 return array( 'ipb_expiry_invalid' );
498 if( false === ( $expiry = Block
::parseExpiryInput( $expirestr ) ) ) {
500 return array( 'ipb_expiry_invalid' );
503 if( $this->BlockHideName
) {
504 // Recheck params here...
505 if( !$userId ||
!$wgUser->isAllowed('hideuser') ) {
506 $this->BlockHideName
= false; // IP users should not be hidden
507 } elseif( $expiry !== 'infinity' ) {
509 return array( 'ipb_expiry_temp' );
510 } elseif( User
::edits( $userId ) > self
::HIDEUSER_CONTRIBLIMIT
) {
511 // Typically, the user should have a handful of edits.
512 // Disallow hiding users with many edits for performance.
513 return array( 'ipb_hide_invalid' );
517 # Create block object
518 # Note: for a user block, ipb_address is only for display purposes
519 $block = new Block( $this->BlockAddress
, $userId, $wgUser->getId(),
520 $reasonstr, wfTimestampNow(), 0, $expiry, $this->BlockAnonOnly
,
521 $this->BlockCreateAccount
, $this->BlockEnableAutoblock
, $this->BlockHideName
,
523 isset( $this->BlockAllowUsertalk
) ?
$this->BlockAllowUsertalk
: $wgBlockAllowsUTEdit
526 # Should this be privately logged?
527 $suppressLog = (bool)$this->BlockHideName
;
528 if( wfRunHooks( 'BlockIp', array( &$block, &$wgUser ) ) ) {
529 # Try to insert block. Is there a conflicting block?
530 if( !$block->insert() ) {
531 # Show form unless the user is already aware of this...
532 if( !$this->BlockReblock
) {
533 return array( 'ipb_already_blocked' );
534 # Otherwise, try to update the block...
536 # This returns direct blocks before autoblocks/rangeblocks, since we should
537 # be sure the user is blocked by now it should work for our purposes
538 $currentBlock = Block
::newFromDB( $this->BlockAddress
, $userId );
539 if( $block->equals( $currentBlock ) ) {
540 return array( 'ipb_already_blocked' );
542 # If the name was hidden and the blocking user cannot hide
543 # names, then don't allow any block changes...
544 if( $currentBlock->mHideName
&& !$wgUser->isAllowed( 'hideuser' ) ) {
545 return array( 'cant-see-hidden-user' );
547 $currentBlock->delete();
549 # If hiding/unhiding a name, this should go in the private logs
550 $suppressLog = $suppressLog ||
(bool)$currentBlock->mHideName
;
551 $log_action = 'reblock';
552 # Unset _deleted fields if requested
553 if( $currentBlock->mHideName
&& !$this->BlockHideName
) {
554 self
::unsuppressUserName( $this->BlockAddress
, $userId );
558 $log_action = 'block';
560 wfRunHooks( 'BlockIpComplete', array( $block, $wgUser ) );
562 # Set *_deleted fields if requested
563 if( $this->BlockHideName
) {
564 self
::suppressUserName( $this->BlockAddress
, $userId );
567 # Only show watch link when this is no range block
568 if( $this->BlockWatchUser
&& $block->mRangeStart
== $block->mRangeEnd
) {
569 $wgUser->addWatch( Title
::makeTitle( NS_USER
, $this->BlockAddress
) );
572 # Block constructor sanitizes certain block options on insert
573 $this->BlockEmail
= $block->mBlockEmail
;
574 $this->BlockEnableAutoblock
= $block->mEnableAutoblock
;
576 # Prepare log parameters
577 $logParams = array();
578 $logParams[] = $expirestr;
579 $logParams[] = $this->blockLogFlags();
581 # Make log entry, if the name is hidden, put it in the oversight log
582 $log_type = $suppressLog ?
'suppress' : 'block';
583 $log = new LogPage( $log_type );
584 $log->addEntry( $log_action, Title
::makeTitle( NS_USER
, $this->BlockAddress
),
585 $reasonstr, $logParams );
590 return array( 'hookaborted' );
594 public static function suppressUserName( $name, $userId, $dbw = null ) {
595 $op = '|'; // bitwise OR
596 return self
::setUsernameBitfields( $name, $userId, $op, $dbw );
599 public static function unsuppressUserName( $name, $userId, $dbw = null ) {
600 $op = '&'; // bitwise AND
601 return self
::setUsernameBitfields( $name, $userId, $op, $dbw );
604 private static function setUsernameBitfields( $name, $userId, $op, $dbw ) {
605 if( $op !== '|' && $op !== '&' ) return false; // sanity check
607 $dbw = wfGetDB( DB_MASTER
);
608 $delUser = Revision
::DELETED_USER | Revision
::DELETED_RESTRICTED
;
609 $delAction = LogPage
::DELETED_ACTION | Revision
::DELETED_RESTRICTED
;
610 # Normalize user name
611 $userTitle = Title
::makeTitleSafe( NS_USER
, $name );
612 $userDbKey = $userTitle->getDBkey();
613 # To suppress, we OR the current bitfields with Revision::DELETED_USER
614 # to put a 1 in the username *_deleted bit. To unsuppress we AND the
615 # current bitfields with the inverse of Revision::DELETED_USER. The
616 # username bit is made to 0 (x & 0 = 0), while others are unchanged (x & 1 = x).
617 # The same goes for the sysop-restricted *_deleted bit.
619 $delUser = "~{$delUser}";
620 $delAction = "~{$delAction}";
622 # Hide name from live edits
623 $dbw->update( 'revision', array( "rev_deleted = rev_deleted $op $delUser" ),
624 array( 'rev_user' => $userId ), __METHOD__
);
625 # Hide name from deleted edits
626 $dbw->update( 'archive', array( "ar_deleted = ar_deleted $op $delUser" ),
627 array( 'ar_user_text' => $name ), __METHOD__
);
628 # Hide name from logs
629 $dbw->update( 'logging', array( "log_deleted = log_deleted $op $delUser" ),
630 array( 'log_user' => $userId, "log_type != 'suppress'" ), __METHOD__
);
631 $dbw->update( 'logging', array( "log_deleted = log_deleted $op $delAction" ),
632 array( 'log_namespace' => NS_USER
, 'log_title' => $userDbKey,
633 "log_type != 'suppress'" ), __METHOD__
);
635 $dbw->update( 'recentchanges', array( "rc_deleted = rc_deleted $op $delUser" ),
636 array( 'rc_user_text' => $name ), __METHOD__
);
637 $dbw->update( 'recentchanges', array( "rc_deleted = rc_deleted $op $delAction" ),
638 array( 'rc_namespace' => NS_USER
, 'rc_title' => $userDbKey, 'rc_logid > 0' ), __METHOD__
);
639 # Hide name from live images
640 $dbw->update( 'oldimage', array( "oi_deleted = oi_deleted $op $delUser" ),
641 array( 'oi_user_text' => $name ), __METHOD__
);
642 # Hide name from deleted images
643 # WMF - schema change pending
644 # $dbw->update( 'filearchive', array( "fa_deleted = fa_deleted $op $delUser" ),
645 # array( 'fa_user_text' => $name ), __METHOD__ );
651 * UI entry point for blocking
652 * Wraps around doBlock()
654 public function doSubmit() {
656 $retval = $this->doBlock();
657 if( empty( $retval ) ) {
658 $titleObj = SpecialPage
::getTitleFor( 'Blockip' );
659 $wgOut->redirect( $titleObj->getFullURL( 'action=success&ip=' .
660 urlencode( $this->BlockAddress
) ) );
663 $this->showForm( $retval );
666 public function showSuccess() {
669 $wgOut->setPageTitle( wfMsg( 'blockip-title' ) );
670 $wgOut->setSubtitle( wfMsg( 'blockipsuccesssub' ) );
671 $text = wfMsgExt( 'blockipsuccesstext', array( 'parse' ), $this->BlockAddress
);
672 $wgOut->addHTML( $text );
675 private function showLogFragment( $out, $title ) {
678 // Used to support GENDER in 'blocklog-showlog' and 'blocklog-showsuppresslog'
679 $userBlocked = $title->getText();
681 LogEventsList
::showLogExtract(
684 $title->getPrefixedText(),
692 'showIfEmpty' => false
696 // Add suppression block entries if allowed
697 if( $wgUser->isAllowed( 'suppressionlog' ) ) {
698 LogEventsList
::showLogExtract( $out, 'suppress', $title->getPrefixedText(), '',
702 'log_action' => array(
709 'blocklog-showsuppresslog',
712 'showIfEmpty' => false
719 * Return a comma-delimited list of "flags" to be passed to the log
720 * reader for this block, to provide more information in the logs
724 private function blockLogFlags() {
725 global $wgBlockAllowsUTEdit;
727 if( $this->BlockAnonOnly
&& IP
::isIPAddress( $this->BlockAddress
) )
728 // when blocking a user the option 'anononly' is not available/has no effect -> do not write this into log
729 $flags[] = 'anononly';
730 if( $this->BlockCreateAccount
)
731 $flags[] = 'nocreate';
732 if( !$this->BlockEnableAutoblock
&& !IP
::isIPAddress( $this->BlockAddress
) )
733 // Same as anononly, this is not displayed when blocking an IP address
734 $flags[] = 'noautoblock';
735 if( $this->BlockEmail
)
736 $flags[] = 'noemail';
737 if( !$this->BlockAllowUsertalk
&& $wgBlockAllowsUTEdit )
738 $flags[] = 'nousertalk';
739 if( $this->BlockHideName
)
740 $flags[] = 'hiddenname';
741 return implode( ',', $flags );
745 * Builds unblock and block list links
749 private function getConvenienceLinks() {
750 global $wgUser, $wgLang;
751 $skin = $wgUser->getSkin();
752 if( $this->BlockAddress
)
753 $links[] = $this->getContribsLink( $skin );
754 $links[] = $this->getUnblockLink( $skin );
755 $links[] = $this->getBlockListLink( $skin );
756 if ( $wgUser->isAllowed( 'editinterface' ) ) {
757 $title = Title
::makeTitle( NS_MEDIAWIKI
, 'Ipbreason-dropdown' );
758 $links[] = $skin->link(
760 wfMsgHtml( 'ipb-edit-dropdown' ),
762 array( 'action' => 'edit' )
765 return '<p class="mw-ipb-conveniencelinks">' . $wgLang->pipeList( $links ) . '</p>';
769 * Build a convenient link to a user or IP's contribs
772 * @param $skin Skin to use
775 private function getContribsLink( $skin ) {
776 $contribsPage = SpecialPage
::getTitleFor( 'Contributions', $this->BlockAddress
);
777 return $skin->link( $contribsPage, wfMsgExt( 'ipb-blocklist-contribs', 'escape', $this->BlockAddress
) );
781 * Build a convenient link to unblock the given username or IP
782 * address, if available; otherwise link to a blank unblock
785 * @param $skin Skin to use
788 private function getUnblockLink( $skin ) {
789 $list = SpecialPage
::getTitleFor( 'Ipblocklist' );
790 $query = array( 'action' => 'unblock' );
792 if( $this->BlockAddress
) {
793 $addr = strtr( $this->BlockAddress
, '_', ' ' );
794 $message = wfMsg( 'ipb-unblock-addr', $addr );
795 $query['ip'] = $this->BlockAddress
;
797 $message = wfMsg( 'ipb-unblock' );
799 return $skin->linkKnown(
801 htmlspecialchars( $message ),
808 * Build a convenience link to the block list
810 * @param $skin Skin to use
813 private function getBlockListLink( $skin ) {
814 $list = SpecialPage
::getTitleFor( 'Ipblocklist' );
817 if( $this->BlockAddress
) {
818 $addr = strtr( $this->BlockAddress
, '_', ' ' );
819 $message = wfMsg( 'ipb-blocklist-addr', $addr );
820 $query['ip'] = $this->BlockAddress
;
822 $message = wfMsg( 'ipb-blocklist' );
825 return $skin->linkKnown(
827 htmlspecialchars( $message ),
834 * Block a list of selected users
836 * @param $users Array
837 * @param $reason String
838 * @param $tag String: replaces user pages
839 * @param $talkTag String: replaces user talk pages
840 * @return Array: list of html-safe usernames
842 public static function doMassUserBlock( $users, $reason = '', $tag = '', $talkTag = '' ) {
844 $counter = $blockSize = 0;
845 $safeUsers = array();
846 $log = new LogPage( 'block' );
847 foreach( $users as $name ) {
851 # Lets not go *too* fast
852 if( $blockSize >= 20 ) {
854 wfWaitForSlaves( 5 );
856 $u = User
::newFromName( $name, false );
857 // If user doesn't exist, it ought to be an IP then
858 if( is_null( $u ) ||
( !$u->getId() && !IP
::isIPAddress( $u->getName() ) ) ) {
861 $userTitle = $u->getUserPage();
862 $userTalkTitle = $u->getTalkPage();
863 $userpage = new Article( $userTitle );
864 $usertalk = new Article( $userTalkTitle );
865 $safeUsers[] = '[[' . $userTitle->getPrefixedText() . '|' . $userTitle->getText() . ']]';
866 $expirestr = $u->getId() ?
'indefinite' : '1 week';
867 $expiry = Block
::parseExpiryInput( $expirestr );
868 $anonOnly = IP
::isIPAddress( $u->getName() ) ?
1 : 0;
870 $block = new Block( $u->getName(), // victim
872 $wgUser->getId(), // blocker
874 wfTimestampNow(), // block time
877 $anonOnly, // anononly?
878 1, // block account creation?
881 0 // block from sending email?
883 $oldblock = Block
::newFromDB( $u->getName(), $u->getId() );
886 # Prepare log parameters
887 $logParams = array();
888 $logParams[] = $expirestr;
890 $logParams[] = 'anononly';
892 $logParams[] = 'nocreate';
894 $log->addEntry( 'block', $userTitle, $reason, $logParams );
896 # Tag userpage! (check length to avoid mistakes)
897 if( strlen( $tag ) > 2 ) {
898 $userpage->doEdit( $tag, $reason, EDIT_MINOR
);
900 if( strlen( $talkTag ) > 2 ) {
901 $usertalk->doEdit( $talkTag, $reason, EDIT_MINOR
);