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->BlockExpiry
= $wgRequest->getVal( 'wpBlockExpiry', wfMsg('ipbotheroption') );
54 $this->BlockOther
= $wgRequest->getVal( 'wpBlockOther', '' );
56 # Unchecked checkboxes are not included in the form data at all, so having one
57 # that is true by default is a bit tricky
58 $byDefault = !$wgRequest->wasPosted();
59 $this->BlockAnonOnly
= $wgRequest->getBool( 'wpAnonOnly', $byDefault );
60 $this->BlockCreateAccount
= $wgRequest->getBool( 'wpCreateAccount', $byDefault );
61 $this->BlockEnableAutoblock
= $wgRequest->getBool( 'wpEnableAutoblock', $byDefault );
62 # Re-check user's rights to hide names, very serious, defaults to 0
63 $this->BlockHideName
= $wgRequest->getBool( 'wpHideName', 0 ) && $wgUser->isAllowed( 'hideuser' );
66 function showForm( $err ) {
67 global $wgOut, $wgUser, $wgSysopUserBans;
69 $wgOut->setPagetitle( wfMsg( 'blockip' ) );
70 $wgOut->addWikiText( wfMsg( 'blockiptext' ) );
72 if($wgSysopUserBans) {
73 $mIpaddress = Xml
::label( wfMsg( 'ipadressorusername' ), 'mw-bi-target' );
75 $mIpaddress = Xml
::label( wfMsg( 'ipadress' ), 'mw-bi-target' );
77 $mIpbexpiry = Xml
::label( wfMsg( 'ipbexpiry' ), 'wpBlockExpiry' );
78 $mIpbother = Xml
::label( wfMsg( 'ipbother' ), 'mw-bi-other' );
79 $mIpbothertime = wfMsgHtml( 'ipbotheroption' );
80 $mIpbreason = Xml
::label( wfMsg( 'ipbreason' ), 'mw-bi-reason' );
81 $titleObj = SpecialPage
::getTitleFor( 'Blockip' );
82 $action = $titleObj->escapeLocalURL( "action=submit" );
85 $wgOut->setSubtitle( wfMsgHtml( 'formerror' ) );
86 $wgOut->addHTML( "<p class='error'>{$err}</p>\n" );
89 $scBlockExpiryOptions = wfMsgForContent( 'ipboptions' );
91 $showblockoptions = $scBlockExpiryOptions != '-';
92 if (!$showblockoptions)
93 $mIpbother = $mIpbexpiry;
95 $blockExpiryFormOptions = "<option value=\"other\">$mIpbothertime</option>";
96 foreach (explode(',', $scBlockExpiryOptions) as $option) {
97 if ( strpos($option, ":") === false ) $option = "$option:$option";
98 list($show, $value) = explode(":", $option);
99 $show = htmlspecialchars($show);
100 $value = htmlspecialchars($value);
102 if ($this->BlockExpiry
=== $value)
103 $selected = ' selected="selected"';
104 $blockExpiryFormOptions .= "<option value=\"$value\"$selected>$show</option>";
107 $token = htmlspecialchars( $wgUser->editToken() );
109 global $wgStylePath, $wgStyleVersion;
111 <script type=\"text/javascript\" src=\"$wgStylePath/common/block.js?$wgStyleVersion\">
113 <form id=\"blockip\" method=\"post\" action=\"{$action}\">
116 <td align=\"right\">{$mIpaddress}:</td>
118 " . Xml
::input( 'wpBlockAddress', 45, $this->BlockAddress
,
121 'id' => 'mw-bi-target',
122 'onchange' => 'updateBlockOptions()' ) ) . "
126 if ($showblockoptions) {
128 <td align=\"right\">{$mIpbexpiry}:</td>
130 <select tabindex='2' id='wpBlockExpiry' name=\"wpBlockExpiry\" onchange=\"considerChangingExpiryFocus()\">
131 $blockExpiryFormOptions
139 <tr id='wpBlockOther'>
140 <td align=\"right\">{$mIpbother}:</td>
142 " . Xml
::input( 'wpBlockOther', 45, $this->BlockOther
,
143 array( 'tabindex' => '3', 'id' => 'mw-bi-other' ) ) . "
147 <td align=\"right\">{$mIpbreason}:</td>
149 " . Xml
::input( 'wpBlockReason', 45, $this->BlockReason
,
150 array( 'tabindex' => '3', 'id' => 'mw-bi-reason' ) ) . "
153 <tr id='wpAnonOnlyRow'>
156 " . wfCheckLabel( wfMsgHtml( 'ipbanononly' ),
157 'wpAnonOnly', 'wpAnonOnly', $this->BlockAnonOnly
,
158 array( 'tabindex' => '4' ) ) . "
161 <tr id='wpCreateAccountRow'>
164 " . wfCheckLabel( wfMsgHtml( 'ipbcreateaccount' ),
165 'wpCreateAccount', 'wpCreateAccount', $this->BlockCreateAccount
,
166 array( 'tabindex' => '5' ) ) . "
169 <tr id='wpEnableAutoblockRow'>
172 " . wfCheckLabel( wfMsgHtml( 'ipbenableautoblock' ),
173 'wpEnableAutoblock', 'wpEnableAutoblock', $this->BlockEnableAutoblock
,
174 array( 'tabindex' => '6' ) ) . "
178 // Allow some users to hide name from block log, blocklist and listusers
179 if ( $wgUser->isAllowed( 'hideuser' ) ) {
184 " . wfCheckLabel( wfMsgHtml( 'ipbhidename' ),
185 'wpHideName', 'wpHideName', $this->BlockHideName
,
186 array( 'tabindex' => '6' ) ) . "
193 <td style='padding-top: 1em'> </td>
194 <td style='padding-top: 1em' align=\"left\">
195 " . Xml
::submitButton( wfMsgHtml( 'ipbsubmit' ),
196 array( 'name' => 'wpBlock', 'tabindex' => '7' ) ) . "
200 Xml
::hidden( 'wpEditToken', $token ) .
202 <script type=\"text/javascript\">updateBlockOptions()</script>
205 $wgOut->addHtml( $this->getConvenienceLinks() );
207 $user = User
::newFromName( $this->BlockAddress
);
208 if( is_object( $user ) ) {
209 $this->showLogFragment( $wgOut, $user->getUserPage() );
210 } elseif( preg_match( '/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/', $this->BlockAddress
) ) {
211 $this->showLogFragment( $wgOut, Title
::makeTitle( NS_USER
, $this->BlockAddress
) );
212 } 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
) ) {
213 $this->showLogFragment( $wgOut, Title
::makeTitle( NS_USER
, $this->BlockAddress
) );
217 function doSubmit() {
218 global $wgOut, $wgUser, $wgSysopUserBans, $wgSysopRangeBans;
221 # Expand valid IPv6 addresses, usernames are left as is
222 $this->BlockAddress
= IP
::sanitizeIP( $this->BlockAddress
);
223 # isIPv4() and IPv6() are used for final validation
224 $rxIP4 = '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}';
225 $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}';
226 $rxIP = "($rxIP4|$rxIP6)";
228 # Check for invalid specifications
229 if ( !preg_match( "/^$rxIP$/", $this->BlockAddress
) ) {
231 if ( preg_match( "/^($rxIP4)\\/(\\d{1,2})$/", $this->BlockAddress
, $matches ) ) {
233 if ( $wgSysopRangeBans ) {
234 if ( !IP
::isIPv4( $this->BlockAddress
) ||
$matches[2] < 16 ||
$matches[2] > 32 ) {
235 $this->showForm( wfMsg( 'ip_range_invalid' ) );
238 $this->BlockAddress
= Block
::normaliseRange( $this->BlockAddress
);
240 # Range block illegal
241 $this->showForm( wfMsg( 'range_block_disabled' ) );
244 } else if ( preg_match( "/^($rxIP6)\\/(\\d{1,3})$/", $this->BlockAddress
, $matches ) ) {
246 if ( $wgSysopRangeBans ) {
247 if ( !IP
::isIPv6( $this->BlockAddress
) ||
$matches[2] < 64 ||
$matches[2] > 128 ) {
248 $this->showForm( wfMsg( 'ip_range_invalid' ) );
251 $this->BlockAddress
= Block
::normaliseRange( $this->BlockAddress
);
253 # Range block illegal
254 $this->showForm( wfMsg( 'range_block_disabled' ) );
259 if ( $wgSysopUserBans ) {
260 $user = User
::newFromName( $this->BlockAddress
);
261 if( !is_null( $user ) && $user->getID() ) {
263 $this->BlockAddress
= $user->getName();
264 $userId = $user->getID();
266 $this->showForm( wfMsg( 'nosuchusershort', htmlspecialchars( $this->BlockAddress
) ) );
270 $this->showForm( wfMsg( 'badipaddress' ) );
276 $expirestr = $this->BlockExpiry
;
277 if( $expirestr == 'other' )
278 $expirestr = $this->BlockOther
;
280 if (strlen($expirestr) == 0) {
281 $this->showForm( wfMsg( 'ipb_expiry_invalid' ) );
285 if ( $expirestr == 'infinite' ||
$expirestr == 'indefinite' ) {
286 $expiry = Block
::infinity();
288 # Convert GNU-style date, on error returns -1 for PHP <5.1 and false for PHP >=5.1
289 $expiry = strtotime( $expirestr );
291 if ( $expiry < 0 ||
$expiry === false ) {
292 $this->showForm( wfMsg( 'ipb_expiry_invalid' ) );
296 $expiry = wfTimestamp( TS_MW
, $expiry );
300 # Note: for a user block, ipb_address is only for display purposes
302 $block = new Block( $this->BlockAddress
, $userId, $wgUser->getID(),
303 $this->BlockReason
, wfTimestampNow(), 0, $expiry, $this->BlockAnonOnly
,
304 $this->BlockCreateAccount
, $this->BlockEnableAutoblock
, $this->BlockHideName
);
306 if (wfRunHooks('BlockIp', array(&$block, &$wgUser))) {
308 if ( !$block->insert() ) {
309 $this->showForm( wfMsg( 'ipb_already_blocked',
310 htmlspecialchars( $this->BlockAddress
) ) );
314 wfRunHooks('BlockIpComplete', array($block, $wgUser));
316 # Prepare log parameters
317 $logParams = array();
318 $logParams[] = $expirestr;
319 $logParams[] = $this->blockLogFlags();
321 # Make log entry, if the name is hidden, put it in the oversight log
322 $log_type = ($this->BlockHideName
) ?
'oversight' : 'block';
323 $log = new LogPage( $log_type );
324 $log->addEntry( 'block', Title
::makeTitle( NS_USER
, $this->BlockAddress
),
325 $this->BlockReason
, $logParams );
328 $titleObj = SpecialPage
::getTitleFor( 'Blockip' );
329 $wgOut->redirect( $titleObj->getFullURL( 'action=success&ip=' .
330 urlencode( $this->BlockAddress
) ) );
334 function showSuccess() {
337 $wgOut->setPagetitle( wfMsg( 'blockip' ) );
338 $wgOut->setSubtitle( wfMsg( 'blockipsuccesssub' ) );
339 $text = wfMsg( 'blockipsuccesstext', $this->BlockAddress
);
340 $wgOut->addWikiText( $text );
343 function showLogFragment( $out, $title ) {
344 $out->addHtml( wfElement( 'h2', NULL, LogPage
::logName( 'block' ) ) );
345 $request = new FauxRequest( array( 'page' => $title->getPrefixedText(), 'type' => 'block' ) );
346 $viewer = new LogViewer( new LogReader( $request ) );
347 $viewer->showList( $out );
351 * Return a comma-delimited list of "flags" to be passed to the log
352 * reader for this block, to provide more information in the logs
356 private function blockLogFlags() {
358 if( $this->BlockAnonOnly
&& IP
::isIPAddress( $this->BlockAddress
) )
359 // when blocking a user the option 'anononly' is not available/has no effect -> do not write this into log
360 $flags[] = 'anononly';
361 if( $this->BlockCreateAccount
)
362 $flags[] = 'nocreate';
363 if( !$this->BlockEnableAutoblock
)
364 $flags[] = 'noautoblock';
365 return implode( ',', $flags );
369 * Builds unblock and block list links
373 private function getConvenienceLinks() {
375 $skin = $wgUser->getSkin();
376 $links[] = $this->getUnblockLink( $skin );
377 $links[] = $this->getBlockListLink( $skin );
378 return '<p class="mw-ipb-conveniencelinks">' . implode( ' | ', $links ) . '</p>';
382 * Build a convenient link to unblock the given username or IP
383 * address, if available; otherwise link to a blank unblock
386 * @param $skin Skin to use
389 private function getUnblockLink( $skin ) {
390 $list = SpecialPage
::getTitleFor( 'Ipblocklist' );
391 if( $this->BlockAddress
) {
392 $addr = htmlspecialchars( strtr( $this->BlockAddress
, '_', ' ' ) );
393 return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-unblock-addr', $addr ),
394 'action=unblock&ip=' . urlencode( $this->BlockAddress
) );
396 return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-unblock' ), 'action=unblock' );
401 * Build a convenience link to the block list
403 * @param $skin Skin to use
406 private function getBlockListLink( $skin ) {
407 $list = SpecialPage
::getTitleFor( 'Ipblocklist' );
408 if( $this->BlockAddress
) {
409 $addr = htmlspecialchars( strtr( $this->BlockAddress
, '_', ' ' ) );
410 return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-blocklist-addr', $addr ),
411 'ip=' . urlencode( $this->BlockAddress
) );
413 return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-blocklist' ) );