Move Blob class to Rdbms namespaces
[mediawiki.git] / includes / utils / MWRestrictions.php
blobcaf88a15f1c83e3d7cd8f9c6b8a4c6a3dd90494c
1 <?php
2 /**
3 * A class to check request restrictions expressed as a JSON object
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 /**
22 * A class to check request restrictions expressed as a JSON object
24 class MWRestrictions {
26 private $ipAddresses = [ '0.0.0.0/0', '::/0' ];
28 /**
29 * @param array $restrictions
30 * @throws InvalidArgumentException
32 protected function __construct( array $restrictions = null ) {
33 if ( $restrictions !== null ) {
34 $this->loadFromArray( $restrictions );
38 /**
39 * @return MWRestrictions
41 public static function newDefault() {
42 return new self();
45 /**
46 * @param array $restrictions
47 * @return MWRestrictions
48 * @throws InvalidArgumentException
50 public static function newFromArray( array $restrictions ) {
51 return new self( $restrictions );
54 /**
55 * @param string $json JSON representation of the restrictions
56 * @return MWRestrictions
57 * @throws InvalidArgumentException
59 public static function newFromJson( $json ) {
60 $restrictions = FormatJson::decode( $json, true );
61 if ( !is_array( $restrictions ) ) {
62 throw new InvalidArgumentException( 'Invalid restrictions JSON' );
64 return new self( $restrictions );
67 private function loadFromArray( array $restrictions ) {
68 static $validKeys = [ 'IPAddresses' ];
69 static $neededKeys = [ 'IPAddresses' ];
71 $keys = array_keys( $restrictions );
72 $invalidKeys = array_diff( $keys, $validKeys );
73 if ( $invalidKeys ) {
74 throw new InvalidArgumentException(
75 'Array contains invalid keys: ' . implode( ', ', $invalidKeys )
78 $missingKeys = array_diff( $neededKeys, $keys );
79 if ( $missingKeys ) {
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 ) {
89 if ( !\IP::isIPAddress( $ip ) ) {
90 throw new InvalidArgumentException( "Invalid IP address: $ip" );
93 $this->ipAddresses = $restrictions['IPAddresses'];
96 /**
97 * Return the restrictions as an array
98 * @return array
100 public function toArray() {
101 return [
102 'IPAddresses' => $this->ipAddresses,
107 * Return the restrictions as a JSON string
108 * @param bool|string $pretty Pretty-print the JSON output, see FormatJson::encode
109 * @return string
111 public function toJson( $pretty = false ) {
112 return FormatJson::encode( $this->toArray(), $pretty, FormatJson::ALL_OK );
115 public function __toString() {
116 return $this->toJson();
120 * Test against the passed WebRequest
121 * @param WebRequest $request
122 * @return Status
124 public function check( WebRequest $request ) {
125 $ok = [
126 'ip' => $this->checkIP( $request->getIP() ),
128 $status = Status::newGood();
129 $status->setResult( $ok === array_filter( $ok ), $ok );
130 return $status;
134 * Test an IP address
135 * @param string $ip
136 * @return bool
138 public function checkIP( $ip ) {
139 foreach ( $this->ipAddresses as $range ) {
140 if ( \IP::isInRange( $ip, $range ) ) {
141 return true;
145 return false;