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
&& !($currentBlock->mRangeStart
&& $currentBlock->mAddress
!= $this->BlockAddress
) ) {
104 $wgOut->addWikiMsg( 'ipb-needreblock', $this->BlockAddress
);
105 $alreadyBlocked = true;
109 $scBlockExpiryOptions = wfMsgForContent( 'ipboptions' );
111 $showblockoptions = $scBlockExpiryOptions != '-';
112 if (!$showblockoptions)
113 $mIpbother = $mIpbexpiry;
115 $blockExpiryFormOptions = Xml
::option( wfMsg( 'ipbotheroption' ), 'other' );
116 foreach (explode(',', $scBlockExpiryOptions) as $option) {
117 if ( strpos($option, ":") === false ) $option = "$option:$option";
118 list($show, $value) = explode(":", $option);
119 $show = htmlspecialchars($show);
120 $value = htmlspecialchars($value);
121 $blockExpiryFormOptions .= Xml
::option( $show, $value, $this->BlockExpiry
=== $value ?
true : false ) . "\n";
124 $reasonDropDown = Xml
::listDropDown( 'wpBlockReasonList',
125 wfMsgForContent( 'ipbreason-dropdown' ),
126 wfMsgForContent( 'ipbreasonotherlist' ), $this->BlockReasonList
, 'wpBlockDropDown', 4 );
128 global $wgStylePath, $wgStyleVersion;
130 Xml
::tags( 'script', array( 'type' => 'text/javascript', 'src' => "$wgStylePath/common/block.js?$wgStyleVersion" ), '' ) .
131 Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $titleObj->getLocalURL( "action=submit" ), 'id' => 'blockip' ) ) .
132 Xml
::openElement( 'fieldset' ) .
133 Xml
::element( 'legend', null, wfMsg( 'blockip-legend' ) ) .
134 Xml
::openElement( 'table', array ( 'border' => '0', 'id' => 'mw-blockip-table' ) ) .
136 <td class='mw-label'>
139 <td class='mw-input'>" .
140 Xml
::input( 'wpBlockAddress', 45, $this->BlockAddress
,
143 'id' => 'mw-bi-target',
144 'onchange' => 'updateBlockOptions()' ) ). "
149 if ( $showblockoptions ) {
151 <td class='mw-label'>
154 <td class='mw-input'>" .
157 'id' => 'wpBlockExpiry',
158 'name' => 'wpBlockExpiry',
159 'onchange' => 'considerChangingExpiryFocus()',
161 $blockExpiryFormOptions ) .
167 <tr id='wpBlockOther'>
168 <td class='mw-label'>
171 <td class='mw-input'>" .
172 Xml
::input( 'wpBlockOther', 45, $this->BlockOther
,
173 array( 'tabindex' => '3', 'id' => 'mw-bi-other' ) ) . "
177 <td class='mw-label'>
180 <td class='mw-input'>
184 <tr id=\"wpBlockReason\">
185 <td class='mw-label'>
188 <td class='mw-input'>" .
189 Xml
::input( 'wpBlockReason', 45, $this->BlockReason
,
190 array( 'tabindex' => '5', 'id' => 'mw-bi-reason', 'maxlength'=> '200' ) ) . "
193 <tr id='wpAnonOnlyRow'>
195 <td class='mw-input'>" .
196 Xml
::checkLabel( wfMsg( 'ipbanononly' ),
197 'wpAnonOnly', 'wpAnonOnly', $this->BlockAnonOnly
,
198 array( 'tabindex' => '6' ) ) . "
201 <tr id='wpCreateAccountRow'>
203 <td class='mw-input'>" .
204 Xml
::checkLabel( wfMsg( 'ipbcreateaccount' ),
205 'wpCreateAccount', 'wpCreateAccount', $this->BlockCreateAccount
,
206 array( 'tabindex' => '7' ) ) . "
209 <tr id='wpEnableAutoblockRow'>
211 <td class='mw-input'>" .
212 Xml
::checkLabel( wfMsg( 'ipbenableautoblock' ),
213 'wpEnableAutoblock', 'wpEnableAutoblock', $this->BlockEnableAutoblock
,
214 array( 'tabindex' => '8' ) ) . "
219 global $wgSysopEmailBans, $wgBlockAllowsUTEdit;
220 if ( $wgSysopEmailBans && $wgUser->isAllowed( 'blockemail' ) ) {
222 <tr id='wpEnableEmailBan'>
224 <td class='mw-input'>" .
225 Xml
::checkLabel( wfMsg( 'ipbemailban' ),
226 'wpEmailBan', 'wpEmailBan', $this->BlockEmail
,
227 array( 'tabindex' => '9' )) . "
233 // Allow some users to hide name from block log, blocklist and listusers
234 if ( $wgUser->isAllowed( 'hideuser' ) ) {
236 <tr id='wpEnableHideUser'>
238 <td class='mw-input'>" .
239 Xml
::checkLabel( wfMsg( 'ipbhidename' ),
240 'wpHideName', 'wpHideName', $this->BlockHideName
,
241 array( 'tabindex' => '10' ) ) . "
247 # Watchlist their user page?
249 <tr id='wpEnableWatchUser'>
251 <td class='mw-input'>" .
252 Xml
::checkLabel( wfMsg( 'ipbwatchuser' ),
253 'wpWatchUser', 'wpWatchUser', $this->BlockWatchUser
,
254 array( 'tabindex' => '11' ) ) . "
258 if( $wgBlockAllowsUTEdit ){
260 <tr id='wpAllowUsertalkRow'>
262 <td class='mw-input'>" .
263 Xml
::checkLabel( wfMsg( 'ipballowusertalk' ),
264 'wpAllowUsertalk', 'wpAllowUsertalk', $this->BlockAllowUsertalk
,
265 array( 'tabindex' => '12' ) ) . "
270 if ( $alreadyBlocked ) {
272 <tr id='wpChangeBlockRow'>
274 <td class='mw-input'>" .
275 Xml
::checkLabel( wfMsg( 'ipb-change-block' ),
276 'wpChangeBlock', 'wpChangeBlock', $this->BlockReblock
,
277 array( 'tabindex' => '13' ) ) . "
285 <td style='padding-top: 1em'> </td>
286 <td class='mw-submit' style='padding-top: 1em'>" .
287 Xml
::submitButton( wfMsg( 'ipbsubmit' ),
288 array( 'name' => 'wpBlock', 'tabindex' => '13', 'accesskey' => 's' ) ) . "
291 Xml
::closeElement( 'table' ) .
292 Xml
::hidden( 'wpEditToken', $wgUser->editToken() ) .
293 Xml
::closeElement( 'fieldset' ) .
294 Xml
::closeElement( 'form' ) .
295 Xml
::tags( 'script', array( 'type' => 'text/javascript' ), 'updateBlockOptions()' ) . "\n"
298 $wgOut->addHtml( $this->getConvenienceLinks() );
300 if( is_object( $user ) ) {
301 $this->showLogFragment( $wgOut, $user->getUserPage() );
302 } elseif( preg_match( '/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/', $this->BlockAddress
) ) {
303 $this->showLogFragment( $wgOut, Title
::makeTitle( NS_USER
, $this->BlockAddress
) );
304 } 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
) ) {
305 $this->showLogFragment( $wgOut, Title
::makeTitle( NS_USER
, $this->BlockAddress
) );
310 * Backend block code.
311 * $userID and $expiry will be filled accordingly
312 * @return array(message key, arguments) on failure, empty array on success
314 function doBlock( &$userId = null, &$expiry = null ) {
315 global $wgUser, $wgSysopUserBans, $wgSysopRangeBans, $wgBlockAllowsUTEdit;
318 # Expand valid IPv6 addresses, usernames are left as is
319 $this->BlockAddress
= IP
::sanitizeIP( $this->BlockAddress
);
320 # isIPv4() and IPv6() are used for final validation
321 $rxIP4 = '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}';
322 $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}';
323 $rxIP = "($rxIP4|$rxIP6)";
325 # Check for invalid specifications
326 if ( !preg_match( "/^$rxIP$/", $this->BlockAddress
) ) {
328 if ( preg_match( "/^($rxIP4)\\/(\\d{1,2})$/", $this->BlockAddress
, $matches ) ) {
330 if ( $wgSysopRangeBans ) {
331 if ( !IP
::isIPv4( $this->BlockAddress
) ||
$matches[2] < 16 ||
$matches[2] > 32 ) {
332 return array('ip_range_invalid');
334 $this->BlockAddress
= Block
::normaliseRange( $this->BlockAddress
);
336 # Range block illegal
337 return array('range_block_disabled');
339 } else if ( preg_match( "/^($rxIP6)\\/(\\d{1,3})$/", $this->BlockAddress
, $matches ) ) {
341 if ( $wgSysopRangeBans ) {
342 if ( !IP
::isIPv6( $this->BlockAddress
) ||
$matches[2] < 64 ||
$matches[2] > 128 ) {
343 return array('ip_range_invalid');
345 $this->BlockAddress
= Block
::normaliseRange( $this->BlockAddress
);
347 # Range block illegal
348 return array('range_block_disabled');
352 if ( $wgSysopUserBans ) {
353 $user = User
::newFromName( $this->BlockAddress
);
354 if( !is_null( $user ) && $user->getId() ) {
356 $userId = $user->getId();
357 $this->BlockAddress
= $user->getName();
359 return array('nosuchusershort', htmlspecialchars( $user ?
$user->getName() : $this->BlockAddress
) );
362 return array('badipaddress');
367 if ( $wgUser->isBlocked() && ( $wgUser->getId() !== $userId ) ) {
368 return array( 'cant-block-while-blocked' );
371 $reasonstr = $this->BlockReasonList
;
372 if ( $reasonstr != 'other' && $this->BlockReason
!= '' ) {
373 // Entry from drop down menu + additional comment
374 $reasonstr .= ': ' . $this->BlockReason
;
375 } elseif ( $reasonstr == 'other' ) {
376 $reasonstr = $this->BlockReason
;
379 $expirestr = $this->BlockExpiry
;
380 if( $expirestr == 'other' )
381 $expirestr = $this->BlockOther
;
383 if ( ( strlen( $expirestr ) == 0) ||
( strlen( $expirestr ) > 50) ) {
384 return array('ipb_expiry_invalid');
387 if ( false === ($expiry = Block
::parseExpiryInput( $expirestr )) ) {
389 return array('ipb_expiry_invalid');
392 if( $this->BlockHideName
&& $expiry != 'infinity' ) {
394 return array('ipb_expiry_temp');
398 # Note: for a user block, ipb_address is only for display purposes
399 $block = new Block( $this->BlockAddress
, $userId, $wgUser->getId(),
400 $reasonstr, wfTimestampNow(), 0, $expiry, $this->BlockAnonOnly
,
401 $this->BlockCreateAccount
, $this->BlockEnableAutoblock
, $this->BlockHideName
,
402 $this->BlockEmail
, isset( $this->BlockAllowUsertalk
) ?
$this->BlockAllowUsertalk
: $wgBlockAllowsUTEdit );
404 if ( wfRunHooks('BlockIp', array(&$block, &$wgUser)) ) {
406 if ( !$block->insert() ) {
407 if ( !$this->BlockReblock
) {
408 return array( 'ipb_already_blocked' );
410 # This returns direct blocks before autoblocks/rangeblocks, since we should be sure the user is blocked by now it should work for our purposes
411 $currentBlock = Block
::newFromDB( $this->BlockAddress
, $userId );
412 $currentBlock->delete();
414 $log_action = 'reblock';
417 $log_action = 'block';
419 wfRunHooks('BlockIpComplete', array($block, $wgUser));
421 if ( $this->BlockWatchUser
) {
422 $wgUser->addWatch ( Title
::makeTitle( NS_USER
, $this->BlockAddress
) );
425 # Prepare log parameters
426 $logParams = array();
427 $logParams[] = $expirestr;
428 $logParams[] = $this->blockLogFlags();
430 # Make log entry, if the name is hidden, put it in the oversight log
431 $log_type = ($this->BlockHideName
) ?
'suppress' : 'block';
432 $log = new LogPage( $log_type );
433 $log->addEntry( $log_action, Title
::makeTitle( NS_USER
, $this->BlockAddress
),
434 $reasonstr, $logParams );
440 return array('hookaborted');
444 * UI entry point for blocking
445 * Wraps around doBlock()
450 $retval = $this->doBlock();
452 $titleObj = SpecialPage
::getTitleFor( 'Blockip' );
453 $wgOut->redirect( $titleObj->getFullURL( 'action=success&ip=' .
454 urlencode( $this->BlockAddress
) ) );
457 $this->showForm( $retval );
460 function showSuccess() {
463 $wgOut->setPagetitle( wfMsg( 'blockip' ) );
464 $wgOut->setSubtitle( wfMsg( 'blockipsuccesssub' ) );
465 $text = wfMsgExt( 'blockipsuccesstext', array( 'parse' ), $this->BlockAddress
);
466 $wgOut->addHtml( $text );
469 function showLogFragment( $out, $title ) {
471 $out->addHtml( Xml
::element( 'h2', NULL, LogPage
::logName( 'block' ) ) );
472 $count = LogEventsList
::showLogExtract( $out, 'block', $title->getPrefixedText(), '', 10 );
474 $out->addHtml( $wgUser->getSkin()->link(
475 SpecialPage
::getTitleFor( 'Log' ),
476 wfMsgHtml( 'blocklog-fulllog' ),
480 'page' => $title->getPrefixedText() ) ) );
485 * Return a comma-delimited list of "flags" to be passed to the log
486 * reader for this block, to provide more information in the logs
490 private function blockLogFlags() {
491 global $wgBlockAllowsUTEdit;
493 if( $this->BlockAnonOnly
&& IP
::isIPAddress( $this->BlockAddress
) )
494 // when blocking a user the option 'anononly' is not available/has no effect -> do not write this into log
495 $flags[] = 'anononly';
496 if( $this->BlockCreateAccount
)
497 $flags[] = 'nocreate';
498 if( !$this->BlockEnableAutoblock
)
499 $flags[] = 'noautoblock';
500 if ( $this->BlockEmail
)
501 $flags[] = 'noemail';
502 if ( !$this->BlockAllowUsertalk
&& $wgBlockAllowsUTEdit )
503 $flags[] = 'nousertalk';
504 return implode( ',', $flags );
508 * Builds unblock and block list links
512 private function getConvenienceLinks() {
514 $skin = $wgUser->getSkin();
515 if( $this->BlockAddress
)
516 $links[] = $this->getContribsLink( $skin );
517 $links[] = $this->getUnblockLink( $skin );
518 $links[] = $this->getBlockListLink( $skin );
519 $links[] = $skin->makeLink ( 'MediaWiki:Ipbreason-dropdown', wfMsgHtml( 'ipb-edit-dropdown' ) );
520 return '<p class="mw-ipb-conveniencelinks">' . implode( ' | ', $links ) . '</p>';
524 * Build a convenient link to a user or IP's contribs
527 * @param $skin Skin to use
530 private function getContribsLink( $skin ) {
531 $contribsPage = SpecialPage
::getTitleFor( 'Contributions', $this->BlockAddress
);
532 return $skin->link( $contribsPage, wfMsgHtml( 'ipb-blocklist-contribs', $this->BlockAddress
) );
536 * Build a convenient link to unblock the given username or IP
537 * address, if available; otherwise link to a blank unblock
540 * @param $skin Skin to use
543 private function getUnblockLink( $skin ) {
544 $list = SpecialPage
::getTitleFor( 'Ipblocklist' );
545 if( $this->BlockAddress
) {
546 $addr = htmlspecialchars( strtr( $this->BlockAddress
, '_', ' ' ) );
547 return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-unblock-addr', $addr ),
548 'action=unblock&ip=' . urlencode( $this->BlockAddress
) );
550 return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-unblock' ), 'action=unblock' );
555 * Build a convenience link to the block list
557 * @param $skin Skin to use
560 private function getBlockListLink( $skin ) {
561 $list = SpecialPage
::getTitleFor( 'Ipblocklist' );
562 if( $this->BlockAddress
) {
563 $addr = htmlspecialchars( strtr( $this->BlockAddress
, '_', ' ' ) );
564 return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-blocklist-addr', $addr ),
565 'ip=' . urlencode( $this->BlockAddress
) );
567 return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-blocklist' ) );
572 * Block a list of selected users
573 * @param array $users
574 * @param string $reason
576 * @returns array, list of html-safe usernames
578 public static function doMassUserBlock( $users, $reason = '', $tag = '' ) {
580 $counter = $blockSize = 0;
581 $safeUsers = array();
582 $log = new LogPage( 'block' );
583 foreach( $users as $name ) {
587 # Lets not go *too* fast
588 if( $blockSize >= 20 ) {
590 wfWaitForSlaves( 5 );
592 $u = User
::newFromName( $name, false );
593 // If user doesn't exist, it ought to be an IP then
594 if( is_null($u) ||
(!$u->getId() && !IP
::isIPAddress( $u->getName() )) ) {
597 $usertitle = Title
::makeTitle( NS_USER
, $u->getName() );
598 $userpage = new Article( $usertitle );
599 $safeUsers[] = '[[' . $usertitle->getPrefixedText() . '|' . $usertitle->getText() . ']]';
600 $expirestr = $u->getId() ?
'indefinite' : '1 week';
601 $expiry = Block
::parseExpiryInput( $expirestr );
602 $anonOnly = IP
::isIPAddress( $u->getName() ) ?
1 : 0;
604 $block = new Block( $u->getName(), // victim
606 $wgUser->getId(), // blocker
608 wfTimestampNow(), // block time
611 $anonOnly, // anononly?
612 1, // block account creation?
615 0 // block from sending email?
617 $oldblock = Block
::newFromDB( $u->getName(), $u->getId() );
620 # Prepare log parameters
621 $logParams = array();
622 $logParams[] = $expirestr;
624 $logParams[] = 'anononly';
626 $logParams[] = 'nocreate';
628 $log->addEntry( 'block', $usertitle, $reason, $logParams );
630 # Tag userpage! (check length to avoid mistakes)
631 if( strlen($tag) > 2 ) {
632 $userpage->doEdit( $tag, $reason, EDIT_MINOR
);