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 ( $expirestr == 'infinite' ||
$expirestr == 'indefinite' ) {
347 $expiry = Block
::infinity();
349 # Convert GNU-style date, on error returns -1 for PHP <5.1 and false for PHP >=5.1
350 $expiry = strtotime( $expirestr );
352 if ( $expiry < 0 ||
$expiry === false ) {
353 return array('ipb_expiry_invalid');
356 $expiry = wfTimestamp( TS_MW
, $expiry );
360 # Note: for a user block, ipb_address is only for display purposes
361 $block = new Block( $this->BlockAddress
, $userId, $wgUser->getID(),
362 $reasonstr, wfTimestampNow(), 0, $expiry, $this->BlockAnonOnly
,
363 $this->BlockCreateAccount
, $this->BlockEnableAutoblock
, $this->BlockHideName
,
366 if ( $this->BlockWatchUser
) {
367 $wgUser->addWatch ( Title
::makeTitle( NS_USER
, $this->BlockAddress
) );
370 if (wfRunHooks('BlockIp', array(&$block, &$wgUser))) {
372 if ( !$block->insert() ) {
373 return array('ipb_already_blocked', htmlspecialchars($this->BlockAddress
));
376 wfRunHooks('BlockIpComplete', array($block, $wgUser));
378 # Prepare log parameters
379 $logParams = array();
380 $logParams[] = $expirestr;
381 $logParams[] = $this->blockLogFlags();
383 # Make log entry, if the name is hidden, put it in the oversight log
384 $log_type = ($this->BlockHideName
) ?
'suppress' : 'block';
385 $log = new LogPage( $log_type );
386 $log->addEntry( 'block', Title
::makeTitle( NS_USER
, $this->BlockAddress
),
387 $reasonstr, $logParams );
393 return array('hookaborted');
397 * UI entry point for blocking
398 * Wraps around doBlock()
403 $retval = $this->doBlock();
405 $titleObj = SpecialPage
::getTitleFor( 'Blockip' );
406 $wgOut->redirect( $titleObj->getFullURL( 'action=success&ip=' .
407 urlencode( $this->BlockAddress
) ) );
410 $key = array_shift($retval);
411 $this->showForm(wfMsgReal($key, $retval));
414 function showSuccess() {
417 $wgOut->setPagetitle( wfMsg( 'blockip' ) );
418 $wgOut->setSubtitle( wfMsg( 'blockipsuccesssub' ) );
419 $text = wfMsgExt( 'blockipsuccesstext', array( 'parse' ), $this->BlockAddress
);
420 $wgOut->addHtml( $text );
423 function showLogFragment( $out, $title ) {
424 $out->addHtml( Xml
::element( 'h2', NULL, LogPage
::logName( 'block' ) ) );
425 LogEventsList
::showLogExtract( $out, 'block', $title->getPrefixedText() );
429 * Return a comma-delimited list of "flags" to be passed to the log
430 * reader for this block, to provide more information in the logs
434 private function blockLogFlags() {
436 if( $this->BlockAnonOnly
&& IP
::isIPAddress( $this->BlockAddress
) )
437 // when blocking a user the option 'anononly' is not available/has no effect -> do not write this into log
438 $flags[] = 'anononly';
439 if( $this->BlockCreateAccount
)
440 $flags[] = 'nocreate';
441 if( !$this->BlockEnableAutoblock
)
442 $flags[] = 'noautoblock';
443 if ( $this->BlockEmail
)
444 $flags[] = 'noemail';
445 return implode( ',', $flags );
449 * Builds unblock and block list links
453 private function getConvenienceLinks() {
455 $skin = $wgUser->getSkin();
456 $links[] = $skin->makeLink ( 'MediaWiki:Ipbreason-dropdown', wfMsgHtml( 'ipb-edit-dropdown' ) );
457 $links[] = $this->getUnblockLink( $skin );
458 $links[] = $this->getBlockListLink( $skin );
459 return '<p class="mw-ipb-conveniencelinks">' . implode( ' | ', $links ) . '</p>';
463 * Build a convenient link to unblock the given username or IP
464 * address, if available; otherwise link to a blank unblock
467 * @param $skin Skin to use
470 private function getUnblockLink( $skin ) {
471 $list = SpecialPage
::getTitleFor( 'Ipblocklist' );
472 if( $this->BlockAddress
) {
473 $addr = htmlspecialchars( strtr( $this->BlockAddress
, '_', ' ' ) );
474 return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-unblock-addr', $addr ),
475 'action=unblock&ip=' . urlencode( $this->BlockAddress
) );
477 return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-unblock' ), 'action=unblock' );
482 * Build a convenience link to the block list
484 * @param $skin Skin to use
487 private function getBlockListLink( $skin ) {
488 $list = SpecialPage
::getTitleFor( 'Ipblocklist' );
489 if( $this->BlockAddress
) {
490 $addr = htmlspecialchars( strtr( $this->BlockAddress
, '_', ' ' ) );
491 return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-blocklist-addr', $addr ),
492 'ip=' . urlencode( $this->BlockAddress
) );
494 return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-blocklist' ) );