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