PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/DevApp/library/ServerLibraries/ZendFramework/1.7/library/Zend/Mime/Message.php

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