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