Decode.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <?php // phpcs:disable WebimpressCodingStandard.NamingConventions.ValidVariableName.NotCamelCaps
  2. namespace Laminas\Mime;
  3. use Laminas\Mail\Headers;
  4. use Laminas\Stdlib\ErrorHandler;
  5. use function count;
  6. use function explode;
  7. use function iconv_mime_decode;
  8. use function preg_match;
  9. use function preg_match_all;
  10. use function preg_split;
  11. use function str_replace;
  12. use function strcasecmp;
  13. use function strlen;
  14. use function strpos;
  15. use function strtok;
  16. use function strtolower;
  17. use function substr;
  18. use const E_NOTICE;
  19. use const E_WARNING;
  20. use const ICONV_MIME_DECODE_CONTINUE_ON_ERROR;
  21. class Decode
  22. {
  23. /**
  24. * Explode MIME multipart string into separate parts
  25. *
  26. * Parts consist of the header and the body of each MIME part.
  27. *
  28. * @param string $body raw body of message
  29. * @param string $boundary boundary as found in content-type
  30. * @return array parts with content of each part, empty if no parts found
  31. * @throws Exception\RuntimeException
  32. */
  33. public static function splitMime($body, $boundary)
  34. {
  35. // TODO: we're ignoring \r for now - is this function fast enough and is it safe to assume noone needs \r?
  36. $body = str_replace("\r", '', $body);
  37. $start = 0;
  38. $res = [];
  39. // find every mime part limiter and cut out the
  40. // string before it.
  41. // the part before the first boundary string is discarded:
  42. $p = strpos($body, '--' . $boundary . "\n", $start);
  43. if ($p === false) {
  44. // no parts found!
  45. return [];
  46. }
  47. // position after first boundary line
  48. $start = $p + 3 + strlen($boundary);
  49. while (($p = strpos($body, '--' . $boundary . "\n", $start)) !== false) {
  50. $res[] = substr($body, $start, $p - $start);
  51. $start = $p + 3 + strlen($boundary);
  52. }
  53. // no more parts, find end boundary
  54. $p = strpos($body, '--' . $boundary . '--', $start);
  55. if ($p === false) {
  56. throw new Exception\RuntimeException('Not a valid Mime Message: End Missing');
  57. }
  58. // the remaining part also needs to be parsed:
  59. $res[] = substr($body, $start, $p - $start);
  60. return $res;
  61. }
  62. /**
  63. * decodes a mime encoded String and returns a
  64. * struct of parts with header and body
  65. *
  66. * @param string $message raw message content
  67. * @param string $boundary boundary as found in content-type
  68. * @param string $EOL EOL string; defaults to {@link Laminas\Mime\Mime::LINEEND}
  69. * @return array|null parts as array('header' => array(name => value), 'body' => content), null if no parts found
  70. * @throws Exception\RuntimeException
  71. */
  72. public static function splitMessageStruct($message, $boundary, $EOL = Mime::LINEEND)
  73. {
  74. $parts = static::splitMime($message, $boundary);
  75. if (! $parts) {
  76. return;
  77. }
  78. $result = [];
  79. $headers = null; // "Declare" variable before the first usage "for reading"
  80. $body = null; // "Declare" variable before the first usage "for reading"
  81. foreach ($parts as $part) {
  82. static::splitMessage($part, $headers, $body, $EOL);
  83. $result[] = [
  84. 'header' => $headers,
  85. 'body' => $body,
  86. ];
  87. }
  88. return $result;
  89. }
  90. /**
  91. * split a message in header and body part, if no header or an
  92. * invalid header is found $headers is empty
  93. *
  94. * The charset of the returned headers depend on your iconv settings.
  95. *
  96. * @param string|Headers $message raw message with header and optional content
  97. * @param Headers $headers output param, headers container
  98. * @param string $body output param, content of message
  99. * @param string $EOL EOL string; defaults to {@link Laminas\Mime\Mime::LINEEND}
  100. * @param bool $strict enable strict mode for parsing message
  101. * @return null
  102. */
  103. public static function splitMessage($message, &$headers, &$body, $EOL = Mime::LINEEND, $strict = false)
  104. {
  105. if ($message instanceof Headers) {
  106. $message = $message->toString();
  107. }
  108. // check for valid header at first line
  109. $firstlinePos = strpos($message, "\n");
  110. $firstline = $firstlinePos === false ? $message : substr($message, 0, $firstlinePos);
  111. if (! preg_match('%^[^\s]+[^:]*:%', $firstline)) {
  112. $headers = new Headers();
  113. // TODO: we're ignoring \r for now - is this function fast enough and is it safe to assume noone needs \r?
  114. $body = str_replace(["\r", "\n"], ['', $EOL], $message);
  115. return;
  116. }
  117. // see @Laminas-372, pops the first line off a message if it doesn't contain a header
  118. if (! $strict) {
  119. $parts = explode(':', $firstline, 2);
  120. if (count($parts) !== 2) {
  121. $message = substr($message, strpos($message, $EOL) + 1);
  122. }
  123. }
  124. // @todo splitMime removes "\r" sequences, which breaks valid mime
  125. // messages as returned by many mail servers
  126. $headersEOL = $EOL;
  127. // find an empty line between headers and body
  128. // default is set new line
  129. // @todo Maybe this is too much "magic"; we should be more strict here
  130. if (strpos($message, $EOL . $EOL)) {
  131. [$headers, $body] = explode($EOL . $EOL, $message, 2);
  132. // next is the standard new line
  133. } elseif ($EOL !== "\r\n" && strpos($message, "\r\n\r\n")) {
  134. [$headers, $body] = explode("\r\n\r\n", $message, 2);
  135. $headersEOL = "\r\n"; // Headers::fromString will fail with incorrect EOL
  136. // next is the other "standard" new line
  137. } elseif ($EOL !== "\n" && strpos($message, "\n\n")) {
  138. [$headers, $body] = explode("\n\n", $message, 2);
  139. $headersEOL = "\n";
  140. // at last resort find anything that looks like a new line
  141. } else {
  142. ErrorHandler::start(E_NOTICE | E_WARNING);
  143. [$headers, $body] = preg_split("%([\r\n]+)\\1%U", $message, 2);
  144. ErrorHandler::stop();
  145. }
  146. $headers = Headers::fromString($headers, $headersEOL);
  147. }
  148. /**
  149. * split a content type in its different parts
  150. *
  151. * @param string $type content-type
  152. * @param string $wantedPart the wanted part, else an array with all parts is returned
  153. * @return string|array wanted part or all parts as array('type' => content-type, partname => value)
  154. */
  155. public static function splitContentType($type, $wantedPart = null)
  156. {
  157. return static::splitHeaderField($type, $wantedPart, 'type');
  158. }
  159. /**
  160. * split a header field like content type in its different parts
  161. *
  162. * @param string $field header field
  163. * @param string $wantedPart the wanted part, else an array with all parts is returned
  164. * @param string $firstName key name for the first part
  165. * @return string|array wanted part or all parts as array($firstName => firstPart, partname => value)
  166. * @throws Exception\RuntimeException
  167. */
  168. public static function splitHeaderField($field, $wantedPart = null, $firstName = '0')
  169. {
  170. $wantedPart = strtolower($wantedPart ?? '');
  171. $firstName = strtolower($firstName);
  172. // special case - a bit optimized
  173. if ($firstName === $wantedPart) {
  174. $field = strtok($field, ';');
  175. return $field[0] === '"' ? substr($field, 1, -1) : $field;
  176. }
  177. $field = $firstName . '=' . $field;
  178. if (! preg_match_all('%([^=\s]+)\s*=\s*("[^"]+"|[^;]+)(;\s*|$)%', $field, $matches)) {
  179. throw new Exception\RuntimeException('not a valid header field');
  180. }
  181. if ($wantedPart) {
  182. foreach ($matches[1] as $key => $name) {
  183. if (strcasecmp($name, $wantedPart)) {
  184. continue;
  185. }
  186. if ($matches[2][$key][0] !== '"') {
  187. return $matches[2][$key];
  188. }
  189. return substr($matches[2][$key], 1, -1);
  190. }
  191. return;
  192. }
  193. $split = [];
  194. foreach ($matches[1] as $key => $name) {
  195. $name = strtolower($name);
  196. if ($matches[2][$key][0] === '"') {
  197. $split[$name] = substr($matches[2][$key], 1, -1);
  198. } else {
  199. $split[$name] = $matches[2][$key];
  200. }
  201. }
  202. return $split;
  203. }
  204. /**
  205. * decode a quoted printable encoded string
  206. *
  207. * The charset of the returned string depends on your iconv settings.
  208. *
  209. * @param string $string encoded string
  210. * @return string decoded string
  211. */
  212. public static function decodeQuotedPrintable($string)
  213. {
  214. return iconv_mime_decode($string, ICONV_MIME_DECODE_CONTINUE_ON_ERROR, 'UTF-8');
  215. }
  216. }