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;
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" );
89 $wgOut->setSubtitle( wfMsgHtml( 'formerror' ) );
90 $wgOut->addHTML( "<p class='error'>{$err}</p>\n" );
93 $scBlockExpiryOptions = wfMsgForContent( 'ipboptions' );
95 $showblockoptions = $scBlockExpiryOptions != '-';
96 if (!$showblockoptions)
97 $mIpbother = $mIpbexpiry;
99 $blockExpiryFormOptions = "<option value=\"other\">$mIpbothertime</option>";
100 foreach (explode(',', $scBlockExpiryOptions) as $option) {
101 if ( strpos($option, ":") === false ) $option = "$option:$option";
102 list($show, $value) = explode(":", $option);
103 $show = htmlspecialchars($show);
104 $value = htmlspecialchars($value);
106 if ($this->BlockExpiry
=== $value)
107 $selected = ' selected="selected"';
108 $blockExpiryFormOptions .= "<option value=\"$value\"$selected>$show</option>";
111 $scBlockReasonList = wfMsgForContent( 'ipbreason-dropdown' );
112 $blockReasonList = '';
113 if ( $scBlockReasonList != '' && $scBlockReasonList != '-' ) {
114 $blockReasonList = "<option value=\"other\">$mIpbreasonotherlist</option>";
116 foreach ( explode( "\n", $scBlockReasonList ) as $option) {
117 $value = trim( htmlspecialchars($option) );
118 if ( $value == '' ) {
120 } elseif ( substr( $value, 0, 1) == '*' && substr( $value, 1, 1) != '*' ) {
121 // A new group is starting ...
122 $value = trim( substr( $value, 1 ) );
123 $blockReasonList .= "$optgroup<optgroup label=\"$value\">";
124 $optgroup = "</optgroup>";
125 } elseif ( substr( $value, 0, 2) == '**' ) {
128 $value = trim( substr( $value, 2 ) );
129 if ( $this->BlockReasonList
=== $value)
130 $selected = ' selected="selected"';
131 $blockReasonList .= "<option value=\"$value\"$selected>$value</option>";
133 // groupless block reason
135 if ( $this->BlockReasonList
=== $value)
136 $selected = ' selected="selected"';
137 $blockReasonList .= "$optgroup<option value=\"$value\"$selected>$value</option>";
141 $blockReasonList .= $optgroup;
144 $token = htmlspecialchars( $wgUser->editToken() );
146 global $wgStylePath, $wgStyleVersion;
148 <script type=\"text/javascript\" src=\"$wgStylePath/common/block.js?$wgStyleVersion\">
150 <form id=\"blockip\" method=\"post\" action=\"{$action}\">
153 <td align=\"right\">{$mIpaddress}:</td>
155 " . Xml
::input( 'wpBlockAddress', 45, $this->BlockAddress
,
158 'id' => 'mw-bi-target',
159 'onchange' => 'updateBlockOptions()' ) ) . "
163 if ($showblockoptions) {
165 <td align=\"right\">{$mIpbexpiry}:</td>
167 <select tabindex='2' id='wpBlockExpiry' name=\"wpBlockExpiry\" onchange=\"considerChangingExpiryFocus()\">
168 $blockExpiryFormOptions
175 <tr id='wpBlockOther'>
176 <td align=\"right\">{$mIpbother}:</td>
178 " . Xml
::input( 'wpBlockOther', 45, $this->BlockOther
,
179 array( 'tabindex' => '3', 'id' => 'mw-bi-other' ) ) . "
182 if ( $blockReasonList != '' ) {
185 <td align=\"right\">{$mIpbreasonother}:</td>
187 <select tabindex='4' id=\"wpBlockReasonList\" name=\"wpBlockReasonList\">
194 <tr id=\"wpBlockReason\">
195 <td align=\"right\">{$mIpbreason}:</td>
197 " . Xml
::input( 'wpBlockReason', 45, $this->BlockReason
,
198 array( 'tabindex' => '5', 'id' => 'mw-bi-reason' ) ) . "
201 <tr id='wpAnonOnlyRow'>
204 " . wfCheckLabel( wfMsgHtml( 'ipbanononly' ),
205 'wpAnonOnly', 'wpAnonOnly', $this->BlockAnonOnly
,
206 array( 'tabindex' => '6' ) ) . "
209 <tr id='wpCreateAccountRow'>
212 " . wfCheckLabel( wfMsgHtml( 'ipbcreateaccount' ),
213 'wpCreateAccount', 'wpCreateAccount', $this->BlockCreateAccount
,
214 array( 'tabindex' => '7' ) ) . "
217 <tr id='wpEnableAutoblockRow'>
220 " . wfCheckLabel( wfMsgHtml( 'ipbenableautoblock' ),
221 'wpEnableAutoblock', 'wpEnableAutoblock', $this->BlockEnableAutoblock
,
222 array( 'tabindex' => '8' ) ) . "
226 // Allow some users to hide name from block log, blocklist and listusers
227 if ( $wgUser->isAllowed( 'hideuser' ) ) {
232 " . wfCheckLabel( wfMsgHtml( 'ipbhidename' ),
233 'wpHideName', 'wpHideName', $this->BlockHideName
,
234 array( 'tabindex' => '9' ) ) . "
241 <td style='padding-top: 1em'> </td>
242 <td style='padding-top: 1em' align=\"left\">
243 " . Xml
::submitButton( wfMsgHtml( 'ipbsubmit' ),
244 array( 'name' => 'wpBlock', 'tabindex' => '10' ) ) . "
248 Xml
::hidden( 'wpEditToken', $token ) .
250 <script type=\"text/javascript\">updateBlockOptions()</script>
253 $wgOut->addHtml( $this->getConvenienceLinks() );
255 $user = User
::newFromName( $this->BlockAddress
);
256 if( is_object( $user ) ) {
257 $this->showLogFragment( $wgOut, $user->getUserPage() );
258 } elseif( preg_match( '/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/', $this->BlockAddress
) ) {
259 $this->showLogFragment( $wgOut, Title
::makeTitle( NS_USER
, $this->BlockAddress
) );
260 } 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
) ) {
261 $this->showLogFragment( $wgOut, Title
::makeTitle( NS_USER
, $this->BlockAddress
) );
265 function doSubmit() {
266 global $wgOut, $wgUser, $wgSysopUserBans, $wgSysopRangeBans;
269 # Expand valid IPv6 addresses, usernames are left as is
270 $this->BlockAddress
= IP
::sanitizeIP( $this->BlockAddress
);
271 # isIPv4() and IPv6() are used for final validation
272 $rxIP4 = '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}';
273 $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}';
274 $rxIP = "($rxIP4|$rxIP6)";
276 # Check for invalid specifications
277 if ( !preg_match( "/^$rxIP$/", $this->BlockAddress
) ) {
279 if ( preg_match( "/^($rxIP4)\\/(\\d{1,2})$/", $this->BlockAddress
, $matches ) ) {
281 if ( $wgSysopRangeBans ) {
282 if ( !IP
::isIPv4( $this->BlockAddress
) ||
$matches[2] < 16 ||
$matches[2] > 32 ) {
283 $this->showForm( wfMsg( 'ip_range_invalid' ) );
286 $this->BlockAddress
= Block
::normaliseRange( $this->BlockAddress
);
288 # Range block illegal
289 $this->showForm( wfMsg( 'range_block_disabled' ) );
292 } else if ( preg_match( "/^($rxIP6)\\/(\\d{1,3})$/", $this->BlockAddress
, $matches ) ) {
294 if ( $wgSysopRangeBans ) {
295 if ( !IP
::isIPv6( $this->BlockAddress
) ||
$matches[2] < 64 ||
$matches[2] > 128 ) {
296 $this->showForm( wfMsg( 'ip_range_invalid' ) );
299 $this->BlockAddress
= Block
::normaliseRange( $this->BlockAddress
);
301 # Range block illegal
302 $this->showForm( wfMsg( 'range_block_disabled' ) );
307 if ( $wgSysopUserBans ) {
308 $user = User
::newFromName( $this->BlockAddress
);
309 if( !is_null( $user ) && $user->getID() ) {
311 $this->BlockAddress
= $user->getName();
312 $userId = $user->getID();
314 $this->showForm( wfMsg( 'nosuchusershort', htmlspecialchars( $this->BlockAddress
) ) );
318 $this->showForm( wfMsg( 'badipaddress' ) );
324 $reasonstr = $this->BlockReasonList
;
325 if ( $reasonstr != 'other' && $this->BlockReason
!= '') {
326 // Entry from drop down menu + additional comment
327 $reasonstr .= ': ' . $this->BlockReason
;
328 } elseif ( $reasonstr == 'other' ) {
329 $reasonstr = $this->BlockReason
;
332 $expirestr = $this->BlockExpiry
;
333 if( $expirestr == 'other' )
334 $expirestr = $this->BlockOther
;
336 if (strlen($expirestr) == 0) {
337 $this->showForm( wfMsg( 'ipb_expiry_invalid' ) );
341 if ( $expirestr == 'infinite' ||
$expirestr == 'indefinite' ) {
342 $expiry = Block
::infinity();
344 # Convert GNU-style date, on error returns -1 for PHP <5.1 and false for PHP >=5.1
345 $expiry = strtotime( $expirestr );
347 if ( $expiry < 0 ||
$expiry === false ) {
348 $this->showForm( wfMsg( 'ipb_expiry_invalid' ) );
352 $expiry = wfTimestamp( TS_MW
, $expiry );
356 # Note: for a user block, ipb_address is only for display purposes
358 $block = new Block( $this->BlockAddress
, $userId, $wgUser->getID(),
359 $reasonstr, wfTimestampNow(), 0, $expiry, $this->BlockAnonOnly
,
360 $this->BlockCreateAccount
, $this->BlockEnableAutoblock
, $this->BlockHideName
);
362 if (wfRunHooks('BlockIp', array(&$block, &$wgUser))) {
364 if ( !$block->insert() ) {
365 $this->showForm( wfMsg( 'ipb_already_blocked',
366 htmlspecialchars( $this->BlockAddress
) ) );
370 wfRunHooks('BlockIpComplete', array($block, $wgUser));
372 # Prepare log parameters
373 $logParams = array();
374 $logParams[] = $expirestr;
375 $logParams[] = $this->blockLogFlags();
377 # Make log entry, if the name is hidden, put it in the oversight log
378 $log_type = ($this->BlockHideName
) ?
'oversight' : 'block';
379 $log = new LogPage( $log_type );
380 $log->addEntry( 'block', Title
::makeTitle( NS_USER
, $this->BlockAddress
),
381 $reasonstr, $logParams );
384 $titleObj = SpecialPage
::getTitleFor( 'Blockip' );
385 $wgOut->redirect( $titleObj->getFullURL( 'action=success&ip=' .
386 urlencode( $this->BlockAddress
) ) );
390 function showSuccess() {
393 $wgOut->setPagetitle( wfMsg( 'blockip' ) );
394 $wgOut->setSubtitle( wfMsg( 'blockipsuccesssub' ) );
395 $text = wfMsg( 'blockipsuccesstext', $this->BlockAddress
);
396 $wgOut->addWikiText( $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 return implode( ',', $flags );
425 * Builds unblock and block list links
429 private function getConvenienceLinks() {
431 $skin = $wgUser->getSkin();
432 $links[] = $skin->makeLink ( 'MediaWiki:ipbreason-dropdown', wfMsgHtml( 'ipb-edit-dropdown' ) );
433 $links[] = $this->getUnblockLink( $skin );
434 $links[] = $this->getBlockListLink( $skin );
435 return '<p class="mw-ipb-conveniencelinks">' . implode( ' | ', $links ) . '</p>';
439 * Build a convenient link to unblock the given username or IP
440 * address, if available; otherwise link to a blank unblock
443 * @param $skin Skin to use
446 private function getUnblockLink( $skin ) {
447 $list = SpecialPage
::getTitleFor( 'Ipblocklist' );
448 if( $this->BlockAddress
) {
449 $addr = htmlspecialchars( strtr( $this->BlockAddress
, '_', ' ' ) );
450 return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-unblock-addr', $addr ),
451 'action=unblock&ip=' . urlencode( $this->BlockAddress
) );
453 return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-unblock' ), 'action=unblock' );
458 * Build a convenience link to the block list
460 * @param $skin Skin to use
463 private function getBlockListLink( $skin ) {
464 $list = SpecialPage
::getTitleFor( 'Ipblocklist' );
465 if( $this->BlockAddress
) {
466 $addr = htmlspecialchars( strtr( $this->BlockAddress
, '_', ' ' ) );
467 return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-blocklist-addr', $addr ),
468 'ip=' . urlencode( $this->BlockAddress
) );
470 return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-blocklist' ) );