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