*
* Optional parameters:
* 'exists' - Whether to validate that the user already exists
+ * 'ipallowed' - Whether an IP adress is interpreted as "valid"
+ * 'iprange' - Whether an IP adress range is interpreted as "valid"
+ * 'iprangelimits' - Specifies the valid IP ranges for IPv4 and IPv6 in an array.
+ * defaults to IPv4 => 16; IPv6 => 32.
*
* @since 1.26
*/
class HTMLUserTextField extends HTMLTextField {
public function __construct( $params ) {
- $params += [
- 'exists' => false,
- 'ipallowed' => false,
- ];
+ $params = wfArrayPlus2d( $params, [
+ 'exists' => false,
+ 'ipallowed' => false,
+ 'iprange' => false,
+ 'iprangelimits' => [
+ 'IPv4' => '16',
+ 'IPv6' => '32',
+ ],
+ ]
+ );
parent::__construct( $params );
}
public function validate( $value, $alldata ) {
// check, if a user exists with the given username
$user = User::newFromName( $value, false );
+ $rangeError = null;
if ( !$user ) {
return $this->msg( 'htmlform-user-not-valid', $value );
} elseif (
+ // check, if the user exists, if requested
( $this->mParams['exists'] && $user->getId() === 0 ) &&
- !( $this->mParams['ipallowed'] && User::isIP( $value ) )
+ // check, if the username is a valid IP address, otherweise save the error message
+ !( $this->mParams['ipallowed'] && IP::isValid( $value ) ) &&
+ // check, if the username is a valid IP range, otherwise save the error message
+ !( $this->mParams['iprange'] && ( $rangeError = $this->isValidIPRange( $value ) ) === true )
) {
+ if ( is_string( $rangeError ) ) {
+ return $rangeError;
+ }
return $this->msg( 'htmlform-user-not-exists', $user->getName() );
}
return parent::validate( $value, $alldata );
}
+ protected function isValidIPRange( $value ) {
+ $cidrIPRanges = $this->mParams['iprangelimits'];
+
+ if ( !IP::isValidBlock( $value ) ) {
+ return false;
+ }
+
+ list( $ip, $range ) = explode( '/', $value, 2 );
+
+ if (
+ ( IP::isIPv4( $ip ) && $cidrIPRanges['IPv4'] == 32 ) ||
+ ( IP::isIPv6( $ip ) && $cidrIPRanges['IPv6'] == 128 )
+ ) {
+ // Range block effectively disabled
+ return $this->msg( 'ip_range_toolow' )->parse();
+ }
+
+ if (
+ ( IP::isIPv4( $ip ) && $range > 32 ) ||
+ ( IP::isIPv6( $ip ) && $range > 128 )
+ ) {
+ // Dodgy range
+ return $this->msg( 'ip_range_invalid' )->parse();
+ }
+
+ if ( IP::isIPv4( $ip ) && $range < $cidrIPRanges['IPv4'] ) {
+ return $this->msg( 'ip_range_exceeded', $cidrIPRanges['IPv4'] )->parse();
+ }
+
+ if ( IP::isIPv6( $ip ) && $range < $cidrIPRanges['IPv6'] ) {
+ return $this->msg( 'ip_range_exceeded', $cidrIPRanges['IPv6'] )->parse();
+ }
+
+ return true;
+ }
+
protected function getInputWidget( $params ) {
return new UserInputWidget( $params );
}
"ipb_blocked_as_range": "Error: The IP address $1 is not blocked directly and cannot be unblocked.\nIt is, however, blocked as part of the range $2, which can be unblocked.",
"ip_range_invalid": "Invalid IP address range.",
"ip_range_toolarge": "Range blocks larger than /$1 are not allowed.",
+ "ip_range_exceeded": "The IP range exceeds its maximum range. Allowed range: /$1.",
+ "ip_range_toolow": "IP ranges are effectively not allowed.",
"proxyblocker": "Proxy blocker",
"proxyblockreason": "Your IP address has been blocked because it is an open proxy.\nPlease contact your Internet service provider or technical support of your organization and inform them of this serious security problem.",
"sorbs": "DNSBL",
"ipb_blocked_as_range": "Used when unblock of a single IP fails. Parameters:\n* $1 - IP address\n* $2 - IP address range",
"ip_range_invalid": "Used as error message in [[Special:Block]].\n\nSee also:\n* {{msg-mw|Range block disabled}}\n* {{msg-mw|Ip range invalid}}\n* {{msg-mw|Ip range toolarge}}",
"ip_range_toolarge": "Used as error message in [[Special:Block]]. Parameters:\n* $1 - a number from 0 to 32 for IPv4 (from 0 to 128 for IPv6); a part of CIDR (Classless Inter-Domain Routing) notation.\nSee also:\n* {{msg-mw|Range block disabled}}\n* {{msg-mw|Ip range invalid}}\n* {{msg-mw|Ip range toolarge}}",
+ "ip_range_exceeded": "Used as error message in HTMLUserTextField when an IP range exceeds its maximum amount. See {{mw-msg|ip_range_toolarge}} for parameter.",
+ "ip_range_toolow": "Used as error message in HTMLUserTextField, if effectively no IP ranges are interpreted as valid (IPv4 CIDR range /32 or IPv6 /128).",
"proxyblocker": "Used in [[Special:BlockMe]].\n\nSee also:\n* {{msg-mw|proxyblocker-disabled}}\n* {{msg-mw|proxyblockreason}}\n* {{msg-mw|proxyblocksuccess}}",
"proxyblockreason": "Used as explanation of the reason in [[Special:BlockMe]].\n\nSee also:\n* {{msg-mw|proxyblocker-disabled}}\n* {{msg-mw|proxyblocker}}\n* {{msg-mw|proxyblocksuccess}}",
"sorbs": "{{optional}}",