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 $this->BlockOther
= wfTimestamp( TS_RFC2822
, $currentBlock->mExpiry
);
117 $this->BlockReason
= $currentBlock->mReason
;
121 $scBlockExpiryOptions = wfMsgForContent( 'ipboptions' );
123 $showblockoptions = $scBlockExpiryOptions != '-';
124 if (!$showblockoptions)
125 $mIpbother = $mIpbexpiry;
127 $blockExpiryFormOptions = Xml
::option( wfMsg( 'ipbotheroption' ), 'other' );
128 foreach (explode(',', $scBlockExpiryOptions) as $option) {
129 if ( strpos($option, ":") === false ) $option = "$option:$option";
130 list($show, $value) = explode(":", $option);
131 $show = htmlspecialchars($show);
132 $value = htmlspecialchars($value);
133 $blockExpiryFormOptions .= Xml
::option( $show, $value, $this->BlockExpiry
=== $value ?
true : false ) . "\n";
136 $reasonDropDown = Xml
::listDropDown( 'wpBlockReasonList',
137 wfMsgForContent( 'ipbreason-dropdown' ),
138 wfMsgForContent( 'ipbreasonotherlist' ), $this->BlockReasonList
, 'wpBlockDropDown', 4 );
140 global $wgStylePath, $wgStyleVersion;
142 Xml
::tags( 'script', array( 'type' => 'text/javascript', 'src' => "$wgStylePath/common/block.js?$wgStyleVersion" ), '' ) .
143 Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $titleObj->getLocalURL( "action=submit" ), 'id' => 'blockip' ) ) .
144 Xml
::openElement( 'fieldset' ) .
145 Xml
::element( 'legend', null, wfMsg( 'blockip-legend' ) ) .
146 Xml
::openElement( 'table', array ( 'border' => '0', 'id' => 'mw-blockip-table' ) ) .
148 <td class='mw-label'>
151 <td class='mw-input'>" .
152 Xml
::input( 'wpBlockAddress', 45, $this->BlockAddress
,
155 'id' => 'mw-bi-target',
156 'onchange' => 'updateBlockOptions()' ) ). "
161 if ( $showblockoptions ) {
163 <td class='mw-label'>
166 <td class='mw-input'>" .
169 'id' => 'wpBlockExpiry',
170 'name' => 'wpBlockExpiry',
171 'onchange' => 'considerChangingExpiryFocus()',
173 $blockExpiryFormOptions ) .
179 <tr id='wpBlockOther'>
180 <td class='mw-label'>
183 <td class='mw-input'>" .
184 Xml
::input( 'wpBlockOther', 45, $this->BlockOther
,
185 array( 'tabindex' => '3', 'id' => 'mw-bi-other' ) ) . "
189 <td class='mw-label'>
192 <td class='mw-input'>
196 <tr id=\"wpBlockReason\">
197 <td class='mw-label'>
200 <td class='mw-input'>" .
201 Xml
::input( 'wpBlockReason', 45, $this->BlockReason
,
202 array( 'tabindex' => '5', 'id' => 'mw-bi-reason', 'maxlength'=> '200' ) ) . "
205 <tr id='wpAnonOnlyRow'>
207 <td class='mw-input'>" .
208 Xml
::checkLabel( wfMsg( 'ipbanononly' ),
209 'wpAnonOnly', 'wpAnonOnly', $this->BlockAnonOnly
,
210 array( 'tabindex' => '6' ) ) . "
213 <tr id='wpCreateAccountRow'>
215 <td class='mw-input'>" .
216 Xml
::checkLabel( wfMsg( 'ipbcreateaccount' ),
217 'wpCreateAccount', 'wpCreateAccount', $this->BlockCreateAccount
,
218 array( 'tabindex' => '7' ) ) . "
221 <tr id='wpEnableAutoblockRow'>
223 <td class='mw-input'>" .
224 Xml
::checkLabel( wfMsg( 'ipbenableautoblock' ),
225 'wpEnableAutoblock', 'wpEnableAutoblock', $this->BlockEnableAutoblock
,
226 array( 'tabindex' => '8' ) ) . "
231 global $wgSysopEmailBans, $wgBlockAllowsUTEdit;
232 if ( $wgSysopEmailBans && $wgUser->isAllowed( 'blockemail' ) ) {
234 <tr id='wpEnableEmailBan'>
236 <td class='mw-input'>" .
237 Xml
::checkLabel( wfMsg( 'ipbemailban' ),
238 'wpEmailBan', 'wpEmailBan', $this->BlockEmail
,
239 array( 'tabindex' => '9' )) . "
245 // Allow some users to hide name from block log, blocklist and listusers
246 if ( $wgUser->isAllowed( 'hideuser' ) ) {
248 <tr id='wpEnableHideUser'>
250 <td class='mw-input'>" .
251 Xml
::checkLabel( wfMsg( 'ipbhidename' ),
252 'wpHideName', 'wpHideName', $this->BlockHideName
,
253 array( 'tabindex' => '10' ) ) . "
259 # Watchlist their user page?
261 <tr id='wpEnableWatchUser'>
263 <td class='mw-input'>" .
264 Xml
::checkLabel( wfMsg( 'ipbwatchuser' ),
265 'wpWatchUser', 'wpWatchUser', $this->BlockWatchUser
,
266 array( 'tabindex' => '11' ) ) . "
270 if( $wgBlockAllowsUTEdit ){
272 <tr id='wpAllowUsertalkRow'>
274 <td class='mw-input'>" .
275 Xml
::checkLabel( wfMsg( 'ipballowusertalk' ),
276 'wpAllowUsertalk', 'wpAllowUsertalk', $this->BlockAllowUsertalk
,
277 array( 'tabindex' => '12' ) ) . "
285 <td style='padding-top: 1em'> </td>
286 <td class='mw-submit' style='padding-top: 1em'>" .
287 Xml
::submitButton( wfMsg( $alreadyBlocked ?
'ipb-change-block' : 'ipbsubmit' ),
288 array( 'name' => 'wpBlock', 'tabindex' => '13', 'accesskey' => 's' ) ) . "
291 Xml
::closeElement( 'table' ) .
292 Xml
::hidden( 'wpEditToken', $wgUser->editToken() ) .
293 ( $alreadyBlocked ? Xml
::hidden( 'wpChangeBlock', 1 ) : "" ) .
294 Xml
::closeElement( 'fieldset' ) .
295 Xml
::closeElement( 'form' ) .
296 Xml
::tags( 'script', array( 'type' => 'text/javascript' ), 'updateBlockOptions()' ) . "\n"
299 $wgOut->addHTML( $this->getConvenienceLinks() );
301 if( is_object( $user ) ) {
302 $this->showLogFragment( $wgOut, $user->getUserPage() );
303 } elseif( preg_match( '/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/', $this->BlockAddress
) ) {
304 $this->showLogFragment( $wgOut, Title
::makeTitle( NS_USER
, $this->BlockAddress
) );
305 } 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
) ) {
306 $this->showLogFragment( $wgOut, Title
::makeTitle( NS_USER
, $this->BlockAddress
) );
311 * Backend block code.
312 * $userID and $expiry will be filled accordingly
313 * @return array(message key, arguments) on failure, empty array on success
315 function doBlock( &$userId = null, &$expiry = null ) {
316 global $wgUser, $wgSysopUserBans, $wgSysopRangeBans, $wgBlockAllowsUTEdit;
319 # Expand valid IPv6 addresses, usernames are left as is
320 $this->BlockAddress
= IP
::sanitizeIP( $this->BlockAddress
);
321 # isIPv4() and IPv6() are used for final validation
322 $rxIP4 = '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}';
323 $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}';
324 $rxIP = "($rxIP4|$rxIP6)";
326 # Check for invalid specifications
327 if ( !preg_match( "/^$rxIP$/", $this->BlockAddress
) ) {
329 if ( preg_match( "/^($rxIP4)\\/(\\d{1,2})$/", $this->BlockAddress
, $matches ) ) {
331 if ( $wgSysopRangeBans ) {
332 if ( !IP
::isIPv4( $this->BlockAddress
) ||
$matches[2] < 16 ||
$matches[2] > 32 ) {
333 return array('ip_range_invalid');
335 $this->BlockAddress
= Block
::normaliseRange( $this->BlockAddress
);
337 # Range block illegal
338 return array('range_block_disabled');
340 } else if ( preg_match( "/^($rxIP6)\\/(\\d{1,3})$/", $this->BlockAddress
, $matches ) ) {
342 if ( $wgSysopRangeBans ) {
343 if ( !IP
::isIPv6( $this->BlockAddress
) ||
$matches[2] < 64 ||
$matches[2] > 128 ) {
344 return array('ip_range_invalid');
346 $this->BlockAddress
= Block
::normaliseRange( $this->BlockAddress
);
348 # Range block illegal
349 return array('range_block_disabled');
353 if ( $wgSysopUserBans ) {
354 $user = User
::newFromName( $this->BlockAddress
);
355 if( !is_null( $user ) && $user->getId() ) {
357 $userId = $user->getId();
358 $this->BlockAddress
= $user->getName();
360 return array('nosuchusershort', htmlspecialchars( $user ?
$user->getName() : $this->BlockAddress
) );
363 return array('badipaddress');
368 if ( $wgUser->isBlocked() && ( $wgUser->getId() !== $userId ) ) {
369 return array( 'cant-block-while-blocked' );
372 $reasonstr = $this->BlockReasonList
;
373 if ( $reasonstr != 'other' && $this->BlockReason
!= '' ) {
374 // Entry from drop down menu + additional comment
375 $reasonstr .= wfMsgForContent( 'colon-separator' ) . $this->BlockReason
;
376 } elseif ( $reasonstr == 'other' ) {
377 $reasonstr = $this->BlockReason
;
380 $expirestr = $this->BlockExpiry
;
381 if( $expirestr == 'other' )
382 $expirestr = $this->BlockOther
;
384 if ( ( strlen( $expirestr ) == 0) ||
( strlen( $expirestr ) > 50) ) {
385 return array('ipb_expiry_invalid');
388 if ( false === ($expiry = Block
::parseExpiryInput( $expirestr )) ) {
390 return array('ipb_expiry_invalid');
393 if( $this->BlockHideName
&& $expiry != 'infinity' ) {
395 return array('ipb_expiry_temp');
399 # Note: for a user block, ipb_address is only for display purposes
400 $block = new Block( $this->BlockAddress
, $userId, $wgUser->getId(),
401 $reasonstr, wfTimestampNow(), 0, $expiry, $this->BlockAnonOnly
,
402 $this->BlockCreateAccount
, $this->BlockEnableAutoblock
, $this->BlockHideName
,
403 $this->BlockEmail
, isset( $this->BlockAllowUsertalk
) ?
$this->BlockAllowUsertalk
: $wgBlockAllowsUTEdit );
405 if ( wfRunHooks('BlockIp', array(&$block, &$wgUser)) ) {
407 if ( !$block->insert() ) {
408 if ( !$this->BlockReblock
) {
409 return array( 'ipb_already_blocked' );
411 # This returns direct blocks before autoblocks/rangeblocks, since we should
412 # be sure the user is blocked by now it should work for our purposes
413 $currentBlock = Block
::newFromDB( $this->BlockAddress
, $userId );
414 if( $block->equals( $currentBlock ) ) {
415 return array( 'ipb_already_blocked' );
417 $currentBlock->delete();
419 $log_action = 'reblock';
422 $log_action = 'block';
424 wfRunHooks('BlockIpComplete', array($block, $wgUser));
426 if ( $this->BlockWatchUser
) {
427 $wgUser->addWatch ( Title
::makeTitle( NS_USER
, $this->BlockAddress
) );
430 # Prepare log parameters
431 $logParams = array();
432 $logParams[] = $expirestr;
433 $logParams[] = $this->blockLogFlags();
435 # Make log entry, if the name is hidden, put it in the oversight log
436 $log_type = ($this->BlockHideName
) ?
'suppress' : 'block';
437 $log = new LogPage( $log_type );
438 $log->addEntry( $log_action, Title
::makeTitle( NS_USER
, $this->BlockAddress
),
439 $reasonstr, $logParams );
445 return array('hookaborted');
449 * UI entry point for blocking
450 * Wraps around doBlock()
455 $retval = $this->doBlock();
457 $titleObj = SpecialPage
::getTitleFor( 'Blockip' );
458 $wgOut->redirect( $titleObj->getFullURL( 'action=success&ip=' .
459 urlencode( $this->BlockAddress
) ) );
462 $this->showForm( $retval );
465 function showSuccess() {
468 $wgOut->setPagetitle( wfMsg( 'blockip' ) );
469 $wgOut->setSubtitle( wfMsg( 'blockipsuccesssub' ) );
470 $text = wfMsgExt( 'blockipsuccesstext', array( 'parse' ), $this->BlockAddress
);
471 $wgOut->addHTML( $text );
474 function showLogFragment( $out, $title ) {
476 $out->addHTML( Xml
::element( 'h2', NULL, LogPage
::logName( 'block' ) ) );
477 $count = LogEventsList
::showLogExtract( $out, 'block', $title->getPrefixedText(), '', 10 );
479 $out->addHTML( $wgUser->getSkin()->link(
480 SpecialPage
::getTitleFor( 'Log' ),
481 wfMsgHtml( 'blocklog-fulllog' ),
485 'page' => $title->getPrefixedText() ) ) );
490 * Return a comma-delimited list of "flags" to be passed to the log
491 * reader for this block, to provide more information in the logs
495 private function blockLogFlags() {
496 global $wgBlockAllowsUTEdit;
498 if( $this->BlockAnonOnly
&& IP
::isIPAddress( $this->BlockAddress
) )
499 // when blocking a user the option 'anononly' is not available/has no effect -> do not write this into log
500 $flags[] = 'anononly';
501 if( $this->BlockCreateAccount
)
502 $flags[] = 'nocreate';
503 if( !$this->BlockEnableAutoblock
)
504 $flags[] = 'noautoblock';
505 if ( $this->BlockEmail
)
506 $flags[] = 'noemail';
507 if ( !$this->BlockAllowUsertalk
&& $wgBlockAllowsUTEdit )
508 $flags[] = 'nousertalk';
509 return implode( ',', $flags );
513 * Builds unblock and block list links
517 private function getConvenienceLinks() {
519 $skin = $wgUser->getSkin();
520 if( $this->BlockAddress
)
521 $links[] = $this->getContribsLink( $skin );
522 $links[] = $this->getUnblockLink( $skin );
523 $links[] = $this->getBlockListLink( $skin );
524 $links[] = $skin->makeLink ( 'MediaWiki:Ipbreason-dropdown', wfMsgHtml( 'ipb-edit-dropdown' ) );
525 return '<p class="mw-ipb-conveniencelinks">' . implode( ' | ', $links ) . '</p>';
529 * Build a convenient link to a user or IP's contribs
532 * @param $skin Skin to use
535 private function getContribsLink( $skin ) {
536 $contribsPage = SpecialPage
::getTitleFor( 'Contributions', $this->BlockAddress
);
537 return $skin->link( $contribsPage, wfMsgHtml( 'ipb-blocklist-contribs', $this->BlockAddress
) );
541 * Build a convenient link to unblock the given username or IP
542 * address, if available; otherwise link to a blank unblock
545 * @param $skin Skin to use
548 private function getUnblockLink( $skin ) {
549 $list = SpecialPage
::getTitleFor( 'Ipblocklist' );
550 if( $this->BlockAddress
) {
551 $addr = htmlspecialchars( strtr( $this->BlockAddress
, '_', ' ' ) );
552 return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-unblock-addr', $addr ),
553 'action=unblock&ip=' . urlencode( $this->BlockAddress
) );
555 return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-unblock' ), 'action=unblock' );
560 * Build a convenience link to the block list
562 * @param $skin Skin to use
565 private function getBlockListLink( $skin ) {
566 $list = SpecialPage
::getTitleFor( 'Ipblocklist' );
567 if( $this->BlockAddress
) {
568 $addr = htmlspecialchars( strtr( $this->BlockAddress
, '_', ' ' ) );
569 return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-blocklist-addr', $addr ),
570 'ip=' . urlencode( $this->BlockAddress
) );
572 return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-blocklist' ) );
577 * Block a list of selected users
578 * @param array $users
579 * @param string $reason
580 * @param string $tag replaces user pages
581 * @param string $talkTag replaces user talk pages
582 * @returns array, list of html-safe usernames
584 public static function doMassUserBlock( $users, $reason = '', $tag = '', $talkTag = '' ) {
586 $counter = $blockSize = 0;
587 $safeUsers = array();
588 $log = new LogPage( 'block' );
589 foreach( $users as $name ) {
593 # Lets not go *too* fast
594 if( $blockSize >= 20 ) {
596 wfWaitForSlaves( 5 );
598 $u = User
::newFromName( $name, false );
599 // If user doesn't exist, it ought to be an IP then
600 if( is_null($u) ||
(!$u->getId() && !IP
::isIPAddress( $u->getName() )) ) {
603 $userTitle = $u->getUserPage();
604 $userTalkTitle = $u->getTalkPage();
605 $userpage = new Article( $userTitle );
606 $usertalk = new Article( $userTalkTitle );
607 $safeUsers[] = '[[' . $userTitle->getPrefixedText() . '|' . $userTitle->getText() . ']]';
608 $expirestr = $u->getId() ?
'indefinite' : '1 week';
609 $expiry = Block
::parseExpiryInput( $expirestr );
610 $anonOnly = IP
::isIPAddress( $u->getName() ) ?
1 : 0;
612 $block = new Block( $u->getName(), // victim
614 $wgUser->getId(), // blocker
616 wfTimestampNow(), // block time
619 $anonOnly, // anononly?
620 1, // block account creation?
623 0 // block from sending email?
625 $oldblock = Block
::newFromDB( $u->getName(), $u->getId() );
628 # Prepare log parameters
629 $logParams = array();
630 $logParams[] = $expirestr;
632 $logParams[] = 'anononly';
634 $logParams[] = 'nocreate';
636 $log->addEntry( 'block', $userTitle, $reason, $logParams );
638 # Tag userpage! (check length to avoid mistakes)
639 if( strlen($tag) > 2 ) {
640 $userpage->doEdit( $tag, $reason, EDIT_MINOR
);
642 if( strlen($talkTag) > 2 ) {
643 $usertalk->doEdit( $talkTag, $reason, EDIT_MINOR
);