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