Part.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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-2012 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-2012 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. public $type = Zend_Mime::TYPE_OCTETSTREAM;
  35. public $encoding = Zend_Mime::ENCODING_8BIT;
  36. public $id;
  37. public $disposition;
  38. public $filename;
  39. public $description;
  40. public $charset;
  41. public $boundary;
  42. public $location;
  43. public $language;
  44. protected $_content;
  45. protected $_isStream = false;
  46. /**
  47. * create a new Mime Part.
  48. * The (unencoded) content of the Part as passed
  49. * as a string or stream
  50. *
  51. * @param mixed $content String or Stream containing the content
  52. */
  53. public function __construct($content)
  54. {
  55. $this->_content = $content;
  56. if (is_resource($content)) {
  57. $this->_isStream = true;
  58. }
  59. }
  60. /**
  61. * @todo setters/getters
  62. * @todo error checking for setting $type
  63. * @todo error checking for setting $encoding
  64. */
  65. /**
  66. * check if this part can be read as a stream.
  67. * if true, getEncodedStream can be called, otherwise
  68. * only getContent can be used to fetch the encoded
  69. * content of the part
  70. *
  71. * @return bool
  72. */
  73. public function isStream()
  74. {
  75. return $this->_isStream;
  76. }
  77. /**
  78. * if this was created with a stream, return a filtered stream for
  79. * reading the content. very useful for large file attachments.
  80. *
  81. * @return stream
  82. * @throws Zend_Mime_Exception if not a stream or unable to append filter
  83. */
  84. public function getEncodedStream()
  85. {
  86. if (!$this->_isStream) {
  87. require_once 'Zend/Mime/Exception.php';
  88. throw new Zend_Mime_Exception('Attempt to get a stream from a string part');
  89. }
  90. //stream_filter_remove(); // ??? is that right?
  91. switch ($this->encoding) {
  92. case Zend_Mime::ENCODING_QUOTEDPRINTABLE:
  93. $filter = stream_filter_append(
  94. $this->_content,
  95. 'convert.quoted-printable-encode',
  96. STREAM_FILTER_READ,
  97. array(
  98. 'line-length' => 76,
  99. 'line-break-chars' => Zend_Mime::LINEEND
  100. )
  101. );
  102. if (!is_resource($filter)) {
  103. require_once 'Zend/Mime/Exception.php';
  104. throw new Zend_Mime_Exception('Failed to append quoted-printable filter');
  105. }
  106. break;
  107. case Zend_Mime::ENCODING_BASE64:
  108. $filter = stream_filter_append(
  109. $this->_content,
  110. 'convert.base64-encode',
  111. STREAM_FILTER_READ,
  112. array(
  113. 'line-length' => 76,
  114. 'line-break-chars' => Zend_Mime::LINEEND
  115. )
  116. );
  117. if (!is_resource($filter)) {
  118. require_once 'Zend/Mime/Exception.php';
  119. throw new Zend_Mime_Exception('Failed to append base64 filter');
  120. }
  121. break;
  122. default:
  123. }
  124. return $this->_content;
  125. }
  126. /**
  127. * Get the Content of the current Mime Part in the given encoding.
  128. *
  129. * @return String
  130. */
  131. public function getContent($EOL = Zend_Mime::LINEEND)
  132. {
  133. if ($this->_isStream) {
  134. return stream_get_contents($this->getEncodedStream());
  135. } else {
  136. return Zend_Mime::encode($this->_content, $this->encoding, $EOL);
  137. }
  138. }
  139. /**
  140. * Get the RAW unencoded content from this part
  141. * @return string
  142. */
  143. public function getRawContent()
  144. {
  145. if ($this->_isStream) {
  146. return stream_get_contents($this->_content);
  147. } else {
  148. return $this->_content;
  149. }
  150. }
  151. /**
  152. * Create and return the array of headers for this MIME part
  153. *
  154. * @access public
  155. * @return array
  156. */
  157. public function getHeadersArray($EOL = Zend_Mime::LINEEND)
  158. {
  159. $headers = array();
  160. $contentType = $this->type;
  161. if ($this->charset) {
  162. $contentType .= '; charset=' . $this->charset;
  163. }
  164. if ($this->boundary) {
  165. $contentType .= ';' . $EOL
  166. . " boundary=\"" . $this->boundary . '"';
  167. }
  168. $headers[] = array('Content-Type', $contentType);
  169. if ($this->encoding) {
  170. $headers[] = array('Content-Transfer-Encoding', $this->encoding);
  171. }
  172. if ($this->id) {
  173. $headers[] = array('Content-ID', '<' . $this->id . '>');
  174. }
  175. if ($this->disposition) {
  176. $disposition = $this->disposition;
  177. if ($this->filename) {
  178. $disposition .= '; filename="' . $this->filename . '"';
  179. }
  180. $headers[] = array('Content-Disposition', $disposition);
  181. }
  182. if ($this->description) {
  183. $headers[] = array('Content-Description', $this->description);
  184. }
  185. if ($this->location) {
  186. $headers[] = array('Content-Location', $this->location);
  187. }
  188. if ($this->language){
  189. $headers[] = array('Content-Language', $this->language);
  190. }
  191. return $headers;
  192. }
  193. /**
  194. * Return the headers for this part as a string
  195. *
  196. * @return String
  197. */
  198. public function getHeaders($EOL = Zend_Mime::LINEEND)
  199. {
  200. $res = '';
  201. foreach ($this->getHeadersArray($EOL) as $header) {
  202. $res .= $header[0] . ': ' . $header[1] . $EOL;
  203. }
  204. return $res;
  205. }
  206. }