MediaWiki master
MWRestrictions.php
Go to the documentation of this file.
1<?php
9namespace MediaWiki\Utils;
10
11use InvalidArgumentException;
17use StatusValue;
18use Stringable;
19use Wikimedia\IPSet;
20use Wikimedia\IPUtils;
21
25class MWRestrictions implements Stringable {
26
28 private $ipAddresses = [ '0.0.0.0/0', '::/0' ];
29
31 private $pages = [];
32
34
39 protected function __construct( ?array $restrictions = null ) {
40 $this->validity = StatusValue::newGood();
41 if ( $restrictions !== null ) {
42 $this->loadFromArray( $restrictions );
43 }
44 }
45
49 public static function newDefault() {
50 return new self();
51 }
52
58 public static function newFromArray( array $restrictions ) {
59 return new self( $restrictions );
60 }
61
67 public static function newFromJson( $json ) {
68 $restrictions = FormatJson::decode( $json, true );
69 if ( !is_array( $restrictions ) ) {
70 throw new InvalidArgumentException( 'Invalid restrictions JSON' );
71 }
72 return new self( $restrictions );
73 }
74
75 private function loadFromArray( array $restrictions ) {
76 static $neededKeys = [ 'IPAddresses' ];
77
78 $keys = array_keys( $restrictions );
79 $missingKeys = array_diff( $neededKeys, $keys );
80 if ( $missingKeys ) {
81 throw new InvalidArgumentException(
82 'Array is missing required keys: ' . implode( ', ', $missingKeys )
83 );
84 }
85
86 if ( !is_array( $restrictions['IPAddresses'] ) ) {
87 throw new InvalidArgumentException( 'IPAddresses is not an array' );
88 }
89 foreach ( $restrictions['IPAddresses'] as $ip ) {
90 if ( !IPUtils::isIPAddress( $ip ) ) {
91 $this->validity->fatal( 'restrictionsfield-badip', $ip );
92 }
93 }
94 $this->ipAddresses = $restrictions['IPAddresses'];
95
96 if ( isset( $restrictions['Pages'] ) ) {
97 if ( !is_array( $restrictions['Pages'] ) ) {
98 throw new InvalidArgumentException( 'Pages is not an array of page names' );
99 }
100 foreach ( $restrictions['Pages'] as $page ) {
101 if ( !is_string( $page ) ) {
102 throw new InvalidArgumentException( "Pages contains non-string value: $page" );
103 }
104 }
105 $this->pages = $restrictions['Pages'];
106 }
107 }
108
113 public function toArray() {
114 $arr = [ 'IPAddresses' => $this->ipAddresses ];
115 if ( count( $this->pages ) ) {
116 $arr['Pages'] = $this->pages;
117 }
118 return $arr;
119 }
120
126 public function toJson( $pretty = false ) {
127 return FormatJson::encode( $this->toArray(), $pretty, FormatJson::ALL_OK );
128 }
129
130 public function __toString() {
131 return $this->toJson();
132 }
133
139 public function check( WebRequest $request ) {
140 $ok = [
141 'ip' => $this->checkIP( $request->getIP() ),
142 ];
143 $status = Status::newGood();
144 $status->setResult( $ok === array_filter( $ok ), $ok );
145 return $status;
146 }
147
155 public function userCan( LinkTarget $target ) {
156 if ( !$this->checkPage( $target ) ) {
157 return StatusValue::newFatal( 'session-page-restricted' );
158 }
159 return StatusValue::newGood();
160 }
161
167 public function checkIP( $ip ) {
168 $set = new IPSet( $this->ipAddresses );
169 return $set->match( $ip );
170 }
171
178 private function checkPage( LinkTarget $target ) {
179 if ( count( $this->pages ) === 0 ) {
180 return true;
181 }
182 $pagesNormalized = array_map( static function ( $titleText ) {
183 $title = Title::newFromText( $titleText );
184 return $title ? $title->getPrefixedText() : '';
185 }, $this->pages );
186 return in_array( Title::newFromLinkTarget( $target )->getPrefixedText(), $pagesNormalized, true );
187 }
188}
189
191class_alias( MWRestrictions::class, 'MWRestrictions' );
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:44
Represents a title within MediaWiki.
Definition Title.php:69
A class to check request restrictions expressed as a JSON object.
toJson( $pretty=false)
Return the restrictions as a JSON string.
__construct(?array $restrictions=null)
toArray()
Return the restrictions as an array.
check(WebRequest $request)
Test against the passed WebRequest.
userCan(LinkTarget $target)
Test whether an action on the target is allowed by the restrictions.
static newFromArray(array $restrictions)
checkIP( $ip)
Test if an IP address is allowed by the restrictions.
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Represents the target of a wiki link.