3 * Constructor for Special:Blockip page
5 * @addtogroup SpecialPage
11 function wfSpecialBlockip( $par ) {
12 global $wgUser, $wgOut, $wgRequest;
14 # Can't block when the database is locked
16 $wgOut->readOnlyPage();
21 if( !$wgUser->isAllowed( 'block' ) ) {
22 $wgOut->permissionRequired( 'block' );
26 $ipb = new IPBlockForm( $par );
28 $action = $wgRequest->getVal( 'action' );
29 if ( 'success' == $action ) {
31 } else if ( $wgRequest->wasPosted() && 'submit' == $action &&
32 $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ) ) ) {
40 * Form object for the Special:Blockip page.
42 * @addtogroup SpecialPage
45 var $BlockAddress, $BlockExpiry, $BlockReason;
48 function IPBlockForm( $par ) {
49 global $wgRequest, $wgUser;
51 $this->BlockAddress
= $wgRequest->getVal( 'wpBlockAddress', $wgRequest->getVal( 'ip', $par ) );
52 $this->BlockAddress
= strtr( $this->BlockAddress
, '_', ' ' );
53 $this->BlockReason
= $wgRequest->getText( 'wpBlockReason' );
54 $this->BlockReasonList
= $wgRequest->getText( 'wpBlockReasonList' );
55 $this->BlockExpiry
= $wgRequest->getVal( 'wpBlockExpiry', wfMsg('ipbotheroption') );
56 $this->BlockOther
= $wgRequest->getVal( 'wpBlockOther', '' );
58 # Unchecked checkboxes are not included in the form data at all, so having one
59 # that is true by default is a bit tricky
60 $byDefault = !$wgRequest->wasPosted();
61 $this->BlockAnonOnly
= $wgRequest->getBool( 'wpAnonOnly', $byDefault );
62 $this->BlockCreateAccount
= $wgRequest->getBool( 'wpCreateAccount', $byDefault );
63 $this->BlockEnableAutoblock
= $wgRequest->getBool( 'wpEnableAutoblock', $byDefault );
64 $this->BlockEmail
= $wgRequest->getBool( 'wpEmailBan', false );
65 $this->BlockWatchUser
= $wgRequest->getBool( 'wpWatchUser', false );
66 # Re-check user's rights to hide names, very serious, defaults to 0
67 $this->BlockHideName
= ( $wgRequest->getBool( 'wpHideName', 0 ) && $wgUser->isAllowed( 'hideuser' ) ) ?
1 : 0;
70 function showForm( $err ) {
71 global $wgOut, $wgUser, $wgSysopUserBans;
73 $wgOut->setPagetitle( wfMsg( 'blockip' ) );
74 $wgOut->addWikiMsg( 'blockiptext' );
76 if($wgSysopUserBans) {
77 $mIpaddress = Xml
::label( wfMsg( 'ipadressorusername' ), 'mw-bi-target' );
79 $mIpaddress = Xml
::label( wfMsg( 'ipaddress' ), 'mw-bi-target' );
81 $mIpbexpiry = Xml
::label( wfMsg( 'ipbexpiry' ), 'wpBlockExpiry' );
82 $mIpbother = Xml
::label( wfMsg( 'ipbother' ), 'mw-bi-other' );
83 $mIpbreasonother = Xml
::label( wfMsg( 'ipbreason' ), 'wpBlockReasonList' );
84 $mIpbreason = Xml
::label( wfMsg( 'ipbotherreason' ), 'mw-bi-reason' );
86 $titleObj = SpecialPage
::getTitleFor( 'Blockip' );
89 $wgOut->setSubtitle( wfMsgHtml( 'formerror' ) );
90 $wgOut->addHTML( Xml
::tags( 'p', array( 'class' => 'error' ), $err ) );
93 $scBlockExpiryOptions = wfMsgForContent( 'ipboptions' );
95 $showblockoptions = $scBlockExpiryOptions != '-';
96 if (!$showblockoptions)
97 $mIpbother = $mIpbexpiry;
99 $blockExpiryFormOptions = Xml
::option( wfMsg( 'ipbotheroption' ), 'other' );
100 foreach (explode(',', $scBlockExpiryOptions) as $option) {
101 if ( strpos($option, ":") === false ) $option = "$option:$option";
102 list($show, $value) = explode(":", $option);
103 $show = htmlspecialchars($show);
104 $value = htmlspecialchars($value);
105 $blockExpiryFormOptions .= Xml
::option( $show, $value, $this->BlockExpiry
=== $value ?
true : false ) . "\n";
108 $reasonDropDown = Xml
::listDropDown( 'wpBlockReasonList',
109 wfMsgForContent( 'ipbreason-dropdown' ),
110 wfMsgForContent( 'ipbreasonotherlist' ), '', 'wpBlockDropDown', 4 );
112 global $wgStylePath, $wgStyleVersion;
114 Xml
::tags( 'script', array( 'type' => 'text/javascript', 'src' => "$wgStylePath/common/block.js?$wgStyleVersion" ), '' ) .
115 Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $titleObj->getLocalURL( "action=submit" ), 'id' => 'blockip' ) ) .
116 Xml
::openElement( 'fieldset' ) .
117 Xml
::element( 'legend', null, wfMsg( 'blockip-legend' ) ) .
118 Xml
::openElement( 'table', array ( 'border' => '0', 'id' => 'mw-blockip-table' ) ) .
120 <td class='mw-label'>
123 <td class='mw-input'>" .
124 Xml
::input( 'wpBlockAddress', 45, $this->BlockAddress
,
127 'id' => 'mw-bi-target',
128 'onchange' => 'updateBlockOptions()' ) ). "
133 if ( $showblockoptions ) {
135 <td class='mw-label'>
138 <td class='mw-input'>" .
141 'id' => 'wpBlockExpiry',
142 'name' => 'wpBlockExpiry',
143 'onchange' => 'considerChangingExpiryFocus()',
145 $blockExpiryFormOptions ) .
151 <tr id='wpBlockOther'>
152 <td class='mw-label'>
155 <td class='mw-input'>" .
156 Xml
::input( 'wpBlockOther', 45, $this->BlockOther
,
157 array( 'tabindex' => '3', 'id' => 'mw-bi-other' ) ) . "
161 <td class='mw-label'>
164 <td class='mw-input'>
168 <tr id=\"wpBlockReason\">
169 <td class='mw-label'>
172 <td class='mw-input'>" .
173 Xml
::input( 'wpBlockReason', 45, $this->BlockReason
,
174 array( 'tabindex' => '5', 'id' => 'mw-bi-reason', 'maxlength'=> '200' ) ) . "
177 <tr id='wpAnonOnlyRow'>
179 <td class='mw-input'>" .
180 Xml
::checkLabel( wfMsg( 'ipbanononly' ),
181 'wpAnonOnly', 'wpAnonOnly', $this->BlockAnonOnly
,
182 array( 'tabindex' => '6' ) ) . "
185 <tr id='wpCreateAccountRow'>
187 <td class='mw-input'>" .
188 Xml
::checkLabel( wfMsg( 'ipbcreateaccount' ),
189 'wpCreateAccount', 'wpCreateAccount', $this->BlockCreateAccount
,
190 array( 'tabindex' => '7' ) ) . "
193 <tr id='wpEnableAutoblockRow'>
195 <td class='mw-input'>" .
196 Xml
::checkLabel( wfMsg( 'ipbenableautoblock' ),
197 'wpEnableAutoblock', 'wpEnableAutoblock', $this->BlockEnableAutoblock
,
198 array( 'tabindex' => '8' ) ) . "
203 global $wgSysopEmailBans;
204 if ( $wgSysopEmailBans && $wgUser->isAllowed( 'blockemail' ) ) {
206 <tr id='wpEnableEmailBan'>
208 <td class='mw-input'>" .
209 Xml
::checkLabel( wfMsg( 'ipbemailban' ),
210 'wpEmailBan', 'wpEmailBan', $this->BlockEmail
,
211 array( 'tabindex' => '9' )) . "
217 // Allow some users to hide name from block log, blocklist and listusers
218 if ( $wgUser->isAllowed( 'hideuser' ) ) {
220 <tr id='wpEnableHideUser'>
222 <td class='mw-input'>" .
223 Xml
::checkLabel( wfMsg( 'ipbhidename' ),
224 'wpHideName', 'wpHideName', $this->BlockHideName
,
225 array( 'tabindex' => '10' ) ) . "
231 # Watchlist their user page?
233 <tr id='wpEnableWatchUser'>
235 <td class='mw-input'>" .
236 Xml
::checkLabel( wfMsg( 'ipbwatchuser' ),
237 'wpWatchUser', 'wpWatchUser', $this->BlockWatchUser
,
238 array( 'tabindex' => '11' ) ) . "
245 <td style='padding-top: 1em'> </td>
246 <td class='mw-submit' style='padding-top: 1em'>" .
247 Xml
::submitButton( wfMsg( 'ipbsubmit' ),
248 array( 'name' => 'wpBlock', 'tabindex' => '12' ) ) . "
251 Xml
::closeElement( 'table' ) .
252 Xml
::hidden( 'wpEditToken', $wgUser->editToken() ) .
253 Xml
::closeElement( 'fieldset' ) .
254 Xml
::closeElement( 'form' ) .
255 Xml
::tags( 'script', array( 'type' => 'text/javascript' ), 'updateBlockOptions()' ) . "\n"
258 $wgOut->addHtml( $this->getConvenienceLinks() );
260 $user = User
::newFromName( $this->BlockAddress
);
261 if( is_object( $user ) ) {
262 $this->showLogFragment( $wgOut, $user->getUserPage() );
263 } elseif( preg_match( '/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/', $this->BlockAddress
) ) {
264 $this->showLogFragment( $wgOut, Title
::makeTitle( NS_USER
, $this->BlockAddress
) );
265 } 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
) ) {
266 $this->showLogFragment( $wgOut, Title
::makeTitle( NS_USER
, $this->BlockAddress
) );
271 * Backend block code.
272 * $userID and $expiry will be filled accordingly
273 * @return array(message key, arguments) on failure, empty array on success
275 function doBlock(&$userId = null, &$expiry = null)
277 global $wgUser, $wgSysopUserBans, $wgSysopRangeBans;
280 # Expand valid IPv6 addresses, usernames are left as is
281 $this->BlockAddress
= IP
::sanitizeIP( $this->BlockAddress
);
282 # isIPv4() and IPv6() are used for final validation
283 $rxIP4 = '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}';
284 $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}';
285 $rxIP = "($rxIP4|$rxIP6)";
287 # Check for invalid specifications
288 if ( !preg_match( "/^$rxIP$/", $this->BlockAddress
) ) {
290 if ( preg_match( "/^($rxIP4)\\/(\\d{1,2})$/", $this->BlockAddress
, $matches ) ) {
292 if ( $wgSysopRangeBans ) {
293 if ( !IP
::isIPv4( $this->BlockAddress
) ||
$matches[2] < 16 ||
$matches[2] > 32 ) {
294 return array('ip_range_invalid');
296 $this->BlockAddress
= Block
::normaliseRange( $this->BlockAddress
);
298 # Range block illegal
299 return array('range_block_disabled');
301 } else if ( preg_match( "/^($rxIP6)\\/(\\d{1,3})$/", $this->BlockAddress
, $matches ) ) {
303 if ( $wgSysopRangeBans ) {
304 if ( !IP
::isIPv6( $this->BlockAddress
) ||
$matches[2] < 64 ||
$matches[2] > 128 ) {
305 return array('ip_range_invalid');
307 $this->BlockAddress
= Block
::normaliseRange( $this->BlockAddress
);
309 # Range block illegal
310 return array('range_block_disabled');
314 if ( $wgSysopUserBans ) {
315 $user = User
::newFromName( $this->BlockAddress
);
316 if( !is_null( $user ) && $user->getID() ) {
318 $userId = $user->getID();
319 $this->BlockAddress
= $user->getName();
321 return array('nosuchusershort', htmlspecialchars( $user ?
$user->getName() : $this->BlockAddress
) );
324 return array('badipaddress');
329 $reasonstr = $this->BlockReasonList
;
330 if ( $reasonstr != 'other' && $this->BlockReason
!= '') {
331 // Entry from drop down menu + additional comment
332 $reasonstr .= ': ' . $this->BlockReason
;
333 } elseif ( $reasonstr == 'other' ) {
334 $reasonstr = $this->BlockReason
;
337 $expirestr = $this->BlockExpiry
;
338 if( $expirestr == 'other' )
339 $expirestr = $this->BlockOther
;
341 if (strlen($expirestr) == 0) {
342 return array('ipb_expiry_invalid');
345 if ( $expirestr == 'infinite' ||
$expirestr == 'indefinite' ) {
346 $expiry = Block
::infinity();
348 # Convert GNU-style date, on error returns -1 for PHP <5.1 and false for PHP >=5.1
349 $expiry = strtotime( $expirestr );
351 if ( $expiry < 0 ||
$expiry === false ) {
352 return array('ipb_expiry_invalid');
355 $expiry = wfTimestamp( TS_MW
, $expiry );
359 # Note: for a user block, ipb_address is only for display purposes
360 $block = new Block( $this->BlockAddress
, $userId, $wgUser->getID(),
361 $reasonstr, wfTimestampNow(), 0, $expiry, $this->BlockAnonOnly
,
362 $this->BlockCreateAccount
, $this->BlockEnableAutoblock
, $this->BlockHideName
,
365 if ( $this->BlockWatchUser
) {
366 $wgUser->addWatch ( Title
::makeTitle( NS_USER
, $this->BlockAddress
) );
369 if (wfRunHooks('BlockIp', array(&$block, &$wgUser))) {
371 if ( !$block->insert() ) {
372 return array('ipb_already_blocked', htmlspecialchars($this->BlockAddress
));
375 wfRunHooks('BlockIpComplete', array($block, $wgUser));
377 # Prepare log parameters
378 $logParams = array();
379 $logParams[] = $expirestr;
380 $logParams[] = $this->blockLogFlags();
382 # Make log entry, if the name is hidden, put it in the oversight log
383 $log_type = ($this->BlockHideName
) ?
'suppress' : 'block';
384 $log = new LogPage( $log_type );
385 $log->addEntry( 'block', Title
::makeTitle( NS_USER
, $this->BlockAddress
),
386 $reasonstr, $logParams );
392 return array('hookaborted');
396 * UI entry point for blocking
397 * Wraps around doBlock()
402 $retval = $this->doBlock();
404 $titleObj = SpecialPage
::getTitleFor( 'Blockip' );
405 $wgOut->redirect( $titleObj->getFullURL( 'action=success&ip=' .
406 urlencode( $this->BlockAddress
) ) );
409 $key = array_shift($retval);
410 $this->showForm(wfMsgReal($key, $retval));
413 function showSuccess() {
416 $wgOut->setPagetitle( wfMsg( 'blockip' ) );
417 $wgOut->setSubtitle( wfMsg( 'blockipsuccesssub' ) );
418 $text = wfMsgExt( 'blockipsuccesstext', array( 'parse' ), $this->BlockAddress
);
419 $wgOut->addHtml( $text );
422 function showLogFragment( $out, $title ) {
423 $out->addHtml( Xml
::element( 'h2', NULL, LogPage
::logName( 'block' ) ) );
424 LogEventsList
::showLogExtract( $out, 'block', $title->getPrefixedText() );
428 * Return a comma-delimited list of "flags" to be passed to the log
429 * reader for this block, to provide more information in the logs
433 private function blockLogFlags() {
435 if( $this->BlockAnonOnly
&& IP
::isIPAddress( $this->BlockAddress
) )
436 // when blocking a user the option 'anononly' is not available/has no effect -> do not write this into log
437 $flags[] = 'anononly';
438 if( $this->BlockCreateAccount
)
439 $flags[] = 'nocreate';
440 if( !$this->BlockEnableAutoblock
)
441 $flags[] = 'noautoblock';
442 if ( $this->BlockEmail
)
443 $flags[] = 'noemail';
444 return implode( ',', $flags );
448 * Builds unblock and block list links
452 private function getConvenienceLinks() {
454 $skin = $wgUser->getSkin();
455 $links[] = $skin->makeLink ( 'MediaWiki:Ipbreason-dropdown', wfMsgHtml( 'ipb-edit-dropdown' ) );
456 $links[] = $this->getUnblockLink( $skin );
457 $links[] = $this->getBlockListLink( $skin );
458 return '<p class="mw-ipb-conveniencelinks">' . implode( ' | ', $links ) . '</p>';
462 * Build a convenient link to unblock the given username or IP
463 * address, if available; otherwise link to a blank unblock
466 * @param $skin Skin to use
469 private function getUnblockLink( $skin ) {
470 $list = SpecialPage
::getTitleFor( 'Ipblocklist' );
471 if( $this->BlockAddress
) {
472 $addr = htmlspecialchars( strtr( $this->BlockAddress
, '_', ' ' ) );
473 return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-unblock-addr', $addr ),
474 'action=unblock&ip=' . urlencode( $this->BlockAddress
) );
476 return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-unblock' ), 'action=unblock' );
481 * Build a convenience link to the block list
483 * @param $skin Skin to use
486 private function getBlockListLink( $skin ) {
487 $list = SpecialPage
::getTitleFor( 'Ipblocklist' );
488 if( $this->BlockAddress
) {
489 $addr = htmlspecialchars( strtr( $this->BlockAddress
, '_', ' ' ) );
490 return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-blocklist-addr', $addr ),
491 'ip=' . urlencode( $this->BlockAddress
) );
493 return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-blocklist' ) );