2015-07-17 22:04:02 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
use MediaWiki\Widget\UserInputWidget;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Implements a text input field for user names.
|
|
|
|
|
* Automatically auto-completes if using the OOUI display format.
|
|
|
|
|
*
|
|
|
|
|
* FIXME: Does not work for forms that support GET requests.
|
|
|
|
|
*
|
|
|
|
|
* Optional parameters:
|
|
|
|
|
* 'exists' - Whether to validate that the user already exists
|
2015-08-25 18:05:35 +00:00
|
|
|
* 'ipallowed' - Whether an IP adress is interpreted as "valid"
|
|
|
|
|
* 'iprange' - Whether an IP adress range is interpreted as "valid"
|
|
|
|
|
* 'iprangelimits' - Specifies the valid IP ranges for IPv4 and IPv6 in an array.
|
|
|
|
|
* defaults to IPv4 => 16; IPv6 => 32.
|
2015-07-17 22:04:02 +00:00
|
|
|
*
|
|
|
|
|
* @since 1.26
|
|
|
|
|
*/
|
|
|
|
|
class HTMLUserTextField extends HTMLTextField {
|
|
|
|
|
public function __construct( $params ) {
|
2015-08-25 18:05:35 +00:00
|
|
|
$params = wfArrayPlus2d( $params, [
|
|
|
|
|
'exists' => false,
|
|
|
|
|
'ipallowed' => false,
|
|
|
|
|
'iprange' => false,
|
|
|
|
|
'iprangelimits' => [
|
|
|
|
|
'IPv4' => '16',
|
|
|
|
|
'IPv6' => '32',
|
|
|
|
|
],
|
|
|
|
|
]
|
|
|
|
|
);
|
2015-07-17 22:04:02 +00:00
|
|
|
|
|
|
|
|
parent::__construct( $params );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function validate( $value, $alldata ) {
|
2018-07-17 04:54:03 +00:00
|
|
|
// Default value (from getDefault()) is null, User::newFromName() expects a string
|
|
|
|
|
if ( $value === null ) {
|
|
|
|
|
$value = '';
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-17 22:04:02 +00:00
|
|
|
// check, if a user exists with the given username
|
2015-07-24 16:59:53 +00:00
|
|
|
$user = User::newFromName( $value, false );
|
2015-08-25 18:05:35 +00:00
|
|
|
$rangeError = null;
|
2015-07-17 22:04:02 +00:00
|
|
|
|
|
|
|
|
if ( !$user ) {
|
2016-11-02 17:13:43 +00:00
|
|
|
return $this->msg( 'htmlform-user-not-valid', $value );
|
2015-07-24 16:59:53 +00:00
|
|
|
} elseif (
|
2015-08-25 18:05:35 +00:00
|
|
|
// check, if the user exists, if requested
|
2015-07-24 16:59:53 +00:00
|
|
|
( $this->mParams['exists'] && $user->getId() === 0 ) &&
|
2018-07-17 04:54:03 +00:00
|
|
|
// check, if the username is a valid IP address, otherwise save the error message
|
2015-08-25 18:05:35 +00:00
|
|
|
!( $this->mParams['ipallowed'] && IP::isValid( $value ) ) &&
|
|
|
|
|
// check, if the username is a valid IP range, otherwise save the error message
|
|
|
|
|
!( $this->mParams['iprange'] && ( $rangeError = $this->isValidIPRange( $value ) ) === true )
|
2015-07-24 16:59:53 +00:00
|
|
|
) {
|
2015-08-25 18:05:35 +00:00
|
|
|
if ( is_string( $rangeError ) ) {
|
|
|
|
|
return $rangeError;
|
|
|
|
|
}
|
2016-11-02 17:13:43 +00:00
|
|
|
return $this->msg( 'htmlform-user-not-exists', $user->getName() );
|
2015-07-17 22:04:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return parent::validate( $value, $alldata );
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-25 18:05:35 +00:00
|
|
|
protected function isValidIPRange( $value ) {
|
|
|
|
|
$cidrIPRanges = $this->mParams['iprangelimits'];
|
|
|
|
|
|
|
|
|
|
if ( !IP::isValidBlock( $value ) ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
list( $ip, $range ) = explode( '/', $value, 2 );
|
|
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
( IP::isIPv4( $ip ) && $cidrIPRanges['IPv4'] == 32 ) ||
|
|
|
|
|
( IP::isIPv6( $ip ) && $cidrIPRanges['IPv6'] == 128 )
|
|
|
|
|
) {
|
|
|
|
|
// Range block effectively disabled
|
|
|
|
|
return $this->msg( 'ip_range_toolow' )->parse();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
( IP::isIPv4( $ip ) && $range > 32 ) ||
|
|
|
|
|
( IP::isIPv6( $ip ) && $range > 128 )
|
|
|
|
|
) {
|
|
|
|
|
// Dodgy range
|
|
|
|
|
return $this->msg( 'ip_range_invalid' )->parse();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( IP::isIPv4( $ip ) && $range < $cidrIPRanges['IPv4'] ) {
|
|
|
|
|
return $this->msg( 'ip_range_exceeded', $cidrIPRanges['IPv4'] )->parse();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( IP::isIPv6( $ip ) && $range < $cidrIPRanges['IPv6'] ) {
|
|
|
|
|
return $this->msg( 'ip_range_exceeded', $cidrIPRanges['IPv6'] )->parse();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-17 22:04:02 +00:00
|
|
|
protected function getInputWidget( $params ) {
|
|
|
|
|
return new UserInputWidget( $params );
|
|
|
|
|
}
|
2015-10-29 18:00:59 +00:00
|
|
|
|
2016-07-26 12:12:21 +00:00
|
|
|
protected function shouldInfuseOOUI() {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-31 14:56:23 +00:00
|
|
|
protected function getOOUIModules() {
|
|
|
|
|
return [ 'mediawiki.widgets.UserInputWidget' ];
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-29 18:00:59 +00:00
|
|
|
public function getInputHtml( $value ) {
|
|
|
|
|
// add the required module and css class for user suggestions in non-OOUI mode
|
|
|
|
|
$this->mParent->getOutput()->addModules( 'mediawiki.userSuggest' );
|
|
|
|
|
$this->mClass .= ' mw-autocomplete-user';
|
|
|
|
|
|
|
|
|
|
// return parent html
|
2016-03-18 13:55:54 +00:00
|
|
|
return parent::getInputHTML( $value );
|
2015-10-29 18:00:59 +00:00
|
|
|
}
|
2015-07-17 22:04:02 +00:00
|
|
|
}
|