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;
112 $scBlockExpiryOptions = wfMsgForContent( 'ipboptions' );
114 $showblockoptions = $scBlockExpiryOptions != '-';
115 if (!$showblockoptions)
116 $mIpbother = $mIpbexpiry;
118 $blockExpiryFormOptions = Xml
::option( wfMsg( 'ipbotheroption' ), 'other' );
119 foreach (explode(',', $scBlockExpiryOptions) as $option) {
120 if ( strpos($option, ":") === false ) $option = "$option:$option";
121 list($show, $value) = explode(":", $option);
122 $show = htmlspecialchars($show);
123 $value = htmlspecialchars($value);
124 $blockExpiryFormOptions .= Xml
::option( $show, $value, $this->BlockExpiry
=== $value ?
true : false ) . "\n";
127 $reasonDropDown = Xml
::listDropDown( 'wpBlockReasonList',
128 wfMsgForContent( 'ipbreason-dropdown' ),
129 wfMsgForContent( 'ipbreasonotherlist' ), $this->BlockReasonList
, 'wpBlockDropDown', 4 );
131 global $wgStylePath, $wgStyleVersion;
133 Xml
::tags( 'script', array( 'type' => 'text/javascript', 'src' => "$wgStylePath/common/block.js?$wgStyleVersion" ), '' ) .
134 Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $titleObj->getLocalURL( "action=submit" ), 'id' => 'blockip' ) ) .
135 Xml
::openElement( 'fieldset' ) .
136 Xml
::element( 'legend', null, wfMsg( 'blockip-legend' ) ) .
137 Xml
::openElement( 'table', array ( 'border' => '0', 'id' => 'mw-blockip-table' ) ) .
139 <td class='mw-label'>
142 <td class='mw-input'>" .
143 Xml
::input( 'wpBlockAddress', 45, $this->BlockAddress
,
146 'id' => 'mw-bi-target',
147 'onchange' => 'updateBlockOptions()' ) ). "
152 if ( $showblockoptions ) {
154 <td class='mw-label'>
157 <td class='mw-input'>" .
160 'id' => 'wpBlockExpiry',
161 'name' => 'wpBlockExpiry',
162 'onchange' => 'considerChangingExpiryFocus()',
164 $blockExpiryFormOptions ) .
170 <tr id='wpBlockOther'>
171 <td class='mw-label'>
174 <td class='mw-input'>" .
175 Xml
::input( 'wpBlockOther', 45, $this->BlockOther
,
176 array( 'tabindex' => '3', 'id' => 'mw-bi-other' ) ) . "
180 <td class='mw-label'>
183 <td class='mw-input'>
187 <tr id=\"wpBlockReason\">
188 <td class='mw-label'>
191 <td class='mw-input'>" .
192 Xml
::input( 'wpBlockReason', 45, $this->BlockReason
,
193 array( 'tabindex' => '5', 'id' => 'mw-bi-reason', 'maxlength'=> '200' ) ) . "
196 <tr id='wpAnonOnlyRow'>
198 <td class='mw-input'>" .
199 Xml
::checkLabel( wfMsg( 'ipbanononly' ),
200 'wpAnonOnly', 'wpAnonOnly', $this->BlockAnonOnly
,
201 array( 'tabindex' => '6' ) ) . "
204 <tr id='wpCreateAccountRow'>
206 <td class='mw-input'>" .
207 Xml
::checkLabel( wfMsg( 'ipbcreateaccount' ),
208 'wpCreateAccount', 'wpCreateAccount', $this->BlockCreateAccount
,
209 array( 'tabindex' => '7' ) ) . "
212 <tr id='wpEnableAutoblockRow'>
214 <td class='mw-input'>" .
215 Xml
::checkLabel( wfMsg( 'ipbenableautoblock' ),
216 'wpEnableAutoblock', 'wpEnableAutoblock', $this->BlockEnableAutoblock
,
217 array( 'tabindex' => '8' ) ) . "
222 global $wgSysopEmailBans, $wgBlockAllowsUTEdit;
223 if ( $wgSysopEmailBans && $wgUser->isAllowed( 'blockemail' ) ) {
225 <tr id='wpEnableEmailBan'>
227 <td class='mw-input'>" .
228 Xml
::checkLabel( wfMsg( 'ipbemailban' ),
229 'wpEmailBan', 'wpEmailBan', $this->BlockEmail
,
230 array( 'tabindex' => '9' )) . "
236 // Allow some users to hide name from block log, blocklist and listusers
237 if ( $wgUser->isAllowed( 'hideuser' ) ) {
239 <tr id='wpEnableHideUser'>
241 <td class='mw-input'>" .
242 Xml
::checkLabel( wfMsg( 'ipbhidename' ),
243 'wpHideName', 'wpHideName', $this->BlockHideName
,
244 array( 'tabindex' => '10' ) ) . "
250 # Watchlist their user page?
252 <tr id='wpEnableWatchUser'>
254 <td class='mw-input'>" .
255 Xml
::checkLabel( wfMsg( 'ipbwatchuser' ),
256 'wpWatchUser', 'wpWatchUser', $this->BlockWatchUser
,
257 array( 'tabindex' => '11' ) ) . "
261 if( $wgBlockAllowsUTEdit ){
263 <tr id='wpAllowUsertalkRow'>
265 <td class='mw-input'>" .
266 Xml
::checkLabel( wfMsg( 'ipballowusertalk' ),
267 'wpAllowUsertalk', 'wpAllowUsertalk', $this->BlockAllowUsertalk
,
268 array( 'tabindex' => '12' ) ) . "
276 <td style='padding-top: 1em'> </td>
277 <td class='mw-submit' style='padding-top: 1em'>" .
278 Xml
::submitButton( wfMsg( $alreadyBlocked ?
'ipb-change-block' : 'ipbsubmit' ),
279 array( 'name' => 'wpBlock', 'tabindex' => '13', 'accesskey' => 's' ) ) . "
282 Xml
::closeElement( 'table' ) .
283 Xml
::hidden( 'wpEditToken', $wgUser->editToken() ) .
284 ( $alreadyBlocked ? Xml
::hidden( 'wpChangeBlock', 1 ) : "" ) .
285 Xml
::closeElement( 'fieldset' ) .
286 Xml
::closeElement( 'form' ) .
287 Xml
::tags( 'script', array( 'type' => 'text/javascript' ), 'updateBlockOptions()' ) . "\n"
290 $wgOut->addHTML( $this->getConvenienceLinks() );
292 if( is_object( $user ) ) {
293 $this->showLogFragment( $wgOut, $user->getUserPage() );
294 } elseif( preg_match( '/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/', $this->BlockAddress
) ) {
295 $this->showLogFragment( $wgOut, Title
::makeTitle( NS_USER
, $this->BlockAddress
) );
296 } 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
) ) {
297 $this->showLogFragment( $wgOut, Title
::makeTitle( NS_USER
, $this->BlockAddress
) );
302 * Backend block code.
303 * $userID and $expiry will be filled accordingly
304 * @return array(message key, arguments) on failure, empty array on success
306 function doBlock( &$userId = null, &$expiry = null ) {
307 global $wgUser, $wgSysopUserBans, $wgSysopRangeBans, $wgBlockAllowsUTEdit;
310 # Expand valid IPv6 addresses, usernames are left as is
311 $this->BlockAddress
= IP
::sanitizeIP( $this->BlockAddress
);
312 # isIPv4() and IPv6() are used for final validation
313 $rxIP4 = '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}';
314 $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}';
315 $rxIP = "($rxIP4|$rxIP6)";
317 # Check for invalid specifications
318 if ( !preg_match( "/^$rxIP$/", $this->BlockAddress
) ) {
320 if ( preg_match( "/^($rxIP4)\\/(\\d{1,2})$/", $this->BlockAddress
, $matches ) ) {
322 if ( $wgSysopRangeBans ) {
323 if ( !IP
::isIPv4( $this->BlockAddress
) ||
$matches[2] < 16 ||
$matches[2] > 32 ) {
324 return array('ip_range_invalid');
326 $this->BlockAddress
= Block
::normaliseRange( $this->BlockAddress
);
328 # Range block illegal
329 return array('range_block_disabled');
331 } else if ( preg_match( "/^($rxIP6)\\/(\\d{1,3})$/", $this->BlockAddress
, $matches ) ) {
333 if ( $wgSysopRangeBans ) {
334 if ( !IP
::isIPv6( $this->BlockAddress
) ||
$matches[2] < 64 ||
$matches[2] > 128 ) {
335 return array('ip_range_invalid');
337 $this->BlockAddress
= Block
::normaliseRange( $this->BlockAddress
);
339 # Range block illegal
340 return array('range_block_disabled');
344 if ( $wgSysopUserBans ) {
345 $user = User
::newFromName( $this->BlockAddress
);
346 if( !is_null( $user ) && $user->getId() ) {
348 $userId = $user->getId();
349 $this->BlockAddress
= $user->getName();
351 return array('nosuchusershort', htmlspecialchars( $user ?
$user->getName() : $this->BlockAddress
) );
354 return array('badipaddress');
359 if ( $wgUser->isBlocked() && ( $wgUser->getId() !== $userId ) ) {
360 return array( 'cant-block-while-blocked' );
363 $reasonstr = $this->BlockReasonList
;
364 if ( $reasonstr != 'other' && $this->BlockReason
!= '' ) {
365 // Entry from drop down menu + additional comment
366 $reasonstr .= wfMsgForContent( 'colon-separator' ) . $this->BlockReason
;
367 } elseif ( $reasonstr == 'other' ) {
368 $reasonstr = $this->BlockReason
;
371 $expirestr = $this->BlockExpiry
;
372 if( $expirestr == 'other' )
373 $expirestr = $this->BlockOther
;
375 if ( ( strlen( $expirestr ) == 0) ||
( strlen( $expirestr ) > 50) ) {
376 return array('ipb_expiry_invalid');
379 if ( false === ($expiry = Block
::parseExpiryInput( $expirestr )) ) {
381 return array('ipb_expiry_invalid');
384 if( $this->BlockHideName
&& $expiry != 'infinity' ) {
386 return array('ipb_expiry_temp');
390 # Note: for a user block, ipb_address is only for display purposes
391 $block = new Block( $this->BlockAddress
, $userId, $wgUser->getId(),
392 $reasonstr, wfTimestampNow(), 0, $expiry, $this->BlockAnonOnly
,
393 $this->BlockCreateAccount
, $this->BlockEnableAutoblock
, $this->BlockHideName
,
394 $this->BlockEmail
, isset( $this->BlockAllowUsertalk
) ?
$this->BlockAllowUsertalk
: $wgBlockAllowsUTEdit );
396 if ( wfRunHooks('BlockIp', array(&$block, &$wgUser)) ) {
398 if ( !$block->insert() ) {
399 if ( !$this->BlockReblock
) {
400 return array( 'ipb_already_blocked' );
402 # This returns direct blocks before autoblocks/rangeblocks, since we should
403 # be sure the user is blocked by now it should work for our purposes
404 $currentBlock = Block
::newFromDB( $this->BlockAddress
, $userId );
405 if( $block->equals( $currentBlock ) ) {
406 return array( 'ipb_already_blocked' );
408 $currentBlock->delete();
410 $log_action = 'reblock';
413 $log_action = 'block';
415 wfRunHooks('BlockIpComplete', array($block, $wgUser));
417 if ( $this->BlockWatchUser
) {
418 $wgUser->addWatch ( Title
::makeTitle( NS_USER
, $this->BlockAddress
) );
421 # Prepare log parameters
422 $logParams = array();
423 $logParams[] = $expirestr;
424 $logParams[] = $this->blockLogFlags();
426 # Make log entry, if the name is hidden, put it in the oversight log
427 $log_type = ($this->BlockHideName
) ?
'suppress' : 'block';
428 $log = new LogPage( $log_type );
429 $log->addEntry( $log_action, Title
::makeTitle( NS_USER
, $this->BlockAddress
),
430 $reasonstr, $logParams );
436 return array('hookaborted');
440 * UI entry point for blocking
441 * Wraps around doBlock()
446 $retval = $this->doBlock();
448 $titleObj = SpecialPage
::getTitleFor( 'Blockip' );
449 $wgOut->redirect( $titleObj->getFullURL( 'action=success&ip=' .
450 urlencode( $this->BlockAddress
) ) );
453 $this->showForm( $retval );
456 function showSuccess() {
459 $wgOut->setPagetitle( wfMsg( 'blockip' ) );
460 $wgOut->setSubtitle( wfMsg( 'blockipsuccesssub' ) );
461 $text = wfMsgExt( 'blockipsuccesstext', array( 'parse' ), $this->BlockAddress
);
462 $wgOut->addHTML( $text );
465 function showLogFragment( $out, $title ) {
467 $out->addHTML( Xml
::element( 'h2', NULL, LogPage
::logName( 'block' ) ) );
468 $count = LogEventsList
::showLogExtract( $out, 'block', $title->getPrefixedText(), '', 10 );
470 $out->addHTML( $wgUser->getSkin()->link(
471 SpecialPage
::getTitleFor( 'Log' ),
472 wfMsgHtml( 'blocklog-fulllog' ),
476 'page' => $title->getPrefixedText() ) ) );
481 * Return a comma-delimited list of "flags" to be passed to the log
482 * reader for this block, to provide more information in the logs
486 private function blockLogFlags() {
487 global $wgBlockAllowsUTEdit;
489 if( $this->BlockAnonOnly
&& IP
::isIPAddress( $this->BlockAddress
) )
490 // when blocking a user the option 'anononly' is not available/has no effect -> do not write this into log
491 $flags[] = 'anononly';
492 if( $this->BlockCreateAccount
)
493 $flags[] = 'nocreate';
494 if( !$this->BlockEnableAutoblock
)
495 $flags[] = 'noautoblock';
496 if ( $this->BlockEmail
)
497 $flags[] = 'noemail';
498 if ( !$this->BlockAllowUsertalk
&& $wgBlockAllowsUTEdit )
499 $flags[] = 'nousertalk';
500 return implode( ',', $flags );
504 * Builds unblock and block list links
508 private function getConvenienceLinks() {
510 $skin = $wgUser->getSkin();
511 if( $this->BlockAddress
)
512 $links[] = $this->getContribsLink( $skin );
513 $links[] = $this->getUnblockLink( $skin );
514 $links[] = $this->getBlockListLink( $skin );
515 $links[] = $skin->makeLink ( 'MediaWiki:Ipbreason-dropdown', wfMsgHtml( 'ipb-edit-dropdown' ) );
516 return '<p class="mw-ipb-conveniencelinks">' . implode( ' | ', $links ) . '</p>';
520 * Build a convenient link to a user or IP's contribs
523 * @param $skin Skin to use
526 private function getContribsLink( $skin ) {
527 $contribsPage = SpecialPage
::getTitleFor( 'Contributions', $this->BlockAddress
);
528 return $skin->link( $contribsPage, wfMsgHtml( 'ipb-blocklist-contribs', $this->BlockAddress
) );
532 * Build a convenient link to unblock the given username or IP
533 * address, if available; otherwise link to a blank unblock
536 * @param $skin Skin to use
539 private function getUnblockLink( $skin ) {
540 $list = SpecialPage
::getTitleFor( 'Ipblocklist' );
541 if( $this->BlockAddress
) {
542 $addr = htmlspecialchars( strtr( $this->BlockAddress
, '_', ' ' ) );
543 return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-unblock-addr', $addr ),
544 'action=unblock&ip=' . urlencode( $this->BlockAddress
) );
546 return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-unblock' ), 'action=unblock' );
551 * Build a convenience link to the block list
553 * @param $skin Skin to use
556 private function getBlockListLink( $skin ) {
557 $list = SpecialPage
::getTitleFor( 'Ipblocklist' );
558 if( $this->BlockAddress
) {
559 $addr = htmlspecialchars( strtr( $this->BlockAddress
, '_', ' ' ) );
560 return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-blocklist-addr', $addr ),
561 'ip=' . urlencode( $this->BlockAddress
) );
563 return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-blocklist' ) );
568 * Block a list of selected users
569 * @param array $users
570 * @param string $reason
571 * @param string $tag replaces user pages
572 * @param string $talkTag replaces user talk pages
573 * @returns array, list of html-safe usernames
575 public static function doMassUserBlock( $users, $reason = '', $tag = '', $talkTag = '' ) {
577 $counter = $blockSize = 0;
578 $safeUsers = array();
579 $log = new LogPage( 'block' );
580 foreach( $users as $name ) {
584 # Lets not go *too* fast
585 if( $blockSize >= 20 ) {
587 wfWaitForSlaves( 5 );
589 $u = User
::newFromName( $name, false );
590 // If user doesn't exist, it ought to be an IP then
591 if( is_null($u) ||
(!$u->getId() && !IP
::isIPAddress( $u->getName() )) ) {
594 $userTitle = $u->getUserPage();
595 $userTalkTitle = $u->getTalkPage();
596 $userpage = new Article( $userTitle );
597 $usertalk = new Article( $userTalkTitle );
598 $safeUsers[] = '[[' . $userTitle->getPrefixedText() . '|' . $userTitle->getText() . ']]';
599 $expirestr = $u->getId() ?
'indefinite' : '1 week';
600 $expiry = Block
::parseExpiryInput( $expirestr );
601 $anonOnly = IP
::isIPAddress( $u->getName() ) ?
1 : 0;
603 $block = new Block( $u->getName(), // victim
605 $wgUser->getId(), // blocker
607 wfTimestampNow(), // block time
610 $anonOnly, // anononly?
611 1, // block account creation?
614 0 // block from sending email?
616 $oldblock = Block
::newFromDB( $u->getName(), $u->getId() );
619 # Prepare log parameters
620 $logParams = array();
621 $logParams[] = $expirestr;
623 $logParams[] = 'anononly';
625 $logParams[] = 'nocreate';
627 $log->addEntry( 'block', $userTitle, $reason, $logParams );
629 # Tag userpage! (check length to avoid mistakes)
630 if( strlen($tag) > 2 ) {
631 $userpage->doEdit( $tag, $reason, EDIT_MINOR
);
633 if( strlen($talkTag) > 2 ) {
634 $usertalk->doEdit( $talkTag, $reason, EDIT_MINOR
);