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