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