MediaWiki  master
MWRestrictions.php
Go to the documentation of this file.
1 <?php
23 use Wikimedia\IPSet;
24 use Wikimedia\IPUtils;
25 
30 
31  private $ipAddresses = [ '0.0.0.0/0', '::/0' ];
32 
37  protected function __construct( array $restrictions = null ) {
38  if ( $restrictions !== null ) {
39  $this->loadFromArray( $restrictions );
40  }
41  }
42 
46  public static function newDefault() {
47  return new self();
48  }
49 
55  public static function newFromArray( array $restrictions ) {
56  return new self( $restrictions );
57  }
58 
64  public static function newFromJson( $json ) {
65  $restrictions = FormatJson::decode( $json, true );
66  if ( !is_array( $restrictions ) ) {
67  throw new InvalidArgumentException( 'Invalid restrictions JSON' );
68  }
69  return new self( $restrictions );
70  }
71 
72  private function loadFromArray( array $restrictions ) {
73  static $validKeys = [ 'IPAddresses' ];
74  static $neededKeys = [ 'IPAddresses' ];
75 
76  $keys = array_keys( $restrictions );
77  $invalidKeys = array_diff( $keys, $validKeys );
78  if ( $invalidKeys ) {
79  throw new InvalidArgumentException(
80  'Array contains invalid keys: ' . implode( ', ', $invalidKeys )
81  );
82  }
83  $missingKeys = array_diff( $neededKeys, $keys );
84  if ( $missingKeys ) {
85  throw new InvalidArgumentException(
86  'Array is missing required keys: ' . implode( ', ', $missingKeys )
87  );
88  }
89 
90  if ( !is_array( $restrictions['IPAddresses'] ) ) {
91  throw new InvalidArgumentException( 'IPAddresses is not an array' );
92  }
93  foreach ( $restrictions['IPAddresses'] as $ip ) {
94  if ( !IPUtils::isIPAddress( $ip ) ) {
95  throw new InvalidArgumentException( "Invalid IP address: $ip" );
96  }
97  }
98  $this->ipAddresses = $restrictions['IPAddresses'];
99  }
100 
105  public function toArray() {
106  return [
107  'IPAddresses' => $this->ipAddresses,
108  ];
109  }
110 
116  public function toJson( $pretty = false ) {
117  return FormatJson::encode( $this->toArray(), $pretty, FormatJson::ALL_OK );
118  }
119 
120  public function __toString() {
121  return $this->toJson();
122  }
123 
129  public function check( WebRequest $request ) {
130  $ok = [
131  'ip' => $this->checkIP( $request->getIP() ),
132  ];
133  $status = Status::newGood();
134  $status->setResult( $ok === array_filter( $ok ), $ok );
135  return $status;
136  }
137 
143  public function checkIP( $ip ) {
144  $set = new IPSet( $this->ipAddresses );
145  return $set->match( $ip );
146  }
147 }
static encode( $value, $pretty=false, $escaping=0)
Returns the JSON representation of a value.
Definition: FormatJson.php:98
static decode( $value, $assoc=false)
Decodes a JSON string.
Definition: FormatJson.php:148
const ALL_OK
Skip escaping as many characters as reasonably possible.
Definition: FormatJson.php:57
A class to check request restrictions expressed as a JSON object.
static newFromArray(array $restrictions)
static newFromJson( $json)
static newDefault()
__construct(array $restrictions=null)
toArray()
Return the restrictions as an array.
checkIP( $ip)
Test if an IP address is allowed by the restrictions.
check(WebRequest $request)
Test against the passed WebRequest.
toJson( $pretty=false)
Return the restrictions as a JSON string.
The WebRequest class encapsulates getting at data passed in the URL or via a POSTed form stripping il...
Definition: WebRequest.php:50
getIP()
Work out the IP address based on various globals For trusted proxies, use the XFF client IP (first of...
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition: Status.php:58