Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
17 / 17 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
1 / 1 |
MailAddress | |
100.00% |
17 / 17 |
|
100.00% |
5 / 5 |
13 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
newFromUser | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
equals | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
3 | |||
toString | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
7 | |||
__toString | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | /** |
3 | * This program is free software; you can redistribute it and/or modify |
4 | * it under the terms of the GNU General Public License as published by |
5 | * the Free Software Foundation; either version 2 of the License, or |
6 | * (at your option) any later version. |
7 | * |
8 | * This program is distributed in the hope that it will be useful, |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
11 | * GNU General Public License for more details. |
12 | * |
13 | * You should have received a copy of the GNU General Public License along |
14 | * with this program; if not, write to the Free Software Foundation, Inc., |
15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
16 | * http://www.gnu.org/copyleft/gpl.html |
17 | * |
18 | * @file |
19 | * @author Brooke Vibber |
20 | * @author <mail@tgries.de> |
21 | * @author Tim Starling |
22 | * @author Luke Welling lwelling@wikimedia.org |
23 | */ |
24 | |
25 | use MediaWiki\Mail\UserEmailContact; |
26 | |
27 | /** |
28 | * Represent and format a single name and email address pair for SMTP. |
29 | * |
30 | * Used by Emailer, e.g. via EmailUser or EmailNotification. |
31 | * |
32 | * @newable |
33 | * @since 1.6.0 |
34 | * @ingroup Mail |
35 | */ |
36 | class MailAddress implements Stringable { |
37 | |
38 | public string $name; |
39 | public string $realName; |
40 | public string $address; |
41 | |
42 | /** |
43 | * @stable to call |
44 | * @since 1.6.0 |
45 | * @param string $address String with an email address |
46 | * @param string|null $name Human-readable name if a string address is given |
47 | * @param string|null $realName Human-readable real name if a string address is given |
48 | */ |
49 | public function __construct( $address, $name = null, $realName = null ) { |
50 | $this->address = strval( $address ); |
51 | $this->name = strval( $name ); |
52 | $this->realName = strval( $realName ); |
53 | } |
54 | |
55 | /** |
56 | * @since 1.24 |
57 | * @param UserEmailContact $user |
58 | * @return MailAddress |
59 | */ |
60 | public static function newFromUser( UserEmailContact $user ) { |
61 | return new MailAddress( $user->getEmail(), $user->getUser()->getName(), $user->getRealName() ); |
62 | } |
63 | |
64 | /** |
65 | * @since 1.40 |
66 | * @param self $other |
67 | * @return bool |
68 | */ |
69 | public function equals( self $other ): bool { |
70 | return $this->address === $other->address && |
71 | $this->name === $other->name && |
72 | $this->realName === $other->realName; |
73 | } |
74 | |
75 | /** |
76 | * Format and quote address for insertion in SMTP headers |
77 | * |
78 | * @since 1.6.0 |
79 | * @return string |
80 | */ |
81 | public function toString() { |
82 | if ( !$this->address ) { |
83 | return ''; |
84 | } |
85 | |
86 | # PHP's mail() implementation under Windows is somewhat shite, and |
87 | # can't handle "Joe Bloggs <joe@bloggs.com>" format email addresses, |
88 | # so don't bother generating them |
89 | if ( $this->name === '' || wfIsWindows() ) { |
90 | return $this->address; |
91 | } |
92 | |
93 | global $wgEnotifUseRealName; |
94 | $name = ( $wgEnotifUseRealName && $this->realName !== '' ) ? $this->realName : $this->name; |
95 | $quoted = UserMailer::quotedPrintable( $name ); |
96 | // Must only be quoted if string does not use =? encoding (T191931) |
97 | if ( $quoted === $name ) { |
98 | $quoted = '"' . addslashes( $quoted ) . '"'; |
99 | } |
100 | |
101 | return "$quoted <{$this->address}>"; |
102 | } |
103 | |
104 | public function __toString() { |
105 | return $this->toString(); |
106 | } |
107 | } |