Fix a bunch of '? true : false' instances
[lhc/web/wiklou.git] / includes / specials / SpecialBlockip.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;
32 // The maximum number of edits a user can have and still be hidden
33 const HIDEUSER_CONTRIBLIMIT = 1000;
34
35 public function __construct() {
36 parent::__construct( 'Blockip', 'block' );
37 }
38
39 public function execute( $par ) {
40 global $wgUser, $wgOut, $wgRequest;
41
42 # Can't block when the database is locked
43 if( wfReadOnly() ) {
44 $wgOut->readOnlyPage();
45 return;
46 }
47 # Permission check
48 if( !$this->userCanExecute( $wgUser ) ) {
49 $wgOut->permissionRequired( 'block' );
50 return;
51 }
52
53 $this->setup( $par );
54
55 # bug 15810: blocked admins should have limited access here
56 if ( $wgUser->isBlocked() ) {
57 $status = IPBlockForm::checkUnblockSelf( $ipb->BlockAddress );
58 if ( $status !== true ) {
59 throw new ErrorPageError( 'badaccess', $status );
60 }
61 }
62
63 $action = $wgRequest->getVal( 'action' );
64 if( 'success' == $action ) {
65 $this->showSuccess();
66 } elseif( $wgRequest->wasPosted() && 'submit' == $action &&
67 $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ) ) ) {
68 $this->doSubmit();
69 } else {
70 $this->showForm( '' );
71 }
72 }
73
74 private function setup( $par ) {
75 global $wgRequest, $wgUser, $wgBlockAllowsUTEdit;
76
77 $this->BlockAddress = $wgRequest->getVal( 'wpBlockAddress', $wgRequest->getVal( 'ip', $par ) );
78 $this->BlockAddress = strtr( $this->BlockAddress, '_', ' ' );
79 $this->BlockReason = $wgRequest->getText( 'wpBlockReason' );
80 $this->BlockReasonList = $wgRequest->getText( 'wpBlockReasonList' );
81 $this->BlockExpiry = $wgRequest->getVal( 'wpBlockExpiry', wfMsg( 'ipbotheroption' ) );
82 $this->BlockOther = $wgRequest->getVal( 'wpBlockOther', '' );
83
84 # Unchecked checkboxes are not included in the form data at all, so having one
85 # that is true by default is a bit tricky
86 $byDefault = !$wgRequest->wasPosted();
87 $this->BlockAnonOnly = $wgRequest->getBool( 'wpAnonOnly', $byDefault );
88 $this->BlockCreateAccount = $wgRequest->getBool( 'wpCreateAccount', $byDefault );
89 $this->BlockEnableAutoblock = $wgRequest->getBool( 'wpEnableAutoblock', $byDefault );
90 $this->BlockEmail = false;
91 if( self::canBlockEmail( $wgUser ) ) {
92 $this->BlockEmail = $wgRequest->getBool( 'wpEmailBan', false );
93 }
94 $this->BlockWatchUser = $wgRequest->getBool( 'wpWatchUser', false ) && $wgUser->isLoggedIn();
95 # Re-check user's rights to hide names, very serious, defaults to null
96 if( $wgUser->isAllowed( 'hideuser' ) ) {
97 $this->BlockHideName = $wgRequest->getBool( 'wpHideName', null );
98 } else {
99 $this->BlockHideName = false;
100 }
101 $this->BlockAllowUsertalk = ( $wgRequest->getBool( 'wpAllowUsertalk', $byDefault ) && $wgBlockAllowsUTEdit );
102 $this->BlockReblock = $wgRequest->getBool( 'wpChangeBlock', false );
103
104 $this->wasPosted = $wgRequest->wasPosted();
105 }
106
107 public function showForm( $err ) {
108 global $wgOut, $wgUser, $wgSysopUserBans;
109
110 $wgOut->setPageTitle( wfMsg( 'blockip-title' ) );
111 $wgOut->addWikiMsg( 'blockiptext' );
112
113 if( $wgSysopUserBans ) {
114 $mIpaddress = Xml::label( wfMsg( 'ipadressorusername' ), 'mw-bi-target' );
115 } else {
116 $mIpaddress = Xml::label( wfMsg( 'ipaddress' ), 'mw-bi-target' );
117 }
118 $mIpbexpiry = Xml::label( wfMsg( 'ipbexpiry' ), 'wpBlockExpiry' );
119 $mIpbother = Xml::label( wfMsg( 'ipbother' ), 'mw-bi-other' );
120 $mIpbreasonother = Xml::label( wfMsg( 'ipbreason' ), 'wpBlockReasonList' );
121 $mIpbreason = Xml::label( wfMsg( 'ipbotherreason' ), 'mw-bi-reason' );
122
123 $titleObj = SpecialPage::getTitleFor( 'Blockip' );
124 $user = User::newFromName( $this->BlockAddress );
125
126 $alreadyBlocked = false;
127 $otherBlockedMsgs = array();
128 if( $err && $err[0] != 'ipb_already_blocked' ) {
129 $key = array_shift( $err );
130 $msg = wfMsgReal( $key, $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 $wgOut->addModules( 'mediawiki.legacy.block' );
199 $wgOut->addHTML(
200 Xml::openElement( 'form', array( 'method' => 'post', 'action' => $titleObj->getLocalURL( 'action=submit' ), 'id' => 'blockip' ) ) .
201 Xml::openElement( 'fieldset' ) .
202 Xml::element( 'legend', null, wfMsg( 'blockip-legend' ) ) .
203 Xml::openElement( 'table', array( 'border' => '0', 'id' => 'mw-blockip-table' ) ) .
204 "<tr>
205 <td class='mw-label'>
206 {$mIpaddress}
207 </td>
208 <td class='mw-input'>" .
209 Html::input( 'wpBlockAddress', $this->BlockAddress, 'text', array(
210 'tabindex' => '1',
211 'id' => 'mw-bi-target',
212 'onchange' => 'updateBlockOptions()',
213 'size' => '45',
214 'required' => ''
215 ) + ( $this->BlockAddress ? array() : array( 'autofocus' ) ) ). "
216 </td>
217 </tr>
218 <tr>"
219 );
220 if( $showblockoptions ) {
221 $wgOut->addHTML("
222 <td class='mw-label'>
223 {$mIpbexpiry}
224 </td>
225 <td class='mw-input'>" .
226 Xml::tags( 'select',
227 array(
228 'id' => 'wpBlockExpiry',
229 'name' => 'wpBlockExpiry',
230 'onchange' => 'considerChangingExpiryFocus()',
231 'tabindex' => '2' ),
232 $blockExpiryFormOptions ) .
233 "</td>"
234 );
235 }
236 $wgOut->addHTML("
237 </tr>
238 <tr id='wpBlockOther'>
239 <td class='mw-label'>
240 {$mIpbother}
241 </td>
242 <td class='mw-input'>" .
243 Xml::input( 'wpBlockOther', 45, $this->BlockOther,
244 array( 'tabindex' => '3', 'id' => 'mw-bi-other' ) ) . "
245 </td>
246 </tr>
247 <tr>
248 <td class='mw-label'>
249 {$mIpbreasonother}
250 </td>
251 <td class='mw-input'>
252 {$reasonDropDown}
253 </td>
254 </tr>
255 <tr id=\"wpBlockReason\">
256 <td class='mw-label'>
257 {$mIpbreason}
258 </td>
259 <td class='mw-input'>" .
260 Html::input( 'wpBlockReason', $this->BlockReason, 'text', array(
261 'tabindex' => '5',
262 'id' => 'mw-bi-reason',
263 'maxlength' => '200',
264 'size' => '45'
265 ) + ( $this->BlockAddress ? array( 'autofocus' ) : array() ) ) . "
266 </td>
267 </tr>
268 <tr id='wpAnonOnlyRow'>
269 <td>&#160;</td>
270 <td class='mw-input'>" .
271 Xml::checkLabel( wfMsg( 'ipbanononly' ),
272 'wpAnonOnly', 'wpAnonOnly', $this->BlockAnonOnly,
273 array( 'tabindex' => '6' ) ) . "
274 </td>
275 </tr>
276 <tr id='wpCreateAccountRow'>
277 <td>&#160;</td>
278 <td class='mw-input'>" .
279 Xml::checkLabel( wfMsg( 'ipbcreateaccount' ),
280 'wpCreateAccount', 'wpCreateAccount', $this->BlockCreateAccount,
281 array( 'tabindex' => '7' ) ) . "
282 </td>
283 </tr>
284 <tr id='wpEnableAutoblockRow'>
285 <td>&#160;</td>
286 <td class='mw-input'>" .
287 Xml::checkLabel( wfMsg( 'ipbenableautoblock' ),
288 'wpEnableAutoblock', 'wpEnableAutoblock', $this->BlockEnableAutoblock,
289 array( 'tabindex' => '8' ) ) . "
290 </td>
291 </tr>"
292 );
293
294 if( self::canBlockEmail( $wgUser ) ) {
295 $wgOut->addHTML("
296 <tr id='wpEnableEmailBan'>
297 <td>&#160;</td>
298 <td class='mw-input'>" .
299 Xml::checkLabel( wfMsg( 'ipbemailban' ),
300 'wpEmailBan', 'wpEmailBan', $this->BlockEmail,
301 array( 'tabindex' => '9' ) ) . "
302 </td>
303 </tr>"
304 );
305 }
306
307 // Allow some users to hide name from block log, blocklist and listusers
308 if( $wgUser->isAllowed( 'hideuser' ) ) {
309 $wgOut->addHTML("
310 <tr id='wpEnableHideUser'>
311 <td>&#160;</td>
312 <td class='mw-input'><strong>" .
313 Xml::checkLabel( wfMsg( 'ipbhidename' ),
314 'wpHideName', 'wpHideName', $this->BlockHideName,
315 array( 'tabindex' => '10' )
316 ) . "
317 </strong></td>
318 </tr>"
319 );
320 }
321
322 # Watchlist their user page? (Only if user is logged in)
323 if( $wgUser->isLoggedIn() ) {
324 $wgOut->addHTML("
325 <tr id='wpEnableWatchUser'>
326 <td>&#160;</td>
327 <td class='mw-input'>" .
328 Xml::checkLabel( wfMsg( 'ipbwatchuser' ),
329 'wpWatchUser', 'wpWatchUser', $this->BlockWatchUser,
330 array( 'tabindex' => '11' ) ) . "
331 </td>
332 </tr>"
333 );
334 }
335
336 # Can we explicitly disallow the use of user_talk?
337 global $wgBlockAllowsUTEdit;
338 if( $wgBlockAllowsUTEdit ){
339 $wgOut->addHTML("
340 <tr id='wpAllowUsertalkRow'>
341 <td>&#160;</td>
342 <td class='mw-input'>" .
343 Xml::checkLabel( wfMsg( 'ipballowusertalk' ),
344 'wpAllowUsertalk', 'wpAllowUsertalk', $this->BlockAllowUsertalk,
345 array( 'tabindex' => '12' ) ) . "
346 </td>
347 </tr>"
348 );
349 }
350
351 $wgOut->addHTML("
352 <tr>
353 <td style='padding-top: 1em'>&#160;</td>
354 <td class='mw-submit' style='padding-top: 1em'>" .
355 Xml::submitButton( wfMsg( $alreadyBlocked ? 'ipb-change-block' : 'ipbsubmit' ),
356 array( 'name' => 'wpBlock', 'tabindex' => '13' )
357 + $wgUser->getSkin()->tooltipAndAccessKeyAttribs( 'blockip-block' ) ). "
358 </td>
359 </tr>" .
360 Xml::closeElement( 'table' ) .
361 Xml::hidden( 'wpEditToken', $wgUser->editToken() ) .
362 ( $alreadyBlocked ? Xml::hidden( 'wpChangeBlock', 1 ) : "" ) .
363 Xml::closeElement( 'fieldset' ) .
364 Xml::closeElement( 'form' ) .
365 Xml::tags( 'script', array( 'type' => 'text/javascript' ), 'updateBlockOptions()' ) . "\n"
366 );
367
368 $wgOut->addHTML( $this->getConvenienceLinks() );
369
370 if( is_object( $user ) ) {
371 $this->showLogFragment( $wgOut, $user->getUserPage() );
372 } elseif( preg_match( '/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/', $this->BlockAddress ) ) {
373 $this->showLogFragment( $wgOut, Title::makeTitle( NS_USER, $this->BlockAddress ) );
374 } 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 ) ) {
375 $this->showLogFragment( $wgOut, Title::makeTitle( NS_USER, $this->BlockAddress ) );
376 }
377 }
378
379 /**
380 * Can we do an email block?
381 * @param $user User: the sysop wanting to make a block
382 * @return Boolean
383 */
384 public static function canBlockEmail( $user ) {
385 global $wgEnableUserEmail, $wgSysopEmailBans;
386 return ( $wgEnableUserEmail && $wgSysopEmailBans && $user->isAllowed( 'blockemail' ) );
387 }
388
389 /**
390 * bug 15810: blocked admins should not be able to block/unblock
391 * others, and probably shouldn't be able to unblock themselves
392 * either.
393 * @param $user User, Int or String
394 */
395 public static function checkUnblockSelf( $user ) {
396 global $wgUser;
397 if ( is_int( $user ) ) {
398 $user = User::newFromId( $user );
399 } elseif ( is_string( $user ) ) {
400 $user = User::newFromName( $user );
401 }
402 if( $user instanceof User && $user->getId() == $wgUser->getId() ) {
403 # User is trying to unblock themselves
404 if ( $wgUser->isAllowed( 'unblockself' ) ) {
405 return true;
406 } else {
407 return 'ipbnounblockself';
408 }
409 } else {
410 # User is trying to block/unblock someone else
411 return 'ipbblocked';
412 }
413 }
414
415 /**
416 * Backend block code.
417 * $userID and $expiry will be filled accordingly
418 * @return array(message key, arguments) on failure, empty array on success
419 */
420 function doBlock( &$userId = null, &$expiry = null ) {
421 global $wgUser, $wgSysopUserBans, $wgSysopRangeBans, $wgBlockAllowsUTEdit, $wgBlockCIDRLimit;
422
423 $userId = 0;
424 # Expand valid IPv6 addresses, usernames are left as is
425 $this->BlockAddress = IP::sanitizeIP( $this->BlockAddress );
426 # isIPv4() and IPv6() are used for final validation
427 $rxIP4 = '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}';
428 $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}';
429 $rxIP = "($rxIP4|$rxIP6)";
430
431 # Check for invalid specifications
432 if( !preg_match( "/^$rxIP$/", $this->BlockAddress ) ) {
433 $matches = array();
434 if( preg_match( "/^($rxIP4)\\/(\\d{1,2})$/", $this->BlockAddress, $matches ) ) {
435 # IPv4
436 if( $wgSysopRangeBans ) {
437 if( !IP::isIPv4( $this->BlockAddress ) || $matches[2] > 32 ) {
438 return array( 'ip_range_invalid' );
439 } elseif ( $matches[2] < $wgBlockCIDRLimit['IPv4'] ) {
440 return array( 'ip_range_toolarge', $wgBlockCIDRLimit['IPv4'] );
441 }
442 $this->BlockAddress = Block::normaliseRange( $this->BlockAddress );
443 } else {
444 # Range block illegal
445 return array( 'range_block_disabled' );
446 }
447 } elseif( preg_match( "/^($rxIP6)\\/(\\d{1,3})$/", $this->BlockAddress, $matches ) ) {
448 # IPv6
449 if( $wgSysopRangeBans ) {
450 if( !IP::isIPv6( $this->BlockAddress ) || $matches[2] > 128 ) {
451 return array( 'ip_range_invalid' );
452 } elseif( $matches[2] < $wgBlockCIDRLimit['IPv6'] ) {
453 return array( 'ip_range_toolarge', $wgBlockCIDRLimit['IPv6'] );
454 }
455 $this->BlockAddress = Block::normaliseRange( $this->BlockAddress );
456 } else {
457 # Range block illegal
458 return array('range_block_disabled');
459 }
460 } else {
461 # Username block
462 if( $wgSysopUserBans ) {
463 $user = User::newFromName( $this->BlockAddress );
464 if( $user instanceof User && $user->getId() ) {
465 # Use canonical name
466 $userId = $user->getId();
467 $this->BlockAddress = $user->getName();
468 } else {
469 return array( 'nosuchusershort', htmlspecialchars( $user ? $user->getName() : $this->BlockAddress ) );
470 }
471 } else {
472 return array( 'badipaddress' );
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 self::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 self::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 public static function suppressUserName( $name, $userId, $dbw = null ) {
594 $op = '|'; // bitwise OR
595 return self::setUsernameBitfields( $name, $userId, $op, $dbw );
596 }
597
598 public static function unsuppressUserName( $name, $userId, $dbw = null ) {
599 $op = '&'; // bitwise AND
600 return self::setUsernameBitfields( $name, $userId, $op, $dbw );
601 }
602
603 private static function setUsernameBitfields( $name, $userId, $op, $dbw ) {
604 if( $op !== '|' && $op !== '&' ) return false; // sanity check
605 if( !$dbw )
606 $dbw = wfGetDB( DB_MASTER );
607 $delUser = Revision::DELETED_USER | Revision::DELETED_RESTRICTED;
608 $delAction = LogPage::DELETED_ACTION | Revision::DELETED_RESTRICTED;
609 # Normalize user name
610 $userTitle = Title::makeTitleSafe( NS_USER, $name );
611 $userDbKey = $userTitle->getDBkey();
612 # To suppress, we OR the current bitfields with Revision::DELETED_USER
613 # to put a 1 in the username *_deleted bit. To unsuppress we AND the
614 # current bitfields with the inverse of Revision::DELETED_USER. The
615 # username bit is made to 0 (x & 0 = 0), while others are unchanged (x & 1 = x).
616 # The same goes for the sysop-restricted *_deleted bit.
617 if( $op == '&' ) {
618 $delUser = "~{$delUser}";
619 $delAction = "~{$delAction}";
620 }
621 # Hide name from live edits
622 $dbw->update( 'revision', array( "rev_deleted = rev_deleted $op $delUser" ),
623 array( 'rev_user' => $userId ), __METHOD__ );
624 # Hide name from deleted edits
625 $dbw->update( 'archive', array( "ar_deleted = ar_deleted $op $delUser" ),
626 array( 'ar_user_text' => $name ), __METHOD__ );
627 # Hide name from logs
628 $dbw->update( 'logging', array( "log_deleted = log_deleted $op $delUser" ),
629 array( 'log_user' => $userId, "log_type != 'suppress'" ), __METHOD__ );
630 $dbw->update( 'logging', array( "log_deleted = log_deleted $op $delAction" ),
631 array( 'log_namespace' => NS_USER, 'log_title' => $userDbKey,
632 "log_type != 'suppress'" ), __METHOD__ );
633 # Hide name from RC
634 $dbw->update( 'recentchanges', array( "rc_deleted = rc_deleted $op $delUser" ),
635 array( 'rc_user_text' => $name ), __METHOD__ );
636 $dbw->update( 'recentchanges', array( "rc_deleted = rc_deleted $op $delAction" ),
637 array( 'rc_namespace' => NS_USER, 'rc_title' => $userDbKey, 'rc_logid > 0' ), __METHOD__ );
638 # Hide name from live images
639 $dbw->update( 'oldimage', array( "oi_deleted = oi_deleted $op $delUser" ),
640 array( 'oi_user_text' => $name ), __METHOD__ );
641 # Hide name from deleted images
642 # WMF - schema change pending
643 # $dbw->update( 'filearchive', array( "fa_deleted = fa_deleted $op $delUser" ),
644 # array( 'fa_user_text' => $name ), __METHOD__ );
645 # Done!
646 return true;
647 }
648
649 /**
650 * UI entry point for blocking
651 * Wraps around doBlock()
652 */
653 public function doSubmit() {
654 global $wgOut;
655 $retval = $this->doBlock();
656 if( empty( $retval ) ) {
657 $titleObj = SpecialPage::getTitleFor( 'Blockip' );
658 $wgOut->redirect( $titleObj->getFullURL( 'action=success&ip=' .
659 urlencode( $this->BlockAddress ) ) );
660 return;
661 }
662 $this->showForm( $retval );
663 }
664
665 public function showSuccess() {
666 global $wgOut;
667
668 $wgOut->setPageTitle( wfMsg( 'blockip-title' ) );
669 $wgOut->setSubtitle( wfMsg( 'blockipsuccesssub' ) );
670 $text = wfMsgExt( 'blockipsuccesstext', array( 'parse' ), $this->BlockAddress );
671 $wgOut->addHTML( $text );
672 }
673
674 private function showLogFragment( $out, $title ) {
675 global $wgUser;
676
677 // Used to support GENDER in 'blocklog-showlog' and 'blocklog-showsuppresslog'
678 $userBlocked = $title->getText();
679
680 LogEventsList::showLogExtract(
681 $out,
682 'block',
683 $title->getPrefixedText(),
684 '',
685 array(
686 'lim' => 10,
687 'msgKey' => array(
688 'blocklog-showlog',
689 $userBlocked
690 ),
691 'showIfEmpty' => false
692 )
693 );
694
695 // Add suppression block entries if allowed
696 if( $wgUser->isAllowed( 'suppressionlog' ) ) {
697 LogEventsList::showLogExtract( $out, 'suppress', $title->getPrefixedText(), '',
698 array(
699 'lim' => 10,
700 'conds' => array(
701 'log_action' => array(
702 'block',
703 'reblock',
704 'unblock'
705 )
706 ),
707 'msgKey' => array(
708 'blocklog-showsuppresslog',
709 $userBlocked
710 ),
711 'showIfEmpty' => false
712 )
713 );
714 }
715 }
716
717 /**
718 * Return a comma-delimited list of "flags" to be passed to the log
719 * reader for this block, to provide more information in the logs
720 *
721 * @return array
722 */
723 private function blockLogFlags() {
724 global $wgBlockAllowsUTEdit;
725 $flags = array();
726 if( $this->BlockAnonOnly && IP::isIPAddress( $this->BlockAddress ) )
727 // when blocking a user the option 'anononly' is not available/has no effect -> do not write this into log
728 $flags[] = 'anononly';
729 if( $this->BlockCreateAccount )
730 $flags[] = 'nocreate';
731 if( !$this->BlockEnableAutoblock && !IP::isIPAddress( $this->BlockAddress ) )
732 // Same as anononly, this is not displayed when blocking an IP address
733 $flags[] = 'noautoblock';
734 if( $this->BlockEmail )
735 $flags[] = 'noemail';
736 if( !$this->BlockAllowUsertalk && $wgBlockAllowsUTEdit )
737 $flags[] = 'nousertalk';
738 if( $this->BlockHideName )
739 $flags[] = 'hiddenname';
740 return implode( ',', $flags );
741 }
742
743 /**
744 * Builds unblock and block list links
745 *
746 * @return string
747 */
748 private function getConvenienceLinks() {
749 global $wgUser, $wgLang;
750 $skin = $wgUser->getSkin();
751 if( $this->BlockAddress )
752 $links[] = $this->getContribsLink( $skin );
753 $links[] = $this->getUnblockLink( $skin );
754 $links[] = $this->getBlockListLink( $skin );
755 if ( $wgUser->isAllowed( 'editinterface' ) ) {
756 $title = Title::makeTitle( NS_MEDIAWIKI, 'Ipbreason-dropdown' );
757 $links[] = $skin->link(
758 $title,
759 wfMsgHtml( 'ipb-edit-dropdown' ),
760 array(),
761 array( 'action' => 'edit' )
762 );
763 }
764 return '<p class="mw-ipb-conveniencelinks">' . $wgLang->pipeList( $links ) . '</p>';
765 }
766
767 /**
768 * Build a convenient link to a user or IP's contribs
769 * form
770 *
771 * @param $skin Skin to use
772 * @return string
773 */
774 private function getContribsLink( $skin ) {
775 $contribsPage = SpecialPage::getTitleFor( 'Contributions', $this->BlockAddress );
776 return $skin->link( $contribsPage, wfMsgExt( 'ipb-blocklist-contribs', 'escape', $this->BlockAddress ) );
777 }
778
779 /**
780 * Build a convenient link to unblock the given username or IP
781 * address, if available; otherwise link to a blank unblock
782 * form
783 *
784 * @param $skin Skin to use
785 * @return string
786 */
787 private function getUnblockLink( $skin ) {
788 $list = SpecialPage::getTitleFor( 'Ipblocklist' );
789 $query = array( 'action' => 'unblock' );
790
791 if( $this->BlockAddress ) {
792 $addr = strtr( $this->BlockAddress, '_', ' ' );
793 $message = wfMsg( 'ipb-unblock-addr', $addr );
794 $query['ip'] = $this->BlockAddress;
795 } else {
796 $message = wfMsg( 'ipb-unblock' );
797 }
798 return $skin->linkKnown(
799 $list,
800 htmlspecialchars( $message ),
801 array(),
802 $query
803 );
804 }
805
806 /**
807 * Build a convenience link to the block list
808 *
809 * @param $skin Skin to use
810 * @return string
811 */
812 private function getBlockListLink( $skin ) {
813 $list = SpecialPage::getTitleFor( 'Ipblocklist' );
814 $query = array();
815
816 if( $this->BlockAddress ) {
817 $addr = strtr( $this->BlockAddress, '_', ' ' );
818 $message = wfMsg( 'ipb-blocklist-addr', $addr );
819 $query['ip'] = $this->BlockAddress;
820 } else {
821 $message = wfMsg( 'ipb-blocklist' );
822 }
823
824 return $skin->linkKnown(
825 $list,
826 htmlspecialchars( $message ),
827 array(),
828 $query
829 );
830 }
831
832 /**
833 * Block a list of selected users
834 *
835 * @param $users Array
836 * @param $reason String
837 * @param $tag String: replaces user pages
838 * @param $talkTag String: replaces user talk pages
839 * @return Array: list of html-safe usernames
840 */
841 public static function doMassUserBlock( $users, $reason = '', $tag = '', $talkTag = '' ) {
842 global $wgUser;
843 $counter = $blockSize = 0;
844 $safeUsers = array();
845 $log = new LogPage( 'block' );
846 foreach( $users as $name ) {
847 # Enforce limits
848 $counter++;
849 $blockSize++;
850 # Lets not go *too* fast
851 if( $blockSize >= 20 ) {
852 $blockSize = 0;
853 wfWaitForSlaves( 5 );
854 }
855 $u = User::newFromName( $name, false );
856 // If user doesn't exist, it ought to be an IP then
857 if( is_null( $u ) || ( !$u->getId() && !IP::isIPAddress( $u->getName() ) ) ) {
858 continue;
859 }
860 $userTitle = $u->getUserPage();
861 $userTalkTitle = $u->getTalkPage();
862 $userpage = new Article( $userTitle );
863 $usertalk = new Article( $userTalkTitle );
864 $safeUsers[] = '[[' . $userTitle->getPrefixedText() . '|' . $userTitle->getText() . ']]';
865 $expirestr = $u->getId() ? 'indefinite' : '1 week';
866 $expiry = Block::parseExpiryInput( $expirestr );
867 $anonOnly = IP::isIPAddress( $u->getName() ) ? 1 : 0;
868 // Create the block
869 $block = new Block( $u->getName(), // victim
870 $u->getId(), // uid
871 $wgUser->getId(), // blocker
872 $reason, // comment
873 wfTimestampNow(), // block time
874 0, // auto ?
875 $expiry, // duration
876 $anonOnly, // anononly?
877 1, // block account creation?
878 1, // autoblocking?
879 0, // suppress name?
880 0 // block from sending email?
881 );
882 $oldblock = Block::newFromDB( $u->getName(), $u->getId() );
883 if( !$oldblock ) {
884 $block->insert();
885 # Prepare log parameters
886 $logParams = array();
887 $logParams[] = $expirestr;
888 if( $anonOnly ) {
889 $logParams[] = 'anononly';
890 }
891 $logParams[] = 'nocreate';
892 # Add log entry
893 $log->addEntry( 'block', $userTitle, $reason, $logParams );
894 }
895 # Tag userpage! (check length to avoid mistakes)
896 if( strlen( $tag ) > 2 ) {
897 $userpage->doEdit( $tag, $reason, EDIT_MINOR );
898 }
899 if( strlen( $talkTag ) > 2 ) {
900 $usertalk->doEdit( $talkTag, $reason, EDIT_MINOR );
901 }
902 }
903 return $safeUsers;
904 }
905 }