Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
7 / 7 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
AllowedDomainsFileUrlChecker | |
100.00% |
7 / 7 |
|
100.00% |
2 / 2 |
6 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
checkSourceUrl | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
5 |
1 | <?php |
2 | |
3 | namespace FileImporter\Remote\MediaWiki; |
4 | |
5 | use FileImporter\Data\SourceUrl; |
6 | |
7 | /** |
8 | * @license GPL-2.0-or-later |
9 | * @author Addshore |
10 | */ |
11 | class AllowedDomainsFileUrlChecker extends AnyMediaWikiFileUrlChecker { |
12 | |
13 | /** @var string[] */ |
14 | private array $allowedDomains; |
15 | |
16 | /** |
17 | * @param string[] $allowedDomains |
18 | */ |
19 | public function __construct( array $allowedDomains ) { |
20 | $this->allowedDomains = $allowedDomains; |
21 | } |
22 | |
23 | /** |
24 | * @inheritDoc |
25 | */ |
26 | public function checkSourceUrl( SourceUrl $sourceUrl ): bool { |
27 | $host = $sourceUrl->getHost(); |
28 | |
29 | foreach ( $this->allowedDomains as $allowedDomain ) { |
30 | if ( $host === $allowedDomain || |
31 | // If the allowed domain starts with a . allow subdomains |
32 | ( str_starts_with( $allowedDomain, '.' ) && str_ends_with( $host, $allowedDomain ) ) |
33 | ) { |
34 | return parent::checkSourceUrl( $sourceUrl ); |
35 | } |
36 | } |
37 | |
38 | return false; |
39 | } |
40 | |
41 | } |