| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
namespace MediaWiki\Block; |
| 23 |
|
| 24 |
use MediaWiki\Config\ServiceOptions; |
| 25 |
use MediaWiki\MainConfigNames; |
| 26 |
use MediaWiki\Status\Status; |
| 27 |
use MediaWiki\User\UserIdentity; |
| 28 |
use MediaWiki\User\UserIdentityLookup; |
| 29 |
use MediaWiki\User\UserIdentityValue; |
| 30 |
use MediaWiki\User\UserNameUtils; |
| 31 |
use Wikimedia\IPUtils; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
class BlockUtils { |
| 48 |
private ServiceOptions $options; |
| 49 |
private UserIdentityLookup $userIdentityLookup; |
| 50 |
private UserNameUtils $userNameUtils; |
| 51 |
|
| 52 |
|
| 53 |
private $wikiId; |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
public const CONSTRUCTOR_OPTIONS = [ |
| 59 |
MainConfigNames::BlockCIDRLimit, |
| 60 |
]; |
| 61 |
|
| 62 |
public function __construct( |
| 63 |
ServiceOptions $options, |
| 64 |
UserIdentityLookup $userIdentityLookup, |
| 65 |
UserNameUtils $userNameUtils, |
| 66 |
$wikiId = Block::LOCAL |
| 67 |
) { |
| 68 |
$options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS ); |
| 69 |
$this->options = $options; |
| 70 |
$this->userIdentityLookup = $userIdentityLookup; |
| 71 |
$this->userNameUtils = $userNameUtils; |
| 72 |
$this->wikiId = $wikiId; |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
public function parseBlockTarget( $target ): array { |
| 94 |
|
| 95 |
if ( $target instanceof UserIdentity ) { |
| 96 |
if ( IPUtils::isValid( $target->getName() ) ) { |
| 97 |
return [ $target, AbstractBlock::TYPE_IP ]; |
| 98 |
} else { |
| 99 |
return [ $target, AbstractBlock::TYPE_USER ]; |
| 100 |
} |
| 101 |
} elseif ( $target === null ) { |
| 102 |
return [ null, null ]; |
| 103 |
} |
| 104 |
|
| 105 |
$target = trim( $target ); |
| 106 |
|
| 107 |
if ( IPUtils::isValid( $target ) ) { |
| 108 |
return [ |
| 109 |
UserIdentityValue::newAnonymous( IPUtils::sanitizeIP( $target ), $this->wikiId ), |
| 110 |
AbstractBlock::TYPE_IP |
| 111 |
]; |
| 112 |
|
| 113 |
} elseif ( IPUtils::isValidRange( $target ) ) { |
| 114 |
|
| 115 |
return [ IPUtils::sanitizeRange( $target ), AbstractBlock::TYPE_RANGE ]; |
| 116 |
} |
| 117 |
|
| 118 |
if ( preg_match( '/^#\d+$/', $target ) ) { |
| 119 |
|
| 120 |
return [ substr( $target, 1 ), AbstractBlock::TYPE_AUTO ]; |
| 121 |
} |
| 122 |
|
| 123 |
$userFromDB = $this->userIdentityLookup->getUserIdentityByName( $target ); |
| 124 |
if ( $userFromDB instanceof UserIdentity ) { |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
return [ $userFromDB, AbstractBlock::TYPE_USER ]; |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
$canonicalName = $this->userNameUtils->getCanonical( $target ); |
| 135 |
if ( $canonicalName !== false ) { |
| 136 |
return [ |
| 137 |
new UserIdentityValue( 0, $canonicalName ), |
| 138 |
AbstractBlock::TYPE_USER |
| 139 |
]; |
| 140 |
} |
| 141 |
|
| 142 |
return [ null, null ]; |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
public function parseBlockTargetRow( $row ) { |
| 154 |
if ( $row->bt_auto ) { |
| 155 |
return [ $row->bl_id, AbstractBlock::TYPE_AUTO ]; |
| 156 |
} elseif ( isset( $row->bt_user ) ) { |
| 157 |
if ( isset( $row->bt_user_text ) ) { |
| 158 |
$user = new UserIdentityValue( $row->bt_user, $row->bt_user_text, $this->wikiId ); |
| 159 |
} else { |
| 160 |
$user = $this->userIdentityLookup->getUserIdentityByUserId( $row->bt_user ); |
| 161 |
} |
| 162 |
return [ $user, AbstractBlock::TYPE_USER ]; |
| 163 |
} elseif ( $row->bt_address === null ) { |
| 164 |
return [ null, null ]; |
| 165 |
} elseif ( IPUtils::isValid( $row->bt_address ) ) { |
| 166 |
return [ |
| 167 |
UserIdentityValue::newAnonymous( IPUtils::sanitizeIP( $row->bt_address ), $this->wikiId ), |
| 168 |
AbstractBlock::TYPE_IP |
| 169 |
]; |
| 170 |
} elseif ( IPUtils::isValidRange( $row->bt_address ) ) { |
| 171 |
|
| 172 |
return [ IPUtils::sanitizeRange( $row->bt_address ), AbstractBlock::TYPE_RANGE ]; |
| 173 |
} else { |
| 174 |
return [ null, null ]; |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
public function validateTarget( $value ): Status { |
| 186 |
[ $target, $type ] = $this->parseBlockTarget( $value ); |
| 187 |
|
| 188 |
$status = Status::newGood( $target ); |
| 189 |
|
| 190 |
switch ( $type ) { |
| 191 |
case AbstractBlock::TYPE_USER: |
| 192 |
if ( !$target->isRegistered() ) { |
| 193 |
$status->fatal( |
| 194 |
'nosuchusershort', |
| 195 |
wfEscapeWikiText( $target->getName() ) |
| 196 |
); |
| 197 |
} |
| 198 |
break; |
| 199 |
|
| 200 |
case AbstractBlock::TYPE_RANGE: |
| 201 |
[ $ip, $range ] = explode( '/', $target, 2 ); |
| 202 |
|
| 203 |
if ( IPUtils::isIPv4( $ip ) ) { |
| 204 |
$status->merge( $this->validateIPv4Range( (int)$range ) ); |
| 205 |
} elseif ( IPUtils::isIPv6( $ip ) ) { |
| 206 |
$status->merge( $this->validateIPv6Range( (int)$range ) ); |
| 207 |
} else { |
| 208 |
|
| 209 |
$status->fatal( 'badipaddress' ); |
| 210 |
} |
| 211 |
break; |
| 212 |
|
| 213 |
case AbstractBlock::TYPE_IP: |
| 214 |
|
| 215 |
break; |
| 216 |
|
| 217 |
default: |
| 218 |
$status->fatal( 'badipaddress' ); |
| 219 |
break; |
| 220 |
} |
| 221 |
|
| 222 |
return $status; |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
private function validateIPv4Range( int $range ): Status { |
| 233 |
$status = Status::newGood(); |
| 234 |
$blockCIDRLimit = $this->options->get( MainConfigNames::BlockCIDRLimit ); |
| 235 |
|
| 236 |
if ( $blockCIDRLimit['IPv4'] == 32 ) { |
| 237 |
|
| 238 |
$status->fatal( 'range_block_disabled' ); |
| 239 |
} elseif ( $range > 32 ) { |
| 240 |
|
| 241 |
$status->fatal( 'ip_range_invalid' ); |
| 242 |
} elseif ( $range < $blockCIDRLimit['IPv4'] ) { |
| 243 |
$status->fatal( 'ip_range_toolarge', $blockCIDRLimit['IPv4'] ); |
| 244 |
} |
| 245 |
|
| 246 |
return $status; |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
private function validateIPv6Range( int $range ): Status { |
| 257 |
$status = Status::newGood(); |
| 258 |
$blockCIDRLimit = $this->options->get( MainConfigNames::BlockCIDRLimit ); |
| 259 |
|
| 260 |
if ( $blockCIDRLimit['IPv6'] == 128 ) { |
| 261 |
|
| 262 |
$status->fatal( 'range_block_disabled' ); |
| 263 |
} elseif ( $range > 128 ) { |
| 264 |
|
| 265 |
$status->fatal( 'ip_range_invalid' ); |
| 266 |
} elseif ( $range < $blockCIDRLimit['IPv6'] ) { |
| 267 |
$status->fatal( 'ip_range_toolarge', $blockCIDRLimit['IPv6'] ); |
| 268 |
} |
| 269 |
|
| 270 |
return $status; |
| 271 |
} |
| 272 |
} |