loadFromArray( $restrictions ); } } /** * @return MWRestrictions */ public static function newDefault() { return new self(); } /** * @param array $restrictions * @return MWRestrictions * @throws InvalidArgumentException */ public static function newFromArray( array $restrictions ) { return new self( $restrictions ); } /** * @param string $json JSON representation of the restrictions * @return MWRestrictions * @throws InvalidArgumentException */ public static function newFromJson( $json ) { $restrictions = FormatJson::decode( $json, true ); if ( !is_array( $restrictions ) ) { throw new InvalidArgumentException( 'Invalid restrictions JSON' ); } return new self( $restrictions ); } private function loadFromArray( array $restrictions ) { static $validKeys = [ 'IPAddresses' ]; static $neededKeys = [ 'IPAddresses' ]; $keys = array_keys( $restrictions ); $invalidKeys = array_diff( $keys, $validKeys ); if ( $invalidKeys ) { throw new InvalidArgumentException( 'Array contains invalid keys: ' . implode( ', ', $invalidKeys ) ); } $missingKeys = array_diff( $neededKeys, $keys ); if ( $missingKeys ) { throw new InvalidArgumentException( 'Array is missing required keys: ' . implode( ', ', $missingKeys ) ); } if ( !is_array( $restrictions['IPAddresses'] ) ) { throw new InvalidArgumentException( 'IPAddresses is not an array' ); } foreach ( $restrictions['IPAddresses'] as $ip ) { if ( !\IP::isIPAddress( $ip ) ) { throw new InvalidArgumentException( "Invalid IP address: $ip" ); } } $this->ipAddresses = $restrictions['IPAddresses']; } /** * Return the restrictions as an array * @return array */ public function toArray() { return [ 'IPAddresses' => $this->ipAddresses, ]; } /** * Return the restrictions as a JSON string * @param bool|string $pretty Pretty-print the JSON output, see FormatJson::encode * @return string */ public function toJson( $pretty = false ) { return FormatJson::encode( $this->toArray(), $pretty, FormatJson::ALL_OK ); } public function __toString() { return $this->toJson(); } /** * Test against the passed WebRequest * @param WebRequest $request * @return Status */ public function check( WebRequest $request ) { $ok = [ 'ip' => $this->checkIP( $request->getIP() ), ]; $status = Status::newGood(); $status->setResult( $ok === array_filter( $ok ), $ok ); return $status; } /** * Test an IP address * @param string $ip * @return bool */ public function checkIP( $ip ) { foreach ( $this->ipAddresses as $range ) { if ( \IP::isInRange( $ip, $range ) ) { return true; } } return false; } }