| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
namespace MediaWiki\User; |
| 24 |
|
| 25 |
use InvalidArgumentException; |
| 26 |
use MediaWiki\Config\ServiceOptions; |
| 27 |
use MediaWiki\HookContainer\HookContainer; |
| 28 |
use MediaWiki\HookContainer\HookRunner; |
| 29 |
use MediaWiki\Language\Language; |
| 30 |
use MediaWiki\MainConfigNames; |
| 31 |
use MediaWiki\Title\MalformedTitleException; |
| 32 |
use MediaWiki\Title\TitleParser; |
| 33 |
use MediaWiki\User\TempUser\TempUserConfig; |
| 34 |
use Psr\Log\LoggerInterface; |
| 35 |
use Wikimedia\IPUtils; |
| 36 |
use Wikimedia\Message\ITextFormatter; |
| 37 |
use Wikimedia\Message\MessageValue; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
class UserNameUtils implements UserRigorOptions { |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
public const CONSTRUCTOR_OPTIONS = [ |
| 50 |
MainConfigNames::MaxNameChars, |
| 51 |
MainConfigNames::ReservedUsernames, |
| 52 |
MainConfigNames::InvalidUsernameCharacters |
| 53 |
]; |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
private const IPV4_ADDRESS = '\d{1,3}\.\d{1,3}\.\d{1,3}\.(?:xxx|\d{1,3})'; |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
private ServiceOptions $options; |
| 64 |
private Language $contentLang; |
| 65 |
private LoggerInterface $logger; |
| 66 |
private TitleParser $titleParser; |
| 67 |
private ITextFormatter $textFormatter; |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
private $reservedUsernames = false; |
| 73 |
|
| 74 |
private HookRunner $hookRunner; |
| 75 |
private TempUserConfig $tempUserConfig; |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
public function __construct( |
| 87 |
ServiceOptions $options, |
| 88 |
Language $contentLang, |
| 89 |
LoggerInterface $logger, |
| 90 |
TitleParser $titleParser, |
| 91 |
ITextFormatter $textFormatter, |
| 92 |
HookContainer $hookContainer, |
| 93 |
TempUserConfig $tempUserConfig |
| 94 |
) { |
| 95 |
$options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS ); |
| 96 |
$this->options = $options; |
| 97 |
$this->contentLang = $contentLang; |
| 98 |
$this->logger = $logger; |
| 99 |
$this->titleParser = $titleParser; |
| 100 |
$this->textFormatter = $textFormatter; |
| 101 |
$this->hookRunner = new HookRunner( $hookContainer ); |
| 102 |
$this->tempUserConfig = $tempUserConfig; |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
public function isValid( string $name ): bool { |
| 117 |
if ( $name === '' |
| 118 |
|| $this->isIP( $name ) |
| 119 |
|| $this->isValidIPRange( $name ) |
| 120 |
|| $this->isLikeIPv4DashRange( $name ) |
| 121 |
|| str_contains( $name, '/' ) |
| 122 |
|| strlen( $name ) > $this->options->get( MainConfigNames::MaxNameChars ) |
| 123 |
|| $name !== $this->contentLang->ucfirst( $name ) |
| 124 |
) { |
| 125 |
return false; |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
try { |
| 131 |
$title = $this->titleParser->parseTitle( $name ); |
| 132 |
} catch ( MalformedTitleException $_ ) { |
| 133 |
$title = null; |
| 134 |
} |
| 135 |
|
| 136 |
if ( $title === null |
| 137 |
|| $title->getNamespace() |
| 138 |
|| strcmp( $name, $title->getText() ) |
| 139 |
) { |
| 140 |
return false; |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
$unicodeList = '/[' . |
| 146 |
'\x{0080}-\x{009f}' . |
| 147 |
'\x{00a0}' . |
| 148 |
'\x{2000}-\x{200f}' . |
| 149 |
'\x{2028}-\x{202f}' . |
| 150 |
'\x{3000}' . |
| 151 |
'\x{e000}-\x{f8ff}' . |
| 152 |
']/u'; |
| 153 |
if ( preg_match( $unicodeList, $name ) ) { |
| 154 |
return false; |
| 155 |
} |
| 156 |
|
| 157 |
return true; |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
public function isUsable( string $name ): bool { |
| 172 |
|
| 173 |
if ( !$this->isValid( $name ) ) { |
| 174 |
return false; |
| 175 |
} |
| 176 |
|
| 177 |
if ( !$this->reservedUsernames ) { |
| 178 |
$reservedUsernames = $this->options->get( MainConfigNames::ReservedUsernames ); |
| 179 |
$this->hookRunner->onUserGetReservedNames( $reservedUsernames ); |
| 180 |
foreach ( $reservedUsernames as &$reserved ) { |
| 181 |
if ( str_starts_with( $reserved, 'msg:' ) ) { |
| 182 |
$reserved = $this->textFormatter->format( |
| 183 |
MessageValue::new( substr( $reserved, 4 ) ) |
| 184 |
); |
| 185 |
} |
| 186 |
} |
| 187 |
$this->reservedUsernames = $reservedUsernames; |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
if ( in_array( $name, $this->reservedUsernames, true ) ) { |
| 192 |
return false; |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
if ( |
| 200 |
$this->isTempReserved( $name ) && |
| 201 |
( !$this->tempUserConfig->isEnabled() || !$this->isTemp( $name ) ) |
| 202 |
) { |
| 203 |
return false; |
| 204 |
} |
| 205 |
|
| 206 |
return true; |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
public function isCreatable( string $name ): bool { |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
if ( strlen( $name ) > 235 ) { |
| 226 |
$this->logger->debug( |
| 227 |
__METHOD__ . ": '$name' uncreatable due to length" |
| 228 |
); |
| 229 |
return false; |
| 230 |
} |
| 231 |
|
| 232 |
$invalid = $this->options->get( MainConfigNames::InvalidUsernameCharacters ); |
| 233 |
|
| 234 |
if ( $invalid !== '' && |
| 235 |
preg_match( '/[' . preg_quote( $invalid, '/' ) . ']/', $name ) |
| 236 |
) { |
| 237 |
$this->logger->debug( |
| 238 |
__METHOD__ . ": '$name' uncreatable due to wgInvalidUsernameCharacters" |
| 239 |
); |
| 240 |
return false; |
| 241 |
} |
| 242 |
|
| 243 |
if ( $this->isTempReserved( $name ) ) { |
| 244 |
$this->logger->debug( |
| 245 |
__METHOD__ . ": '$name' uncreatable due to TempUserConfig" |
| 246 |
); |
| 247 |
return false; |
| 248 |
} |
| 249 |
|
| 250 |
return $this->isUsable( $name ); |
| 251 |
} |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
public function getCanonical( string $name, string $validate = self::RIGOR_VALID ) { |
| 268 |
|
| 269 |
$name = $this->contentLang->ucfirst( $name ); |
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
if ( strpos( $name, '#' ) !== false ) { |
| 275 |
return false; |
| 276 |
} |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
if ( $validate === self::RIGOR_NONE ) { |
| 281 |
|
| 282 |
|
| 283 |
$nsPrefix = $this->contentLang->getNsText( NS_USER ) . ':'; |
| 284 |
if ( str_starts_with( $name, $nsPrefix ) ) { |
| 285 |
$name = str_replace( $nsPrefix, '', $name ); |
| 286 |
} |
| 287 |
$name = strtr( $name, '_', ' ' ); |
| 288 |
return $name; |
| 289 |
} |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
try { |
| 294 |
$title = $this->titleParser->parseTitle( $name, NS_USER ); |
| 295 |
} catch ( MalformedTitleException $_ ) { |
| 296 |
$title = null; |
| 297 |
} |
| 298 |
|
| 299 |
|
| 300 |
if ( $title === null |
| 301 |
|| $title->getNamespace() !== NS_USER |
| 302 |
|| $title->isExternal() |
| 303 |
) { |
| 304 |
return false; |
| 305 |
} |
| 306 |
|
| 307 |
$name = $title->getText(); |
| 308 |
|
| 309 |
|
| 310 |
switch ( $validate ) { |
| 311 |
case self::RIGOR_VALID: |
| 312 |
return $this->isValid( $name ) ? $name : false; |
| 313 |
case self::RIGOR_USABLE: |
| 314 |
return $this->isUsable( $name ) ? $name : false; |
| 315 |
case self::RIGOR_CREATABLE: |
| 316 |
return $this->isCreatable( $name ) ? $name : false; |
| 317 |
default: |
| 318 |
throw new InvalidArgumentException( |
| 319 |
"Invalid parameter value for validation ($validate) in " . |
| 320 |
__METHOD__ |
| 321 |
); |
| 322 |
} |
| 323 |
} |
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
public function isIP( string $name ): bool { |
| 344 |
$anyIPv4 = '/^' . self::IPV4_ADDRESS . '$/'; |
| 345 |
$validIP = IPUtils::isValid( $name ); |
| 346 |
return $validIP || preg_match( $anyIPv4, $name ); |
| 347 |
} |
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
public function isValidIPRange( string $range ): bool { |
| 356 |
return IPUtils::isValidRange( $range ); |
| 357 |
} |
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
public function isLikeIPv4DashRange( string $range ): bool { |
| 368 |
return preg_match( |
| 369 |
'/^' . self::IPV4_ADDRESS . '-' . self::IPV4_ADDRESS . '$/', |
| 370 |
$range |
| 371 |
); |
| 372 |
} |
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
public function isTemp( string $name ) { |
| 382 |
return $this->tempUserConfig->isTempName( $name ); |
| 383 |
} |
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
public function isTempReserved( string $name ) { |
| 396 |
return $this->tempUserConfig->isReservedName( $name ); |
| 397 |
} |
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
public function getTempPlaceholder() { |
| 406 |
return $this->tempUserConfig->getPlaceholderName(); |
| 407 |
} |
| 408 |
} |