Introduce default expiry time for special:block (bug 32178)
[lhc/web/wiklou.git] / includes / specials / SpecialBlock.php
1 <?php
2 /**
3 * Implements Special:Block
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup SpecialPage
22 */
23
24 /**
25 * A special page that allows users with 'block' right to block users from
26 * editing pages and other actions
27 *
28 * @ingroup SpecialPage
29 */
30 class SpecialBlock extends FormSpecialPage {
31 /** The maximum number of edits a user can have and still be hidden
32 * TODO: config setting? */
33 const HIDEUSER_CONTRIBLIMIT = 1000;
34
35 /** @var User user to be blocked, as passed either by parameter (url?wpTarget=Foo)
36 * or as subpage (Special:Block/Foo) */
37 protected $target;
38
39 /// @var Block::TYPE_ constant
40 protected $type;
41
42 /// @var User|String the previous block target
43 protected $previousTarget;
44
45 /// @var Bool whether the previous submission of the form asked for HideUser
46 protected $requestedHideUser;
47
48 /// @var Bool
49 protected $alreadyBlocked;
50
51 /// @var Array
52 protected $preErrors = array();
53
54 public function __construct() {
55 parent::__construct( 'Block', 'block' );
56 }
57
58 /**
59 * Checks that the user can unblock themselves if they are trying to do so
60 *
61 * @param User $user
62 * @throws ErrorPageError
63 */
64 protected function checkExecutePermissions( User $user ) {
65 parent::checkExecutePermissions( $user );
66
67 # bug 15810: blocked admins should have limited access here
68 $status = self::checkUnblockSelf( $this->target, $user );
69 if ( $status !== true ) {
70 throw new ErrorPageError( 'badaccess', $status );
71 }
72 }
73
74 /**
75 * Handle some magic here
76 *
77 * @param $par String
78 */
79 protected function setParameter( $par ) {
80 # Extract variables from the request. Try not to get into a situation where we
81 # need to extract *every* variable from the form just for processing here, but
82 # there are legitimate uses for some variables
83 $request = $this->getRequest();
84 list( $this->target, $this->type ) = self::getTargetAndType( $par, $request );
85 if ( $this->target instanceof User ) {
86 # Set the 'relevant user' in the skin, so it displays links like Contributions,
87 # User logs, UserRights, etc.
88 $this->getSkin()->setRelevantUser( $this->target );
89 }
90
91 list( $this->previousTarget, /*...*/ ) = Block::parseTarget( $request->getVal( 'wpPreviousTarget' ) );
92 $this->requestedHideUser = $request->getBool( 'wpHideUser' );
93 }
94
95 /**
96 * Customizes the HTMLForm a bit
97 *
98 * @param $form HTMLForm
99 */
100 protected function alterForm( HTMLForm $form ) {
101 $form->setWrapperLegendMsg( 'blockip-legend' );
102 $form->setHeaderText( '' );
103 $form->setSubmitCallback( array( __CLASS__, 'processUIForm' ) );
104
105 $msg = $this->alreadyBlocked ? 'ipb-change-block' : 'ipbsubmit';
106 $form->setSubmitTextMsg( $msg );
107
108 # Don't need to do anything if the form has been posted
109 if( !$this->getRequest()->wasPosted() && $this->preErrors ){
110 $s = HTMLForm::formatErrors( $this->preErrors );
111 if( $s ){
112 $form->addHeaderText( Html::rawElement(
113 'div',
114 array( 'class' => 'error' ),
115 $s
116 ) );
117 }
118 }
119 }
120
121 /**
122 * Get the HTMLForm descriptor array for the block form
123 * @return Array
124 */
125 protected function getFormFields(){
126 global $wgBlockAllowsUTEdit;
127
128 $user = $this->getUser();
129
130 $a = array(
131 'Target' => array(
132 'type' => 'text',
133 'label-message' => 'ipadressorusername',
134 'tabindex' => '1',
135 'id' => 'mw-bi-target',
136 'size' => '45',
137 'required' => true,
138 'validation-callback' => array( __CLASS__, 'validateTargetField' ),
139 ),
140 'Expiry' => array(
141 'type' => !count( self::getSuggestedDurations() ) ? 'text' : 'selectorother',
142 'label-message' => 'ipbexpiry',
143 'required' => true,
144 'tabindex' => '2',
145 'options' => self::getSuggestedDurations(),
146 'other' => $this->msg( 'ipbother' )->text(),
147 'default' => $this->msg( 'ipb-default-expiry' )->inContentLanguage()->text(),
148 ),
149 'Reason' => array(
150 'type' => 'selectandother',
151 'label-message' => 'ipbreason',
152 'options-message' => 'ipbreason-dropdown',
153 ),
154 'CreateAccount' => array(
155 'type' => 'check',
156 'label-message' => 'ipbcreateaccount',
157 'default' => true,
158 ),
159 );
160
161 if( self::canBlockEmail( $user ) ) {
162 $a['DisableEmail'] = array(
163 'type' => 'check',
164 'label-message' => 'ipbemailban',
165 );
166 }
167
168 if( $wgBlockAllowsUTEdit ){
169 $a['DisableUTEdit'] = array(
170 'type' => 'check',
171 'label-message' => 'ipb-disableusertalk',
172 'default' => false,
173 );
174 }
175
176 $a['AutoBlock'] = array(
177 'type' => 'check',
178 'label-message' => 'ipbenableautoblock',
179 'default' => true,
180 );
181
182 # Allow some users to hide name from block log, blocklist and listusers
183 if( $user->isAllowed( 'hideuser' ) ) {
184 $a['HideUser'] = array(
185 'type' => 'check',
186 'label-message' => 'ipbhidename',
187 'cssclass' => 'mw-block-hideuser',
188 );
189 }
190
191 # Watchlist their user page? (Only if user is logged in)
192 if( $user->isLoggedIn() ) {
193 $a['Watch'] = array(
194 'type' => 'check',
195 'label-message' => 'ipbwatchuser',
196 );
197 }
198
199 $a['HardBlock'] = array(
200 'type' => 'check',
201 'label-message' => 'ipb-hardblock',
202 'default' => false,
203 );
204
205 # This is basically a copy of the Target field, but the user can't change it, so we
206 # can see if the warnings we maybe showed to the user before still apply
207 $a['PreviousTarget'] = array(
208 'type' => 'hidden',
209 'default' => false,
210 );
211
212 # We'll turn this into a checkbox if we need to
213 $a['Confirm'] = array(
214 'type' => 'hidden',
215 'default' => '',
216 'label-message' => 'ipb-confirm',
217 );
218
219 $this->maybeAlterFormDefaults( $a );
220
221 return $a;
222 }
223
224 /**
225 * If the user has already been blocked with similar settings, load that block
226 * and change the defaults for the form fields to match the existing settings.
227 * @param $fields Array HTMLForm descriptor array
228 * @return Bool whether fields were altered (that is, whether the target is
229 * already blocked)
230 */
231 protected function maybeAlterFormDefaults( &$fields ){
232 # This will be overwritten by request data
233 $fields['Target']['default'] = (string)$this->target;
234
235 # This won't be
236 $fields['PreviousTarget']['default'] = (string)$this->target;
237
238 $block = Block::newFromTarget( $this->target );
239
240 if( $block instanceof Block && !$block->mAuto # The block exists and isn't an autoblock
241 && ( $this->type != Block::TYPE_RANGE # The block isn't a rangeblock
242 || $block->getTarget() == $this->target ) # or if it is, the range is what we're about to block
243 )
244 {
245 $fields['HardBlock']['default'] = $block->isHardblock();
246 $fields['CreateAccount']['default'] = $block->prevents( 'createaccount' );
247 $fields['AutoBlock']['default'] = $block->isAutoblocking();
248
249 if( isset( $fields['DisableEmail'] ) ){
250 $fields['DisableEmail']['default'] = $block->prevents( 'sendemail' );
251 }
252
253 if( isset( $fields['HideUser'] ) ){
254 $fields['HideUser']['default'] = $block->mHideName;
255 }
256
257 if( isset( $fields['DisableUTEdit'] ) ){
258 $fields['DisableUTEdit']['default'] = $block->prevents( 'editownusertalk' );
259 }
260
261 $fields['Reason']['default'] = $block->mReason;
262
263 if( $this->getRequest()->wasPosted() ){
264 # Ok, so we got a POST submission asking us to reblock a user. So show the
265 # confirm checkbox; the user will only see it if they haven't previously
266 $fields['Confirm']['type'] = 'check';
267 } else {
268 # We got a target, but it wasn't a POST request, so the user must have gone
269 # to a link like [[Special:Block/User]]. We don't need to show the checkbox
270 # as long as they go ahead and block *that* user
271 $fields['Confirm']['default'] = 1;
272 }
273
274 if( $block->mExpiry == 'infinity' ) {
275 $fields['Expiry']['default'] = 'infinite';
276 } else {
277 $fields['Expiry']['default'] = wfTimestamp( TS_RFC2822, $block->mExpiry );
278 }
279
280 $this->alreadyBlocked = true;
281 $this->preErrors[] = array( 'ipb-needreblock', wfEscapeWikiText( (string)$block->getTarget() ) );
282 }
283
284 # We always need confirmation to do HideUser
285 if( $this->requestedHideUser ){
286 $fields['Confirm']['type'] = 'check';
287 unset( $fields['Confirm']['default'] );
288 $this->preErrors[] = 'ipb-confirmhideuser';
289 }
290
291 # Or if the user is trying to block themselves
292 if( (string)$this->target === $this->getUser()->getName() ){
293 $fields['Confirm']['type'] = 'check';
294 unset( $fields['Confirm']['default'] );
295 $this->preErrors[] = 'ipb-blockingself';
296 }
297 }
298
299 /**
300 * Add header elements like block log entries, etc.
301 * @return String
302 */
303 protected function preText(){
304 $text = $this->msg( 'blockiptext' )->parse();
305
306 $otherBlockMessages = array();
307 if( $this->target !== null ) {
308 # Get other blocks, i.e. from GlobalBlocking or TorBlock extension
309 wfRunHooks( 'OtherBlockLogLink', array( &$otherBlockMessages, $this->target ) );
310
311 if( count( $otherBlockMessages ) ) {
312 $s = Html::rawElement(
313 'h2',
314 array(),
315 $this->msg( 'ipb-otherblocks-header', count( $otherBlockMessages ) )->parse()
316 ) . "\n";
317
318 $list = '';
319
320 foreach( $otherBlockMessages as $link ) {
321 $list .= Html::rawElement( 'li', array(), $link ) . "\n";
322 }
323
324 $s .= Html::rawElement(
325 'ul',
326 array( 'class' => 'mw-blockip-alreadyblocked' ),
327 $list
328 ) . "\n";
329
330 $text .= $s;
331 }
332 }
333
334 return $text;
335 }
336
337 /**
338 * Add footer elements to the form
339 * @return string
340 */
341 protected function postText(){
342 $links = array();
343
344 # Link to the user's contributions, if applicable
345 if( $this->target instanceof User ){
346 $contribsPage = SpecialPage::getTitleFor( 'Contributions', $this->target->getName() );
347 $links[] = Linker::link(
348 $contribsPage,
349 $this->msg( 'ipb-blocklist-contribs', $this->target->getName() )->escaped()
350 );
351 }
352
353 # Link to unblock the specified user, or to a blank unblock form
354 if( $this->target instanceof User ) {
355 $message = $this->msg( 'ipb-unblock-addr', wfEscapeWikiText( $this->target->getName() ) )->parse();
356 $list = SpecialPage::getTitleFor( 'Unblock', $this->target->getName() );
357 } else {
358 $message = $this->msg( 'ipb-unblock' )->parse();
359 $list = SpecialPage::getTitleFor( 'Unblock' );
360 }
361 $links[] = Linker::linkKnown( $list, $message, array() );
362
363 # Link to the block list
364 $links[] = Linker::linkKnown(
365 SpecialPage::getTitleFor( 'BlockList' ),
366 $this->msg( 'ipb-blocklist' )->escaped()
367 );
368
369 $user = $this->getUser();
370
371 # Link to edit the block dropdown reasons, if applicable
372 if ( $user->isAllowed( 'editinterface' ) ) {
373 $links[] = Linker::link(
374 Title::makeTitle( NS_MEDIAWIKI, 'Ipbreason-dropdown' ),
375 $this->msg( 'ipb-edit-dropdown' )->escaped(),
376 array(),
377 array( 'action' => 'edit' )
378 );
379 }
380
381 $text = Html::rawElement(
382 'p',
383 array( 'class' => 'mw-ipb-conveniencelinks' ),
384 $this->getLanguage()->pipeList( $links )
385 );
386
387 $userTitle = self::getTargetUserTitle( $this->target );
388 if( $userTitle ){
389 # Get relevant extracts from the block and suppression logs, if possible
390 $out = '';
391
392 LogEventsList::showLogExtract(
393 $out,
394 'block',
395 $userTitle,
396 '',
397 array(
398 'lim' => 10,
399 'msgKey' => array( 'blocklog-showlog', $userTitle->getText() ),
400 'showIfEmpty' => false
401 )
402 );
403 $text .= $out;
404
405 # Add suppression block entries if allowed
406 if( $user->isAllowed( 'suppressionlog' ) ) {
407 LogEventsList::showLogExtract(
408 $out,
409 'suppress',
410 $userTitle,
411 '',
412 array(
413 'lim' => 10,
414 'conds' => array( 'log_action' => array( 'block', 'reblock', 'unblock' ) ),
415 'msgKey' => array( 'blocklog-showsuppresslog', $userTitle->getText() ),
416 'showIfEmpty' => false
417 )
418 );
419
420 $text .= $out;
421 }
422 }
423
424 return $text;
425 }
426
427 /**
428 * Get a user page target for things like logs.
429 * This handles account and IP range targets.
430 * @param $target User|string
431 * @return Title|null
432 */
433 protected static function getTargetUserTitle( $target ) {
434 if( $target instanceof User ) {
435 return $target->getUserPage();
436 } elseif ( IP::isIPAddress( $target ) ) {
437 return Title::makeTitleSafe( NS_USER, $target );
438 }
439 return null;
440 }
441
442 /**
443 * Determine the target of the block, and the type of target
444 * TODO: should be in Block.php?
445 * @param $par String subpage parameter passed to setup, or data value from
446 * the HTMLForm
447 * @param $request WebRequest optionally try and get data from a request too
448 * @return array( User|string|null, Block::TYPE_ constant|null )
449 */
450 public static function getTargetAndType( $par, WebRequest $request = null ){
451 $i = 0;
452 $target = null;
453
454 while( true ){
455 switch( $i++ ){
456 case 0:
457 # The HTMLForm will check wpTarget first and only if it doesn't get
458 # a value use the default, which will be generated from the options
459 # below; so this has to have a higher precedence here than $par, or
460 # we could end up with different values in $this->target and the HTMLForm!
461 if( $request instanceof WebRequest ){
462 $target = $request->getText( 'wpTarget', null );
463 }
464 break;
465 case 1:
466 $target = $par;
467 break;
468 case 2:
469 if( $request instanceof WebRequest ){
470 $target = $request->getText( 'ip', null );
471 }
472 break;
473 case 3:
474 # B/C @since 1.18
475 if( $request instanceof WebRequest ){
476 $target = $request->getText( 'wpBlockAddress', null );
477 }
478 break;
479 case 4:
480 break 2;
481 }
482
483 list( $target, $type ) = Block::parseTarget( $target );
484
485 if( $type !== null ){
486 return array( $target, $type );
487 }
488 }
489
490 return array( null, null );
491 }
492
493 /**
494 * HTMLForm field validation-callback for Target field.
495 * @since 1.18
496 * @param $value String
497 * @param $alldata Array
498 * @param $form HTMLForm
499 * @return Message
500 */
501 public static function validateTargetField( $value, $alldata, $form ) {
502 global $wgBlockCIDRLimit;
503
504 list( $target, $type ) = self::getTargetAndType( $value );
505
506 if( $type == Block::TYPE_USER ){
507 # TODO: why do we not have a User->exists() method?
508 if( !$target->getId() ){
509 return $form->msg( 'nosuchusershort',
510 wfEscapeWikiText( $target->getName() ) );
511 }
512
513 $status = self::checkUnblockSelf( $target, $form->getUser() );
514 if ( $status !== true ) {
515 return $form->msg( 'badaccess', $status );
516 }
517
518 } elseif( $type == Block::TYPE_RANGE ){
519 list( $ip, $range ) = explode( '/', $target, 2 );
520
521 if( ( IP::isIPv4( $ip ) && $wgBlockCIDRLimit['IPv4'] == 32 )
522 || ( IP::isIPv6( $ip ) && $wgBlockCIDRLimit['IPv6'] == 128 ) )
523 {
524 # Range block effectively disabled
525 return $form->msg( 'range_block_disabled' );
526 }
527
528 if( ( IP::isIPv4( $ip ) && $range > 32 )
529 || ( IP::isIPv6( $ip ) && $range > 128 ) )
530 {
531 # Dodgy range
532 return $form->msg( 'ip_range_invalid' );
533 }
534
535 if( IP::isIPv4( $ip ) && $range < $wgBlockCIDRLimit['IPv4'] ) {
536 return $form->msg( 'ip_range_toolarge', $wgBlockCIDRLimit['IPv4'] );
537 }
538
539 if( IP::isIPv6( $ip ) && $range < $wgBlockCIDRLimit['IPv6'] ) {
540 return $form->msg( 'ip_range_toolarge', $wgBlockCIDRLimit['IPv6'] );
541 }
542 } elseif( $type == Block::TYPE_IP ){
543 # All is well
544 } else {
545 return $form->msg( 'badipaddress' );
546 }
547
548 return true;
549 }
550
551 /**
552 * Submit callback for an HTMLForm object, will simply pass
553 * @param $data array
554 * @param $form HTMLForm
555 * @return Bool|String
556 */
557 public static function processUIForm( array $data, HTMLForm $form ) {
558 return self::processForm( $data, $form->getContext() );
559 }
560
561 /**
562 * Given the form data, actually implement a block
563 * @param $data Array
564 * @param $context IContextSource
565 * @return Bool|String
566 */
567 public static function processForm( array $data, IContextSource $context ){
568 global $wgBlockAllowsUTEdit;
569
570 $performer = $context->getUser();
571
572 // Handled by field validator callback
573 // self::validateTargetField( $data['Target'] );
574
575 # This might have been a hidden field or a checkbox, so interesting data
576 # can come from it
577 $data['Confirm'] = !in_array( $data['Confirm'], array( '', '0', null, false ), true );
578
579 list( $target, $type ) = self::getTargetAndType( $data['Target'] );
580 if( $type == Block::TYPE_USER ){
581 $user = $target;
582 $target = $user->getName();
583 $userId = $user->getId();
584
585 # Give admins a heads-up before they go and block themselves. Much messier
586 # to do this for IPs, but it's pretty unlikely they'd ever get the 'block'
587 # permission anyway, although the code does allow for it.
588 # Note: Important to use $target instead of $data['Target']
589 # since both $data['PreviousTarget'] and $target are normalized
590 # but $data['target'] gets overriden by (non-normalized) request variable
591 # from previous request.
592 if( $target === $performer->getName() &&
593 ( $data['PreviousTarget'] !== $target || !$data['Confirm'] ) )
594 {
595 return array( 'ipb-blockingself' );
596 }
597 } elseif( $type == Block::TYPE_RANGE ){
598 $userId = 0;
599 } elseif( $type == Block::TYPE_IP ){
600 $target = $target->getName();
601 $userId = 0;
602 } else {
603 # This should have been caught in the form field validation
604 return array( 'badipaddress' );
605 }
606
607 if( ( strlen( $data['Expiry'] ) == 0) || ( strlen( $data['Expiry'] ) > 50 )
608 || !self::parseExpiryInput( $data['Expiry'] ) )
609 {
610 return array( 'ipb_expiry_invalid' );
611 }
612
613 if( !isset( $data['DisableEmail'] ) ){
614 $data['DisableEmail'] = false;
615 }
616
617 # If the user has done the form 'properly', they won't even have been given the
618 # option to suppress-block unless they have the 'hideuser' permission
619 if( !isset( $data['HideUser'] ) ){
620 $data['HideUser'] = false;
621 }
622
623 if( $data['HideUser'] ) {
624 if( !$performer->isAllowed('hideuser') ){
625 # this codepath is unreachable except by a malicious user spoofing forms,
626 # or by race conditions (user has oversight and sysop, loads block form,
627 # and is de-oversighted before submission); so need to fail completely
628 # rather than just silently disable hiding
629 return array( 'badaccess-group0' );
630 }
631
632 # Recheck params here...
633 if( $type != Block::TYPE_USER ) {
634 $data['HideUser'] = false; # IP users should not be hidden
635 } elseif( !in_array( $data['Expiry'], array( 'infinite', 'infinity', 'indefinite' ) ) ) {
636 # Bad expiry.
637 return array( 'ipb_expiry_temp' );
638 } elseif( $user->getEditCount() > self::HIDEUSER_CONTRIBLIMIT ) {
639 # Typically, the user should have a handful of edits.
640 # Disallow hiding users with many edits for performance.
641 return array( 'ipb_hide_invalid' );
642 } elseif( !$data['Confirm'] ){
643 return array( 'ipb-confirmhideuser' );
644 }
645 }
646
647 # Create block object.
648 $block = new Block();
649 $block->setTarget( $target );
650 $block->setBlocker( $performer );
651 $block->mReason = $data['Reason'][0];
652 $block->mExpiry = self::parseExpiryInput( $data['Expiry'] );
653 $block->prevents( 'createaccount', $data['CreateAccount'] );
654 $block->prevents( 'editownusertalk', ( !$wgBlockAllowsUTEdit || $data['DisableUTEdit'] ) );
655 $block->prevents( 'sendemail', $data['DisableEmail'] );
656 $block->isHardblock( $data['HardBlock'] );
657 $block->isAutoblocking( $data['AutoBlock'] );
658 $block->mHideName = $data['HideUser'];
659
660 if( !wfRunHooks( 'BlockIp', array( &$block, &$performer ) ) ) {
661 return array( 'hookaborted' );
662 }
663
664 # Try to insert block. Is there a conflicting block?
665 $status = $block->insert();
666 if( !$status ) {
667 # Show form unless the user is already aware of this...
668 if( !$data['Confirm'] || ( array_key_exists( 'PreviousTarget', $data )
669 && $data['PreviousTarget'] !== $target ) )
670 {
671 return array( array( 'ipb_already_blocked', $block->getTarget() ) );
672 # Otherwise, try to update the block...
673 } else {
674 # This returns direct blocks before autoblocks/rangeblocks, since we should
675 # be sure the user is blocked by now it should work for our purposes
676 $currentBlock = Block::newFromTarget( $target );
677
678 if( $block->equals( $currentBlock ) ) {
679 return array( array( 'ipb_already_blocked', $block->getTarget() ) );
680 }
681
682 # If the name was hidden and the blocking user cannot hide
683 # names, then don't allow any block changes...
684 if( $currentBlock->mHideName && !$performer->isAllowed( 'hideuser' ) ) {
685 return array( 'cant-see-hidden-user' );
686 }
687
688 $currentBlock->delete();
689 $status = $block->insert();
690 $logaction = 'reblock';
691
692 # Unset _deleted fields if requested
693 if( $currentBlock->mHideName && !$data['HideUser'] ) {
694 RevisionDeleteUser::unsuppressUserName( $target, $userId );
695 }
696
697 # If hiding/unhiding a name, this should go in the private logs
698 if( (bool)$currentBlock->mHideName ){
699 $data['HideUser'] = true;
700 }
701 }
702 } else {
703 $logaction = 'block';
704 }
705
706 wfRunHooks( 'BlockIpComplete', array( $block, $performer ) );
707
708 # Set *_deleted fields if requested
709 if( $data['HideUser'] ) {
710 RevisionDeleteUser::suppressUserName( $target, $userId );
711 }
712
713 # Can't watch a rangeblock
714 if( $type != Block::TYPE_RANGE && $data['Watch'] ) {
715 $performer->addWatch( Title::makeTitle( NS_USER, $target ) );
716 }
717
718 # Block constructor sanitizes certain block options on insert
719 $data['BlockEmail'] = $block->prevents( 'sendemail' );
720 $data['AutoBlock'] = $block->isAutoblocking();
721
722 # Prepare log parameters
723 $logParams = array();
724 $logParams[] = $data['Expiry'];
725 $logParams[] = self::blockLogFlags( $data, $type );
726
727 # Make log entry, if the name is hidden, put it in the oversight log
728 $log_type = $data['HideUser'] ? 'suppress' : 'block';
729 $log = new LogPage( $log_type );
730 $log_id = $log->addEntry(
731 $logaction,
732 Title::makeTitle( NS_USER, $target ),
733 $data['Reason'][0],
734 $logParams
735 );
736 # Relate log ID to block IDs (bug 25763)
737 $blockIds = array_merge( array( $status['id'] ), $status['autoIds'] );
738 $log->addRelations( 'ipb_id', $blockIds, $log_id );
739
740 # Report to the user
741 return true;
742 }
743
744 /**
745 * Get an array of suggested block durations from MediaWiki:Ipboptions
746 * @todo FIXME: This uses a rather odd syntax for the options, should it be converted
747 * to the standard "**<duration>|<displayname>" format?
748 * @param $lang Language|null the language to get the durations in, or null to use
749 * the wiki's content language
750 * @return Array
751 */
752 public static function getSuggestedDurations( $lang = null ){
753 $a = array();
754 $msg = $lang === null
755 ? wfMessage( 'ipboptions' )->inContentLanguage()->text()
756 : wfMessage( 'ipboptions' )->inLanguage( $lang )->text();
757
758 if( $msg == '-' ){
759 return array();
760 }
761
762 foreach( explode( ',', $msg ) as $option ) {
763 if( strpos( $option, ':' ) === false ){
764 $option = "$option:$option";
765 }
766
767 list( $show, $value ) = explode( ':', $option );
768 $a[htmlspecialchars( $show )] = htmlspecialchars( $value );
769 }
770
771 return $a;
772 }
773
774 /**
775 * Convert a submitted expiry time, which may be relative ("2 weeks", etc) or absolute
776 * ("24 May 2034", etc), into an absolute timestamp we can put into the database.
777 * @param $expiry String: whatever was typed into the form
778 * @return String: timestamp or "infinity" string for the DB implementation
779 */
780 public static function parseExpiryInput( $expiry ) {
781 static $infinity;
782 if( $infinity == null ){
783 $infinity = wfGetDB( DB_SLAVE )->getInfinity();
784 }
785
786 if ( $expiry == 'infinite' || $expiry == 'indefinite' ) {
787 $expiry = $infinity;
788 } else {
789 $expiry = strtotime( $expiry );
790
791 if ( $expiry < 0 || $expiry === false ) {
792 return false;
793 }
794
795 $expiry = wfTimestamp( TS_MW, $expiry );
796 }
797
798 return $expiry;
799 }
800
801 /**
802 * Can we do an email block?
803 * @param $user User: the sysop wanting to make a block
804 * @return Boolean
805 */
806 public static function canBlockEmail( $user ) {
807 global $wgEnableUserEmail, $wgSysopEmailBans;
808
809 return ( $wgEnableUserEmail && $wgSysopEmailBans && $user->isAllowed( 'blockemail' ) );
810 }
811
812 /**
813 * bug 15810: blocked admins should not be able to block/unblock
814 * others, and probably shouldn't be able to unblock themselves
815 * either.
816 * @param $user User|Int|String
817 * @param $performer User user doing the request
818 * @return Bool|String true or error message key
819 */
820 public static function checkUnblockSelf( $user, User $performer ) {
821 if ( is_int( $user ) ) {
822 $user = User::newFromId( $user );
823 } elseif ( is_string( $user ) ) {
824 $user = User::newFromName( $user );
825 }
826
827 if( $performer->isBlocked() ){
828 if( $user instanceof User && $user->getId() == $performer->getId() ) {
829 # User is trying to unblock themselves
830 if ( $performer->isAllowed( 'unblockself' ) ) {
831 return true;
832 # User blocked themselves and is now trying to reverse it
833 } elseif ( $performer->blockedBy() === $performer->getName() ) {
834 return true;
835 } else {
836 return 'ipbnounblockself';
837 }
838 } else {
839 # User is trying to block/unblock someone else
840 return 'ipbblocked';
841 }
842 } else {
843 return true;
844 }
845 }
846
847 /**
848 * Return a comma-delimited list of "flags" to be passed to the log
849 * reader for this block, to provide more information in the logs
850 * @param $data Array from HTMLForm data
851 * @param $type Block::TYPE_ constant (USER, RANGE, or IP)
852 * @return array
853 */
854 protected static function blockLogFlags( array $data, $type ) {
855 global $wgBlockAllowsUTEdit;
856 $flags = array();
857
858 # when blocking a user the option 'anononly' is not available/has no effect -> do not write this into log
859 if( !$data['HardBlock'] && $type != Block::TYPE_USER ){
860 // For grepping: message block-log-flags-anononly
861 $flags[] = 'anononly';
862 }
863
864 if( $data['CreateAccount'] ){
865 // For grepping: message block-log-flags-nocreate
866 $flags[] = 'nocreate';
867 }
868
869 # Same as anononly, this is not displayed when blocking an IP address
870 if( !$data['AutoBlock'] && $type == Block::TYPE_USER ){
871 // For grepping: message block-log-flags-noautoblock
872 $flags[] = 'noautoblock';
873 }
874
875 if( $data['DisableEmail'] ){
876 // For grepping: message block-log-flags-noemail
877 $flags[] = 'noemail';
878 }
879
880 if( $wgBlockAllowsUTEdit && $data['DisableUTEdit'] ){
881 // For grepping: message block-log-flags-nousertalk
882 $flags[] = 'nousertalk';
883 }
884
885 if( $data['HideUser'] ){
886 // For grepping: message block-log-flags-hiddenname
887 $flags[] = 'hiddenname';
888 }
889
890 return implode( ',', $flags );
891 }
892
893 /**
894 * Process the form on POST submission.
895 * @param $data Array
896 * @return Bool|Array true for success, false for didn't-try, array of errors on failure
897 */
898 public function onSubmit( array $data ) {
899 // This isn't used since we need that HTMLForm that's passed in the
900 // second parameter. See alterForm for the real function
901 }
902
903 /**
904 * Do something exciting on successful processing of the form, most likely to show a
905 * confirmation message
906 */
907 public function onSuccess() {
908 $out = $this->getOutput();
909 $out->setPageTitle( $this->msg( 'blockipsuccesssub' ) );
910 $out->addWikiMsg( 'blockipsuccesstext', wfEscapeWikiText( $this->target ) );
911 }
912 }
913
914 # BC @since 1.18
915 class IPBlockForm extends SpecialBlock {}