r83790 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r83789‎ | r83790 | r83791 >
Date:22:48, 12 March 2011
Author:happy-melon
Status:deferred
Tags:
Comment:
Follow-up r83786, try to keep the svn history of SpecialBlockip. This rev will be broken.
Modified paths:
  • /trunk/phase3/includes/specials/SpecialBlockip.php (added) (history)

Diff [purge]

Index: trunk/phase3/includes/specials/SpecialBlockip.php
@@ -0,0 +1,846 @@
 2+<?php
 3+/**
 4+ * Implements Special:Blockip
 5+ *
 6+ * This program is free software; you can redistribute it and/or modify
 7+ * it under the terms of the GNU General Public License as published by
 8+ * the Free Software Foundation; either version 2 of the License, or
 9+ * (at your option) any later version.
 10+ *
 11+ * This program is distributed in the hope that it will be useful,
 12+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
 13+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 14+ * GNU General Public License for more details.
 15+ *
 16+ * You should have received a copy of the GNU General Public License along
 17+ * with this program; if not, write to the Free Software Foundation, Inc.,
 18+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 19+ * http://www.gnu.org/copyleft/gpl.html
 20+ *
 21+ * @file
 22+ * @ingroup SpecialPage
 23+ */
 24+
 25+/**
 26+ * A special page that allows users with 'block' right to block users from
 27+ * editing pages and other actions
 28+ *
 29+ * @ingroup SpecialPage
 30+ */
 31+class IPBlockForm extends SpecialPage {
 32+ var $BlockAddress, $BlockExpiry, $BlockReason, $BlockReasonList, $BlockOther, $BlockAnonOnly, $BlockCreateAccount,
 33+ $BlockEnableAutoblock, $BlockEmail, $BlockHideName, $BlockAllowUsertalk, $BlockReblock, $BlockWatchUser;
 34+ // The maximum number of edits a user can have and still be hidden
 35+ const HIDEUSER_CONTRIBLIMIT = 1000;
 36+
 37+ public function __construct() {
 38+ parent::__construct( 'Blockip', 'block' );
 39+ }
 40+
 41+ public function execute( $par ) {
 42+ global $wgUser, $wgOut, $wgRequest;
 43+
 44+ # Can't block when the database is locked
 45+ if( wfReadOnly() ) {
 46+ $wgOut->readOnlyPage();
 47+ return;
 48+ }
 49+ # Permission check
 50+ if( !$this->userCanExecute( $wgUser ) ) {
 51+ $wgOut->permissionRequired( 'block' );
 52+ return;
 53+ }
 54+
 55+ $this->setup( $par );
 56+
 57+ # bug 15810: blocked admins should have limited access here
 58+ if ( $wgUser->isBlocked() ) {
 59+ $status = IPBlockForm::checkUnblockSelf( $this->BlockAddress );
 60+ if ( $status !== true ) {
 61+ throw new ErrorPageError( 'badaccess', $status );
 62+ }
 63+ }
 64+
 65+ $action = $wgRequest->getVal( 'action' );
 66+ if( 'success' == $action ) {
 67+ $this->showSuccess();
 68+ } elseif( $wgRequest->wasPosted() && 'submit' == $action &&
 69+ $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ) ) ) {
 70+ $this->doSubmit();
 71+ } else {
 72+ $this->showForm( '' );
 73+ }
 74+ }
 75+
 76+ private function setup( $par ) {
 77+ global $wgRequest, $wgUser, $wgBlockAllowsUTEdit;
 78+
 79+ $this->BlockAddress = $wgRequest->getVal( 'wpBlockAddress', $wgRequest->getVal( 'ip', $par ) );
 80+ $this->BlockAddress = strtr( $this->BlockAddress, '_', ' ' );
 81+ $this->BlockReason = $wgRequest->getText( 'wpBlockReason' );
 82+ $this->BlockReasonList = $wgRequest->getText( 'wpBlockReasonList' );
 83+ $this->BlockExpiry = $wgRequest->getVal( 'wpBlockExpiry', wfMsg( 'ipbotheroption' ) );
 84+ $this->BlockOther = $wgRequest->getVal( 'wpBlockOther', '' );
 85+
 86+ # Unchecked checkboxes are not included in the form data at all, so having one
 87+ # that is true by default is a bit tricky
 88+ $byDefault = !$wgRequest->wasPosted();
 89+ $this->BlockAnonOnly = $wgRequest->getBool( 'wpAnonOnly', $byDefault );
 90+ $this->BlockCreateAccount = $wgRequest->getBool( 'wpCreateAccount', $byDefault );
 91+ $this->BlockEnableAutoblock = $wgRequest->getBool( 'wpEnableAutoblock', $byDefault );
 92+ $this->BlockEmail = false;
 93+ if( self::canBlockEmail( $wgUser ) ) {
 94+ $this->BlockEmail = $wgRequest->getBool( 'wpEmailBan', false );
 95+ }
 96+ $this->BlockWatchUser = $wgRequest->getBool( 'wpWatchUser', false ) && $wgUser->isLoggedIn();
 97+ # Re-check user's rights to hide names, very serious, defaults to null
 98+ if( $wgUser->isAllowed( 'hideuser' ) ) {
 99+ $this->BlockHideName = $wgRequest->getBool( 'wpHideName', null );
 100+ } else {
 101+ $this->BlockHideName = false;
 102+ }
 103+ $this->BlockAllowUsertalk = ( $wgRequest->getBool( 'wpAllowUsertalk', $byDefault ) && $wgBlockAllowsUTEdit );
 104+ $this->BlockReblock = $wgRequest->getBool( 'wpChangeBlock', false );
 105+
 106+ $this->wasPosted = $wgRequest->wasPosted();
 107+ }
 108+
 109+ public function showForm( $err ) {
 110+ global $wgOut, $wgUser;
 111+
 112+ $wgOut->setPageTitle( wfMsg( 'blockip-title' ) );
 113+ $wgOut->addWikiMsg( 'blockiptext' );
 114+
 115+ $mIpaddress = Xml::label( wfMsg( 'ipadressorusername' ), 'mw-bi-target' );
 116+ $mIpbexpiry = Xml::label( wfMsg( 'ipbexpiry' ), 'wpBlockExpiry' );
 117+ $mIpbother = Xml::label( wfMsg( 'ipbother' ), 'mw-bi-other' );
 118+ $mIpbreasonother = Xml::label( wfMsg( 'ipbreason' ), 'wpBlockReasonList' );
 119+ $mIpbreason = Xml::label( wfMsg( 'ipbotherreason' ), 'mw-bi-reason' );
 120+
 121+ $titleObj = SpecialPage::getTitleFor( 'Blockip' );
 122+ $user = User::newFromName( $this->BlockAddress );
 123+ if ( is_object( $user ) || User::isIP( $this->BlockAddress ) ) {
 124+ $wgUser->getSkin()->setRelevantUser( is_object($user) ? $user : User::newFromName( $this->BlockAddress, false ) );
 125+ }
 126+
 127+ $alreadyBlocked = false;
 128+ $otherBlockedMsgs = array();
 129+ if( $err && $err[0] != 'ipb_already_blocked' ) {
 130+ $key = array_shift( $err );
 131+ $msg = wfMsgExt( $key, 'parsemag', $err );
 132+ $wgOut->setSubtitle( wfMsgHtml( 'formerror' ) );
 133+ $wgOut->addHTML( Xml::tags( 'p', array( 'class' => 'error' ), $msg ) );
 134+ } elseif( $this->BlockAddress !== null ) {
 135+ # Get other blocks, i.e. from GlobalBlocking or TorBlock extension
 136+ wfRunHooks( 'OtherBlockLogLink', array( &$otherBlockedMsgs, $this->BlockAddress ) );
 137+
 138+ $userId = is_object( $user ) ? $user->getId() : 0;
 139+ $currentBlock = Block::newFromDB( $this->BlockAddress, $userId );
 140+ if( !is_null( $currentBlock ) && !$currentBlock->mAuto && # The block exists and isn't an autoblock
 141+ ( $currentBlock->mRangeStart == $currentBlock->mRangeEnd || # The block isn't a rangeblock
 142+ # or if it is, the range is what we're about to block
 143+ ( $currentBlock->mAddress == $this->BlockAddress ) )
 144+ ) {
 145+ $alreadyBlocked = true;
 146+ # Set the block form settings to the existing block
 147+ if( !$this->wasPosted ) {
 148+ $this->BlockAnonOnly = $currentBlock->mAnonOnly;
 149+ $this->BlockCreateAccount = $currentBlock->mCreateAccount;
 150+ $this->BlockEnableAutoblock = $currentBlock->mEnableAutoblock;
 151+ $this->BlockEmail = $currentBlock->mBlockEmail;
 152+ $this->BlockHideName = $currentBlock->mHideName;
 153+ $this->BlockAllowUsertalk = $currentBlock->mAllowUsertalk;
 154+ if( $currentBlock->mExpiry == 'infinity' ) {
 155+ $this->BlockOther = 'indefinite';
 156+ } else {
 157+ $this->BlockOther = wfTimestamp( TS_ISO_8601, $currentBlock->mExpiry );
 158+ }
 159+ $this->BlockReason = $currentBlock->mReason;
 160+ }
 161+ }
 162+ }
 163+
 164+ # Show other blocks from extensions, i.e. GlockBlocking and TorBlock
 165+ if( count( $otherBlockedMsgs ) ) {
 166+ $wgOut->addHTML(
 167+ Html::rawElement( 'h2', array(), wfMsgExt( 'ipb-otherblocks-header', 'parseinline', count( $otherBlockedMsgs ) ) ) . "\n"
 168+ );
 169+ $list = '';
 170+ foreach( $otherBlockedMsgs as $link ) {
 171+ $list .= Html::rawElement( 'li', array(), $link ) . "\n";
 172+ }
 173+ $wgOut->addHTML( Html::rawElement( 'ul', array( 'class' => 'mw-blockip-alreadyblocked' ), $list ) . "\n" );
 174+ }
 175+
 176+ # Username/IP is blocked already locally
 177+ if( $alreadyBlocked ) {
 178+ $wgOut->wrapWikiMsg( "<div class='mw-ipb-needreblock'>\n$1\n</div>", array( 'ipb-needreblock', $this->BlockAddress ) );
 179+ }
 180+
 181+ $scBlockExpiryOptions = wfMsgForContent( 'ipboptions' );
 182+
 183+ $showblockoptions = $scBlockExpiryOptions != '-';
 184+ if( !$showblockoptions ) $mIpbother = $mIpbexpiry;
 185+
 186+ $blockExpiryFormOptions = Xml::option( wfMsg( 'ipbotheroption' ), 'other' );
 187+ foreach( explode( ',', $scBlockExpiryOptions ) as $option ) {
 188+ if( strpos( $option, ':' ) === false ) $option = "$option:$option";
 189+ list( $show, $value ) = explode( ':', $option );
 190+ $show = htmlspecialchars( $show );
 191+ $value = htmlspecialchars( $value );
 192+ $blockExpiryFormOptions .= Xml::option( $show, $value, $this->BlockExpiry === $value ) . "\n";
 193+ }
 194+
 195+ $reasonDropDown = Xml::listDropDown( 'wpBlockReasonList',
 196+ wfMsgForContent( 'ipbreason-dropdown' ),
 197+ wfMsgForContent( 'ipbreasonotherlist' ), $this->BlockReasonList, 'wpBlockDropDown', 4 );
 198+
 199+ # FIXME: this should actually use HTMLForm, not just some of its JavaScript
 200+ $wgOut->addModules( array( 'mediawiki.special.block', 'mediawiki.htmlform' ) );
 201+
 202+ $wgOut->addHTML(
 203+ Xml::openElement( 'form', array( 'method' => 'post', 'action' => $titleObj->getLocalURL( 'action=submit' ), 'id' => 'blockip' ) ) .
 204+ Xml::openElement( 'fieldset' ) .
 205+ Xml::element( 'legend', null, wfMsg( 'blockip-legend' ) ) .
 206+ Xml::openElement( 'table', array( 'border' => '0', 'id' => 'mw-blockip-table' ) ) .
 207+ "<tr>
 208+ <td class='mw-label'>
 209+ {$mIpaddress}
 210+ </td>
 211+ <td class='mw-input'>" .
 212+ Html::input( 'wpBlockAddress', $this->BlockAddress, 'text', array(
 213+ 'tabindex' => '1',
 214+ 'id' => 'mw-bi-target',
 215+ 'size' => '45',
 216+ 'required' => ''
 217+ ) + ( $this->BlockAddress ? array() : array( 'autofocus' ) ) ). "
 218+ </td>
 219+ </tr>
 220+ <tr>
 221+ <td class='mw-label'>
 222+ {$mIpbexpiry}
 223+ </td>
 224+ <td class='mw-input'>"
 225+ );
 226+ if( $showblockoptions ) {
 227+ $wgOut->addHTML(
 228+ Xml::tags( 'select',
 229+ array(
 230+ 'id' => 'wpBlockExpiry',
 231+ 'name' => 'wpBlockExpiry',
 232+ 'class' => 'mw-htmlform-select-or-other', # FIXME: actually make this use HTMLForm
 233+ 'tabindex' => '2' ),
 234+ $blockExpiryFormOptions
 235+ ) . "<br/>\n"
 236+ );
 237+ }
 238+ $wgOut->addHTML(
 239+ Xml::input(
 240+ 'wpBlockOther',
 241+ 45,
 242+ $this->BlockOther,
 243+ array(
 244+ 'tabindex' => '3',
 245+ 'id' => 'wpBlockExpiry-other'
 246+ )
 247+ ) . "
 248+ </td>
 249+ </tr>
 250+ <tr>
 251+ <td class='mw-label'>
 252+ {$mIpbreasonother}
 253+ </td>
 254+ <td class='mw-input'>
 255+ {$reasonDropDown}
 256+ </td>
 257+ </tr>
 258+ <tr id=\"wpBlockReason\">
 259+ <td class='mw-label'>
 260+ {$mIpbreason}
 261+ </td>
 262+ <td class='mw-input'>" .
 263+ Html::input( 'wpBlockReason', $this->BlockReason, 'text', array(
 264+ 'tabindex' => '5',
 265+ 'id' => 'mw-bi-reason',
 266+ 'maxlength' => '200',
 267+ 'size' => '45'
 268+ ) + ( $this->BlockAddress ? array( 'autofocus' ) : array() ) ) . "
 269+ </td>
 270+ </tr>
 271+ <tr id='wpCreateAccountRow'>
 272+ <td>&#160;</td>
 273+ <td class='mw-input'>" .
 274+ Xml::checkLabel( wfMsg( 'ipbcreateaccount' ),
 275+ 'wpCreateAccount', 'wpCreateAccount', $this->BlockCreateAccount,
 276+ array( 'tabindex' => '7' ) ) . "
 277+ </td>
 278+ </tr>"
 279+ );
 280+
 281+ if( self::canBlockEmail( $wgUser ) ) {
 282+ $wgOut->addHTML("
 283+ <tr id='wpEnableEmailBan'>
 284+ <td>&#160;</td>
 285+ <td class='mw-input'>" .
 286+ Xml::checkLabel( wfMsg( 'ipbemailban' ),
 287+ 'wpEmailBan', 'wpEmailBan', $this->BlockEmail,
 288+ array( 'tabindex' => '9' ) ) . "
 289+ </td>
 290+ </tr>"
 291+ );
 292+ }
 293+
 294+ # Can we explicitly disallow the use of user_talk?
 295+ global $wgBlockAllowsUTEdit;
 296+ if( $wgBlockAllowsUTEdit ){
 297+ $wgOut->addHTML("
 298+ <tr id='wpAllowUsertalkRow'>
 299+ <td>&#160;</td>
 300+ <td class='mw-input'>" .
 301+ Xml::checkLabel( wfMsg( 'ipballowusertalk' ),
 302+ 'wpAllowUsertalk', 'wpAllowUsertalk', $this->BlockAllowUsertalk,
 303+ array( 'tabindex' => '12' ) ) . "
 304+ </td>
 305+ </tr>"
 306+ );
 307+ }
 308+
 309+ $wgOut->addHTML( "
 310+ <tr id='wpEnableAutoblockRow'>
 311+ <td>&#160;</td>
 312+ <td class='mw-input'>" .
 313+ Xml::checkLabel( wfMsg( 'ipbenableautoblock' ),
 314+ 'wpEnableAutoblock', 'wpEnableAutoblock', $this->BlockEnableAutoblock,
 315+ array( 'tabindex' => '8' ) ) . "
 316+ </td>
 317+ </tr>"
 318+ );
 319+
 320+ // Allow some users to hide name from block log, blocklist and listusers
 321+ if( $wgUser->isAllowed( 'hideuser' ) ) {
 322+ $wgOut->addHTML("
 323+ <tr id='wpEnableHideUser'>
 324+ <td>&#160;</td>
 325+ <td class='mw-input'><strong>" .
 326+ Xml::checkLabel( wfMsg( 'ipbhidename' ),
 327+ 'wpHideName', 'wpHideName', $this->BlockHideName,
 328+ array( 'tabindex' => '10' )
 329+ ) . "
 330+ </strong></td>
 331+ </tr>"
 332+ );
 333+ }
 334+
 335+ # Watchlist their user page? (Only if user is logged in)
 336+ if( $wgUser->isLoggedIn() ) {
 337+ $wgOut->addHTML("
 338+ <tr id='wpEnableWatchUser'>
 339+ <td>&#160;</td>
 340+ <td class='mw-input'>" .
 341+ Xml::checkLabel( wfMsg( 'ipbwatchuser' ),
 342+ 'wpWatchUser', 'wpWatchUser', $this->BlockWatchUser,
 343+ array( 'tabindex' => '11' ) ) . "
 344+ </td>
 345+ </tr>"
 346+ );
 347+ }
 348+
 349+ $wgOut->addHTML("
 350+ <tr id='wpAnonOnlyRow'>
 351+ <td>&#160;</td>
 352+ <td class='mw-input'>" .
 353+ Xml::checkLabel( wfMsg( 'ipbanononly' ),
 354+ 'wpAnonOnly', 'wpAnonOnly', $this->BlockAnonOnly,
 355+ array( 'tabindex' => '6' ) ) . "
 356+ </td>
 357+ </tr>
 358+ <tr>
 359+ <td style='padding-top: 1em'>&#160;</td>
 360+ <td class='mw-submit' style='padding-top: 1em'>" .
 361+ Xml::submitButton( wfMsg( $alreadyBlocked ? 'ipb-change-block' : 'ipbsubmit' ),
 362+ array( 'name' => 'wpBlock', 'tabindex' => '13' )
 363+ + $wgUser->getSkin()->tooltipAndAccessKeyAttribs( 'blockip-block' ) ). "
 364+ </td>
 365+ </tr>" .
 366+ Xml::closeElement( 'table' ) .
 367+ Html::hidden( 'wpEditToken', $wgUser->editToken() ) .
 368+ ( $alreadyBlocked ? Html::hidden( 'wpChangeBlock', 1 ) : "" ) .
 369+ Xml::closeElement( 'fieldset' ) .
 370+ Xml::closeElement( 'form' )
 371+ );
 372+
 373+ $wgOut->addHTML( $this->getConvenienceLinks() );
 374+
 375+ if( is_object( $user ) ) {
 376+ $this->showLogFragment( $wgOut, $user->getUserPage() );
 377+ } elseif( preg_match( '/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/', $this->BlockAddress ) ) {
 378+ $this->showLogFragment( $wgOut, Title::makeTitle( NS_USER, $this->BlockAddress ) );
 379+ } elseif( preg_match( '/^\w{1,4}:\w{1,4}:\w{1,4}:\w{1,4}:\w{1,4}:\w{1,4}:\w{1,4}:\w{1,4}/', $this->BlockAddress ) ) {
 380+ $this->showLogFragment( $wgOut, Title::makeTitle( NS_USER, $this->BlockAddress ) );
 381+ }
 382+ }
 383+
 384+ /**
 385+ * Can we do an email block?
 386+ * @param $user User: the sysop wanting to make a block
 387+ * @return Boolean
 388+ */
 389+ public static function canBlockEmail( $user ) {
 390+ global $wgEnableUserEmail, $wgSysopEmailBans;
 391+ return ( $wgEnableUserEmail && $wgSysopEmailBans && $user->isAllowed( 'blockemail' ) );
 392+ }
 393+
 394+ /**
 395+ * bug 15810: blocked admins should not be able to block/unblock
 396+ * others, and probably shouldn't be able to unblock themselves
 397+ * either.
 398+ * @param $user User|Int|String
 399+ */
 400+ public static function checkUnblockSelf( $user ) {
 401+ global $wgUser;
 402+ if ( is_int( $user ) ) {
 403+ $user = User::newFromId( $user );
 404+ } elseif ( is_string( $user ) ) {
 405+ $user = User::newFromName( $user );
 406+ }
 407+ if( $user instanceof User && $user->getId() == $wgUser->getId() ) {
 408+ # User is trying to unblock themselves
 409+ if ( $wgUser->isAllowed( 'unblockself' ) ) {
 410+ return true;
 411+ } else {
 412+ return 'ipbnounblockself';
 413+ }
 414+ } else {
 415+ # User is trying to block/unblock someone else
 416+ return 'ipbblocked';
 417+ }
 418+ }
 419+
 420+ /**
 421+ * Backend block code.
 422+ * $userID and $expiry will be filled accordingly
 423+ * @return array(message key, arguments) on failure, empty array on success
 424+ */
 425+ function doBlock( &$userId = null, &$expiry = null ) {
 426+ global $wgUser, $wgBlockAllowsUTEdit, $wgBlockCIDRLimit;
 427+
 428+ $userId = 0;
 429+ # Expand valid IPv6 addresses, usernames are left as is
 430+ $this->BlockAddress = IP::sanitizeIP( $this->BlockAddress );
 431+ # isIPv4() and IPv6() are used for final validation
 432+ $rxIP4 = '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}';
 433+ $rxIP6 = '\w{1,4}:\w{1,4}:\w{1,4}:\w{1,4}:\w{1,4}:\w{1,4}:\w{1,4}:\w{1,4}';
 434+ $rxIP = "($rxIP4|$rxIP6)";
 435+
 436+ # Check for invalid specifications
 437+ if( !preg_match( "/^$rxIP$/", $this->BlockAddress ) ) {
 438+ $matches = array();
 439+ if( preg_match( "/^($rxIP4)\\/(\\d{1,2})$/", $this->BlockAddress, $matches ) ) {
 440+ # IPv4
 441+ if( $wgBlockCIDRLimit['IPv4'] != 32 ){
 442+ if( !IP::isIPv4( $this->BlockAddress ) || $matches[2] > 32 ) {
 443+ return array( 'ip_range_invalid' );
 444+ } elseif ( $matches[2] < $wgBlockCIDRLimit['IPv4'] ) {
 445+ return array( 'ip_range_toolarge', $wgBlockCIDRLimit['IPv4'] );
 446+ }
 447+ $this->BlockAddress = Block::normaliseRange( $this->BlockAddress );
 448+ } else {
 449+ # Range block illegal
 450+ return array( 'range_block_disabled' );
 451+ }
 452+ } elseif( preg_match( "/^($rxIP6)\\/(\\d{1,3})$/", $this->BlockAddress, $matches ) ) {
 453+ # IPv6
 454+ if( $wgBlockCIDRLimit['IPv6'] != 128 ) {
 455+ if( !IP::isIPv6( $this->BlockAddress ) || $matches[2] > 128 ) {
 456+ return array( 'ip_range_invalid' );
 457+ } elseif( $matches[2] < $wgBlockCIDRLimit['IPv6'] ) {
 458+ return array( 'ip_range_toolarge', $wgBlockCIDRLimit['IPv6'] );
 459+ }
 460+ $this->BlockAddress = Block::normaliseRange( $this->BlockAddress );
 461+ } else {
 462+ # Range block illegal
 463+ return array('range_block_disabled');
 464+ }
 465+ } else {
 466+ # Username block
 467+ $user = User::newFromName( $this->BlockAddress );
 468+ if( $user instanceof User && $user->getId() ) {
 469+ # Use canonical name
 470+ $userId = $user->getId();
 471+ $this->BlockAddress = $user->getName();
 472+ } else {
 473+ return array( 'nosuchusershort', htmlspecialchars( $user ? $user->getName() : $this->BlockAddress ) );
 474+ }
 475+ }
 476+ }
 477+
 478+ if( $wgUser->isBlocked() && ( $wgUser->getId() !== $userId ) ) {
 479+ return array( 'cant-block-while-blocked' );
 480+ }
 481+
 482+ $reasonstr = $this->BlockReasonList;
 483+ if( $reasonstr != 'other' && $this->BlockReason != '' ) {
 484+ // Entry from drop down menu + additional comment
 485+ $reasonstr .= wfMsgForContent( 'colon-separator' ) . $this->BlockReason;
 486+ } elseif( $reasonstr == 'other' ) {
 487+ $reasonstr = $this->BlockReason;
 488+ }
 489+
 490+ $expirestr = $this->BlockExpiry;
 491+ if( $expirestr == 'other' )
 492+ $expirestr = $this->BlockOther;
 493+
 494+ if( ( strlen( $expirestr ) == 0) || ( strlen( $expirestr ) > 50 ) ) {
 495+ return array( 'ipb_expiry_invalid' );
 496+ }
 497+
 498+ if( false === ( $expiry = Block::parseExpiryInput( $expirestr ) ) ) {
 499+ // Bad expiry.
 500+ return array( 'ipb_expiry_invalid' );
 501+ }
 502+
 503+ if( $this->BlockHideName ) {
 504+ // Recheck params here...
 505+ if( !$userId || !$wgUser->isAllowed('hideuser') ) {
 506+ $this->BlockHideName = false; // IP users should not be hidden
 507+ } elseif( $expiry !== 'infinity' ) {
 508+ // Bad expiry.
 509+ return array( 'ipb_expiry_temp' );
 510+ } elseif( User::edits( $userId ) > self::HIDEUSER_CONTRIBLIMIT ) {
 511+ // Typically, the user should have a handful of edits.
 512+ // Disallow hiding users with many edits for performance.
 513+ return array( 'ipb_hide_invalid' );
 514+ }
 515+ }
 516+
 517+ # Create block object
 518+ # Note: for a user block, ipb_address is only for display purposes
 519+ $block = new Block( $this->BlockAddress, $userId, $wgUser->getId(),
 520+ $reasonstr, wfTimestampNow(), 0, $expiry, $this->BlockAnonOnly,
 521+ $this->BlockCreateAccount, $this->BlockEnableAutoblock, $this->BlockHideName,
 522+ $this->BlockEmail,
 523+ isset( $this->BlockAllowUsertalk ) ? $this->BlockAllowUsertalk : $wgBlockAllowsUTEdit
 524+ );
 525+
 526+ # Should this be privately logged?
 527+ $suppressLog = (bool)$this->BlockHideName;
 528+ if( wfRunHooks( 'BlockIp', array( &$block, &$wgUser ) ) ) {
 529+ # Try to insert block. Is there a conflicting block?
 530+ if( !$block->insert() ) {
 531+ # Show form unless the user is already aware of this...
 532+ if( !$this->BlockReblock ) {
 533+ return array( 'ipb_already_blocked' );
 534+ # Otherwise, try to update the block...
 535+ } else {
 536+ # This returns direct blocks before autoblocks/rangeblocks, since we should
 537+ # be sure the user is blocked by now it should work for our purposes
 538+ $currentBlock = Block::newFromDB( $this->BlockAddress, $userId );
 539+ if( $block->equals( $currentBlock ) ) {
 540+ return array( 'ipb_already_blocked' );
 541+ }
 542+ # If the name was hidden and the blocking user cannot hide
 543+ # names, then don't allow any block changes...
 544+ if( $currentBlock->mHideName && !$wgUser->isAllowed( 'hideuser' ) ) {
 545+ return array( 'cant-see-hidden-user' );
 546+ }
 547+ $currentBlock->delete();
 548+ $block->insert();
 549+ # If hiding/unhiding a name, this should go in the private logs
 550+ $suppressLog = $suppressLog || (bool)$currentBlock->mHideName;
 551+ $log_action = 'reblock';
 552+ # Unset _deleted fields if requested
 553+ if( $currentBlock->mHideName && !$this->BlockHideName ) {
 554+ RevisionDeleteUser::unsuppressUserName( $this->BlockAddress, $userId );
 555+ }
 556+ }
 557+ } else {
 558+ $log_action = 'block';
 559+ }
 560+ wfRunHooks( 'BlockIpComplete', array( $block, $wgUser ) );
 561+
 562+ # Set *_deleted fields if requested
 563+ if( $this->BlockHideName ) {
 564+ RevisionDeleteUser::suppressUserName( $this->BlockAddress, $userId );
 565+ }
 566+
 567+ # Only show watch link when this is no range block
 568+ if( $this->BlockWatchUser && $block->mRangeStart == $block->mRangeEnd ) {
 569+ $wgUser->addWatch( Title::makeTitle( NS_USER, $this->BlockAddress ) );
 570+ }
 571+
 572+ # Block constructor sanitizes certain block options on insert
 573+ $this->BlockEmail = $block->mBlockEmail;
 574+ $this->BlockEnableAutoblock = $block->mEnableAutoblock;
 575+
 576+ # Prepare log parameters
 577+ $logParams = array();
 578+ $logParams[] = $expirestr;
 579+ $logParams[] = $this->blockLogFlags();
 580+
 581+ # Make log entry, if the name is hidden, put it in the oversight log
 582+ $log_type = $suppressLog ? 'suppress' : 'block';
 583+ $log = new LogPage( $log_type );
 584+ $log->addEntry( $log_action, Title::makeTitle( NS_USER, $this->BlockAddress ),
 585+ $reasonstr, $logParams );
 586+
 587+ # Report to the user
 588+ return array();
 589+ } else {
 590+ return array( 'hookaborted' );
 591+ }
 592+ }
 593+
 594+ # @deprecated since 1.18
 595+ public static function suppressUserName( $name, $userId, $dbw = null ) {
 596+ return RevisionDeleteUser::suppressUserName( $name, $userId, $dbw );
 597+ }
 598+
 599+ # @deprecated since 1.18
 600+ public static function unsuppressUserName( $name, $userId, $dbw = null ) {
 601+ return RevisionDeleteUser::unsuppressUserName( $name, $userId, $dbw );
 602+ }
 603+
 604+ /**
 605+ * UI entry point for blocking
 606+ * Wraps around doBlock()
 607+ */
 608+ public function doSubmit() {
 609+ global $wgOut;
 610+ $retval = $this->doBlock();
 611+ if( empty( $retval ) ) {
 612+ $titleObj = SpecialPage::getTitleFor( 'Blockip' );
 613+ $wgOut->redirect( $titleObj->getFullURL( 'action=success&ip=' .
 614+ urlencode( $this->BlockAddress ) ) );
 615+ return;
 616+ }
 617+ $this->showForm( $retval );
 618+ }
 619+
 620+ public function showSuccess() {
 621+ global $wgOut;
 622+
 623+ $wgOut->setPageTitle( wfMsg( 'blockip-title' ) );
 624+ $wgOut->setSubtitle( wfMsg( 'blockipsuccesssub' ) );
 625+ $text = wfMsgExt( 'blockipsuccesstext', array( 'parse' ), $this->BlockAddress );
 626+ $wgOut->addHTML( $text );
 627+ }
 628+
 629+ private function showLogFragment( $out, $title ) {
 630+ global $wgUser;
 631+
 632+ // Used to support GENDER in 'blocklog-showlog' and 'blocklog-showsuppresslog'
 633+ $userBlocked = $title->getText();
 634+
 635+ LogEventsList::showLogExtract(
 636+ $out,
 637+ 'block',
 638+ $title->getPrefixedText(),
 639+ '',
 640+ array(
 641+ 'lim' => 10,
 642+ 'msgKey' => array(
 643+ 'blocklog-showlog',
 644+ $userBlocked
 645+ ),
 646+ 'showIfEmpty' => false
 647+ )
 648+ );
 649+
 650+ // Add suppression block entries if allowed
 651+ if( $wgUser->isAllowed( 'suppressionlog' ) ) {
 652+ LogEventsList::showLogExtract( $out, 'suppress', $title->getPrefixedText(), '',
 653+ array(
 654+ 'lim' => 10,
 655+ 'conds' => array(
 656+ 'log_action' => array(
 657+ 'block',
 658+ 'reblock',
 659+ 'unblock'
 660+ )
 661+ ),
 662+ 'msgKey' => array(
 663+ 'blocklog-showsuppresslog',
 664+ $userBlocked
 665+ ),
 666+ 'showIfEmpty' => false
 667+ )
 668+ );
 669+ }
 670+ }
 671+
 672+ /**
 673+ * Return a comma-delimited list of "flags" to be passed to the log
 674+ * reader for this block, to provide more information in the logs
 675+ *
 676+ * @return array
 677+ */
 678+ private function blockLogFlags() {
 679+ global $wgBlockAllowsUTEdit;
 680+ $flags = array();
 681+ if( $this->BlockAnonOnly && IP::isIPAddress( $this->BlockAddress ) )
 682+ // when blocking a user the option 'anononly' is not available/has no effect -> do not write this into log
 683+ $flags[] = 'anononly';
 684+ if( $this->BlockCreateAccount )
 685+ $flags[] = 'nocreate';
 686+ if( !$this->BlockEnableAutoblock && !IP::isIPAddress( $this->BlockAddress ) )
 687+ // Same as anononly, this is not displayed when blocking an IP address
 688+ $flags[] = 'noautoblock';
 689+ if( $this->BlockEmail )
 690+ $flags[] = 'noemail';
 691+ if( !$this->BlockAllowUsertalk && $wgBlockAllowsUTEdit )
 692+ $flags[] = 'nousertalk';
 693+ if( $this->BlockHideName )
 694+ $flags[] = 'hiddenname';
 695+ return implode( ',', $flags );
 696+ }
 697+
 698+ /**
 699+ * Builds unblock and block list links
 700+ *
 701+ * @return string
 702+ */
 703+ private function getConvenienceLinks() {
 704+ global $wgUser, $wgLang;
 705+ $skin = $wgUser->getSkin();
 706+ if( $this->BlockAddress )
 707+ $links[] = $this->getContribsLink( $skin );
 708+ $links[] = $this->getUnblockLink( $skin );
 709+ $links[] = $this->getBlockListLink( $skin );
 710+ if ( $wgUser->isAllowed( 'editinterface' ) ) {
 711+ $title = Title::makeTitle( NS_MEDIAWIKI, 'Ipbreason-dropdown' );
 712+ $links[] = $skin->link(
 713+ $title,
 714+ wfMsgHtml( 'ipb-edit-dropdown' ),
 715+ array(),
 716+ array( 'action' => 'edit' )
 717+ );
 718+ }
 719+ return '<p class="mw-ipb-conveniencelinks">' . $wgLang->pipeList( $links ) . '</p>';
 720+ }
 721+
 722+ /**
 723+ * Build a convenient link to a user or IP's contribs
 724+ * form
 725+ *
 726+ * @param $skin Skin to use
 727+ * @return string
 728+ */
 729+ private function getContribsLink( $skin ) {
 730+ $contribsPage = SpecialPage::getTitleFor( 'Contributions', $this->BlockAddress );
 731+ return $skin->link( $contribsPage, wfMsgExt( 'ipb-blocklist-contribs', 'escape', $this->BlockAddress ) );
 732+ }
 733+
 734+ /**
 735+ * Build a convenient link to unblock the given username or IP
 736+ * address, if available; otherwise link to a blank unblock
 737+ * form
 738+ *
 739+ * @param $skin Skin to use
 740+ * @return string
 741+ */
 742+ private function getUnblockLink( $skin ) {
 743+ $list = SpecialPage::getTitleFor( 'Ipblocklist' );
 744+ $query = array( 'action' => 'unblock' );
 745+
 746+ if( $this->BlockAddress ) {
 747+ $addr = strtr( $this->BlockAddress, '_', ' ' );
 748+ $message = wfMsg( 'ipb-unblock-addr', $addr );
 749+ $query['ip'] = $this->BlockAddress;
 750+ } else {
 751+ $message = wfMsg( 'ipb-unblock' );
 752+ }
 753+ return $skin->linkKnown(
 754+ $list,
 755+ htmlspecialchars( $message ),
 756+ array(),
 757+ $query
 758+ );
 759+ }
 760+
 761+ /**
 762+ * Build a convenience link to the block list
 763+ *
 764+ * @param $skin Skin to use
 765+ * @return string
 766+ */
 767+ private function getBlockListLink( $skin ) {
 768+ return $skin->linkKnown(
 769+ SpecialPage::getTitleFor( 'Ipblocklist' ),
 770+ wfMsg( 'ipb-blocklist' )
 771+ );
 772+ }
 773+
 774+ /**
 775+ * Block a list of selected users
 776+ *
 777+ * @param $users Array
 778+ * @param $reason String
 779+ * @param $tag String: replaces user pages
 780+ * @param $talkTag String: replaces user talk pages
 781+ * @return Array: list of html-safe usernames
 782+ */
 783+ public static function doMassUserBlock( $users, $reason = '', $tag = '', $talkTag = '' ) {
 784+ global $wgUser;
 785+ $counter = $blockSize = 0;
 786+ $safeUsers = array();
 787+ $log = new LogPage( 'block' );
 788+ foreach( $users as $name ) {
 789+ # Enforce limits
 790+ $counter++;
 791+ $blockSize++;
 792+ # Lets not go *too* fast
 793+ if( $blockSize >= 20 ) {
 794+ $blockSize = 0;
 795+ wfWaitForSlaves( 5 );
 796+ }
 797+ $u = User::newFromName( $name, false );
 798+ // If user doesn't exist, it ought to be an IP then
 799+ if( is_null( $u ) || ( !$u->getId() && !IP::isIPAddress( $u->getName() ) ) ) {
 800+ continue;
 801+ }
 802+ $userTitle = $u->getUserPage();
 803+ $userTalkTitle = $u->getTalkPage();
 804+ $userpage = new Article( $userTitle );
 805+ $usertalk = new Article( $userTalkTitle );
 806+ $safeUsers[] = '[[' . $userTitle->getPrefixedText() . '|' . $userTitle->getText() . ']]';
 807+ $expirestr = $u->getId() ? 'indefinite' : '1 week';
 808+ $expiry = Block::parseExpiryInput( $expirestr );
 809+ $anonOnly = IP::isIPAddress( $u->getName() ) ? 1 : 0;
 810+ // Create the block
 811+ $block = new Block( $u->getName(), // victim
 812+ $u->getId(), // uid
 813+ $wgUser->getId(), // blocker
 814+ $reason, // comment
 815+ wfTimestampNow(), // block time
 816+ 0, // auto ?
 817+ $expiry, // duration
 818+ $anonOnly, // anononly?
 819+ 1, // block account creation?
 820+ 1, // autoblocking?
 821+ 0, // suppress name?
 822+ 0 // block from sending email?
 823+ );
 824+ $oldblock = Block::newFromDB( $u->getName(), $u->getId() );
 825+ if( !$oldblock ) {
 826+ $block->insert();
 827+ # Prepare log parameters
 828+ $logParams = array();
 829+ $logParams[] = $expirestr;
 830+ if( $anonOnly ) {
 831+ $logParams[] = 'anononly';
 832+ }
 833+ $logParams[] = 'nocreate';
 834+ # Add log entry
 835+ $log->addEntry( 'block', $userTitle, $reason, $logParams );
 836+ }
 837+ # Tag userpage! (check length to avoid mistakes)
 838+ if( strlen( $tag ) > 2 ) {
 839+ $userpage->doEdit( $tag, $reason, EDIT_MINOR );
 840+ }
 841+ if( strlen( $talkTag ) > 2 ) {
 842+ $usertalk->doEdit( $talkTag, $reason, EDIT_MINOR );
 843+ }
 844+ }
 845+ return $safeUsers;
 846+ }
 847+}
Property changes on: trunk/phase3/includes/specials/SpecialBlockip.php
___________________________________________________________________
Added: svn:mergeinfo
1848 Merged /branches/REL1_15/phase3/includes/specials/SpecialBlockip.php:r51646
2849 Merged /branches/sqlite/includes/specials/SpecialBlockip.php:r58211-58321
3850 Merged /branches/wmf-deployment/includes/specials/SpecialBlockip.php:r53381,56967
Added: svn:eol-style
4851 + native
Added: svn:keywords
5852 + Author Date Id Revision

Follow-up revisions

RevisionCommit summaryAuthorDate
r83794Follow-up r83790: svn-move SpecialBlockip.php to SpecialBlock.php. This revi...happy-melon22:51, 12 March 2011

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r83786Divert a river through the Augean Stables that is SpecialBlockip.php....happy-melon21:54, 12 March 2011

Status & tagging log