Message.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. <?php
  2. /**
  3. * @see https://github.com/zendframework/zend-mime for the canonical source repository
  4. * @copyright Copyright (c) 2005-2019 Zend Technologies USA Inc. (https://www.zend.com)
  5. * @license https://github.com/zendframework/zend-mime/blob/master/LICENSE.md New BSD License
  6. */
  7. namespace Zend\Mime;
  8. class Message
  9. {
  10. protected $parts = [];
  11. protected $mime = null;
  12. /**
  13. * Returns the list of all Zend\Mime\Part in the message
  14. *
  15. * @return Part[]
  16. */
  17. public function getParts()
  18. {
  19. return $this->parts;
  20. }
  21. /**
  22. * Sets the given array of Zend\Mime\Part as the array for the message
  23. *
  24. * @param array $parts
  25. * @return self
  26. */
  27. public function setParts($parts)
  28. {
  29. $this->parts = $parts;
  30. return $this;
  31. }
  32. /**
  33. * Append a new Zend\Mime\Part to the current message
  34. *
  35. * @param \Zend\Mime\Part $part
  36. * @throws Exception\InvalidArgumentException
  37. * @return self
  38. */
  39. public function addPart(Part $part)
  40. {
  41. foreach ($this->getParts() as $key => $row) {
  42. if ($part == $row) {
  43. throw new Exception\InvalidArgumentException(sprintf(
  44. 'Provided part %s already defined.',
  45. $part->getId()
  46. ));
  47. }
  48. }
  49. $this->parts[] = $part;
  50. return $this;
  51. }
  52. /**
  53. * Check if message needs to be sent as multipart
  54. * MIME message or if it has only one part.
  55. *
  56. * @return bool
  57. */
  58. public function isMultiPart()
  59. {
  60. return (count($this->parts) > 1);
  61. }
  62. /**
  63. * Set Zend\Mime\Mime object for the message
  64. *
  65. * This can be used to set the boundary specifically or to use a subclass of
  66. * Zend\Mime for generating the boundary.
  67. *
  68. * @param \Zend\Mime\Mime $mime
  69. * @return self
  70. */
  71. public function setMime(Mime $mime)
  72. {
  73. $this->mime = $mime;
  74. return $this;
  75. }
  76. /**
  77. * Returns the Zend\Mime\Mime object in use by the message
  78. *
  79. * If the object was not present, it is created and returned. Can be used to
  80. * determine the boundary used in this message.
  81. *
  82. * @return \Zend\Mime\Mime
  83. */
  84. public function getMime()
  85. {
  86. if ($this->mime === null) {
  87. $this->mime = new Mime();
  88. }
  89. return $this->mime;
  90. }
  91. /**
  92. * Generate MIME-compliant message from the current configuration
  93. *
  94. * This can be a multipart message if more than one MIME part was added. If
  95. * only one part is present, the content of this part is returned. If no
  96. * part had been added, an empty string is returned.
  97. *
  98. * Parts are separated by the mime boundary as defined in Zend\Mime\Mime. If
  99. * {@link setMime()} has been called before this method, the Zend\Mime\Mime
  100. * object set by this call will be used. Otherwise, a new Zend\Mime\Mime object
  101. * is generated and used.
  102. *
  103. * @param string $EOL EOL string; defaults to {@link Zend\Mime\Mime::LINEEND}
  104. * @return string
  105. */
  106. public function generateMessage($EOL = Mime::LINEEND)
  107. {
  108. if (! $this->isMultiPart()) {
  109. if (empty($this->parts)) {
  110. return '';
  111. }
  112. $part = current($this->parts);
  113. $body = $part->getContent($EOL);
  114. } else {
  115. $mime = $this->getMime();
  116. $boundaryLine = $mime->boundaryLine($EOL);
  117. $body = 'This is a message in Mime Format. If you see this, '
  118. . "your mail reader does not support this format." . $EOL;
  119. foreach (array_keys($this->parts) as $p) {
  120. $body .= $boundaryLine
  121. . $this->getPartHeaders($p, $EOL)
  122. . $EOL
  123. . $this->getPartContent($p, $EOL);
  124. }
  125. $body .= $mime->mimeEnd($EOL);
  126. }
  127. return trim($body);
  128. }
  129. /**
  130. * Get the headers of a given part as an array
  131. *
  132. * @param int $partnum
  133. * @return array
  134. */
  135. public function getPartHeadersArray($partnum)
  136. {
  137. return $this->parts[$partnum]->getHeadersArray();
  138. }
  139. /**
  140. * Get the headers of a given part as a string
  141. *
  142. * @param int $partnum
  143. * @param string $EOL
  144. * @return string
  145. */
  146. public function getPartHeaders($partnum, $EOL = Mime::LINEEND)
  147. {
  148. return $this->parts[$partnum]->getHeaders($EOL);
  149. }
  150. /**
  151. * Get the (encoded) content of a given part as a string
  152. *
  153. * @param int $partnum
  154. * @param string $EOL
  155. * @return string
  156. */
  157. public function getPartContent($partnum, $EOL = Mime::LINEEND)
  158. {
  159. return $this->parts[$partnum]->getContent($EOL);
  160. }
  161. /**
  162. * Explode MIME multipart string into separate parts
  163. *
  164. * Parts consist of the header and the body of each MIME part.
  165. *
  166. * @param string $body
  167. * @param string $boundary
  168. * @throws Exception\RuntimeException
  169. * @return array
  170. */
  171. // @codingStandardsIgnoreStart
  172. protected static function _disassembleMime($body, $boundary)
  173. {
  174. // @codingStandardsIgnoreEnd
  175. $start = 0;
  176. $res = [];
  177. // find every mime part limiter and cut out the
  178. // string before it.
  179. // the part before the first boundary string is discarded:
  180. $p = strpos($body, '--' . $boundary."\n", $start);
  181. if ($p === false) {
  182. // no parts found!
  183. return [];
  184. }
  185. // position after first boundary line
  186. $start = $p + 3 + strlen($boundary);
  187. while (($p = strpos($body, '--' . $boundary . "\n", $start)) !== false) {
  188. $res[] = substr($body, $start, $p - $start);
  189. $start = $p + 3 + strlen($boundary);
  190. }
  191. // no more parts, find end boundary
  192. $p = strpos($body, '--' . $boundary . '--', $start);
  193. if ($p === false) {
  194. throw new Exception\RuntimeException('Not a valid Mime Message: End Missing');
  195. }
  196. // the remaining part also needs to be parsed:
  197. $res[] = substr($body, $start, $p - $start);
  198. return $res;
  199. }
  200. /**
  201. * Decodes a MIME encoded string and returns a Zend\Mime\Message object with
  202. * all the MIME parts set according to the given string
  203. *
  204. * @param string $message
  205. * @param string $boundary Multipart boundary; if omitted, $message will be
  206. * treated as a single part.
  207. * @param string $EOL EOL string; defaults to {@link Zend\Mime\Mime::LINEEND}
  208. * @throws Exception\RuntimeException
  209. * @return Message
  210. */
  211. public static function createFromMessage($message, $boundary = null, $EOL = Mime::LINEEND)
  212. {
  213. if ($boundary) {
  214. $parts = Decode::splitMessageStruct($message, $boundary, $EOL);
  215. } else {
  216. Decode::splitMessage($message, $headers, $body, $EOL);
  217. $parts = [[
  218. 'header' => $headers,
  219. 'body' => $body,
  220. ]];
  221. }
  222. $res = new static();
  223. foreach ($parts as $part) {
  224. // now we build a new MimePart for the current Message Part:
  225. $properties = [];
  226. foreach ($part['header'] as $header) {
  227. /** @var \Zend\Mail\Header\HeaderInterface $header */
  228. /**
  229. * @todo check for characterset and filename
  230. */
  231. $fieldName = $header->getFieldName();
  232. $fieldValue = $header->getFieldValue();
  233. switch (strtolower($fieldName)) {
  234. case 'content-type':
  235. $properties['type'] = $fieldValue;
  236. break;
  237. case 'content-transfer-encoding':
  238. $properties['encoding'] = $fieldValue;
  239. break;
  240. case 'content-id':
  241. $properties['id'] = trim($fieldValue, '<>');
  242. break;
  243. case 'content-disposition':
  244. $properties['disposition'] = $fieldValue;
  245. break;
  246. case 'content-description':
  247. $properties['description'] = $fieldValue;
  248. break;
  249. case 'content-location':
  250. $properties['location'] = $fieldValue;
  251. break;
  252. case 'content-language':
  253. $properties['language'] = $fieldValue;
  254. break;
  255. default:
  256. // Ignore unknown header
  257. break;
  258. }
  259. }
  260. $body = $part['body'];
  261. if (isset($properties['encoding'])) {
  262. switch ($properties['encoding']) {
  263. case 'quoted-printable':
  264. $body = quoted_printable_decode($body);
  265. break;
  266. case 'base64':
  267. $body = base64_decode($body);
  268. break;
  269. }
  270. }
  271. $newPart = new Part($body);
  272. foreach ($properties as $key => $value) {
  273. $newPart->$key = $value;
  274. }
  275. $res->addPart($newPart);
  276. }
  277. return $res;
  278. }
  279. }