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' ) ) ) {
35 } else if ( $ipb->AlreadyBlocked
) {
36 $ipb->showForm( wfMsg('ipb_tweak_block', $ipb->BlockAddress
) );
43 * Form object for the Special:Blockip page.
45 * @ingroup SpecialPage
48 var $BlockAddress, $BlockExpiry, $BlockReason;
51 function IPBlockForm( $par ) {
52 global $wgRequest, $wgUser, $wgLang;
54 $this->BlockAddress
= $wgRequest->getVal( 'wpBlockAddress', $wgRequest->getVal( 'ip', $par ) );
55 $this->BlockAddress
= strtr( $this->BlockAddress
, '_', ' ' );
56 $this->AlreadyBlocked
= false;
58 if( $this->BlockAddress
&& !$wgRequest->wasPosted() ){
59 $this->mBlock
= new Block();
60 if( $this->mBlock
->load($this->BlockAddress
) && $this->BlockAddress
== $this->mBlock
->mAddress
) {
61 $this->AlreadyBlocked
= true;
62 $this->BlockReason
= wfMsgForContent( 'ipb_modifying_block' );
63 $this->BlockReasonList
= $wgRequest->getText( 'wpBlockReasonList' );
64 $this->BlockExpiry
= wfMsg('ipbotheroption');
65 $this->BlockOther
= $wgLang->timeanddate( $this->mBlock
->mExpiry
);
66 $this->BlockAnonOnly
= $wgRequest->getBool( 'wpAnonOnly', true );
67 $this->BlockCreateAccount
= $wgRequest->getBool( 'wpCreateAccount', true );
68 $this->BlockEnableAutoblock
= $wgRequest->getBool( 'wpEnableAutoblock', true );
69 $this->BlockEmail
= $wgRequest->getBool( 'wpEmailBan', false );
70 $this->BlockEmail
= $this->mBlock
->mBlockEmail
;
71 $this->BlockWatchUser
= $wgRequest->getBool( 'wpWatchUser', false );
72 # Re-check user's rights to hide names, very serious, defaults to 0
73 $this->BlockHideName
= ( $this->mBlock
->mHideName
&& $wgUser->isAllowed( 'hideuser' ) ) ?
1 : 0;
78 $this->BlockReason
= $wgRequest->getText( 'wpBlockReason' );
79 $this->BlockReasonList
= $wgRequest->getText( 'wpBlockReasonList' );
80 $this->BlockExpiry
= $wgRequest->getVal( 'wpBlockExpiry', wfMsg('ipbotheroption') );
81 $this->BlockOther
= $wgRequest->getVal( 'wpBlockOther', '' );
83 # Unchecked checkboxes are not included in the form data at all, so having one
84 # that is true by default is a bit tricky
85 $byDefault = !$wgRequest->wasPosted();
86 $this->BlockAnonOnly
= $wgRequest->getBool( 'wpAnonOnly', $byDefault );
87 $this->BlockCreateAccount
= $wgRequest->getBool( 'wpCreateAccount', $byDefault );
88 $this->BlockEnableAutoblock
= $wgRequest->getBool( 'wpEnableAutoblock', $byDefault );
89 $this->BlockEmail
= $wgRequest->getBool( 'wpEmailBan', false );
90 $this->BlockWatchUser
= $wgRequest->getBool( 'wpWatchUser', false );
91 # Re-check user's rights to hide names, very serious, defaults to 0
92 $this->BlockHideName
= ( $wgRequest->getBool( 'wpHideName', 0 ) && $wgUser->isAllowed( 'hideuser' ) ) ?
1 : 0;
95 function showForm( $err ) {
96 global $wgOut, $wgUser, $wgSysopUserBans, $wgRequest;
98 $wgOut->setPagetitle( wfMsg( 'blockip' ) );
99 $wgOut->addWikiMsg( 'blockiptext' );
101 if($wgSysopUserBans) {
102 $mIpaddress = Xml
::label( wfMsg( 'ipadressorusername' ), 'mw-bi-target' );
104 $mIpaddress = Xml
::label( wfMsg( 'ipaddress' ), 'mw-bi-target' );
106 $mIpbexpiry = Xml
::label( wfMsg( 'ipbexpiry' ), 'wpBlockExpiry' );
107 $mIpbother = Xml
::label( wfMsg( 'ipbother' ), 'mw-bi-other' );
108 $mIpbreasonother = Xml
::label( wfMsg( 'ipbreason' ), 'wpBlockReasonList' );
109 $mIpbreason = Xml
::label( wfMsg( 'ipbotherreason' ), 'mw-bi-reason' );
111 $titleObj = SpecialPage
::getTitleFor( 'Blockip' );
113 if ( $wgRequest->wasPosted() ) {
114 $wgOut->setSubtitle( wfMsgHtml( 'formerror' ) );
117 $wgOut->addHTML( Xml
::tags( 'p', array( 'class' => 'error' ), $err ) );
120 $scBlockExpiryOptions = wfMsgForContent( 'ipboptions' );
122 $showblockoptions = $scBlockExpiryOptions != '-';
123 if (!$showblockoptions)
124 $mIpbother = $mIpbexpiry;
126 $blockExpiryFormOptions = Xml
::option( wfMsg( 'ipbotheroption' ), 'other' );
127 foreach (explode(',', $scBlockExpiryOptions) as $option) {
128 if ( strpos($option, ":") === false ) $option = "$option:$option";
129 list($show, $value) = explode(":", $option);
130 $show = htmlspecialchars($show);
131 $value = htmlspecialchars($value);
132 $blockExpiryFormOptions .= Xml
::option( $show, $value, $this->BlockExpiry
=== $value ?
true : false ) . "\n";
135 $reasonDropDown = Xml
::listDropDown( 'wpBlockReasonList',
136 wfMsgForContent( 'ipbreason-dropdown' ),
137 wfMsgForContent( 'ipbreasonotherlist' ), '', 'wpBlockDropDown', 4 );
139 global $wgStylePath, $wgStyleVersion;
141 Xml
::tags( 'script', array( 'type' => 'text/javascript', 'src' => "$wgStylePath/common/block.js?$wgStyleVersion" ), '' ) .
142 Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $titleObj->getLocalURL( "action=submit" ), 'id' => 'blockip' ) ) .
143 Xml
::openElement( 'fieldset' ) .
144 Xml
::element( 'legend', null, wfMsg( 'blockip-legend' ) ) .
145 Xml
::openElement( 'table', array ( 'border' => '0', 'id' => 'mw-blockip-table' ) ) .
147 <td class='mw-label'>
150 <td class='mw-input'>" .
151 Xml
::input( 'wpBlockAddress', 45, $this->BlockAddress
,
154 'id' => 'mw-bi-target',
155 'onchange' => 'updateBlockOptions()' ) ). "
160 if ( $showblockoptions ) {
162 <td class='mw-label'>
165 <td class='mw-input'>" .
168 'id' => 'wpBlockExpiry',
169 'name' => 'wpBlockExpiry',
170 'onchange' => 'considerChangingExpiryFocus()',
172 $blockExpiryFormOptions ) .
178 <tr id='wpBlockOther'>
179 <td class='mw-label'>
182 <td class='mw-input'>" .
183 Xml
::input( 'wpBlockOther', 45, $this->BlockOther
,
184 array( 'tabindex' => '3', 'id' => 'mw-bi-other' ) ) . "
188 <td class='mw-label'>
191 <td class='mw-input'>
195 <tr id=\"wpBlockReason\">
196 <td class='mw-label'>
199 <td class='mw-input'>" .
200 Xml
::input( 'wpBlockReason', 45, $this->BlockReason
,
201 array( 'tabindex' => '5', 'id' => 'mw-bi-reason', 'maxlength'=> '200' ) ) . "
204 <tr id='wpAnonOnlyRow'>
206 <td class='mw-input'>" .
207 Xml
::checkLabel( wfMsg( 'ipbanononly' ),
208 'wpAnonOnly', 'wpAnonOnly', $this->BlockAnonOnly
,
209 array( 'tabindex' => '6' ) ) . "
212 <tr id='wpCreateAccountRow'>
214 <td class='mw-input'>" .
215 Xml
::checkLabel( wfMsg( 'ipbcreateaccount' ),
216 'wpCreateAccount', 'wpCreateAccount', $this->BlockCreateAccount
,
217 array( 'tabindex' => '7' ) ) . "
220 <tr id='wpEnableAutoblockRow'>
222 <td class='mw-input'>" .
223 Xml
::checkLabel( wfMsg( 'ipbenableautoblock' ),
224 'wpEnableAutoblock', 'wpEnableAutoblock', $this->BlockEnableAutoblock
,
225 array( 'tabindex' => '8' ) ) . "
230 global $wgSysopEmailBans;
231 if ( $wgSysopEmailBans && $wgUser->isAllowed( 'blockemail' ) ) {
233 <tr id='wpEnableEmailBan'>
235 <td class='mw-input'>" .
236 Xml
::checkLabel( wfMsg( 'ipbemailban' ),
237 'wpEmailBan', 'wpEmailBan', $this->BlockEmail
,
238 array( 'tabindex' => '9' )) . "
244 // Allow some users to hide name from block log, blocklist and listusers
245 if ( $wgUser->isAllowed( 'hideuser' ) ) {
247 <tr id='wpEnableHideUser'>
249 <td class='mw-input'>" .
250 Xml
::checkLabel( wfMsg( 'ipbhidename' ),
251 'wpHideName', 'wpHideName', $this->BlockHideName
,
252 array( 'tabindex' => '10' ) ) . "
258 # Watchlist their user page?
260 <tr id='wpEnableWatchUser'>
262 <td class='mw-input'>" .
263 Xml
::checkLabel( wfMsg( 'ipbwatchuser' ),
264 'wpWatchUser', 'wpWatchUser', $this->BlockWatchUser
,
265 array( 'tabindex' => '11' ) ) . "
272 <td style='padding-top: 1em'> </td>
273 <td class='mw-submit' style='padding-top: 1em'>" .
274 Xml
::submitButton( wfMsg( 'ipbsubmit' ),
275 array( 'name' => 'wpBlock', 'tabindex' => '12' ) ) . "
278 Xml
::closeElement( 'table' ) .
279 Xml
::hidden( 'wpEditToken', $wgUser->editToken() ) .
280 Xml
::closeElement( 'fieldset' ) .
281 Xml
::closeElement( 'form' ) .
282 Xml
::tags( 'script', array( 'type' => 'text/javascript' ), 'updateBlockOptions()' ) . "\n"
285 $wgOut->addHtml( $this->getConvenienceLinks() );
287 $user = User
::newFromName( $this->BlockAddress
);
288 if( is_object( $user ) ) {
289 $this->showLogFragment( $wgOut, $user->getUserPage() );
290 } elseif( preg_match( '/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/', $this->BlockAddress
) ) {
291 $this->showLogFragment( $wgOut, Title
::makeTitle( NS_USER
, $this->BlockAddress
) );
292 } 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
) ) {
293 $this->showLogFragment( $wgOut, Title
::makeTitle( NS_USER
, $this->BlockAddress
) );
298 * Backend block code.
299 * $userID and $expiry will be filled accordingly
300 * @return array(message key, arguments) on failure, empty array on success
302 function doBlock(&$userId = null, &$expiry = null)
304 global $wgUser, $wgSysopUserBans, $wgSysopRangeBans;
307 # Expand valid IPv6 addresses, usernames are left as is
308 $this->BlockAddress
= IP
::sanitizeIP( $this->BlockAddress
);
309 # isIPv4() and IPv6() are used for final validation
310 $rxIP4 = '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}';
311 $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}';
312 $rxIP = "($rxIP4|$rxIP6)";
314 # Check for invalid specifications
315 if ( !preg_match( "/^$rxIP$/", $this->BlockAddress
) ) {
317 if ( preg_match( "/^($rxIP4)\\/(\\d{1,2})$/", $this->BlockAddress
, $matches ) ) {
319 if ( $wgSysopRangeBans ) {
320 if ( !IP
::isIPv4( $this->BlockAddress
) ||
$matches[2] < 16 ||
$matches[2] > 32 ) {
321 return array('ip_range_invalid');
323 $this->BlockAddress
= Block
::normaliseRange( $this->BlockAddress
);
325 # Range block illegal
326 return array('range_block_disabled');
328 } else if ( preg_match( "/^($rxIP6)\\/(\\d{1,3})$/", $this->BlockAddress
, $matches ) ) {
330 if ( $wgSysopRangeBans ) {
331 if ( !IP
::isIPv6( $this->BlockAddress
) ||
$matches[2] < 64 ||
$matches[2] > 128 ) {
332 return array('ip_range_invalid');
334 $this->BlockAddress
= Block
::normaliseRange( $this->BlockAddress
);
336 # Range block illegal
337 return array('range_block_disabled');
341 if ( $wgSysopUserBans ) {
342 $user = User
::newFromName( $this->BlockAddress
);
343 if( !is_null( $user ) && $user->getId() ) {
345 $userId = $user->getId();
346 $this->BlockAddress
= $user->getName();
348 return array('nosuchusershort', htmlspecialchars( $user ?
$user->getName() : $this->BlockAddress
) );
351 return array('badipaddress');
356 $reasonstr = $this->BlockReasonList
;
357 if ( $reasonstr != 'other' && $this->BlockReason
!= '') {
358 // Entry from drop down menu + additional comment
359 $reasonstr .= ': ' . $this->BlockReason
;
360 } elseif ( $reasonstr == 'other' ) {
361 $reasonstr = $this->BlockReason
;
364 $expirestr = $this->BlockExpiry
;
365 if( $expirestr == 'other' )
366 $expirestr = $this->BlockOther
;
368 if (strlen($expirestr) == 0) {
369 return array('ipb_expiry_invalid');
372 if ( false === ($expiry = Block
::parseExpiryInput( $expirestr )) ) {
374 return array('ipb_expiry_invalid');
377 if( $this->BlockHideName
&& $expiry != 'infinity' ) {
379 return array('ipb_expiry_temp');
383 # Note: for a user block, ipb_address is only for display purposes
384 $block = new Block( $this->BlockAddress
, $userId, $wgUser->getId(),
385 $reasonstr, wfTimestampNow(), 0, $expiry, $this->BlockAnonOnly
,
386 $this->BlockCreateAccount
, $this->BlockEnableAutoblock
, $this->BlockHideName
,
389 if ( wfRunHooks('BlockIp', array(&$block, &$wgUser)) ) {
391 if ( !$block->insert() ) {
392 // Block already exists. Silently delete the existing block and insert it again
393 $oldblock = Block
::newFromDB( $this->BlockAddress
);
398 wfRunHooks('BlockIpComplete', array($block, $wgUser));
400 if ( $this->BlockWatchUser
) {
401 $wgUser->addWatch ( Title
::makeTitle( NS_USER
, $this->BlockAddress
) );
404 # Prepare log parameters
405 $logParams = array();
406 $logParams[] = $expirestr;
407 $logParams[] = $this->blockLogFlags();
409 # Make log entry, if the name is hidden, put it in the oversight log
410 $log_type = ($this->BlockHideName
) ?
'suppress' : 'block';
411 $log = new LogPage( $log_type );
412 $log->addEntry( 'block', Title
::makeTitle( NS_USER
, $this->BlockAddress
),
413 $reasonstr, $logParams );
419 return array('hookaborted');
423 * UI entry point for blocking
424 * Wraps around doBlock()
429 $retval = $this->doBlock();
431 $titleObj = SpecialPage
::getTitleFor( 'Blockip' );
432 $wgOut->redirect( $titleObj->getFullURL( 'action=success&ip=' .
433 urlencode( $this->BlockAddress
) ) );
436 $key = array_shift($retval);
437 $this->showForm(wfMsgReal($key, $retval));
440 function showSuccess() {
443 $wgOut->setPagetitle( wfMsg( 'blockip' ) );
444 $wgOut->setSubtitle( wfMsg( 'blockipsuccesssub' ) );
445 $text = wfMsgExt( 'blockipsuccesstext', array( 'parse' ), $this->BlockAddress
);
446 $wgOut->addHtml( $text );
449 function showLogFragment( $out, $title ) {
450 $out->addHtml( Xml
::element( 'h2', NULL, LogPage
::logName( 'block' ) ) );
451 LogEventsList
::showLogExtract( $out, 'block', $title->getPrefixedText() );
455 * Return a comma-delimited list of "flags" to be passed to the log
456 * reader for this block, to provide more information in the logs
460 private function blockLogFlags() {
462 if( $this->BlockAnonOnly
&& IP
::isIPAddress( $this->BlockAddress
) )
463 // when blocking a user the option 'anononly' is not available/has no effect -> do not write this into log
464 $flags[] = 'anononly';
465 if( $this->BlockCreateAccount
)
466 $flags[] = 'nocreate';
467 if( !$this->BlockEnableAutoblock
)
468 $flags[] = 'noautoblock';
469 if ( $this->BlockEmail
)
470 $flags[] = 'noemail';
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' ) );