Part.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. * Class representing a MIME part.
  27. *
  28. * @category Zend
  29. * @package Zend_Mime
  30. * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. */
  33. class Zend_Mime_Part {
  34. /**
  35. * Type
  36. *
  37. * @var string
  38. */
  39. public $type = Zend_Mime::TYPE_OCTETSTREAM;
  40. /**
  41. * Encoding
  42. *
  43. * @var string
  44. */
  45. public $encoding = Zend_Mime::ENCODING_8BIT;
  46. /**
  47. * ID
  48. *
  49. * @var string
  50. */
  51. public $id;
  52. /**
  53. * Disposition
  54. *
  55. * @var string
  56. */
  57. public $disposition;
  58. /**
  59. * Filename
  60. *
  61. * @var string
  62. */
  63. public $filename;
  64. /**
  65. * Description
  66. *
  67. * @var string
  68. */
  69. public $description;
  70. /**
  71. * Character set
  72. *
  73. * @var string
  74. */
  75. public $charset;
  76. /**
  77. * Boundary
  78. *
  79. * @var string
  80. */
  81. public $boundary;
  82. /**
  83. * Location
  84. *
  85. * @var string
  86. */
  87. public $location;
  88. /**
  89. * Language
  90. *
  91. * @var string
  92. */
  93. public $language;
  94. /**
  95. * Content
  96. *
  97. * @var mixed
  98. */
  99. protected $_content;
  100. /**
  101. * @var bool
  102. */
  103. protected $_isStream = false;
  104. /**
  105. * create a new Mime Part.
  106. * The (unencoded) content of the Part as passed
  107. * as a string or stream
  108. *
  109. * @param mixed $content String or Stream containing the content
  110. */
  111. public function __construct($content)
  112. {
  113. $this->_content = $content;
  114. if (is_resource($content)) {
  115. $this->_isStream = true;
  116. }
  117. }
  118. /**
  119. * @todo setters/getters
  120. * @todo error checking for setting $type
  121. * @todo error checking for setting $encoding
  122. */
  123. /**
  124. * check if this part can be read as a stream.
  125. * if true, getEncodedStream can be called, otherwise
  126. * only getContent can be used to fetch the encoded
  127. * content of the part
  128. *
  129. * @return bool
  130. */
  131. public function isStream()
  132. {
  133. return $this->_isStream;
  134. }
  135. /**
  136. * if this was created with a stream, return a filtered stream for
  137. * reading the content. very useful for large file attachments.
  138. *
  139. * @return mixed Stream
  140. * @throws Zend_Mime_Exception if not a stream or unable to append filter
  141. */
  142. public function getEncodedStream()
  143. {
  144. if (!$this->_isStream) {
  145. require_once 'Zend/Mime/Exception.php';
  146. throw new Zend_Mime_Exception('Attempt to get a stream from a string part');
  147. }
  148. //stream_filter_remove(); // ??? is that right?
  149. switch ($this->encoding) {
  150. case Zend_Mime::ENCODING_QUOTEDPRINTABLE:
  151. $filter = stream_filter_append(
  152. $this->_content,
  153. 'convert.quoted-printable-encode',
  154. STREAM_FILTER_READ,
  155. array(
  156. 'line-length' => 76,
  157. 'line-break-chars' => Zend_Mime::LINEEND
  158. )
  159. );
  160. if (!is_resource($filter)) {
  161. require_once 'Zend/Mime/Exception.php';
  162. throw new Zend_Mime_Exception('Failed to append quoted-printable filter');
  163. }
  164. break;
  165. case Zend_Mime::ENCODING_BASE64:
  166. $filter = stream_filter_append(
  167. $this->_content,
  168. 'convert.base64-encode',
  169. STREAM_FILTER_READ,
  170. array(
  171. 'line-length' => 76,
  172. 'line-break-chars' => Zend_Mime::LINEEND
  173. )
  174. );
  175. if (!is_resource($filter)) {
  176. require_once 'Zend/Mime/Exception.php';
  177. throw new Zend_Mime_Exception('Failed to append base64 filter');
  178. }
  179. break;
  180. default:
  181. }
  182. return $this->_content;
  183. }
  184. /**
  185. * Get the Content of the current Mime Part in the given encoding.
  186. *
  187. * @param string $EOL Line end; defaults to {@link Zend_Mime::LINEEND}
  188. * @throws Zend_Mime_Exception
  189. * @return string
  190. */
  191. public function getContent($EOL = Zend_Mime::LINEEND)
  192. {
  193. if ($this->_isStream) {
  194. return stream_get_contents($this->getEncodedStream());
  195. } else {
  196. return Zend_Mime::encode($this->_content, $this->encoding, $EOL);
  197. }
  198. }
  199. /**
  200. * Get the RAW unencoded content from this part
  201. *
  202. * @return string
  203. */
  204. public function getRawContent()
  205. {
  206. if ($this->_isStream) {
  207. return stream_get_contents($this->_content);
  208. } else {
  209. return $this->_content;
  210. }
  211. }
  212. /**
  213. * Create and return the array of headers for this MIME part
  214. *
  215. * @param string $EOL Line end; defaults to {@link Zend_Mime::LINEEND}
  216. * @return array
  217. */
  218. public function getHeadersArray($EOL = Zend_Mime::LINEEND)
  219. {
  220. $headers = array();
  221. $contentType = $this->type;
  222. if ($this->charset) {
  223. $contentType .= '; charset=' . $this->charset;
  224. }
  225. if ($this->boundary) {
  226. $contentType .= ';' . $EOL
  227. . " boundary=\"" . $this->boundary . '"';
  228. }
  229. $headers[] = array('Content-Type', $contentType);
  230. if ($this->encoding) {
  231. $headers[] = array('Content-Transfer-Encoding', $this->encoding);
  232. }
  233. if ($this->id) {
  234. $headers[] = array('Content-ID', '<' . $this->id . '>');
  235. }
  236. if ($this->disposition) {
  237. $disposition = $this->disposition;
  238. if ($this->filename) {
  239. $disposition .= '; filename="' . $this->filename . '"';
  240. }
  241. $headers[] = array('Content-Disposition', $disposition);
  242. }
  243. if ($this->description) {
  244. $headers[] = array('Content-Description', $this->description);
  245. }
  246. if ($this->location) {
  247. $headers[] = array('Content-Location', $this->location);
  248. }
  249. if ($this->language){
  250. $headers[] = array('Content-Language', $this->language);
  251. }
  252. return $headers;
  253. }
  254. /**
  255. * Return the headers for this part as a string
  256. *
  257. * @param string $EOL Line end; defaults to {@link Zend_Mime::LINEEND}
  258. * @return string
  259. */
  260. public function getHeaders($EOL = Zend_Mime::LINEEND)
  261. {
  262. $res = '';
  263. foreach ($this->getHeadersArray($EOL) as $header) {
  264. $res .= $header[0] . ': ' . $header[1] . $EOL;
  265. }
  266. return $res;
  267. }
  268. }