Part.php 7.8 KB

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