* Merged WikiPage::updateRestrictions() and Title::updateTitleProtection() into WikiP...
[mediawiki.git] / includes / specials / SpecialBlock.php
blobfab295ac08fd1a7e5baf887bbccb5a7a0b51ac5a
1 <?php
2 /**
3 * Implements Special:Block
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.
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.
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
20 * @file
21 * @ingroup SpecialPage
24 /**
25 * A special page that allows users with 'block' right to block users from
26 * editing pages and other actions
28 * @ingroup SpecialPage
30 class SpecialBlock extends FormSpecialPage {
31 /** The maximum number of edits a user can have and still be hidden
32 * TODO: config setting? */
33 const HIDEUSER_CONTRIBLIMIT = 1000;
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;
39 /// @var Block::TYPE_ constant
40 protected $type;
42 /// @var User|String the previous block target
43 protected $previousTarget;
45 /// @var Bool whether the previous submission of the form asked for HideUser
46 protected $requestedHideUser;
48 /// @var Bool
49 protected $alreadyBlocked;
51 /// @var Array
52 protected $preErrors = array();
54 public function __construct() {
55 parent::__construct( 'Block', 'block' );
58 /**
59 * Checks that the user can unblock themselves if they are trying to do so
61 * @param User $user
62 * @throws ErrorPageError
64 protected function checkExecutePermissions( User $user ) {
65 parent::checkExecutePermissions( $user );
67 # bug 15810: blocked admins should have limited access here
68 $status = self::checkUnblockSelf( $this->target, $user );
69 if ( $status !== true ) {
70 throw new ErrorPageError( 'badaccess', $status );
74 /**
75 * Handle some magic here
77 * @param $par String
79 protected function setParameter( $par ) {
80 # Extract variables from the request. Try not to get into a situation where we
81 # need to extract *every* variable from the form just for processing here, but
82 # there are legitimate uses for some variables
83 $request = $this->getRequest();
84 list( $this->target, $this->type ) = self::getTargetAndType( $par, $request );
85 if ( $this->target instanceof User ) {
86 # Set the 'relevant user' in the skin, so it displays links like Contributions,
87 # User logs, UserRights, etc.
88 $this->getSkin()->setRelevantUser( $this->target );
91 list( $this->previousTarget, /*...*/ ) = Block::parseTarget( $request->getVal( 'wpPreviousTarget' ) );
92 $this->requestedHideUser = $request->getBool( 'wpHideUser' );
95 /**
96 * Customizes the HTMLForm a bit
98 * @param $form HTMLForm
100 protected function alterForm( HTMLForm $form ) {
101 $form->setWrapperLegendMsg( 'blockip-legend' );
102 $form->setHeaderText( '' );
103 $form->setSubmitCallback( array( __CLASS__, 'processUIForm' ) );
105 $msg = $this->alreadyBlocked ? 'ipb-change-block' : 'ipbsubmit';
106 $form->setSubmitTextMsg( $msg );
108 # Don't need to do anything if the form has been posted
109 if( !$this->getRequest()->wasPosted() && $this->preErrors ){
110 $s = HTMLForm::formatErrors( $this->preErrors );
111 if( $s ){
112 $form->addHeaderText( Html::rawElement(
113 'div',
114 array( 'class' => 'error' ),
116 ) );
122 * Get the HTMLForm descriptor array for the block form
123 * @return Array
125 protected function getFormFields(){
126 global $wgBlockAllowsUTEdit;
128 $user = $this->getUser();
130 $a = array(
131 'Target' => array(
132 'type' => 'text',
133 'label-message' => 'ipadressorusername',
134 'tabindex' => '1',
135 'id' => 'mw-bi-target',
136 'size' => '45',
137 'required' => true,
138 'validation-callback' => array( __CLASS__, 'validateTargetField' ),
140 'Expiry' => array(
141 'type' => !count( self::getSuggestedDurations() ) ? 'text' : 'selectorother',
142 'label-message' => 'ipbexpiry',
143 'required' => true,
144 'tabindex' => '2',
145 'options' => self::getSuggestedDurations(),
146 'other' => wfMsg( 'ipbother' ),
148 'Reason' => array(
149 'type' => 'selectandother',
150 'label-message' => 'ipbreason',
151 'options-message' => 'ipbreason-dropdown',
153 'CreateAccount' => array(
154 'type' => 'check',
155 'label-message' => 'ipbcreateaccount',
156 'default' => true,
160 if( self::canBlockEmail( $user ) ) {
161 $a['DisableEmail'] = array(
162 'type' => 'check',
163 'label-message' => 'ipbemailban',
167 if( $wgBlockAllowsUTEdit ){
168 $a['DisableUTEdit'] = array(
169 'type' => 'check',
170 'label-message' => 'ipb-disableusertalk',
171 'default' => false,
175 $a['AutoBlock'] = array(
176 'type' => 'check',
177 'label-message' => 'ipbenableautoblock',
178 'default' => true,
181 # Allow some users to hide name from block log, blocklist and listusers
182 if( $user->isAllowed( 'hideuser' ) ) {
183 $a['HideUser'] = array(
184 'type' => 'check',
185 'label-message' => 'ipbhidename',
186 'cssclass' => 'mw-block-hideuser',
190 # Watchlist their user page? (Only if user is logged in)
191 if( $user->isLoggedIn() ) {
192 $a['Watch'] = array(
193 'type' => 'check',
194 'label-message' => 'ipbwatchuser',
198 $a['HardBlock'] = array(
199 'type' => 'check',
200 'label-message' => 'ipb-hardblock',
201 'default' => false,
204 # This is basically a copy of the Target field, but the user can't change it, so we
205 # can see if the warnings we maybe showed to the user before still apply
206 $a['PreviousTarget'] = array(
207 'type' => 'hidden',
208 'default' => false,
211 # We'll turn this into a checkbox if we need to
212 $a['Confirm'] = array(
213 'type' => 'hidden',
214 'default' => '',
215 'label-message' => 'ipb-confirm',
218 $this->maybeAlterFormDefaults( $a );
220 return $a;
224 * If the user has already been blocked with similar settings, load that block
225 * and change the defaults for the form fields to match the existing settings.
226 * @param $fields Array HTMLForm descriptor array
227 * @return Bool whether fields were altered (that is, whether the target is
228 * already blocked)
230 protected function maybeAlterFormDefaults( &$fields ){
231 # This will be overwritten by request data
232 $fields['Target']['default'] = (string)$this->target;
234 # This won't be
235 $fields['PreviousTarget']['default'] = (string)$this->target;
237 $block = Block::newFromTarget( $this->target );
239 if( $block instanceof Block && !$block->mAuto # The block exists and isn't an autoblock
240 && ( $this->type != Block::TYPE_RANGE # The block isn't a rangeblock
241 || $block->getTarget() == $this->target ) # or if it is, the range is what we're about to block
244 $fields['HardBlock']['default'] = $block->isHardblock();
245 $fields['CreateAccount']['default'] = $block->prevents( 'createaccount' );
246 $fields['AutoBlock']['default'] = $block->isAutoblocking();
248 if( isset( $fields['DisableEmail'] ) ){
249 $fields['DisableEmail']['default'] = $block->prevents( 'sendemail' );
252 if( isset( $fields['HideUser'] ) ){
253 $fields['HideUser']['default'] = $block->mHideName;
256 if( isset( $fields['DisableUTEdit'] ) ){
257 $fields['DisableUTEdit']['default'] = $block->prevents( 'editownusertalk' );
260 $fields['Reason']['default'] = $block->mReason;
262 if( $this->getRequest()->wasPosted() ){
263 # Ok, so we got a POST submission asking us to reblock a user. So show the
264 # confirm checkbox; the user will only see it if they haven't previously
265 $fields['Confirm']['type'] = 'check';
266 } else {
267 # We got a target, but it wasn't a POST request, so the user must have gone
268 # to a link like [[Special:Block/User]]. We don't need to show the checkbox
269 # as long as they go ahead and block *that* user
270 $fields['Confirm']['default'] = 1;
273 if( $block->mExpiry == 'infinity' ) {
274 $fields['Expiry']['default'] = 'indefinite';
275 } else {
276 $fields['Expiry']['default'] = wfTimestamp( TS_RFC2822, $block->mExpiry );
279 $this->alreadyBlocked = true;
280 $this->preErrors[] = array( 'ipb-needreblock', (string)$block->getTarget() );
283 # We always need confirmation to do HideUser
284 if( $this->requestedHideUser ){
285 $fields['Confirm']['type'] = 'check';
286 unset( $fields['Confirm']['default'] );
287 $this->preErrors[] = 'ipb-confirmhideuser';
290 # Or if the user is trying to block themselves
291 if( (string)$this->target === $this->getUser()->getName() ){
292 $fields['Confirm']['type'] = 'check';
293 unset( $fields['Confirm']['default'] );
294 $this->preErrors[] = 'ipb-blockingself';
299 * Add header elements like block log entries, etc.
301 protected function preText(){
302 $text = $this->msg( 'blockiptext' )->parse();
304 $otherBlockMessages = array();
305 if( $this->target !== null ) {
306 # Get other blocks, i.e. from GlobalBlocking or TorBlock extension
307 wfRunHooks( 'OtherBlockLogLink', array( &$otherBlockMessages, $this->target ) );
309 if( count( $otherBlockMessages ) ) {
310 $s = Html::rawElement(
311 'h2',
312 array(),
313 wfMsgExt( 'ipb-otherblocks-header', 'parseinline', count( $otherBlockMessages ) )
314 ) . "\n";
316 $list = '';
318 foreach( $otherBlockMessages as $link ) {
319 $list .= Html::rawElement( 'li', array(), $link ) . "\n";
322 $s .= Html::rawElement(
323 'ul',
324 array( 'class' => 'mw-blockip-alreadyblocked' ),
325 $list
326 ) . "\n";
328 $text .= $s;
332 return $text;
336 * Add footer elements to the form
337 * @return void
339 protected function postText(){
340 # Link to the user's contributions, if applicable
341 if( $this->target instanceof User ){
342 $contribsPage = SpecialPage::getTitleFor( 'Contributions', $this->target->getName() );
343 $links[] = Linker::link(
344 $contribsPage,
345 wfMsgExt( 'ipb-blocklist-contribs', 'escape', $this->target->getName() )
349 # Link to unblock the specified user, or to a blank unblock form
350 if( $this->target instanceof User ) {
351 $message = wfMsgExt( 'ipb-unblock-addr', array( 'parseinline' ), $this->target->getName() );
352 $list = SpecialPage::getTitleFor( 'Unblock', $this->target->getName() );
353 } else {
354 $message = wfMsgExt( 'ipb-unblock', array( 'parseinline' ) );
355 $list = SpecialPage::getTitleFor( 'Unblock' );
357 $links[] = Linker::linkKnown( $list, $message, array() );
359 # Link to the block list
360 $links[] = Linker::linkKnown(
361 SpecialPage::getTitleFor( 'BlockList' ),
362 wfMsg( 'ipb-blocklist' )
365 $user = $this->getUser();
367 # Link to edit the block dropdown reasons, if applicable
368 if ( $user->isAllowed( 'editinterface' ) ) {
369 $links[] = Linker::link(
370 Title::makeTitle( NS_MEDIAWIKI, 'Ipbreason-dropdown' ),
371 wfMsgHtml( 'ipb-edit-dropdown' ),
372 array(),
373 array( 'action' => 'edit' )
377 $text = Html::rawElement(
378 'p',
379 array( 'class' => 'mw-ipb-conveniencelinks' ),
380 $this->getLanguage()->pipeList( $links )
383 if( $this->target instanceof User ){
384 # Get relevant extracts from the block and suppression logs, if possible
385 $userpage = $this->target->getUserPage();
386 $out = '';
388 LogEventsList::showLogExtract(
389 $out,
390 'block',
391 $userpage,
393 array(
394 'lim' => 10,
395 'msgKey' => array( 'blocklog-showlog', $userpage->getText() ),
396 'showIfEmpty' => false
399 $text .= $out;
401 # Add suppression block entries if allowed
402 if( $user->isAllowed( 'suppressionlog' ) ) {
403 LogEventsList::showLogExtract(
404 $out,
405 'suppress',
406 $userpage,
408 array(
409 'lim' => 10,
410 'conds' => array( 'log_action' => array( 'block', 'reblock', 'unblock' ) ),
411 'msgKey' => array( 'blocklog-showsuppresslog', $userpage->getText() ),
412 'showIfEmpty' => false
416 $text .= $out;
420 return $text;
424 * Determine the target of the block, and the type of target
425 * TODO: should be in Block.php?
426 * @param $par String subpage parameter passed to setup, or data value from
427 * the HTMLForm
428 * @param $request WebRequest optionally try and get data from a request too
429 * @return array( User|string|null, Block::TYPE_ constant|null )
431 public static function getTargetAndType( $par, WebRequest $request = null ){
432 $i = 0;
433 $target = null;
435 while( true ){
436 switch( $i++ ){
437 case 0:
438 # The HTMLForm will check wpTarget first and only if it doesn't get
439 # a value use the default, which will be generated from the options
440 # below; so this has to have a higher precedence here than $par, or
441 # we could end up with different values in $this->target and the HTMLForm!
442 if( $request instanceof WebRequest ){
443 $target = $request->getText( 'wpTarget', null );
445 break;
446 case 1:
447 $target = $par;
448 break;
449 case 2:
450 if( $request instanceof WebRequest ){
451 $target = $request->getText( 'ip', null );
453 break;
454 case 3:
455 # B/C @since 1.18
456 if( $request instanceof WebRequest ){
457 $target = $request->getText( 'wpBlockAddress', null );
459 break;
460 case 4:
461 break 2;
464 list( $target, $type ) = Block::parseTarget( $target );
466 if( $type !== null ){
467 return array( $target, $type );
471 return array( null, null );
475 * HTMLForm field validation-callback for Target field.
476 * @since 1.18
477 * @param $value String
478 * @param $alldata Array
479 * @param $form HTMLForm
480 * @return Message
482 public static function validateTargetField( $value, $alldata, $form ) {
483 global $wgBlockCIDRLimit;
485 list( $target, $type ) = self::getTargetAndType( $value );
487 if( $type == Block::TYPE_USER ){
488 # TODO: why do we not have a User->exists() method?
489 if( !$target->getId() ){
490 return $form->msg( 'nosuchusershort',
491 wfEscapeWikiText( $target->getName() ) );
494 $status = self::checkUnblockSelf( $target, $form->getUser() );
495 if ( $status !== true ) {
496 return $form->msg( 'badaccess', $status );
499 } elseif( $type == Block::TYPE_RANGE ){
500 list( $ip, $range ) = explode( '/', $target, 2 );
502 if( ( IP::isIPv4( $ip ) && $wgBlockCIDRLimit['IPv4'] == 32 )
503 || ( IP::isIPv6( $ip ) && $wgBlockCIDRLimit['IPv6'] == 128 ) )
505 # Range block effectively disabled
506 return $form->msg( 'range_block_disabled' );
509 if( ( IP::isIPv4( $ip ) && $range > 32 )
510 || ( IP::isIPv6( $ip ) && $range > 128 ) )
512 # Dodgy range
513 return $form->msg( 'ip_range_invalid' );
516 if( IP::isIPv4( $ip ) && $range < $wgBlockCIDRLimit['IPv4'] ) {
517 return $form->msg( 'ip_range_toolarge', $wgBlockCIDRLimit['IPv4'] );
520 if( IP::isIPv6( $ip ) && $range < $wgBlockCIDRLimit['IPv6'] ) {
521 return $form->msg( 'ip_range_toolarge', $wgBlockCIDRLimit['IPv6'] );
523 } elseif( $type == Block::TYPE_IP ){
524 # All is well
525 } else {
526 return $form->msg( 'badipaddress' );
529 return true;
533 * Submit callback for an HTMLForm object, will simply pass
534 * @param $data array
535 * @param $form HTMLForm
536 * @return Bool|String
538 public static function processUIForm( array $data, HTMLForm $form ) {
539 return self::processForm( $data, $form->getContext() );
543 * Given the form data, actually implement a block
544 * @param $data Array
545 * @param $context IContextSource
546 * @return Bool|String
548 public static function processForm( array $data, IContextSource $context ){
549 global $wgBlockAllowsUTEdit;
551 $performer = $context->getUser();
553 // Handled by field validator callback
554 // self::validateTargetField( $data['Target'] );
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 );
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();
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 === $performer->getName() &&
570 ( $data['PreviousTarget'] !== $data['Target'] || !$data['Confirm'] ) )
572 return array( 'ipb-blockingself' );
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' );
584 if( ( strlen( $data['Expiry'] ) == 0) || ( strlen( $data['Expiry'] ) > 50 )
585 || !self::parseExpiryInput( $data['Expiry'] ) )
587 return array( 'ipb_expiry_invalid' );
590 if( !isset( $data['DisableEmail'] ) ){
591 $data['DisableEmail'] = false;
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;
600 if( $data['HideUser'] ) {
601 if( !$performer->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' );
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' );
624 # Create block object.
625 $block = new Block();
626 $block->setTarget( $target );
627 $block->setBlocker( $performer );
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'];
637 if( !wfRunHooks( 'BlockIp', array( &$block, &$performer ) ) ) {
638 return array( 'hookaborted' );
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 ) )
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 );
655 if( $block->equals( $currentBlock ) ) {
656 return array( array( 'ipb_already_blocked', $block->getTarget() ) );
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 && !$performer->isAllowed( 'hideuser' ) ) {
662 return array( 'cant-see-hidden-user' );
665 $currentBlock->delete();
666 $status = $block->insert();
667 $logaction = 'reblock';
669 # Unset _deleted fields if requested
670 if( $currentBlock->mHideName && !$data['HideUser'] ) {
671 RevisionDeleteUser::unsuppressUserName( $target, $userId );
674 # If hiding/unhiding a name, this should go in the private logs
675 if( (bool)$currentBlock->mHideName ){
676 $data['HideUser'] = true;
679 } else {
680 $logaction = 'block';
683 wfRunHooks( 'BlockIpComplete', array( $block, $performer ) );
685 # Set *_deleted fields if requested
686 if( $data['HideUser'] ) {
687 RevisionDeleteUser::suppressUserName( $target, $userId );
690 # Can't watch a rangeblock
691 if( $type != Block::TYPE_RANGE && $data['Watch'] ) {
692 $performer->addWatch( Title::makeTitle( NS_USER, $target ) );
695 # Block constructor sanitizes certain block options on insert
696 $data['BlockEmail'] = $block->prevents( 'sendemail' );
697 $data['AutoBlock'] = $block->isAutoblocking();
699 # Prepare log parameters
700 $logParams = array();
701 $logParams[] = $data['Expiry'];
702 $logParams[] = self::blockLogFlags( $data, $type );
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
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 );
717 # Report to the user
718 return true;
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
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();
735 if( $msg == '-' ){
736 return array();
739 foreach( explode( ',', $msg ) as $option ) {
740 if( strpos( $option, ':' ) === false ){
741 $option = "$option:$option";
744 list( $show, $value ) = explode( ':', $option );
745 $a[htmlspecialchars( $show )] = htmlspecialchars( $value );
748 return $a;
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
757 public static function parseExpiryInput( $expiry ) {
758 static $infinity;
759 if( $infinity == null ){
760 $infinity = wfGetDB( DB_SLAVE )->getInfinity();
763 if ( $expiry == 'infinite' || $expiry == 'indefinite' ) {
764 $expiry = $infinity;
765 } else {
766 $expiry = strtotime( $expiry );
768 if ( $expiry < 0 || $expiry === false ) {
769 return false;
772 $expiry = wfTimestamp( TS_MW, $expiry );
775 return $expiry;
779 * Can we do an email block?
780 * @param $user User: the sysop wanting to make a block
781 * @return Boolean
783 public static function canBlockEmail( $user ) {
784 global $wgEnableUserEmail, $wgSysopEmailBans;
786 return ( $wgEnableUserEmail && $wgSysopEmailBans && $user->isAllowed( 'blockemail' ) );
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 * @param $performer User user doing the request
795 * @return Bool|String true or error message key
797 public static function checkUnblockSelf( $user, User $performer ) {
798 if ( is_int( $user ) ) {
799 $user = User::newFromId( $user );
800 } elseif ( is_string( $user ) ) {
801 $user = User::newFromName( $user );
804 if( $performer->isBlocked() ){
805 if( $user instanceof User && $user->getId() == $performer->getId() ) {
806 # User is trying to unblock themselves
807 if ( $performer->isAllowed( 'unblockself' ) ) {
808 return true;
809 # User blocked themselves and is now trying to reverse it
810 } elseif ( $performer->blockedBy() === $performer->getName() ) {
811 return true;
812 } else {
813 return 'ipbnounblockself';
815 } else {
816 # User is trying to block/unblock someone else
817 return 'ipbblocked';
819 } else {
820 return true;
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 (USER, RANGE, or IP)
829 * @return array
831 protected static function blockLogFlags( array $data, $type ) {
832 global $wgBlockAllowsUTEdit;
833 $flags = array();
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 // For grepping: message block-log-flags-anononly
838 $flags[] = 'anononly';
841 if( $data['CreateAccount'] ){
842 // For grepping: message block-log-flags-nocreate
843 $flags[] = 'nocreate';
846 # Same as anononly, this is not displayed when blocking an IP address
847 if( !$data['AutoBlock'] && $type == Block::TYPE_USER ){
848 // For grepping: message block-log-flags-noautoblock
849 $flags[] = 'noautoblock';
852 if( $data['DisableEmail'] ){
853 // For grepping: message block-log-flags-noemail
854 $flags[] = 'noemail';
857 if( $wgBlockAllowsUTEdit && $data['DisableUTEdit'] ){
858 // For grepping: message block-log-flags-nousertalk
859 $flags[] = 'nousertalk';
862 if( $data['HideUser'] ){
863 // For grepping: message block-log-flags-hiddenname
864 $flags[] = 'hiddenname';
867 return implode( ',', $flags );
871 * Process the form on POST submission.
872 * @param $data Array
873 * @return Bool|Array true for success, false for didn't-try, array of errors on failure
875 public function onSubmit( array $data ) {
876 // This isn't used since we need that HTMLForm that's passed in the
877 // second parameter. See alterForm for the real function
881 * Do something exciting on successful processing of the form, most likely to show a
882 * confirmation message
884 public function onSuccess() {
885 $out = $this->getOutput();
886 $out->setPageTitle( $this->msg( 'blockipsuccesssub' ) );
887 $out->addWikiMsg( 'blockipsuccesstext', $this->target );
891 # BC @since 1.18
892 class IPBlockForm extends SpecialBlock {}