MediaWiki master
MWRestrictions.php
Go to the documentation of this file.
1<?php
13use Wikimedia\IPSet;
14use Wikimedia\IPUtils;
15
19class MWRestrictions implements Stringable {
20
22 private $ipAddresses = [ '0.0.0.0/0', '::/0' ];
23
25 private $pages = [];
26
28
33 protected function __construct( ?array $restrictions = null ) {
34 $this->validity = StatusValue::newGood();
35 if ( $restrictions !== null ) {
36 $this->loadFromArray( $restrictions );
37 }
38 }
39
43 public static function newDefault() {
44 return new self();
45 }
46
52 public static function newFromArray( array $restrictions ) {
53 return new self( $restrictions );
54 }
55
61 public static function newFromJson( $json ) {
62 $restrictions = FormatJson::decode( $json, true );
63 if ( !is_array( $restrictions ) ) {
64 throw new InvalidArgumentException( 'Invalid restrictions JSON' );
65 }
66 return new self( $restrictions );
67 }
68
69 private function loadFromArray( array $restrictions ) {
70 static $neededKeys = [ 'IPAddresses' ];
71
72 $keys = array_keys( $restrictions );
73 $missingKeys = array_diff( $neededKeys, $keys );
74 if ( $missingKeys ) {
75 throw new InvalidArgumentException(
76 'Array is missing required keys: ' . implode( ', ', $missingKeys )
77 );
78 }
79
80 if ( !is_array( $restrictions['IPAddresses'] ) ) {
81 throw new InvalidArgumentException( 'IPAddresses is not an array' );
82 }
83 foreach ( $restrictions['IPAddresses'] as $ip ) {
84 if ( !IPUtils::isIPAddress( $ip ) ) {
85 $this->validity->fatal( 'restrictionsfield-badip', $ip );
86 }
87 }
88 $this->ipAddresses = $restrictions['IPAddresses'];
89
90 if ( isset( $restrictions['Pages'] ) ) {
91 if ( !is_array( $restrictions['Pages'] ) ) {
92 throw new InvalidArgumentException( 'Pages is not an array of page names' );
93 }
94 foreach ( $restrictions['Pages'] as $page ) {
95 if ( !is_string( $page ) ) {
96 throw new InvalidArgumentException( "Pages contains non-string value: $page" );
97 }
98 }
99 $this->pages = $restrictions['Pages'];
100 }
101 }
102
107 public function toArray() {
108 $arr = [ 'IPAddresses' => $this->ipAddresses ];
109 if ( count( $this->pages ) ) {
110 $arr['Pages'] = $this->pages;
111 }
112 return $arr;
113 }
114
120 public function toJson( $pretty = false ) {
121 return FormatJson::encode( $this->toArray(), $pretty, FormatJson::ALL_OK );
122 }
123
124 public function __toString() {
125 return $this->toJson();
126 }
127
133 public function check( WebRequest $request ) {
134 $ok = [
135 'ip' => $this->checkIP( $request->getIP() ),
136 ];
137 $status = Status::newGood();
138 $status->setResult( $ok === array_filter( $ok ), $ok );
139 return $status;
140 }
141
149 public function userCan( LinkTarget $target ) {
150 if ( !$this->checkPage( $target ) ) {
151 return StatusValue::newFatal( 'session-page-restricted' );
152 }
153 return StatusValue::newGood();
154 }
155
161 public function checkIP( $ip ) {
162 $set = new IPSet( $this->ipAddresses );
163 return $set->match( $ip );
164 }
165
172 private function checkPage( LinkTarget $target ) {
173 if ( count( $this->pages ) === 0 ) {
174 return true;
175 }
176 $pagesNormalized = array_map( static function ( $titleText ) {
177 $title = Title::newFromText( $titleText );
178 return $title ? $title->getPrefixedText() : '';
179 }, $this->pages );
180 return in_array( Title::newFromLinkTarget( $target )->getPrefixedText(), $pagesNormalized, true );
181 }
182}
A class to check request restrictions expressed as a JSON object.
static newFromArray(array $restrictions)
static newFromJson( $json)
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
__construct(?array $restrictions=null)
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:44
Represents a title within MediaWiki.
Definition Title.php:70
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Represents the target of a wiki link.