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