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