MediaWiki  REL1_31
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  // check, if a user exists with the given username
38  $user = User::newFromName( $value, false );
39  $rangeError = null;
40 
41  if ( !$user ) {
42  return $this->msg( 'htmlform-user-not-valid', $value );
43  } elseif (
44  // check, if the user exists, if requested
45  ( $this->mParams['exists'] && $user->getId() === 0 ) &&
46  // check, if the username is a valid IP address, otherweise save the error message
47  !( $this->mParams['ipallowed'] && IP::isValid( $value ) ) &&
48  // check, if the username is a valid IP range, otherwise save the error message
49  !( $this->mParams['iprange'] && ( $rangeError = $this->isValidIPRange( $value ) ) === true )
50  ) {
51  if ( is_string( $rangeError ) ) {
52  return $rangeError;
53  }
54  return $this->msg( 'htmlform-user-not-exists', $user->getName() );
55  }
56 
57  return parent::validate( $value, $alldata );
58  }
59 
60  protected function isValidIPRange( $value ) {
61  $cidrIPRanges = $this->mParams['iprangelimits'];
62 
63  if ( !IP::isValidBlock( $value ) ) {
64  return false;
65  }
66 
67  list( $ip, $range ) = explode( '/', $value, 2 );
68 
69  if (
70  ( IP::isIPv4( $ip ) && $cidrIPRanges['IPv4'] == 32 ) ||
71  ( IP::isIPv6( $ip ) && $cidrIPRanges['IPv6'] == 128 )
72  ) {
73  // Range block effectively disabled
74  return $this->msg( 'ip_range_toolow' )->parse();
75  }
76 
77  if (
78  ( IP::isIPv4( $ip ) && $range > 32 ) ||
79  ( IP::isIPv6( $ip ) && $range > 128 )
80  ) {
81  // Dodgy range
82  return $this->msg( 'ip_range_invalid' )->parse();
83  }
84 
85  if ( IP::isIPv4( $ip ) && $range < $cidrIPRanges['IPv4'] ) {
86  return $this->msg( 'ip_range_exceeded', $cidrIPRanges['IPv4'] )->parse();
87  }
88 
89  if ( IP::isIPv6( $ip ) && $range < $cidrIPRanges['IPv6'] ) {
90  return $this->msg( 'ip_range_exceeded', $cidrIPRanges['IPv6'] )->parse();
91  }
92 
93  return true;
94  }
95 
96  protected function getInputWidget( $params ) {
97  return new UserInputWidget( $params );
98  }
99 
100  protected function shouldInfuseOOUI() {
101  return true;
102  }
103 
104  protected function getOOUIModules() {
105  return [ 'mediawiki.widgets.UserInputWidget' ];
106  }
107 
108  public function getInputHtml( $value ) {
109  // add the required module and css class for user suggestions in non-OOUI mode
110  $this->mParent->getOutput()->addModules( 'mediawiki.userSuggest' );
111  $this->mClass .= ' mw-autocomplete-user';
112 
113  // return parent html
114  return parent::getInputHTML( $value );
115  }
116 }
HTMLUserTextField\getOOUIModules
getOOUIModules()
Get the list of extra ResourceLoader modules which must be loaded client-side before it's possible to...
Definition: HTMLUserTextField.php:104
HTMLUserTextField\shouldInfuseOOUI
shouldInfuseOOUI()
Whether the field should be automatically infused.
Definition: HTMLUserTextField.php:100
$user
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a account $user
Definition: hooks.txt:247
use
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
Definition: APACHE-LICENSE-2.0.txt:10
wfArrayPlus2d
wfArrayPlus2d(array $baseArray, array $newValues)
Merges two (possibly) 2 dimensional arrays into the target array ($baseArray).
Definition: GlobalFunctions.php:3427
MediaWiki\Widget\UserInputWidget
User input widget.
Definition: UserInputWidget.php:11
$params
$params
Definition: styleTest.css.php:40
User\newFromName
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition: User.php:591
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:37
HTMLTextField
<input> field.
Definition: HTMLTextField.php:11
HTMLUserTextField\getInputWidget
getInputWidget( $params)
Definition: HTMLUserTextField.php:96
HTMLUserTextField
Implements a text input field for user names.
Definition: HTMLUserTextField.php:20
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:45
HTMLFormField\msg
msg()
Get a translated interface message.
Definition: HTMLFormField.php:77
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
HTMLUserTextField\getInputHtml
getInputHtml( $value)
Definition: HTMLUserTextField.php:108
HTMLUserTextField\__construct
__construct( $params)
Definition: HTMLUserTextField.php:21
HTMLUserTextField\isValidIPRange
isValidIPRange( $value)
Definition: HTMLUserTextField.php:60
HTMLUserTextField\validate
validate( $value, $alldata)
Override this function to add specific validation checks on the field input.
Definition: HTMLUserTextField.php:36