| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
use MediaWiki\Json\FormatJson; |
| 22 |
use MediaWiki\Linker\LinkTarget; |
| 23 |
use MediaWiki\Request\WebRequest; |
| 24 |
use MediaWiki\Status\Status; |
| 25 |
use MediaWiki\Title\Title; |
| 26 |
use Wikimedia\IPSet; |
| 27 |
use Wikimedia\IPUtils; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
class MWRestrictions implements Stringable { |
| 33 |
|
| 34 |
|
| 35 |
private $ipAddresses = [ '0.0.0.0/0', '::/0' ]; |
| 36 |
|
| 37 |
|
| 38 |
private $pages = []; |
| 39 |
|
| 40 |
public StatusValue $validity; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
protected function __construct( ?array $restrictions = null ) { |
| 47 |
$this->validity = StatusValue::newGood(); |
| 48 |
if ( $restrictions !== null ) { |
| 49 |
$this->loadFromArray( $restrictions ); |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
public static function newDefault() { |
| 57 |
return new self(); |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
public static function newFromArray( array $restrictions ) { |
| 66 |
return new self( $restrictions ); |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
public static function newFromJson( $json ) { |
| 75 |
$restrictions = FormatJson::decode( $json, true ); |
| 76 |
if ( !is_array( $restrictions ) ) { |
| 77 |
throw new InvalidArgumentException( 'Invalid restrictions JSON' ); |
| 78 |
} |
| 79 |
return new self( $restrictions ); |
| 80 |
} |
| 81 |
|
| 82 |
private function loadFromArray( array $restrictions ) { |
| 83 |
static $neededKeys = [ 'IPAddresses' ]; |
| 84 |
|
| 85 |
$keys = array_keys( $restrictions ); |
| 86 |
$missingKeys = array_diff( $neededKeys, $keys ); |
| 87 |
if ( $missingKeys ) { |
| 88 |
throw new InvalidArgumentException( |
| 89 |
'Array is missing required keys: ' . implode( ', ', $missingKeys ) |
| 90 |
); |
| 91 |
} |
| 92 |
|
| 93 |
if ( !is_array( $restrictions['IPAddresses'] ) ) { |
| 94 |
throw new InvalidArgumentException( 'IPAddresses is not an array' ); |
| 95 |
} |
| 96 |
foreach ( $restrictions['IPAddresses'] as $ip ) { |
| 97 |
if ( !IPUtils::isIPAddress( $ip ) ) { |
| 98 |
$this->validity->fatal( 'restrictionsfield-badip', $ip ); |
| 99 |
} |
| 100 |
} |
| 101 |
$this->ipAddresses = $restrictions['IPAddresses']; |
| 102 |
|
| 103 |
if ( isset( $restrictions['Pages'] ) ) { |
| 104 |
if ( !is_array( $restrictions['Pages'] ) ) { |
| 105 |
throw new InvalidArgumentException( 'Pages is not an array of page names' ); |
| 106 |
} |
| 107 |
foreach ( $restrictions['Pages'] as $page ) { |
| 108 |
if ( !is_string( $page ) ) { |
| 109 |
throw new InvalidArgumentException( "Pages contains non-string value: $page" ); |
| 110 |
} |
| 111 |
} |
| 112 |
$this->pages = $restrictions['Pages']; |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
public function toArray() { |
| 121 |
$arr = [ 'IPAddresses' => $this->ipAddresses ]; |
| 122 |
if ( count( $this->pages ) ) { |
| 123 |
$arr['Pages'] = $this->pages; |
| 124 |
} |
| 125 |
return $arr; |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
public function toJson( $pretty = false ) { |
| 134 |
return FormatJson::encode( $this->toArray(), $pretty, FormatJson::ALL_OK ); |
| 135 |
} |
| 136 |
|
| 137 |
public function __toString() { |
| 138 |
return $this->toJson(); |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
public function check( WebRequest $request ) { |
| 147 |
$ok = [ |
| 148 |
'ip' => $this->checkIP( $request->getIP() ), |
| 149 |
]; |
| 150 |
$status = Status::newGood(); |
| 151 |
$status->setResult( $ok === array_filter( $ok ), $ok ); |
| 152 |
return $status; |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
public function userCan( LinkTarget $target ) { |
| 163 |
if ( !$this->checkPage( $target ) ) { |
| 164 |
return StatusValue::newFatal( 'session-page-restricted' ); |
| 165 |
} |
| 166 |
return StatusValue::newGood(); |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
public function checkIP( $ip ) { |
| 175 |
$set = new IPSet( $this->ipAddresses ); |
| 176 |
return $set->match( $ip ); |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
private function checkPage( LinkTarget $target ) { |
| 186 |
if ( count( $this->pages ) === 0 ) { |
| 187 |
return true; |
| 188 |
} |
| 189 |
$pagesNormalized = array_map( static function ( $titleText ) { |
| 190 |
$title = Title::newFromText( $titleText ); |
| 191 |
return $title ? $title->getPrefixedText() : ''; |
| 192 |
}, $this->pages ); |
| 193 |
return in_array( Title::newFromLinkTarget( $target )->getPrefixedText(), $pagesNormalized, true ); |
| 194 |
} |
| 195 |
} |