Elgg  Version 5.1
Address.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Email;
4 
6 use Laminas\Mail\Address as ZendAddress;
7 use Laminas\Validator\EmailAddress as EmailAddressValidator;
9 
15 class Address extends ZendAddress {
16 
20  protected $entity = null;
21 
25  public function __construct($email, $name = null, $comment = null) {
26  if (isset($name) && is_string($name)) {
27  $name = html_entity_decode($name, ENT_QUOTES | ENT_XHTML, 'UTF-8');
28  }
29 
30  try {
31  parent::__construct($email, $name, $comment);
32  } catch (\Laminas\Mail\Exception\InvalidArgumentException $e) {
33  throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
34  }
35  }
36 
46  public function setEmail($email) {
47 
48  if (!is_string($email) || empty($email)) {
49  throw new InvalidArgumentException('Email must be a valid email address');
50  }
51 
52  if (preg_match("/[\r\n]/", $email)) {
53  throw new InvalidArgumentException('CRLF injection detected');
54  }
55 
56  $emailAddressValidator = new EmailAddressValidator(Hostname::ALLOW_DNS | Hostname::ALLOW_LOCAL);
57  if (!$emailAddressValidator->isValid($email)) {
58  $invalidMessages = $emailAddressValidator->getMessages();
59  throw new InvalidArgumentException(array_shift($invalidMessages));
60  }
61 
62  $this->email = $email;
63  }
64 
74  public function setName($name) {
75 
76  if (!is_string($name)) {
77  throw new InvalidArgumentException('Name must be a string');
78  }
79 
80  if (preg_match("/[\r\n]/", $name)) {
81  throw new InvalidArgumentException('CRLF injection detected');
82  }
83 
84  $this->name = html_entity_decode($name, ENT_QUOTES | ENT_XHTML, 'UTF-8');
85  }
86 
93  public function unsetName() {
94  $this->name = null;
95  }
96 
106  public function setEntity(\ElggEntity $entity): void {
107  $this->entity = $entity;
108  }
109 
117  public function getEntity(): ?\ElggEntity {
118  return $this->entity;
119  }
120 
133  public static function fromString($address, $comment = null): Address {
134  $containsName = preg_match('/<(.*)>/', $address, $matches) == 1;
135  if ($containsName) {
136  $name = trim(elgg_substr($address, 0, elgg_strpos($address, '<')));
137  return new static($matches[1], $name);
138  }
139 
140  return new static(trim($address));
141  }
142 
152  public static function fromEntity(\ElggEntity $entity): Address {
153  $address = new static($entity->email, $entity->getDisplayName());
154  $address->setEntity($entity);
155  return $address;
156  }
157 
170  public static function getFormattedEmailAddress($email, $name = null) {
171  $mail = new static($email, $name);
172  return $mail->toString();
173  }
174 }
getEntity()
Returns the saved entity.
Definition: Address.php:117
Exception thrown if an argument is not of the expected type.
if(!$user||!$user->canDelete()) $name
Definition: delete.php:22
c Accompany it with the information you received as to the offer to distribute corresponding source complete source code means all the source code for all modules it plus any associated interface definition plus the scripts used to control compilation and installation of the executable as a special the source code distributed need not include anything that is normally and so on of the operating system on which the executable unless that component itself accompanies the executable If distribution of executable or object code is made by offering access to copy from a designated then offering equivalent access to copy the source code from the same place counts as distribution of the source even though third parties are not compelled to copy the source along with the object code You may not or distribute the Program except as expressly provided under this License Any attempt otherwise to sublicense or distribute the Program is void
Definition: LICENSE.txt:215
$email
Definition: change_email.php:7
static getFormattedEmailAddress($email, $name=null)
Format an email address and name into a formatted email address.
Definition: Address.php:170
$site email
Definition: settings.php:16
elgg_strpos()
Wrapper function for mb_strpos().
Definition: mb_wrapper.php:71
static fromString($address, $comment=null)
Parses strings like "Evan <evan@elgg.org>" into name/email objects.
Definition: Address.php:133
__construct($email, $name=null, $comment=null)
{}
Definition: Address.php:25
if(!$entity instanceof\ElggEntity) if(!$entity->canComment()) $comment
Definition: save.php:42
elgg_substr()
Wrapper function for mb_substr().
Definition: mb_wrapper.php:230
setName($name)
Set the name.
Definition: Address.php:74
unsetName()
Clear the name from the email address.
Definition: Address.php:93
setEntity(\ElggEntity $entity)
Store the ElggEntity related to this Address.
Definition: Address.php:106
static fromEntity(\ElggEntity $entity)
Create an Address based on a Entity.
Definition: Address.php:152
Email address.
Definition: Address.php:15
$site name
Definition: settings.php:15
setEmail($email)
Set the email address.
Definition: Address.php:46
getDisplayName()
Get the entity&#39;s display name.
Definition: ElggEntity.php:312