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