* Use local context instead of global variables (where possible)
[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 $out = $this->getOutput();
91 $out->setPageTitle( wfMsg( 'blockip-title' ) );
92 $out->addModules( 'mediawiki.special', 'mediawiki.special.block' );
93
94 $fields = $this->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 $out->setPageTitle( wfMsg( 'blockipsuccesssub' ) );
112 $out->addWikiMsg( 'blockipsuccesstext', $this->target );
113 }
114 }
115
116 /**
117 * Get the HTMLForm descriptor array for the block form
118 * @return Array
119 */
120 protected function getFormFields(){
121 global $wgBlockAllowsUTEdit;
122
123 $user = $this->getUser();
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( $user ) ) {
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( $user->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( $user->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 # 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( $this->getRequest()->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 === $this->getUser()->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 # Don't need to do anything if the form has been posted
335 if( !$this->getRequest()->wasPosted() && $this->preErrors ){
336 $s = HTMLForm::formatErrors( $this->preErrors );
337 if( $s ){
338 $form->addHeaderText( Html::rawElement(
339 'div',
340 array( 'class' => 'error' ),
341 $s
342 ) );
343 }
344 }
345 }
346
347 /**
348 * Add footer elements to the form
349 * @param $form HTMLForm
350 * @return void
351 */
352 protected function doPostText( HTMLForm &$form ){
353 # Link to the user's contributions, if applicable
354 if( $this->target instanceof User ){
355 $contribsPage = SpecialPage::getTitleFor( 'Contributions', $this->target->getName() );
356 $links[] = Linker::link(
357 $contribsPage,
358 wfMsgExt( 'ipb-blocklist-contribs', 'escape', $this->target->getName() )
359 );
360 }
361
362 # Link to unblock the specified user, or to a blank unblock form
363 if( $this->target instanceof User ) {
364 $message = wfMsgExt( 'ipb-unblock-addr', array( 'parseinline' ), $this->target->getName() );
365 $list = SpecialPage::getTitleFor( 'Unblock', $this->target->getName() );
366 } else {
367 $message = wfMsgExt( 'ipb-unblock', array( 'parseinline' ) );
368 $list = SpecialPage::getTitleFor( 'Unblock' );
369 }
370 $links[] = Linker::linkKnown( $list, $message, array() );
371
372 # Link to the block list
373 $links[] = Linker::linkKnown(
374 SpecialPage::getTitleFor( 'BlockList' ),
375 wfMsg( 'ipb-blocklist' )
376 );
377
378 $user = $this->getUser();
379
380 # Link to edit the block dropdown reasons, if applicable
381 if ( $user->isAllowed( 'editinterface' ) ) {
382 $links[] = Linker::link(
383 Title::makeTitle( NS_MEDIAWIKI, 'Ipbreason-dropdown' ),
384 wfMsgHtml( 'ipb-edit-dropdown' ),
385 array(),
386 array( 'action' => 'edit' )
387 );
388 }
389
390 $form->addPostText( Html::rawElement(
391 'p',
392 array( 'class' => 'mw-ipb-conveniencelinks' ),
393 $this->getLang()->pipeList( $links )
394 ) );
395
396 if( $this->target instanceof User ){
397 # Get relevant extracts from the block and suppression logs, if possible
398 $userpage = $this->target->getUserPage();
399 $out = '';
400
401 LogEventsList::showLogExtract(
402 $out,
403 'block',
404 $userpage->getPrefixedText(),
405 '',
406 array(
407 'lim' => 10,
408 'msgKey' => array( 'blocklog-showlog', $userpage->getText() ),
409 'showIfEmpty' => false
410 )
411 );
412 $form->addPostText( $out );
413
414 # Add suppression block entries if allowed
415 if( $user->isAllowed( 'suppressionlog' ) ) {
416 LogEventsList::showLogExtract(
417 $out,
418 'suppress',
419 $userpage->getPrefixedText(),
420 '',
421 array(
422 'lim' => 10,
423 'conds' => array( 'log_action' => array( 'block', 'reblock', 'unblock' ) ),
424 'msgKey' => array( 'blocklog-showsuppresslog', $userpage->getText() ),
425 'showIfEmpty' => false
426 )
427 );
428
429 $form->addPostText( $out );
430 }
431 }
432 }
433
434 /**
435 * Determine the target of the block, and the type of target
436 * TODO: should be in Block.php?
437 * @param $par String subpage parameter passed to setup, or data value from
438 * the HTMLForm
439 * @param $request WebRequest optionally try and get data from a request too
440 * @return array( User|string|null, Block::TYPE_ constant|null )
441 */
442 public static function getTargetAndType( $par, WebRequest $request = null ){
443 $i = 0;
444 $target = null;
445
446 while( true ){
447 switch( $i++ ){
448 case 0:
449 # The HTMLForm will check wpTarget first and only if it doesn't get
450 # a value use the default, which will be generated from the options
451 # below; so this has to have a higher precedence here than $par, or
452 # we could end up with different values in $this->target and the HTMLForm!
453 if( $request instanceof WebRequest ){
454 $target = $request->getText( 'wpTarget', null );
455 }
456 break;
457 case 1:
458 $target = $par;
459 break;
460 case 2:
461 if( $request instanceof WebRequest ){
462 $target = $request->getText( 'ip', null );
463 }
464 break;
465 case 3:
466 # B/C @since 1.18
467 if( $request instanceof WebRequest ){
468 $target = $request->getText( 'wpBlockAddress', null );
469 }
470 break;
471 case 4:
472 break 2;
473 }
474
475 list( $target, $type ) = Block::parseTarget( $target );
476
477 if( $type !== null ){
478 return array( $target, $type );
479 }
480 }
481
482 return array( null, null );
483 }
484
485 /**
486 * HTMLForm field validation-callback for Target field.
487 * @since 1.18
488 * @param $value String
489 * @param $alldata Array
490 * @return Message
491 */
492 public static function validateTargetField( $value, $alldata = null ) {
493 global $wgBlockCIDRLimit;
494
495 list( $target, $type ) = self::getTargetAndType( $value );
496
497 if( $type == Block::TYPE_USER ){
498 # TODO: why do we not have a User->exists() method?
499 if( !$target->getId() ){
500 return wfMessage( 'nosuchusershort',
501 wfEscapeWikiText( $target->getName() ) );
502 }
503
504 $status = self::checkUnblockSelf( $target );
505 if ( $status !== true ) {
506 return wfMessage( 'badaccess', $status );
507 }
508
509 } elseif( $type == Block::TYPE_RANGE ){
510 list( $ip, $range ) = explode( '/', $target, 2 );
511
512 if( ( IP::isIPv4( $ip ) && $wgBlockCIDRLimit['IPv4'] == 32 )
513 || ( IP::isIPv6( $ip ) && $wgBlockCIDRLimit['IPV6'] == 128 ) )
514 {
515 # Range block effectively disabled
516 return wfMessage( 'range_block_disabled' );
517 }
518
519 if( ( IP::isIPv4( $ip ) && $range > 32 )
520 || ( IP::isIPv6( $ip ) && $range > 128 ) )
521 {
522 # Dodgy range
523 return wfMessage( 'ip_range_invalid' );
524 }
525
526 if( IP::isIPv4( $ip ) && $range < $wgBlockCIDRLimit['IPv4'] ) {
527 return wfMessage( 'ip_range_toolarge', $wgBlockCIDRLimit['IPv4'] );
528 }
529
530 if( IP::isIPv6( $ip ) && $range < $wgBlockCIDRLimit['IPv6'] ) {
531 return wfMessage( 'ip_range_toolarge', $wgBlockCIDRLimit['IPv6'] );
532 }
533 } elseif( $type == Block::TYPE_IP ){
534 # All is well
535 } else {
536 return wfMessage( 'badipaddress' );
537 }
538
539 return true;
540 }
541
542 /**
543 * Given the form data, actually implement a block
544 * @param $data Array
545 * @return Bool|String
546 */
547 public static function processForm( array $data ){
548 global $wgUser, $wgBlockAllowsUTEdit;
549
550 // Handled by field validator callback
551 // self::validateTargetField( $data['Target'] );
552
553 # This might have been a hidden field or a checkbox, so interesting data
554 # can come from it
555 $data['Confirm'] = !in_array( $data['Confirm'], array( '', '0', null, false ), true );
556
557 list( $target, $type ) = self::getTargetAndType( $data['Target'] );
558 if( $type == Block::TYPE_USER ){
559 $user = $target;
560 $target = $user->getName();
561 $userId = $user->getId();
562
563 # Give admins a heads-up before they go and block themselves. Much messier
564 # to do this for IPs, but it's pretty unlikely they'd ever get the 'block'
565 # permission anyway, although the code does allow for it
566 if( $target === $wgUser->getName() &&
567 ( $data['PreviousTarget'] !== $data['Target'] || !$data['Confirm'] ) )
568 {
569 return array( 'ipb-blockingself' );
570 }
571 } elseif( $type == Block::TYPE_RANGE ){
572 $userId = 0;
573 } elseif( $type == Block::TYPE_IP ){
574 $target = $target->getName();
575 $userId = 0;
576 } else {
577 # This should have been caught in the form field validation
578 return array( 'badipaddress' );
579 }
580
581 if( ( strlen( $data['Expiry'] ) == 0) || ( strlen( $data['Expiry'] ) > 50 )
582 || !self::parseExpiryInput( $data['Expiry'] ) )
583 {
584 return array( 'ipb_expiry_invalid' );
585 }
586
587 if( !isset( $data['DisableEmail'] ) ){
588 $data['DisableEmail'] = false;
589 }
590
591 # If the user has done the form 'properly', they won't even have been given the
592 # option to suppress-block unless they have the 'hideuser' permission
593 if( !isset( $data['HideUser'] ) ){
594 $data['HideUser'] = false;
595 }
596
597 if( $data['HideUser'] ) {
598 if( !$wgUser->isAllowed('hideuser') ){
599 # this codepath is unreachable except by a malicious user spoofing forms,
600 # or by race conditions (user has oversight and sysop, loads block form,
601 # and is de-oversighted before submission); so need to fail completely
602 # rather than just silently disable hiding
603 return array( 'badaccess-group0' );
604 }
605
606 # Recheck params here...
607 if( $type != Block::TYPE_USER ) {
608 $data['HideUser'] = false; # IP users should not be hidden
609 } elseif( !in_array( $data['Expiry'], array( 'infinite', 'infinity', 'indefinite' ) ) ) {
610 # Bad expiry.
611 return array( 'ipb_expiry_temp' );
612 } elseif( $user->getEditCount() > self::HIDEUSER_CONTRIBLIMIT ) {
613 # Typically, the user should have a handful of edits.
614 # Disallow hiding users with many edits for performance.
615 return array( 'ipb_hide_invalid' );
616 } elseif( !$data['Confirm'] ){
617 return array( 'ipb-confirmhideuser' );
618 }
619 }
620
621 # Create block object.
622 $block = new Block();
623 $block->setTarget( $target );
624 $block->setBlocker( $wgUser );
625 $block->mReason = $data['Reason'][0];
626 $block->mExpiry = self::parseExpiryInput( $data['Expiry'] );
627 $block->prevents( 'createaccount', $data['CreateAccount'] );
628 $block->prevents( 'editownusertalk', ( !$wgBlockAllowsUTEdit || $data['DisableUTEdit'] ) );
629 $block->prevents( 'sendemail', $data['DisableEmail'] );
630 $block->isHardblock( $data['HardBlock'] );
631 $block->isAutoblocking( $data['AutoBlock'] );
632 $block->mHideName = $data['HideUser'];
633
634 if( !wfRunHooks( 'BlockIp', array( &$block, &$wgUser ) ) ) {
635 return array( 'hookaborted' );
636 }
637
638 # Try to insert block. Is there a conflicting block?
639 $status = $block->insert();
640 if( !$status ) {
641 # Show form unless the user is already aware of this...
642 if( !$data['Confirm'] || ( array_key_exists( 'PreviousTarget', $data )
643 && $data['PreviousTarget'] !== $block->getTarget() ) )
644 {
645 return array( array( 'ipb_already_blocked', $block->getTarget() ) );
646 # Otherwise, try to update the block...
647 } else {
648 # This returns direct blocks before autoblocks/rangeblocks, since we should
649 # be sure the user is blocked by now it should work for our purposes
650 $currentBlock = Block::newFromTarget( $target );
651
652 if( $block->equals( $currentBlock ) ) {
653 return array( array( 'ipb_already_blocked', $block->getTarget() ) );
654 }
655
656 # If the name was hidden and the blocking user cannot hide
657 # names, then don't allow any block changes...
658 if( $currentBlock->mHideName && !$wgUser->isAllowed( 'hideuser' ) ) {
659 return array( 'cant-see-hidden-user' );
660 }
661
662 $currentBlock->delete();
663 $status = $block->insert();
664 $logaction = 'reblock';
665
666 # Unset _deleted fields if requested
667 if( $currentBlock->mHideName && !$data['HideUser'] ) {
668 RevisionDeleteUser::unsuppressUserName( $target, $userId );
669 }
670
671 # If hiding/unhiding a name, this should go in the private logs
672 if( (bool)$currentBlock->mHideName ){
673 $data['HideUser'] = true;
674 }
675 }
676 } else {
677 $logaction = 'block';
678 }
679
680 wfRunHooks( 'BlockIpComplete', array( $block, $wgUser ) );
681
682 # Set *_deleted fields if requested
683 if( $data['HideUser'] ) {
684 RevisionDeleteUser::suppressUserName( $target, $userId );
685 }
686
687 # Can't watch a rangeblock
688 if( $type != Block::TYPE_RANGE && $data['Watch'] ) {
689 $wgUser->addWatch( Title::makeTitle( NS_USER, $target ) );
690 }
691
692 # Block constructor sanitizes certain block options on insert
693 $data['BlockEmail'] = $block->prevents( 'sendemail' );
694 $data['AutoBlock'] = $block->isAutoblocking();
695
696 # Prepare log parameters
697 $logParams = array();
698 $logParams[] = $data['Expiry'];
699 $logParams[] = self::blockLogFlags( $data, $type );
700
701 # Make log entry, if the name is hidden, put it in the oversight log
702 $log_type = $data['HideUser'] ? 'suppress' : 'block';
703 $log = new LogPage( $log_type );
704 $log_id = $log->addEntry(
705 $logaction,
706 Title::makeTitle( NS_USER, $target ),
707 $data['Reason'][0],
708 $logParams
709 );
710 # Relate log ID to block IDs (bug 25763)
711 $blockIds = array_merge( array( $status['id'] ), $status['autoIds'] );
712 $log->addRelations( 'ipb_id', $blockIds, $log_id );
713
714 # Report to the user
715 return true;
716 }
717
718 /**
719 * Get an array of suggested block durations from MediaWiki:Ipboptions
720 * @todo FIXME: This uses a rather odd syntax for the options, should it be converted
721 * to the standard "**<duration>|<displayname>" format?
722 * @param $lang Language|null the language to get the durations in, or null to use
723 * the wiki's content language
724 * @return Array
725 */
726 public static function getSuggestedDurations( $lang = null ){
727 $a = array();
728 $msg = $lang === null
729 ? wfMessage( 'ipboptions' )->inContentLanguage()->text()
730 : wfMessage( 'ipboptions' )->inLanguage( $lang )->text();
731
732 if( $msg == '-' ){
733 return array();
734 }
735
736 foreach( explode( ',', $msg ) as $option ) {
737 if( strpos( $option, ':' ) === false ){
738 $option = "$option:$option";
739 }
740
741 list( $show, $value ) = explode( ':', $option );
742 $a[htmlspecialchars( $show )] = htmlspecialchars( $value );
743 }
744
745 return $a;
746 }
747
748 /**
749 * Convert a submitted expiry time, which may be relative ("2 weeks", etc) or absolute
750 * ("24 May 2034", etc), into an absolute timestamp we can put into the database.
751 * @param $expiry String: whatever was typed into the form
752 * @return String: timestamp or "infinity" string for the DB implementation
753 */
754 public static function parseExpiryInput( $expiry ) {
755 static $infinity;
756 if( $infinity == null ){
757 $infinity = wfGetDB( DB_SLAVE )->getInfinity();
758 }
759
760 if ( $expiry == 'infinite' || $expiry == 'indefinite' ) {
761 $expiry = $infinity;
762 } else {
763 $expiry = strtotime( $expiry );
764
765 if ( $expiry < 0 || $expiry === false ) {
766 return false;
767 }
768
769 $expiry = wfTimestamp( TS_MW, $expiry );
770 }
771
772 return $expiry;
773 }
774
775 /**
776 * Can we do an email block?
777 * @param $user User: the sysop wanting to make a block
778 * @return Boolean
779 */
780 public static function canBlockEmail( $user ) {
781 global $wgEnableUserEmail, $wgSysopEmailBans;
782
783 return ( $wgEnableUserEmail && $wgSysopEmailBans && $user->isAllowed( 'blockemail' ) );
784 }
785
786 /**
787 * bug 15810: blocked admins should not be able to block/unblock
788 * others, and probably shouldn't be able to unblock themselves
789 * either.
790 * @param $user User|Int|String
791 * @return Bool|String true or error message key
792 */
793 public static function checkUnblockSelf( $user ) {
794 global $wgUser;
795
796 if ( is_int( $user ) ) {
797 $user = User::newFromId( $user );
798 } elseif ( is_string( $user ) ) {
799 $user = User::newFromName( $user );
800 }
801
802 if( $wgUser->isBlocked() ){
803 if( $user instanceof User && $user->getId() == $wgUser->getId() ) {
804 # User is trying to unblock themselves
805 if ( $wgUser->isAllowed( 'unblockself' ) ) {
806 return true;
807 # User blocked themselves and is now trying to reverse it
808 } elseif ( $wgUser->blockedBy() === $wgUser->getName() ) {
809 return true;
810 } else {
811 return 'ipbnounblockself';
812 }
813 } else {
814 # User is trying to block/unblock someone else
815 return 'ipbblocked';
816 }
817 } else {
818 return true;
819 }
820 }
821
822 /**
823 * Return a comma-delimited list of "flags" to be passed to the log
824 * reader for this block, to provide more information in the logs
825 * @param $data Array from HTMLForm data
826 * @param $type Block::TYPE_ constant
827 * @return array
828 */
829 protected static function blockLogFlags( array $data, $type ) {
830 global $wgBlockAllowsUTEdit;
831 $flags = array();
832
833 # when blocking a user the option 'anononly' is not available/has no effect -> do not write this into log
834 if( !$data['HardBlock'] && $type != Block::TYPE_USER ){
835 // For grepping: message block-log-flags-anononly
836 $flags[] = 'anononly';
837 }
838
839 if( $data['CreateAccount'] ){
840 // For grepping: message block-log-flags-nocreate
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 // For grepping: message block-log-flags-noautoblock
847 $flags[] = 'noautoblock';
848 }
849
850 if( $data['DisableEmail'] ){
851 // For grepping: message block-log-flags-noemail
852 $flags[] = 'noemail';
853 }
854
855 if( $wgBlockAllowsUTEdit && $data['DisableUTEdit'] ){
856 // For grepping: message block-log-flags-nousertalk
857 $flags[] = 'nousertalk';
858 }
859
860 if( $data['HideUser'] ){
861 // For grepping: message block-log-flags-hiddenname
862 $flags[] = 'hiddenname';
863 }
864
865 return implode( ',', $flags );
866 }
867 }
868
869 # BC @since 1.18
870 class IPBlockForm extends SpecialBlock {}