| Total Complexity | 109 |
| Total Lines | 682 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Mail_mimeDecode 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 Mail_mimeDecode, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 69 | class Mail_mimeDecode extends PEAR |
||
| 70 | { |
||
| 71 | /** |
||
| 72 | * The raw email to decode |
||
| 73 | * @var string |
||
| 74 | */ |
||
| 75 | public $_input; |
||
| 76 | /** |
||
| 77 | * The header part of the input |
||
| 78 | * @var string |
||
| 79 | */ |
||
| 80 | public $_header; |
||
| 81 | /** |
||
| 82 | * The body part of the input |
||
| 83 | * @var string |
||
| 84 | */ |
||
| 85 | public $_body; |
||
| 86 | /** |
||
| 87 | * If an error occurs, this is used to store the message |
||
| 88 | * @var string |
||
| 89 | */ |
||
| 90 | public $_error; |
||
| 91 | /** |
||
| 92 | * Flag to determine whether to include bodies in the |
||
| 93 | * returned object. |
||
| 94 | * @var bool |
||
| 95 | */ |
||
| 96 | public $_include_bodies; |
||
| 97 | /** |
||
| 98 | * Flag to determine whether to decode bodies |
||
| 99 | * @var bool |
||
| 100 | */ |
||
| 101 | public $_decode_bodies; |
||
| 102 | /** |
||
| 103 | * Flag to determine whether to decode headers |
||
| 104 | * @var bool |
||
| 105 | */ |
||
| 106 | public $_decode_headers; |
||
| 107 | /** |
||
| 108 | * If invoked from a class, $this will be set. This has problematic |
||
| 109 | * connotations for calling decode() statically. Hence this variable |
||
| 110 | * is used to determine if we are indeed being called statically or |
||
| 111 | * via an object. |
||
| 112 | */ |
||
| 113 | public $mailMimeDecode; |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Constructor. |
||
| 117 | * |
||
| 118 | * Sets up the object, initialise the variables, and splits and |
||
| 119 | * stores the header and body of the input. |
||
| 120 | * |
||
| 121 | * @param mixed $input |
||
| 122 | */ |
||
| 123 | public function __construct($input) |
||
| 134 | } |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Begins the decoding process. If called statically |
||
| 138 | * it will create an object and call the decode() method |
||
| 139 | * of it. |
||
| 140 | * |
||
| 141 | * @param null|mixed $params |
||
| 142 | * @return object Decoded results |
||
| 143 | */ |
||
| 144 | public function decode($params = null) |
||
| 145 | { |
||
| 146 | // Have we been called statically? If so, create an object and pass details to that. |
||
| 147 | if (!isset($this->mailMimeDecode) && isset($params['input'])) { |
||
| 148 | $obj = new self($params['input']); |
||
| 149 | $structure = $obj->decode($params); |
||
| 150 | // Called statically but no input |
||
| 151 | } elseif (isset($this->mailMimeDecode)) { |
||
| 152 | $this->_include_bodies = $params['include_bodies'] ?? false; |
||
| 153 | $this->_decode_bodies = $params['decode_bodies'] ?? false; |
||
| 154 | $this->_decode_headers = $params['decode_headers'] ?? false; |
||
| 155 | |||
| 156 | $structure = $this->_decode($this->_header, $this->_body); |
||
|
|
|||
| 157 | if (false === $structure) { |
||
| 158 | $structure = $this->raiseError($this->_error); |
||
| 159 | } |
||
| 160 | } else { |
||
| 161 | return PEAR::raiseError('Called statically and no input given'); |
||
| 162 | // Called via an object |
||
| 163 | } |
||
| 164 | |||
| 165 | return $structure; |
||
| 166 | } |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Performs the decoding. Decodes the body string passed to it |
||
| 170 | * If it finds certain content-types it will call itself in a |
||
| 171 | * recursive fashion |
||
| 172 | * |
||
| 173 | * @param array $headers |
||
| 174 | * @param string $body |
||
| 175 | * @param string $default_ctype |
||
| 176 | * @return object|false Results of decoding process |
||
| 177 | */ |
||
| 178 | public function _decode(array $headers, string $body, string $default_ctype = 'text/plain') |
||
| 283 | } |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Given the output of the above function, this will return an |
||
| 287 | * array of references to the parts, indexed by mime number. |
||
| 288 | * |
||
| 289 | * @param object $structure The structure to go through |
||
| 290 | * @param bool $no_refs |
||
| 291 | * @param string $mime_number Internal use only. |
||
| 292 | * @param string $prepend |
||
| 293 | * @return array Mime numbers |
||
| 294 | */ |
||
| 295 | public function &getMimeNumbers(object $structure, bool $no_refs, string $mime_number = '', string $prepend = ''): array |
||
| 296 | { |
||
| 297 | $return = []; |
||
| 298 | if (!empty($structure->parts)) { |
||
| 299 | if ('' != $mime_number) { |
||
| 300 | $structure->mime_id = $prepend . $mime_number; |
||
| 301 | $return[$prepend . $mime_number] = &$structure; |
||
| 302 | } |
||
| 303 | foreach ($structure->parts as $i => $iValue) { |
||
| 304 | if (!empty($structure->headers['content-type']) && 0 === mb_strpos(mb_strtolower($structure->headers['content-type']), 'message/')) { |
||
| 305 | $prepend .= $mime_number . '.'; |
||
| 306 | $_mime_number = ''; |
||
| 307 | } else { |
||
| 308 | $_mime_number = ('' == $mime_number ? $i + 1 : sprintf('%s.%s', $mime_number, $i + 1)); |
||
| 309 | } |
||
| 310 | |||
| 311 | $arr = &self::getMimeNumbers($structure->parts[$i], $no_refs, $_mime_number, $prepend); |
||
| 312 | foreach ($arr as $key => $val) { |
||
| 313 | $no_refs ? $return[$key] = '' : $return[$key] = &$val; |
||
| 314 | } |
||
| 315 | } |
||
| 316 | } else { |
||
| 317 | if ('' === $mime_number) { |
||
| 318 | $mime_number = '1'; |
||
| 319 | } |
||
| 320 | $structure->mime_id = $prepend . $mime_number; |
||
| 321 | $no_refs ? $return[$prepend . $mime_number] = '' : $return[$prepend . $mime_number] = &$structure; |
||
| 322 | } |
||
| 323 | |||
| 324 | return $return; |
||
| 325 | } |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Given a string containing a header and body |
||
| 329 | * section, this function will split them (at the first |
||
| 330 | * blank line) and return them. |
||
| 331 | * |
||
| 332 | * @param mixed $input |
||
| 333 | * @return array|false Contains header and body section |
||
| 334 | */ |
||
| 335 | public function _splitBodyHeader($input) |
||
| 336 | { |
||
| 337 | if (preg_match("/^(.*?)\r?\n\r?\n(.*)/s", $input, $match)) { |
||
| 338 | return [$match[1], $match[2]]; |
||
| 339 | } |
||
| 340 | $this->_error = 'Could not split header and body'; |
||
| 341 | |||
| 342 | return false; |
||
| 343 | } |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Parse headers given in $input and return |
||
| 347 | * as assoc array. |
||
| 348 | * |
||
| 349 | * @param mixed $input |
||
| 350 | * @return array Contains parsed headers |
||
| 351 | */ |
||
| 352 | public function _parseHeaders($input): array |
||
| 377 | } |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Function to parse a header value, |
||
| 381 | * extract first part, and any secondary |
||
| 382 | * parts (after ;) This function is not as |
||
| 383 | * robust as it could be. Eg. header comments |
||
| 384 | * in the wrong place will probably break it. |
||
| 385 | * |
||
| 386 | * @param mixed $input |
||
| 387 | * @return array Contains parsed result |
||
| 388 | */ |
||
| 389 | public function _parseHeaderValue($input): array |
||
| 390 | { |
||
| 391 | if (false !== ($pos = mb_strpos($input, ';'))) { |
||
| 392 | $return['value'] = trim(mb_substr($input, 0, $pos)); |
||
| 393 | $input = trim(mb_substr($input, $pos + 1)); |
||
| 394 | |||
| 395 | if ('' !== $input) { |
||
| 396 | // This splits on a semi-colon, if there's no preceeding backslash |
||
| 397 | // Can't handle if it's in double quotes however. (Of course anyone |
||
| 398 | // sending that needs a good slap). |
||
| 399 | $parameters = preg_split('/\s*(?<!\\\\);\s*/i', $input); |
||
| 400 | |||
| 401 | foreach ($parameters as $i => $iValue) { |
||
| 402 | $param_name = mb_substr($iValue, 0, $pos = mb_strpos($iValue, '=')); |
||
| 403 | $param_value = mb_substr($iValue, $pos + 1); |
||
| 404 | if ('"' === $param_value[0]) { |
||
| 405 | $param_value = mb_substr($param_value, 1, -1); |
||
| 406 | } |
||
| 407 | $return['other'][$param_name] = $param_value; |
||
| 408 | $return['other'][mb_strtolower($param_name)] = $param_value; |
||
| 409 | } |
||
| 410 | } |
||
| 411 | } else { |
||
| 412 | $return['value'] = trim($input); |
||
| 413 | } |
||
| 414 | |||
| 415 | return $return; |
||
| 416 | } |
||
| 417 | |||
| 418 | /** |
||
| 419 | * This function splits the input based |
||
| 420 | * on the given boundary |
||
| 421 | * |
||
| 422 | * @param string $input |
||
| 423 | * @param string $boundary |
||
| 424 | * @return array Contains array of resulting mime parts |
||
| 425 | */ |
||
| 426 | public function _boundarySplit(string $input, string $boundary): array |
||
| 427 | { |
||
| 428 | $tmp = explode('--' . $boundary, $input); |
||
| 429 | |||
| 430 | for ($i = 1; $i < count($tmp) - 1; ++$i) { |
||
| 431 | $parts[] = $tmp[$i]; |
||
| 432 | } |
||
| 433 | |||
| 434 | return $parts; |
||
| 435 | } |
||
| 436 | |||
| 437 | /** |
||
| 438 | * Given a header, this function will decode it |
||
| 439 | * according to RFC2047. Probably not *exactly* |
||
| 440 | * conformant, but it does pass all the given |
||
| 441 | * examples (in RFC2047). |
||
| 442 | * |
||
| 443 | * @param mixed $input |
||
| 444 | * @return string Decoded header value |
||
| 445 | */ |
||
| 446 | public function _decodeHeader($input): string |
||
| 475 | } |
||
| 476 | |||
| 477 | /** |
||
| 478 | * Given a body string and an encoding type, |
||
| 479 | * this function will decode and return it. |
||
| 480 | * |
||
| 481 | * @param mixed $input |
||
| 482 | * @param mixed $encoding |
||
| 483 | * @return string Decoded body |
||
| 484 | */ |
||
| 485 | public function _decodeBody($input, $encoding = '7bit'): string |
||
| 486 | { |
||
| 487 | switch ($encoding) { |
||
| 488 | case '7bit': |
||
| 489 | return $input; |
||
| 490 | case 'quoted-printable': |
||
| 491 | return $this->_quotedPrintableDecode($input); |
||
| 492 | case 'base64': |
||
| 493 | return base64_decode($input, true); |
||
| 494 | default: |
||
| 495 | return $input; |
||
| 496 | } |
||
| 497 | } |
||
| 498 | |||
| 499 | /** |
||
| 500 | * Given a quoted-printable string, this |
||
| 501 | * function will decode and return it. |
||
| 502 | * |
||
| 503 | * @param mixed $input |
||
| 504 | * @return string Decoded body |
||
| 505 | */ |
||
| 506 | public function _quotedPrintableDecode($input): string |
||
| 507 | { |
||
| 508 | // Remove soft line breaks |
||
| 509 | $input = preg_replace("/=\r?\n/", '', $input); |
||
| 510 | |||
| 511 | // Replace encoded characters |
||
| 512 | $input = preg_replace('/=([a-f0-9]{2})/ie', "chr(hexdec('\\1'))", $input); |
||
| 513 | |||
| 514 | return $input; |
||
| 515 | } |
||
| 516 | |||
| 517 | /** |
||
| 518 | * Checks the input for uuencoded files and returns |
||
| 519 | * an array of them. Can be called statically, eg: |
||
| 520 | * |
||
| 521 | * $files =& Mail_mimeDecode::uudecode($some_text); |
||
| 522 | * |
||
| 523 | * It will check for the begin 666 ... end syntax |
||
| 524 | * however and won't just blindly decode whatever you |
||
| 525 | * pass it. |
||
| 526 | * |
||
| 527 | * @param mixed $input |
||
| 528 | * @return array Decoded bodies, filenames and permissions |
||
| 529 | * @author Unknown |
||
| 530 | */ |
||
| 531 | public function &uudecode($input): array |
||
| 532 | { |
||
| 533 | // Find all uuencoded sections |
||
| 534 | preg_match_all("/begin ([0-7]{3}) (.+)\r?\n(.+)\r?\nend/Us", $input, $matches); |
||
| 535 | |||
| 536 | for ($j = 0, $jMax = count($matches[3]); $j < $jMax; $j++) { |
||
| 537 | $str = $matches[3][$j]; |
||
| 538 | $filename = $matches[2][$j]; |
||
| 539 | $fileperm = $matches[1][$j]; |
||
| 540 | |||
| 541 | $file = ''; |
||
| 542 | $str = preg_split("/\r?\n/", trim($str)); |
||
| 543 | |||
| 544 | foreach ($str as $i => $iValue) { |
||
| 545 | $pos = 1; |
||
| 546 | $d = 0; |
||
| 547 | $len = (((ord(mb_substr($iValue, 0, 1)) - 32) - ' ') & 077); |
||
| 548 | |||
| 549 | while (($d + 3 <= $len) and ($pos + 4 <= mb_strlen($iValue))) { |
||
| 550 | $c0 = (ord(mb_substr($iValue, $pos, 1)) ^ 0x20); |
||
| 551 | $c1 = (ord(mb_substr($iValue, $pos + 1, 1)) ^ 0x20); |
||
| 552 | $c2 = (ord(mb_substr($iValue, $pos + 2, 1)) ^ 0x20); |
||
| 553 | $c3 = (ord(mb_substr($iValue, $pos + 3, 1)) ^ 0x20); |
||
| 554 | $file .= chr(((($c0 - ' ') & 077) << 2) | ((($c1 - ' ') & 077) >> 4)); |
||
| 555 | |||
| 556 | $file .= chr(((($c1 - ' ') & 077) << 4) | ((($c2 - ' ') & 077) >> 2)); |
||
| 557 | |||
| 558 | $file .= chr(((($c2 - ' ') & 077) << 6) | (($c3 - ' ') & 077)); |
||
| 559 | |||
| 560 | $pos += 4; |
||
| 561 | $d += 3; |
||
| 562 | } |
||
| 563 | |||
| 564 | if (($d + 2 <= $len) && ($pos + 3 <= mb_strlen($iValue))) { |
||
| 565 | $c0 = (ord(mb_substr($iValue, $pos, 1)) ^ 0x20); |
||
| 566 | $c1 = (ord(mb_substr($iValue, $pos + 1, 1)) ^ 0x20); |
||
| 567 | $c2 = (ord(mb_substr($iValue, $pos + 2, 1)) ^ 0x20); |
||
| 568 | $file .= chr(((($c0 - ' ') & 077) << 2) | ((($c1 - ' ') & 077) >> 4)); |
||
| 569 | |||
| 570 | $file .= chr(((($c1 - ' ') & 077) << 4) | ((($c2 - ' ') & 077) >> 2)); |
||
| 571 | |||
| 572 | $pos += 3; |
||
| 573 | $d += 2; |
||
| 574 | } |
||
| 575 | |||
| 576 | if (($d + 1 <= $len) && ($pos + 2 <= mb_strlen($iValue))) { |
||
| 577 | $c0 = (ord(mb_substr($iValue, $pos, 1)) ^ 0x20); |
||
| 578 | $c1 = (ord(mb_substr($iValue, $pos + 1, 1)) ^ 0x20); |
||
| 579 | $file .= chr(((($c0 - ' ') & 077) << 2) | ((($c1 - ' ') & 077) >> 4)); |
||
| 580 | } |
||
| 581 | } |
||
| 582 | $files[] = ['filename' => $filename, 'fileperm' => $fileperm, 'filedata' => $file]; |
||
| 583 | } |
||
| 584 | |||
| 585 | return $files; |
||
| 586 | } |
||
| 587 | |||
| 588 | /** |
||
| 589 | * getSendArray() returns the arguments required for Mail::send() |
||
| 590 | * used to build the arguments for a mail::send() call |
||
| 591 | * |
||
| 592 | * Usage: |
||
| 593 | * $mailtext = Full email (for example generated by a template) |
||
| 594 | * $decoder = new Mail_mimeDecode($mailtext); |
||
| 595 | * $parts = $decoder->getSendArray(); |
||
| 596 | * if (!PEAR::isError($parts) { |
||
| 597 | * list($recipents,$headers,$body) = $parts; |
||
| 598 | * $mail = Mail::factory('smtp'); |
||
| 599 | * $mail->send($recipents,$headers,$body); |
||
| 600 | * } else { |
||
| 601 | * echo $parts->message; |
||
| 602 | * } |
||
| 603 | * @return array|\PEAR_Error array of recipeint, headers,body or Pear_Error |
||
| 604 | * @author Alan Knowles <[email protected]> |
||
| 605 | */ |
||
| 606 | public function getSendArray() |
||
| 607 | { |
||
| 608 | // prevent warning if this is not set |
||
| 609 | $this->_decode_headers = false; |
||
| 610 | $headerlist = $this->_parseHeaders($this->_header); |
||
| 611 | $to = ''; |
||
| 612 | if (!$headerlist) { |
||
| 613 | return $this->raiseError('Message did not contain headers'); |
||
| 614 | } |
||
| 615 | foreach ($headerlist as $item) { |
||
| 616 | $header[$item['name']] = $item['value']; |
||
| 617 | switch (mb_strtolower($item['name'])) { |
||
| 618 | case 'to': |
||
| 619 | case 'cc': |
||
| 620 | case 'bcc': |
||
| 621 | $to = ',' . $item['value']; |
||
| 622 | // no break |
||
| 623 | default: |
||
| 624 | break; |
||
| 625 | } |
||
| 626 | } |
||
| 627 | if ('' === $to) { |
||
| 628 | return $this->raiseError('Message did not contain any recipents'); |
||
| 629 | } |
||
| 630 | $to = mb_substr($to, 1); |
||
| 631 | |||
| 632 | return [$to, $header, $this->_body]; |
||
| 633 | } |
||
| 634 | |||
| 635 | /** |
||
| 636 | * Returns a xml copy of the output of |
||
| 637 | * Mail_mimeDecode::decode. Pass the output in as the |
||
| 638 | * argument. This function can be called statically. Eg: |
||
| 639 | * |
||
| 640 | * $output = $obj->decode(); |
||
| 641 | * $xml = Mail_mimeDecode::getXML($output); |
||
| 642 | * |
||
| 643 | * The DTD used for this should have been in the package. Or |
||
| 644 | * alternatively you can get it from cvs, or here: |
||
| 645 | * https://www.phpguru.org/xmail/xmail.dtd. |
||
| 646 | * |
||
| 647 | * @param mixed $input |
||
| 648 | * @return string XML version of input |
||
| 649 | */ |
||
| 650 | public function getXML($input): string |
||
| 651 | { |
||
| 652 | $crlf = "\r\n"; |
||
| 653 | $output = '<?xml version=\'1.0\'?>' . $crlf . '<!DOCTYPE email SYSTEM "https://www.phpguru.org/xmail/xmail.dtd">' . $crlf . '<email>' . $crlf . self::_getXML($input) . '</email>'; |
||
| 654 | |||
| 655 | return $output; |
||
| 656 | } |
||
| 657 | |||
| 658 | /** |
||
| 659 | * Function that does the actual conversion to xml. Does a single |
||
| 660 | * mimepart at a time. |
||
| 661 | * |
||
| 662 | * @param mixed $input |
||
| 663 | * @param mixed $indent |
||
| 664 | * @return string XML version of input |
||
| 665 | */ |
||
| 666 | public function _getXML($input, $indent = 1): string |
||
| 694 | } |
||
| 695 | |||
| 696 | /** |
||
| 697 | * Helper function to _getXML(). Returns xml of a header. |
||
| 698 | * |
||
| 699 | * @param mixed $hdr_name |
||
| 700 | * @param mixed $hdr_value |
||
| 701 | * @param mixed $indent |
||
| 702 | * @return string XML version of input |
||
| 703 | */ |
||
| 704 | public function _getXML_helper($hdr_name, $hdr_value, $indent): string |
||
| 751 | } |
||
| 752 | } // End of class |
||
| 753 |