wiki.techinc.nl/includes/mail/MailAddress.php
Daimona Eaytoy e5f17be06c mail: Round 2 of EmailUser refactoring
- Strengthen types
- Use StatusValue instead of Status
- Extract duplicated code to a new private method
- Add MailAddress::equals to compare addresses, instead of relying on
  the weak '!=' comparison. There might be a question of whether having
  the same address should be enough for two MailAddress objects to be
  equal, but the implementation in this patch preserves the status quo.
- Make the new validateTarget accept a User object directly. This is
  better than passing strings around just to have the "validator" create
  a user for you.
- Avoid returning false if there's a hook error in submit(). Accepting 5
  different error formats in the same hook is simply unreasonable, and
  this should eventually be standardized to use StatusValue. The old
  methods in SpecialEmailUser retain BC.

Bug: T265541
Change-Id: Ia0ba27fe634e328dff1a6c06fb9979cb8ce4f7e7
2023-05-17 09:58:21 +00:00

117 lines
3.1 KiB
PHP

<?php
/**
* Classes used to send e-mails
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
* @author <brion@pobox.com>
* @author <mail@tgries.de>
* @author Tim Starling
* @author Luke Welling lwelling@wikimedia.org
*/
use MediaWiki\Mail\UserEmailContact;
/**
* Stores a single person's name and email address.
* These are passed in via the constructor, and will be returned in SMTP
* header format when requested.
*
* @newable
*/
class MailAddress {
/**
* @var string
*/
public $name;
/**
* @var string
*/
public $realName;
/**
* @var string
*/
public $address;
/**
* @stable to call
*
* @param string $address String with an email address
* @param string|null $name Human-readable name if a string address is given
* @param string|null $realName Human-readable real name if a string address is given
*/
public function __construct( $address, $name = null, $realName = null ) {
$this->address = strval( $address );
$this->name = strval( $name );
$this->realName = strval( $realName );
}
/**
* Create a new MailAddress object for the given user
*
* @param UserEmailContact $user
* @return MailAddress
* @since 1.24
*/
public static function newFromUser( UserEmailContact $user ) {
return new MailAddress( $user->getEmail(), $user->getUser()->getName(), $user->getRealName() );
}
/**
* @param self $other
* @return bool
* @since 1.40
*/
public function equals( self $other ): bool {
return $this->address === $other->address &&
$this->name === $other->name &&
$this->realName === $other->realName;
}
/**
* Return formatted and quoted address to insert into SMTP headers
* @return string
*/
public function toString() {
if ( !$this->address ) {
return '';
}
# PHP's mail() implementation under Windows is somewhat shite, and
# can't handle "Joe Bloggs <joe@bloggs.com>" format email addresses,
# so don't bother generating them
if ( $this->name === '' || wfIsWindows() ) {
return $this->address;
}
global $wgEnotifUseRealName;
$name = ( $wgEnotifUseRealName && $this->realName !== '' ) ? $this->realName : $this->name;
$quoted = UserMailer::quotedPrintable( $name );
// Must only be quoted if string does not use =? encoding (T191931)
if ( $quoted === $name ) {
$quoted = '"' . addslashes( $quoted ) . '"';
}
return "$quoted <{$this->address}>";
}
public function __toString() {
return $this->toString();
}
}