Go to the documentation of this file.
32 protected function __construct( array $restrictions =
null ) {
33 if ( $restrictions !==
null ) {
51 return new self( $restrictions );
61 if ( !is_array( $restrictions ) ) {
62 throw new InvalidArgumentException(
'Invalid restrictions JSON' );
64 return new self( $restrictions );
68 static $validKeys = [
'IPAddresses' ];
69 static $neededKeys = [
'IPAddresses' ];
71 $keys = array_keys( $restrictions );
72 $invalidKeys = array_diff(
$keys, $validKeys );
74 throw new InvalidArgumentException(
75 'Array contains invalid keys: ' . implode(
', ', $invalidKeys )
78 $missingKeys = array_diff( $neededKeys,
$keys );
80 throw new InvalidArgumentException(
81 'Array is missing required keys: ' . implode(
', ', $missingKeys )
85 if ( !is_array( $restrictions[
'IPAddresses'] ) ) {
86 throw new InvalidArgumentException(
'IPAddresses is not an array' );
88 foreach ( $restrictions[
'IPAddresses'] as $ip ) {
90 throw new InvalidArgumentException(
"Invalid IP address: $ip" );
93 $this->ipAddresses = $restrictions[
'IPAddresses'];
111 public function toJson( $pretty =
false ) {
129 $status->setResult( $ok === array_filter( $ok ), $ok );
139 foreach ( $this->ipAddresses as $range ) {
A class to check request restrictions expressed as a JSON object.
loadFromArray(array $restrictions)
check(WebRequest $request)
Test against the passed WebRequest.
checkIP( $ip)
Test an IP address.
toJson( $pretty=false)
Return the restrictions as a JSON string.
__construct(array $restrictions=null)
static isInRange( $addr, $range)
Determine if a given IPv4/IPv6 address is in a given CIDR network.
static newGood( $value=null)
Factory function for good results.
The WebRequest class encapsulates getting at data passed in the URL or via a POSTed form stripping il...
getIP()
Work out the IP address based on various globals For trusted proxies, use the XFF client IP (first of...
toArray()
Return the restrictions as an array.
static newFromJson( $json)
static newFromArray(array $restrictions)
static isIPAddress( $ip)
Determine if a string is as valid IP address or network (CIDR prefix).