MediaWiki REL1_37
MWRestrictions.php
Go to the documentation of this file.
1<?php
21use Wikimedia\IPSet;
22use 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}
A class to check request restrictions expressed as a JSON object.
static newFromArray(array $restrictions)
static newFromJson( $json)
__construct(array $restrictions=null)
toArray()
Return the restrictions as an array.
checkIP( $ip)
Test if an IP address is allowed by the restrictions.
loadFromArray(array $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...
getIP()
Work out the IP address based on various globals For trusted proxies, use the XFF client IP (first of...