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