MediaWiki master
MWRestrictions.php
Go to the documentation of this file.
1<?php
26use Wikimedia\IPSet;
27use Wikimedia\IPUtils;
28
32class MWRestrictions implements Stringable {
33
34 private $ipAddresses = [ '0.0.0.0/0', '::/0' ];
35
36 private $pages = [];
37
39
44 protected function __construct( array $restrictions = null ) {
45 $this->validity = StatusValue::newGood();
46 if ( $restrictions !== null ) {
47 $this->loadFromArray( $restrictions );
48 }
49 }
50
54 public static function newDefault() {
55 return new self();
56 }
57
63 public static function newFromArray( array $restrictions ) {
64 return new self( $restrictions );
65 }
66
72 public static function newFromJson( $json ) {
73 $restrictions = FormatJson::decode( $json, true );
74 if ( !is_array( $restrictions ) ) {
75 throw new InvalidArgumentException( 'Invalid restrictions JSON' );
76 }
77 return new self( $restrictions );
78 }
79
80 private function loadFromArray( array $restrictions ) {
81 static $neededKeys = [ 'IPAddresses' ];
82
83 $keys = array_keys( $restrictions );
84 $missingKeys = array_diff( $neededKeys, $keys );
85 if ( $missingKeys ) {
86 throw new InvalidArgumentException(
87 'Array is missing required keys: ' . implode( ', ', $missingKeys )
88 );
89 }
90
91 if ( !is_array( $restrictions['IPAddresses'] ) ) {
92 throw new InvalidArgumentException( 'IPAddresses is not an array' );
93 }
94 foreach ( $restrictions['IPAddresses'] as $ip ) {
95 if ( !IPUtils::isIPAddress( $ip ) ) {
96 $this->validity->fatal( 'restrictionsfield-badip', $ip );
97 }
98 }
99 $this->ipAddresses = $restrictions['IPAddresses'];
100
101 if ( isset( $restrictions['Pages'] ) ) {
102 if ( !is_array( $restrictions['Pages'] ) ) {
103 throw new InvalidArgumentException( 'Pages is not an array of page names' );
104 }
105 foreach ( $restrictions['Pages'] as $page ) {
106 if ( !is_string( $page ) ) {
107 throw new InvalidArgumentException( "Pages contains non-string value: $page" );
108 }
109 }
110 $this->pages = $restrictions['Pages'];
111 }
112 }
113
118 public function toArray() {
119 $arr = [ 'IPAddresses' => $this->ipAddresses ];
120 if ( count( $this->pages ) ) {
121 $arr['Pages'] = $this->pages;
122 }
123 return $arr;
124 }
125
131 public function toJson( $pretty = false ) {
132 return FormatJson::encode( $this->toArray(), $pretty, FormatJson::ALL_OK );
133 }
134
135 public function __toString() {
136 return $this->toJson();
137 }
138
144 public function check( WebRequest $request ) {
145 $ok = [
146 'ip' => $this->checkIP( $request->getIP() ),
147 ];
148 $status = Status::newGood();
149 $status->setResult( $ok === array_filter( $ok ), $ok );
150 return $status;
151 }
152
160 public function userCan( LinkTarget $target ) {
161 if ( !$this->checkPage( $target ) ) {
162 return StatusValue::newFatal( 'session-page-restricted' );
163 }
164 return StatusValue::newGood();
165 }
166
172 public function checkIP( $ip ) {
173 $set = new IPSet( $this->ipAddresses );
174 return $set->match( $ip );
175 }
176
183 private function checkPage( LinkTarget $target ) {
184 if ( count( $this->pages ) === 0 ) {
185 return true;
186 }
187 $pagesNormalized = array_map( static function ( $titleText ) {
188 $title = Title::newFromText( $titleText );
189 return $title ? $title->getPrefixedText() : '';
190 }, $this->pages );
191 return in_array( Title::newFromLinkTarget( $target )->getPrefixedText(), $pagesNormalized, true );
192 }
193}
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.
userCan(LinkTarget $target)
Test whether an action on the target is allowed by the restrictions.
check(WebRequest $request)
Test against the passed WebRequest.
StatusValue $validity
toJson( $pretty=false)
Return the restrictions as a JSON string.
JSON formatter wrapper class.
The WebRequest class encapsulates getting at data passed in the URL or via a POSTed form,...
getIP()
Work out the IP address based on various globals For trusted proxies, use the XFF client IP (first of...
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition Status.php:54
Represents a title within MediaWiki.
Definition Title.php:79
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Represents the target of a wiki link.