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;
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;
71 function showForm( $err ) {
72 global $wgOut, $wgUser, $wgSysopUserBans;
74 $wgOut->setPagetitle( wfMsg( 'blockip' ) );
75 $wgOut->addWikiMsg( 'blockiptext' );
77 if($wgSysopUserBans) {
78 $mIpaddress = Xml
::label( wfMsg( 'ipadressorusername' ), 'mw-bi-target' );
80 $mIpaddress = Xml
::label( wfMsg( 'ipaddress' ), 'mw-bi-target' );
82 $mIpbexpiry = Xml
::label( wfMsg( 'ipbexpiry' ), 'wpBlockExpiry' );
83 $mIpbother = Xml
::label( wfMsg( 'ipbother' ), 'mw-bi-other' );
84 $mIpbreasonother = Xml
::label( wfMsg( 'ipbreason' ), 'wpBlockReasonList' );
85 $mIpbreason = Xml
::label( wfMsg( 'ipbotherreason' ), 'mw-bi-reason' );
87 $titleObj = SpecialPage
::getTitleFor( 'Blockip' );
90 $wgOut->setSubtitle( wfMsgHtml( 'formerror' ) );
91 $wgOut->addHTML( Xml
::tags( 'p', array( 'class' => 'error' ), $err ) );
94 $scBlockExpiryOptions = wfMsgForContent( 'ipboptions' );
96 $showblockoptions = $scBlockExpiryOptions != '-';
97 if (!$showblockoptions)
98 $mIpbother = $mIpbexpiry;
100 $blockExpiryFormOptions = Xml
::option( wfMsg( 'ipbotheroption' ), 'other' );
101 foreach (explode(',', $scBlockExpiryOptions) as $option) {
102 if ( strpos($option, ":") === false ) $option = "$option:$option";
103 list($show, $value) = explode(":", $option);
104 $show = htmlspecialchars($show);
105 $value = htmlspecialchars($value);
106 $blockExpiryFormOptions .= Xml
::option( $show, $value, $this->BlockExpiry
=== $value ?
true : false ) . "\n";
109 $reasonDropDown = Xml
::listDropDown( 'wpBlockReasonList',
110 wfMsgForContent( 'ipbreason-dropdown' ),
111 wfMsgForContent( 'ipbreasonotherlist' ), '', 'wpBlockDropDown', 4 );
113 global $wgStylePath, $wgStyleVersion;
115 Xml
::tags( 'script', array( 'type' => 'text/javascript', 'src' => "$wgStylePath/common/block.js?$wgStyleVersion" ), '' ) .
116 Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $titleObj->getLocalURL( "action=submit" ), 'id' => 'blockip' ) ) .
117 Xml
::openElement( 'fieldset' ) .
118 Xml
::element( 'legend', null, wfMsg( 'blockip-legend' ) ) .
119 Xml
::openElement( 'table', array ( 'border' => '0', 'id' => 'mw-blockip-table' ) ) .
121 <td class='mw-label'>
124 <td class='mw-input'>" .
125 Xml
::input( 'wpBlockAddress', 45, $this->BlockAddress
,
128 'id' => 'mw-bi-target',
129 'onchange' => 'updateBlockOptions()' ) ). "
134 if ( $showblockoptions ) {
136 <td class='mw-label'>
139 <td class='mw-input'>" .
142 'id' => 'wpBlockExpiry',
143 'name' => 'wpBlockExpiry',
144 'onchange' => 'considerChangingExpiryFocus()',
146 $blockExpiryFormOptions ) .
152 <tr id='wpBlockOther'>
153 <td class='mw-label'>
156 <td class='mw-input'>" .
157 Xml
::input( 'wpBlockOther', 45, $this->BlockOther
,
158 array( 'tabindex' => '3', 'id' => 'mw-bi-other' ) ) . "
162 <td class='mw-label'>
165 <td class='mw-input'>
169 <tr id=\"wpBlockReason\">
170 <td class='mw-label'>
173 <td class='mw-input'>" .
174 Xml
::input( 'wpBlockReason', 45, $this->BlockReason
,
175 array( 'tabindex' => '5', 'id' => 'mw-bi-reason', 'maxlength'=> '200' ) ) . "
178 <tr id='wpAnonOnlyRow'>
180 <td class='mw-input'>" .
181 Xml
::checkLabel( wfMsg( 'ipbanononly' ),
182 'wpAnonOnly', 'wpAnonOnly', $this->BlockAnonOnly
,
183 array( 'tabindex' => '6' ) ) . "
186 <tr id='wpCreateAccountRow'>
188 <td class='mw-input'>" .
189 Xml
::checkLabel( wfMsg( 'ipbcreateaccount' ),
190 'wpCreateAccount', 'wpCreateAccount', $this->BlockCreateAccount
,
191 array( 'tabindex' => '7' ) ) . "
194 <tr id='wpEnableAutoblockRow'>
196 <td class='mw-input'>" .
197 Xml
::checkLabel( wfMsg( 'ipbenableautoblock' ),
198 'wpEnableAutoblock', 'wpEnableAutoblock', $this->BlockEnableAutoblock
,
199 array( 'tabindex' => '8' ) ) . "
204 global $wgSysopEmailBans;
205 if ( $wgSysopEmailBans && $wgUser->isAllowed( 'blockemail' ) ) {
207 <tr id='wpEnableEmailBan'>
209 <td class='mw-input'>" .
210 Xml
::checkLabel( wfMsg( 'ipbemailban' ),
211 'wpEmailBan', 'wpEmailBan', $this->BlockEmail
,
212 array( 'tabindex' => '9' )) . "
218 // Allow some users to hide name from block log, blocklist and listusers
219 if ( $wgUser->isAllowed( 'hideuser' ) ) {
221 <tr id='wpEnableHideUser'>
223 <td class='mw-input'>" .
224 Xml
::checkLabel( wfMsg( 'ipbhidename' ),
225 'wpHideName', 'wpHideName', $this->BlockHideName
,
226 array( 'tabindex' => '10' ) ) . "
232 # Watchlist their user page?
234 <tr id='wpEnableWatchUser'>
236 <td class='mw-input'>" .
237 Xml
::checkLabel( wfMsg( 'ipbwatchuser' ),
238 'wpWatchUser', 'wpWatchUser', $this->BlockWatchUser
,
239 array( 'tabindex' => '11' ) ) . "
246 <td style='padding-top: 1em'> </td>
247 <td class='mw-submit' style='padding-top: 1em'>" .
248 Xml
::submitButton( wfMsg( 'ipbsubmit' ),
249 array( 'name' => 'wpBlock', 'tabindex' => '12' ) ) . "
252 Xml
::closeElement( 'table' ) .
253 Xml
::hidden( 'wpEditToken', $wgUser->editToken() ) .
254 Xml
::closeElement( 'fieldset' ) .
255 Xml
::closeElement( 'form' ) .
256 Xml
::tags( 'script', array( 'type' => 'text/javascript' ), 'updateBlockOptions()' ) . "\n"
259 $wgOut->addHtml( $this->getConvenienceLinks() );
261 $user = User
::newFromName( $this->BlockAddress
);
262 if( is_object( $user ) ) {
263 $this->showLogFragment( $wgOut, $user->getUserPage() );
264 } elseif( preg_match( '/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/', $this->BlockAddress
) ) {
265 $this->showLogFragment( $wgOut, Title
::makeTitle( NS_USER
, $this->BlockAddress
) );
266 } 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
) ) {
267 $this->showLogFragment( $wgOut, Title
::makeTitle( NS_USER
, $this->BlockAddress
) );
272 * Backend block code.
273 * $userID and $expiry will be filled accordingly
274 * @return array(message key, arguments) on failure, empty array on success
276 function doBlock(&$userId = null, &$expiry = null)
278 global $wgUser, $wgSysopUserBans, $wgSysopRangeBans;
281 # Expand valid IPv6 addresses, usernames are left as is
282 $this->BlockAddress
= IP
::sanitizeIP( $this->BlockAddress
);
283 # isIPv4() and IPv6() are used for final validation
284 $rxIP4 = '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}';
285 $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}';
286 $rxIP = "($rxIP4|$rxIP6)";
288 # Check for invalid specifications
289 if ( !preg_match( "/^$rxIP$/", $this->BlockAddress
) ) {
291 if ( preg_match( "/^($rxIP4)\\/(\\d{1,2})$/", $this->BlockAddress
, $matches ) ) {
293 if ( $wgSysopRangeBans ) {
294 if ( !IP
::isIPv4( $this->BlockAddress
) ||
$matches[2] < 16 ||
$matches[2] > 32 ) {
295 return array('ip_range_invalid');
297 $this->BlockAddress
= Block
::normaliseRange( $this->BlockAddress
);
299 # Range block illegal
300 return array('range_block_disabled');
302 } else if ( preg_match( "/^($rxIP6)\\/(\\d{1,3})$/", $this->BlockAddress
, $matches ) ) {
304 if ( $wgSysopRangeBans ) {
305 if ( !IP
::isIPv6( $this->BlockAddress
) ||
$matches[2] < 64 ||
$matches[2] > 128 ) {
306 return array('ip_range_invalid');
308 $this->BlockAddress
= Block
::normaliseRange( $this->BlockAddress
);
310 # Range block illegal
311 return array('range_block_disabled');
315 if ( $wgSysopUserBans ) {
316 $user = User
::newFromName( $this->BlockAddress
);
317 if( !is_null( $user ) && $user->getId() ) {
319 $userId = $user->getId();
320 $this->BlockAddress
= $user->getName();
322 return array('nosuchusershort', htmlspecialchars( $user ?
$user->getName() : $this->BlockAddress
) );
325 return array('badipaddress');
330 $reasonstr = $this->BlockReasonList
;
331 if ( $reasonstr != 'other' && $this->BlockReason
!= '') {
332 // Entry from drop down menu + additional comment
333 $reasonstr .= ': ' . $this->BlockReason
;
334 } elseif ( $reasonstr == 'other' ) {
335 $reasonstr = $this->BlockReason
;
338 $expirestr = $this->BlockExpiry
;
339 if( $expirestr == 'other' )
340 $expirestr = $this->BlockOther
;
342 if (strlen($expirestr) == 0) {
343 return array('ipb_expiry_invalid');
346 if ( false === ($expiry = Block
::parseExpiryInput( $expirestr )) ) {
348 return array('ipb_expiry_invalid');
352 # Note: for a user block, ipb_address is only for display purposes
353 $block = new Block( $this->BlockAddress
, $userId, $wgUser->getId(),
354 $reasonstr, wfTimestampNow(), 0, $expiry, $this->BlockAnonOnly
,
355 $this->BlockCreateAccount
, $this->BlockEnableAutoblock
, $this->BlockHideName
,
358 if (wfRunHooks('BlockIp', array(&$block, &$wgUser))) {
360 if ( !$block->insert() ) {
361 return array('ipb_already_blocked', htmlspecialchars($this->BlockAddress
));
364 wfRunHooks('BlockIpComplete', array($block, $wgUser));
366 if ( $this->BlockWatchUser
) {
367 $wgUser->addWatch ( Title
::makeTitle( NS_USER
, $this->BlockAddress
) );
370 # Prepare log parameters
371 $logParams = array();
372 $logParams[] = $expirestr;
373 $logParams[] = $this->blockLogFlags();
375 # Make log entry, if the name is hidden, put it in the oversight log
376 $log_type = ($this->BlockHideName
) ?
'suppress' : 'block';
377 $log = new LogPage( $log_type );
378 $log->addEntry( 'block', Title
::makeTitle( NS_USER
, $this->BlockAddress
),
379 $reasonstr, $logParams );
385 return array('hookaborted');
389 * UI entry point for blocking
390 * Wraps around doBlock()
395 $retval = $this->doBlock();
397 $titleObj = SpecialPage
::getTitleFor( 'Blockip' );
398 $wgOut->redirect( $titleObj->getFullURL( 'action=success&ip=' .
399 urlencode( $this->BlockAddress
) ) );
402 $key = array_shift($retval);
403 $this->showForm(wfMsgReal($key, $retval));
406 function showSuccess() {
409 $wgOut->setPagetitle( wfMsg( 'blockip' ) );
410 $wgOut->setSubtitle( wfMsg( 'blockipsuccesssub' ) );
411 $text = wfMsgExt( 'blockipsuccesstext', array( 'parse' ), $this->BlockAddress
);
412 $wgOut->addHtml( $text );
415 function showLogFragment( $out, $title ) {
416 $out->addHtml( Xml
::element( 'h2', NULL, LogPage
::logName( 'block' ) ) );
417 LogEventsList
::showLogExtract( $out, 'block', $title->getPrefixedText() );
421 * Return a comma-delimited list of "flags" to be passed to the log
422 * reader for this block, to provide more information in the logs
426 private function blockLogFlags() {
428 if( $this->BlockAnonOnly
&& IP
::isIPAddress( $this->BlockAddress
) )
429 // when blocking a user the option 'anononly' is not available/has no effect -> do not write this into log
430 $flags[] = 'anononly';
431 if( $this->BlockCreateAccount
)
432 $flags[] = 'nocreate';
433 if( !$this->BlockEnableAutoblock
)
434 $flags[] = 'noautoblock';
435 if ( $this->BlockEmail
)
436 $flags[] = 'noemail';
437 return implode( ',', $flags );
441 * Builds unblock and block list links
445 private function getConvenienceLinks() {
447 $skin = $wgUser->getSkin();
448 $links[] = $skin->makeLink ( 'MediaWiki:Ipbreason-dropdown', wfMsgHtml( 'ipb-edit-dropdown' ) );
449 $links[] = $this->getUnblockLink( $skin );
450 $links[] = $this->getBlockListLink( $skin );
451 return '<p class="mw-ipb-conveniencelinks">' . implode( ' | ', $links ) . '</p>';
455 * Build a convenient link to unblock the given username or IP
456 * address, if available; otherwise link to a blank unblock
459 * @param $skin Skin to use
462 private function getUnblockLink( $skin ) {
463 $list = SpecialPage
::getTitleFor( 'Ipblocklist' );
464 if( $this->BlockAddress
) {
465 $addr = htmlspecialchars( strtr( $this->BlockAddress
, '_', ' ' ) );
466 return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-unblock-addr', $addr ),
467 'action=unblock&ip=' . urlencode( $this->BlockAddress
) );
469 return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-unblock' ), 'action=unblock' );
474 * Build a convenience link to the block list
476 * @param $skin Skin to use
479 private function getBlockListLink( $skin ) {
480 $list = SpecialPage
::getTitleFor( 'Ipblocklist' );
481 if( $this->BlockAddress
) {
482 $addr = htmlspecialchars( strtr( $this->BlockAddress
, '_', ' ' ) );
483 return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-blocklist-addr', $addr ),
484 'ip=' . urlencode( $this->BlockAddress
) );
486 return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-blocklist' ) );