Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
77.78% covered (warning)
77.78%
56 / 72
14.29% covered (danger)
14.29%
1 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
HTMLUserTextField
78.87% covered (warning)
78.87%
56 / 71
14.29% covered (danger)
14.29%
1 / 7
58.63
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
1
 validate
93.55% covered (success)
93.55%
29 / 31
0.00% covered (danger)
0.00%
0 / 1
21.12
 isValidIPRange
80.00% covered (warning)
80.00%
12 / 15
0.00% covered (danger)
0.00%
0 / 1
15.57
 getInputWidget
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
12
 shouldInfuseOOUI
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getOOUIModules
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getInputHtml
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace MediaWiki\HTMLForm\Field;
4
5use MediaWiki\MediaWikiServices;
6use MediaWiki\Message\Message;
7use MediaWiki\User\ExternalUserNames;
8use MediaWiki\Widget\UserInputWidget;
9use Wikimedia\ArrayUtils\ArrayUtils;
10use Wikimedia\IPUtils;
11
12/**
13 * Implements a text input field for user names.
14 * Automatically auto-completes if using the OOUI display format.
15 *
16 * Optional parameters:
17 * 'exists' - Whether to validate that the user already exists
18 * 'external' - Whether an external user (imported actor) is interpreted as "valid"
19 * 'ipallowed' - Whether an IP address is interpreted as "valid"
20 * 'usemodwiki-ipallowed' - Whether an IP address in the usemod wiki format (e.g. 300.300.300.xxx) is accepted. The
21 *    'ipallowed' parameter must be set to true if this parameter is set to true.
22 * 'iprange' - Whether an IP address range is interpreted as "valid"
23 * 'iprangelimits' - Specifies the valid IP ranges for IPv4 and IPv6 in an array.
24 * 'excludenamed' - Whether to exclude named users or not.
25 * 'excludetemp' - Whether to exclude temporary users or not.
26 *
27 * @stable to extend
28 * @since 1.26
29 */
30class HTMLUserTextField extends HTMLTextField {
31    /**
32     * @stable to call
33     * @inheritDoc
34     */
35    public function __construct( $params ) {
36        $params = ArrayUtils::arrayPlus2d( $params, [
37                'exists' => false,
38                'external' => false,
39                'ipallowed' => false,
40                'usemodwiki-ipallowed' => false,
41                'iprange' => false,
42                'iprangelimits' => [
43                    'IPv4' => 0,
44                    'IPv6' => 0,
45                ],
46                'excludenamed' => false,
47                'excludetemp' => false,
48            ]
49        );
50
51        parent::__construct( $params );
52    }
53
54    /** @inheritDoc */
55    public function validate( $value, $alldata ) {
56        // If the value is null, reset it to an empty string which is what is expected by the parent.
57        $value ??= '';
58
59        // If the value is empty, there are no additional checks that can be performed.
60        if ( $value === '' ) {
61            return parent::validate( $value, $alldata );
62        }
63
64        // check if the input is a valid username
65        $user = MediaWikiServices::getInstance()->getUserFactory()->newFromName( $value );
66        if ( $user ) {
67            // check if the user exists, if requested
68            if ( $this->mParams['exists'] && !(
69                $user->isRegistered() &&
70                // Treat hidden users as unregistered if current user can't view them (T309894)
71                !( $user->isHidden() && !$this->mParent->getUser()->isAllowed( 'hideuser' ) )
72            ) ) {
73                return $this->msg( 'htmlform-user-not-exists', wfEscapeWikiText( $user->getName() ) );
74            }
75
76            // check if the user account type matches the account type filter
77            $excludeNamed = $this->mParams['excludenamed'] ?? null;
78            $excludeTemp = $this->mParams['excludetemp'] ?? null;
79            if ( ( $excludeTemp && $user->isTemp() ) || ( $excludeNamed && $user->isNamed() ) ) {
80                return $this->msg( 'htmlform-user-not-valid', wfEscapeWikiText( $user->getName() ) );
81            }
82        } else {
83            // not a valid username
84            $valid = false;
85            // check if the input is a valid external user
86            if ( $this->mParams['external'] && ExternalUserNames::isExternal( $value ) ) {
87                $valid = true;
88            }
89            // check if the input is a valid IP address, optionally also checking for usemod wiki IPs
90            if ( $this->mParams['ipallowed'] ) {
91                $b = IPUtils::RE_IP_BYTE;
92                if ( IPUtils::isValid( $value ) ) {
93                    $valid = true;
94                } elseif ( $this->mParams['usemodwiki-ipallowed'] && preg_match( "/^$b\.$b\.$b\.xxx$/", $value ) ) {
95                    $valid = true;
96                }
97            }
98            // check if the input is a valid IP range
99            if ( $this->mParams['iprange'] ) {
100                $rangeError = $this->isValidIPRange( $value );
101                if ( $rangeError === true ) {
102                    $valid = true;
103                } elseif ( $rangeError !== false ) {
104                    return $rangeError;
105                }
106            }
107            if ( !$valid ) {
108                return $this->msg( 'htmlform-user-not-valid', wfEscapeWikiText( $value ) );
109            }
110        }
111
112        return parent::validate( $value, $alldata );
113    }
114
115    /**
116     * @param string $value
117     * @return bool|Message
118     */
119    protected function isValidIPRange( $value ) {
120        $cidrIPRanges = $this->mParams['iprangelimits'];
121
122        if ( !IPUtils::isValidRange( $value ) ) {
123            return false;
124        }
125
126        [ $ip, $range ] = explode( '/', $value, 2 );
127
128        if (
129            ( IPUtils::isIPv4( $ip ) && $cidrIPRanges['IPv4'] == 32 ) ||
130            ( IPUtils::isIPv6( $ip ) && $cidrIPRanges['IPv6'] == 128 )
131        ) {
132            // Range block effectively disabled
133            return $this->msg( 'ip_range_toolow' );
134        }
135
136        if (
137            ( IPUtils::isIPv4( $ip ) && $range > 32 ) ||
138            ( IPUtils::isIPv6( $ip ) && $range > 128 )
139        ) {
140            // Dodgy range
141            return $this->msg( 'ip_range_invalid' );
142        }
143
144        if ( IPUtils::isIPv4( $ip ) && $range < $cidrIPRanges['IPv4'] ) {
145            return $this->msg( 'ip_range_exceeded', $cidrIPRanges['IPv4'] );
146        }
147
148        if ( IPUtils::isIPv6( $ip ) && $range < $cidrIPRanges['IPv6'] ) {
149            return $this->msg( 'ip_range_exceeded', $cidrIPRanges['IPv6'] );
150        }
151
152        return true;
153    }
154
155    /** @inheritDoc */
156    protected function getInputWidget( $params ) {
157        if ( isset( $this->mParams['excludenamed'] ) ) {
158            $params['excludenamed'] = $this->mParams['excludenamed'];
159        }
160
161        if ( isset( $this->mParams['excludetemp'] ) ) {
162            $params['excludetemp'] = $this->mParams['excludetemp'];
163        }
164
165        return new UserInputWidget( $params );
166    }
167
168    /** @inheritDoc */
169    protected function shouldInfuseOOUI() {
170        return true;
171    }
172
173    /** @inheritDoc */
174    protected function getOOUIModules() {
175        return [ 'mediawiki.widgets.UserInputWidget' ];
176    }
177
178    /** @inheritDoc */
179    public function getInputHtml( $value ) {
180        // add the required module and css class for user suggestions in non-OOUI mode
181        $this->mParent->getOutput()->addModules( 'mediawiki.userSuggest' );
182        $this->mClass .= ' mw-autocomplete-user';
183
184        // return parent html
185        return parent::getInputHTML( $value );
186    }
187}
188
189/** @deprecated class alias since 1.42 */
190class_alias( HTMLUserTextField::class, 'HTMLUserTextField' );