Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
address.php
1<?php
10
16{
18 protected $name = null;
19
21 protected $email = null;
22 private $checkingPunycode;
23
30 public static function isValid($address)
31 {
32 return (new static($address))->validate();
33 }
34
41 public function __construct($address = null, $options = [])
42 {
43 if (array_key_exists('checkingPunycode', $options))
44 {
45 $this->setCheckingPunycode($options['checkingPunycode']);
46 }
47 if ($address)
48 {
49 $this->set($address);
50 }
51 }
52
53 public function setCheckingPunycode($checkingPunycode)
54 {
55 $this->checkingPunycode = (bool)$checkingPunycode;
56 }
57
63 public function getEncoded()
64 {
65 if (!$this->email)
66 {
67 return null;
68 }
69
70 if ($this->name)
71 {
72 $address = sprintf(
73 '%s <%s>',
74 sprintf('=?%s?B?%s?=', SITE_CHARSET, base64_encode($this->name)),
75 $this->email
76 );
77 }
78 else
79 {
80 $address = "<{$this->email}>";
81 }
82
83 return $address;
84 }
85
91 public function get()
92 {
93 if (!$this->email)
94 {
95 return null;
96 }
97
98 $address = '';
99 if ($this->name)
100 {
101 $address = $this->name . ' ';
102 }
103
104 $address .= "<{$this->email}>";
105
106 return $address;
107 }
108
115 public function set($address)
116 {
117 $this->parse($address);
118 return $this;
119 }
120
126 public function getName()
127 {
128 return Emoji::decode($this->name);
129 }
130
137 public function setName($name)
138 {
139 $name = trim($name, "\x20\t\n\r\0\x0b");
140 if ($name != '')
141 {
142 $name = str_replace(
143 array('\\', '"', '<', '>'),
144 array('/', '\'', '(', ')'),
145 $name
146 );
147 }
148
149 $this->name = $name;
150 return $this;
151 }
152
158 public function getEmail()
159 {
160 return $this->email;
161 }
162
169 public function setEmail($email)
170 {
171 $email = mb_strtolower(trim($email));
172 if (!$this->checkMail($email))
173 {
174 $email = null;
175 }
176
177 $this->email = $email;
178 return $this;
179 }
180
181
182
188 public function validate()
189 {
190 return !empty($this->email);
191 }
192
199 protected function parse($address)
200 {
201 $this->setName('');
202 $this->setEmail('');
203
204 if (!$address)
205 {
206 return;
207 }
208
209 if (preg_match('/(.*)<(.+?)>\s*$/is', $address, $matches))
210 {
211 $this->setName($matches[1]);
212 $this->setEmail($matches[2]);
213 }
214 else
215 {
216 $this->setEmail($address);
217 }
218 }
219
220 private function checkMail($email)
221 {
222 if (check_email($email, true))
223 {
224 return true;
225 }
226 if ($this->checkingPunycode)
227 {
228 $addressWithPunycodeDomain = $this->convertAddressToPunycode($email);
229 if ($addressWithPunycodeDomain)
230 {
231 return check_email($addressWithPunycodeDomain, true);
232 }
233 }
234
235 return false;
236 }
237
242 private function convertAddressToPunycode($email)
243 {
244 if (count(explode('@', $email)) === 2)
245 {
246 $domainPart = array_pop(explode('@', $email));
247 if ($domainPart)
248 {
249 $emailAddressName = array_shift(explode('@', $email));
250 $encoder = new \CBXPunycode();
251 if ($encodedDomain = $encoder->encode($domainPart))
252 {
253 return $emailAddressName . '@' . $encodedDomain;
254 }
255 }
256 }
257 return false;
258 }
259}
setCheckingPunycode($checkingPunycode)
Definition address.php:53
static isValid($address)
Definition address.php:30
__construct($address=null, $options=[])
Definition address.php:41
static decode($text)
Definition emoji.php:24