Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
4 / 4
TrustedXFF
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
2 / 2
6
100.00% covered (success)
100.00%
4 / 4
 __construct
n/a
0 / 0
1
n/a
0 / 0
 onIsTrustedProxy
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
3 / 3
 getInstance
n/a
0 / 0
2
n/a
0 / 0
 isTrusted
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
<?php
namespace MediaWiki\Extension\TrustedXFF;
use Wikimedia\IPSet;
class TrustedXFF {
    /**
     * @internal For tests only
     * @var TrustedXFF
     */
    public static $instance;
    /** @var IPSet */
    private $ipSet;
    /**
     * @codeCoverageIgnore
     * @param array $ips List of IPs and IP ranges
     */
    private function __construct( array $ips ) {
        $this->ipSet = new IPSet( $ips );
    }
    /**
     * @param string &$ip
     * @param bool &$trusted
     * @return bool
     */
    public static function onIsTrustedProxy( &$ip, &$trusted ) {
        // Don't want to override hosts that are already trusted
        if ( !$trusted ) {
            $trusted = self::getInstance()->isTrusted( $ip );
        }
        return true;
    }
    /**
     * @codeCoverageIgnore
     * @return TrustedXFF
     */
    public static function getInstance() {
        if ( !self::$instance ) {
            self::$instance = new TrustedXFF(
                require dirname( __DIR__ ) . '/trusted-hosts.php'
            );
        }
        return self::$instance;
    }
    /**
     * @param string $ip
     * @return bool
     */
    public function isTrusted( $ip ) {
        return $this->ipSet->match( $ip );
    }
}