Merge "Http::getProxy() method to get proxy configuration"
[lhc/web/wiklou.git] / includes / utils / MWRestrictions.php
1 <?php
2 /**
3 * A class to check request restrictions expressed as a JSON object
4 *
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.
9 *
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.
14 *
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
19 */
20
21 /**
22 * A class to check request restrictions expressed as a JSON object
23 */
24 class MWRestrictions {
25
26 private $ipAddresses = [ '0.0.0.0/0', '::/0' ];
27
28 /**
29 * @param array $restrictions
30 */
31 protected function __construct( array $restrictions = null ) {
32 if ( $restrictions !== null ) {
33 $this->loadFromArray( $restrictions );
34 }
35 }
36
37 /**
38 * @return MWRestrictions
39 */
40 public static function newDefault() {
41 return new self();
42 }
43
44 /**
45 * @param array $restrictions
46 * @return MWRestrictions
47 */
48 public static function newFromArray( array $restrictions ) {
49 return new self( $restrictions );
50 }
51
52 /**
53 * @param string $json JSON representation of the restrictions
54 * @return MWRestrictions
55 */
56 public static function newFromJson( $json ) {
57 $restrictions = FormatJson::decode( $json, true );
58 if ( !is_array( $restrictions ) ) {
59 throw new InvalidArgumentException( 'Invalid restrictions JSON' );
60 }
61 return new self( $restrictions );
62 }
63
64 private function loadFromArray( array $restrictions ) {
65 static $validKeys = [ 'IPAddresses' ];
66 static $neededKeys = [ 'IPAddresses' ];
67
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 )
73 );
74 }
75 $missingKeys = array_diff( $neededKeys, $keys );
76 if ( $missingKeys ) {
77 throw new InvalidArgumentException(
78 'Array is missing required keys: ' . implode( ', ', $missingKeys )
79 );
80 }
81
82 if ( !is_array( $restrictions['IPAddresses'] ) ) {
83 throw new InvalidArgumentException( 'IPAddresses is not an array' );
84 }
85 foreach ( $restrictions['IPAddresses'] as $ip ) {
86 if ( !\IP::isIPAddress( $ip ) ) {
87 throw new InvalidArgumentException( "Invalid IP address: $ip" );
88 }
89 }
90 $this->ipAddresses = $restrictions['IPAddresses'];
91 }
92
93 /**
94 * Return the restrictions as an array
95 * @return array
96 */
97 public function toArray() {
98 return [
99 'IPAddresses' => $this->ipAddresses,
100 ];
101 }
102
103 /**
104 * Return the restrictions as a JSON string
105 * @param bool|string $pretty Pretty-print the JSON output, see FormatJson::encode
106 * @return string
107 */
108 public function toJson( $pretty = false ) {
109 return FormatJson::encode( $this->toArray(), $pretty, FormatJson::ALL_OK );
110 }
111
112 public function __toString() {
113 return $this->toJson();
114 }
115
116 /**
117 * Test against the passed WebRequest
118 * @param WebRequest $request
119 * @return Status
120 */
121 public function check( WebRequest $request ) {
122 $ok = [
123 'ip' => $this->checkIP( $request->getIP() ),
124 ];
125 $status = Status::newGood();
126 $status->setResult( $ok === array_filter( $ok ), $ok );
127 return $status;
128 }
129
130 /**
131 * Test an IP address
132 * @param string $ip
133 * @return bool
134 */
135 public function checkIP( $ip ) {
136 foreach ( $this->ipAddresses as $range ) {
137 if ( \IP::isInRange( $ip, $range ) ) {
138 return true;
139 }
140 }
141
142 return false;
143 }
144 }