Message.php 8.7 KB

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