Total Complexity | 134 |
Total Lines | 665 |
Duplicated Lines | 0 % |
Changes | 14 | ||
Bugs | 2 | Features | 5 |
Complex classes like Header often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Header, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
25 | class Header { |
||
26 | |||
27 | /** |
||
28 | * Raw header |
||
29 | * |
||
30 | * @var string $raw |
||
31 | */ |
||
32 | public $raw = ""; |
||
33 | |||
34 | /** |
||
35 | * Attribute holder |
||
36 | * |
||
37 | * @var Attribute[]|array $attributes |
||
38 | */ |
||
39 | protected $attributes = []; |
||
40 | |||
41 | /** |
||
42 | * Config holder |
||
43 | * |
||
44 | * @var array $config |
||
45 | */ |
||
46 | protected $config = []; |
||
47 | |||
48 | /** |
||
49 | * Fallback Encoding |
||
50 | * |
||
51 | * @var string |
||
52 | */ |
||
53 | public $fallback_encoding = 'UTF-8'; |
||
54 | |||
55 | /** |
||
56 | * Convert parsed values to attributes |
||
57 | * |
||
58 | * @var bool |
||
59 | */ |
||
60 | protected $attributize = false; |
||
61 | |||
62 | /** |
||
63 | * Header constructor. |
||
64 | * @param $raw_header |
||
65 | * @param boolean $attributize |
||
66 | * |
||
67 | * @throws InvalidMessageDateException |
||
68 | */ |
||
69 | public function __construct($raw_header, $attributize = true) { |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * Call dynamic attribute setter and getter methods |
||
78 | * @param string $method |
||
79 | * @param array $arguments |
||
80 | * |
||
81 | * @return Attribute|mixed |
||
82 | * @throws MethodNotFoundException |
||
83 | */ |
||
84 | public function __call($method, $arguments) { |
||
85 | if(strtolower(substr($method, 0, 3)) === 'get') { |
||
86 | $name = preg_replace('/(.)(?=[A-Z])/u', '$1_', substr(strtolower($method), 3)); |
||
87 | |||
88 | if(in_array($name, array_keys($this->attributes))) { |
||
89 | return $this->attributes[$name]; |
||
90 | } |
||
91 | |||
92 | } |
||
93 | |||
94 | throw new MethodNotFoundException("Method ".self::class.'::'.$method.'() is not supported'); |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * Magic getter |
||
99 | * @param $name |
||
100 | * |
||
101 | * @return Attribute|null |
||
102 | */ |
||
103 | public function __get($name) { |
||
104 | return $this->get($name); |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * Get a specific header attribute |
||
109 | * @param $name |
||
110 | * |
||
111 | * @return Attribute|mixed |
||
112 | */ |
||
113 | public function get($name) { |
||
114 | if(isset($this->attributes[$name])) { |
||
115 | return $this->attributes[$name]; |
||
116 | } |
||
117 | |||
118 | return null; |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | * Set a specific attribute |
||
123 | * @param string $name |
||
124 | * @param array|mixed $value |
||
125 | * @param boolean $strict |
||
126 | * |
||
127 | * @return Attribute |
||
128 | */ |
||
129 | public function set($name, $value, $strict = false) { |
||
130 | if(isset($this->attributes[$name]) && $strict === false) { |
||
131 | if ($this->attributize) { |
||
132 | $this->attributes[$name]->add($value, true); |
||
133 | }else{ |
||
134 | if(isset($this->attributes[$name])) { |
||
135 | if (is_array($this->attributes[$name]) == false) { |
||
|
|||
136 | $this->attributes[$name] = [$this->attributes[$name], $value]; |
||
137 | }else{ |
||
138 | $this->attributes[$name][] = $value; |
||
139 | } |
||
140 | }else{ |
||
141 | $this->attributes[$name] = $value; |
||
142 | } |
||
143 | } |
||
144 | }elseif($this->attributize == false){ |
||
145 | $this->attributes[$name] = $value; |
||
146 | }else{ |
||
147 | $this->attributes[$name] = new Attribute($name, $value); |
||
148 | } |
||
149 | |||
150 | return $this->attributes[$name]; |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * Perform a regex match all on the raw header and return the first result |
||
155 | * @param $pattern |
||
156 | * |
||
157 | * @return mixed|null |
||
158 | */ |
||
159 | public function find($pattern) { |
||
160 | if (preg_match_all($pattern, $this->raw, $matches)) { |
||
161 | if (isset($matches[1])) { |
||
162 | if(count($matches[1]) > 0) { |
||
163 | return $matches[1][0]; |
||
164 | } |
||
165 | } |
||
166 | } |
||
167 | return null; |
||
168 | } |
||
169 | |||
170 | /** |
||
171 | * Parse the raw headers |
||
172 | * |
||
173 | * @throws InvalidMessageDateException |
||
174 | */ |
||
175 | protected function parse(){ |
||
176 | $header = $this->rfc822_parse_headers($this->raw); |
||
177 | |||
178 | $this->extractAddresses($header); |
||
179 | |||
180 | if (property_exists($header, 'subject')) { |
||
181 | $this->set("subject", $this->decode($header->subject)); |
||
182 | } |
||
183 | if (property_exists($header, 'references')) { |
||
184 | $this->set("references", $this->decode($header->references)); |
||
185 | } |
||
186 | if (property_exists($header, 'message_id')) { |
||
187 | $this->set("message_id", str_replace(['<', '>'], '', $header->message_id)); |
||
188 | } |
||
189 | |||
190 | $this->parseDate($header); |
||
191 | foreach ($header as $key => $value) { |
||
192 | $key = trim(rtrim(strtolower($key))); |
||
193 | if(!isset($this->attributes[$key])){ |
||
194 | $this->set($key, $value); |
||
195 | } |
||
196 | } |
||
197 | |||
198 | $this->extractHeaderExtensions(); |
||
199 | $this->findPriority(); |
||
200 | } |
||
201 | |||
202 | /** |
||
203 | * Parse mail headers from a string |
||
204 | * @link https://php.net/manual/en/function.imap-rfc822-parse-headers.php |
||
205 | * @param $raw_headers |
||
206 | * |
||
207 | * @return object |
||
208 | */ |
||
209 | public function rfc822_parse_headers($raw_headers){ |
||
210 | $headers = []; |
||
211 | $imap_headers = []; |
||
212 | if (extension_loaded('imap')) { |
||
213 | $raw_imap_headers = (array) \imap_rfc822_parse_headers($this->raw); |
||
214 | foreach($raw_imap_headers as $key => $values) { |
||
215 | $key = str_replace("-", "_", $key); |
||
216 | $imap_headers[$key] = $values; |
||
217 | } |
||
218 | } |
||
219 | |||
220 | $lines = explode("\r\n", $raw_headers); |
||
221 | $prev_header = null; |
||
222 | foreach($lines as $line) { |
||
223 | if (substr($line, 0, 1) === "\n") { |
||
224 | $line = substr($line, 1); |
||
225 | } |
||
226 | |||
227 | if (substr($line, 0, 1) === "\t") { |
||
228 | $line = substr($line, 1); |
||
229 | $line = trim(rtrim($line)); |
||
230 | if ($prev_header !== null) { |
||
231 | $headers[$prev_header][] = $line; |
||
232 | } |
||
233 | }elseif (substr($line, 0, 1) === " ") { |
||
234 | $line = substr($line, 1); |
||
235 | $line = trim(rtrim($line)); |
||
236 | if ($prev_header !== null) { |
||
237 | if (!isset($headers[$prev_header])) { |
||
238 | $headers[$prev_header] = ""; |
||
239 | } |
||
240 | if (is_array($headers[$prev_header])) { |
||
241 | $headers[$prev_header][] = $line; |
||
242 | }else{ |
||
243 | $headers[$prev_header] .= $line; |
||
244 | } |
||
245 | } |
||
246 | }else{ |
||
247 | if (($pos = strpos($line, ":")) > 0) { |
||
248 | $key = trim(rtrim(strtolower(substr($line, 0, $pos)))); |
||
249 | $key = str_replace("-", "_", $key); |
||
250 | |||
251 | $value = trim(rtrim(substr($line, $pos + 1))); |
||
252 | if (isset($headers[$key])) { |
||
253 | $headers[$key][] = $value; |
||
254 | }else{ |
||
255 | $headers[$key] = [$value]; |
||
256 | } |
||
257 | $prev_header = $key; |
||
258 | } |
||
259 | } |
||
260 | } |
||
261 | |||
262 | foreach($headers as $key => $values) { |
||
263 | if (isset($imap_headers[$key])) continue; |
||
264 | $value = null; |
||
265 | switch($key){ |
||
266 | case 'from': |
||
267 | case 'to': |
||
268 | case 'cc': |
||
269 | case 'bcc': |
||
270 | case 'reply_to': |
||
271 | case 'sender': |
||
272 | $value = $this->decodeAddresses($values); |
||
273 | $headers[$key."address"] = implode(", ", $values); |
||
274 | break; |
||
275 | case 'subject': |
||
276 | $value = implode(" ", $values); |
||
277 | break; |
||
278 | default: |
||
279 | if (is_array($values)) { |
||
280 | foreach($values as $k => $v) { |
||
281 | if ($v == "") { |
||
282 | unset($values[$k]); |
||
283 | } |
||
284 | } |
||
285 | $available_values = count($values); |
||
286 | if ($available_values === 1) { |
||
287 | $value = array_pop($values); |
||
288 | } elseif ($available_values === 2) { |
||
289 | $value = implode(" ", $values); |
||
290 | } elseif ($available_values > 2) { |
||
291 | $value = array_values($values); |
||
292 | } else { |
||
293 | $value = ""; |
||
294 | } |
||
295 | } |
||
296 | break; |
||
297 | } |
||
298 | $headers[$key] = $value; |
||
299 | } |
||
300 | |||
301 | return (object) array_merge($headers, $imap_headers); |
||
302 | } |
||
303 | |||
304 | /** |
||
305 | * Decode MIME header elements |
||
306 | * @link https://php.net/manual/en/function.imap-mime-header-decode.php |
||
307 | * @param string $text The MIME text |
||
308 | * |
||
309 | * @return array The decoded elements are returned in an array of objects, where each |
||
310 | * object has two properties, charset and text. |
||
311 | */ |
||
312 | public function mime_header_decode($text){ |
||
313 | if (extension_loaded('imap')) { |
||
314 | return \imap_mime_header_decode($text); |
||
315 | } |
||
316 | $charset = $this->getEncoding($text); |
||
317 | return [(object)[ |
||
318 | "charset" => $charset, |
||
319 | "text" => $this->convertEncoding($text, $charset) |
||
320 | ]]; |
||
321 | } |
||
322 | |||
323 | /** |
||
324 | * Check if a given pair of strings has ben decoded |
||
325 | * @param $encoded |
||
326 | * @param $decoded |
||
327 | * |
||
328 | * @return bool |
||
329 | */ |
||
330 | private function notDecoded($encoded, $decoded) { |
||
331 | return 0 === strpos($decoded, '=?') |
||
332 | && strlen($decoded) - 2 === strpos($decoded, '?=') |
||
333 | && false !== strpos($encoded, $decoded); |
||
334 | } |
||
335 | |||
336 | /** |
||
337 | * Convert the encoding |
||
338 | * @param $str |
||
339 | * @param string $from |
||
340 | * @param string $to |
||
341 | * |
||
342 | * @return mixed|string |
||
343 | */ |
||
344 | public function convertEncoding($str, $from = "ISO-8859-2", $to = "UTF-8") { |
||
345 | |||
346 | $from = EncodingAliases::get($from, $this->fallback_encoding); |
||
347 | $to = EncodingAliases::get($to, $this->fallback_encoding); |
||
348 | |||
349 | if ($from === $to) { |
||
350 | return $str; |
||
351 | } |
||
352 | |||
353 | // We don't need to do convertEncoding() if charset is ASCII (us-ascii): |
||
354 | // ASCII is a subset of UTF-8, so all ASCII files are already UTF-8 encoded |
||
355 | // https://stackoverflow.com/a/11303410 |
||
356 | // |
||
357 | // us-ascii is the same as ASCII: |
||
358 | // ASCII is the traditional name for the encoding system; the Internet Assigned Numbers Authority (IANA) |
||
359 | // prefers the updated name US-ASCII, which clarifies that this system was developed in the US and |
||
360 | // based on the typographical symbols predominantly in use there. |
||
361 | // https://en.wikipedia.org/wiki/ASCII |
||
362 | // |
||
363 | // convertEncoding() function basically means convertToUtf8(), so when we convert ASCII string into UTF-8 it gets broken. |
||
364 | if (strtolower($from) == 'us-ascii' && $to == 'UTF-8') { |
||
365 | return $str; |
||
366 | } |
||
367 | |||
368 | try { |
||
369 | if (function_exists('iconv') && $from != 'UTF-7' && $to != 'UTF-7') { |
||
370 | return iconv($from, $to, $str); |
||
371 | } else { |
||
372 | if (!$from) { |
||
373 | return mb_convert_encoding($str, $to); |
||
374 | } |
||
375 | return mb_convert_encoding($str, $to, $from); |
||
376 | } |
||
377 | } catch (\Exception $e) { |
||
378 | if (strstr($from, '-')) { |
||
379 | $from = str_replace('-', '', $from); |
||
380 | return $this->convertEncoding($str, $from, $to); |
||
381 | } else { |
||
382 | return $str; |
||
383 | } |
||
384 | } |
||
385 | } |
||
386 | |||
387 | /** |
||
388 | * Get the encoding of a given abject |
||
389 | * @param object|string $structure |
||
390 | * |
||
391 | * @return string |
||
392 | */ |
||
393 | public function getEncoding($structure) { |
||
394 | if (property_exists($structure, 'parameters')) { |
||
395 | foreach ($structure->parameters as $parameter) { |
||
396 | if (strtolower($parameter->attribute) == "charset") { |
||
397 | return EncodingAliases::get($parameter->value, $this->fallback_encoding); |
||
398 | } |
||
399 | } |
||
400 | }elseif (property_exists($structure, 'charset')) { |
||
401 | return EncodingAliases::get($structure->charset, $this->fallback_encoding); |
||
402 | }elseif (is_string($structure) === true){ |
||
403 | return mb_detect_encoding($structure); |
||
404 | } |
||
405 | |||
406 | return $this->fallback_encoding; |
||
407 | } |
||
408 | |||
409 | /** |
||
410 | * Try to decode a specific header |
||
411 | * @param mixed $value |
||
412 | * |
||
413 | * @return mixed |
||
414 | */ |
||
415 | private function decode($value) { |
||
416 | if (is_array($value)) { |
||
417 | return $this->decodeArray($value); |
||
418 | } |
||
419 | $original_value = $value; |
||
420 | $decoder = $this->config['decoder']['message']; |
||
421 | |||
422 | if ($value !== null) { |
||
423 | if($decoder === 'utf-8' && extension_loaded('imap')) { |
||
424 | $value = \imap_utf8($value); |
||
425 | if (strpos(strtolower($value), '=?utf-8?') === 0) { |
||
426 | $value = mb_decode_mimeheader($value); |
||
427 | } |
||
428 | if ($this->notDecoded($original_value, $value)) { |
||
429 | $decoded_value = $this->mime_header_decode($value); |
||
430 | if (count($decoded_value) > 0) { |
||
431 | if(property_exists($decoded_value[0], "text")) { |
||
432 | $value = $decoded_value[0]->text; |
||
433 | } |
||
434 | } |
||
435 | } |
||
436 | }elseif($decoder === 'iconv') { |
||
437 | $value = iconv_mime_decode($value); |
||
438 | }else{ |
||
439 | $value = mb_decode_mimeheader($value); |
||
440 | } |
||
441 | |||
442 | if (strpos(strtolower($value), '=?utf-8?') === 0) { |
||
443 | $value = mb_decode_mimeheader($value); |
||
444 | } |
||
445 | |||
446 | if ($this->notDecoded($original_value, $value)) { |
||
447 | $value = $this->convertEncoding($original_value, $this->getEncoding($original_value)); |
||
448 | } |
||
449 | } |
||
450 | |||
451 | return $value; |
||
452 | } |
||
453 | |||
454 | /** |
||
455 | * Decode a given array |
||
456 | * @param array $values |
||
457 | * |
||
458 | * @return array |
||
459 | */ |
||
460 | private function decodeArray($values) { |
||
461 | foreach($values as $key => $value) { |
||
462 | $values[$key] = $this->decode($value); |
||
463 | } |
||
464 | return $values; |
||
465 | } |
||
466 | |||
467 | /** |
||
468 | * Try to extract the priority from a given raw header string |
||
469 | */ |
||
470 | private function findPriority() { |
||
494 | } |
||
495 | |||
496 | /** |
||
497 | * Extract a given part as address array from a given header |
||
498 | * @param $values |
||
499 | * |
||
500 | * @return array |
||
501 | */ |
||
502 | private function decodeAddresses($values) { |
||
525 | } |
||
526 | |||
527 | /** |
||
528 | * Extract a given part as address array from a given header |
||
529 | * @param object $header |
||
530 | */ |
||
531 | private function extractAddresses($header) { |
||
532 | foreach(['from', 'to', 'cc', 'bcc', 'reply_to', 'sender'] as $key){ |
||
533 | if (property_exists($header, $key)) { |
||
534 | $this->set($key, $this->parseAddresses($header->$key)); |
||
535 | } |
||
536 | } |
||
537 | } |
||
538 | |||
539 | /** |
||
540 | * Parse Addresses |
||
541 | * @param $list |
||
542 | * |
||
543 | * @return array |
||
544 | */ |
||
545 | private function parseAddresses($list) { |
||
585 | } |
||
586 | |||
587 | /** |
||
588 | * Search and extract potential header extensions |
||
589 | */ |
||
590 | private function extractHeaderExtensions(){ |
||
591 | foreach ($this->attributes as $key => $value) { |
||
616 | } |
||
617 | } |
||
618 | } |
||
619 | } |
||
620 | } |
||
621 | } |
||
622 | } |
||
623 | |||
624 | /** |
||
625 | * Exception handling for invalid dates |
||
626 | * |
||
627 | * Currently known invalid formats: |
||
628 | * ^ Datetime ^ Problem ^ Cause |
||
629 | * | Mon, 20 Nov 2017 20:31:31 +0800 (GMT+8:00) | Double timezone specification | A Windows feature |
||
630 | * | Thu, 8 Nov 2018 08:54:58 -0200 (-02) | |
||
631 | * | | and invalid timezone (max 6 char) | |
||
632 | * | 04 Jan 2018 10:12:47 UT | Missing letter "C" | Unknown |
||
633 | * | Thu, 31 May 2018 18:15:00 +0800 (added by) | Non-standard details added by the | Unknown |
||
634 | * | | mail server | |
||
635 | * | Sat, 31 Aug 2013 20:08:23 +0580 | Invalid timezone | PHPMailer bug https://sourceforge.net/p/phpmailer/mailman/message/6132703/ |
||
636 | * |
||
637 | * Please report any new invalid timestamps to [#45](https://github.com/Webklex/php-imap/issues) |
||
638 | * |
||
639 | * @param object $header |
||
640 | * |
||
641 | * @throws InvalidMessageDateException |
||
642 | */ |
||
643 | private function parseDate($header) { |
||
680 | } |
||
681 | } |
||
682 | |||
683 | /** |
||
684 | * Get all available attributes |
||
685 | * |
||
686 | * @return array |
||
687 | */ |
||
688 | public function getAttributes() { |
||
690 | } |
||
691 | |||
693 |
When comparing two booleans, it is generally considered safer to use the strict comparison operator.