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;
47 function IPBlockForm( $par ) {
48 global $wgRequest, $wgUser;
50 $this->BlockAddress
= $wgRequest->getVal( 'wpBlockAddress', $wgRequest->getVal( 'ip', $par ) );
51 $this->BlockAddress
= strtr( $this->BlockAddress
, '_', ' ' );
52 $this->BlockReason
= $wgRequest->getText( 'wpBlockReason' );
53 $this->BlockReasonList
= $wgRequest->getText( 'wpBlockReasonList' );
54 $this->BlockExpiry
= $wgRequest->getVal( 'wpBlockExpiry', wfMsg('ipbotheroption') );
55 $this->BlockOther
= $wgRequest->getVal( 'wpBlockOther', '' );
57 # Unchecked checkboxes are not included in the form data at all, so having one
58 # that is true by default is a bit tricky
59 $byDefault = !$wgRequest->wasPosted();
60 $this->BlockAnonOnly
= $wgRequest->getBool( 'wpAnonOnly', $byDefault );
61 $this->BlockCreateAccount
= $wgRequest->getBool( 'wpCreateAccount', $byDefault );
62 $this->BlockEnableAutoblock
= $wgRequest->getBool( 'wpEnableAutoblock', $byDefault );
63 # Re-check user's rights to hide names, very serious, defaults to 0
64 $this->BlockHideName
= $wgRequest->getBool( 'wpHideName', 0 ) && $wgUser->isAllowed( 'hideuser' );
67 function showForm( $err ) {
68 global $wgOut, $wgUser, $wgSysopUserBans, $wgContLang;
70 $wgOut->setPagetitle( wfMsg( 'blockip' ) );
71 $wgOut->addWikiText( wfMsg( 'blockiptext' ) );
73 if($wgSysopUserBans) {
74 $mIpaddress = Xml
::label( wfMsg( 'ipadressorusername' ), 'mw-bi-target' );
76 $mIpaddress = Xml
::label( wfMsg( 'ipadress' ), 'mw-bi-target' );
78 $mIpbexpiry = Xml
::label( wfMsg( 'ipbexpiry' ), 'wpBlockExpiry' );
79 $mIpbother = Xml
::label( wfMsg( 'ipbother' ), 'mw-bi-other' );
80 $mIpbothertime = wfMsgHtml( 'ipbotheroption' );
81 $mIpbreasonother = Xml
::label( wfMsg( 'ipbreason' ), 'wpBlockReasonList' );
82 $mIpbreason = Xml
::label( wfMsg( 'ipbotherreason' ), 'mw-bi-reason' );
83 $mIpbreasonotherlist = wfMsgHtml( 'ipbreasonotherlist' );
85 $titleObj = SpecialPage
::getTitleFor( 'Blockip' );
86 $action = $titleObj->escapeLocalURL( "action=submit" );
87 $alignRight = $wgContLang->isRtl() ?
'left' : 'right';
90 $wgOut->setSubtitle( wfMsgHtml( 'formerror' ) );
91 $wgOut->addHTML( "<p class='error'>{$err}</p>\n" );
94 $scBlockExpiryOptions = wfMsgForContent( 'ipboptions' );
96 $showblockoptions = $scBlockExpiryOptions != '-';
97 if (!$showblockoptions)
98 $mIpbother = $mIpbexpiry;
100 $blockExpiryFormOptions = "<option value=\"other\">$mIpbothertime</option>";
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);
107 if ($this->BlockExpiry
=== $value)
108 $selected = ' selected="selected"';
109 $blockExpiryFormOptions .= "<option value=\"$value\"$selected>$show</option>";
112 $scBlockReasonList = wfMsgForContent( 'ipbreason-dropdown' );
113 $blockReasonList = '';
114 if ( $scBlockReasonList != '' && $scBlockReasonList != '-' ) {
115 $blockReasonList = "<option value=\"other\">$mIpbreasonotherlist</option>";
117 foreach ( explode( "\n", $scBlockReasonList ) as $option) {
118 $value = trim( htmlspecialchars($option) );
119 if ( $value == '' ) {
121 } elseif ( substr( $value, 0, 1) == '*' && substr( $value, 1, 1) != '*' ) {
122 // A new group is starting ...
123 $value = trim( substr( $value, 1 ) );
124 $blockReasonList .= "$optgroup<optgroup label=\"$value\">";
125 $optgroup = "</optgroup>";
126 } elseif ( substr( $value, 0, 2) == '**' ) {
129 $value = trim( substr( $value, 2 ) );
130 if ( $this->BlockReasonList
=== $value)
131 $selected = ' selected="selected"';
132 $blockReasonList .= "<option value=\"$value\"$selected>$value</option>";
134 // groupless block reason
136 if ( $this->BlockReasonList
=== $value)
137 $selected = ' selected="selected"';
138 $blockReasonList .= "$optgroup<option value=\"$value\"$selected>$value</option>";
142 $blockReasonList .= $optgroup;
145 $token = htmlspecialchars( $wgUser->editToken() );
147 global $wgStylePath, $wgStyleVersion;
149 <script type=\"text/javascript\" src=\"$wgStylePath/common/block.js?$wgStyleVersion\">
151 <form id=\"blockip\" method=\"post\" action=\"{$action}\">
154 <td align=\"$alignRight\">{$mIpaddress}</td>
156 " . Xml
::input( 'wpBlockAddress', 45, $this->BlockAddress
,
159 'id' => 'mw-bi-target',
160 'onchange' => 'updateBlockOptions()' ) ) . "
164 if ($showblockoptions) {
166 <td align=\"$alignRight\">{$mIpbexpiry}</td>
168 <select tabindex='2' id='wpBlockExpiry' name=\"wpBlockExpiry\" onchange=\"considerChangingExpiryFocus()\">
169 $blockExpiryFormOptions
176 <tr id='wpBlockOther'>
177 <td align=\"$alignRight\">{$mIpbother}</td>
179 " . Xml
::input( 'wpBlockOther', 45, $this->BlockOther
,
180 array( 'tabindex' => '3', 'id' => 'mw-bi-other' ) ) . "
183 if ( $blockReasonList != '' ) {
186 <td align=\"$alignRight\">{$mIpbreasonother}</td>
188 <select tabindex='4' id=\"wpBlockReasonList\" name=\"wpBlockReasonList\">
195 <tr id=\"wpBlockReason\">
196 <td align=\"$alignRight\">{$mIpbreason}</td>
198 " . Xml
::input( 'wpBlockReason', 45, $this->BlockReason
,
199 array( 'tabindex' => '5', 'id' => 'mw-bi-reason' ) ) . "
202 <tr id='wpAnonOnlyRow'>
205 " . wfCheckLabel( wfMsgHtml( 'ipbanononly' ),
206 'wpAnonOnly', 'wpAnonOnly', $this->BlockAnonOnly
,
207 array( 'tabindex' => '6' ) ) . "
210 <tr id='wpCreateAccountRow'>
213 " . wfCheckLabel( wfMsgHtml( 'ipbcreateaccount' ),
214 'wpCreateAccount', 'wpCreateAccount', $this->BlockCreateAccount
,
215 array( 'tabindex' => '7' ) ) . "
218 <tr id='wpEnableAutoblockRow'>
221 " . wfCheckLabel( wfMsgHtml( 'ipbenableautoblock' ),
222 'wpEnableAutoblock', 'wpEnableAutoblock', $this->BlockEnableAutoblock
,
223 array( 'tabindex' => '8' ) ) . "
227 // Allow some users to hide name from block log, blocklist and listusers
228 if ( $wgUser->isAllowed( 'hideuser' ) ) {
233 " . wfCheckLabel( wfMsgHtml( 'ipbhidename' ),
234 'wpHideName', 'wpHideName', $this->BlockHideName
,
235 array( 'tabindex' => '9' ) ) . "
242 <td style='padding-top: 1em'> </td>
243 <td style='padding-top: 1em'>
244 " . Xml
::submitButton( wfMsg( 'ipbsubmit' ),
245 array( 'name' => 'wpBlock', 'tabindex' => '10' ) ) . "
249 Xml
::hidden( 'wpEditToken', $token ) .
251 <script type=\"text/javascript\">updateBlockOptions()</script>
254 $wgOut->addHtml( $this->getConvenienceLinks() );
256 $user = User
::newFromName( $this->BlockAddress
);
257 if( is_object( $user ) ) {
258 $this->showLogFragment( $wgOut, $user->getUserPage() );
259 } elseif( preg_match( '/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/', $this->BlockAddress
) ) {
260 $this->showLogFragment( $wgOut, Title
::makeTitle( NS_USER
, $this->BlockAddress
) );
261 } 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
) ) {
262 $this->showLogFragment( $wgOut, Title
::makeTitle( NS_USER
, $this->BlockAddress
) );
266 function doSubmit() {
267 global $wgOut, $wgUser, $wgSysopUserBans, $wgSysopRangeBans;
270 # Expand valid IPv6 addresses, usernames are left as is
271 $this->BlockAddress
= IP
::sanitizeIP( $this->BlockAddress
);
272 # isIPv4() and IPv6() are used for final validation
273 $rxIP4 = '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}';
274 $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}';
275 $rxIP = "($rxIP4|$rxIP6)";
277 # Check for invalid specifications
278 if ( !preg_match( "/^$rxIP$/", $this->BlockAddress
) ) {
280 if ( preg_match( "/^($rxIP4)\\/(\\d{1,2})$/", $this->BlockAddress
, $matches ) ) {
282 if ( $wgSysopRangeBans ) {
283 if ( !IP
::isIPv4( $this->BlockAddress
) ||
$matches[2] < 16 ||
$matches[2] > 32 ) {
284 $this->showForm( wfMsg( 'ip_range_invalid' ) );
287 $this->BlockAddress
= Block
::normaliseRange( $this->BlockAddress
);
289 # Range block illegal
290 $this->showForm( wfMsg( 'range_block_disabled' ) );
293 } else if ( preg_match( "/^($rxIP6)\\/(\\d{1,3})$/", $this->BlockAddress
, $matches ) ) {
295 if ( $wgSysopRangeBans ) {
296 if ( !IP
::isIPv6( $this->BlockAddress
) ||
$matches[2] < 64 ||
$matches[2] > 128 ) {
297 $this->showForm( wfMsg( 'ip_range_invalid' ) );
300 $this->BlockAddress
= Block
::normaliseRange( $this->BlockAddress
);
302 # Range block illegal
303 $this->showForm( wfMsg( 'range_block_disabled' ) );
308 if ( $wgSysopUserBans ) {
309 $user = User
::newFromName( $this->BlockAddress
);
310 if( !is_null( $user ) && $user->getID() ) {
312 $this->BlockAddress
= $user->getName();
313 $userId = $user->getID();
315 $this->showForm( wfMsg( 'nosuchusershort', htmlspecialchars( $this->BlockAddress
) ) );
319 $this->showForm( wfMsg( 'badipaddress' ) );
325 $reasonstr = $this->BlockReasonList
;
326 if ( $reasonstr != 'other' && $this->BlockReason
!= '') {
327 // Entry from drop down menu + additional comment
328 $reasonstr .= ': ' . $this->BlockReason
;
329 } elseif ( $reasonstr == 'other' ) {
330 $reasonstr = $this->BlockReason
;
333 $expirestr = $this->BlockExpiry
;
334 if( $expirestr == 'other' )
335 $expirestr = $this->BlockOther
;
337 if (strlen($expirestr) == 0) {
338 $this->showForm( wfMsg( 'ipb_expiry_invalid' ) );
342 if ( $expirestr == 'infinite' ||
$expirestr == 'indefinite' ) {
343 $expiry = Block
::infinity();
345 # Convert GNU-style date, on error returns -1 for PHP <5.1 and false for PHP >=5.1
346 $expiry = strtotime( $expirestr );
348 if ( $expiry < 0 ||
$expiry === false ) {
349 $this->showForm( wfMsg( 'ipb_expiry_invalid' ) );
353 $expiry = wfTimestamp( TS_MW
, $expiry );
357 # Note: for a user block, ipb_address is only for display purposes
359 $block = new Block( $this->BlockAddress
, $userId, $wgUser->getID(),
360 $reasonstr, wfTimestampNow(), 0, $expiry, $this->BlockAnonOnly
,
361 $this->BlockCreateAccount
, $this->BlockEnableAutoblock
, $this->BlockHideName
);
363 if (wfRunHooks('BlockIp', array(&$block, &$wgUser))) {
365 if ( !$block->insert() ) {
366 $this->showForm( wfMsg( 'ipb_already_blocked',
367 htmlspecialchars( $this->BlockAddress
) ) );
371 wfRunHooks('BlockIpComplete', array($block, $wgUser));
373 # Prepare log parameters
374 $logParams = array();
375 $logParams[] = $expirestr;
376 $logParams[] = $this->blockLogFlags();
378 # Make log entry, if the name is hidden, put it in the oversight log
379 $log_type = ($this->BlockHideName
) ?
'oversight' : 'block';
380 $log = new LogPage( $log_type );
381 $log->addEntry( 'block', Title
::makeTitle( NS_USER
, $this->BlockAddress
),
382 $reasonstr, $logParams );
385 $titleObj = SpecialPage
::getTitleFor( 'Blockip' );
386 $wgOut->redirect( $titleObj->getFullURL( 'action=success&ip=' .
387 urlencode( $this->BlockAddress
) ) );
391 function showSuccess() {
394 $wgOut->setPagetitle( wfMsg( 'blockip' ) );
395 $wgOut->setSubtitle( wfMsg( 'blockipsuccesssub' ) );
396 $text = wfMsg( 'blockipsuccesstext', $this->BlockAddress
);
397 $wgOut->addWikiText( $text );
400 function showLogFragment( $out, $title ) {
401 $out->addHtml( wfElement( 'h2', NULL, LogPage
::logName( 'block' ) ) );
402 $request = new FauxRequest( array( 'page' => $title->getPrefixedText(), 'type' => 'block' ) );
403 $viewer = new LogViewer( new LogReader( $request ) );
404 $viewer->showList( $out );
408 * Return a comma-delimited list of "flags" to be passed to the log
409 * reader for this block, to provide more information in the logs
413 private function blockLogFlags() {
415 if( $this->BlockAnonOnly
&& IP
::isIPAddress( $this->BlockAddress
) )
416 // when blocking a user the option 'anononly' is not available/has no effect -> do not write this into log
417 $flags[] = 'anononly';
418 if( $this->BlockCreateAccount
)
419 $flags[] = 'nocreate';
420 if( !$this->BlockEnableAutoblock
)
421 $flags[] = 'noautoblock';
422 return implode( ',', $flags );
426 * Builds unblock and block list links
430 private function getConvenienceLinks() {
432 $skin = $wgUser->getSkin();
433 $links[] = $skin->makeLink ( 'MediaWiki:ipbreason-dropdown', wfMsgHtml( 'ipb-edit-dropdown' ) );
434 $links[] = $this->getUnblockLink( $skin );
435 $links[] = $this->getBlockListLink( $skin );
436 return '<p class="mw-ipb-conveniencelinks">' . implode( ' | ', $links ) . '</p>';
440 * Build a convenient link to unblock the given username or IP
441 * address, if available; otherwise link to a blank unblock
444 * @param $skin Skin to use
447 private function getUnblockLink( $skin ) {
448 $list = SpecialPage
::getTitleFor( 'Ipblocklist' );
449 if( $this->BlockAddress
) {
450 $addr = htmlspecialchars( strtr( $this->BlockAddress
, '_', ' ' ) );
451 return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-unblock-addr', $addr ),
452 'action=unblock&ip=' . urlencode( $this->BlockAddress
) );
454 return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-unblock' ), 'action=unblock' );
459 * Build a convenience link to the block list
461 * @param $skin Skin to use
464 private function getBlockListLink( $skin ) {
465 $list = SpecialPage
::getTitleFor( 'Ipblocklist' );
466 if( $this->BlockAddress
) {
467 $addr = htmlspecialchars( strtr( $this->BlockAddress
, '_', ' ' ) );
468 return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-blocklist-addr', $addr ),
469 'ip=' . urlencode( $this->BlockAddress
) );
471 return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-blocklist' ) );