* Rollback should not work if the wiki is read-only
[lhc/web/wiklou.git] / includes / SpecialBlockip.php
1 <?php
2 /**
3 * Constructor for Special:Blockip page
4 *
5 * @addtogroup SpecialPage
6 */
7
8 /**
9 * Constructor
10 */
11 function wfSpecialBlockip( $par ) {
12 global $wgUser, $wgOut, $wgRequest;
13
14 # Can't block when the database is locked
15 if( wfReadOnly() ) {
16 $wgOut->readOnlyPage();
17 return;
18 }
19
20 # Permission check
21 if( !$wgUser->isAllowed( 'block' ) ) {
22 $wgOut->permissionRequired( 'block' );
23 return;
24 }
25
26 $ipb = new IPBlockForm( $par );
27
28 $action = $wgRequest->getVal( 'action' );
29 if ( 'success' == $action ) {
30 $ipb->showSuccess();
31 } else if ( $wgRequest->wasPosted() && 'submit' == $action &&
32 $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ) ) ) {
33 $ipb->doSubmit();
34 } else {
35 $ipb->showForm( '' );
36 }
37 }
38
39 /**
40 * Form object for the Special:Blockip page.
41 *
42 * @addtogroup SpecialPage
43 */
44 class IPBlockForm {
45 var $BlockAddress, $BlockExpiry, $BlockReason;
46 # var $BlockEmail;
47
48 function IPBlockForm( $par ) {
49 global $wgRequest, $wgUser;
50
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', '' );
57
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;
67 }
68
69 function showForm( $err ) {
70 global $wgOut, $wgUser, $wgSysopUserBans, $wgContLang;
71
72 $wgOut->setPagetitle( wfMsg( 'blockip' ) );
73 $wgOut->addWikiText( wfMsg( 'blockiptext' ) );
74
75 if($wgSysopUserBans) {
76 $mIpaddress = Xml::label( wfMsg( 'ipadressorusername' ), 'mw-bi-target' );
77 } else {
78 $mIpaddress = Xml::label( wfMsg( 'ipadress' ), 'mw-bi-target' );
79 }
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' );
86
87 $titleObj = SpecialPage::getTitleFor( 'Blockip' );
88 $action = $titleObj->escapeLocalURL( "action=submit" );
89 $alignRight = $wgContLang->isRtl() ? 'left' : 'right';
90
91 if ( "" != $err ) {
92 $wgOut->setSubtitle( wfMsgHtml( 'formerror' ) );
93 $wgOut->addHTML( "<p class='error'>{$err}</p>\n" );
94 }
95
96 $scBlockExpiryOptions = wfMsgForContent( 'ipboptions' );
97
98 $showblockoptions = $scBlockExpiryOptions != '-';
99 if (!$showblockoptions)
100 $mIpbother = $mIpbexpiry;
101
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);
108 $selected = "";
109 if ($this->BlockExpiry === $value)
110 $selected = ' selected="selected"';
111 $blockExpiryFormOptions .= "<option value=\"$value\"$selected>$show</option>";
112 }
113
114 $scBlockReasonList = wfMsgForContent( 'ipbreason-dropdown' );
115 $blockReasonList = '';
116 if ( $scBlockReasonList != '' && $scBlockReasonList != '-' ) {
117 $blockReasonList = "<option value=\"other\">$mIpbreasonotherlist</option>";
118 $optgroup = "";
119 foreach ( explode( "\n", $scBlockReasonList ) as $option) {
120 $value = trim( htmlspecialchars($option) );
121 if ( $value == '' ) {
122 continue;
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) == '**' ) {
129 // groupmember
130 $selected = "";
131 $value = trim( substr( $value, 2 ) );
132 if ( $this->BlockReasonList === $value)
133 $selected = ' selected="selected"';
134 $blockReasonList .= "<option value=\"$value\"$selected>$value</option>";
135 } else {
136 // groupless block reason
137 $selected = "";
138 if ( $this->BlockReasonList === $value)
139 $selected = ' selected="selected"';
140 $blockReasonList .= "$optgroup<option value=\"$value\"$selected>$value</option>";
141 $optgroup = "";
142 }
143 }
144 $blockReasonList .= $optgroup;
145 }
146
147 $token = $wgUser->editToken();
148
149 global $wgStylePath, $wgStyleVersion;
150 $wgOut->addHTML( "
151 <script type=\"text/javascript\" src=\"$wgStylePath/common/block.js?$wgStyleVersion\">
152 </script>
153 <form id=\"blockip\" method=\"post\" action=\"{$action}\">
154 <table border='0'>
155 <tr>
156 <td align=\"$alignRight\">{$mIpaddress}</td>
157 <td>
158 " . Xml::input( 'wpBlockAddress', 45, $this->BlockAddress,
159 array(
160 'tabindex' => '1',
161 'id' => 'mw-bi-target',
162 'onchange' => 'updateBlockOptions()' ) ) . "
163 </td>
164 </tr>
165 <tr>");
166 if ($showblockoptions) {
167 $wgOut->addHTML("
168 <td align=\"$alignRight\">{$mIpbexpiry}</td>
169 <td>
170 <select tabindex='2' id='wpBlockExpiry' name=\"wpBlockExpiry\" onchange=\"considerChangingExpiryFocus()\">
171 $blockExpiryFormOptions
172 </select>
173 </td>
174 ");
175 }
176 $wgOut->addHTML("
177 </tr>
178 <tr id='wpBlockOther'>
179 <td align=\"$alignRight\">{$mIpbother}</td>
180 <td>
181 " . Xml::input( 'wpBlockOther', 45, $this->BlockOther,
182 array( 'tabindex' => '3', 'id' => 'mw-bi-other' ) ) . "
183 </td>
184 </tr>");
185 if ( $blockReasonList != '' ) {
186 $wgOut->addHTML("
187 <tr>
188 <td align=\"$alignRight\">{$mIpbreasonother}</td>
189 <td>
190 <select tabindex='4' id=\"wpBlockReasonList\" name=\"wpBlockReasonList\">
191 $blockReasonList
192 </select>
193 </td>
194 </tr>");
195 }
196 $wgOut->addHTML("
197 <tr id=\"wpBlockReason\">
198 <td align=\"$alignRight\">{$mIpbreason}</td>
199 <td>
200 " . Xml::input( 'wpBlockReason', 45, $this->BlockReason,
201 array( 'tabindex' => '5', 'id' => 'mw-bi-reason',
202 'maxlength'=> '200' ) ) . "
203 </td>
204 </tr>
205 <tr id='wpAnonOnlyRow'>
206 <td>&nbsp;</td>
207 <td>
208 " . wfCheckLabel( wfMsgHtml( 'ipbanononly' ),
209 'wpAnonOnly', 'wpAnonOnly', $this->BlockAnonOnly,
210 array( 'tabindex' => '6' ) ) . "
211 </td>
212 </tr>
213 <tr id='wpCreateAccountRow'>
214 <td>&nbsp;</td>
215 <td>
216 " . wfCheckLabel( wfMsgHtml( 'ipbcreateaccount' ),
217 'wpCreateAccount', 'wpCreateAccount', $this->BlockCreateAccount,
218 array( 'tabindex' => '7' ) ) . "
219 </td>
220 </tr>
221 <tr id='wpEnableAutoblockRow'>
222 <td>&nbsp;</td>
223 <td>
224 " . wfCheckLabel( wfMsgHtml( 'ipbenableautoblock' ),
225 'wpEnableAutoblock', 'wpEnableAutoblock', $this->BlockEnableAutoblock,
226 array( 'tabindex' => '8' ) ) . "
227 </td>
228 </tr>
229 ");
230
231 global $wgSysopEmailBans;
232 if ( $wgSysopEmailBans && $wgUser->isAllowed( 'blockemail' ) ) {
233 $wgOut->addHTML("
234 <tr id='wpEnableEmailBan'>
235 <td>&nbsp;</td>
236 <td>
237 " . wfCheckLabel( wfMsgHtml( 'ipbemailban' ),
238 'wpEmailBan', 'wpEmailBan', $this->BlockEmail,
239 array( 'tabindex' => '10' )) . "
240 </td>
241 </tr>
242 ");
243 }
244
245 // Allow some users to hide name from block log, blocklist and listusers
246 if ( $wgUser->isAllowed( 'hideuser' ) ) {
247 $wgOut->addHTML("
248 <tr id='wpEnableHideUser'>
249 <td>&nbsp;</td>
250 <td>
251 " . wfCheckLabel( wfMsgHtml( 'ipbhidename' ),
252 'wpHideName', 'wpHideName', $this->BlockHideName,
253 array( 'tabindex' => '9' ) ) . "
254 </td>
255 </tr>
256 ");
257 }
258
259 $wgOut->addHTML("
260 <tr>
261 <td style='padding-top: 1em'>&nbsp;</td>
262 <td style='padding-top: 1em'>
263 " . Xml::submitButton( wfMsg( 'ipbsubmit' ),
264 array( 'name' => 'wpBlock', 'tabindex' => '11' ) ) . "
265 </td>
266 </tr>
267 </table>" .
268 Xml::hidden( 'wpEditToken', $token ) .
269 "</form>
270 <script type=\"text/javascript\">updateBlockOptions()</script>
271 \n" );
272
273 $wgOut->addHtml( $this->getConvenienceLinks() );
274
275 $user = User::newFromName( $this->BlockAddress );
276 if( is_object( $user ) ) {
277 $this->showLogFragment( $wgOut, $user->getUserPage() );
278 } elseif( preg_match( '/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/', $this->BlockAddress ) ) {
279 $this->showLogFragment( $wgOut, Title::makeTitle( NS_USER, $this->BlockAddress ) );
280 } 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 ) ) {
281 $this->showLogFragment( $wgOut, Title::makeTitle( NS_USER, $this->BlockAddress ) );
282 }
283 }
284
285 const BLOCK_SUCCESS = 0; // Success
286 const BLOCK_RANGE_INVALID = 1; // Invalid IP range
287 const BLOCK_RANGE_DISABLED = 2; // Sysops can't block ranges
288 const BLOCK_NONEXISTENT_USER = 3; // No such user
289 const BLOCK_IP_INVALID = 4; // Invalid IP address
290 const BLOCK_EXPIRY_INVALID = 5; // Invalid expiry time
291 const BLOCK_ALREADY_BLOCKED = 6; // User is already blocked
292 /**
293 * Backend block code.
294 * $userID and $expiry will be filled accordingly
295 * Returns one of the BLOCK_* constants
296 */
297 function doBlock(&$userId = null, &$expiry = null)
298 {
299 global $wgUser, $wgSysopUserBans, $wgSysopRangeBans;
300
301 $userId = 0;
302 # Expand valid IPv6 addresses, usernames are left as is
303 $this->BlockAddress = IP::sanitizeIP( $this->BlockAddress );
304 # isIPv4() and IPv6() are used for final validation
305 $rxIP4 = '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}';
306 $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}';
307 $rxIP = "($rxIP4|$rxIP6)";
308
309 # Check for invalid specifications
310 if ( !preg_match( "/^$rxIP$/", $this->BlockAddress ) ) {
311 $matches = array();
312 if ( preg_match( "/^($rxIP4)\\/(\\d{1,2})$/", $this->BlockAddress, $matches ) ) {
313 # IPv4
314 if ( $wgSysopRangeBans ) {
315 if ( !IP::isIPv4( $this->BlockAddress ) || $matches[2] < 16 || $matches[2] > 32 ) {
316 return self::BLOCK_RANGE_INVALID;
317 }
318 $this->BlockAddress = Block::normaliseRange( $this->BlockAddress );
319 } else {
320 # Range block illegal
321 return self::BLOCK_RANGE_DISABLED;
322 }
323 } else if ( preg_match( "/^($rxIP6)\\/(\\d{1,3})$/", $this->BlockAddress, $matches ) ) {
324 # IPv6
325 if ( $wgSysopRangeBans ) {
326 if ( !IP::isIPv6( $this->BlockAddress ) || $matches[2] < 64 || $matches[2] > 128 ) {
327 return self::BLOCK_RANGE_INVALID;
328 }
329 $this->BlockAddress = Block::normaliseRange( $this->BlockAddress );
330 } else {
331 # Range block illegal
332 return self::BLOCK_RANGE_DISABLED;
333 }
334 } else {
335 # Username block
336 if ( $wgSysopUserBans ) {
337 $user = User::newFromName( $this->BlockAddress );
338 if( !is_null( $user ) && $user->getID() ) {
339 # Use canonical name
340 $this->BlockAddress = $user->getName();
341 $userId = $user->getID();
342 } else {
343 return self::BLOCK_NONEXISTENT_USER;
344 }
345 } else {
346 return self::BLOCK_IP_INVALID;
347 }
348 }
349 }
350
351 $reasonstr = $this->BlockReasonList;
352 if ( $reasonstr != 'other' && $this->BlockReason != '') {
353 // Entry from drop down menu + additional comment
354 $reasonstr .= ': ' . $this->BlockReason;
355 } elseif ( $reasonstr == 'other' ) {
356 $reasonstr = $this->BlockReason;
357 }
358
359 $expirestr = $this->BlockExpiry;
360 if( $expirestr == 'other' )
361 $expirestr = $this->BlockOther;
362
363 if (strlen($expirestr) == 0) {
364 return self::BLOCK_EXPIRY_INVALID;
365 }
366
367 if ( $expirestr == 'infinite' || $expirestr == 'indefinite' ) {
368 $expiry = Block::infinity();
369 } else {
370 # Convert GNU-style date, on error returns -1 for PHP <5.1 and false for PHP >=5.1
371 $expiry = strtotime( $expirestr );
372
373 if ( $expiry < 0 || $expiry === false ) {
374 return self::BLOCK_EXPIRY_INVALID;
375 }
376
377 $expiry = wfTimestamp( TS_MW, $expiry );
378 }
379
380 # Create block
381 # Note: for a user block, ipb_address is only for display purposes
382 $block = new Block( $this->BlockAddress, $userId, $wgUser->getID(),
383 $reasonstr, wfTimestampNow(), 0, $expiry, $this->BlockAnonOnly,
384 $this->BlockCreateAccount, $this->BlockEnableAutoblock, $this->BlockHideName,
385 $this->BlockEmail);
386
387 if (wfRunHooks('BlockIp', array(&$block, &$wgUser))) {
388
389 if ( !$block->insert() ) {
390 return self::BLOCK_ALREADY_BLOCKED;
391 }
392
393 wfRunHooks('BlockIpComplete', array($block, $wgUser));
394
395 # Prepare log parameters
396 $logParams = array();
397 $logParams[] = $expirestr;
398 $logParams[] = $this->blockLogFlags();
399
400 # Make log entry, if the name is hidden, put it in the oversight log
401 $log_type = ($this->BlockHideName) ? 'oversight' : 'block';
402 $log = new LogPage( $log_type );
403 $log->addEntry( 'block', Title::makeTitle( NS_USER, $this->BlockAddress ),
404 $reasonstr, $logParams );
405
406 # Report to the user
407 return self::BLOCK_SUCCESS;
408 }
409 }
410
411 /**
412 * UI entry point for blocking
413 * Wraps around doBlock()
414 */
415 function doSubmit()
416 {
417 global $wgOut;
418 $retval = $this->doBlock();
419 switch($retval)
420 {
421 case self::BLOCK_RANGE_INVALID:
422 $this->showForm( wfMsg( 'ip_range_invalid' ) );
423 return;
424 case self::BLOCK_RANGE_DISABLED:
425 $this->showForm( wfMsg( 'range_block_disabled' ) );
426 return;
427 case self::BLOCK_NONEXISTENT_USER:
428 $this->showForm( wfMsg( 'nosuchusershort', htmlspecialchars( $this->BlockAddress ) ) );
429 return;
430 case self::BLOCK_IP_INVALID:
431 $this->showForm( wfMsg( 'badipaddress' ) );
432 return;
433 case self::BLOCK_EXPIRY_INVALID:
434 $this->showForm( wfMsg( 'ipb_expiry_invalid' ) );
435 return;
436 case self::BLOCK_ALREADY_BLOCKED:
437 $this->showForm( wfMsg( 'ipb_already_blocked', htmlspecialchars( $this->BlockAddress ) ) );
438 return;
439 case self::BLOCK_SUCCESS:
440 $titleObj = SpecialPage::getTitleFor( 'Blockip' );
441 $wgOut->redirect( $titleObj->getFullURL( 'action=success&ip=' .
442 urlencode( $this->BlockAddress ) ) );
443 return;
444 default:
445 throw new MWException( __METHOD__ . ": Unknown return value ``{$retval}''" );
446 }
447 }
448
449 function showSuccess() {
450 global $wgOut;
451
452 $wgOut->setPagetitle( wfMsg( 'blockip' ) );
453 $wgOut->setSubtitle( wfMsg( 'blockipsuccesssub' ) );
454 $text = wfMsg( 'blockipsuccesstext', $this->BlockAddress );
455 $wgOut->addWikiText( $text );
456 }
457
458 function showLogFragment( $out, $title ) {
459 $out->addHtml( wfElement( 'h2', NULL, LogPage::logName( 'block' ) ) );
460 $request = new FauxRequest( array( 'page' => $title->getPrefixedText(), 'type' => 'block' ) );
461 $viewer = new LogViewer( new LogReader( $request ) );
462 $viewer->showList( $out );
463 }
464
465 /**
466 * Return a comma-delimited list of "flags" to be passed to the log
467 * reader for this block, to provide more information in the logs
468 *
469 * @return array
470 */
471 private function blockLogFlags() {
472 $flags = array();
473 if( $this->BlockAnonOnly && IP::isIPAddress( $this->BlockAddress ) )
474 // when blocking a user the option 'anononly' is not available/has no effect -> do not write this into log
475 $flags[] = 'anononly';
476 if( $this->BlockCreateAccount )
477 $flags[] = 'nocreate';
478 if( !$this->BlockEnableAutoblock )
479 $flags[] = 'noautoblock';
480 if ( $this->BlockEmail )
481 $flags[] = 'noemail';
482 return implode( ',', $flags );
483 }
484
485 /**
486 * Builds unblock and block list links
487 *
488 * @return string
489 */
490 private function getConvenienceLinks() {
491 global $wgUser;
492 $skin = $wgUser->getSkin();
493 $links[] = $skin->makeLink ( 'MediaWiki:ipbreason-dropdown', wfMsgHtml( 'ipb-edit-dropdown' ) );
494 $links[] = $this->getUnblockLink( $skin );
495 $links[] = $this->getBlockListLink( $skin );
496 return '<p class="mw-ipb-conveniencelinks">' . implode( ' | ', $links ) . '</p>';
497 }
498
499 /**
500 * Build a convenient link to unblock the given username or IP
501 * address, if available; otherwise link to a blank unblock
502 * form
503 *
504 * @param $skin Skin to use
505 * @return string
506 */
507 private function getUnblockLink( $skin ) {
508 $list = SpecialPage::getTitleFor( 'Ipblocklist' );
509 if( $this->BlockAddress ) {
510 $addr = htmlspecialchars( strtr( $this->BlockAddress, '_', ' ' ) );
511 return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-unblock-addr', $addr ),
512 'action=unblock&ip=' . urlencode( $this->BlockAddress ) );
513 } else {
514 return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-unblock' ), 'action=unblock' );
515 }
516 }
517
518 /**
519 * Build a convenience link to the block list
520 *
521 * @param $skin Skin to use
522 * @return string
523 */
524 private function getBlockListLink( $skin ) {
525 $list = SpecialPage::getTitleFor( 'Ipblocklist' );
526 if( $this->BlockAddress ) {
527 $addr = htmlspecialchars( strtr( $this->BlockAddress, '_', ' ' ) );
528 return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-blocklist-addr', $addr ),
529 'ip=' . urlencode( $this->BlockAddress ) );
530 } else {
531 return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-blocklist' ) );
532 }
533 }
534 }
535