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 );
72 function showForm( $err ) {
73 global $wgOut, $wgUser, $wgSysopUserBans;
75 $wgOut->setPagetitle( wfMsg( 'blockip' ) );
76 $wgOut->addWikiMsg( 'blockiptext' );
78 if($wgSysopUserBans) {
79 $mIpaddress = Xml
::label( wfMsg( 'ipadressorusername' ), 'mw-bi-target' );
81 $mIpaddress = Xml
::label( wfMsg( 'ipaddress' ), 'mw-bi-target' );
83 $mIpbexpiry = Xml
::label( wfMsg( 'ipbexpiry' ), 'wpBlockExpiry' );
84 $mIpbother = Xml
::label( wfMsg( 'ipbother' ), 'mw-bi-other' );
85 $mIpbreasonother = Xml
::label( wfMsg( 'ipbreason' ), 'wpBlockReasonList' );
86 $mIpbreason = Xml
::label( wfMsg( 'ipbotherreason' ), 'mw-bi-reason' );
88 $titleObj = SpecialPage
::getTitleFor( 'Blockip' );
91 $wgOut->setSubtitle( wfMsgHtml( 'formerror' ) );
92 $wgOut->addHTML( Xml
::tags( 'p', array( 'class' => 'error' ), $err ) );
95 $scBlockExpiryOptions = wfMsgForContent( 'ipboptions' );
97 $showblockoptions = $scBlockExpiryOptions != '-';
98 if (!$showblockoptions)
99 $mIpbother = $mIpbexpiry;
101 $blockExpiryFormOptions = Xml
::option( wfMsg( 'ipbotheroption' ), 'other' );
102 foreach (explode(',', $scBlockExpiryOptions) as $option) {
103 if ( strpos($option, ":") === false ) $option = "$option:$option";
104 list($show, $value) = explode(":", $option);
105 $show = htmlspecialchars($show);
106 $value = htmlspecialchars($value);
107 $blockExpiryFormOptions .= Xml
::option( $show, $value, $this->BlockExpiry
=== $value ?
true : false ) . "\n";
110 $reasonDropDown = Xml
::listDropDown( 'wpBlockReasonList',
111 wfMsgForContent( 'ipbreason-dropdown' ),
112 wfMsgForContent( 'ipbreasonotherlist' ), $this->BlockReasonList
, 'wpBlockDropDown', 4 );
114 global $wgStylePath, $wgStyleVersion;
116 Xml
::tags( 'script', array( 'type' => 'text/javascript', 'src' => "$wgStylePath/common/block.js?$wgStyleVersion" ), '' ) .
117 Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $titleObj->getLocalURL( "action=submit" ), 'id' => 'blockip' ) ) .
118 Xml
::openElement( 'fieldset' ) .
119 Xml
::element( 'legend', null, wfMsg( 'blockip-legend' ) ) .
120 Xml
::openElement( 'table', array ( 'border' => '0', 'id' => 'mw-blockip-table' ) ) .
122 <td class='mw-label'>
125 <td class='mw-input'>" .
126 Xml
::input( 'wpBlockAddress', 45, $this->BlockAddress
,
129 'id' => 'mw-bi-target',
130 'onchange' => 'updateBlockOptions()' ) ). "
135 if ( $showblockoptions ) {
137 <td class='mw-label'>
140 <td class='mw-input'>" .
143 'id' => 'wpBlockExpiry',
144 'name' => 'wpBlockExpiry',
145 'onchange' => 'considerChangingExpiryFocus()',
147 $blockExpiryFormOptions ) .
153 <tr id='wpBlockOther'>
154 <td class='mw-label'>
157 <td class='mw-input'>" .
158 Xml
::input( 'wpBlockOther', 45, $this->BlockOther
,
159 array( 'tabindex' => '3', 'id' => 'mw-bi-other' ) ) . "
163 <td class='mw-label'>
166 <td class='mw-input'>
170 <tr id=\"wpBlockReason\">
171 <td class='mw-label'>
174 <td class='mw-input'>" .
175 Xml
::input( 'wpBlockReason', 45, $this->BlockReason
,
176 array( 'tabindex' => '5', 'id' => 'mw-bi-reason', 'maxlength'=> '200' ) ) . "
179 <tr id='wpAnonOnlyRow'>
181 <td class='mw-input'>" .
182 Xml
::checkLabel( wfMsg( 'ipbanononly' ),
183 'wpAnonOnly', 'wpAnonOnly', $this->BlockAnonOnly
,
184 array( 'tabindex' => '6' ) ) . "
187 <tr id='wpCreateAccountRow'>
189 <td class='mw-input'>" .
190 Xml
::checkLabel( wfMsg( 'ipbcreateaccount' ),
191 'wpCreateAccount', 'wpCreateAccount', $this->BlockCreateAccount
,
192 array( 'tabindex' => '7' ) ) . "
195 <tr id='wpEnableAutoblockRow'>
197 <td class='mw-input'>" .
198 Xml
::checkLabel( wfMsg( 'ipbenableautoblock' ),
199 'wpEnableAutoblock', 'wpEnableAutoblock', $this->BlockEnableAutoblock
,
200 array( 'tabindex' => '8' ) ) . "
205 global $wgSysopEmailBans, $wgBlockAllowsUTEdit;
206 if ( $wgSysopEmailBans && $wgUser->isAllowed( 'blockemail' ) ) {
208 <tr id='wpEnableEmailBan'>
210 <td class='mw-input'>" .
211 Xml
::checkLabel( wfMsg( 'ipbemailban' ),
212 'wpEmailBan', 'wpEmailBan', $this->BlockEmail
,
213 array( 'tabindex' => '9' )) . "
219 // Allow some users to hide name from block log, blocklist and listusers
220 if ( $wgUser->isAllowed( 'hideuser' ) ) {
222 <tr id='wpEnableHideUser'>
224 <td class='mw-input'>" .
225 Xml
::checkLabel( wfMsg( 'ipbhidename' ),
226 'wpHideName', 'wpHideName', $this->BlockHideName
,
227 array( 'tabindex' => '10' ) ) . "
233 # Watchlist their user page?
235 <tr id='wpEnableWatchUser'>
237 <td class='mw-input'>" .
238 Xml
::checkLabel( wfMsg( 'ipbwatchuser' ),
239 'wpWatchUser', 'wpWatchUser', $this->BlockWatchUser
,
240 array( 'tabindex' => '11' ) ) . "
244 if( $wgBlockAllowsUTEdit ){
246 <tr id='wpAllowUsertalkRow'>
248 <td class='mw-input'>" .
249 Xml
::checkLabel( wfMsg( 'ipballowusertalk' ),
250 'wpAllowUsertalk', 'wpAllowUsertalk', $this->BlockAllowUsertalk
,
251 array( 'tabindex' => '12' ) ) . "
259 <td style='padding-top: 1em'> </td>
260 <td class='mw-submit' style='padding-top: 1em'>" .
261 Xml
::submitButton( wfMsg( 'ipbsubmit' ),
262 array( 'name' => 'wpBlock', 'tabindex' => '13', 'accesskey' => 's' ) ) . "
265 Xml
::closeElement( 'table' ) .
266 Xml
::hidden( 'wpEditToken', $wgUser->editToken() ) .
267 Xml
::closeElement( 'fieldset' ) .
268 Xml
::closeElement( 'form' ) .
269 Xml
::tags( 'script', array( 'type' => 'text/javascript' ), 'updateBlockOptions()' ) . "\n"
272 $wgOut->addHtml( $this->getConvenienceLinks() );
274 $user = User
::newFromName( $this->BlockAddress
);
275 if( is_object( $user ) ) {
276 $this->showLogFragment( $wgOut, $user->getUserPage() );
277 } elseif( preg_match( '/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/', $this->BlockAddress
) ) {
278 $this->showLogFragment( $wgOut, Title
::makeTitle( NS_USER
, $this->BlockAddress
) );
279 } 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
) ) {
280 $this->showLogFragment( $wgOut, Title
::makeTitle( NS_USER
, $this->BlockAddress
) );
285 * Backend block code.
286 * $userID and $expiry will be filled accordingly
287 * @return array(message key, arguments) on failure, empty array on success
289 function doBlock( &$userId = null, &$expiry = null ) {
290 global $wgUser, $wgSysopUserBans, $wgSysopRangeBans, $wgBlockAllowsUTEdit;
293 # Expand valid IPv6 addresses, usernames are left as is
294 $this->BlockAddress
= IP
::sanitizeIP( $this->BlockAddress
);
295 # isIPv4() and IPv6() are used for final validation
296 $rxIP4 = '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}';
297 $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}';
298 $rxIP = "($rxIP4|$rxIP6)";
300 # Check for invalid specifications
301 if ( !preg_match( "/^$rxIP$/", $this->BlockAddress
) ) {
303 if ( preg_match( "/^($rxIP4)\\/(\\d{1,2})$/", $this->BlockAddress
, $matches ) ) {
305 if ( $wgSysopRangeBans ) {
306 if ( !IP
::isIPv4( $this->BlockAddress
) ||
$matches[2] < 16 ||
$matches[2] > 32 ) {
307 return array('ip_range_invalid');
309 $this->BlockAddress
= Block
::normaliseRange( $this->BlockAddress
);
311 # Range block illegal
312 return array('range_block_disabled');
314 } else if ( preg_match( "/^($rxIP6)\\/(\\d{1,3})$/", $this->BlockAddress
, $matches ) ) {
316 if ( $wgSysopRangeBans ) {
317 if ( !IP
::isIPv6( $this->BlockAddress
) ||
$matches[2] < 64 ||
$matches[2] > 128 ) {
318 return array('ip_range_invalid');
320 $this->BlockAddress
= Block
::normaliseRange( $this->BlockAddress
);
322 # Range block illegal
323 return array('range_block_disabled');
327 if ( $wgSysopUserBans ) {
328 $user = User
::newFromName( $this->BlockAddress
);
329 if( !is_null( $user ) && $user->getId() ) {
331 $userId = $user->getId();
332 $this->BlockAddress
= $user->getName();
334 return array('nosuchusershort', htmlspecialchars( $user ?
$user->getName() : $this->BlockAddress
) );
337 return array('badipaddress');
342 if ( $wgUser->isBlocked() && ( $wgUser->getId() !== $userId ) ) {
343 return array( 'cant-block-while-blocked' );
346 $reasonstr = $this->BlockReasonList
;
347 if ( $reasonstr != 'other' && $this->BlockReason
!= '' ) {
348 // Entry from drop down menu + additional comment
349 $reasonstr .= ': ' . $this->BlockReason
;
350 } elseif ( $reasonstr == 'other' ) {
351 $reasonstr = $this->BlockReason
;
354 $expirestr = $this->BlockExpiry
;
355 if( $expirestr == 'other' )
356 $expirestr = $this->BlockOther
;
358 if ( ( strlen( $expirestr ) == 0) ||
( strlen( $expirestr ) > 50) ) {
359 return array('ipb_expiry_invalid');
362 if ( false === ($expiry = Block
::parseExpiryInput( $expirestr )) ) {
364 return array('ipb_expiry_invalid');
367 if( $this->BlockHideName
&& $expiry != 'infinity' ) {
369 return array('ipb_expiry_temp');
373 # Note: for a user block, ipb_address is only for display purposes
374 $block = new Block( $this->BlockAddress
, $userId, $wgUser->getId(),
375 $reasonstr, wfTimestampNow(), 0, $expiry, $this->BlockAnonOnly
,
376 $this->BlockCreateAccount
, $this->BlockEnableAutoblock
, $this->BlockHideName
,
377 $this->BlockEmail
, isset( $this->BlockAllowUsertalk
) ?
$this->BlockAllowUsertalk
: $wgBlockAllowsUTEdit );
379 if ( wfRunHooks('BlockIp', array(&$block, &$wgUser)) ) {
381 if ( !$block->insert() ) {
382 return array('ipb_already_blocked', htmlspecialchars($this->BlockAddress
));
385 wfRunHooks('BlockIpComplete', array($block, $wgUser));
387 if ( $this->BlockWatchUser
) {
388 $wgUser->addWatch ( Title
::makeTitle( NS_USER
, $this->BlockAddress
) );
391 # Prepare log parameters
392 $logParams = array();
393 $logParams[] = $expirestr;
394 $logParams[] = $this->blockLogFlags();
396 # Make log entry, if the name is hidden, put it in the oversight log
397 $log_type = ($this->BlockHideName
) ?
'suppress' : 'block';
398 $log = new LogPage( $log_type );
399 $log->addEntry( 'block', Title
::makeTitle( NS_USER
, $this->BlockAddress
),
400 $reasonstr, $logParams );
406 return array('hookaborted');
410 * UI entry point for blocking
411 * Wraps around doBlock()
416 $retval = $this->doBlock();
418 $titleObj = SpecialPage
::getTitleFor( 'Blockip' );
419 $wgOut->redirect( $titleObj->getFullURL( 'action=success&ip=' .
420 urlencode( $this->BlockAddress
) ) );
423 $key = array_shift($retval);
424 $this->showForm(wfMsgReal($key, $retval));
427 function showSuccess() {
430 $wgOut->setPagetitle( wfMsg( 'blockip' ) );
431 $wgOut->setSubtitle( wfMsg( 'blockipsuccesssub' ) );
432 $text = wfMsgExt( 'blockipsuccesstext', array( 'parse' ), $this->BlockAddress
);
433 $wgOut->addHtml( $text );
436 function showLogFragment( $out, $title ) {
438 $out->addHtml( Xml
::element( 'h2', NULL, LogPage
::logName( 'block' ) ) );
439 $count = LogEventsList
::showLogExtract( $out, 'block', $title->getPrefixedText(), '', 10 );
441 $out->addHtml( $wgUser->getSkin()->link(
442 SpecialPage
::getTitleFor( 'Log' ),
443 wfMsgHtml( 'blocklog-fulllog' ),
447 'page' => $title->getPrefixedText() ) ) );
452 * Return a comma-delimited list of "flags" to be passed to the log
453 * reader for this block, to provide more information in the logs
457 private function blockLogFlags() {
458 global $wgBlockAllowsUTEdit;
460 if( $this->BlockAnonOnly
&& IP
::isIPAddress( $this->BlockAddress
) )
461 // when blocking a user the option 'anononly' is not available/has no effect -> do not write this into log
462 $flags[] = 'anononly';
463 if( $this->BlockCreateAccount
)
464 $flags[] = 'nocreate';
465 if( !$this->BlockEnableAutoblock
)
466 $flags[] = 'noautoblock';
467 if ( $this->BlockEmail
)
468 $flags[] = 'noemail';
469 if ( !$this->BlockAllowUsertalk
&& $wgBlockAllowsUTEdit )
470 $flags[] = 'nousertalk';
471 return implode( ',', $flags );
475 * Builds unblock and block list links
479 private function getConvenienceLinks() {
481 $skin = $wgUser->getSkin();
482 $links[] = $skin->makeLink ( 'MediaWiki:Ipbreason-dropdown', wfMsgHtml( 'ipb-edit-dropdown' ) );
483 $links[] = $this->getUnblockLink( $skin );
484 $links[] = $this->getBlockListLink( $skin );
485 return '<p class="mw-ipb-conveniencelinks">' . implode( ' | ', $links ) . '</p>';
489 * Build a convenient link to unblock the given username or IP
490 * address, if available; otherwise link to a blank unblock
493 * @param $skin Skin to use
496 private function getUnblockLink( $skin ) {
497 $list = SpecialPage
::getTitleFor( 'Ipblocklist' );
498 if( $this->BlockAddress
) {
499 $addr = htmlspecialchars( strtr( $this->BlockAddress
, '_', ' ' ) );
500 return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-unblock-addr', $addr ),
501 'action=unblock&ip=' . urlencode( $this->BlockAddress
) );
503 return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-unblock' ), 'action=unblock' );
508 * Build a convenience link to the block list
510 * @param $skin Skin to use
513 private function getBlockListLink( $skin ) {
514 $list = SpecialPage
::getTitleFor( 'Ipblocklist' );
515 if( $this->BlockAddress
) {
516 $addr = htmlspecialchars( strtr( $this->BlockAddress
, '_', ' ' ) );
517 return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-blocklist-addr', $addr ),
518 'ip=' . urlencode( $this->BlockAddress
) );
520 return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-blocklist' ) );
525 * Block a list of selected users
526 * @param array $users
527 * @param string $reason
529 * @returns array, list of html-safe usernames
531 public static function doMassUserBlock( $users, $reason = '', $tag = '' ) {
533 $counter = $blockSize = 0;
534 $safeUsers = array();
535 $log = new LogPage( 'block' );
536 foreach( $users as $name ) {
540 # Lets not go *too* fast
541 if( $blockSize >= 20 ) {
543 wfWaitForSlaves( 5 );
545 $u = User
::newFromName( $name, false );
546 // If user doesn't exist, it ought to be an IP then
547 if( is_null($u) ||
(!$u->getId() && !IP
::isIPAddress( $u->getName() )) ) {
550 $usertitle = Title
::makeTitle( NS_USER
, $u->getName() );
551 $userpage = new Article( $usertitle );
552 $safeUsers[] = '[[' . $usertitle->getPrefixedText() . '|' . $usertitle->getText() . ']]';
553 $expirestr = $u->getId() ?
'indefinite' : '1 week';
554 $expiry = Block
::parseExpiryInput( $expirestr );
555 $anonOnly = IP
::isIPAddress( $u->getName() ) ?
1 : 0;
557 $block = new Block( $u->getName(), // victim
559 $wgUser->getId(), // blocker
561 wfTimestampNow(), // block time
564 $anonOnly, // anononly?
565 1, // block account creation?
568 0 // block from sending email?
570 $oldblock = Block
::newFromDB( $u->getName() );
573 # Prepare log parameters
574 $logParams = array();
575 $logParams[] = $expirestr;
577 $logParams[] = 'anononly';
579 $logParams[] = 'nocreate';
581 $log->addEntry( 'block', $usertitle, $reason, $logParams );
583 # Tag userpage! (check length to avoid mistakes)
584 if( strlen($tag) > 2 ) {
585 $userpage->doEdit( $tag, $reason, EDIT_MINOR
);