MediaWiki 1.42.1
MWRestrictions.php
Go to the documentation of this file.
1<?php
24use Wikimedia\IPSet;
25use Wikimedia\IPUtils;
26
31
32 private $ipAddresses = [ '0.0.0.0/0', '::/0' ];
33
34 private $pages = [];
35
37
42 protected function __construct( array $restrictions = null ) {
43 $this->validity = StatusValue::newGood();
44 if ( $restrictions !== null ) {
45 $this->loadFromArray( $restrictions );
46 }
47 }
48
52 public static function newDefault() {
53 return new self();
54 }
55
61 public static function newFromArray( array $restrictions ) {
62 return new self( $restrictions );
63 }
64
70 public static function newFromJson( $json ) {
71 $restrictions = FormatJson::decode( $json, true );
72 if ( !is_array( $restrictions ) ) {
73 throw new InvalidArgumentException( 'Invalid restrictions JSON' );
74 }
75 return new self( $restrictions );
76 }
77
78 private function loadFromArray( array $restrictions ) {
79 static $neededKeys = [ 'IPAddresses' ];
80
81 $keys = array_keys( $restrictions );
82 $missingKeys = array_diff( $neededKeys, $keys );
83 if ( $missingKeys ) {
84 throw new InvalidArgumentException(
85 'Array is missing required keys: ' . implode( ', ', $missingKeys )
86 );
87 }
88
89 if ( !is_array( $restrictions['IPAddresses'] ) ) {
90 throw new InvalidArgumentException( 'IPAddresses is not an array' );
91 }
92 foreach ( $restrictions['IPAddresses'] as $ip ) {
93 if ( !IPUtils::isIPAddress( $ip ) ) {
94 $this->validity->fatal( 'restrictionsfield-badip', $ip );
95 }
96 }
97 $this->ipAddresses = $restrictions['IPAddresses'];
98
99 if ( isset( $restrictions['Pages'] ) ) {
100 if ( !is_array( $restrictions['Pages'] ) ) {
101 throw new InvalidArgumentException( 'Pages is not an array of page names' );
102 }
103 foreach ( $restrictions['Pages'] as $page ) {
104 if ( !is_string( $page ) ) {
105 throw new InvalidArgumentException( "Pages contains non-string value: $page" );
106 }
107 }
108 $this->pages = $restrictions['Pages'];
109 }
110 }
111
116 public function toArray() {
117 $arr = [ 'IPAddresses' => $this->ipAddresses ];
118 if ( count( $this->pages ) ) {
119 $arr['Pages'] = $this->pages;
120 }
121 return $arr;
122 }
123
129 public function toJson( $pretty = false ) {
130 return FormatJson::encode( $this->toArray(), $pretty, FormatJson::ALL_OK );
131 }
132
133 public function __toString() {
134 return $this->toJson();
135 }
136
142 public function check( WebRequest $request ) {
143 $ok = [
144 'ip' => $this->checkIP( $request->getIP() ),
145 ];
146 $status = Status::newGood();
147 $status->setResult( $ok === array_filter( $ok ), $ok );
148 return $status;
149 }
150
158 public function userCan( LinkTarget $target ) {
159 if ( !$this->checkPage( $target ) ) {
160 return StatusValue::newFatal( 'session-page-restricted' );
161 }
162 return StatusValue::newGood();
163 }
164
170 public function checkIP( $ip ) {
171 $set = new IPSet( $this->ipAddresses );
172 return $set->match( $ip );
173 }
174
181 private function checkPage( LinkTarget $target ) {
182 if ( count( $this->pages ) === 0 ) {
183 return true;
184 }
185 $pagesNormalized = array_map( static function ( $titleText ) {
186 $title = Title::newFromText( $titleText );
187 return $title ? $title->getPrefixedText() : '';
188 }, $this->pages );
189 return in_array( Title::newFromLinkTarget( $target )->getPrefixedText(), $pagesNormalized, true );
190 }
191}
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.
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
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Represents the target of a wiki link.