MediaWiki  master
MWRestrictions.php
Go to the documentation of this file.
1 <?php
21 use Wikimedia\IPSet;
22 use Wikimedia\IPUtils;
23 
28 
29  private $ipAddresses = [ '0.0.0.0/0', '::/0' ];
30 
35  protected function __construct( array $restrictions = null ) {
36  if ( $restrictions !== null ) {
37  $this->loadFromArray( $restrictions );
38  }
39  }
40 
44  public static function newDefault() {
45  return new self();
46  }
47 
53  public static function newFromArray( array $restrictions ) {
54  return new self( $restrictions );
55  }
56 
62  public static function newFromJson( $json ) {
63  $restrictions = FormatJson::decode( $json, true );
64  if ( !is_array( $restrictions ) ) {
65  throw new InvalidArgumentException( 'Invalid restrictions JSON' );
66  }
67  return new self( $restrictions );
68  }
69 
70  private function loadFromArray( array $restrictions ) {
71  static $validKeys = [ 'IPAddresses' ];
72  static $neededKeys = [ 'IPAddresses' ];
73 
74  $keys = array_keys( $restrictions );
75  $invalidKeys = array_diff( $keys, $validKeys );
76  if ( $invalidKeys ) {
77  throw new InvalidArgumentException(
78  'Array contains invalid keys: ' . implode( ', ', $invalidKeys )
79  );
80  }
81  $missingKeys = array_diff( $neededKeys, $keys );
82  if ( $missingKeys ) {
83  throw new InvalidArgumentException(
84  'Array is missing required keys: ' . implode( ', ', $missingKeys )
85  );
86  }
87 
88  if ( !is_array( $restrictions['IPAddresses'] ) ) {
89  throw new InvalidArgumentException( 'IPAddresses is not an array' );
90  }
91  foreach ( $restrictions['IPAddresses'] as $ip ) {
92  if ( !IPUtils::isIPAddress( $ip ) ) {
93  throw new InvalidArgumentException( "Invalid IP address: $ip" );
94  }
95  }
96  $this->ipAddresses = $restrictions['IPAddresses'];
97  }
98 
103  public function toArray() {
104  return [
105  'IPAddresses' => $this->ipAddresses,
106  ];
107  }
108 
114  public function toJson( $pretty = false ) {
115  return FormatJson::encode( $this->toArray(), $pretty, FormatJson::ALL_OK );
116  }
117 
118  public function __toString() {
119  return $this->toJson();
120  }
121 
127  public function check( WebRequest $request ) {
128  $ok = [
129  'ip' => $this->checkIP( $request->getIP() ),
130  ];
131  $status = Status::newGood();
132  $status->setResult( $ok === array_filter( $ok ), $ok );
133  return $status;
134  }
135 
141  public function checkIP( $ip ) {
142  $set = new IPSet( $this->ipAddresses );
143  return $set->match( $ip );
144  }
145 }
MWRestrictions
A class to check request restrictions expressed as a JSON object.
Definition: MWRestrictions.php:27
MWRestrictions\loadFromArray
loadFromArray(array $restrictions)
Definition: MWRestrictions.php:70
MWRestrictions\__toString
__toString()
Definition: MWRestrictions.php:118
MWRestrictions\check
check(WebRequest $request)
Test against the passed WebRequest.
Definition: MWRestrictions.php:127
MWRestrictions\$ipAddresses
$ipAddresses
Definition: MWRestrictions.php:29
FormatJson\ALL_OK
const ALL_OK
Skip escaping as many characters as reasonably possible.
Definition: FormatJson.php:55
MWRestrictions\checkIP
checkIP( $ip)
Test if an IP address is allowed by the restrictions.
Definition: MWRestrictions.php:141
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:114
MWRestrictions\__construct
__construct(array $restrictions=null)
Definition: MWRestrictions.php:35
MWRestrictions\newDefault
static newDefault()
Definition: MWRestrictions.php:44
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:1269
MWRestrictions\toArray
toArray()
Return the restrictions as an array.
Definition: MWRestrictions.php:103
MWRestrictions\newFromJson
static newFromJson( $json)
Definition: MWRestrictions.php:62
MWRestrictions\newFromArray
static newFromArray(array $restrictions)
Definition: MWRestrictions.php:53
$keys
$keys
Definition: testCompression.php:72