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->addWikiText( wfMsg( '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' );
85 $mIpbreasonotherlist = wfMsgHtml( 'ipbreasonotherlist' );
87 $titleObj = SpecialPage
::getTitleFor( 'Blockip' );
88 $action = $titleObj->escapeLocalURL( "action=submit" );
89 $alignRight = $wgContLang->isRtl() ?
'left' : 'right';
92 $wgOut->setSubtitle( wfMsgHtml( 'formerror' ) );
93 $wgOut->addHTML( "<p class='error'>{$err}</p>\n" );
96 $scBlockExpiryOptions = wfMsgForContent( 'ipboptions' );
98 $showblockoptions = $scBlockExpiryOptions != '-';
99 if (!$showblockoptions)
100 $mIpbother = $mIpbexpiry;
102 $blockExpiryFormOptions = "<option value=\"other\">$mIpbothertime</option>";
103 foreach (explode(',', $scBlockExpiryOptions) as $option) {
104 if ( strpos($option, ":") === false ) $option = "$option:$option";
105 list($show, $value) = explode(":", $option);
106 $show = htmlspecialchars($show);
107 $value = htmlspecialchars($value);
109 if ($this->BlockExpiry
=== $value)
110 $selected = ' selected="selected"';
111 $blockExpiryFormOptions .= "<option value=\"$value\"$selected>$show</option>";
114 $scBlockReasonList = wfMsgForContent( 'ipbreason-dropdown' );
115 $blockReasonList = '';
116 if ( $scBlockReasonList != '' && $scBlockReasonList != '-' ) {
117 $blockReasonList = "<option value=\"other\">$mIpbreasonotherlist</option>";
119 foreach ( explode( "\n", $scBlockReasonList ) as $option) {
120 $value = trim( htmlspecialchars($option) );
121 if ( $value == '' ) {
123 } elseif ( substr( $value, 0, 1) == '*' && substr( $value, 1, 1) != '*' ) {
124 // A new group is starting ...
125 $value = trim( substr( $value, 1 ) );
126 $blockReasonList .= "$optgroup<optgroup label=\"$value\">";
127 $optgroup = "</optgroup>";
128 } elseif ( substr( $value, 0, 2) == '**' ) {
131 $value = trim( substr( $value, 2 ) );
132 if ( $this->BlockReasonList
=== $value)
133 $selected = ' selected="selected"';
134 $blockReasonList .= "<option value=\"$value\"$selected>$value</option>";
136 // groupless block reason
138 if ( $this->BlockReasonList
=== $value)
139 $selected = ' selected="selected"';
140 $blockReasonList .= "$optgroup<option value=\"$value\"$selected>$value</option>";
144 $blockReasonList .= $optgroup;
147 $token = htmlspecialchars( $wgUser->editToken() );
149 global $wgStylePath, $wgStyleVersion;
151 <script type=\"text/javascript\" src=\"$wgStylePath/common/block.js?$wgStyleVersion\">
153 <form id=\"blockip\" method=\"post\" action=\"{$action}\">
156 <td align=\"$alignRight\">{$mIpaddress}</td>
158 " . Xml
::input( 'wpBlockAddress', 45, $this->BlockAddress
,
161 'id' => 'mw-bi-target',
162 'onchange' => 'updateBlockOptions()' ) ) . "
166 if ($showblockoptions) {
168 <td align=\"$alignRight\">{$mIpbexpiry}</td>
170 <select tabindex='2' id='wpBlockExpiry' name=\"wpBlockExpiry\" onchange=\"considerChangingExpiryFocus()\">
171 $blockExpiryFormOptions
178 <tr id='wpBlockOther'>
179 <td align=\"$alignRight\">{$mIpbother}</td>
181 " . Xml
::input( 'wpBlockOther', 45, $this->BlockOther
,
182 array( 'tabindex' => '3', 'id' => 'mw-bi-other' ) ) . "
185 if ( $blockReasonList != '' ) {
188 <td align=\"$alignRight\">{$mIpbreasonother}</td>
190 <select tabindex='4' id=\"wpBlockReasonList\" name=\"wpBlockReasonList\">
197 <tr id=\"wpBlockReason\">
198 <td align=\"$alignRight\">{$mIpbreason}</td>
200 " . Xml
::input( 'wpBlockReason', 45, $this->BlockReason
,
201 array( 'tabindex' => '5', 'id' => 'mw-bi-reason',
202 'maxlength'=> '200' ) ) . "
205 <tr id='wpAnonOnlyRow'>
208 " . wfCheckLabel( wfMsgHtml( 'ipbanononly' ),
209 'wpAnonOnly', 'wpAnonOnly', $this->BlockAnonOnly
,
210 array( 'tabindex' => '6' ) ) . "
213 <tr id='wpCreateAccountRow'>
216 " . wfCheckLabel( wfMsgHtml( 'ipbcreateaccount' ),
217 'wpCreateAccount', 'wpCreateAccount', $this->BlockCreateAccount
,
218 array( 'tabindex' => '7' ) ) . "
221 <tr id='wpEnableAutoblockRow'>
224 " . wfCheckLabel( wfMsgHtml( 'ipbenableautoblock' ),
225 'wpEnableAutoblock', 'wpEnableAutoblock', $this->BlockEnableAutoblock
,
226 array( 'tabindex' => '8' ) ) . "
230 // Allow some users to hide name from block log, blocklist and listusers
231 if ( $wgUser->isAllowed( 'hideuser' ) ) {
236 " . wfCheckLabel( wfMsgHtml( 'ipbhidename' ),
237 'wpHideName', 'wpHideName', $this->BlockHideName
,
238 array( 'tabindex' => '9' ) ) . "
244 global $wgSysopEmailBans;
246 if ( $wgSysopEmailBans && $wgUser->isAllowed( 'blockemail' ) ) {
251 " . wfCheckLabel( wfMsgHtml( 'ipbemailban' ),
252 'wpEmailBan', 'wpEmailBan', $this->BlockEmail
,
253 array( 'tabindex' => '10' )) . "
260 <td style='padding-top: 1em'> </td>
261 <td style='padding-top: 1em'>
262 " . Xml
::submitButton( wfMsg( 'ipbsubmit' ),
263 array( 'name' => 'wpBlock', 'tabindex' => '11' ) ) . "
267 Xml
::hidden( 'wpEditToken', $token ) .
269 <script type=\"text/javascript\">updateBlockOptions()</script>
272 $wgOut->addHtml( $this->getConvenienceLinks() );
274 $user = User
::newFromName( $this->BlockAddress
);
275 if( is_object( $user ) ) {
276 $this->showLogFragment( $wgOut, $user->getUserPage() );
277 } elseif( preg_match( '/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/', $this->BlockAddress
) ) {
278 $this->showLogFragment( $wgOut, Title
::makeTitle( NS_USER
, $this->BlockAddress
) );
279 } 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
) ) {
280 $this->showLogFragment( $wgOut, Title
::makeTitle( NS_USER
, $this->BlockAddress
) );
284 function doSubmit() {
285 global $wgOut, $wgUser, $wgSysopUserBans, $wgSysopRangeBans;
288 # Expand valid IPv6 addresses, usernames are left as is
289 $this->BlockAddress
= IP
::sanitizeIP( $this->BlockAddress
);
290 # isIPv4() and IPv6() are used for final validation
291 $rxIP4 = '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}';
292 $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}';
293 $rxIP = "($rxIP4|$rxIP6)";
295 # Check for invalid specifications
296 if ( !preg_match( "/^$rxIP$/", $this->BlockAddress
) ) {
298 if ( preg_match( "/^($rxIP4)\\/(\\d{1,2})$/", $this->BlockAddress
, $matches ) ) {
300 if ( $wgSysopRangeBans ) {
301 if ( !IP
::isIPv4( $this->BlockAddress
) ||
$matches[2] < 16 ||
$matches[2] > 32 ) {
302 $this->showForm( wfMsg( 'ip_range_invalid' ) );
305 $this->BlockAddress
= Block
::normaliseRange( $this->BlockAddress
);
307 # Range block illegal
308 $this->showForm( wfMsg( 'range_block_disabled' ) );
311 } else if ( preg_match( "/^($rxIP6)\\/(\\d{1,3})$/", $this->BlockAddress
, $matches ) ) {
313 if ( $wgSysopRangeBans ) {
314 if ( !IP
::isIPv6( $this->BlockAddress
) ||
$matches[2] < 64 ||
$matches[2] > 128 ) {
315 $this->showForm( wfMsg( 'ip_range_invalid' ) );
318 $this->BlockAddress
= Block
::normaliseRange( $this->BlockAddress
);
320 # Range block illegal
321 $this->showForm( wfMsg( 'range_block_disabled' ) );
326 if ( $wgSysopUserBans ) {
327 $user = User
::newFromName( $this->BlockAddress
);
328 if( !is_null( $user ) && $user->getID() ) {
330 $this->BlockAddress
= $user->getName();
331 $userId = $user->getID();
333 $this->showForm( wfMsg( 'nosuchusershort', htmlspecialchars( $this->BlockAddress
) ) );
337 $this->showForm( wfMsg( 'badipaddress' ) );
343 $reasonstr = $this->BlockReasonList
;
344 if ( $reasonstr != 'other' && $this->BlockReason
!= '') {
345 // Entry from drop down menu + additional comment
346 $reasonstr .= ': ' . $this->BlockReason
;
347 } elseif ( $reasonstr == 'other' ) {
348 $reasonstr = $this->BlockReason
;
351 $expirestr = $this->BlockExpiry
;
352 if( $expirestr == 'other' )
353 $expirestr = $this->BlockOther
;
355 if (strlen($expirestr) == 0) {
356 $this->showForm( wfMsg( 'ipb_expiry_invalid' ) );
360 if ( $expirestr == 'infinite' ||
$expirestr == 'indefinite' ) {
361 $expiry = Block
::infinity();
363 # Convert GNU-style date, on error returns -1 for PHP <5.1 and false for PHP >=5.1
364 $expiry = strtotime( $expirestr );
366 if ( $expiry < 0 ||
$expiry === false ) {
367 $this->showForm( wfMsg( 'ipb_expiry_invalid' ) );
371 $expiry = wfTimestamp( TS_MW
, $expiry );
375 # Note: for a user block, ipb_address is only for display purposes
376 $block = new Block( $this->BlockAddress
, $userId, $wgUser->getID(),
377 $reasonstr, wfTimestampNow(), 0, $expiry, $this->BlockAnonOnly
,
378 $this->BlockCreateAccount
, $this->BlockEnableAutoblock
, $this->BlockHideName
,
381 if (wfRunHooks('BlockIp', array(&$block, &$wgUser))) {
383 if ( !$block->insert() ) {
384 $this->showForm( wfMsg( 'ipb_already_blocked',
385 htmlspecialchars( $this->BlockAddress
) ) );
389 wfRunHooks('BlockIpComplete', array($block, $wgUser));
391 # Prepare log parameters
392 $logParams = array();
393 $logParams[] = $expirestr;
394 $logParams[] = $this->blockLogFlags();
396 # Make log entry, if the name is hidden, put it in the oversight log
397 $log_type = ($this->BlockHideName
) ?
'oversight' : 'block';
398 $log = new LogPage( $log_type );
399 $log->addEntry( 'block', Title
::makeTitle( NS_USER
, $this->BlockAddress
),
400 $reasonstr, $logParams );
403 $titleObj = SpecialPage
::getTitleFor( 'Blockip' );
404 $wgOut->redirect( $titleObj->getFullURL( 'action=success&ip=' .
405 urlencode( $this->BlockAddress
) ) );
409 function showSuccess() {
412 $wgOut->setPagetitle( wfMsg( 'blockip' ) );
413 $wgOut->setSubtitle( wfMsg( 'blockipsuccesssub' ) );
414 $text = wfMsg( 'blockipsuccesstext', $this->BlockAddress
);
415 $wgOut->addWikiText( $text );
418 function showLogFragment( $out, $title ) {
419 $out->addHtml( wfElement( 'h2', NULL, LogPage
::logName( 'block' ) ) );
420 $request = new FauxRequest( array( 'page' => $title->getPrefixedText(), 'type' => 'block' ) );
421 $viewer = new LogViewer( new LogReader( $request ) );
422 $viewer->showList( $out );
426 * Return a comma-delimited list of "flags" to be passed to the log
427 * reader for this block, to provide more information in the logs
431 private function blockLogFlags() {
433 if( $this->BlockAnonOnly
&& IP
::isIPAddress( $this->BlockAddress
) )
434 // when blocking a user the option 'anononly' is not available/has no effect -> do not write this into log
435 $flags[] = 'anononly';
436 if( $this->BlockCreateAccount
)
437 $flags[] = 'nocreate';
438 if( !$this->BlockEnableAutoblock
)
439 $flags[] = 'noautoblock';
440 if ( $this->BlockEmail
)
441 $flags[] = 'noemail';
442 return implode( ',', $flags );
446 * Builds unblock and block list links
450 private function getConvenienceLinks() {
452 $skin = $wgUser->getSkin();
453 $links[] = $skin->makeLink ( 'MediaWiki:ipbreason-dropdown', wfMsgHtml( 'ipb-edit-dropdown' ) );
454 $links[] = $this->getUnblockLink( $skin );
455 $links[] = $this->getBlockListLink( $skin );
456 return '<p class="mw-ipb-conveniencelinks">' . implode( ' | ', $links ) . '</p>';
460 * Build a convenient link to unblock the given username or IP
461 * address, if available; otherwise link to a blank unblock
464 * @param $skin Skin to use
467 private function getUnblockLink( $skin ) {
468 $list = SpecialPage
::getTitleFor( 'Ipblocklist' );
469 if( $this->BlockAddress
) {
470 $addr = htmlspecialchars( strtr( $this->BlockAddress
, '_', ' ' ) );
471 return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-unblock-addr', $addr ),
472 'action=unblock&ip=' . urlencode( $this->BlockAddress
) );
474 return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-unblock' ), 'action=unblock' );
479 * Build a convenience link to the block list
481 * @param $skin Skin to use
484 private function getBlockListLink( $skin ) {
485 $list = SpecialPage
::getTitleFor( 'Ipblocklist' );
486 if( $this->BlockAddress
) {
487 $addr = htmlspecialchars( strtr( $this->BlockAddress
, '_', ' ' ) );
488 return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-blocklist-addr', $addr ),
489 'ip=' . urlencode( $this->BlockAddress
) );
491 return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-blocklist' ) );