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