MediaWiki 1.41.2
BlockUtils.php
Go to the documentation of this file.
1<?php
2
22namespace MediaWiki\Block;
23
31use Wikimedia\IPUtils;
32
48 private $options;
49
51 private $userIdentityLookup;
52
54 private $userNameUtils;
55
59 public const CONSTRUCTOR_OPTIONS = [
61 ];
62
68 public function __construct(
69 ServiceOptions $options,
70 UserIdentityLookup $userIdentityLookup,
71 UserNameUtils $userNameUtils
72 ) {
73 $options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
74 $this->options = $options;
75 $this->userIdentityLookup = $userIdentityLookup;
76 $this->userNameUtils = $userNameUtils;
77 }
78
92 public function parseBlockTarget( $target ): array {
93 // We may have been through this before
94 if ( $target instanceof UserIdentity ) {
95 if ( IPUtils::isValid( $target->getName() ) ) {
96 return [ $target, AbstractBlock::TYPE_IP ];
97 } else {
98 return [ $target, AbstractBlock::TYPE_USER ];
99 }
100 } elseif ( $target === null ) {
101 return [ null, null ];
102 }
103
104 $target = trim( $target );
105
106 if ( IPUtils::isValid( $target ) ) {
107 return [
108 UserIdentityValue::newAnonymous( IPUtils::sanitizeIP( $target ) ),
110 ];
111
112 } elseif ( IPUtils::isValidRange( $target ) ) {
113 // Can't create a UserIdentity from an IP range
114 return [ IPUtils::sanitizeRange( $target ), AbstractBlock::TYPE_RANGE ];
115 }
116
117 // Consider the possibility that this is not a username at all
118 // but actually an old subpage (T31797)
119 if ( str_contains( $target, '/' ) ) {
120 // An old subpage, drill down to the user behind it
121 $target = explode( '/', $target )[0];
122 }
123
124 if ( preg_match( '/^#\d+$/', $target ) ) {
125 // Autoblock reference in the form "#12345"
126 return [ substr( $target, 1 ), AbstractBlock::TYPE_AUTO ];
127 }
128
129 $userFromDB = $this->userIdentityLookup->getUserIdentityByName( $target );
130 if ( $userFromDB instanceof UserIdentity ) {
131 // Note that since numbers are valid usernames, a $target of "12345" will be
132 // considered a UserIdentity. If you want to pass a block ID, prepend a hash "#12345",
133 // since hash characters are not valid in usernames or titles generally.
134 return [ $userFromDB, AbstractBlock::TYPE_USER ];
135 }
136
137 // Wrap the invalid user in a UserIdentityValue.
138 // This allows validateTarget() to return a "nosuchusershort" message,
139 // which is needed for Special:Block.
140 $canonicalName = $this->userNameUtils->getCanonical( $target );
141 if ( $canonicalName ) {
142 return [
143 new UserIdentityValue( 0, $canonicalName ),
145 ];
146 }
147
148 return [ null, null ];
149 }
150
158 public function validateTarget( $value ): Status {
159 [ $target, $type ] = $this->parseBlockTarget( $value );
160
161 $status = Status::newGood( $target );
162
163 switch ( $type ) {
165 if ( !$target->isRegistered() ) {
166 $status->fatal(
167 'nosuchusershort',
168 wfEscapeWikiText( $target->getName() )
169 );
170 }
171 break;
172
174 [ $ip, $range ] = explode( '/', $target, 2 );
175
176 if ( IPUtils::isIPv4( $ip ) ) {
177 $status->merge( $this->validateIPv4Range( (int)$range ) );
178 } elseif ( IPUtils::isIPv6( $ip ) ) {
179 $status->merge( $this->validateIPv6Range( (int)$range ) );
180 } else {
181 // Something is FUBAR
182 $status->fatal( 'badipaddress' );
183 }
184 break;
185
187 // All is well
188 break;
189
190 default:
191 $status->fatal( 'badipaddress' );
192 break;
193 }
194
195 return $status;
196 }
197
205 private function validateIPv4Range( int $range ): Status {
206 $status = Status::newGood();
207 $blockCIDRLimit = $this->options->get( MainConfigNames::BlockCIDRLimit );
208
209 if ( $blockCIDRLimit['IPv4'] == 32 ) {
210 // Range block effectively disabled
211 $status->fatal( 'range_block_disabled' );
212 } elseif ( $range > 32 ) {
213 // Such a range cannot exist
214 $status->fatal( 'ip_range_invalid' );
215 } elseif ( $range < $blockCIDRLimit['IPv4'] ) {
216 $status->fatal( 'ip_range_toolarge', $blockCIDRLimit['IPv4'] );
217 }
218
219 return $status;
220 }
221
229 private function validateIPv6Range( int $range ): Status {
230 $status = Status::newGood();
231 $blockCIDRLimit = $this->options->get( MainConfigNames::BlockCIDRLimit );
232
233 if ( $blockCIDRLimit['IPv6'] == 128 ) {
234 // Range block effectively disabled
235 $status->fatal( 'range_block_disabled' );
236 } elseif ( $range > 128 ) {
237 // Dodgy range - such a range cannot exist
238 $status->fatal( 'ip_range_invalid' );
239 } elseif ( $range < $blockCIDRLimit['IPv6'] ) {
240 $status->fatal( 'ip_range_toolarge', $blockCIDRLimit['IPv6'] );
241 }
242
243 return $status;
244 }
245}
wfEscapeWikiText( $text)
Escapes the given text so that it may be output using addWikiText() without any linking,...
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:88
Backend class for blocking utils.
parseBlockTarget( $target)
From an existing block, get the target and the type of target.
__construct(ServiceOptions $options, UserIdentityLookup $userIdentityLookup, UserNameUtils $userNameUtils)
validateTarget( $value)
Validate block target.
A class for passing options to services.
assertRequiredOptions(array $expectedKeys)
Assert that the list of options provided in this instance exactly match $expectedKeys,...
A class containing constants representing the names of configuration variables.
const BlockCIDRLimit
Name constant for the BlockCIDRLimit setting, for use with Config::get()
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition Status.php:58
Value object representing a user's identity.
UserNameUtils service.
Interface for objects representing user identity.