Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
3 / 3 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
TrustedXFF | |
100.00% |
3 / 3 |
|
100.00% |
2 / 2 |
6 | |
100.00% |
1 / 1 |
__construct | n/a |
0 / 0 |
n/a |
0 / 0 |
1 | |||||
onIsTrustedProxy | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
getInstance | n/a |
0 / 0 |
n/a |
0 / 0 |
2 | |||||
isTrusted | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | namespace MediaWiki\Extension\TrustedXFF; |
4 | |
5 | use MediaWiki\Hook\IsTrustedProxyHook; |
6 | use Wikimedia\IPSet; |
7 | |
8 | class TrustedXFF implements IsTrustedProxyHook { |
9 | /** |
10 | * @var TrustedXFF |
11 | */ |
12 | private static $instance; |
13 | |
14 | /** @var IPSet */ |
15 | private $ipSet; |
16 | |
17 | /** |
18 | * @codeCoverageIgnore |
19 | * @param IPSet $set |
20 | */ |
21 | private function __construct( IPSet $set ) { |
22 | $this->ipSet = $set; |
23 | } |
24 | |
25 | /** |
26 | * @param string $ip |
27 | * @param bool &$trusted |
28 | */ |
29 | public function onIsTrustedProxy( $ip, &$trusted ) { |
30 | // Don't want to override hosts that are already trusted |
31 | if ( !$trusted ) { |
32 | $trusted = $this->isTrusted( $ip ); |
33 | } |
34 | } |
35 | |
36 | /** |
37 | * @codeCoverageIgnore |
38 | * @return TrustedXFF |
39 | */ |
40 | public static function getInstance() { |
41 | if ( !self::$instance ) { |
42 | self::$instance = new TrustedXFF( |
43 | IPSet::newFromJson( |
44 | file_get_contents( dirname( __DIR__ ) . '/trusted-hosts.json' ) |
45 | ) |
46 | ); |
47 | } |
48 | return self::$instance; |
49 | } |
50 | |
51 | /** |
52 | * @param string $ip |
53 | * @return bool |
54 | */ |
55 | public function isTrusted( $ip ) { |
56 | return $this->ipSet->match( $ip ); |
57 | } |
58 | } |