Total Complexity | 129 |
Total Lines | 638 |
Duplicated Lines | 0 % |
Changes | 13 | ||
Bugs | 2 | Features | 4 |
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[] $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 | * Header constructor. |
||
57 | * @param $raw_header |
||
58 | * |
||
59 | * @throws InvalidMessageDateException |
||
60 | */ |
||
61 | public function __construct($raw_header) { |
||
65 | } |
||
66 | |||
67 | /** |
||
68 | * Call dynamic attribute setter and getter methods |
||
69 | * @param string $method |
||
70 | * @param array $arguments |
||
71 | * |
||
72 | * @return Attribute |
||
73 | * @throws MethodNotFoundException |
||
74 | */ |
||
75 | public function __call($method, $arguments) { |
||
76 | if(strtolower(substr($method, 0, 3)) === 'get') { |
||
77 | $name = preg_replace('/(.)(?=[A-Z])/u', '$1_', substr(strtolower($method), 3)); |
||
78 | |||
79 | if(in_array($name, array_keys($this->attributes))) { |
||
80 | return $this->attributes[$name]; |
||
81 | } |
||
82 | |||
83 | } |
||
84 | |||
85 | throw new MethodNotFoundException("Method ".self::class.'::'.$method.'() is not supported'); |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * Magic getter |
||
90 | * @param $name |
||
91 | * |
||
92 | * @return Attribute|null |
||
93 | */ |
||
94 | public function __get($name) { |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * Get a specific header attribute |
||
100 | * @param $name |
||
101 | * |
||
102 | * @return Attribute|null |
||
103 | */ |
||
104 | public function get($name) { |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * Set a specific attribute |
||
114 | * @param string $name |
||
115 | * @param array|mixed $value |
||
116 | * @param boolean $strict |
||
117 | * |
||
118 | * @return Attribute |
||
119 | */ |
||
120 | public function set($name, $value, $strict = false) { |
||
121 | if(isset($this->attributes[$name]) && $strict === false) { |
||
122 | $this->attributes[$name]->add($value, true); |
||
123 | }else{ |
||
124 | $this->attributes[$name] = new Attribute($name, $value); |
||
125 | } |
||
126 | |||
127 | return $this->attributes[$name]; |
||
128 | } |
||
129 | |||
130 | /** |
||
131 | * Perform a regex match all on the raw header and return the first result |
||
132 | * @param $pattern |
||
133 | * |
||
134 | * @return mixed|null |
||
135 | */ |
||
136 | public function find($pattern) { |
||
137 | if (preg_match_all($pattern, $this->raw, $matches)) { |
||
138 | if (isset($matches[1])) { |
||
139 | if(count($matches[1]) > 0) { |
||
140 | return $matches[1][0]; |
||
141 | } |
||
142 | } |
||
143 | } |
||
144 | return null; |
||
145 | } |
||
146 | |||
147 | /** |
||
148 | * Parse the raw headers |
||
149 | * |
||
150 | * @throws InvalidMessageDateException |
||
151 | */ |
||
152 | protected function parse(){ |
||
153 | $header = $this->rfc822_parse_headers($this->raw); |
||
154 | |||
155 | $this->extractAddresses($header); |
||
156 | |||
157 | if (property_exists($header, 'subject')) { |
||
158 | $this->set("subject", $this->decode($header->subject)); |
||
159 | } |
||
160 | if (property_exists($header, 'references')) { |
||
161 | $this->set("references", $this->decode($header->references)); |
||
162 | } |
||
163 | if (property_exists($header, 'message_id')) { |
||
164 | $this->set("message_id", str_replace(['<', '>'], '', $header->message_id)); |
||
165 | } |
||
166 | |||
167 | $this->parseDate($header); |
||
168 | foreach ($header as $key => $value) { |
||
169 | $key = trim(rtrim(strtolower($key))); |
||
170 | if(!isset($this->attributes[$key])){ |
||
171 | $this->set($key, $value); |
||
172 | } |
||
173 | } |
||
174 | |||
175 | $this->extractHeaderExtensions(); |
||
176 | $this->findPriority(); |
||
177 | } |
||
178 | |||
179 | /** |
||
180 | * Parse mail headers from a string |
||
181 | * @link https://php.net/manual/en/function.imap-rfc822-parse-headers.php |
||
182 | * @param $raw_headers |
||
183 | * |
||
184 | * @return object |
||
185 | */ |
||
186 | public function rfc822_parse_headers($raw_headers){ |
||
187 | $headers = []; |
||
188 | $imap_headers = []; |
||
189 | if (extension_loaded('imap')) { |
||
190 | $raw_imap_headers = (array) \imap_rfc822_parse_headers($this->raw); |
||
191 | foreach($raw_imap_headers as $key => $values) { |
||
192 | $key = str_replace("-", "_", $key); |
||
193 | $imap_headers[$key] = $values; |
||
194 | } |
||
195 | } |
||
196 | |||
197 | $lines = explode("\r\n", $raw_headers); |
||
198 | $prev_header = null; |
||
199 | foreach($lines as $line) { |
||
200 | if (substr($line, 0, 1) === "\n") { |
||
201 | $line = substr($line, 1); |
||
202 | } |
||
203 | |||
204 | if (substr($line, 0, 1) === "\t") { |
||
205 | $line = substr($line, 1); |
||
206 | $line = trim(rtrim($line)); |
||
207 | if ($prev_header !== null) { |
||
208 | $headers[$prev_header][] = $line; |
||
209 | } |
||
210 | }elseif (substr($line, 0, 1) === " ") { |
||
211 | $line = substr($line, 1); |
||
212 | $line = trim(rtrim($line)); |
||
213 | if ($prev_header !== null) { |
||
214 | if (!isset($headers[$prev_header])) { |
||
215 | $headers[$prev_header] = ""; |
||
216 | } |
||
217 | if (is_array($headers[$prev_header])) { |
||
218 | $headers[$prev_header][] = $line; |
||
219 | }else{ |
||
220 | $headers[$prev_header] .= $line; |
||
221 | } |
||
222 | } |
||
223 | }else{ |
||
224 | if (($pos = strpos($line, ":")) > 0) { |
||
225 | $key = trim(rtrim(strtolower(substr($line, 0, $pos)))); |
||
226 | $key = str_replace("-", "_", $key); |
||
227 | |||
228 | $value = trim(rtrim(substr($line, $pos + 1))); |
||
229 | if (isset($headers[$key])) { |
||
230 | $headers[$key][] = $value; |
||
231 | }else{ |
||
232 | $headers[$key] = [$value]; |
||
233 | } |
||
234 | $prev_header = $key; |
||
235 | } |
||
236 | } |
||
237 | } |
||
238 | |||
239 | foreach($headers as $key => $values) { |
||
240 | if (isset($imap_headers[$key])) continue; |
||
241 | $value = null; |
||
242 | switch($key){ |
||
243 | case 'from': |
||
244 | case 'to': |
||
245 | case 'cc': |
||
246 | case 'bcc': |
||
247 | case 'reply_to': |
||
248 | case 'sender': |
||
249 | $value = $this->decodeAddresses($values); |
||
250 | $headers[$key."address"] = implode(", ", $values); |
||
251 | break; |
||
252 | case 'subject': |
||
253 | $value = implode(" ", $values); |
||
254 | break; |
||
255 | default: |
||
256 | if (is_array($values)) { |
||
257 | foreach($values as $k => $v) { |
||
258 | if ($v == "") { |
||
259 | unset($values[$k]); |
||
260 | } |
||
261 | } |
||
262 | $available_values = count($values); |
||
263 | if ($available_values === 1) { |
||
264 | $value = array_pop($values); |
||
265 | } elseif ($available_values === 2) { |
||
266 | $value = implode(" ", $values); |
||
267 | } elseif ($available_values > 2) { |
||
268 | $value = array_values($values); |
||
269 | } else { |
||
270 | $value = ""; |
||
271 | } |
||
272 | } |
||
273 | break; |
||
274 | } |
||
275 | $headers[$key] = $value; |
||
276 | } |
||
277 | |||
278 | return (object) array_merge($headers, $imap_headers); |
||
279 | } |
||
280 | |||
281 | /** |
||
282 | * Decode MIME header elements |
||
283 | * @link https://php.net/manual/en/function.imap-mime-header-decode.php |
||
284 | * @param string $text The MIME text |
||
285 | * |
||
286 | * @return array The decoded elements are returned in an array of objects, where each |
||
287 | * object has two properties, charset and text. |
||
288 | */ |
||
289 | public function mime_header_decode($text){ |
||
290 | if (extension_loaded('imap')) { |
||
291 | return \imap_mime_header_decode($text); |
||
292 | } |
||
293 | $charset = $this->getEncoding($text); |
||
294 | return [(object)[ |
||
295 | "charset" => $charset, |
||
296 | "text" => $this->convertEncoding($text, $charset) |
||
297 | ]]; |
||
298 | } |
||
299 | |||
300 | /** |
||
301 | * Check if a given pair of strings has ben decoded |
||
302 | * @param $encoded |
||
303 | * @param $decoded |
||
304 | * |
||
305 | * @return bool |
||
306 | */ |
||
307 | private function notDecoded($encoded, $decoded) { |
||
308 | return 0 === strpos($decoded, '=?') |
||
309 | && strlen($decoded) - 2 === strpos($decoded, '?=') |
||
310 | && false !== strpos($encoded, $decoded); |
||
311 | } |
||
312 | |||
313 | /** |
||
314 | * Convert the encoding |
||
315 | * @param $str |
||
316 | * @param string $from |
||
317 | * @param string $to |
||
318 | * |
||
319 | * @return mixed|string |
||
320 | */ |
||
321 | public function convertEncoding($str, $from = "ISO-8859-2", $to = "UTF-8") { |
||
322 | |||
323 | $from = EncodingAliases::get($from, $this->fallback_encoding); |
||
324 | $to = EncodingAliases::get($to, $this->fallback_encoding); |
||
325 | |||
326 | if ($from === $to) { |
||
327 | return $str; |
||
328 | } |
||
329 | |||
330 | // We don't need to do convertEncoding() if charset is ASCII (us-ascii): |
||
331 | // ASCII is a subset of UTF-8, so all ASCII files are already UTF-8 encoded |
||
332 | // https://stackoverflow.com/a/11303410 |
||
333 | // |
||
334 | // us-ascii is the same as ASCII: |
||
335 | // ASCII is the traditional name for the encoding system; the Internet Assigned Numbers Authority (IANA) |
||
336 | // prefers the updated name US-ASCII, which clarifies that this system was developed in the US and |
||
337 | // based on the typographical symbols predominantly in use there. |
||
338 | // https://en.wikipedia.org/wiki/ASCII |
||
339 | // |
||
340 | // convertEncoding() function basically means convertToUtf8(), so when we convert ASCII string into UTF-8 it gets broken. |
||
341 | if (strtolower($from) == 'us-ascii' && $to == 'UTF-8') { |
||
342 | return $str; |
||
343 | } |
||
344 | |||
345 | try { |
||
346 | if (function_exists('iconv') && $from != 'UTF-7' && $to != 'UTF-7') { |
||
347 | return iconv($from, $to, $str); |
||
348 | } else { |
||
349 | if (!$from) { |
||
350 | return mb_convert_encoding($str, $to); |
||
351 | } |
||
352 | return mb_convert_encoding($str, $to, $from); |
||
353 | } |
||
354 | } catch (\Exception $e) { |
||
355 | if (strstr($from, '-')) { |
||
356 | $from = str_replace('-', '', $from); |
||
357 | return $this->convertEncoding($str, $from, $to); |
||
358 | } else { |
||
359 | return $str; |
||
360 | } |
||
361 | } |
||
362 | } |
||
363 | |||
364 | /** |
||
365 | * Get the encoding of a given abject |
||
366 | * @param object|string $structure |
||
367 | * |
||
368 | * @return string |
||
369 | */ |
||
370 | public function getEncoding($structure) { |
||
371 | if (property_exists($structure, 'parameters')) { |
||
372 | foreach ($structure->parameters as $parameter) { |
||
373 | if (strtolower($parameter->attribute) == "charset") { |
||
374 | return EncodingAliases::get($parameter->value, $this->fallback_encoding); |
||
375 | } |
||
376 | } |
||
377 | }elseif (property_exists($structure, 'charset')) { |
||
378 | return EncodingAliases::get($structure->charset, $this->fallback_encoding); |
||
379 | }elseif (is_string($structure) === true){ |
||
380 | return mb_detect_encoding($structure); |
||
381 | } |
||
382 | |||
383 | return $this->fallback_encoding; |
||
384 | } |
||
385 | |||
386 | /** |
||
387 | * Try to decode a specific header |
||
388 | * @param mixed $value |
||
389 | * |
||
390 | * @return mixed |
||
391 | */ |
||
392 | private function decode($value) { |
||
393 | if (is_array($value)) { |
||
394 | return $this->decodeArray($value); |
||
395 | } |
||
396 | $original_value = $value; |
||
397 | $decoder = $this->config['decoder']['message']; |
||
398 | |||
399 | if ($value !== null) { |
||
400 | if($decoder === 'utf-8' && extension_loaded('imap')) { |
||
401 | $value = \imap_utf8($value); |
||
402 | if (strpos(strtolower($value), '=?utf-8?') === 0) { |
||
403 | $value = mb_decode_mimeheader($value); |
||
404 | } |
||
405 | if ($this->notDecoded($original_value, $value)) { |
||
406 | $decoded_value = $this->mime_header_decode($value); |
||
407 | if (count($decoded_value) > 0) { |
||
408 | if(property_exists($decoded_value[0], "text")) { |
||
409 | $value = $decoded_value[0]->text; |
||
410 | } |
||
411 | } |
||
412 | } |
||
413 | }elseif($decoder === 'iconv') { |
||
414 | $value = iconv_mime_decode($value); |
||
415 | }else{ |
||
416 | $value = mb_decode_mimeheader($value); |
||
417 | } |
||
418 | |||
419 | if (strpos(strtolower($value), '=?utf-8?') === 0) { |
||
420 | $value = mb_decode_mimeheader($value); |
||
421 | } |
||
422 | |||
423 | if ($this->notDecoded($original_value, $value)) { |
||
424 | $value = $this->convertEncoding($original_value, $this->getEncoding($original_value)); |
||
425 | } |
||
426 | } |
||
427 | |||
428 | return $value; |
||
429 | } |
||
430 | |||
431 | /** |
||
432 | * Decode a given array |
||
433 | * @param array $values |
||
434 | * |
||
435 | * @return array |
||
436 | */ |
||
437 | private function decodeArray($values) { |
||
438 | foreach($values as $key => $value) { |
||
439 | $values[$key] = $this->decode($value); |
||
440 | } |
||
441 | return $values; |
||
442 | } |
||
443 | |||
444 | /** |
||
445 | * Try to extract the priority from a given raw header string |
||
446 | */ |
||
447 | private function findPriority() { |
||
471 | } |
||
472 | |||
473 | /** |
||
474 | * Extract a given part as address array from a given header |
||
475 | * @param $values |
||
476 | * |
||
477 | * @return array |
||
478 | */ |
||
479 | private function decodeAddresses($values) { |
||
502 | } |
||
503 | |||
504 | /** |
||
505 | * Extract a given part as address array from a given header |
||
506 | * @param object $header |
||
507 | */ |
||
508 | private function extractAddresses($header) { |
||
509 | foreach(['from', 'to', 'cc', 'bcc', 'reply_to', 'sender'] as $key){ |
||
510 | if (property_exists($header, $key)) { |
||
511 | $this->set($key, $this->parseAddresses($header->$key)); |
||
512 | } |
||
513 | } |
||
514 | } |
||
515 | |||
516 | /** |
||
517 | * Parse Addresses |
||
518 | * @param $list |
||
519 | * |
||
520 | * @return array |
||
521 | */ |
||
522 | private function parseAddresses($list) { |
||
562 | } |
||
563 | |||
564 | /** |
||
565 | * Search and extract potential header extensions |
||
566 | */ |
||
567 | private function extractHeaderExtensions(){ |
||
589 | } |
||
590 | } |
||
591 | } |
||
592 | } |
||
593 | } |
||
594 | } |
||
595 | } |
||
596 | |||
597 | /** |
||
598 | * Exception handling for invalid dates |
||
599 | * |
||
600 | * Currently known invalid formats: |
||
601 | * ^ Datetime ^ Problem ^ Cause |
||
602 | * | Mon, 20 Nov 2017 20:31:31 +0800 (GMT+8:00) | Double timezone specification | A Windows feature |
||
603 | * | Thu, 8 Nov 2018 08:54:58 -0200 (-02) | |
||
604 | * | | and invalid timezone (max 6 char) | |
||
605 | * | 04 Jan 2018 10:12:47 UT | Missing letter "C" | Unknown |
||
606 | * | Thu, 31 May 2018 18:15:00 +0800 (added by) | Non-standard details added by the | Unknown |
||
607 | * | | mail server | |
||
608 | * | Sat, 31 Aug 2013 20:08:23 +0580 | Invalid timezone | PHPMailer bug https://sourceforge.net/p/phpmailer/mailman/message/6132703/ |
||
609 | * |
||
610 | * Please report any new invalid timestamps to [#45](https://github.com/Webklex/php-imap/issues) |
||
611 | * |
||
612 | * @param object $header |
||
613 | * |
||
614 | * @throws InvalidMessageDateException |
||
615 | */ |
||
616 | private function parseDate($header) { |
||
653 | } |
||
654 | } |
||
655 | |||
656 | /** |
||
657 | * Get all available attributes |
||
658 | * |
||
659 | * @return array |
||
660 | */ |
||
661 | public function getAttributes() { |
||
663 | } |
||
664 | |||
666 |