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 # Re-check user's rights to hide names, very serious, defaults to 0
66 $this->BlockHideName
= ( $wgRequest->getBool( 'wpHideName', 0 ) && $wgUser->isAllowed( 'hideuser' ) ) ?
1 : 0;
69 function showForm( $err ) {
70 global $wgOut, $wgUser, $wgSysopUserBans, $wgContLang;
72 $wgOut->setPagetitle( wfMsg( 'blockip' ) );
73 $wgOut->addWikiMsg( 'blockiptext' );
75 if($wgSysopUserBans) {
76 $mIpaddress = Xml
::label( wfMsg( 'ipadressorusername' ), 'mw-bi-target' );
78 $mIpaddress = Xml
::label( wfMsg( 'ipadress' ), 'mw-bi-target' );
80 $mIpbexpiry = Xml
::label( wfMsg( 'ipbexpiry' ), 'wpBlockExpiry' );
81 $mIpbother = Xml
::label( wfMsg( 'ipbother' ), 'mw-bi-other' );
82 $mIpbothertime = wfMsgHtml( 'ipbotheroption' );
83 $mIpbreasonother = Xml
::label( wfMsg( 'ipbreason' ), 'wpBlockReasonList' );
84 $mIpbreason = Xml
::label( wfMsg( 'ipbotherreason' ), 'mw-bi-reason' );
86 $titleObj = SpecialPage
::getTitleFor( 'Blockip' );
87 $action = $titleObj->escapeLocalURL( "action=submit" );
88 $alignRight = $wgContLang->isRtl() ?
'left' : 'right';
91 $wgOut->setSubtitle( wfMsgHtml( 'formerror' ) );
92 $wgOut->addHTML( "<p class='error'>{$err}</p>\n" );
95 $scBlockExpiryOptions = wfMsgForContent( 'ipboptions' );
97 $showblockoptions = $scBlockExpiryOptions != '-';
98 if (!$showblockoptions)
99 $mIpbother = $mIpbexpiry;
101 $blockExpiryFormOptions = "<option value=\"other\">$mIpbothertime</option>";
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);
108 if ($this->BlockExpiry
=== $value)
109 $selected = ' selected="selected"';
110 $blockExpiryFormOptions .= "<option value=\"$value\"$selected>$show</option>";
113 $reasonDropDown = Xml
::listDropDown( 'wpBlockReasonList',
114 wfMsgForContent( 'ipbreason-dropdown' ),
115 wfMsgForContent( 'ipbreasonotherlist' ), '', 'wpBlockDropDown', 4 );
117 $token = $wgUser->editToken();
119 global $wgStylePath, $wgStyleVersion;
121 <script type=\"text/javascript\" src=\"$wgStylePath/common/block.js?$wgStyleVersion\">
123 <form id=\"blockip\" method=\"post\" action=\"{$action}\">
126 <td align=\"$alignRight\">{$mIpaddress}</td>
128 " . Xml
::input( 'wpBlockAddress', 45, $this->BlockAddress
,
131 'id' => 'mw-bi-target',
132 'onchange' => 'updateBlockOptions()' ) ) . "
136 if ($showblockoptions) {
138 <td align=\"$alignRight\">{$mIpbexpiry}</td>
140 <select tabindex='2' id='wpBlockExpiry' name=\"wpBlockExpiry\" onchange=\"considerChangingExpiryFocus()\">
141 $blockExpiryFormOptions
148 <tr id='wpBlockOther'>
149 <td align=\"$alignRight\">{$mIpbother}</td>
151 " . Xml
::input( 'wpBlockOther', 45, $this->BlockOther
,
152 array( 'tabindex' => '3', 'id' => 'mw-bi-other' ) ) . "
157 <td align=\"$alignRight\">{$mIpbreasonother}</td>
163 <tr id=\"wpBlockReason\">
164 <td align=\"$alignRight\">{$mIpbreason}</td>
166 " . Xml
::input( 'wpBlockReason', 45, $this->BlockReason
,
167 array( 'tabindex' => '5', 'id' => 'mw-bi-reason',
168 'maxlength'=> '200' ) ) . "
171 <tr id='wpAnonOnlyRow'>
174 " . wfCheckLabel( wfMsgHtml( 'ipbanononly' ),
175 'wpAnonOnly', 'wpAnonOnly', $this->BlockAnonOnly
,
176 array( 'tabindex' => '6' ) ) . "
179 <tr id='wpCreateAccountRow'>
182 " . wfCheckLabel( wfMsgHtml( 'ipbcreateaccount' ),
183 'wpCreateAccount', 'wpCreateAccount', $this->BlockCreateAccount
,
184 array( 'tabindex' => '7' ) ) . "
187 <tr id='wpEnableAutoblockRow'>
190 " . wfCheckLabel( wfMsgHtml( 'ipbenableautoblock' ),
191 'wpEnableAutoblock', 'wpEnableAutoblock', $this->BlockEnableAutoblock
,
192 array( 'tabindex' => '8' ) ) . "
197 global $wgSysopEmailBans;
198 if ( $wgSysopEmailBans && $wgUser->isAllowed( 'blockemail' ) ) {
200 <tr id='wpEnableEmailBan'>
203 " . wfCheckLabel( wfMsgHtml( 'ipbemailban' ),
204 'wpEmailBan', 'wpEmailBan', $this->BlockEmail
,
205 array( 'tabindex' => '10' )) . "
211 // Allow some users to hide name from block log, blocklist and listusers
212 if ( $wgUser->isAllowed( 'hideuser' ) ) {
214 <tr id='wpEnableHideUser'>
217 " . wfCheckLabel( wfMsgHtml( 'ipbhidename' ),
218 'wpHideName', 'wpHideName', $this->BlockHideName
,
219 array( 'tabindex' => '9' ) ) . "
227 <td style='padding-top: 1em'> </td>
228 <td style='padding-top: 1em'>
229 " . Xml
::submitButton( wfMsg( 'ipbsubmit' ),
230 array( 'name' => 'wpBlock', 'tabindex' => '11' ) ) . "
234 Xml
::hidden( 'wpEditToken', $token ) .
236 <script type=\"text/javascript\">updateBlockOptions()</script>
239 $wgOut->addHtml( $this->getConvenienceLinks() );
241 $user = User
::newFromName( $this->BlockAddress
);
242 if( is_object( $user ) ) {
243 $this->showLogFragment( $wgOut, $user->getUserPage() );
244 } elseif( preg_match( '/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/', $this->BlockAddress
) ) {
245 $this->showLogFragment( $wgOut, Title
::makeTitle( NS_USER
, $this->BlockAddress
) );
246 } 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
) ) {
247 $this->showLogFragment( $wgOut, Title
::makeTitle( NS_USER
, $this->BlockAddress
) );
252 * Backend block code.
253 * $userID and $expiry will be filled accordingly
254 * @return array(message key, arguments) on failure, empty array on success
256 function doBlock(&$userId = null, &$expiry = null)
258 global $wgUser, $wgSysopUserBans, $wgSysopRangeBans;
261 # Expand valid IPv6 addresses, usernames are left as is
262 $this->BlockAddress
= IP
::sanitizeIP( $this->BlockAddress
);
263 # isIPv4() and IPv6() are used for final validation
264 $rxIP4 = '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}';
265 $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}';
266 $rxIP = "($rxIP4|$rxIP6)";
268 # Check for invalid specifications
269 if ( !preg_match( "/^$rxIP$/", $this->BlockAddress
) ) {
271 if ( preg_match( "/^($rxIP4)\\/(\\d{1,2})$/", $this->BlockAddress
, $matches ) ) {
273 if ( $wgSysopRangeBans ) {
274 if ( !IP
::isIPv4( $this->BlockAddress
) ||
$matches[2] < 16 ||
$matches[2] > 32 ) {
275 return array('ip_range_invalid');
277 $this->BlockAddress
= Block
::normaliseRange( $this->BlockAddress
);
279 # Range block illegal
280 return array('range_block_disabled');
282 } else if ( preg_match( "/^($rxIP6)\\/(\\d{1,3})$/", $this->BlockAddress
, $matches ) ) {
284 if ( $wgSysopRangeBans ) {
285 if ( !IP
::isIPv6( $this->BlockAddress
) ||
$matches[2] < 64 ||
$matches[2] > 128 ) {
286 return array('ip_range_invalid');
288 $this->BlockAddress
= Block
::normaliseRange( $this->BlockAddress
);
290 # Range block illegal
291 return array('range_block_disabled');
295 if ( $wgSysopUserBans ) {
296 $user = User
::newFromName( $this->BlockAddress
);
297 if( !is_null( $user ) && $user->getID() ) {
299 $userId = $user->getID();
300 $this->BlockAddress
= $user->getName();
302 return array('nosuchusershort', htmlspecialchars( $user ?
$user->getName() : $this->BlockAddress
) );
305 return array('badipaddress');
310 $reasonstr = $this->BlockReasonList
;
311 if ( $reasonstr != 'other' && $this->BlockReason
!= '') {
312 // Entry from drop down menu + additional comment
313 $reasonstr .= ': ' . $this->BlockReason
;
314 } elseif ( $reasonstr == 'other' ) {
315 $reasonstr = $this->BlockReason
;
318 $expirestr = $this->BlockExpiry
;
319 if( $expirestr == 'other' )
320 $expirestr = $this->BlockOther
;
322 if (strlen($expirestr) == 0) {
323 return array('ipb_expiry_invalid');
326 if ( $expirestr == 'infinite' ||
$expirestr == 'indefinite' ) {
327 $expiry = Block
::infinity();
329 # Convert GNU-style date, on error returns -1 for PHP <5.1 and false for PHP >=5.1
330 $expiry = strtotime( $expirestr );
332 if ( $expiry < 0 ||
$expiry === false ) {
333 return array('ipb_expiry_invalid');
336 $expiry = wfTimestamp( TS_MW
, $expiry );
340 # Note: for a user block, ipb_address is only for display purposes
341 $block = new Block( $this->BlockAddress
, $userId, $wgUser->getID(),
342 $reasonstr, wfTimestampNow(), 0, $expiry, $this->BlockAnonOnly
,
343 $this->BlockCreateAccount
, $this->BlockEnableAutoblock
, $this->BlockHideName
,
346 if (wfRunHooks('BlockIp', array(&$block, &$wgUser))) {
348 if ( !$block->insert() ) {
349 return array('ipb_already_blocked', htmlspecialchars($this->BlockAddress
));
352 wfRunHooks('BlockIpComplete', array($block, $wgUser));
354 # Prepare log parameters
355 $logParams = array();
356 $logParams[] = $expirestr;
357 $logParams[] = $this->blockLogFlags();
359 # Make log entry, if the name is hidden, put it in the oversight log
360 $log_type = ($this->BlockHideName
) ?
'oversight' : 'block';
361 $log = new LogPage( $log_type );
362 $log->addEntry( 'block', Title
::makeTitle( NS_USER
, $this->BlockAddress
),
363 $reasonstr, $logParams );
369 return array('hookaborted');
373 * UI entry point for blocking
374 * Wraps around doBlock()
379 $retval = $this->doBlock();
381 $titleObj = SpecialPage
::getTitleFor( 'Blockip' );
382 $wgOut->redirect( $titleObj->getFullURL( 'action=success&ip=' .
383 urlencode( $this->BlockAddress
) ) );
386 $key = array_shift($retval);
387 $this->showForm(wfMsgReal($key, $retval));
390 function showSuccess() {
393 $wgOut->setPagetitle( wfMsg( 'blockip' ) );
394 $wgOut->setSubtitle( wfMsg( 'blockipsuccesssub' ) );
395 $text = wfMsgExt( 'blockipsuccesstext', array( 'parse' ), $this->BlockAddress
);
396 $wgOut->addHtml( $text );
399 function showLogFragment( $out, $title ) {
400 $out->addHtml( wfElement( 'h2', NULL, LogPage
::logName( 'block' ) ) );
401 $request = new FauxRequest( array( 'page' => $title->getPrefixedText(), 'type' => 'block' ) );
402 $viewer = new LogViewer( new LogReader( $request ) );
403 $viewer->showList( $out );
407 * Return a comma-delimited list of "flags" to be passed to the log
408 * reader for this block, to provide more information in the logs
412 private function blockLogFlags() {
414 if( $this->BlockAnonOnly
&& IP
::isIPAddress( $this->BlockAddress
) )
415 // when blocking a user the option 'anononly' is not available/has no effect -> do not write this into log
416 $flags[] = 'anononly';
417 if( $this->BlockCreateAccount
)
418 $flags[] = 'nocreate';
419 if( !$this->BlockEnableAutoblock
)
420 $flags[] = 'noautoblock';
421 if ( $this->BlockEmail
)
422 $flags[] = 'noemail';
423 return implode( ',', $flags );
427 * Builds unblock and block list links
431 private function getConvenienceLinks() {
433 $skin = $wgUser->getSkin();
434 $links[] = $skin->makeLink ( 'MediaWiki:ipbreason-dropdown', wfMsgHtml( 'ipb-edit-dropdown' ) );
435 $links[] = $this->getUnblockLink( $skin );
436 $links[] = $this->getBlockListLink( $skin );
437 return '<p class="mw-ipb-conveniencelinks">' . implode( ' | ', $links ) . '</p>';
441 * Build a convenient link to unblock the given username or IP
442 * address, if available; otherwise link to a blank unblock
445 * @param $skin Skin to use
448 private function getUnblockLink( $skin ) {
449 $list = SpecialPage
::getTitleFor( 'Ipblocklist' );
450 if( $this->BlockAddress
) {
451 $addr = htmlspecialchars( strtr( $this->BlockAddress
, '_', ' ' ) );
452 return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-unblock-addr', $addr ),
453 'action=unblock&ip=' . urlencode( $this->BlockAddress
) );
455 return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-unblock' ), 'action=unblock' );
460 * Build a convenience link to the block list
462 * @param $skin Skin to use
465 private function getBlockListLink( $skin ) {
466 $list = SpecialPage
::getTitleFor( 'Ipblocklist' );
467 if( $this->BlockAddress
) {
468 $addr = htmlspecialchars( strtr( $this->BlockAddress
, '_', ' ' ) );
469 return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-blocklist-addr', $addr ),
470 'ip=' . urlencode( $this->BlockAddress
) );
472 return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-blocklist' ) );