PageRenderTime 57ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/library/Zend/Mime/Message.php

https://github.com/snippet/zf2
PHP | 283 lines | 124 code | 25 blank | 134 comment | 6 complexity | 059fa0a2473a48d9fe2ace51982c9f1c MD5 | raw file
  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-2010 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. * @namespace
  23. */
  24. namespace Zend\Mime;
  25. use Zend;
  26. /**
  27. * @uses \Zend\Exception
  28. * @uses \Zend\Mime\Mime
  29. * @uses \Zend\Mime\Decode
  30. * @uses \Zend\Mime\Part
  31. * @category Zend
  32. * @package Zend_Mime
  33. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  34. * @license http://framework.zend.com/license/new-bsd New BSD License
  35. */
  36. class Message
  37. {
  38. protected $_parts = array();
  39. protected $_mime = null;
  40. /**
  41. * Returns the list of all Zend_Mime_Parts in the message
  42. *
  43. * @return array of \Zend\Mime\Part
  44. */
  45. public function getParts()
  46. {
  47. return $this->_parts;
  48. }
  49. /**
  50. * Sets the given array of Zend_Mime_Parts as the array for the message
  51. *
  52. * @param array $parts
  53. */
  54. public function setParts($parts)
  55. {
  56. $this->_parts = $parts;
  57. }
  58. /**
  59. * Append a new Zend_Mime_Part to the current message
  60. *
  61. * @param \Zend\Mime\Part $part
  62. */
  63. public function addPart(Part $part)
  64. {
  65. /**
  66. * @todo check for duplicate object handle
  67. */
  68. $this->_parts[] = $part;
  69. }
  70. /**
  71. * Check if message needs to be sent as multipart
  72. * MIME message or if it has only one part.
  73. *
  74. * @return boolean
  75. */
  76. public function isMultiPart()
  77. {
  78. return (count($this->_parts) > 1);
  79. }
  80. /**
  81. * Set Zend_Mime object for the message
  82. *
  83. * This can be used to set the boundary specifically or to use a subclass of
  84. * Zend_Mime for generating the boundary.
  85. *
  86. * @param \Zend\Mime\Mime $mime
  87. */
  88. public function setMime(Mime $mime)
  89. {
  90. $this->_mime = $mime;
  91. }
  92. /**
  93. * Returns the Zend_Mime object in use by the message
  94. *
  95. * If the object was not present, it is created and returned. Can be used to
  96. * determine the boundary used in this message.
  97. *
  98. * @return \Zend\Mime\Mime
  99. */
  100. public function getMime()
  101. {
  102. if ($this->_mime === null) {
  103. $this->_mime = new Mime();
  104. }
  105. return $this->_mime;
  106. }
  107. /**
  108. * Generate MIME-compliant message from the current configuration
  109. *
  110. * This can be a multipart message if more than one MIME part was added. If
  111. * only one part is present, the content of this part is returned. If no
  112. * part had been added, an empty string is returned.
  113. *
  114. * Parts are seperated by the mime boundary as defined in Zend_Mime. If
  115. * {@link setMime()} has been called before this method, the Zend_Mime
  116. * object set by this call will be used. Otherwise, a new Zend_Mime object
  117. * is generated and used.
  118. *
  119. * @param string $EOL EOL string; defaults to {@link Zend_Mime::LINEEND}
  120. * @return string
  121. */
  122. public function generateMessage($EOL = Mime::LINEEND)
  123. {
  124. if (! $this->isMultiPart()) {
  125. $body = array_shift($this->_parts);
  126. $body = $body->getContent($EOL);
  127. } else {
  128. $mime = $this->getMime();
  129. $boundaryLine = $mime->boundaryLine($EOL);
  130. $body = 'This is a message in Mime Format. If you see this, '
  131. . "your mail reader does not support this format." . $EOL;
  132. foreach (array_keys($this->_parts) as $p) {
  133. $body .= $boundaryLine
  134. . $this->getPartHeaders($p, $EOL)
  135. . $EOL
  136. . $this->getPartContent($p, $EOL);
  137. }
  138. $body .= $mime->mimeEnd($EOL);
  139. }
  140. return trim($body);
  141. }
  142. /**
  143. * Get the headers of a given part as an array
  144. *
  145. * @param int $partnum
  146. * @return array
  147. */
  148. public function getPartHeadersArray($partnum)
  149. {
  150. return $this->_parts[$partnum]->getHeadersArray();
  151. }
  152. /**
  153. * Get the headers of a given part as a string
  154. *
  155. * @param int $partnum
  156. * @return string
  157. */
  158. public function getPartHeaders($partnum, $EOL = Mime::LINEEND)
  159. {
  160. return $this->_parts[$partnum]->getHeaders($EOL);
  161. }
  162. /**
  163. * Get the (encoded) content of a given part as a string
  164. *
  165. * @param int $partnum
  166. * @return string
  167. */
  168. public function getPartContent($partnum, $EOL = Mime::LINEEND)
  169. {
  170. return $this->_parts[$partnum]->getContent($EOL);
  171. }
  172. /**
  173. * Explode MIME multipart string into seperate parts
  174. *
  175. * Parts consist of the header and the body of each MIME part.
  176. *
  177. * @param string $body
  178. * @param string $boundary
  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. * @return \Zend\Mime\Message
  216. */
  217. public static function createFromMessage($message, $boundary, $EOL = Mime::LINEEND)
  218. {
  219. $parts = Decode::splitMessageStruct($message, $boundary, $EOL);
  220. $res = new self();
  221. foreach ($parts as $part) {
  222. // now we build a new MimePart for the current Message Part:
  223. $newPart = new Part($part['body']);
  224. foreach ($part['header'] as $key => $value) {
  225. /**
  226. * @todo check for characterset and filename
  227. */
  228. switch(strtolower($key)) {
  229. case 'content-type':
  230. $newPart->type = $value;
  231. break;
  232. case 'content-transfer-encoding':
  233. $newPart->encoding = $value;
  234. break;
  235. case 'content-id':
  236. $newPart->id = trim($value,'<>');
  237. break;
  238. case 'content-disposition':
  239. $newPart->disposition = $value;
  240. break;
  241. case 'content-description':
  242. $newPart->description = $value;
  243. break;
  244. case 'content-location':
  245. $newPart->location = $value;
  246. break;
  247. case 'content-language':
  248. $newPart->language = $value;
  249. break;
  250. default:
  251. throw new Zend\Exception('Unknown header ignored for MimePart:' . $key);
  252. }
  253. }
  254. $res->addPart($newPart);
  255. }
  256. return $res;
  257. }
  258. }