Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
AutoblockExemptionList
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 2
30
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 isExempt
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2
3namespace MediaWiki\Block;
4
5use Psr\Log\LoggerInterface;
6use Wikimedia\IPUtils;
7use Wikimedia\Message\ITextFormatter;
8use Wikimedia\Message\MessageValue;
9
10/**
11 * Provides access to the wiki's autoblock exemption list.
12 * @since 1.42
13 */
14class AutoblockExemptionList {
15    private LoggerInterface $logger;
16    /** Should be for the wiki's content language */
17    private ITextFormatter $textFormatter;
18
19    public function __construct(
20        LoggerInterface $logger,
21        ITextFormatter $textFormatter
22    ) {
23        $this->logger = $logger;
24        $this->textFormatter = $textFormatter;
25    }
26
27    /**
28     * Checks whether a given IP is on the autoblock exemption list.
29     *
30     * @param string $ip The IP to check
31     * @return bool
32     */
33    public function isExempt( $ip ) {
34        $list = $this->textFormatter->format(
35            MessageValue::new( 'block-autoblock-exemptionlist' )
36        );
37        $lines = explode( "\n", $list );
38        $this->logger->debug( "Checking the autoblock exemption list.." );
39        foreach ( $lines as $line ) {
40            // List items only
41            if ( !str_starts_with( $line, '*' ) ) {
42                continue;
43            }
44
45            $wlEntry = substr( $line, 1 );
46            $wlEntry = trim( $wlEntry );
47
48            $this->logger->debug( "Checking $ip against $wlEntry..." );
49
50            // Is the IP in this range?
51            if ( IPUtils::isInRange( $ip, $wlEntry ) ) {
52                $this->logger->debug( " IP $ip matches $wlEntry, not autoblocking" );
53                return true;
54            } else {
55                $this->logger->debug( " No match" );
56            }
57        }
58
59        return false;
60    }
61
62}