Merge "Fix importation of weird file names in importTextFiles.php"
[lhc/web/wiklou.git] / includes / Block.php
1 <?php
2 /**
3 * Blocks and bans object
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22 class Block {
23 /** @var string */
24 public $mReason;
25
26 /** @var string */
27 public $mTimestamp;
28
29 /** @var bool */
30 public $mAuto;
31
32 /** @var string */
33 public $mExpiry;
34
35 /** @var bool */
36 public $mHideName;
37
38 /** @var int */
39 public $mParentBlockId;
40
41 /** @var int */
42 protected $mId;
43
44 /** @var bool */
45 protected $mFromMaster;
46
47 /** @var bool */
48 protected $mBlockEmail;
49
50 /** @var bool */
51 protected $mDisableUsertalk;
52
53 /** @var bool */
54 protected $mCreateAccount;
55
56 /** @var User|string */
57 protected $target;
58
59 /** @var int Hack for foreign blocking (CentralAuth) */
60 protected $forcedTargetID;
61
62 /** @var int Block::TYPE_ constant. Can only be USER, IP or RANGE internally */
63 protected $type;
64
65 /** @var User */
66 protected $blocker;
67
68 /** @var bool */
69 protected $isHardblock;
70
71 /** @var bool */
72 protected $isAutoblocking;
73
74 # TYPE constants
75 const TYPE_USER = 1;
76 const TYPE_IP = 2;
77 const TYPE_RANGE = 3;
78 const TYPE_AUTO = 4;
79 const TYPE_ID = 5;
80
81 /**
82 * Create a new block with specified parameters on a user, IP or IP range.
83 *
84 * @param array $options Parameters of the block:
85 * address string|User Target user name, User object, IP address or IP range
86 * user int Override target user ID (for foreign users)
87 * by int User ID of the blocker
88 * reason string Reason of the block
89 * timestamp string The time at which the block comes into effect
90 * auto bool Is this an automatic block?
91 * expiry string Timestamp of expiration of the block or 'infinity'
92 * anonOnly bool Only disallow anonymous actions
93 * createAccount bool Disallow creation of new accounts
94 * enableAutoblock bool Enable automatic blocking
95 * hideName bool Hide the target user name
96 * blockEmail bool Disallow sending emails
97 * allowUsertalk bool Allow the target to edit its own talk page
98 * byText string Username of the blocker (for foreign users)
99 *
100 * @since 1.26 accepts $options array instead of individual parameters; order
101 * of parameters above reflects the original order
102 */
103 function __construct( $options = [] ) {
104 $defaults = [
105 'address' => '',
106 'user' => null,
107 'by' => null,
108 'reason' => '',
109 'timestamp' => '',
110 'auto' => false,
111 'expiry' => '',
112 'anonOnly' => false,
113 'createAccount' => false,
114 'enableAutoblock' => false,
115 'hideName' => false,
116 'blockEmail' => false,
117 'allowUsertalk' => false,
118 'byText' => '',
119 ];
120
121 if ( func_num_args() > 1 || !is_array( $options ) ) {
122 $options = array_combine(
123 array_slice( array_keys( $defaults ), 0, func_num_args() ),
124 func_get_args()
125 );
126 wfDeprecated( __METHOD__ . ' with multiple arguments', '1.26' );
127 }
128
129 $options += $defaults;
130
131 $this->setTarget( $options['address'] );
132
133 if ( $this->target instanceof User && $options['user'] ) {
134 # Needed for foreign users
135 $this->forcedTargetID = $options['user'];
136 }
137
138 if ( $options['by'] ) {
139 # Local user
140 $this->setBlocker( User::newFromId( $options['by'] ) );
141 } else {
142 # Foreign user
143 $this->setBlocker( $options['byText'] );
144 }
145
146 $this->mReason = $options['reason'];
147 $this->mTimestamp = wfTimestamp( TS_MW, $options['timestamp'] );
148 $this->mExpiry = wfGetDB( DB_REPLICA )->decodeExpiry( $options['expiry'] );
149
150 # Boolean settings
151 $this->mAuto = (bool)$options['auto'];
152 $this->mHideName = (bool)$options['hideName'];
153 $this->isHardblock( !$options['anonOnly'] );
154 $this->isAutoblocking( (bool)$options['enableAutoblock'] );
155
156 # Prevention measures
157 $this->prevents( 'sendemail', (bool)$options['blockEmail'] );
158 $this->prevents( 'editownusertalk', !$options['allowUsertalk'] );
159 $this->prevents( 'createaccount', (bool)$options['createAccount'] );
160
161 $this->mFromMaster = false;
162 }
163
164 /**
165 * Load a blocked user from their block id.
166 *
167 * @param int $id Block id to search for
168 * @return Block|null
169 */
170 public static function newFromID( $id ) {
171 $dbr = wfGetDB( DB_REPLICA );
172 $res = $dbr->selectRow(
173 'ipblocks',
174 self::selectFields(),
175 [ 'ipb_id' => $id ],
176 __METHOD__
177 );
178 if ( $res ) {
179 return self::newFromRow( $res );
180 } else {
181 return null;
182 }
183 }
184
185 /**
186 * Return the list of ipblocks fields that should be selected to create
187 * a new block.
188 * @return array
189 */
190 public static function selectFields() {
191 return [
192 'ipb_id',
193 'ipb_address',
194 'ipb_by',
195 'ipb_by_text',
196 'ipb_reason',
197 'ipb_timestamp',
198 'ipb_auto',
199 'ipb_anon_only',
200 'ipb_create_account',
201 'ipb_enable_autoblock',
202 'ipb_expiry',
203 'ipb_deleted',
204 'ipb_block_email',
205 'ipb_allow_usertalk',
206 'ipb_parent_block_id',
207 ];
208 }
209
210 /**
211 * Check if two blocks are effectively equal. Doesn't check irrelevant things like
212 * the blocking user or the block timestamp, only things which affect the blocked user
213 *
214 * @param Block $block
215 *
216 * @return bool
217 */
218 public function equals( Block $block ) {
219 return (
220 (string)$this->target == (string)$block->target
221 && $this->type == $block->type
222 && $this->mAuto == $block->mAuto
223 && $this->isHardblock() == $block->isHardblock()
224 && $this->prevents( 'createaccount' ) == $block->prevents( 'createaccount' )
225 && $this->mExpiry == $block->mExpiry
226 && $this->isAutoblocking() == $block->isAutoblocking()
227 && $this->mHideName == $block->mHideName
228 && $this->prevents( 'sendemail' ) == $block->prevents( 'sendemail' )
229 && $this->prevents( 'editownusertalk' ) == $block->prevents( 'editownusertalk' )
230 && $this->mReason == $block->mReason
231 );
232 }
233
234 /**
235 * Load a block from the database which affects the already-set $this->target:
236 * 1) A block directly on the given user or IP
237 * 2) A rangeblock encompassing the given IP (smallest first)
238 * 3) An autoblock on the given IP
239 * @param User|string $vagueTarget Also search for blocks affecting this target. Doesn't
240 * make any sense to use TYPE_AUTO / TYPE_ID here. Leave blank to skip IP lookups.
241 * @throws MWException
242 * @return bool Whether a relevant block was found
243 */
244 protected function newLoad( $vagueTarget = null ) {
245 $db = wfGetDB( $this->mFromMaster ? DB_MASTER : DB_REPLICA );
246
247 if ( $this->type !== null ) {
248 $conds = [
249 'ipb_address' => [ (string)$this->target ],
250 ];
251 } else {
252 $conds = [ 'ipb_address' => [] ];
253 }
254
255 # Be aware that the != '' check is explicit, since empty values will be
256 # passed by some callers (bug 29116)
257 if ( $vagueTarget != '' ) {
258 list( $target, $type ) = self::parseTarget( $vagueTarget );
259 switch ( $type ) {
260 case self::TYPE_USER:
261 # Slightly weird, but who are we to argue?
262 $conds['ipb_address'][] = (string)$target;
263 break;
264
265 case self::TYPE_IP:
266 $conds['ipb_address'][] = (string)$target;
267 $conds[] = self::getRangeCond( IP::toHex( $target ) );
268 $conds = $db->makeList( $conds, LIST_OR );
269 break;
270
271 case self::TYPE_RANGE:
272 list( $start, $end ) = IP::parseRange( $target );
273 $conds['ipb_address'][] = (string)$target;
274 $conds[] = self::getRangeCond( $start, $end );
275 $conds = $db->makeList( $conds, LIST_OR );
276 break;
277
278 default:
279 throw new MWException( "Tried to load block with invalid type" );
280 }
281 }
282
283 $res = $db->select( 'ipblocks', self::selectFields(), $conds, __METHOD__ );
284
285 # This result could contain a block on the user, a block on the IP, and a russian-doll
286 # set of rangeblocks. We want to choose the most specific one, so keep a leader board.
287 $bestRow = null;
288
289 # Lower will be better
290 $bestBlockScore = 100;
291
292 # This is begging for $this = $bestBlock, but that's not allowed in PHP :(
293 $bestBlockPreventsEdit = null;
294
295 foreach ( $res as $row ) {
296 $block = self::newFromRow( $row );
297
298 # Don't use expired blocks
299 if ( $block->isExpired() ) {
300 continue;
301 }
302
303 # Don't use anon only blocks on users
304 if ( $this->type == self::TYPE_USER && !$block->isHardblock() ) {
305 continue;
306 }
307
308 if ( $block->getType() == self::TYPE_RANGE ) {
309 # This is the number of bits that are allowed to vary in the block, give
310 # or take some floating point errors
311 $end = Wikimedia\base_convert( $block->getRangeEnd(), 16, 10 );
312 $start = Wikimedia\base_convert( $block->getRangeStart(), 16, 10 );
313 $size = log( $end - $start + 1, 2 );
314
315 # This has the nice property that a /32 block is ranked equally with a
316 # single-IP block, which is exactly what it is...
317 $score = self::TYPE_RANGE - 1 + ( $size / 128 );
318
319 } else {
320 $score = $block->getType();
321 }
322
323 if ( $score < $bestBlockScore ) {
324 $bestBlockScore = $score;
325 $bestRow = $row;
326 $bestBlockPreventsEdit = $block->prevents( 'edit' );
327 }
328 }
329
330 if ( $bestRow !== null ) {
331 $this->initFromRow( $bestRow );
332 $this->prevents( 'edit', $bestBlockPreventsEdit );
333 return true;
334 } else {
335 return false;
336 }
337 }
338
339 /**
340 * Get a set of SQL conditions which will select rangeblocks encompassing a given range
341 * @param string $start Hexadecimal IP representation
342 * @param string $end Hexadecimal IP representation, or null to use $start = $end
343 * @return string
344 */
345 public static function getRangeCond( $start, $end = null ) {
346 if ( $end === null ) {
347 $end = $start;
348 }
349 # Per bug 14634, we want to include relevant active rangeblocks; for
350 # rangeblocks, we want to include larger ranges which enclose the given
351 # range. We know that all blocks must be smaller than $wgBlockCIDRLimit,
352 # so we can improve performance by filtering on a LIKE clause
353 $chunk = self::getIpFragment( $start );
354 $dbr = wfGetDB( DB_REPLICA );
355 $like = $dbr->buildLike( $chunk, $dbr->anyString() );
356
357 # Fairly hard to make a malicious SQL statement out of hex characters,
358 # but stranger things have happened...
359 $safeStart = $dbr->addQuotes( $start );
360 $safeEnd = $dbr->addQuotes( $end );
361
362 return $dbr->makeList(
363 [
364 "ipb_range_start $like",
365 "ipb_range_start <= $safeStart",
366 "ipb_range_end >= $safeEnd",
367 ],
368 LIST_AND
369 );
370 }
371
372 /**
373 * Get the component of an IP address which is certain to be the same between an IP
374 * address and a rangeblock containing that IP address.
375 * @param string $hex Hexadecimal IP representation
376 * @return string
377 */
378 protected static function getIpFragment( $hex ) {
379 global $wgBlockCIDRLimit;
380 if ( substr( $hex, 0, 3 ) == 'v6-' ) {
381 return 'v6-' . substr( substr( $hex, 3 ), 0, floor( $wgBlockCIDRLimit['IPv6'] / 4 ) );
382 } else {
383 return substr( $hex, 0, floor( $wgBlockCIDRLimit['IPv4'] / 4 ) );
384 }
385 }
386
387 /**
388 * Given a database row from the ipblocks table, initialize
389 * member variables
390 * @param stdClass $row A row from the ipblocks table
391 */
392 protected function initFromRow( $row ) {
393 $this->setTarget( $row->ipb_address );
394 if ( $row->ipb_by ) { // local user
395 $this->setBlocker( User::newFromId( $row->ipb_by ) );
396 } else { // foreign user
397 $this->setBlocker( $row->ipb_by_text );
398 }
399
400 $this->mReason = $row->ipb_reason;
401 $this->mTimestamp = wfTimestamp( TS_MW, $row->ipb_timestamp );
402 $this->mAuto = $row->ipb_auto;
403 $this->mHideName = $row->ipb_deleted;
404 $this->mId = (int)$row->ipb_id;
405 $this->mParentBlockId = $row->ipb_parent_block_id;
406
407 // I wish I didn't have to do this
408 $this->mExpiry = wfGetDB( DB_REPLICA )->decodeExpiry( $row->ipb_expiry );
409
410 $this->isHardblock( !$row->ipb_anon_only );
411 $this->isAutoblocking( $row->ipb_enable_autoblock );
412
413 $this->prevents( 'createaccount', $row->ipb_create_account );
414 $this->prevents( 'sendemail', $row->ipb_block_email );
415 $this->prevents( 'editownusertalk', !$row->ipb_allow_usertalk );
416 }
417
418 /**
419 * Create a new Block object from a database row
420 * @param stdClass $row Row from the ipblocks table
421 * @return Block
422 */
423 public static function newFromRow( $row ) {
424 $block = new Block;
425 $block->initFromRow( $row );
426 return $block;
427 }
428
429 /**
430 * Delete the row from the IP blocks table.
431 *
432 * @throws MWException
433 * @return bool
434 */
435 public function delete() {
436 if ( wfReadOnly() ) {
437 return false;
438 }
439
440 if ( !$this->getId() ) {
441 throw new MWException( "Block::delete() requires that the mId member be filled\n" );
442 }
443
444 $dbw = wfGetDB( DB_MASTER );
445 $dbw->delete( 'ipblocks', [ 'ipb_parent_block_id' => $this->getId() ], __METHOD__ );
446 $dbw->delete( 'ipblocks', [ 'ipb_id' => $this->getId() ], __METHOD__ );
447
448 return $dbw->affectedRows() > 0;
449 }
450
451 /**
452 * Insert a block into the block table. Will fail if there is a conflicting
453 * block (same name and options) already in the database.
454 *
455 * @param IDatabase $dbw If you have one available
456 * @return bool|array False on failure, assoc array on success:
457 * ('id' => block ID, 'autoIds' => array of autoblock IDs)
458 */
459 public function insert( $dbw = null ) {
460 global $wgBlockDisablesLogin;
461 wfDebug( "Block::insert; timestamp {$this->mTimestamp}\n" );
462
463 if ( $dbw === null ) {
464 $dbw = wfGetDB( DB_MASTER );
465 }
466
467 # Periodic purge via commit hooks
468 if ( mt_rand( 0, 9 ) == 0 ) {
469 Block::purgeExpired();
470 }
471
472 $row = $this->getDatabaseArray();
473 $row['ipb_id'] = $dbw->nextSequenceValue( "ipblocks_ipb_id_seq" );
474
475 $dbw->insert( 'ipblocks', $row, __METHOD__, [ 'IGNORE' ] );
476 $affected = $dbw->affectedRows();
477 $this->mId = $dbw->insertId();
478
479 # Don't collide with expired blocks.
480 # Do this after trying to insert to avoid locking.
481 if ( !$affected ) {
482 # T96428: The ipb_address index uses a prefix on a field, so
483 # use a standard SELECT + DELETE to avoid annoying gap locks.
484 $ids = $dbw->selectFieldValues( 'ipblocks',
485 'ipb_id',
486 [
487 'ipb_address' => $row['ipb_address'],
488 'ipb_user' => $row['ipb_user'],
489 'ipb_expiry < ' . $dbw->addQuotes( $dbw->timestamp() )
490 ],
491 __METHOD__
492 );
493 if ( $ids ) {
494 $dbw->delete( 'ipblocks', [ 'ipb_id' => $ids ], __METHOD__ );
495 $dbw->insert( 'ipblocks', $row, __METHOD__, [ 'IGNORE' ] );
496 $affected = $dbw->affectedRows();
497 $this->mId = $dbw->insertId();
498 }
499 }
500
501 if ( $affected ) {
502 $auto_ipd_ids = $this->doRetroactiveAutoblock();
503
504 if ( $wgBlockDisablesLogin && $this->target instanceof User ) {
505 // Change user login token to force them to be logged out.
506 $this->target->setToken();
507 $this->target->saveSettings();
508 }
509
510 return [ 'id' => $this->mId, 'autoIds' => $auto_ipd_ids ];
511 }
512
513 return false;
514 }
515
516 /**
517 * Update a block in the DB with new parameters.
518 * The ID field needs to be loaded first.
519 *
520 * @return bool|array False on failure, array on success:
521 * ('id' => block ID, 'autoIds' => array of autoblock IDs)
522 */
523 public function update() {
524 wfDebug( "Block::update; timestamp {$this->mTimestamp}\n" );
525 $dbw = wfGetDB( DB_MASTER );
526
527 $dbw->startAtomic( __METHOD__ );
528
529 $dbw->update(
530 'ipblocks',
531 $this->getDatabaseArray( $dbw ),
532 [ 'ipb_id' => $this->getId() ],
533 __METHOD__
534 );
535
536 $affected = $dbw->affectedRows();
537
538 if ( $this->isAutoblocking() ) {
539 // update corresponding autoblock(s) (bug 48813)
540 $dbw->update(
541 'ipblocks',
542 $this->getAutoblockUpdateArray(),
543 [ 'ipb_parent_block_id' => $this->getId() ],
544 __METHOD__
545 );
546 } else {
547 // autoblock no longer required, delete corresponding autoblock(s)
548 $dbw->delete(
549 'ipblocks',
550 [ 'ipb_parent_block_id' => $this->getId() ],
551 __METHOD__
552 );
553 }
554
555 $dbw->endAtomic( __METHOD__ );
556
557 if ( $affected ) {
558 $auto_ipd_ids = $this->doRetroactiveAutoblock();
559 return [ 'id' => $this->mId, 'autoIds' => $auto_ipd_ids ];
560 }
561
562 return false;
563 }
564
565 /**
566 * Get an array suitable for passing to $dbw->insert() or $dbw->update()
567 * @param IDatabase $db
568 * @return array
569 */
570 protected function getDatabaseArray( $db = null ) {
571 if ( !$db ) {
572 $db = wfGetDB( DB_REPLICA );
573 }
574 $expiry = $db->encodeExpiry( $this->mExpiry );
575
576 if ( $this->forcedTargetID ) {
577 $uid = $this->forcedTargetID;
578 } else {
579 $uid = $this->target instanceof User ? $this->target->getId() : 0;
580 }
581
582 $a = [
583 'ipb_address' => (string)$this->target,
584 'ipb_user' => $uid,
585 'ipb_by' => $this->getBy(),
586 'ipb_by_text' => $this->getByName(),
587 'ipb_reason' => $this->mReason,
588 'ipb_timestamp' => $db->timestamp( $this->mTimestamp ),
589 'ipb_auto' => $this->mAuto,
590 'ipb_anon_only' => !$this->isHardblock(),
591 'ipb_create_account' => $this->prevents( 'createaccount' ),
592 'ipb_enable_autoblock' => $this->isAutoblocking(),
593 'ipb_expiry' => $expiry,
594 'ipb_range_start' => $this->getRangeStart(),
595 'ipb_range_end' => $this->getRangeEnd(),
596 'ipb_deleted' => intval( $this->mHideName ), // typecast required for SQLite
597 'ipb_block_email' => $this->prevents( 'sendemail' ),
598 'ipb_allow_usertalk' => !$this->prevents( 'editownusertalk' ),
599 'ipb_parent_block_id' => $this->mParentBlockId
600 ];
601
602 return $a;
603 }
604
605 /**
606 * @return array
607 */
608 protected function getAutoblockUpdateArray() {
609 return [
610 'ipb_by' => $this->getBy(),
611 'ipb_by_text' => $this->getByName(),
612 'ipb_reason' => $this->mReason,
613 'ipb_create_account' => $this->prevents( 'createaccount' ),
614 'ipb_deleted' => (int)$this->mHideName, // typecast required for SQLite
615 'ipb_allow_usertalk' => !$this->prevents( 'editownusertalk' ),
616 ];
617 }
618
619 /**
620 * Retroactively autoblocks the last IP used by the user (if it is a user)
621 * blocked by this Block.
622 *
623 * @return array Block IDs of retroactive autoblocks made
624 */
625 protected function doRetroactiveAutoblock() {
626 $blockIds = [];
627 # If autoblock is enabled, autoblock the LAST IP(s) used
628 if ( $this->isAutoblocking() && $this->getType() == self::TYPE_USER ) {
629 wfDebug( "Doing retroactive autoblocks for " . $this->getTarget() . "\n" );
630
631 $continue = Hooks::run(
632 'PerformRetroactiveAutoblock', [ $this, &$blockIds ] );
633
634 if ( $continue ) {
635 self::defaultRetroactiveAutoblock( $this, $blockIds );
636 }
637 }
638 return $blockIds;
639 }
640
641 /**
642 * Retroactively autoblocks the last IP used by the user (if it is a user)
643 * blocked by this Block. This will use the recentchanges table.
644 *
645 * @param Block $block
646 * @param array &$blockIds
647 */
648 protected static function defaultRetroactiveAutoblock( Block $block, array &$blockIds ) {
649 global $wgPutIPinRC;
650
651 // No IPs are in recentchanges table, so nothing to select
652 if ( !$wgPutIPinRC ) {
653 return;
654 }
655
656 $dbr = wfGetDB( DB_REPLICA );
657
658 $options = [ 'ORDER BY' => 'rc_timestamp DESC' ];
659 $conds = [ 'rc_user_text' => (string)$block->getTarget() ];
660
661 // Just the last IP used.
662 $options['LIMIT'] = 1;
663
664 $res = $dbr->select( 'recentchanges', [ 'rc_ip' ], $conds,
665 __METHOD__, $options );
666
667 if ( !$res->numRows() ) {
668 # No results, don't autoblock anything
669 wfDebug( "No IP found to retroactively autoblock\n" );
670 } else {
671 foreach ( $res as $row ) {
672 if ( $row->rc_ip ) {
673 $id = $block->doAutoblock( $row->rc_ip );
674 if ( $id ) {
675 $blockIds[] = $id;
676 }
677 }
678 }
679 }
680 }
681
682 /**
683 * Checks whether a given IP is on the autoblock whitelist.
684 * TODO: this probably belongs somewhere else, but not sure where...
685 *
686 * @param string $ip The IP to check
687 * @return bool
688 */
689 public static function isWhitelistedFromAutoblocks( $ip ) {
690 // Try to get the autoblock_whitelist from the cache, as it's faster
691 // than getting the msg raw and explode()'ing it.
692 $cache = ObjectCache::getMainWANInstance();
693 $lines = $cache->getWithSetCallback(
694 wfMemcKey( 'ipb', 'autoblock', 'whitelist' ),
695 $cache::TTL_DAY,
696 function () {
697 return explode( "\n",
698 wfMessage( 'autoblock_whitelist' )->inContentLanguage()->plain() );
699 }
700 );
701
702 wfDebug( "Checking the autoblock whitelist..\n" );
703
704 foreach ( $lines as $line ) {
705 # List items only
706 if ( substr( $line, 0, 1 ) !== '*' ) {
707 continue;
708 }
709
710 $wlEntry = substr( $line, 1 );
711 $wlEntry = trim( $wlEntry );
712
713 wfDebug( "Checking $ip against $wlEntry..." );
714
715 # Is the IP in this range?
716 if ( IP::isInRange( $ip, $wlEntry ) ) {
717 wfDebug( " IP $ip matches $wlEntry, not autoblocking\n" );
718 return true;
719 } else {
720 wfDebug( " No match\n" );
721 }
722 }
723
724 return false;
725 }
726
727 /**
728 * Autoblocks the given IP, referring to this Block.
729 *
730 * @param string $autoblockIP The IP to autoblock.
731 * @return int|bool Block ID if an autoblock was inserted, false if not.
732 */
733 public function doAutoblock( $autoblockIP ) {
734 # If autoblocks are disabled, go away.
735 if ( !$this->isAutoblocking() ) {
736 return false;
737 }
738
739 # Check for presence on the autoblock whitelist.
740 if ( self::isWhitelistedFromAutoblocks( $autoblockIP ) ) {
741 return false;
742 }
743
744 # Allow hooks to cancel the autoblock.
745 if ( !Hooks::run( 'AbortAutoblock', [ $autoblockIP, &$this ] ) ) {
746 wfDebug( "Autoblock aborted by hook.\n" );
747 return false;
748 }
749
750 # It's okay to autoblock. Go ahead and insert/update the block...
751
752 # Do not add a *new* block if the IP is already blocked.
753 $ipblock = Block::newFromTarget( $autoblockIP );
754 if ( $ipblock ) {
755 # Check if the block is an autoblock and would exceed the user block
756 # if renewed. If so, do nothing, otherwise prolong the block time...
757 if ( $ipblock->mAuto && // @todo Why not compare $ipblock->mExpiry?
758 $this->mExpiry > Block::getAutoblockExpiry( $ipblock->mTimestamp )
759 ) {
760 # Reset block timestamp to now and its expiry to
761 # $wgAutoblockExpiry in the future
762 $ipblock->updateTimestamp();
763 }
764 return false;
765 }
766
767 # Make a new block object with the desired properties.
768 $autoblock = new Block;
769 wfDebug( "Autoblocking {$this->getTarget()}@" . $autoblockIP . "\n" );
770 $autoblock->setTarget( $autoblockIP );
771 $autoblock->setBlocker( $this->getBlocker() );
772 $autoblock->mReason = wfMessage( 'autoblocker', $this->getTarget(), $this->mReason )
773 ->inContentLanguage()->plain();
774 $timestamp = wfTimestampNow();
775 $autoblock->mTimestamp = $timestamp;
776 $autoblock->mAuto = 1;
777 $autoblock->prevents( 'createaccount', $this->prevents( 'createaccount' ) );
778 # Continue suppressing the name if needed
779 $autoblock->mHideName = $this->mHideName;
780 $autoblock->prevents( 'editownusertalk', $this->prevents( 'editownusertalk' ) );
781 $autoblock->mParentBlockId = $this->mId;
782
783 if ( $this->mExpiry == 'infinity' ) {
784 # Original block was indefinite, start an autoblock now
785 $autoblock->mExpiry = Block::getAutoblockExpiry( $timestamp );
786 } else {
787 # If the user is already blocked with an expiry date, we don't
788 # want to pile on top of that.
789 $autoblock->mExpiry = min( $this->mExpiry, Block::getAutoblockExpiry( $timestamp ) );
790 }
791
792 # Insert the block...
793 $status = $autoblock->insert();
794 return $status
795 ? $status['id']
796 : false;
797 }
798
799 /**
800 * Check if a block has expired. Delete it if it is.
801 * @return bool
802 */
803 public function deleteIfExpired() {
804
805 if ( $this->isExpired() ) {
806 wfDebug( "Block::deleteIfExpired() -- deleting\n" );
807 $this->delete();
808 $retVal = true;
809 } else {
810 wfDebug( "Block::deleteIfExpired() -- not expired\n" );
811 $retVal = false;
812 }
813
814 return $retVal;
815 }
816
817 /**
818 * Has the block expired?
819 * @return bool
820 */
821 public function isExpired() {
822 $timestamp = wfTimestampNow();
823 wfDebug( "Block::isExpired() checking current " . $timestamp . " vs $this->mExpiry\n" );
824
825 if ( !$this->mExpiry ) {
826 return false;
827 } else {
828 return $timestamp > $this->mExpiry;
829 }
830 }
831
832 /**
833 * Is the block address valid (i.e. not a null string?)
834 * @return bool
835 */
836 public function isValid() {
837 return $this->getTarget() != null;
838 }
839
840 /**
841 * Update the timestamp on autoblocks.
842 */
843 public function updateTimestamp() {
844 if ( $this->mAuto ) {
845 $this->mTimestamp = wfTimestamp();
846 $this->mExpiry = Block::getAutoblockExpiry( $this->mTimestamp );
847
848 $dbw = wfGetDB( DB_MASTER );
849 $dbw->update( 'ipblocks',
850 [ /* SET */
851 'ipb_timestamp' => $dbw->timestamp( $this->mTimestamp ),
852 'ipb_expiry' => $dbw->timestamp( $this->mExpiry ),
853 ],
854 [ /* WHERE */
855 'ipb_id' => $this->getId(),
856 ],
857 __METHOD__
858 );
859 }
860 }
861
862 /**
863 * Get the IP address at the start of the range in Hex form
864 * @throws MWException
865 * @return string IP in Hex form
866 */
867 public function getRangeStart() {
868 switch ( $this->type ) {
869 case self::TYPE_USER:
870 return '';
871 case self::TYPE_IP:
872 return IP::toHex( $this->target );
873 case self::TYPE_RANGE:
874 list( $start, /*...*/ ) = IP::parseRange( $this->target );
875 return $start;
876 default:
877 throw new MWException( "Block with invalid type" );
878 }
879 }
880
881 /**
882 * Get the IP address at the end of the range in Hex form
883 * @throws MWException
884 * @return string IP in Hex form
885 */
886 public function getRangeEnd() {
887 switch ( $this->type ) {
888 case self::TYPE_USER:
889 return '';
890 case self::TYPE_IP:
891 return IP::toHex( $this->target );
892 case self::TYPE_RANGE:
893 list( /*...*/, $end ) = IP::parseRange( $this->target );
894 return $end;
895 default:
896 throw new MWException( "Block with invalid type" );
897 }
898 }
899
900 /**
901 * Get the user id of the blocking sysop
902 *
903 * @return int (0 for foreign users)
904 */
905 public function getBy() {
906 $blocker = $this->getBlocker();
907 return ( $blocker instanceof User )
908 ? $blocker->getId()
909 : 0;
910 }
911
912 /**
913 * Get the username of the blocking sysop
914 *
915 * @return string
916 */
917 public function getByName() {
918 $blocker = $this->getBlocker();
919 return ( $blocker instanceof User )
920 ? $blocker->getName()
921 : (string)$blocker; // username
922 }
923
924 /**
925 * Get the block ID
926 * @return int
927 */
928 public function getId() {
929 return $this->mId;
930 }
931
932 /**
933 * Get/set a flag determining whether the master is used for reads
934 *
935 * @param bool|null $x
936 * @return bool
937 */
938 public function fromMaster( $x = null ) {
939 return wfSetVar( $this->mFromMaster, $x );
940 }
941
942 /**
943 * Get/set whether the Block is a hardblock (affects logged-in users on a given IP/range)
944 * @param bool|null $x
945 * @return bool
946 */
947 public function isHardblock( $x = null ) {
948 wfSetVar( $this->isHardblock, $x );
949
950 # You can't *not* hardblock a user
951 return $this->getType() == self::TYPE_USER
952 ? true
953 : $this->isHardblock;
954 }
955
956 /**
957 * @param null|bool $x
958 * @return bool
959 */
960 public function isAutoblocking( $x = null ) {
961 wfSetVar( $this->isAutoblocking, $x );
962
963 # You can't put an autoblock on an IP or range as we don't have any history to
964 # look over to get more IPs from
965 return $this->getType() == self::TYPE_USER
966 ? $this->isAutoblocking
967 : false;
968 }
969
970 /**
971 * Get/set whether the Block prevents a given action
972 *
973 * @param string $action Action to check
974 * @param bool|null $x Value for set, or null to just get value
975 * @return bool|null Null for unrecognized rights.
976 */
977 public function prevents( $action, $x = null ) {
978 global $wgBlockDisablesLogin;
979 $res = null;
980 switch ( $action ) {
981 case 'edit':
982 # For now... <evil laugh>
983 $res = true;
984 break;
985 case 'createaccount':
986 $res = wfSetVar( $this->mCreateAccount, $x );
987 break;
988 case 'sendemail':
989 $res = wfSetVar( $this->mBlockEmail, $x );
990 break;
991 case 'editownusertalk':
992 $res = wfSetVar( $this->mDisableUsertalk, $x );
993 break;
994 case 'read':
995 $res = false;
996 break;
997 }
998 if ( !$res && $wgBlockDisablesLogin ) {
999 // If a block would disable login, then it should
1000 // prevent any action that all users cannot do
1001 $anon = new User;
1002 $res = $anon->isAllowed( $action ) ? $res : true;
1003 }
1004
1005 return $res;
1006 }
1007
1008 /**
1009 * Get the block name, but with autoblocked IPs hidden as per standard privacy policy
1010 * @return string Text is escaped
1011 */
1012 public function getRedactedName() {
1013 if ( $this->mAuto ) {
1014 return Html::rawElement(
1015 'span',
1016 [ 'class' => 'mw-autoblockid' ],
1017 wfMessage( 'autoblockid', $this->mId )
1018 );
1019 } else {
1020 return htmlspecialchars( $this->getTarget() );
1021 }
1022 }
1023
1024 /**
1025 * Get a timestamp of the expiry for autoblocks
1026 *
1027 * @param string|int $timestamp
1028 * @return string
1029 */
1030 public static function getAutoblockExpiry( $timestamp ) {
1031 global $wgAutoblockExpiry;
1032
1033 return wfTimestamp( TS_MW, wfTimestamp( TS_UNIX, $timestamp ) + $wgAutoblockExpiry );
1034 }
1035
1036 /**
1037 * Purge expired blocks from the ipblocks table
1038 */
1039 public static function purgeExpired() {
1040 if ( wfReadOnly() ) {
1041 return;
1042 }
1043
1044 DeferredUpdates::addUpdate( new AtomicSectionUpdate(
1045 wfGetDB( DB_MASTER ),
1046 __METHOD__,
1047 function ( IDatabase $dbw, $fname ) {
1048 $dbw->delete(
1049 'ipblocks',
1050 [ 'ipb_expiry < ' . $dbw->addQuotes( $dbw->timestamp() ) ],
1051 $fname
1052 );
1053 }
1054 ) );
1055 }
1056
1057 /**
1058 * Given a target and the target's type, get an existing Block object if possible.
1059 * @param string|User|int $specificTarget A block target, which may be one of several types:
1060 * * A user to block, in which case $target will be a User
1061 * * An IP to block, in which case $target will be a User generated by using
1062 * User::newFromName( $ip, false ) to turn off name validation
1063 * * An IP range, in which case $target will be a String "123.123.123.123/18" etc
1064 * * The ID of an existing block, in the format "#12345" (since pure numbers are valid
1065 * usernames
1066 * Calling this with a user, IP address or range will not select autoblocks, and will
1067 * only select a block where the targets match exactly (so looking for blocks on
1068 * 1.2.3.4 will not select 1.2.0.0/16 or even 1.2.3.4/32)
1069 * @param string|User|int $vagueTarget As above, but we will search for *any* block which
1070 * affects that target (so for an IP address, get ranges containing that IP; and also
1071 * get any relevant autoblocks). Leave empty or blank to skip IP-based lookups.
1072 * @param bool $fromMaster Whether to use the DB_MASTER database
1073 * @return Block|null (null if no relevant block could be found). The target and type
1074 * of the returned Block will refer to the actual block which was found, which might
1075 * not be the same as the target you gave if you used $vagueTarget!
1076 */
1077 public static function newFromTarget( $specificTarget, $vagueTarget = null, $fromMaster = false ) {
1078
1079 list( $target, $type ) = self::parseTarget( $specificTarget );
1080 if ( $type == Block::TYPE_ID || $type == Block::TYPE_AUTO ) {
1081 return Block::newFromID( $target );
1082
1083 } elseif ( $target === null && $vagueTarget == '' ) {
1084 # We're not going to find anything useful here
1085 # Be aware that the == '' check is explicit, since empty values will be
1086 # passed by some callers (bug 29116)
1087 return null;
1088
1089 } elseif ( in_array(
1090 $type,
1091 [ Block::TYPE_USER, Block::TYPE_IP, Block::TYPE_RANGE, null ] )
1092 ) {
1093 $block = new Block();
1094 $block->fromMaster( $fromMaster );
1095
1096 if ( $type !== null ) {
1097 $block->setTarget( $target );
1098 }
1099
1100 if ( $block->newLoad( $vagueTarget ) ) {
1101 return $block;
1102 }
1103 }
1104 return null;
1105 }
1106
1107 /**
1108 * Get all blocks that match any IP from an array of IP addresses
1109 *
1110 * @param array $ipChain List of IPs (strings), usually retrieved from the
1111 * X-Forwarded-For header of the request
1112 * @param bool $isAnon Exclude anonymous-only blocks if false
1113 * @param bool $fromMaster Whether to query the master or replica DB
1114 * @return array Array of Blocks
1115 * @since 1.22
1116 */
1117 public static function getBlocksForIPList( array $ipChain, $isAnon, $fromMaster = false ) {
1118 if ( !count( $ipChain ) ) {
1119 return [];
1120 }
1121
1122 $conds = [];
1123 foreach ( array_unique( $ipChain ) as $ipaddr ) {
1124 # Discard invalid IP addresses. Since XFF can be spoofed and we do not
1125 # necessarily trust the header given to us, make sure that we are only
1126 # checking for blocks on well-formatted IP addresses (IPv4 and IPv6).
1127 # Do not treat private IP spaces as special as it may be desirable for wikis
1128 # to block those IP ranges in order to stop misbehaving proxies that spoof XFF.
1129 if ( !IP::isValid( $ipaddr ) ) {
1130 continue;
1131 }
1132 # Don't check trusted IPs (includes local squids which will be in every request)
1133 if ( IP::isTrustedProxy( $ipaddr ) ) {
1134 continue;
1135 }
1136 # Check both the original IP (to check against single blocks), as well as build
1137 # the clause to check for rangeblocks for the given IP.
1138 $conds['ipb_address'][] = $ipaddr;
1139 $conds[] = self::getRangeCond( IP::toHex( $ipaddr ) );
1140 }
1141
1142 if ( !count( $conds ) ) {
1143 return [];
1144 }
1145
1146 if ( $fromMaster ) {
1147 $db = wfGetDB( DB_MASTER );
1148 } else {
1149 $db = wfGetDB( DB_REPLICA );
1150 }
1151 $conds = $db->makeList( $conds, LIST_OR );
1152 if ( !$isAnon ) {
1153 $conds = [ $conds, 'ipb_anon_only' => 0 ];
1154 }
1155 $selectFields = array_merge(
1156 [ 'ipb_range_start', 'ipb_range_end' ],
1157 Block::selectFields()
1158 );
1159 $rows = $db->select( 'ipblocks',
1160 $selectFields,
1161 $conds,
1162 __METHOD__
1163 );
1164
1165 $blocks = [];
1166 foreach ( $rows as $row ) {
1167 $block = self::newFromRow( $row );
1168 if ( !$block->isExpired() ) {
1169 $blocks[] = $block;
1170 }
1171 }
1172
1173 return $blocks;
1174 }
1175
1176 /**
1177 * From a list of multiple blocks, find the most exact and strongest Block.
1178 *
1179 * The logic for finding the "best" block is:
1180 * - Blocks that match the block's target IP are preferred over ones in a range
1181 * - Hardblocks are chosen over softblocks that prevent account creation
1182 * - Softblocks that prevent account creation are chosen over other softblocks
1183 * - Other softblocks are chosen over autoblocks
1184 * - If there are multiple exact or range blocks at the same level, the one chosen
1185 * is random
1186 * This should be used when $blocks where retrieved from the user's IP address
1187 * and $ipChain is populated from the same IP address information.
1188 *
1189 * @param array $blocks Array of Block objects
1190 * @param array $ipChain List of IPs (strings). This is used to determine how "close"
1191 * a block is to the server, and if a block matches exactly, or is in a range.
1192 * The order is furthest from the server to nearest e.g., (Browser, proxy1, proxy2,
1193 * local-squid, ...)
1194 * @throws MWException
1195 * @return Block|null The "best" block from the list
1196 */
1197 public static function chooseBlock( array $blocks, array $ipChain ) {
1198 if ( !count( $blocks ) ) {
1199 return null;
1200 } elseif ( count( $blocks ) == 1 ) {
1201 return $blocks[0];
1202 }
1203
1204 // Sort hard blocks before soft ones and secondarily sort blocks
1205 // that disable account creation before those that don't.
1206 usort( $blocks, function ( Block $a, Block $b ) {
1207 $aWeight = (int)$a->isHardblock() . (int)$a->prevents( 'createaccount' );
1208 $bWeight = (int)$b->isHardblock() . (int)$b->prevents( 'createaccount' );
1209 return strcmp( $bWeight, $aWeight ); // highest weight first
1210 } );
1211
1212 $blocksListExact = [
1213 'hard' => false,
1214 'disable_create' => false,
1215 'other' => false,
1216 'auto' => false
1217 ];
1218 $blocksListRange = [
1219 'hard' => false,
1220 'disable_create' => false,
1221 'other' => false,
1222 'auto' => false
1223 ];
1224 $ipChain = array_reverse( $ipChain );
1225
1226 /** @var Block $block */
1227 foreach ( $blocks as $block ) {
1228 // Stop searching if we have already have a "better" block. This
1229 // is why the order of the blocks matters
1230 if ( !$block->isHardblock() && $blocksListExact['hard'] ) {
1231 break;
1232 } elseif ( !$block->prevents( 'createaccount' ) && $blocksListExact['disable_create'] ) {
1233 break;
1234 }
1235
1236 foreach ( $ipChain as $checkip ) {
1237 $checkipHex = IP::toHex( $checkip );
1238 if ( (string)$block->getTarget() === $checkip ) {
1239 if ( $block->isHardblock() ) {
1240 $blocksListExact['hard'] = $blocksListExact['hard'] ?: $block;
1241 } elseif ( $block->prevents( 'createaccount' ) ) {
1242 $blocksListExact['disable_create'] = $blocksListExact['disable_create'] ?: $block;
1243 } elseif ( $block->mAuto ) {
1244 $blocksListExact['auto'] = $blocksListExact['auto'] ?: $block;
1245 } else {
1246 $blocksListExact['other'] = $blocksListExact['other'] ?: $block;
1247 }
1248 // We found closest exact match in the ip list, so go to the next Block
1249 break;
1250 } elseif ( array_filter( $blocksListExact ) == []
1251 && $block->getRangeStart() <= $checkipHex
1252 && $block->getRangeEnd() >= $checkipHex
1253 ) {
1254 if ( $block->isHardblock() ) {
1255 $blocksListRange['hard'] = $blocksListRange['hard'] ?: $block;
1256 } elseif ( $block->prevents( 'createaccount' ) ) {
1257 $blocksListRange['disable_create'] = $blocksListRange['disable_create'] ?: $block;
1258 } elseif ( $block->mAuto ) {
1259 $blocksListRange['auto'] = $blocksListRange['auto'] ?: $block;
1260 } else {
1261 $blocksListRange['other'] = $blocksListRange['other'] ?: $block;
1262 }
1263 break;
1264 }
1265 }
1266 }
1267
1268 if ( array_filter( $blocksListExact ) == [] ) {
1269 $blocksList = &$blocksListRange;
1270 } else {
1271 $blocksList = &$blocksListExact;
1272 }
1273
1274 $chosenBlock = null;
1275 if ( $blocksList['hard'] ) {
1276 $chosenBlock = $blocksList['hard'];
1277 } elseif ( $blocksList['disable_create'] ) {
1278 $chosenBlock = $blocksList['disable_create'];
1279 } elseif ( $blocksList['other'] ) {
1280 $chosenBlock = $blocksList['other'];
1281 } elseif ( $blocksList['auto'] ) {
1282 $chosenBlock = $blocksList['auto'];
1283 } else {
1284 throw new MWException( "Proxy block found, but couldn't be classified." );
1285 }
1286
1287 return $chosenBlock;
1288 }
1289
1290 /**
1291 * From an existing Block, get the target and the type of target.
1292 * Note that, except for null, it is always safe to treat the target
1293 * as a string; for User objects this will return User::__toString()
1294 * which in turn gives User::getName().
1295 *
1296 * @param string|int|User|null $target
1297 * @return array( User|String|null, Block::TYPE_ constant|null )
1298 */
1299 public static function parseTarget( $target ) {
1300 # We may have been through this before
1301 if ( $target instanceof User ) {
1302 if ( IP::isValid( $target->getName() ) ) {
1303 return [ $target, self::TYPE_IP ];
1304 } else {
1305 return [ $target, self::TYPE_USER ];
1306 }
1307 } elseif ( $target === null ) {
1308 return [ null, null ];
1309 }
1310
1311 $target = trim( $target );
1312
1313 if ( IP::isValid( $target ) ) {
1314 # We can still create a User if it's an IP address, but we need to turn
1315 # off validation checking (which would exclude IP addresses)
1316 return [
1317 User::newFromName( IP::sanitizeIP( $target ), false ),
1318 Block::TYPE_IP
1319 ];
1320
1321 } elseif ( IP::isValidBlock( $target ) ) {
1322 # Can't create a User from an IP range
1323 return [ IP::sanitizeRange( $target ), Block::TYPE_RANGE ];
1324 }
1325
1326 # Consider the possibility that this is not a username at all
1327 # but actually an old subpage (bug #29797)
1328 if ( strpos( $target, '/' ) !== false ) {
1329 # An old subpage, drill down to the user behind it
1330 $target = explode( '/', $target )[0];
1331 }
1332
1333 $userObj = User::newFromName( $target );
1334 if ( $userObj instanceof User ) {
1335 # Note that since numbers are valid usernames, a $target of "12345" will be
1336 # considered a User. If you want to pass a block ID, prepend a hash "#12345",
1337 # since hash characters are not valid in usernames or titles generally.
1338 return [ $userObj, Block::TYPE_USER ];
1339
1340 } elseif ( preg_match( '/^#\d+$/', $target ) ) {
1341 # Autoblock reference in the form "#12345"
1342 return [ substr( $target, 1 ), Block::TYPE_AUTO ];
1343
1344 } else {
1345 # WTF?
1346 return [ null, null ];
1347 }
1348 }
1349
1350 /**
1351 * Get the type of target for this particular block
1352 * @return int Block::TYPE_ constant, will never be TYPE_ID
1353 */
1354 public function getType() {
1355 return $this->mAuto
1356 ? self::TYPE_AUTO
1357 : $this->type;
1358 }
1359
1360 /**
1361 * Get the target and target type for this particular Block. Note that for autoblocks,
1362 * this returns the unredacted name; frontend functions need to call $block->getRedactedName()
1363 * in this situation.
1364 * @return array( User|String, Block::TYPE_ constant )
1365 * @todo FIXME: This should be an integral part of the Block member variables
1366 */
1367 public function getTargetAndType() {
1368 return [ $this->getTarget(), $this->getType() ];
1369 }
1370
1371 /**
1372 * Get the target for this particular Block. Note that for autoblocks,
1373 * this returns the unredacted name; frontend functions need to call $block->getRedactedName()
1374 * in this situation.
1375 * @return User|string
1376 */
1377 public function getTarget() {
1378 return $this->target;
1379 }
1380
1381 /**
1382 * @since 1.19
1383 *
1384 * @return mixed|string
1385 */
1386 public function getExpiry() {
1387 return $this->mExpiry;
1388 }
1389
1390 /**
1391 * Set the target for this block, and update $this->type accordingly
1392 * @param mixed $target
1393 */
1394 public function setTarget( $target ) {
1395 list( $this->target, $this->type ) = self::parseTarget( $target );
1396 }
1397
1398 /**
1399 * Get the user who implemented this block
1400 * @return User|string Local User object or string for a foreign user
1401 */
1402 public function getBlocker() {
1403 return $this->blocker;
1404 }
1405
1406 /**
1407 * Set the user who implemented (or will implement) this block
1408 * @param User|string $user Local User object or username string for foreign users
1409 */
1410 public function setBlocker( $user ) {
1411 $this->blocker = $user;
1412 }
1413
1414 /**
1415 * Get the key and parameters for the corresponding error message.
1416 *
1417 * @since 1.22
1418 * @param IContextSource $context
1419 * @return array
1420 */
1421 public function getPermissionsError( IContextSource $context ) {
1422 $blocker = $this->getBlocker();
1423 if ( $blocker instanceof User ) { // local user
1424 $blockerUserpage = $blocker->getUserPage();
1425 $link = "[[{$blockerUserpage->getPrefixedText()}|{$blockerUserpage->getText()}]]";
1426 } else { // foreign user
1427 $link = $blocker;
1428 }
1429
1430 $reason = $this->mReason;
1431 if ( $reason == '' ) {
1432 $reason = $context->msg( 'blockednoreason' )->text();
1433 }
1434
1435 /* $ip returns who *is* being blocked, $intended contains who was meant to be blocked.
1436 * This could be a username, an IP range, or a single IP. */
1437 $intended = $this->getTarget();
1438
1439 $lang = $context->getLanguage();
1440 return [
1441 $this->mAuto ? 'autoblockedtext' : 'blockedtext',
1442 $link,
1443 $reason,
1444 $context->getRequest()->getIP(),
1445 $this->getByName(),
1446 $this->getId(),
1447 $lang->formatExpiry( $this->mExpiry ),
1448 (string)$intended,
1449 $lang->userTimeAndDate( $this->mTimestamp, $context->getUser() ),
1450 ];
1451 }
1452 }