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