PageRenderTime 27ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/zend/Zend/Mime/Message.php

http://github.com/moodle/moodle
PHP | 286 lines | 125 code | 28 blank | 133 comment | 6 complexity | ca46f96a5431edc50799e2467a235187 MD5 | raw file
Possible License(s): MIT, AGPL-3.0, MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, Apache-2.0, LGPL-2.1, BSD-3-Clause
  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. * 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-2010 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. * @return array
  179. */
  180. protected static function _disassembleMime($body, $boundary)
  181. {
  182. $start = 0;
  183. $res = array();
  184. // find every mime part limiter and cut out the
  185. // string before it.
  186. // the part before the first boundary string is discarded:
  187. $p = strpos($body, '--'.$boundary."\n", $start);
  188. if ($p === false) {
  189. // no parts found!
  190. return array();
  191. }
  192. // position after first boundary line
  193. $start = $p + 3 + strlen($boundary);
  194. while (($p = strpos($body, '--' . $boundary . "\n", $start)) !== false) {
  195. $res[] = substr($body, $start, $p-$start);
  196. $start = $p + 3 + strlen($boundary);
  197. }
  198. // no more parts, find end boundary
  199. $p = strpos($body, '--' . $boundary . '--', $start);
  200. if ($p===false) {
  201. throw new Zend_Exception('Not a valid Mime Message: End Missing');
  202. }
  203. // the remaining part also needs to be parsed:
  204. $res[] = substr($body, $start, $p-$start);
  205. return $res;
  206. }
  207. /**
  208. * Decodes a MIME encoded string and returns a Zend_Mime_Message object with
  209. * all the MIME parts set according to the given string
  210. *
  211. * @param string $message
  212. * @param string $boundary
  213. * @param string $EOL EOL string; defaults to {@link Zend_Mime::LINEEND}
  214. * @return Zend_Mime_Message
  215. */
  216. public static function createFromMessage($message, $boundary, $EOL = Zend_Mime::LINEEND)
  217. {
  218. require_once 'Zend/Mime/Decode.php';
  219. $parts = Zend_Mime_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 Zend_Mime_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. }