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