MediaWiki  master
MWRestrictions.php
Go to the documentation of this file.
1 <?php
21 use Wikimedia\IPUtils;
22 
27 
28  private $ipAddresses = [ '0.0.0.0/0', '::/0' ];
29 
34  protected function __construct( array $restrictions = null ) {
35  if ( $restrictions !== null ) {
36  $this->loadFromArray( $restrictions );
37  }
38  }
39 
43  public static function newDefault() {
44  return new self();
45  }
46 
52  public static function newFromArray( array $restrictions ) {
53  return new self( $restrictions );
54  }
55 
61  public static function newFromJson( $json ) {
62  $restrictions = FormatJson::decode( $json, true );
63  if ( !is_array( $restrictions ) ) {
64  throw new InvalidArgumentException( 'Invalid restrictions JSON' );
65  }
66  return new self( $restrictions );
67  }
68 
69  private function loadFromArray( array $restrictions ) {
70  static $validKeys = [ 'IPAddresses' ];
71  static $neededKeys = [ 'IPAddresses' ];
72 
73  $keys = array_keys( $restrictions );
74  $invalidKeys = array_diff( $keys, $validKeys );
75  if ( $invalidKeys ) {
76  throw new InvalidArgumentException(
77  'Array contains invalid keys: ' . implode( ', ', $invalidKeys )
78  );
79  }
80  $missingKeys = array_diff( $neededKeys, $keys );
81  if ( $missingKeys ) {
82  throw new InvalidArgumentException(
83  'Array is missing required keys: ' . implode( ', ', $missingKeys )
84  );
85  }
86 
87  if ( !is_array( $restrictions['IPAddresses'] ) ) {
88  throw new InvalidArgumentException( 'IPAddresses is not an array' );
89  }
90  foreach ( $restrictions['IPAddresses'] as $ip ) {
91  if ( !IPUtils::isIPAddress( $ip ) ) {
92  throw new InvalidArgumentException( "Invalid IP address: $ip" );
93  }
94  }
95  $this->ipAddresses = $restrictions['IPAddresses'];
96  }
97 
102  public function toArray() {
103  return [
104  'IPAddresses' => $this->ipAddresses,
105  ];
106  }
107 
113  public function toJson( $pretty = false ) {
114  return FormatJson::encode( $this->toArray(), $pretty, FormatJson::ALL_OK );
115  }
116 
117  public function __toString() {
118  return $this->toJson();
119  }
120 
126  public function check( WebRequest $request ) {
127  $ok = [
128  'ip' => $this->checkIP( $request->getIP() ),
129  ];
130  $status = Status::newGood();
131  $status->setResult( $ok === array_filter( $ok ), $ok );
132  return $status;
133  }
134 
140  public function checkIP( $ip ) {
141  foreach ( $this->ipAddresses as $range ) {
142  if ( IPUtils::isInRange( $ip, $range ) ) {
143  return true;
144  }
145  }
146 
147  return false;
148  }
149 }
MWRestrictions
A class to check request restrictions expressed as a JSON object.
Definition: MWRestrictions.php:26
MWRestrictions\loadFromArray
loadFromArray(array $restrictions)
Definition: MWRestrictions.php:69
MWRestrictions\__toString
__toString()
Definition: MWRestrictions.php:117
MWRestrictions\check
check(WebRequest $request)
Test against the passed WebRequest.
Definition: MWRestrictions.php:126
MWRestrictions\$ipAddresses
$ipAddresses
Definition: MWRestrictions.php:28
FormatJson\ALL_OK
const ALL_OK
Skip escaping as many characters as reasonably possible.
Definition: FormatJson.php:55
MWRestrictions\checkIP
checkIP( $ip)
Test an IP address.
Definition: MWRestrictions.php:140
FormatJson\decode
static decode( $value, $assoc=false)
Decodes a JSON string.
Definition: FormatJson.php:174
FormatJson\encode
static encode( $value, $pretty=false, $escaping=0)
Returns the JSON representation of a value.
Definition: FormatJson.php:115
MWRestrictions\toJson
toJson( $pretty=false)
Return the restrictions as a JSON string.
Definition: MWRestrictions.php:113
MWRestrictions\__construct
__construct(array $restrictions=null)
Definition: MWRestrictions.php:34
MWRestrictions\newDefault
static newDefault()
Definition: MWRestrictions.php:43
StatusValue\newGood
static newGood( $value=null)
Factory function for good results.
Definition: StatusValue.php:82
WebRequest
The WebRequest class encapsulates getting at data passed in the URL or via a POSTed form stripping il...
Definition: WebRequest.php:42
WebRequest\getIP
getIP()
Work out the IP address based on various globals For trusted proxies, use the XFF client IP (first of...
Definition: WebRequest.php:1287
MWRestrictions\toArray
toArray()
Return the restrictions as an array.
Definition: MWRestrictions.php:102
MWRestrictions\newFromJson
static newFromJson( $json)
Definition: MWRestrictions.php:61
MWRestrictions\newFromArray
static newFromArray(array $restrictions)
Definition: MWRestrictions.php:52
$keys
$keys
Definition: testCompression.php:72