pass user ID to Block::newFromDB in case of renames per comments on r42843.
[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
21 # Permission check
22 if( !$wgUser->isAllowed( 'block' ) ) {
23 $wgOut->permissionRequired( 'block' );
24 return;
25 }
26
27 $ipb = new IPBlockForm( $par );
28
29 $action = $wgRequest->getVal( 'action' );
30 if ( 'success' == $action ) {
31 $ipb->showSuccess();
32 } else if ( $wgRequest->wasPosted() && 'submit' == $action &&
33 $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ) ) ) {
34 $ipb->doSubmit();
35 } else {
36 $ipb->showForm( '' );
37 }
38 }
39
40 /**
41 * Form object for the Special:Blockip page.
42 *
43 * @ingroup SpecialPage
44 */
45 class IPBlockForm {
46 var $BlockAddress, $BlockExpiry, $BlockReason;
47 # var $BlockEmail;
48
49 function IPBlockForm( $par ) {
50 global $wgRequest, $wgUser, $wgBlockAllowsUTEdit;
51
52 $this->BlockAddress = $wgRequest->getVal( 'wpBlockAddress', $wgRequest->getVal( 'ip', $par ) );
53 $this->BlockAddress = strtr( $this->BlockAddress, '_', ' ' );
54 $this->BlockReason = $wgRequest->getText( 'wpBlockReason' );
55 $this->BlockReasonList = $wgRequest->getText( 'wpBlockReasonList' );
56 $this->BlockExpiry = $wgRequest->getVal( 'wpBlockExpiry', wfMsg('ipbotheroption') );
57 $this->BlockOther = $wgRequest->getVal( 'wpBlockOther', '' );
58
59 # Unchecked checkboxes are not included in the form data at all, so having one
60 # that is true by default is a bit tricky
61 $byDefault = !$wgRequest->wasPosted();
62 $this->BlockAnonOnly = $wgRequest->getBool( 'wpAnonOnly', $byDefault );
63 $this->BlockCreateAccount = $wgRequest->getBool( 'wpCreateAccount', $byDefault );
64 $this->BlockEnableAutoblock = $wgRequest->getBool( 'wpEnableAutoblock', $byDefault );
65 $this->BlockEmail = $wgRequest->getBool( 'wpEmailBan', false );
66 $this->BlockWatchUser = $wgRequest->getBool( 'wpWatchUser', false );
67 # Re-check user's rights to hide names, very serious, defaults to 0
68 $this->BlockHideName = ( $wgRequest->getBool( 'wpHideName', 0 ) && $wgUser->isAllowed( 'hideuser' ) ) ? 1 : 0;
69 $this->BlockAllowUsertalk = ( $wgRequest->getBool( 'wpAllowUsertalk', $byDefault ) && $wgBlockAllowsUTEdit );
70 $this->BlockReblock = $wgRequest->getBool( 'wpChangeBlock', false );
71 }
72
73 function showForm( $err ) {
74 global $wgOut, $wgUser, $wgSysopUserBans;
75
76 $wgOut->setPagetitle( wfMsg( 'blockip' ) );
77 $wgOut->addWikiMsg( 'blockiptext' );
78
79 if($wgSysopUserBans) {
80 $mIpaddress = Xml::label( wfMsg( 'ipadressorusername' ), 'mw-bi-target' );
81 } else {
82 $mIpaddress = Xml::label( wfMsg( 'ipaddress' ), 'mw-bi-target' );
83 }
84 $mIpbexpiry = Xml::label( wfMsg( 'ipbexpiry' ), 'wpBlockExpiry' );
85 $mIpbother = Xml::label( wfMsg( 'ipbother' ), 'mw-bi-other' );
86 $mIpbreasonother = Xml::label( wfMsg( 'ipbreason' ), 'wpBlockReasonList' );
87 $mIpbreason = Xml::label( wfMsg( 'ipbotherreason' ), 'mw-bi-reason' );
88
89 $titleObj = SpecialPage::getTitleFor( 'Blockip' );
90 $user = User::newFromName( $this->BlockAddress );
91
92 $alreadyBlocked = false;
93 if ( $err && $err[0] != 'ipb_already_blocked' ) {
94 $key = array_shift($err);
95 $msg = wfMsgReal($key, $err);
96 $wgOut->setSubtitle( wfMsgHtml( 'formerror' ) );
97 $wgOut->addHTML( Xml::tags( 'p', array( 'class' => 'error' ), $msg ) );
98 } elseif ( $this->BlockAddress ) {
99 $userId = 0;
100 if ( is_object( $user ) )
101 $userId = $user->getId();
102 $currentBlock = Block::newFromDB( $this->BlockAddress, $userId );
103 if ( !is_null($currentBlock) && !$currentBlock->mAuto && !($currentBlock->mRangeStart && $currentBlock->mAddress != $this->BlockAddress) ) {
104 $wgOut->addWikiMsg( 'ipb-needreblock', $this->BlockAddress );
105 $alreadyBlocked = true;
106 }
107 }
108
109 $scBlockExpiryOptions = wfMsgForContent( 'ipboptions' );
110
111 $showblockoptions = $scBlockExpiryOptions != '-';
112 if (!$showblockoptions)
113 $mIpbother = $mIpbexpiry;
114
115 $blockExpiryFormOptions = Xml::option( wfMsg( 'ipbotheroption' ), 'other' );
116 foreach (explode(',', $scBlockExpiryOptions) as $option) {
117 if ( strpos($option, ":") === false ) $option = "$option:$option";
118 list($show, $value) = explode(":", $option);
119 $show = htmlspecialchars($show);
120 $value = htmlspecialchars($value);
121 $blockExpiryFormOptions .= Xml::option( $show, $value, $this->BlockExpiry === $value ? true : false ) . "\n";
122 }
123
124 $reasonDropDown = Xml::listDropDown( 'wpBlockReasonList',
125 wfMsgForContent( 'ipbreason-dropdown' ),
126 wfMsgForContent( 'ipbreasonotherlist' ), $this->BlockReasonList, 'wpBlockDropDown', 4 );
127
128 global $wgStylePath, $wgStyleVersion;
129 $wgOut->addHTML(
130 Xml::tags( 'script', array( 'type' => 'text/javascript', 'src' => "$wgStylePath/common/block.js?$wgStyleVersion" ), '' ) .
131 Xml::openElement( 'form', array( 'method' => 'post', 'action' => $titleObj->getLocalURL( "action=submit" ), 'id' => 'blockip' ) ) .
132 Xml::openElement( 'fieldset' ) .
133 Xml::element( 'legend', null, wfMsg( 'blockip-legend' ) ) .
134 Xml::openElement( 'table', array ( 'border' => '0', 'id' => 'mw-blockip-table' ) ) .
135 "<tr>
136 <td class='mw-label'>
137 {$mIpaddress}
138 </td>
139 <td class='mw-input'>" .
140 Xml::input( 'wpBlockAddress', 45, $this->BlockAddress,
141 array(
142 'tabindex' => '1',
143 'id' => 'mw-bi-target',
144 'onchange' => 'updateBlockOptions()' ) ). "
145 </td>
146 </tr>
147 <tr>"
148 );
149 if ( $showblockoptions ) {
150 $wgOut->addHTML("
151 <td class='mw-label'>
152 {$mIpbexpiry}
153 </td>
154 <td class='mw-input'>" .
155 Xml::tags( 'select',
156 array(
157 'id' => 'wpBlockExpiry',
158 'name' => 'wpBlockExpiry',
159 'onchange' => 'considerChangingExpiryFocus()',
160 'tabindex' => '2' ),
161 $blockExpiryFormOptions ) .
162 "</td>"
163 );
164 }
165 $wgOut->addHTML("
166 </tr>
167 <tr id='wpBlockOther'>
168 <td class='mw-label'>
169 {$mIpbother}
170 </td>
171 <td class='mw-input'>" .
172 Xml::input( 'wpBlockOther', 45, $this->BlockOther,
173 array( 'tabindex' => '3', 'id' => 'mw-bi-other' ) ) . "
174 </td>
175 </tr>
176 <tr>
177 <td class='mw-label'>
178 {$mIpbreasonother}
179 </td>
180 <td class='mw-input'>
181 {$reasonDropDown}
182 </td>
183 </tr>
184 <tr id=\"wpBlockReason\">
185 <td class='mw-label'>
186 {$mIpbreason}
187 </td>
188 <td class='mw-input'>" .
189 Xml::input( 'wpBlockReason', 45, $this->BlockReason,
190 array( 'tabindex' => '5', 'id' => 'mw-bi-reason', 'maxlength'=> '200' ) ) . "
191 </td>
192 </tr>
193 <tr id='wpAnonOnlyRow'>
194 <td>&nbsp;</td>
195 <td class='mw-input'>" .
196 Xml::checkLabel( wfMsg( 'ipbanononly' ),
197 'wpAnonOnly', 'wpAnonOnly', $this->BlockAnonOnly,
198 array( 'tabindex' => '6' ) ) . "
199 </td>
200 </tr>
201 <tr id='wpCreateAccountRow'>
202 <td>&nbsp;</td>
203 <td class='mw-input'>" .
204 Xml::checkLabel( wfMsg( 'ipbcreateaccount' ),
205 'wpCreateAccount', 'wpCreateAccount', $this->BlockCreateAccount,
206 array( 'tabindex' => '7' ) ) . "
207 </td>
208 </tr>
209 <tr id='wpEnableAutoblockRow'>
210 <td>&nbsp;</td>
211 <td class='mw-input'>" .
212 Xml::checkLabel( wfMsg( 'ipbenableautoblock' ),
213 'wpEnableAutoblock', 'wpEnableAutoblock', $this->BlockEnableAutoblock,
214 array( 'tabindex' => '8' ) ) . "
215 </td>
216 </tr>"
217 );
218
219 global $wgSysopEmailBans, $wgBlockAllowsUTEdit;
220 if ( $wgSysopEmailBans && $wgUser->isAllowed( 'blockemail' ) ) {
221 $wgOut->addHTML("
222 <tr id='wpEnableEmailBan'>
223 <td>&nbsp;</td>
224 <td class='mw-input'>" .
225 Xml::checkLabel( wfMsg( 'ipbemailban' ),
226 'wpEmailBan', 'wpEmailBan', $this->BlockEmail,
227 array( 'tabindex' => '9' )) . "
228 </td>
229 </tr>"
230 );
231 }
232
233 // Allow some users to hide name from block log, blocklist and listusers
234 if ( $wgUser->isAllowed( 'hideuser' ) ) {
235 $wgOut->addHTML("
236 <tr id='wpEnableHideUser'>
237 <td>&nbsp;</td>
238 <td class='mw-input'>" .
239 Xml::checkLabel( wfMsg( 'ipbhidename' ),
240 'wpHideName', 'wpHideName', $this->BlockHideName,
241 array( 'tabindex' => '10' ) ) . "
242 </td>
243 </tr>"
244 );
245 }
246
247 # Watchlist their user page?
248 $wgOut->addHTML("
249 <tr id='wpEnableWatchUser'>
250 <td>&nbsp;</td>
251 <td class='mw-input'>" .
252 Xml::checkLabel( wfMsg( 'ipbwatchuser' ),
253 'wpWatchUser', 'wpWatchUser', $this->BlockWatchUser,
254 array( 'tabindex' => '11' ) ) . "
255 </td>
256 </tr>"
257 );
258 if( $wgBlockAllowsUTEdit ){
259 $wgOut->addHTML("
260 <tr id='wpAllowUsertalkRow'>
261 <td>&nbsp;</td>
262 <td class='mw-input'>" .
263 Xml::checkLabel( wfMsg( 'ipballowusertalk' ),
264 'wpAllowUsertalk', 'wpAllowUsertalk', $this->BlockAllowUsertalk,
265 array( 'tabindex' => '12' ) ) . "
266 </td>
267 </tr>"
268 );
269 }
270 if ( $alreadyBlocked ) {
271 $wgOut->addHTML("
272 <tr id='wpChangeBlockRow'>
273 <td>&nbsp;</td>
274 <td class='mw-input'>" .
275 Xml::checkLabel( wfMsg( 'ipb-change-block' ),
276 'wpChangeBlock', 'wpChangeBlock', $this->BlockReblock,
277 array( 'tabindex' => '13' ) ) . "
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( 'ipbsubmit' ),
288 array( 'name' => 'wpBlock', 'tabindex' => '13', 'accesskey' => 's' ) ) . "
289 </td>
290 </tr>" .
291 Xml::closeElement( 'table' ) .
292 Xml::hidden( 'wpEditToken', $wgUser->editToken() ) .
293 Xml::closeElement( 'fieldset' ) .
294 Xml::closeElement( 'form' ) .
295 Xml::tags( 'script', array( 'type' => 'text/javascript' ), 'updateBlockOptions()' ) . "\n"
296 );
297
298 $wgOut->addHtml( $this->getConvenienceLinks() );
299
300 if( is_object( $user ) ) {
301 $this->showLogFragment( $wgOut, $user->getUserPage() );
302 } elseif( preg_match( '/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/', $this->BlockAddress ) ) {
303 $this->showLogFragment( $wgOut, Title::makeTitle( NS_USER, $this->BlockAddress ) );
304 } 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 ) ) {
305 $this->showLogFragment( $wgOut, Title::makeTitle( NS_USER, $this->BlockAddress ) );
306 }
307 }
308
309 /**
310 * Backend block code.
311 * $userID and $expiry will be filled accordingly
312 * @return array(message key, arguments) on failure, empty array on success
313 */
314 function doBlock( &$userId = null, &$expiry = null ) {
315 global $wgUser, $wgSysopUserBans, $wgSysopRangeBans, $wgBlockAllowsUTEdit;
316
317 $userId = 0;
318 # Expand valid IPv6 addresses, usernames are left as is
319 $this->BlockAddress = IP::sanitizeIP( $this->BlockAddress );
320 # isIPv4() and IPv6() are used for final validation
321 $rxIP4 = '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}';
322 $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}';
323 $rxIP = "($rxIP4|$rxIP6)";
324
325 # Check for invalid specifications
326 if ( !preg_match( "/^$rxIP$/", $this->BlockAddress ) ) {
327 $matches = array();
328 if ( preg_match( "/^($rxIP4)\\/(\\d{1,2})$/", $this->BlockAddress, $matches ) ) {
329 # IPv4
330 if ( $wgSysopRangeBans ) {
331 if ( !IP::isIPv4( $this->BlockAddress ) || $matches[2] < 16 || $matches[2] > 32 ) {
332 return array('ip_range_invalid');
333 }
334 $this->BlockAddress = Block::normaliseRange( $this->BlockAddress );
335 } else {
336 # Range block illegal
337 return array('range_block_disabled');
338 }
339 } else if ( preg_match( "/^($rxIP6)\\/(\\d{1,3})$/", $this->BlockAddress, $matches ) ) {
340 # IPv6
341 if ( $wgSysopRangeBans ) {
342 if ( !IP::isIPv6( $this->BlockAddress ) || $matches[2] < 64 || $matches[2] > 128 ) {
343 return array('ip_range_invalid');
344 }
345 $this->BlockAddress = Block::normaliseRange( $this->BlockAddress );
346 } else {
347 # Range block illegal
348 return array('range_block_disabled');
349 }
350 } else {
351 # Username block
352 if ( $wgSysopUserBans ) {
353 $user = User::newFromName( $this->BlockAddress );
354 if( !is_null( $user ) && $user->getId() ) {
355 # Use canonical name
356 $userId = $user->getId();
357 $this->BlockAddress = $user->getName();
358 } else {
359 return array('nosuchusershort', htmlspecialchars( $user ? $user->getName() : $this->BlockAddress ) );
360 }
361 } else {
362 return array('badipaddress');
363 }
364 }
365 }
366
367 if ( $wgUser->isBlocked() && ( $wgUser->getId() !== $userId ) ) {
368 return array( 'cant-block-while-blocked' );
369 }
370
371 $reasonstr = $this->BlockReasonList;
372 if ( $reasonstr != 'other' && $this->BlockReason != '' ) {
373 // Entry from drop down menu + additional comment
374 $reasonstr .= ': ' . $this->BlockReason;
375 } elseif ( $reasonstr == 'other' ) {
376 $reasonstr = $this->BlockReason;
377 }
378
379 $expirestr = $this->BlockExpiry;
380 if( $expirestr == 'other' )
381 $expirestr = $this->BlockOther;
382
383 if ( ( strlen( $expirestr ) == 0) || ( strlen( $expirestr ) > 50) ) {
384 return array('ipb_expiry_invalid');
385 }
386
387 if ( false === ($expiry = Block::parseExpiryInput( $expirestr )) ) {
388 // Bad expiry.
389 return array('ipb_expiry_invalid');
390 }
391
392 if( $this->BlockHideName && $expiry != 'infinity' ) {
393 // Bad expiry.
394 return array('ipb_expiry_temp');
395 }
396
397 # Create block
398 # Note: for a user block, ipb_address is only for display purposes
399 $block = new Block( $this->BlockAddress, $userId, $wgUser->getId(),
400 $reasonstr, wfTimestampNow(), 0, $expiry, $this->BlockAnonOnly,
401 $this->BlockCreateAccount, $this->BlockEnableAutoblock, $this->BlockHideName,
402 $this->BlockEmail, isset( $this->BlockAllowUsertalk ) ? $this->BlockAllowUsertalk : $wgBlockAllowsUTEdit );
403
404 if ( wfRunHooks('BlockIp', array(&$block, &$wgUser)) ) {
405
406 if ( !$block->insert() ) {
407 if ( !$this->BlockReblock ) {
408 return array( 'ipb_already_blocked' );
409 } else {
410 # This returns direct blocks before autoblocks/rangeblocks, since we should be sure the user is blocked by now it should work for our purposes
411 $currentBlock = Block::newFromDB( $this->BlockAddress, $userId );
412 $currentBlock->delete();
413 $block->insert();
414 $log_action = 'reblock';
415 }
416 } else {
417 $log_action = 'block';
418 }
419 wfRunHooks('BlockIpComplete', array($block, $wgUser));
420
421 if ( $this->BlockWatchUser ) {
422 $wgUser->addWatch ( Title::makeTitle( NS_USER, $this->BlockAddress ) );
423 }
424
425 # Prepare log parameters
426 $logParams = array();
427 $logParams[] = $expirestr;
428 $logParams[] = $this->blockLogFlags();
429
430 # Make log entry, if the name is hidden, put it in the oversight log
431 $log_type = ($this->BlockHideName) ? 'suppress' : 'block';
432 $log = new LogPage( $log_type );
433 $log->addEntry( $log_action, Title::makeTitle( NS_USER, $this->BlockAddress ),
434 $reasonstr, $logParams );
435
436 # Report to the user
437 return array();
438 }
439 else
440 return array('hookaborted');
441 }
442
443 /**
444 * UI entry point for blocking
445 * Wraps around doBlock()
446 */
447 function doSubmit()
448 {
449 global $wgOut;
450 $retval = $this->doBlock();
451 if(empty($retval)) {
452 $titleObj = SpecialPage::getTitleFor( 'Blockip' );
453 $wgOut->redirect( $titleObj->getFullURL( 'action=success&ip=' .
454 urlencode( $this->BlockAddress ) ) );
455 return;
456 }
457 $this->showForm( $retval );
458 }
459
460 function showSuccess() {
461 global $wgOut;
462
463 $wgOut->setPagetitle( wfMsg( 'blockip' ) );
464 $wgOut->setSubtitle( wfMsg( 'blockipsuccesssub' ) );
465 $text = wfMsgExt( 'blockipsuccesstext', array( 'parse' ), $this->BlockAddress );
466 $wgOut->addHtml( $text );
467 }
468
469 function showLogFragment( $out, $title ) {
470 global $wgUser;
471 $out->addHtml( Xml::element( 'h2', NULL, LogPage::logName( 'block' ) ) );
472 $count = LogEventsList::showLogExtract( $out, 'block', $title->getPrefixedText(), '', 10 );
473 if($count > 10){
474 $out->addHtml( $wgUser->getSkin()->link(
475 SpecialPage::getTitleFor( 'Log' ),
476 wfMsgHtml( 'blocklog-fulllog' ),
477 array(),
478 array(
479 'type' => 'block',
480 'page' => $title->getPrefixedText() ) ) );
481 }
482 }
483
484 /**
485 * Return a comma-delimited list of "flags" to be passed to the log
486 * reader for this block, to provide more information in the logs
487 *
488 * @return array
489 */
490 private function blockLogFlags() {
491 global $wgBlockAllowsUTEdit;
492 $flags = array();
493 if( $this->BlockAnonOnly && IP::isIPAddress( $this->BlockAddress ) )
494 // when blocking a user the option 'anononly' is not available/has no effect -> do not write this into log
495 $flags[] = 'anononly';
496 if( $this->BlockCreateAccount )
497 $flags[] = 'nocreate';
498 if( !$this->BlockEnableAutoblock )
499 $flags[] = 'noautoblock';
500 if ( $this->BlockEmail )
501 $flags[] = 'noemail';
502 if ( !$this->BlockAllowUsertalk && $wgBlockAllowsUTEdit )
503 $flags[] = 'nousertalk';
504 return implode( ',', $flags );
505 }
506
507 /**
508 * Builds unblock and block list links
509 *
510 * @return string
511 */
512 private function getConvenienceLinks() {
513 global $wgUser;
514 $skin = $wgUser->getSkin();
515 if( $this->BlockAddress )
516 $links[] = $this->getContribsLink( $skin );
517 $links[] = $this->getUnblockLink( $skin );
518 $links[] = $this->getBlockListLink( $skin );
519 $links[] = $skin->makeLink ( 'MediaWiki:Ipbreason-dropdown', wfMsgHtml( 'ipb-edit-dropdown' ) );
520 return '<p class="mw-ipb-conveniencelinks">' . implode( ' | ', $links ) . '</p>';
521 }
522
523 /**
524 * Build a convenient link to a user or IP's contribs
525 * form
526 *
527 * @param $skin Skin to use
528 * @return string
529 */
530 private function getContribsLink( $skin ) {
531 $contribsPage = SpecialPage::getTitleFor( 'Contributions', $this->BlockAddress );
532 return $skin->link( $contribsPage, wfMsgHtml( 'ipb-blocklist-contribs', $this->BlockAddress ) );
533 }
534
535 /**
536 * Build a convenient link to unblock the given username or IP
537 * address, if available; otherwise link to a blank unblock
538 * form
539 *
540 * @param $skin Skin to use
541 * @return string
542 */
543 private function getUnblockLink( $skin ) {
544 $list = SpecialPage::getTitleFor( 'Ipblocklist' );
545 if( $this->BlockAddress ) {
546 $addr = htmlspecialchars( strtr( $this->BlockAddress, '_', ' ' ) );
547 return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-unblock-addr', $addr ),
548 'action=unblock&ip=' . urlencode( $this->BlockAddress ) );
549 } else {
550 return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-unblock' ), 'action=unblock' );
551 }
552 }
553
554 /**
555 * Build a convenience link to the block list
556 *
557 * @param $skin Skin to use
558 * @return string
559 */
560 private function getBlockListLink( $skin ) {
561 $list = SpecialPage::getTitleFor( 'Ipblocklist' );
562 if( $this->BlockAddress ) {
563 $addr = htmlspecialchars( strtr( $this->BlockAddress, '_', ' ' ) );
564 return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-blocklist-addr', $addr ),
565 'ip=' . urlencode( $this->BlockAddress ) );
566 } else {
567 return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-blocklist' ) );
568 }
569 }
570
571 /**
572 * Block a list of selected users
573 * @param array $users
574 * @param string $reason
575 * @param string $tag
576 * @returns array, list of html-safe usernames
577 */
578 public static function doMassUserBlock( $users, $reason = '', $tag = '' ) {
579 global $wgUser;
580 $counter = $blockSize = 0;
581 $safeUsers = array();
582 $log = new LogPage( 'block' );
583 foreach( $users as $name ) {
584 # Enforce limits
585 $counter++;
586 $blockSize++;
587 # Lets not go *too* fast
588 if( $blockSize >= 20 ) {
589 $blockSize = 0;
590 wfWaitForSlaves( 5 );
591 }
592 $u = User::newFromName( $name, false );
593 // If user doesn't exist, it ought to be an IP then
594 if( is_null($u) || (!$u->getId() && !IP::isIPAddress( $u->getName() )) ) {
595 continue;
596 }
597 $usertitle = Title::makeTitle( NS_USER, $u->getName() );
598 $userpage = new Article( $usertitle );
599 $safeUsers[] = '[[' . $usertitle->getPrefixedText() . '|' . $usertitle->getText() . ']]';
600 $expirestr = $u->getId() ? 'indefinite' : '1 week';
601 $expiry = Block::parseExpiryInput( $expirestr );
602 $anonOnly = IP::isIPAddress( $u->getName() ) ? 1 : 0;
603 // Create the block
604 $block = new Block( $u->getName(), // victim
605 $u->getId(), // uid
606 $wgUser->getId(), // blocker
607 $reason, // comment
608 wfTimestampNow(), // block time
609 0, // auto ?
610 $expiry, // duration
611 $anonOnly, // anononly?
612 1, // block account creation?
613 1, // autoblocking?
614 0, // suppress name?
615 0 // block from sending email?
616 );
617 $oldblock = Block::newFromDB( $u->getName(), $u->getId() );
618 if( !$oldblock ) {
619 $block->insert();
620 # Prepare log parameters
621 $logParams = array();
622 $logParams[] = $expirestr;
623 if( $anonOnly ) {
624 $logParams[] = 'anononly';
625 }
626 $logParams[] = 'nocreate';
627 # Add log entry
628 $log->addEntry( 'block', $usertitle, $reason, $logParams );
629 }
630 # Tag userpage! (check length to avoid mistakes)
631 if( strlen($tag) > 2 ) {
632 $userpage->doEdit( $tag, $reason, EDIT_MINOR );
633 }
634 }
635 return $safeUsers;
636 }
637 }