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