Expose $wgMaxArticleSize in siteinfo query api
[mediawiki.git] / includes / utils / MWRestrictions.php
blob617e8f59fd3fec6aa673e79e3578266577476bb4
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
31 protected function __construct( array $restrictions = null ) {
32 if ( $restrictions !== null ) {
33 $this->loadFromArray( $restrictions );
37 /**
38 * @return MWRestrictions
40 public static function newDefault() {
41 return new self();
44 /**
45 * @param array $restrictions
46 * @return MWRestrictions
48 public static function newFromArray( array $restrictions ) {
49 return new self( $restrictions );
52 /**
53 * @param string $json JSON representation of the restrictions
54 * @return MWRestrictions
56 public static function newFromJson( $json ) {
57 $restrictions = FormatJson::decode( $json, true );
58 if ( !is_array( $restrictions ) ) {
59 throw new InvalidArgumentException( 'Invalid restrictions JSON' );
61 return new self( $restrictions );
64 private function loadFromArray( array $restrictions ) {
65 static $validKeys = [ 'IPAddresses' ];
66 static $neededKeys = [ 'IPAddresses' ];
68 $keys = array_keys( $restrictions );
69 $invalidKeys = array_diff( $keys, $validKeys );
70 if ( $invalidKeys ) {
71 throw new InvalidArgumentException(
72 'Array contains invalid keys: ' . implode( ', ', $invalidKeys )
75 $missingKeys = array_diff( $neededKeys, $keys );
76 if ( $missingKeys ) {
77 throw new InvalidArgumentException(
78 'Array is missing required keys: ' . implode( ', ', $missingKeys )
82 if ( !is_array( $restrictions['IPAddresses'] ) ) {
83 throw new InvalidArgumentException( 'IPAddresses is not an array' );
85 foreach ( $restrictions['IPAddresses'] as $ip ) {
86 if ( !\IP::isIPAddress( $ip ) ) {
87 throw new InvalidArgumentException( "Invalid IP address: $ip" );
90 $this->ipAddresses = $restrictions['IPAddresses'];
93 /**
94 * Return the restrictions as an array
95 * @return array
97 public function toArray() {
98 return [
99 'IPAddresses' => $this->ipAddresses,
104 * Return the restrictions as a JSON string
105 * @param bool|string $pretty Pretty-print the JSON output, see FormatJson::encode
106 * @return string
108 public function toJson( $pretty = false ) {
109 return FormatJson::encode( $this->toArray(), $pretty, FormatJson::ALL_OK );
112 public function __toString() {
113 return $this->toJson();
117 * Test against the passed WebRequest
118 * @param WebRequest $request
119 * @return Status
121 public function check( WebRequest $request ) {
122 $ok = [
123 'ip' => $this->checkIP( $request->getIP() ),
125 $status = Status::newGood();
126 $status->setResult( $ok === array_filter( $ok ), $ok );
127 return $status;
131 * Test an IP address
132 * @param string $ip
133 * @return bool
135 public function checkIP( $ip ) {
136 foreach ( $this->ipAddresses as $range ) {
137 if ( \IP::isInRange( $ip, $range ) ) {
138 return true;
142 return false;