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