Elgg  Version master
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;
8 use Laminas\Validator\Hostname;
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 }
$site name
Definition: settings.php:15
$site email
Definition: settings.php:16
$entity
Definition: reset.php:8
$email
Definition: change_email.php:7
if(! $user||! $user->canDelete()) $name
Definition: delete.php:22
if(! $entity instanceof \ElggEntity) if(! $entity->canComment()) $comment
Definition: save.php:42
return[ 'admin/delete_admin_notices'=>['access'=> 'admin'], 'admin/menu/save'=>['access'=> 'admin'], 'admin/plugins/activate'=>['access'=> 'admin'], 'admin/plugins/activate_all'=>['access'=> 'admin'], 'admin/plugins/deactivate'=>['access'=> 'admin'], 'admin/plugins/deactivate_all'=>['access'=> 'admin'], 'admin/plugins/set_priority'=>['access'=> 'admin'], 'admin/security/security_txt'=>['access'=> 'admin'], 'admin/security/settings'=>['access'=> 'admin'], 'admin/security/regenerate_site_secret'=>['access'=> 'admin'], 'admin/site/cache/invalidate'=>['access'=> 'admin'], 'admin/site/flush_cache'=>['access'=> 'admin'], 'admin/site/icons'=>['access'=> 'admin'], 'admin/site/set_maintenance_mode'=>['access'=> 'admin'], 'admin/site/set_robots'=>['access'=> 'admin'], 'admin/site/theme'=>['access'=> 'admin'], 'admin/site/unlock_upgrade'=>['access'=> 'admin'], 'admin/site/settings'=>['access'=> 'admin'], 'admin/upgrade'=>['access'=> 'admin'], 'admin/upgrade/reset'=>['access'=> 'admin'], 'admin/user/ban'=>['access'=> 'admin'], 'admin/user/bulk/ban'=>['access'=> 'admin'], 'admin/user/bulk/delete'=>['access'=> 'admin'], 'admin/user/bulk/unban'=>['access'=> 'admin'], 'admin/user/bulk/validate'=>['access'=> 'admin'], 'admin/user/change_email'=>['access'=> 'admin'], 'admin/user/delete'=>['access'=> 'admin'], 'admin/user/login_as'=>['access'=> 'admin'], 'admin/user/logout_as'=>[], 'admin/user/makeadmin'=>['access'=> 'admin'], 'admin/user/resetpassword'=>['access'=> 'admin'], 'admin/user/removeadmin'=>['access'=> 'admin'], 'admin/user/unban'=>['access'=> 'admin'], 'admin/user/validate'=>['access'=> 'admin'], 'annotation/delete'=>[], 'avatar/upload'=>[], 'comment/save'=>[], 'diagnostics/download'=>['access'=> 'admin'], 'entity/chooserestoredestination'=>[], 'entity/delete'=>[], 'entity/mute'=>[], 'entity/restore'=>[], 'entity/subscribe'=>[], 'entity/trash'=>[], 'entity/unmute'=>[], 'entity/unsubscribe'=>[], 'login'=>['access'=> 'logged_out'], 'logout'=>[], 'notifications/mute'=>['access'=> 'public'], 'plugins/settings/remove'=>['access'=> 'admin'], 'plugins/settings/save'=>['access'=> 'admin'], 'plugins/usersettings/save'=>[], 'register'=>['access'=> 'logged_out', 'middleware'=>[\Elgg\Router\Middleware\RegistrationAllowedGatekeeper::class,],], 'river/delete'=>[], 'settings/notifications'=>[], 'settings/notifications/subscriptions'=>[], 'user/changepassword'=>['access'=> 'public'], 'user/requestnewpassword'=>['access'=> 'public'], 'useradd'=>['access'=> 'admin'], 'usersettings/save'=>[], 'widgets/add'=>[], 'widgets/delete'=>[], 'widgets/move'=>[], 'widgets/save'=>[],]
Definition: actions.php:73
Email address.
Definition: Address.php:15
__construct($email, $name=null, $comment=null)
{}
Definition: Address.php:25
getEntity()
Returns the saved entity.
Definition: Address.php:117
unsetName()
Clear the name from the email address.
Definition: Address.php:93
setEmail($email)
Set the email address.
Definition: Address.php:46
setEntity(\ElggEntity $entity)
Store the ElggEntity related to this Address.
Definition: Address.php:106
static fromString($address, $comment=null)
Parses strings like "Evan <evan@elgg.org>" into name/email objects.
Definition: Address.php:133
static fromEntity(\ElggEntity $entity)
Create an Address based on a Entity.
Definition: Address.php:152
static getFormattedEmailAddress($email, $name=null)
Format an email address and name into a formatted email address.
Definition: Address.php:170
setName($name)
Set the name.
Definition: Address.php:74
Exception thrown if an argument is not of the expected type.
elgg_substr()
Wrapper function for mb_substr().
Definition: mb_wrapper.php:195
elgg_strpos()
Wrapper function for mb_strpos().
Definition: mb_wrapper.php:71