| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
namespace MediaWiki\Mail; |
| 22 |
|
| 23 |
use MailAddress; |
| 24 |
use MediaWiki\Config\ServiceOptions; |
| 25 |
use MediaWiki\HookContainer\HookContainer; |
| 26 |
use MediaWiki\HookContainer\HookRunner; |
| 27 |
use MediaWiki\MainConfigNames; |
| 28 |
use MediaWiki\Permissions\Authority; |
| 29 |
use MediaWiki\Permissions\PermissionStatus; |
| 30 |
use MediaWiki\Preferences\MultiUsernameFilter; |
| 31 |
use MediaWiki\SpecialPage\SpecialPage; |
| 32 |
use MediaWiki\User\CentralId\CentralIdLookup; |
| 33 |
use MediaWiki\User\Options\UserOptionsLookup; |
| 34 |
use MediaWiki\User\UserFactory; |
| 35 |
use StatusValue; |
| 36 |
use UnexpectedValueException; |
| 37 |
use Wikimedia\Message\IMessageFormatterFactory; |
| 38 |
use Wikimedia\Message\ITextFormatter; |
| 39 |
use Wikimedia\Message\MessageSpecifier; |
| 40 |
use Wikimedia\Message\MessageValue; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
class EmailUser { |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
public const CONSTRUCTOR_OPTIONS = [ |
| 57 |
MainConfigNames::EnableEmail, |
| 58 |
MainConfigNames::EnableUserEmail, |
| 59 |
MainConfigNames::EnableSpecialMute, |
| 60 |
MainConfigNames::PasswordSender, |
| 61 |
MainConfigNames::UserEmailUseReplyTo, |
| 62 |
]; |
| 63 |
|
| 64 |
private ServiceOptions $options; |
| 65 |
private HookRunner $hookRunner; |
| 66 |
private UserOptionsLookup $userOptionsLookup; |
| 67 |
private CentralIdLookup $centralIdLookup; |
| 68 |
private UserFactory $userFactory; |
| 69 |
private IEmailer $emailer; |
| 70 |
private IMessageFormatterFactory $messageFormatterFactory; |
| 71 |
private ITextFormatter $contLangMsgFormatter; |
| 72 |
private Authority $sender; |
| 73 |
|
| 74 |
|
| 75 |
private string $editToken = ''; |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
public function __construct( |
| 91 |
ServiceOptions $options, |
| 92 |
HookContainer $hookContainer, |
| 93 |
UserOptionsLookup $userOptionsLookup, |
| 94 |
CentralIdLookup $centralIdLookup, |
| 95 |
UserFactory $userFactory, |
| 96 |
IEmailer $emailer, |
| 97 |
IMessageFormatterFactory $messageFormatterFactory, |
| 98 |
ITextFormatter $contLangMsgFormatter, |
| 99 |
Authority $sender |
| 100 |
) { |
| 101 |
$options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS ); |
| 102 |
$this->options = $options; |
| 103 |
$this->hookRunner = new HookRunner( $hookContainer ); |
| 104 |
$this->userOptionsLookup = $userOptionsLookup; |
| 105 |
$this->centralIdLookup = $centralIdLookup; |
| 106 |
$this->userFactory = $userFactory; |
| 107 |
$this->emailer = $emailer; |
| 108 |
$this->messageFormatterFactory = $messageFormatterFactory; |
| 109 |
$this->contLangMsgFormatter = $contLangMsgFormatter; |
| 110 |
|
| 111 |
$this->sender = $sender; |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
public function validateTarget( UserEmailContact $target ): StatusValue { |
| 122 |
$targetIdentity = $target->getUser(); |
| 123 |
|
| 124 |
if ( !$targetIdentity->getId() ) { |
| 125 |
return StatusValue::newFatal( 'emailnotarget' ); |
| 126 |
} |
| 127 |
|
| 128 |
if ( !$target->isEmailConfirmed() ) { |
| 129 |
return StatusValue::newFatal( 'noemailtext' ); |
| 130 |
} |
| 131 |
|
| 132 |
$targetUser = $this->userFactory->newFromUserIdentity( $targetIdentity ); |
| 133 |
if ( !$targetUser->canReceiveEmail() ) { |
| 134 |
return StatusValue::newFatal( 'nowikiemailtext' ); |
| 135 |
} |
| 136 |
|
| 137 |
$senderUser = $this->userFactory->newFromAuthority( $this->sender ); |
| 138 |
if ( |
| 139 |
!$this->userOptionsLookup->getOption( $targetIdentity, 'email-allow-new-users' ) && |
| 140 |
$senderUser->isNewbie() |
| 141 |
) { |
| 142 |
return StatusValue::newFatal( 'nowikiemailtext' ); |
| 143 |
} |
| 144 |
|
| 145 |
$muteList = $this->userOptionsLookup->getOption( |
| 146 |
$targetIdentity, |
| 147 |
'email-blacklist', |
| 148 |
'' |
| 149 |
); |
| 150 |
if ( $muteList ) { |
| 151 |
$muteList = MultiUsernameFilter::splitIds( $muteList ); |
| 152 |
$senderId = $this->centralIdLookup->centralIdFromLocalUser( $this->sender->getUser() ); |
| 153 |
if ( $senderId !== 0 && in_array( $senderId, $muteList ) ) { |
| 154 |
return StatusValue::newFatal( 'nowikiemailtext' ); |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
return StatusValue::newGood(); |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
public function canSend(): StatusValue { |
| 168 |
if ( |
| 169 |
!$this->options->get( MainConfigNames::EnableEmail ) || |
| 170 |
!$this->options->get( MainConfigNames::EnableUserEmail ) |
| 171 |
) { |
| 172 |
return StatusValue::newFatal( 'usermaildisabled' ); |
| 173 |
} |
| 174 |
|
| 175 |
$user = $this->userFactory->newFromAuthority( $this->sender ); |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
if ( !$user->isEmailConfirmed() ) { |
| 180 |
return StatusValue::newFatal( 'mailnologin' ); |
| 181 |
} |
| 182 |
|
| 183 |
$status = PermissionStatus::newGood(); |
| 184 |
if ( !$this->sender->isDefinitelyAllowed( 'sendemail', $status ) ) { |
| 185 |
return $status; |
| 186 |
} |
| 187 |
|
| 188 |
$hookErr = false; |
| 189 |
|
| 190 |
|
| 191 |
$this->hookRunner->onUserCanSendEmail( $user, $hookErr ); |
| 192 |
$this->hookRunner->onEmailUserPermissionsErrors( $user, $this->editToken, $hookErr ); |
| 193 |
if ( is_array( $hookErr ) ) { |
| 194 |
|
| 195 |
$msgParamsArray = is_array( $hookErr[2] ) ? $hookErr[2] : []; |
| 196 |
$ret = StatusValue::newFatal( $hookErr[1], ...$msgParamsArray ); |
| 197 |
$ret->value = $hookErr[0]; |
| 198 |
return $ret; |
| 199 |
} |
| 200 |
|
| 201 |
return StatusValue::newGood(); |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
public function authorizeSend(): StatusValue { |
| 211 |
$status = $this->canSend(); |
| 212 |
if ( !$status->isOK() ) { |
| 213 |
return $status; |
| 214 |
} |
| 215 |
|
| 216 |
$status = PermissionStatus::newGood(); |
| 217 |
if ( !$this->sender->authorizeAction( 'sendemail', $status ) ) { |
| 218 |
return $status; |
| 219 |
} |
| 220 |
|
| 221 |
$hookRes = $this->hookRunner->onEmailUserAuthorizeSend( $this->sender, $status ); |
| 222 |
if ( !$hookRes && !$status->isGood() ) { |
| 223 |
return $status; |
| 224 |
} |
| 225 |
|
| 226 |
return StatusValue::newGood(); |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
public function sendEmailUnsafe( |
| 240 |
UserEmailContact $target, |
| 241 |
string $subject, |
| 242 |
string $text, |
| 243 |
bool $CCMe, |
| 244 |
string $langCode |
| 245 |
): StatusValue { |
| 246 |
$senderIdentity = $this->sender->getUser(); |
| 247 |
$targetStatus = $this->validateTarget( $target ); |
| 248 |
if ( !$targetStatus->isGood() ) { |
| 249 |
return $targetStatus; |
| 250 |
} |
| 251 |
|
| 252 |
$senderUser = $this->userFactory->newFromAuthority( $this->sender ); |
| 253 |
|
| 254 |
$toAddress = MailAddress::newFromUser( $target ); |
| 255 |
$fromAddress = MailAddress::newFromUser( $senderUser ); |
| 256 |
|
| 257 |
|
| 258 |
$text = rtrim( $text ) . "\n\n-- \n"; |
| 259 |
$text .= $this->contLangMsgFormatter->format( |
| 260 |
MessageValue::new( 'emailuserfooter', [ $fromAddress->name, $toAddress->name ] ) |
| 261 |
); |
| 262 |
|
| 263 |
if ( $this->options->get( MainConfigNames::EnableSpecialMute ) ) { |
| 264 |
$text .= "\n" . $this->contLangMsgFormatter->format( |
| 265 |
MessageValue::new( |
| 266 |
'specialmute-email-footer', |
| 267 |
[ |
| 268 |
$this->getSpecialMuteCanonicalURL( $senderIdentity->getName() ), |
| 269 |
$senderIdentity->getName() |
| 270 |
] |
| 271 |
) |
| 272 |
); |
| 273 |
} |
| 274 |
|
| 275 |
$error = false; |
| 276 |
|
| 277 |
if ( !$this->hookRunner->onEmailUser( $toAddress, $fromAddress, $subject, $text, $error ) ) { |
| 278 |
if ( $error instanceof StatusValue ) { |
| 279 |
return $error; |
| 280 |
} elseif ( $error === false || $error === '' || $error === [] ) { |
| 281 |
|
| 282 |
return StatusValue::newFatal( 'hookaborted' ); |
| 283 |
} elseif ( $error === true ) { |
| 284 |
|
| 285 |
return StatusValue::newGood(); |
| 286 |
} elseif ( is_array( $error ) ) { |
| 287 |
$status = StatusValue::newGood(); |
| 288 |
foreach ( $error as $e ) { |
| 289 |
$status->fatal( $e ); |
| 290 |
} |
| 291 |
return $status; |
| 292 |
} elseif ( $error instanceof MessageSpecifier ) { |
| 293 |
return StatusValue::newFatal( $error ); |
| 294 |
} else { |
| 295 |
|
| 296 |
|
| 297 |
$type = get_debug_type( $error ); |
| 298 |
throw new UnexpectedValueException( |
| 299 |
'EmailUser hook set $error to unsupported type ' . $type |
| 300 |
); |
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
$hookStatus = StatusValue::newGood(); |
| 305 |
$hookRes = $this->hookRunner->onEmailUserSendEmail( |
| 306 |
$this->sender, |
| 307 |
$fromAddress, |
| 308 |
$target, |
| 309 |
$toAddress, |
| 310 |
$subject, |
| 311 |
$text, |
| 312 |
$hookStatus |
| 313 |
); |
| 314 |
if ( !$hookRes && !$hookStatus->isGood() ) { |
| 315 |
return $hookStatus; |
| 316 |
} |
| 317 |
|
| 318 |
[ $mailFrom, $replyTo ] = $this->getFromAndReplyTo( $fromAddress ); |
| 319 |
|
| 320 |
$status = $this->emailer->send( |
| 321 |
$toAddress, |
| 322 |
$mailFrom, |
| 323 |
$subject, |
| 324 |
$text, |
| 325 |
null, |
| 326 |
[ 'replyTo' => $replyTo ] |
| 327 |
); |
| 328 |
|
| 329 |
if ( !$status->isGood() ) { |
| 330 |
return $status; |
| 331 |
} |
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
if ( $CCMe && !$toAddress->equals( $fromAddress ) ) { |
| 337 |
$userMsgFormatter = $this->messageFormatterFactory->getTextFormatter( $langCode ); |
| 338 |
$ccTo = $fromAddress; |
| 339 |
$ccFrom = $fromAddress; |
| 340 |
$ccSubject = $userMsgFormatter->format( |
| 341 |
MessageValue::new( 'emailccsubject' )->plaintextParams( |
| 342 |
$target->getUser()->getName(), |
| 343 |
$subject |
| 344 |
) |
| 345 |
); |
| 346 |
$ccText = $text; |
| 347 |
|
| 348 |
$this->hookRunner->onEmailUserCC( $ccTo, $ccFrom, $ccSubject, $ccText ); |
| 349 |
|
| 350 |
[ $mailFrom, $replyTo ] = $this->getFromAndReplyTo( $ccFrom ); |
| 351 |
|
| 352 |
$ccStatus = $this->emailer->send( |
| 353 |
$ccTo, |
| 354 |
$mailFrom, |
| 355 |
$ccSubject, |
| 356 |
$ccText, |
| 357 |
null, |
| 358 |
[ 'replyTo' => $replyTo ] |
| 359 |
); |
| 360 |
$status->merge( $ccStatus ); |
| 361 |
} |
| 362 |
|
| 363 |
$this->hookRunner->onEmailUserComplete( $toAddress, $fromAddress, $subject, $text ); |
| 364 |
|
| 365 |
return $status; |
| 366 |
} |
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
private function getFromAndReplyTo( MailAddress $fromAddress ): array { |
| 374 |
if ( $this->options->get( MainConfigNames::UserEmailUseReplyTo ) ) { |
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
$mailFrom = new MailAddress( |
| 384 |
$this->options->get( MainConfigNames::PasswordSender ), |
| 385 |
$this->contLangMsgFormatter->format( MessageValue::new( 'emailsender' ) ) |
| 386 |
); |
| 387 |
$replyTo = $fromAddress; |
| 388 |
} else { |
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
$mailFrom = $fromAddress; |
| 405 |
$replyTo = null; |
| 406 |
} |
| 407 |
return [ $mailFrom, $replyTo ]; |
| 408 |
} |
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
private function getSpecialMuteCanonicalURL( string $targetName ): string { |
| 417 |
if ( defined( 'MW_PHPUNIT_TEST' ) ) { |
| 418 |
return "Ceci n'est pas une URL"; |
| 419 |
} |
| 420 |
return SpecialPage::getTitleFor( 'Mute', $targetName )->getCanonicalURL(); |
| 421 |
} |
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
public function setEditToken( string $token ): void { |
| 428 |
$this->editToken = $token; |
| 429 |
} |
| 430 |
|
| 431 |
} |