MediaWiki  1.33.0
HTMLUserTextField.php
Go to the documentation of this file.
1 <?php
2 
4 
21  public function __construct( $params ) {
23  'exists' => false,
24  'ipallowed' => false,
25  'iprange' => false,
26  'iprangelimits' => [
27  'IPv4' => '16',
28  'IPv6' => '32',
29  ],
30  ]
31  );
32 
33  parent::__construct( $params );
34  }
35 
36  public function validate( $value, $alldata ) {
37  // Default value (from getDefault()) is null, User::newFromName() expects a string
38  if ( $value === null ) {
39  $value = '';
40  }
41 
42  // check, if a user exists with the given username
43  $user = User::newFromName( $value, false );
44  $rangeError = null;
45 
46  if ( !$user ) {
47  return $this->msg( 'htmlform-user-not-valid', $value );
48  } elseif (
49  // check, if the user exists, if requested
50  ( $this->mParams['exists'] && $user->getId() === 0 ) &&
51  // check, if the username is a valid IP address, otherwise save the error message
52  !( $this->mParams['ipallowed'] && IP::isValid( $value ) ) &&
53  // check, if the username is a valid IP range, otherwise save the error message
54  !( $this->mParams['iprange'] && ( $rangeError = $this->isValidIPRange( $value ) ) === true )
55  ) {
56  if ( is_string( $rangeError ) ) {
57  return $rangeError;
58  }
59  return $this->msg( 'htmlform-user-not-exists', $user->getName() );
60  }
61 
62  return parent::validate( $value, $alldata );
63  }
64 
65  protected function isValidIPRange( $value ) {
66  $cidrIPRanges = $this->mParams['iprangelimits'];
67 
68  if ( !IP::isValidBlock( $value ) ) {
69  return false;
70  }
71 
72  list( $ip, $range ) = explode( '/', $value, 2 );
73 
74  if (
75  ( IP::isIPv4( $ip ) && $cidrIPRanges['IPv4'] == 32 ) ||
76  ( IP::isIPv6( $ip ) && $cidrIPRanges['IPv6'] == 128 )
77  ) {
78  // Range block effectively disabled
79  return $this->msg( 'ip_range_toolow' )->parse();
80  }
81 
82  if (
83  ( IP::isIPv4( $ip ) && $range > 32 ) ||
84  ( IP::isIPv6( $ip ) && $range > 128 )
85  ) {
86  // Dodgy range
87  return $this->msg( 'ip_range_invalid' )->parse();
88  }
89 
90  if ( IP::isIPv4( $ip ) && $range < $cidrIPRanges['IPv4'] ) {
91  return $this->msg( 'ip_range_exceeded', $cidrIPRanges['IPv4'] )->parse();
92  }
93 
94  if ( IP::isIPv6( $ip ) && $range < $cidrIPRanges['IPv6'] ) {
95  return $this->msg( 'ip_range_exceeded', $cidrIPRanges['IPv6'] )->parse();
96  }
97 
98  return true;
99  }
100 
101  protected function getInputWidget( $params ) {
102  return new UserInputWidget( $params );
103  }
104 
105  protected function shouldInfuseOOUI() {
106  return true;
107  }
108 
109  protected function getOOUIModules() {
110  return [ 'mediawiki.widgets.UserInputWidget' ];
111  }
112 
113  public function getInputHtml( $value ) {
114  // add the required module and css class for user suggestions in non-OOUI mode
115  $this->mParent->getOutput()->addModules( 'mediawiki.userSuggest' );
116  $this->mClass .= ' mw-autocomplete-user';
117 
118  // return parent html
119  return parent::getInputHTML( $value );
120  }
121 }
HTMLUserTextField\getOOUIModules
getOOUIModules()
Get the list of extra ResourceLoader modules which must be loaded client-side before it's possible to...
Definition: HTMLUserTextField.php:109
HTMLUserTextField\shouldInfuseOOUI
shouldInfuseOOUI()
Whether the field should be automatically infused.
Definition: HTMLUserTextField.php:105
$user
return true to allow those checks to and false if checking is done & $user
Definition: hooks.txt:1476
wfArrayPlus2d
wfArrayPlus2d(array $baseArray, array $newValues)
Merges two (possibly) 2 dimensional arrays into the target array ($baseArray).
Definition: GlobalFunctions.php:3225
MediaWiki\Widget\UserInputWidget
User input widget.
Definition: UserInputWidget.php:11
$params
$params
Definition: styleTest.css.php:44
User\newFromName
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition: User.php:585
IP\isIPv6
static isIPv6( $ip)
Given a string, determine if it as valid IP in IPv6 only.
Definition: IP.php:88
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
HTMLTextField
<input> field.
Definition: HTMLTextField.php:11
HTMLUserTextField\getInputWidget
getInputWidget( $params)
Definition: HTMLUserTextField.php:101
HTMLUserTextField
Implements a text input field for user names.
Definition: HTMLUserTextField.php:20
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
IP\isValidBlock
static isValidBlock( $ipRange)
Validate an IP range (valid address with a valid CIDR prefix).
Definition: IP.php:125
list
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
Definition: deferred.txt:11
$value
$value
Definition: styleTest.css.php:49
HTMLFormField\msg
msg()
Get a translated interface message.
Definition: HTMLFormField.php:80
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
true
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return true
Definition: hooks.txt:1985
HTMLUserTextField\getInputHtml
getInputHtml( $value)
Definition: HTMLUserTextField.php:113
HTMLUserTextField\__construct
__construct( $params)
Definition: HTMLUserTextField.php:21
HTMLUserTextField\isValidIPRange
isValidIPRange( $value)
Definition: HTMLUserTextField.php:65
HTMLUserTextField\validate
validate( $value, $alldata)
Override this function to add specific validation checks on the field input.
Definition: HTMLUserTextField.php:36