MediaWiki  master
HTMLUserTextField.php
Go to the documentation of this file.
1 <?php
2 
4 use Wikimedia\IPUtils;
5 
20  public function __construct( $params ) {
21  $params = wfArrayPlus2d( $params, [
22  'exists' => false,
23  'ipallowed' => false,
24  'iprange' => false,
25  'iprangelimits' => [
26  'IPv4' => '16',
27  'IPv6' => '32',
28  ],
29  ]
30  );
31 
32  parent::__construct( $params );
33  }
34 
35  public function validate( $value, $alldata ) {
36  // If the value is null, reset it to an empty string which is what is expected by the parent.
37  if ( $value === null ) {
38  $value = '';
39  }
40 
41  // If the value is empty, there are no additional checks that can be performed.
42  if ( $value === '' ) {
43  return parent::validate( $value, $alldata );
44  }
45 
46  // check, if a user exists with the given username
47  $user = User::newFromName( $value, false );
48  $rangeError = null;
49 
50  if ( !$user ) {
51  return $this->msg( 'htmlform-user-not-valid', $value );
52  } elseif (
53  // check, if the user exists, if requested
54  ( $this->mParams['exists'] && $user->getId() === 0 ) &&
55  // check, if the username is a valid IP address, otherwise save the error message
56  !( $this->mParams['ipallowed'] && IPUtils::isValid( $value ) ) &&
57  // check, if the username is a valid IP range, otherwise save the error message
58  !( $this->mParams['iprange'] && ( $rangeError = $this->isValidIPRange( $value ) ) === true )
59  ) {
60  if ( is_string( $rangeError ) ) {
61  return $rangeError;
62  }
63  return $this->msg( 'htmlform-user-not-exists', $user->getName() );
64  }
65 
66  return parent::validate( $value, $alldata );
67  }
68 
69  protected function isValidIPRange( $value ) {
70  $cidrIPRanges = $this->mParams['iprangelimits'];
71 
72  if ( !IPUtils::isValidRange( $value ) ) {
73  return false;
74  }
75 
76  list( $ip, $range ) = explode( '/', $value, 2 );
77 
78  if (
79  ( IPUtils::isIPv4( $ip ) && $cidrIPRanges['IPv4'] == 32 ) ||
80  ( IPUtils::isIPv6( $ip ) && $cidrIPRanges['IPv6'] == 128 )
81  ) {
82  // Range block effectively disabled
83  return $this->msg( 'ip_range_toolow' )->parse();
84  }
85 
86  if (
87  ( IPUtils::isIPv4( $ip ) && $range > 32 ) ||
88  ( IPUtils::isIPv6( $ip ) && $range > 128 )
89  ) {
90  // Dodgy range
91  return $this->msg( 'ip_range_invalid' )->parse();
92  }
93 
94  if ( IPUtils::isIPv4( $ip ) && $range < $cidrIPRanges['IPv4'] ) {
95  return $this->msg( 'ip_range_exceeded', $cidrIPRanges['IPv4'] )->parse();
96  }
97 
98  if ( IPUtils::isIPv6( $ip ) && $range < $cidrIPRanges['IPv6'] ) {
99  return $this->msg( 'ip_range_exceeded', $cidrIPRanges['IPv6'] )->parse();
100  }
101 
102  return true;
103  }
104 
105  protected function getInputWidget( $params ) {
106  return new UserInputWidget( $params );
107  }
108 
109  protected function shouldInfuseOOUI() {
110  return true;
111  }
112 
113  protected function getOOUIModules() {
114  return [ 'mediawiki.widgets.UserInputWidget' ];
115  }
116 
117  public function getInputHtml( $value ) {
118  // add the required module and css class for user suggestions in non-OOUI mode
119  $this->mParent->getOutput()->addModules( 'mediawiki.userSuggest' );
120  $this->mClass .= ' mw-autocomplete-user';
121 
122  // return parent html
123  return parent::getInputHTML( $value );
124  }
125 }
HTMLUserTextField\getOOUIModules
getOOUIModules()
Get the list of extra ResourceLoader modules which must be loaded client-side before it's possible to...
Definition: HTMLUserTextField.php:113
HTMLUserTextField\shouldInfuseOOUI
shouldInfuseOOUI()
Whether the field should be automatically infused.
Definition: HTMLUserTextField.php:109
wfArrayPlus2d
wfArrayPlus2d(array $baseArray, array $newValues)
Merges two (possibly) 2 dimensional arrays into the target array ($baseArray).
Definition: GlobalFunctions.php:2947
MediaWiki\Widget\UserInputWidget
User input widget.
Definition: UserInputWidget.php:11
User\newFromName
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition: User.php:535
HTMLTextField
<input> field.
Definition: HTMLTextField.php:11
HTMLUserTextField\getInputWidget
getInputWidget( $params)
Definition: HTMLUserTextField.php:105
HTMLUserTextField
Implements a text input field for user names.
Definition: HTMLUserTextField.php:19
HTMLFormField\msg
msg( $key,... $params)
Get a translated interface message.
Definition: HTMLFormField.php:83
HTMLUserTextField\getInputHtml
getInputHtml( $value)
Definition: HTMLUserTextField.php:117
HTMLUserTextField\__construct
__construct( $params)
Definition: HTMLUserTextField.php:20
HTMLUserTextField\isValidIPRange
isValidIPRange( $value)
Definition: HTMLUserTextField.php:69
HTMLUserTextField\validate
validate( $value, $alldata)
Override this function to add specific validation checks on the field input.
Definition: HTMLUserTextField.php:35