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( 'ipaddress' ), '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}\">" .
124 Xml
::openElement( 'fieldset' ) .
125 Xml
::element( 'legend', null, wfMsg( 'blockip-legend' ) ) .
126 Xml
::openElement( 'table', array ( 'border' => '0', 'id' => 'mw-blockip-table' ) ) .
128 <td align=\"$alignRight\">{$mIpaddress}</td>
130 Xml
::input( 'wpBlockAddress', 45, $this->BlockAddress
,
133 'id' => 'mw-bi-target',
134 'onchange' => 'updateBlockOptions()' ) ). "
139 if ( $showblockoptions ) {
141 <td align=\"$alignRight\">{$mIpbexpiry}</td>
143 <select tabindex='2' id='wpBlockExpiry' name=\"wpBlockExpiry\" onchange=\"considerChangingExpiryFocus()\">
144 $blockExpiryFormOptions
151 <tr id='wpBlockOther'>
152 <td align=\"$alignRight\">{$mIpbother}</td>
154 Xml
::input( 'wpBlockOther', 45, $this->BlockOther
,
155 array( 'tabindex' => '3', 'id' => 'mw-bi-other' ) ) . "
159 <td align=\"$alignRight\">{$mIpbreasonother}</td>
164 <tr id=\"wpBlockReason\">
165 <td align=\"$alignRight\">{$mIpbreason}</td>
167 Xml
::input( 'wpBlockReason', 45, $this->BlockReason
,
168 array( 'tabindex' => '5', 'id' => 'mw-bi-reason', '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' ) ) . "
233 Xml
::closeElement( 'table' ) .
234 Xml
::hidden( 'wpEditToken', $token ) .
235 Xml
::closeElement( 'fieldset' ) .
237 <script type=\"text/javascript\">updateBlockOptions()</script>
241 $wgOut->addHtml( $this->getConvenienceLinks() );
243 $user = User
::newFromName( $this->BlockAddress
);
244 if( is_object( $user ) ) {
245 $this->showLogFragment( $wgOut, $user->getUserPage() );
246 } elseif( preg_match( '/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/', $this->BlockAddress
) ) {
247 $this->showLogFragment( $wgOut, Title
::makeTitle( NS_USER
, $this->BlockAddress
) );
248 } 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
) ) {
249 $this->showLogFragment( $wgOut, Title
::makeTitle( NS_USER
, $this->BlockAddress
) );
254 * Backend block code.
255 * $userID and $expiry will be filled accordingly
256 * @return array(message key, arguments) on failure, empty array on success
258 function doBlock(&$userId = null, &$expiry = null)
260 global $wgUser, $wgSysopUserBans, $wgSysopRangeBans;
263 # Expand valid IPv6 addresses, usernames are left as is
264 $this->BlockAddress
= IP
::sanitizeIP( $this->BlockAddress
);
265 # isIPv4() and IPv6() are used for final validation
266 $rxIP4 = '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}';
267 $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}';
268 $rxIP = "($rxIP4|$rxIP6)";
270 # Check for invalid specifications
271 if ( !preg_match( "/^$rxIP$/", $this->BlockAddress
) ) {
273 if ( preg_match( "/^($rxIP4)\\/(\\d{1,2})$/", $this->BlockAddress
, $matches ) ) {
275 if ( $wgSysopRangeBans ) {
276 if ( !IP
::isIPv4( $this->BlockAddress
) ||
$matches[2] < 16 ||
$matches[2] > 32 ) {
277 return array('ip_range_invalid');
279 $this->BlockAddress
= Block
::normaliseRange( $this->BlockAddress
);
281 # Range block illegal
282 return array('range_block_disabled');
284 } else if ( preg_match( "/^($rxIP6)\\/(\\d{1,3})$/", $this->BlockAddress
, $matches ) ) {
286 if ( $wgSysopRangeBans ) {
287 if ( !IP
::isIPv6( $this->BlockAddress
) ||
$matches[2] < 64 ||
$matches[2] > 128 ) {
288 return array('ip_range_invalid');
290 $this->BlockAddress
= Block
::normaliseRange( $this->BlockAddress
);
292 # Range block illegal
293 return array('range_block_disabled');
297 if ( $wgSysopUserBans ) {
298 $user = User
::newFromName( $this->BlockAddress
);
299 if( !is_null( $user ) && $user->getID() ) {
301 $userId = $user->getID();
302 $this->BlockAddress
= $user->getName();
304 return array('nosuchusershort', htmlspecialchars( $user ?
$user->getName() : $this->BlockAddress
) );
307 return array('badipaddress');
312 $reasonstr = $this->BlockReasonList
;
313 if ( $reasonstr != 'other' && $this->BlockReason
!= '') {
314 // Entry from drop down menu + additional comment
315 $reasonstr .= ': ' . $this->BlockReason
;
316 } elseif ( $reasonstr == 'other' ) {
317 $reasonstr = $this->BlockReason
;
320 $expirestr = $this->BlockExpiry
;
321 if( $expirestr == 'other' )
322 $expirestr = $this->BlockOther
;
324 if (strlen($expirestr) == 0) {
325 return array('ipb_expiry_invalid');
328 if ( $expirestr == 'infinite' ||
$expirestr == 'indefinite' ) {
329 $expiry = Block
::infinity();
331 # Convert GNU-style date, on error returns -1 for PHP <5.1 and false for PHP >=5.1
332 $expiry = strtotime( $expirestr );
334 if ( $expiry < 0 ||
$expiry === false ) {
335 return array('ipb_expiry_invalid');
338 $expiry = wfTimestamp( TS_MW
, $expiry );
342 # Note: for a user block, ipb_address is only for display purposes
343 $block = new Block( $this->BlockAddress
, $userId, $wgUser->getID(),
344 $reasonstr, wfTimestampNow(), 0, $expiry, $this->BlockAnonOnly
,
345 $this->BlockCreateAccount
, $this->BlockEnableAutoblock
, $this->BlockHideName
,
348 if (wfRunHooks('BlockIp', array(&$block, &$wgUser))) {
350 if ( !$block->insert() ) {
351 return array('ipb_already_blocked', htmlspecialchars($this->BlockAddress
));
354 wfRunHooks('BlockIpComplete', array($block, $wgUser));
356 # Prepare log parameters
357 $logParams = array();
358 $logParams[] = $expirestr;
359 $logParams[] = $this->blockLogFlags();
361 # Make log entry, if the name is hidden, put it in the oversight log
362 $log_type = ($this->BlockHideName
) ?
'oversight' : 'block';
363 $log = new LogPage( $log_type );
364 $log->addEntry( 'block', Title
::makeTitle( NS_USER
, $this->BlockAddress
),
365 $reasonstr, $logParams );
371 return array('hookaborted');
375 * UI entry point for blocking
376 * Wraps around doBlock()
381 $retval = $this->doBlock();
383 $titleObj = SpecialPage
::getTitleFor( 'Blockip' );
384 $wgOut->redirect( $titleObj->getFullURL( 'action=success&ip=' .
385 urlencode( $this->BlockAddress
) ) );
388 $key = array_shift($retval);
389 $this->showForm(wfMsgReal($key, $retval));
392 function showSuccess() {
395 $wgOut->setPagetitle( wfMsg( 'blockip' ) );
396 $wgOut->setSubtitle( wfMsg( 'blockipsuccesssub' ) );
397 $text = wfMsgExt( 'blockipsuccesstext', array( 'parse' ), $this->BlockAddress
);
398 $wgOut->addHtml( $text );
401 function showLogFragment( $out, $title ) {
402 $out->addHtml( Xml
::element( 'h2', NULL, LogPage
::logName( 'block' ) ) );
403 $request = new FauxRequest( array( 'page' => $title->getPrefixedText(), 'type' => 'block' ) );
404 $viewer = new LogViewer( new LogReader( $request ) );
405 $viewer->showList( $out );
409 * Return a comma-delimited list of "flags" to be passed to the log
410 * reader for this block, to provide more information in the logs
414 private function blockLogFlags() {
416 if( $this->BlockAnonOnly
&& IP
::isIPAddress( $this->BlockAddress
) )
417 // when blocking a user the option 'anononly' is not available/has no effect -> do not write this into log
418 $flags[] = 'anononly';
419 if( $this->BlockCreateAccount
)
420 $flags[] = 'nocreate';
421 if( !$this->BlockEnableAutoblock
)
422 $flags[] = 'noautoblock';
423 if ( $this->BlockEmail
)
424 $flags[] = 'noemail';
425 return implode( ',', $flags );
429 * Builds unblock and block list links
433 private function getConvenienceLinks() {
435 $skin = $wgUser->getSkin();
436 $links[] = $skin->makeLink ( 'MediaWiki:Ipbreason-dropdown', wfMsgHtml( 'ipb-edit-dropdown' ) );
437 $links[] = $this->getUnblockLink( $skin );
438 $links[] = $this->getBlockListLink( $skin );
439 return '<p class="mw-ipb-conveniencelinks">' . implode( ' | ', $links ) . '</p>';
443 * Build a convenient link to unblock the given username or IP
444 * address, if available; otherwise link to a blank unblock
447 * @param $skin Skin to use
450 private function getUnblockLink( $skin ) {
451 $list = SpecialPage
::getTitleFor( 'Ipblocklist' );
452 if( $this->BlockAddress
) {
453 $addr = htmlspecialchars( strtr( $this->BlockAddress
, '_', ' ' ) );
454 return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-unblock-addr', $addr ),
455 'action=unblock&ip=' . urlencode( $this->BlockAddress
) );
457 return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-unblock' ), 'action=unblock' );
462 * Build a convenience link to the block list
464 * @param $skin Skin to use
467 private function getBlockListLink( $skin ) {
468 $list = SpecialPage
::getTitleFor( 'Ipblocklist' );
469 if( $this->BlockAddress
) {
470 $addr = htmlspecialchars( strtr( $this->BlockAddress
, '_', ' ' ) );
471 return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-blocklist-addr', $addr ),
472 'ip=' . urlencode( $this->BlockAddress
) );
474 return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-blocklist' ) );