In Special:RevisionDelete:
[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;
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( self::canBlockEmail( $wgUser ) ) {
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 if( self::canBlockEmail( $wgUser ) ) {
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
275 # Can we explicitly disallow the use of user_talk?
276 global $wgBlockAllowsUTEdit;
277 if( $wgBlockAllowsUTEdit ){
278 $wgOut->addHTML("
279 <tr id='wpAllowUsertalkRow'>
280 <td>&nbsp;</td>
281 <td class='mw-input'>" .
282 Xml::checkLabel( wfMsg( 'ipballowusertalk' ),
283 'wpAllowUsertalk', 'wpAllowUsertalk', $this->BlockAllowUsertalk,
284 array( 'tabindex' => '12' ) ) . "
285 </td>
286 </tr>"
287 );
288 }
289
290 $wgOut->addHTML("
291 <tr>
292 <td style='padding-top: 1em'>&nbsp;</td>
293 <td class='mw-submit' style='padding-top: 1em'>" .
294 Xml::submitButton( wfMsg( $alreadyBlocked ? 'ipb-change-block' : 'ipbsubmit' ),
295 array( 'name' => 'wpBlock', 'tabindex' => '13', 'accesskey' => 's' ) ) . "
296 </td>
297 </tr>" .
298 Xml::closeElement( 'table' ) .
299 Xml::hidden( 'wpEditToken', $wgUser->editToken() ) .
300 ( $alreadyBlocked ? Xml::hidden( 'wpChangeBlock', 1 ) : "" ) .
301 Xml::closeElement( 'fieldset' ) .
302 Xml::closeElement( 'form' ) .
303 Xml::tags( 'script', array( 'type' => 'text/javascript' ), 'updateBlockOptions()' ) . "\n"
304 );
305
306 $wgOut->addHTML( $this->getConvenienceLinks() );
307
308 if( is_object( $user ) ) {
309 $this->showLogFragment( $wgOut, $user->getUserPage() );
310 } elseif( preg_match( '/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/', $this->BlockAddress ) ) {
311 $this->showLogFragment( $wgOut, Title::makeTitle( NS_USER, $this->BlockAddress ) );
312 } 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 ) ) {
313 $this->showLogFragment( $wgOut, Title::makeTitle( NS_USER, $this->BlockAddress ) );
314 }
315 }
316
317 /**
318 * Can we do an email block?
319 * @param User $user The sysop wanting to make a block
320 * @return boolean
321 */
322 public static function canBlockEmail( $user ) {
323 global $wgEnableUserEmail, $wgSysopEmailBans;
324 return ($wgEnableUserEmail && $wgSysopEmailBans && $user->isAllowed( 'blockemail' ));
325 }
326
327 /**
328 * Backend block code.
329 * $userID and $expiry will be filled accordingly
330 * @return array(message key, arguments) on failure, empty array on success
331 */
332 function doBlock( &$userId = null, &$expiry = null ) {
333 global $wgUser, $wgSysopUserBans, $wgSysopRangeBans, $wgBlockAllowsUTEdit;
334
335 $userId = 0;
336 # Expand valid IPv6 addresses, usernames are left as is
337 $this->BlockAddress = IP::sanitizeIP( $this->BlockAddress );
338 # isIPv4() and IPv6() are used for final validation
339 $rxIP4 = '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}';
340 $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}';
341 $rxIP = "($rxIP4|$rxIP6)";
342
343 # Check for invalid specifications
344 if( !preg_match( "/^$rxIP$/", $this->BlockAddress ) ) {
345 $matches = array();
346 if( preg_match( "/^($rxIP4)\\/(\\d{1,2})$/", $this->BlockAddress, $matches ) ) {
347 # IPv4
348 if( $wgSysopRangeBans ) {
349 if( !IP::isIPv4( $this->BlockAddress ) || $matches[2] < 16 || $matches[2] > 32 ) {
350 return array('ip_range_invalid');
351 }
352 $this->BlockAddress = Block::normaliseRange( $this->BlockAddress );
353 } else {
354 # Range block illegal
355 return array('range_block_disabled');
356 }
357 } else if( preg_match( "/^($rxIP6)\\/(\\d{1,3})$/", $this->BlockAddress, $matches ) ) {
358 # IPv6
359 if( $wgSysopRangeBans ) {
360 if( !IP::isIPv6( $this->BlockAddress ) || $matches[2] < 64 || $matches[2] > 128 ) {
361 return array('ip_range_invalid');
362 }
363 $this->BlockAddress = Block::normaliseRange( $this->BlockAddress );
364 } else {
365 # Range block illegal
366 return array('range_block_disabled');
367 }
368 } else {
369 # Username block
370 if( $wgSysopUserBans ) {
371 $user = User::newFromName( $this->BlockAddress );
372 if( !is_null( $user ) && $user->getId() ) {
373 # Use canonical name
374 $userId = $user->getId();
375 $this->BlockAddress = $user->getName();
376 } else {
377 return array('nosuchusershort', htmlspecialchars( $user ? $user->getName() : $this->BlockAddress ) );
378 }
379 } else {
380 return array('badipaddress');
381 }
382 }
383 }
384
385 if( $wgUser->isBlocked() && ( $wgUser->getId() !== $userId ) ) {
386 return array( 'cant-block-while-blocked' );
387 }
388
389 $reasonstr = $this->BlockReasonList;
390 if( $reasonstr != 'other' && $this->BlockReason != '' ) {
391 // Entry from drop down menu + additional comment
392 $reasonstr .= wfMsgForContent( 'colon-separator' ) . $this->BlockReason;
393 } elseif( $reasonstr == 'other' ) {
394 $reasonstr = $this->BlockReason;
395 }
396
397 $expirestr = $this->BlockExpiry;
398 if( $expirestr == 'other' )
399 $expirestr = $this->BlockOther;
400
401 if( ( strlen( $expirestr ) == 0) || ( strlen( $expirestr ) > 50) ) {
402 return array('ipb_expiry_invalid');
403 }
404
405 if( false === ($expiry = Block::parseExpiryInput( $expirestr )) ) {
406 // Bad expiry.
407 return array('ipb_expiry_invalid');
408 }
409
410 if( $this->BlockHideName ) {
411 // Recheck params here...
412 if( !$userId || !$wgUser->isAllowed('hideuser') ) {
413 $this->BlockHideName = false; // IP users should not be hidden
414 } else if( $expiry !== 'infinity' ) {
415 // Bad expiry.
416 return array('ipb_expiry_temp');
417 } else if( User::edits($userId) > self::HIDEUSER_CONTRIBLIMIT ) {
418 // Typically, the user should have a handful of edits.
419 // Disallow hiding users with many edits for performance.
420 return array('ipb_hide_invalid');
421 }
422 }
423
424 # Create block object
425 # Note: for a user block, ipb_address is only for display purposes
426 $block = new Block( $this->BlockAddress, $userId, $wgUser->getId(),
427 $reasonstr, wfTimestampNow(), 0, $expiry, $this->BlockAnonOnly,
428 $this->BlockCreateAccount, $this->BlockEnableAutoblock, $this->BlockHideName,
429 $this->BlockEmail, isset( $this->BlockAllowUsertalk ) ? $this->BlockAllowUsertalk : $wgBlockAllowsUTEdit
430 );
431
432 # Should this be privately logged?
433 $suppressLog = (bool)$this->BlockHideName;
434 if( wfRunHooks('BlockIp', array(&$block, &$wgUser)) ) {
435 # Try to insert block. Is there a conflicting block?
436 if( !$block->insert() ) {
437 # Show form unless the user is already aware of this...
438 if( !$this->BlockReblock ) {
439 return array( 'ipb_already_blocked' );
440 # Otherwise, try to update the block...
441 } else {
442 # This returns direct blocks before autoblocks/rangeblocks, since we should
443 # be sure the user is blocked by now it should work for our purposes
444 $currentBlock = Block::newFromDB( $this->BlockAddress, $userId );
445 if( $block->equals( $currentBlock ) ) {
446 return array( 'ipb_already_blocked' );
447 }
448 # If the name was hidden and the blocking user cannot hide
449 # names, then don't allow any block changes...
450 if( $currentBlock->mHideName && !$wgUser->isAllowed('hideuser') ) {
451 return array( 'hookaborted' );
452 }
453 $currentBlock->delete();
454 $block->insert();
455 # If hiding/unhiding a name, this should go in the private logs
456 $suppressLog = $suppressLog || (bool)$currentBlock->mHideName;
457 $log_action = 'reblock';
458 # Unset _deleted fields if requested
459 if( $currentBlock->mHideName && !$this->BlockHideName ) {
460 self::unsuppressUserName( $this->BlockAddress, $userId );
461 }
462 }
463 } else {
464 $log_action = 'block';
465 }
466 wfRunHooks('BlockIpComplete', array($block, $wgUser));
467
468 # Set *_deleted fields if requested
469 if( $this->BlockHideName ) {
470 self::suppressUserName( $this->BlockAddress, $userId );
471 }
472
473 # Only show watch link when this is no range block
474 if( $this->BlockWatchUser && $block->mRangeStart == $block->mRangeEnd ) {
475 $wgUser->addWatch( Title::makeTitle( NS_USER, $this->BlockAddress ) );
476 }
477
478 # Block constructor sanitizes certain block options on insert
479 $this->BlockEmail = $block->mBlockEmail;
480 $this->BlockEnableAutoblock = $block->mEnableAutoblock;
481
482 # Prepare log parameters
483 $logParams = array();
484 $logParams[] = $expirestr;
485 $logParams[] = $this->blockLogFlags();
486
487 # Make log entry, if the name is hidden, put it in the oversight log
488 $log_type = $suppressLog ? 'suppress' : 'block';
489 $log = new LogPage( $log_type );
490 $log->addEntry( $log_action, Title::makeTitle( NS_USER, $this->BlockAddress ),
491 $reasonstr, $logParams );
492
493 # Report to the user
494 return array();
495 } else {
496 return array('hookaborted');
497 }
498 }
499
500 public static function suppressUserName( $name, $userId ) {
501 $op = '|'; // bitwise OR
502 return self::setUsernameBitfields( $name, $userId, $op );
503 }
504
505 public static function unsuppressUserName( $name, $userId ) {
506 $op = '&'; // bitwise AND
507 return self::setUsernameBitfields( $name, $userId, $op );
508 }
509
510 private static function setUsernameBitfields( $name, $userId, $op ) {
511 if( $op !== '|' && $op !== '&' ) return false; // sanity check
512 $dbw = wfGetDB( DB_MASTER );
513 $delUser = Revision::DELETED_USER | Revision::DELETED_RESTRICTED;
514 $delAction = LogPage::DELETED_ACTION | Revision::DELETED_RESTRICTED;
515 # Normalize user name
516 $userTitle = Title::makeTitleSafe( NS_USER, $name );
517 $userDbKey = $userTitle->getDBkey();
518 # To suppress, we OR the current bitfields with Revision::DELETED_USER
519 # to put a 1 in the username *_deleted bit. To unsuppress we AND the
520 # current bitfields with the inverse of Revision::DELETED_USER. The
521 # username bit is made to 0 (x & 0 = 0), while others are unchanged (x & 1 = x).
522 # The same goes for the sysop-restricted *_deleted bit.
523 if( $op == '&' ) {
524 $delUser = "~{$delUser}";
525 $delAction = "~{$delAction}";
526 }
527 # Hide name from live edits
528 $dbw->update( 'revision', array("rev_deleted = rev_deleted $op $delUser"),
529 array('rev_user' => $userId), __METHOD__ );
530 # Hide name from deleted edits
531 $dbw->update( 'archive', array("ar_deleted = ar_deleted $op $delUser"),
532 array('ar_user_text' => $name), __METHOD__ );
533 # Hide name from logs
534 $dbw->update( 'logging', array("log_deleted = log_deleted $op $delUser"),
535 array('log_user' => $userId, "log_type != 'suppress'"), __METHOD__ );
536 $dbw->update( 'logging', array("log_deleted = log_deleted $op $delAction"),
537 array('log_namespace' => NS_USER, 'log_title' => $userDbKey,
538 "log_type != 'suppress'"), __METHOD__ );
539 # Hide name from RC
540 $dbw->update( 'recentchanges', array("rc_deleted = rc_deleted $op $delUser"),
541 array('rc_user_text' => $name), __METHOD__ );
542 $dbw->update( 'recentchanges', array("rc_deleted = rc_deleted $op $delAction"),
543 array('rc_namespace' => NS_USER, 'rc_title' => $userDbKey, 'rc_logid > 0'), __METHOD__ );
544 # Hide name from live images
545 $dbw->update( 'oldimage', array("oi_deleted = oi_deleted $op $delUser"),
546 array('oi_user_text' => $name), __METHOD__ );
547 # Hide name from deleted images
548 # WMF - schema change pending
549 # $dbw->update( 'filearchive', array("fa_deleted = fa_deleted $op $delUser"),
550 # array('fa_user_text' => $name), __METHOD__ );
551 # Done!
552 return true;
553 }
554
555 /**
556 * UI entry point for blocking
557 * Wraps around doBlock()
558 */
559 public function doSubmit() {
560 global $wgOut;
561 $retval = $this->doBlock();
562 if( empty($retval) ) {
563 $titleObj = SpecialPage::getTitleFor( 'Blockip' );
564 $wgOut->redirect( $titleObj->getFullURL( 'action=success&ip=' .
565 urlencode( $this->BlockAddress ) ) );
566 return;
567 }
568 $this->showForm( $retval );
569 }
570
571 public function showSuccess() {
572 global $wgOut;
573
574 $wgOut->setPagetitle( wfMsg( 'blockip' ) );
575 $wgOut->setSubtitle( wfMsg( 'blockipsuccesssub' ) );
576 $text = wfMsgExt( 'blockipsuccesstext', array( 'parse' ), $this->BlockAddress );
577 $wgOut->addHTML( $text );
578 }
579
580 private function showLogFragment( $out, $title ) {
581 global $wgUser;
582 $out->addHTML( Xml::element( 'h2', NULL, LogPage::logName( 'block' ) ) );
583 $count = LogEventsList::showLogExtract( $out, 'block', $title->getPrefixedText(), '', 10 );
584 if( $count > 10 ) {
585 $out->addHTML( $wgUser->getSkin()->link(
586 SpecialPage::getTitleFor( 'Log' ),
587 wfMsgHtml( 'blocklog-fulllog' ),
588 array(),
589 array(
590 'type' => 'block',
591 'page' => $title->getPrefixedText() ) ) );
592 }
593 // Add suppression block entries if allowed
594 if( $wgUser->isAllowed('hideuser') ) {
595 $out->addHTML( Xml::element( 'h2', NULL, LogPage::logName( 'suppress' ) ) );
596 LogEventsList::showLogExtract( $out, 'suppress', $title->getPrefixedText(), '',
597 10, array('log_action' => array('block','reblock','unblock')) );
598 }
599 }
600
601 /**
602 * Return a comma-delimited list of "flags" to be passed to the log
603 * reader for this block, to provide more information in the logs
604 *
605 * @return array
606 */
607 private function blockLogFlags() {
608 global $wgBlockAllowsUTEdit;
609 $flags = array();
610 if( $this->BlockAnonOnly && IP::isIPAddress( $this->BlockAddress ) )
611 // when blocking a user the option 'anononly' is not available/has no effect -> do not write this into log
612 $flags[] = 'anononly';
613 if( $this->BlockCreateAccount )
614 $flags[] = 'nocreate';
615 if( !$this->BlockEnableAutoblock && !IP::isIPAddress( $this->BlockAddress ) )
616 // Same as anononly, this is not displayed when blocking an IP address
617 $flags[] = 'noautoblock';
618 if( $this->BlockEmail )
619 $flags[] = 'noemail';
620 if( !$this->BlockAllowUsertalk && $wgBlockAllowsUTEdit )
621 $flags[] = 'nousertalk';
622 if( $this->BlockHideName )
623 $flags[] = 'hiddenname';
624 return implode( ',', $flags );
625 }
626
627 /**
628 * Builds unblock and block list links
629 *
630 * @return string
631 */
632 private function getConvenienceLinks() {
633 global $wgUser, $wgLang;
634 $skin = $wgUser->getSkin();
635 if( $this->BlockAddress )
636 $links[] = $this->getContribsLink( $skin );
637 $links[] = $this->getUnblockLink( $skin );
638 $links[] = $this->getBlockListLink( $skin );
639 $links[] = $skin->makeLink ( 'MediaWiki:Ipbreason-dropdown', wfMsgHtml( 'ipb-edit-dropdown' ) );
640 return '<p class="mw-ipb-conveniencelinks">' . $wgLang->pipeList( $links ) . '</p>';
641 }
642
643 /**
644 * Build a convenient link to a user or IP's contribs
645 * form
646 *
647 * @param $skin Skin to use
648 * @return string
649 */
650 private function getContribsLink( $skin ) {
651 $contribsPage = SpecialPage::getTitleFor( 'Contributions', $this->BlockAddress );
652 return $skin->link( $contribsPage, wfMsgHtml( 'ipb-blocklist-contribs', $this->BlockAddress ) );
653 }
654
655 /**
656 * Build a convenient link to unblock the given username or IP
657 * address, if available; otherwise link to a blank unblock
658 * form
659 *
660 * @param $skin Skin to use
661 * @return string
662 */
663 private function getUnblockLink( $skin ) {
664 $list = SpecialPage::getTitleFor( 'Ipblocklist' );
665 if( $this->BlockAddress ) {
666 $addr = htmlspecialchars( strtr( $this->BlockAddress, '_', ' ' ) );
667 return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-unblock-addr', $addr ),
668 'action=unblock&ip=' . urlencode( $this->BlockAddress ) );
669 } else {
670 return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-unblock' ), 'action=unblock' );
671 }
672 }
673
674 /**
675 * Build a convenience link to the block list
676 *
677 * @param $skin Skin to use
678 * @return string
679 */
680 private function getBlockListLink( $skin ) {
681 $list = SpecialPage::getTitleFor( 'Ipblocklist' );
682 if( $this->BlockAddress ) {
683 $addr = htmlspecialchars( strtr( $this->BlockAddress, '_', ' ' ) );
684 return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-blocklist-addr', $addr ),
685 'ip=' . urlencode( $this->BlockAddress ) );
686 } else {
687 return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-blocklist' ) );
688 }
689 }
690
691 /**
692 * Block a list of selected users
693 * @param array $users
694 * @param string $reason
695 * @param string $tag replaces user pages
696 * @param string $talkTag replaces user talk pages
697 * @returns array, list of html-safe usernames
698 */
699 public static function doMassUserBlock( $users, $reason = '', $tag = '', $talkTag = '' ) {
700 global $wgUser;
701 $counter = $blockSize = 0;
702 $safeUsers = array();
703 $log = new LogPage( 'block' );
704 foreach( $users as $name ) {
705 # Enforce limits
706 $counter++;
707 $blockSize++;
708 # Lets not go *too* fast
709 if( $blockSize >= 20 ) {
710 $blockSize = 0;
711 wfWaitForSlaves( 5 );
712 }
713 $u = User::newFromName( $name, false );
714 // If user doesn't exist, it ought to be an IP then
715 if( is_null($u) || (!$u->getId() && !IP::isIPAddress( $u->getName() )) ) {
716 continue;
717 }
718 $userTitle = $u->getUserPage();
719 $userTalkTitle = $u->getTalkPage();
720 $userpage = new Article( $userTitle );
721 $usertalk = new Article( $userTalkTitle );
722 $safeUsers[] = '[[' . $userTitle->getPrefixedText() . '|' . $userTitle->getText() . ']]';
723 $expirestr = $u->getId() ? 'indefinite' : '1 week';
724 $expiry = Block::parseExpiryInput( $expirestr );
725 $anonOnly = IP::isIPAddress( $u->getName() ) ? 1 : 0;
726 // Create the block
727 $block = new Block( $u->getName(), // victim
728 $u->getId(), // uid
729 $wgUser->getId(), // blocker
730 $reason, // comment
731 wfTimestampNow(), // block time
732 0, // auto ?
733 $expiry, // duration
734 $anonOnly, // anononly?
735 1, // block account creation?
736 1, // autoblocking?
737 0, // suppress name?
738 0 // block from sending email?
739 );
740 $oldblock = Block::newFromDB( $u->getName(), $u->getId() );
741 if( !$oldblock ) {
742 $block->insert();
743 # Prepare log parameters
744 $logParams = array();
745 $logParams[] = $expirestr;
746 if( $anonOnly ) {
747 $logParams[] = 'anononly';
748 }
749 $logParams[] = 'nocreate';
750 # Add log entry
751 $log->addEntry( 'block', $userTitle, $reason, $logParams );
752 }
753 # Tag userpage! (check length to avoid mistakes)
754 if( strlen($tag) > 2 ) {
755 $userpage->doEdit( $tag, $reason, EDIT_MINOR );
756 }
757 if( strlen($talkTag) > 2 ) {
758 $usertalk->doEdit( $talkTag, $reason, EDIT_MINOR );
759 }
760 }
761 return $safeUsers;
762 }
763 }