Message.php 8.3 KB

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