3 * Constructor for Special:Blockip page
12 function wfSpecialBlockip( $par ) {
13 global $wgUser, $wgOut, $wgRequest;
15 # Can't block when the database is locked
17 $wgOut->readOnlyPage();
22 if( !$wgUser->isAllowed( 'block' ) ) {
23 $wgOut->permissionRequired( 'block' );
27 $ipb = new IPBlockForm( $par );
29 $action = $wgRequest->getVal( 'action' );
30 if ( 'success' == $action ) {
32 } else if ( $wgRequest->wasPosted() && 'submit' == $action &&
33 $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ) ) ) {
41 * Form object for the Special:Blockip page.
43 * @ingroup SpecialPage
46 var $BlockAddress, $BlockExpiry, $BlockReason;
49 function IPBlockForm( $par ) {
50 global $wgRequest, $wgUser, $wgBlockAllowsUTEdit;
52 $this->BlockAddress
= $wgRequest->getVal( 'wpBlockAddress', $wgRequest->getVal( 'ip', $par ) );
53 $this->BlockAddress
= strtr( $this->BlockAddress
, '_', ' ' );
54 $this->BlockReason
= $wgRequest->getText( 'wpBlockReason' );
55 $this->BlockReasonList
= $wgRequest->getText( 'wpBlockReasonList' );
56 $this->BlockExpiry
= $wgRequest->getVal( 'wpBlockExpiry', wfMsg('ipbotheroption') );
57 $this->BlockOther
= $wgRequest->getVal( 'wpBlockOther', '' );
59 # Unchecked checkboxes are not included in the form data at all, so having one
60 # that is true by default is a bit tricky
61 $byDefault = !$wgRequest->wasPosted();
62 $this->BlockAnonOnly
= $wgRequest->getBool( 'wpAnonOnly', $byDefault );
63 $this->BlockCreateAccount
= $wgRequest->getBool( 'wpCreateAccount', $byDefault );
64 $this->BlockEnableAutoblock
= $wgRequest->getBool( 'wpEnableAutoblock', $byDefault );
65 $this->BlockEmail
= $wgRequest->getBool( 'wpEmailBan', false );
66 $this->BlockWatchUser
= $wgRequest->getBool( 'wpWatchUser', false );
67 # Re-check user's rights to hide names, very serious, defaults to 0
68 $this->BlockHideName
= ( $wgRequest->getBool( 'wpHideName', 0 ) && $wgUser->isAllowed( 'hideuser' ) ) ?
1 : 0;
69 $this->BlockAllowUsertalk
= ( $wgRequest->getBool( 'wpAllowUsertalk', $byDefault ) && $wgBlockAllowsUTEdit );
70 $this->BlockReblock
= $wgRequest->getBool( 'wpChangeBlock', false );
73 function showForm( $err ) {
74 global $wgOut, $wgUser, $wgSysopUserBans;
76 $wgOut->setPagetitle( wfMsg( 'blockip' ) );
77 $wgOut->addWikiMsg( 'blockiptext' );
79 if($wgSysopUserBans) {
80 $mIpaddress = Xml
::label( wfMsg( 'ipadressorusername' ), 'mw-bi-target' );
82 $mIpaddress = Xml
::label( wfMsg( 'ipaddress' ), 'mw-bi-target' );
84 $mIpbexpiry = Xml
::label( wfMsg( 'ipbexpiry' ), 'wpBlockExpiry' );
85 $mIpbother = Xml
::label( wfMsg( 'ipbother' ), 'mw-bi-other' );
86 $mIpbreasonother = Xml
::label( wfMsg( 'ipbreason' ), 'wpBlockReasonList' );
87 $mIpbreason = Xml
::label( wfMsg( 'ipbotherreason' ), 'mw-bi-reason' );
89 $titleObj = SpecialPage
::getTitleFor( 'Blockip' );
90 $user = User
::newFromName( $this->BlockAddress
);
92 $alreadyBlocked = false;
93 if ( $err && $err[0] != 'ipb_already_blocked' ) {
94 $key = array_shift($err);
95 $msg = wfMsgReal($key, $err);
96 $wgOut->setSubtitle( wfMsgHtml( 'formerror' ) );
97 $wgOut->addHTML( Xml
::tags( 'p', array( 'class' => 'error' ), $msg ) );
98 } elseif ( $this->BlockAddress
) {
100 if ( is_object( $user ) )
101 $userId = $user->getId();
102 $currentBlock = Block
::newFromDB( $this->BlockAddress
, $userId );
103 if ( !is_null($currentBlock) && !$currentBlock->mAuto
&& # The block exists and isn't an autoblock
104 ( $currentBlock->mRangeStart
== $currentBlock->mRangeEnd ||
# The block isn't a rangeblock
105 # or if it is, the range is what we're about to block
106 ( $currentBlock->mAddress
== $this->BlockAddress
) ) ) {
107 $wgOut->addWikiMsg( 'ipb-needreblock', $this->BlockAddress
);
108 $alreadyBlocked = true;
109 # Set the block form settings to the existing block
110 $this->BlockAnonOnly
= $currentBlock->mAnonOnly
;
111 $this->BlockCreateAccount
= $currentBlock->mCreateAccount
;
112 $this->BlockEnableAutoblock
= $currentBlock->mEnableAutoblock
;
113 $this->BlockEmail
= $currentBlock->mBlockEmail
;
114 $this->BlockHideName
= $currentBlock->mHideName
;
115 $this->BlockAllowUsertalk
= $currentBlock->mAllowUsertalk
;
116 if( $currentBlock->mExpiry
== 'infinity' ) {
117 $this->BlockOther
= 'indefinite';
119 $this->BlockOther
= wfTimestamp( TS_ISO_8601
, $currentBlock->mExpiry
);
121 $this->BlockReason
= $currentBlock->mReason
;
125 $scBlockExpiryOptions = wfMsgForContent( 'ipboptions' );
127 $showblockoptions = $scBlockExpiryOptions != '-';
128 if (!$showblockoptions)
129 $mIpbother = $mIpbexpiry;
131 $blockExpiryFormOptions = Xml
::option( wfMsg( 'ipbotheroption' ), 'other' );
132 foreach (explode(',', $scBlockExpiryOptions) as $option) {
133 if ( strpos($option, ":") === false ) $option = "$option:$option";
134 list($show, $value) = explode(":", $option);
135 $show = htmlspecialchars($show);
136 $value = htmlspecialchars($value);
137 $blockExpiryFormOptions .= Xml
::option( $show, $value, $this->BlockExpiry
=== $value ?
true : false ) . "\n";
140 $reasonDropDown = Xml
::listDropDown( 'wpBlockReasonList',
141 wfMsgForContent( 'ipbreason-dropdown' ),
142 wfMsgForContent( 'ipbreasonotherlist' ), $this->BlockReasonList
, 'wpBlockDropDown', 4 );
144 global $wgStylePath, $wgStyleVersion;
146 Xml
::tags( 'script', array( 'type' => 'text/javascript', 'src' => "$wgStylePath/common/block.js?$wgStyleVersion" ), '' ) .
147 Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $titleObj->getLocalURL( "action=submit" ), 'id' => 'blockip' ) ) .
148 Xml
::openElement( 'fieldset' ) .
149 Xml
::element( 'legend', null, wfMsg( 'blockip-legend' ) ) .
150 Xml
::openElement( 'table', array ( 'border' => '0', 'id' => 'mw-blockip-table' ) ) .
152 <td class='mw-label'>
155 <td class='mw-input'>" .
156 Xml
::input( 'wpBlockAddress', 45, $this->BlockAddress
,
159 'id' => 'mw-bi-target',
160 'onchange' => 'updateBlockOptions()' ) ). "
165 if ( $showblockoptions ) {
167 <td class='mw-label'>
170 <td class='mw-input'>" .
173 'id' => 'wpBlockExpiry',
174 'name' => 'wpBlockExpiry',
175 'onchange' => 'considerChangingExpiryFocus()',
177 $blockExpiryFormOptions ) .
183 <tr id='wpBlockOther'>
184 <td class='mw-label'>
187 <td class='mw-input'>" .
188 Xml
::input( 'wpBlockOther', 45, $this->BlockOther
,
189 array( 'tabindex' => '3', 'id' => 'mw-bi-other' ) ) . "
193 <td class='mw-label'>
196 <td class='mw-input'>
200 <tr id=\"wpBlockReason\">
201 <td class='mw-label'>
204 <td class='mw-input'>" .
205 Xml
::input( 'wpBlockReason', 45, $this->BlockReason
,
206 array( 'tabindex' => '5', 'id' => 'mw-bi-reason', 'maxlength'=> '200' ) ) . "
209 <tr id='wpAnonOnlyRow'>
211 <td class='mw-input'>" .
212 Xml
::checkLabel( wfMsg( 'ipbanononly' ),
213 'wpAnonOnly', 'wpAnonOnly', $this->BlockAnonOnly
,
214 array( 'tabindex' => '6' ) ) . "
217 <tr id='wpCreateAccountRow'>
219 <td class='mw-input'>" .
220 Xml
::checkLabel( wfMsg( 'ipbcreateaccount' ),
221 'wpCreateAccount', 'wpCreateAccount', $this->BlockCreateAccount
,
222 array( 'tabindex' => '7' ) ) . "
225 <tr id='wpEnableAutoblockRow'>
227 <td class='mw-input'>" .
228 Xml
::checkLabel( wfMsg( 'ipbenableautoblock' ),
229 'wpEnableAutoblock', 'wpEnableAutoblock', $this->BlockEnableAutoblock
,
230 array( 'tabindex' => '8' ) ) . "
235 global $wgSysopEmailBans, $wgBlockAllowsUTEdit;
236 if ( $wgSysopEmailBans && $wgUser->isAllowed( 'blockemail' ) ) {
238 <tr id='wpEnableEmailBan'>
240 <td class='mw-input'>" .
241 Xml
::checkLabel( wfMsg( 'ipbemailban' ),
242 'wpEmailBan', 'wpEmailBan', $this->BlockEmail
,
243 array( 'tabindex' => '9' )) . "
249 // Allow some users to hide name from block log, blocklist and listusers
250 if ( $wgUser->isAllowed( 'hideuser' ) ) {
252 <tr id='wpEnableHideUser'>
254 <td class='mw-input'>" .
255 Xml
::checkLabel( wfMsg( 'ipbhidename' ),
256 'wpHideName', 'wpHideName', $this->BlockHideName
,
257 array( 'tabindex' => '10' ) ) . "
263 # Watchlist their user page?
265 <tr id='wpEnableWatchUser'>
267 <td class='mw-input'>" .
268 Xml
::checkLabel( wfMsg( 'ipbwatchuser' ),
269 'wpWatchUser', 'wpWatchUser', $this->BlockWatchUser
,
270 array( 'tabindex' => '11' ) ) . "
274 if( $wgBlockAllowsUTEdit ){
276 <tr id='wpAllowUsertalkRow'>
278 <td class='mw-input'>" .
279 Xml
::checkLabel( wfMsg( 'ipballowusertalk' ),
280 'wpAllowUsertalk', 'wpAllowUsertalk', $this->BlockAllowUsertalk
,
281 array( 'tabindex' => '12' ) ) . "
289 <td style='padding-top: 1em'> </td>
290 <td class='mw-submit' style='padding-top: 1em'>" .
291 Xml
::submitButton( wfMsg( $alreadyBlocked ?
'ipb-change-block' : 'ipbsubmit' ),
292 array( 'name' => 'wpBlock', 'tabindex' => '13', 'accesskey' => 's' ) ) . "
295 Xml
::closeElement( 'table' ) .
296 Xml
::hidden( 'wpEditToken', $wgUser->editToken() ) .
297 ( $alreadyBlocked ? Xml
::hidden( 'wpChangeBlock', 1 ) : "" ) .
298 Xml
::closeElement( 'fieldset' ) .
299 Xml
::closeElement( 'form' ) .
300 Xml
::tags( 'script', array( 'type' => 'text/javascript' ), 'updateBlockOptions()' ) . "\n"
303 $wgOut->addHTML( $this->getConvenienceLinks() );
305 if( is_object( $user ) ) {
306 $this->showLogFragment( $wgOut, $user->getUserPage() );
307 } elseif( preg_match( '/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/', $this->BlockAddress
) ) {
308 $this->showLogFragment( $wgOut, Title
::makeTitle( NS_USER
, $this->BlockAddress
) );
309 } 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
) ) {
310 $this->showLogFragment( $wgOut, Title
::makeTitle( NS_USER
, $this->BlockAddress
) );
315 * Backend block code.
316 * $userID and $expiry will be filled accordingly
317 * @return array(message key, arguments) on failure, empty array on success
319 function doBlock( &$userId = null, &$expiry = null ) {
320 global $wgUser, $wgSysopUserBans, $wgSysopRangeBans, $wgBlockAllowsUTEdit;
323 # Expand valid IPv6 addresses, usernames are left as is
324 $this->BlockAddress
= IP
::sanitizeIP( $this->BlockAddress
);
325 # isIPv4() and IPv6() are used for final validation
326 $rxIP4 = '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}';
327 $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}';
328 $rxIP = "($rxIP4|$rxIP6)";
330 # Check for invalid specifications
331 if ( !preg_match( "/^$rxIP$/", $this->BlockAddress
) ) {
333 if ( preg_match( "/^($rxIP4)\\/(\\d{1,2})$/", $this->BlockAddress
, $matches ) ) {
335 if ( $wgSysopRangeBans ) {
336 if ( !IP
::isIPv4( $this->BlockAddress
) ||
$matches[2] < 16 ||
$matches[2] > 32 ) {
337 return array('ip_range_invalid');
339 $this->BlockAddress
= Block
::normaliseRange( $this->BlockAddress
);
341 # Range block illegal
342 return array('range_block_disabled');
344 } else if ( preg_match( "/^($rxIP6)\\/(\\d{1,3})$/", $this->BlockAddress
, $matches ) ) {
346 if ( $wgSysopRangeBans ) {
347 if ( !IP
::isIPv6( $this->BlockAddress
) ||
$matches[2] < 64 ||
$matches[2] > 128 ) {
348 return array('ip_range_invalid');
350 $this->BlockAddress
= Block
::normaliseRange( $this->BlockAddress
);
352 # Range block illegal
353 return array('range_block_disabled');
357 if ( $wgSysopUserBans ) {
358 $user = User
::newFromName( $this->BlockAddress
);
359 if( !is_null( $user ) && $user->getId() ) {
361 $userId = $user->getId();
362 $this->BlockAddress
= $user->getName();
364 return array('nosuchusershort', htmlspecialchars( $user ?
$user->getName() : $this->BlockAddress
) );
367 return array('badipaddress');
372 if ( $wgUser->isBlocked() && ( $wgUser->getId() !== $userId ) ) {
373 return array( 'cant-block-while-blocked' );
376 $reasonstr = $this->BlockReasonList
;
377 if ( $reasonstr != 'other' && $this->BlockReason
!= '' ) {
378 // Entry from drop down menu + additional comment
379 $reasonstr .= wfMsgForContent( 'colon-separator' ) . $this->BlockReason
;
380 } elseif ( $reasonstr == 'other' ) {
381 $reasonstr = $this->BlockReason
;
384 $expirestr = $this->BlockExpiry
;
385 if( $expirestr == 'other' )
386 $expirestr = $this->BlockOther
;
388 if ( ( strlen( $expirestr ) == 0) ||
( strlen( $expirestr ) > 50) ) {
389 return array('ipb_expiry_invalid');
392 if ( false === ($expiry = Block
::parseExpiryInput( $expirestr )) ) {
394 return array('ipb_expiry_invalid');
397 if( $this->BlockHideName
&& $expiry != 'infinity' ) {
399 return array('ipb_expiry_temp');
403 # Note: for a user block, ipb_address is only for display purposes
404 $block = new Block( $this->BlockAddress
, $userId, $wgUser->getId(),
405 $reasonstr, wfTimestampNow(), 0, $expiry, $this->BlockAnonOnly
,
406 $this->BlockCreateAccount
, $this->BlockEnableAutoblock
, $this->BlockHideName
,
407 $this->BlockEmail
, isset( $this->BlockAllowUsertalk
) ?
$this->BlockAllowUsertalk
: $wgBlockAllowsUTEdit );
409 if ( wfRunHooks('BlockIp', array(&$block, &$wgUser)) ) {
411 if ( !$block->insert() ) {
412 if ( !$this->BlockReblock
) {
413 return array( 'ipb_already_blocked' );
415 # This returns direct blocks before autoblocks/rangeblocks, since we should
416 # be sure the user is blocked by now it should work for our purposes
417 $currentBlock = Block
::newFromDB( $this->BlockAddress
, $userId );
418 if( $block->equals( $currentBlock ) ) {
419 return array( 'ipb_already_blocked' );
421 $currentBlock->delete();
423 $log_action = 'reblock';
426 $log_action = 'block';
428 wfRunHooks('BlockIpComplete', array($block, $wgUser));
430 if ( $this->BlockWatchUser
) {
431 $wgUser->addWatch ( Title
::makeTitle( NS_USER
, $this->BlockAddress
) );
434 # Prepare log parameters
435 $logParams = array();
436 $logParams[] = $expirestr;
437 $logParams[] = $this->blockLogFlags();
439 # Make log entry, if the name is hidden, put it in the oversight log
440 $log_type = ($this->BlockHideName
) ?
'suppress' : 'block';
441 $log = new LogPage( $log_type );
442 $log->addEntry( $log_action, Title
::makeTitle( NS_USER
, $this->BlockAddress
),
443 $reasonstr, $logParams );
449 return array('hookaborted');
453 * UI entry point for blocking
454 * Wraps around doBlock()
459 $retval = $this->doBlock();
461 $titleObj = SpecialPage
::getTitleFor( 'Blockip' );
462 $wgOut->redirect( $titleObj->getFullURL( 'action=success&ip=' .
463 urlencode( $this->BlockAddress
) ) );
466 $this->showForm( $retval );
469 function showSuccess() {
472 $wgOut->setPagetitle( wfMsg( 'blockip' ) );
473 $wgOut->setSubtitle( wfMsg( 'blockipsuccesssub' ) );
474 $text = wfMsgExt( 'blockipsuccesstext', array( 'parse' ), $this->BlockAddress
);
475 $wgOut->addHTML( $text );
478 function showLogFragment( $out, $title ) {
480 $out->addHTML( Xml
::element( 'h2', NULL, LogPage
::logName( 'block' ) ) );
481 $count = LogEventsList
::showLogExtract( $out, 'block', $title->getPrefixedText(), '', 10 );
483 $out->addHTML( $wgUser->getSkin()->link(
484 SpecialPage
::getTitleFor( 'Log' ),
485 wfMsgHtml( 'blocklog-fulllog' ),
489 'page' => $title->getPrefixedText() ) ) );
494 * Return a comma-delimited list of "flags" to be passed to the log
495 * reader for this block, to provide more information in the logs
499 private function blockLogFlags() {
500 global $wgBlockAllowsUTEdit;
502 if( $this->BlockAnonOnly
&& IP
::isIPAddress( $this->BlockAddress
) )
503 // when blocking a user the option 'anononly' is not available/has no effect -> do not write this into log
504 $flags[] = 'anononly';
505 if( $this->BlockCreateAccount
)
506 $flags[] = 'nocreate';
507 if( !$this->BlockEnableAutoblock
)
508 $flags[] = 'noautoblock';
509 if ( $this->BlockEmail
)
510 $flags[] = 'noemail';
511 if ( !$this->BlockAllowUsertalk
&& $wgBlockAllowsUTEdit )
512 $flags[] = 'nousertalk';
513 return implode( ',', $flags );
517 * Builds unblock and block list links
521 private function getConvenienceLinks() {
523 $skin = $wgUser->getSkin();
524 if( $this->BlockAddress
)
525 $links[] = $this->getContribsLink( $skin );
526 $links[] = $this->getUnblockLink( $skin );
527 $links[] = $this->getBlockListLink( $skin );
528 $links[] = $skin->makeLink ( 'MediaWiki:Ipbreason-dropdown', wfMsgHtml( 'ipb-edit-dropdown' ) );
529 return '<p class="mw-ipb-conveniencelinks">' . implode( ' | ', $links ) . '</p>';
533 * Build a convenient link to a user or IP's contribs
536 * @param $skin Skin to use
539 private function getContribsLink( $skin ) {
540 $contribsPage = SpecialPage
::getTitleFor( 'Contributions', $this->BlockAddress
);
541 return $skin->link( $contribsPage, wfMsgHtml( 'ipb-blocklist-contribs', $this->BlockAddress
) );
545 * Build a convenient link to unblock the given username or IP
546 * address, if available; otherwise link to a blank unblock
549 * @param $skin Skin to use
552 private function getUnblockLink( $skin ) {
553 $list = SpecialPage
::getTitleFor( 'Ipblocklist' );
554 if( $this->BlockAddress
) {
555 $addr = htmlspecialchars( strtr( $this->BlockAddress
, '_', ' ' ) );
556 return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-unblock-addr', $addr ),
557 'action=unblock&ip=' . urlencode( $this->BlockAddress
) );
559 return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-unblock' ), 'action=unblock' );
564 * Build a convenience link to the block list
566 * @param $skin Skin to use
569 private function getBlockListLink( $skin ) {
570 $list = SpecialPage
::getTitleFor( 'Ipblocklist' );
571 if( $this->BlockAddress
) {
572 $addr = htmlspecialchars( strtr( $this->BlockAddress
, '_', ' ' ) );
573 return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-blocklist-addr', $addr ),
574 'ip=' . urlencode( $this->BlockAddress
) );
576 return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-blocklist' ) );
581 * Block a list of selected users
582 * @param array $users
583 * @param string $reason
584 * @param string $tag replaces user pages
585 * @param string $talkTag replaces user talk pages
586 * @returns array, list of html-safe usernames
588 public static function doMassUserBlock( $users, $reason = '', $tag = '', $talkTag = '' ) {
590 $counter = $blockSize = 0;
591 $safeUsers = array();
592 $log = new LogPage( 'block' );
593 foreach( $users as $name ) {
597 # Lets not go *too* fast
598 if( $blockSize >= 20 ) {
600 wfWaitForSlaves( 5 );
602 $u = User
::newFromName( $name, false );
603 // If user doesn't exist, it ought to be an IP then
604 if( is_null($u) ||
(!$u->getId() && !IP
::isIPAddress( $u->getName() )) ) {
607 $userTitle = $u->getUserPage();
608 $userTalkTitle = $u->getTalkPage();
609 $userpage = new Article( $userTitle );
610 $usertalk = new Article( $userTalkTitle );
611 $safeUsers[] = '[[' . $userTitle->getPrefixedText() . '|' . $userTitle->getText() . ']]';
612 $expirestr = $u->getId() ?
'indefinite' : '1 week';
613 $expiry = Block
::parseExpiryInput( $expirestr );
614 $anonOnly = IP
::isIPAddress( $u->getName() ) ?
1 : 0;
616 $block = new Block( $u->getName(), // victim
618 $wgUser->getId(), // blocker
620 wfTimestampNow(), // block time
623 $anonOnly, // anononly?
624 1, // block account creation?
627 0 // block from sending email?
629 $oldblock = Block
::newFromDB( $u->getName(), $u->getId() );
632 # Prepare log parameters
633 $logParams = array();
634 $logParams[] = $expirestr;
636 $logParams[] = 'anononly';
638 $logParams[] = 'nocreate';
640 $log->addEntry( 'block', $userTitle, $reason, $logParams );
642 # Tag userpage! (check length to avoid mistakes)
643 if( strlen($tag) > 2 ) {
644 $userpage->doEdit( $tag, $reason, EDIT_MINOR
);
646 if( strlen($talkTag) > 2 ) {
647 $usertalk->doEdit( $talkTag, $reason, EDIT_MINOR
);