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