Total Complexity | 121 |
Total Lines | 582 |
Duplicated Lines | 0 % |
Changes | 7 | ||
Bugs | 1 | Features | 2 |
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 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 | * 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 mixed |
||
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 mixed|null |
||
93 | */ |
||
94 | public function __get($name) { |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * Get a specific header attribute |
||
100 | * @param $name |
||
101 | * |
||
102 | * @return mixed|null |
||
103 | */ |
||
104 | public function get($name) { |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * Perform a regex match all on the raw header and return the first result |
||
114 | * @param $pattern |
||
115 | * |
||
116 | * @return mixed|null |
||
117 | */ |
||
118 | public function find($pattern) { |
||
127 | } |
||
128 | |||
129 | /** |
||
130 | * Parse the raw headers |
||
131 | * |
||
132 | * @throws InvalidMessageDateException |
||
133 | */ |
||
134 | protected function parse(){ |
||
135 | $header = $this->rfc822_parse_headers($this->raw); |
||
136 | |||
137 | $this->extractAddresses($header); |
||
138 | |||
139 | if (property_exists($header, 'subject')) { |
||
140 | $this->attributes["subject"] = $this->decode($header->subject); |
||
141 | } |
||
142 | if (property_exists($header, 'in_reply_to')) { |
||
143 | $this->attributes["in_reply_to"] = is_array($header->in_reply_to) ? $header->in_reply_to : [$header->in_reply_to]; |
||
144 | } |
||
145 | if (property_exists($header, 'references')) { |
||
146 | $this->attributes["references"] = $this->decode($header->references); |
||
147 | } |
||
148 | if (property_exists($header, 'message_id')) { |
||
149 | $this->attributes["message_id"] = str_replace(['<', '>'], '', $header->message_id); |
||
150 | } |
||
151 | |||
152 | $this->parseDate($header); |
||
153 | foreach ($header as $key => $value) { |
||
154 | $key = trim(rtrim(strtolower($key))); |
||
155 | if(!isset($this->attributes[$key])){ |
||
156 | $this->attributes[$key] = $value; |
||
157 | } |
||
158 | } |
||
159 | |||
160 | $this->extractHeaderExtensions(); |
||
161 | $this->findPriority(); |
||
162 | } |
||
163 | |||
164 | /** |
||
165 | * Parse mail headers from a string |
||
166 | * @link https://php.net/manual/en/function.imap-rfc822-parse-headers.php |
||
167 | * @param $raw_headers |
||
168 | * |
||
169 | * @return object |
||
170 | */ |
||
171 | public function rfc822_parse_headers($raw_headers){ |
||
172 | $headers = []; |
||
173 | $imap_headers = []; |
||
174 | if (extension_loaded('imap')) { |
||
175 | $imap_headers = (array) \imap_rfc822_parse_headers($this->raw); |
||
176 | } |
||
177 | |||
178 | $lines = explode("\r\n", $raw_headers); |
||
179 | $prev_header = null; |
||
180 | foreach($lines as $line) { |
||
181 | if (substr($line, 0, 1) === "\n") { |
||
182 | $line = substr($line, 1); |
||
183 | } |
||
184 | |||
185 | if (substr($line, 0, 1) === "\t") { |
||
186 | $line = substr($line, 1); |
||
187 | $line = trim(rtrim($line)); |
||
188 | if ($prev_header !== null) { |
||
189 | $headers[$prev_header][] = $line; |
||
190 | } |
||
191 | }elseif (substr($line, 0, 1) === " ") { |
||
192 | $line = substr($line, 1); |
||
193 | $line = trim(rtrim($line)); |
||
194 | if ($prev_header !== null) { |
||
195 | if (!isset($headers[$prev_header])) { |
||
196 | $headers[$prev_header] = ""; |
||
197 | } |
||
198 | if (is_array($headers[$prev_header])) { |
||
199 | $headers[$prev_header][] = $line; |
||
200 | }else{ |
||
201 | $headers[$prev_header] .= $line; |
||
202 | } |
||
203 | } |
||
204 | }else{ |
||
205 | if (($pos = strpos($line, ":")) > 0) { |
||
206 | $key = trim(rtrim(strtolower(substr($line, 0, $pos)))); |
||
207 | $value = trim(rtrim(strtolower(substr($line, $pos + 1)))); |
||
208 | $headers[$key] = [$value]; |
||
209 | $prev_header = $key; |
||
210 | } |
||
211 | } |
||
212 | } |
||
213 | |||
214 | foreach($headers as $key => $values) { |
||
215 | if (isset($imap_headers[$key])) continue; |
||
216 | $value = null; |
||
217 | switch($key){ |
||
218 | case 'from': |
||
219 | case 'to': |
||
220 | case 'cc': |
||
221 | case 'bcc': |
||
222 | case 'reply_to': |
||
223 | case 'sender': |
||
224 | $value = $this->decodeAddresses($values); |
||
225 | $headers[$key."address"] = implode(", ", $values); |
||
226 | break; |
||
227 | case 'subject': |
||
228 | $value = implode(" ", $values); |
||
229 | break; |
||
230 | default: |
||
231 | if (is_array($values)) { |
||
232 | foreach($values as $k => $v) { |
||
233 | if ($v == "") { |
||
234 | unset($values[$k]); |
||
235 | } |
||
236 | } |
||
237 | $available_values = count($values); |
||
238 | if ($available_values === 1) { |
||
239 | $value = array_pop($values); |
||
240 | } elseif ($available_values === 2) { |
||
241 | $value = implode(" ", $values); |
||
242 | } elseif ($available_values > 2) { |
||
243 | $value = array_values($values); |
||
244 | } else { |
||
245 | $value = ""; |
||
246 | } |
||
247 | } |
||
248 | break; |
||
249 | } |
||
250 | $headers[$key] = $value; |
||
251 | } |
||
252 | |||
253 | return (object) array_merge($headers, $imap_headers); |
||
254 | } |
||
255 | |||
256 | /** |
||
257 | * Decode MIME header elements |
||
258 | * @link https://php.net/manual/en/function.imap-mime-header-decode.php |
||
259 | * @param string $text The MIME text |
||
260 | * |
||
261 | * @return array The decoded elements are returned in an array of objects, where each |
||
262 | * object has two properties, charset and text. |
||
263 | */ |
||
264 | public function mime_header_decode($text){ |
||
265 | if (extension_loaded('imap')) { |
||
266 | return \imap_mime_header_decode($text); |
||
267 | } |
||
268 | $charset = $this->getEncoding($text); |
||
269 | return [(object)[ |
||
270 | "charset" => $charset, |
||
271 | "text" => $this->convertEncoding($text, $charset) |
||
272 | ]]; |
||
273 | } |
||
274 | |||
275 | /** |
||
276 | * Check if a given pair of strings has ben decoded |
||
277 | * @param $encoded |
||
278 | * @param $decoded |
||
279 | * |
||
280 | * @return bool |
||
281 | */ |
||
282 | private function notDecoded($encoded, $decoded) { |
||
283 | return 0 === strpos($decoded, '=?') |
||
284 | && strlen($decoded) - 2 === strpos($decoded, '?=') |
||
285 | && false !== strpos($encoded, $decoded); |
||
286 | } |
||
287 | |||
288 | /** |
||
289 | * Convert the encoding |
||
290 | * @param $str |
||
291 | * @param string $from |
||
292 | * @param string $to |
||
293 | * |
||
294 | * @return mixed|string |
||
295 | */ |
||
296 | public function convertEncoding($str, $from = "ISO-8859-2", $to = "UTF-8") { |
||
297 | |||
298 | $from = EncodingAliases::get($from, $this->fallback_encoding); |
||
299 | $to = EncodingAliases::get($to, $this->fallback_encoding); |
||
300 | |||
301 | if ($from === $to) { |
||
302 | return $str; |
||
303 | } |
||
304 | |||
305 | // We don't need to do convertEncoding() if charset is ASCII (us-ascii): |
||
306 | // ASCII is a subset of UTF-8, so all ASCII files are already UTF-8 encoded |
||
307 | // https://stackoverflow.com/a/11303410 |
||
308 | // |
||
309 | // us-ascii is the same as ASCII: |
||
310 | // ASCII is the traditional name for the encoding system; the Internet Assigned Numbers Authority (IANA) |
||
311 | // prefers the updated name US-ASCII, which clarifies that this system was developed in the US and |
||
312 | // based on the typographical symbols predominantly in use there. |
||
313 | // https://en.wikipedia.org/wiki/ASCII |
||
314 | // |
||
315 | // convertEncoding() function basically means convertToUtf8(), so when we convert ASCII string into UTF-8 it gets broken. |
||
316 | if (strtolower($from) == 'us-ascii' && $to == 'UTF-8') { |
||
317 | return $str; |
||
318 | } |
||
319 | |||
320 | try { |
||
321 | if (function_exists('iconv') && $from != 'UTF-7' && $to != 'UTF-7') { |
||
322 | return iconv($from, $to, $str); |
||
323 | } else { |
||
324 | if (!$from) { |
||
325 | return mb_convert_encoding($str, $to); |
||
326 | } |
||
327 | return mb_convert_encoding($str, $to, $from); |
||
328 | } |
||
329 | } catch (\Exception $e) { |
||
330 | if (strstr($from, '-')) { |
||
331 | $from = str_replace('-', '', $from); |
||
332 | return $this->convertEncoding($str, $from, $to); |
||
333 | } else { |
||
334 | return $str; |
||
335 | } |
||
336 | } |
||
337 | } |
||
338 | |||
339 | /** |
||
340 | * Get the encoding of a given abject |
||
341 | * @param object|string $structure |
||
342 | * |
||
343 | * @return string |
||
344 | */ |
||
345 | public function getEncoding($structure) { |
||
346 | if (property_exists($structure, 'parameters')) { |
||
347 | foreach ($structure->parameters as $parameter) { |
||
348 | if (strtolower($parameter->attribute) == "charset") { |
||
349 | return EncodingAliases::get($parameter->value, $this->fallback_encoding); |
||
350 | } |
||
351 | } |
||
352 | }elseif (property_exists($structure, 'charset')) { |
||
353 | return EncodingAliases::get($structure->charset, $this->fallback_encoding); |
||
354 | }elseif (is_string($structure) === true){ |
||
355 | return mb_detect_encoding($structure); |
||
356 | } |
||
357 | |||
358 | return $this->fallback_encoding; |
||
359 | } |
||
360 | |||
361 | /** |
||
362 | * Try to decode a specific header |
||
363 | * @param $value |
||
364 | * |
||
365 | * @return string|null |
||
366 | */ |
||
367 | private function decode($value) { |
||
368 | $original_value = $value; |
||
369 | $decoder = $this->config['decoder']['message']; |
||
370 | |||
371 | if ($value !== null) { |
||
372 | if($decoder === 'utf-8' && extension_loaded('imap')) { |
||
373 | $value = \imap_utf8($value); |
||
374 | if (strpos(strtolower($value), '=?utf-8?') === 0) { |
||
375 | $value = mb_decode_mimeheader($value); |
||
376 | } |
||
377 | if ($this->notDecoded($original_value, $value)) { |
||
378 | $decoded_value = $this->mime_header_decode($value); |
||
379 | if (count($decoded_value) > 0) { |
||
380 | if(property_exists($decoded_value[0], "text")) { |
||
381 | $value = $decoded_value[0]->text; |
||
382 | } |
||
383 | } |
||
384 | } |
||
385 | }elseif($decoder === 'iconv') { |
||
386 | $value = iconv_mime_decode($value); |
||
387 | }else{ |
||
388 | $value = mb_decode_mimeheader($value); |
||
389 | } |
||
390 | |||
391 | if (strpos(strtolower($value), '=?utf-8?') === 0) { |
||
392 | $value = mb_decode_mimeheader($value); |
||
393 | } |
||
394 | |||
395 | if ($this->notDecoded($original_value, $value)) { |
||
396 | $value = $this->convertEncoding($original_value, $this->getEncoding($original_value)); |
||
397 | } |
||
398 | } |
||
399 | |||
400 | return $value; |
||
401 | } |
||
402 | |||
403 | /** |
||
404 | * Try to extract the priority from a given raw header string |
||
405 | */ |
||
406 | private function findPriority() { |
||
430 | } |
||
431 | |||
432 | /** |
||
433 | * Extract a given part as address array from a given header |
||
434 | * @param $values |
||
435 | * |
||
436 | * @return array |
||
437 | */ |
||
438 | private function decodeAddresses($values) { |
||
457 | } |
||
458 | |||
459 | /** |
||
460 | * Extract a given part as address array from a given header |
||
461 | * @param object $header |
||
462 | */ |
||
463 | private function extractAddresses($header) { |
||
464 | foreach(['from', 'to', 'cc', 'bcc', 'reply_to', 'sender'] as $key){ |
||
465 | if (property_exists($header, $key)) { |
||
466 | $this->attributes[$key] = $this->parseAddresses($header->$key); |
||
467 | } |
||
468 | } |
||
469 | } |
||
470 | |||
471 | /** |
||
472 | * Parse Addresses |
||
473 | * @param $list |
||
474 | * |
||
475 | * @return array |
||
476 | */ |
||
477 | private function parseAddresses($list) { |
||
513 | } |
||
514 | |||
515 | /** |
||
516 | * Search and extract potential header extensions |
||
517 | */ |
||
518 | private function extractHeaderExtensions(){ |
||
534 | } |
||
535 | } |
||
536 | } |
||
537 | } |
||
538 | } |
||
539 | } |
||
540 | |||
541 | /** |
||
542 | * Exception handling for invalid dates |
||
543 | * |
||
544 | * Currently known invalid formats: |
||
545 | * ^ Datetime ^ Problem ^ Cause |
||
546 | * | Mon, 20 Nov 2017 20:31:31 +0800 (GMT+8:00) | Double timezone specification | A Windows feature |
||
547 | * | Thu, 8 Nov 2018 08:54:58 -0200 (-02) | |
||
548 | * | | and invalid timezone (max 6 char) | |
||
549 | * | 04 Jan 2018 10:12:47 UT | Missing letter "C" | Unknown |
||
550 | * | Thu, 31 May 2018 18:15:00 +0800 (added by) | Non-standard details added by the | Unknown |
||
551 | * | | mail server | |
||
552 | * | Sat, 31 Aug 2013 20:08:23 +0580 | Invalid timezone | PHPMailer bug https://sourceforge.net/p/phpmailer/mailman/message/6132703/ |
||
553 | * |
||
554 | * Please report any new invalid timestamps to [#45](https://github.com/Webklex/php-imap/issues) |
||
555 | * |
||
556 | * @param object $header |
||
557 | * |
||
558 | * @throws InvalidMessageDateException |
||
559 | */ |
||
560 | private function parseDate($header) { |
||
597 | } |
||
598 | } |
||
599 | |||
600 | /** |
||
601 | * Get all available attributes |
||
602 | * |
||
603 | * @return array |
||
604 | */ |
||
605 | public function getAttributes() { |
||
607 | } |
||
608 | |||
610 |