/vendor/zendframework/zendframework/library/Zend/Mime/Part.php

https://github.com/sunsetsystems/openemr · PHP · 494 lines · 283 code · 41 blank · 170 comment · 18 complexity · ddb2d787dc1ecc0e59f1f322fd3ca14d MD5 · raw file

  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. */
  9. namespace Zend\Mime;
  10. /**
  11. * Class representing a MIME part.
  12. */
  13. class Part
  14. {
  15. public $type = Mime::TYPE_OCTETSTREAM;
  16. public $encoding = Mime::ENCODING_8BIT;
  17. public $id;
  18. public $disposition;
  19. public $filename;
  20. public $description;
  21. public $charset;
  22. public $boundary;
  23. public $location;
  24. public $language;
  25. protected $content;
  26. protected $isStream = false;
  27. protected $filters = array();
  28. /**
  29. * create a new Mime Part.
  30. * The (unencoded) content of the Part as passed
  31. * as a string or stream
  32. *
  33. * @param mixed $content String or Stream containing the content
  34. * @throws Exception\InvalidArgumentException
  35. */
  36. public function __construct($content = '')
  37. {
  38. if (! is_string($content) && ! is_resource($content)) {
  39. throw new Exception\InvalidArgumentException(sprintf(
  40. "'%s' must be string or resource",
  41. $content
  42. ));
  43. }
  44. $this->content = $content;
  45. if (is_resource($content)) {
  46. $this->isStream = true;
  47. }
  48. }
  49. /**
  50. * @todo error checking for setting $type
  51. * @todo error checking for setting $encoding
  52. */
  53. /**
  54. * Set type
  55. * @param string $type
  56. * @return self
  57. */
  58. public function setType($type = Mime::TYPE_OCTETSTREAM)
  59. {
  60. $this->type = $type;
  61. return $this;
  62. }
  63. /**
  64. * Get type
  65. * @return string
  66. */
  67. public function getType()
  68. {
  69. return $this->type;
  70. }
  71. /**
  72. * Set encoding
  73. * @param string $encoding
  74. * @return self
  75. */
  76. public function setEncoding($encoding = Mime::ENCODING_8BIT)
  77. {
  78. $this->encoding = $encoding;
  79. return $this;
  80. }
  81. /**
  82. * Get encoding
  83. * @return string
  84. */
  85. public function getEncoding()
  86. {
  87. return $this->encoding;
  88. }
  89. /**
  90. * Set id
  91. * @param string $id
  92. * @return self
  93. */
  94. public function setId($id)
  95. {
  96. $this->id = $id;
  97. return $this;
  98. }
  99. /**
  100. * Get id
  101. * @return string
  102. */
  103. public function getId()
  104. {
  105. return $this->id;
  106. }
  107. /**
  108. * Set disposition
  109. * @param string $disposition
  110. * @return self
  111. */
  112. public function setDisposition($disposition)
  113. {
  114. $this->disposition = $disposition;
  115. return $this;
  116. }
  117. /**
  118. * Get disposition
  119. * @return string
  120. */
  121. public function getDisposition()
  122. {
  123. return $this->disposition;
  124. }
  125. /**
  126. * Set description
  127. * @param string $description
  128. * @return self
  129. */
  130. public function setDescription($description)
  131. {
  132. $this->description = $description;
  133. return $this;
  134. }
  135. /**
  136. * Get description
  137. * @return string
  138. */
  139. public function getDescription()
  140. {
  141. return $this->description;
  142. }
  143. /**
  144. * Set filename
  145. * @param string $fileName
  146. * @return self
  147. */
  148. public function setFileName($fileName)
  149. {
  150. $this->filename = $fileName;
  151. return $this;
  152. }
  153. /**
  154. * Get filename
  155. * @return string
  156. */
  157. public function getFileName()
  158. {
  159. return $this->filename;
  160. }
  161. /**
  162. * Set charset
  163. * @param string $type
  164. * @return self
  165. */
  166. public function setCharset($charset)
  167. {
  168. $this->charset = $charset;
  169. return $this;
  170. }
  171. /**
  172. * Get charset
  173. * @return string
  174. */
  175. public function getCharset()
  176. {
  177. return $this->charset;
  178. }
  179. /**
  180. * Set boundary
  181. * @param string $boundary
  182. * @return self
  183. */
  184. public function setBoundary($boundary)
  185. {
  186. $this->boundary = $boundary;
  187. return $this;
  188. }
  189. /**
  190. * Get boundary
  191. * @return string
  192. */
  193. public function getBoundary()
  194. {
  195. return $this->boundary;
  196. }
  197. /**
  198. * Set location
  199. * @param string $location
  200. * @return self
  201. */
  202. public function setLocation($location)
  203. {
  204. $this->location = $location;
  205. return $this;
  206. }
  207. /**
  208. * Get location
  209. * @return string
  210. */
  211. public function getLocation()
  212. {
  213. return $this->location;
  214. }
  215. /**
  216. * Set language
  217. * @param string $language
  218. * @return self
  219. */
  220. public function setLanguage($language)
  221. {
  222. $this->language = $language;
  223. return $this;
  224. }
  225. /**
  226. * Get language
  227. * @return string
  228. */
  229. public function getLanguage()
  230. {
  231. return $this->language;
  232. }
  233. /**
  234. * Set content
  235. * @param mixed $content String or Stream containing the content
  236. * @throws Exception\InvalidArgumentException
  237. * @return self
  238. */
  239. public function setContent($content)
  240. {
  241. if (! is_string($content) && ! is_resource($content)) {
  242. throw new Exception\InvalidArgumentException(
  243. "'%s' must be string or resource",
  244. $content
  245. );
  246. }
  247. $this->content = $content;
  248. if (is_resource($content)) {
  249. $this->isStream = true;
  250. }
  251. return $this;
  252. }
  253. /**
  254. * Set isStream
  255. * @param bool $isStream
  256. * @return self
  257. */
  258. public function setIsStream($isStream = false)
  259. {
  260. $this->isStream = (bool) $isStream;
  261. return $this;
  262. }
  263. /**
  264. * Get isStream
  265. * @return bool
  266. */
  267. public function getIsStream()
  268. {
  269. return $this->isStream;
  270. }
  271. /**
  272. * Set filters
  273. * @param array $filters
  274. * @return self
  275. */
  276. public function setFilters($filters = array())
  277. {
  278. $this->filters = $filters;
  279. return $this;
  280. }
  281. /**
  282. * Get Filters
  283. * @return array
  284. */
  285. public function getFilters()
  286. {
  287. return $this->filters;
  288. }
  289. /**
  290. * check if this part can be read as a stream.
  291. * if true, getEncodedStream can be called, otherwise
  292. * only getContent can be used to fetch the encoded
  293. * content of the part
  294. *
  295. * @return bool
  296. */
  297. public function isStream()
  298. {
  299. return $this->isStream;
  300. }
  301. /**
  302. * if this was created with a stream, return a filtered stream for
  303. * reading the content. very useful for large file attachments.
  304. *
  305. * @param string $EOL
  306. * @return resource
  307. * @throws Exception\RuntimeException if not a stream or unable to append filter
  308. */
  309. public function getEncodedStream($EOL = Mime::LINEEND)
  310. {
  311. if (! $this->isStream) {
  312. throw new Exception\RuntimeException('Attempt to get a stream from a string part');
  313. }
  314. //stream_filter_remove(); // ??? is that right?
  315. switch ($this->encoding) {
  316. case Mime::ENCODING_QUOTEDPRINTABLE:
  317. if (array_key_exists(Mime::ENCODING_QUOTEDPRINTABLE, $this->filters)) {
  318. stream_filter_remove($this->filters[Mime::ENCODING_QUOTEDPRINTABLE]);
  319. }
  320. $filter = stream_filter_append(
  321. $this->content,
  322. 'convert.quoted-printable-encode',
  323. STREAM_FILTER_READ,
  324. array(
  325. 'line-length' => 76,
  326. 'line-break-chars' => $EOL
  327. )
  328. );
  329. $this->filters[Mime::ENCODING_QUOTEDPRINTABLE] = $filter;
  330. if (! is_resource($filter)) {
  331. throw new Exception\RuntimeException('Failed to append quoted-printable filter');
  332. }
  333. break;
  334. case Mime::ENCODING_BASE64:
  335. if (array_key_exists(Mime::ENCODING_BASE64, $this->filters)) {
  336. stream_filter_remove($this->filters[Mime::ENCODING_BASE64]);
  337. }
  338. $filter = stream_filter_append(
  339. $this->content,
  340. 'convert.base64-encode',
  341. STREAM_FILTER_READ,
  342. array(
  343. 'line-length' => 76,
  344. 'line-break-chars' => $EOL
  345. )
  346. );
  347. $this->filters[Mime::ENCODING_BASE64] = $filter;
  348. if (! is_resource($filter)) {
  349. throw new Exception\RuntimeException('Failed to append base64 filter');
  350. }
  351. break;
  352. default:
  353. }
  354. return $this->content;
  355. }
  356. /**
  357. * Get the Content of the current Mime Part in the given encoding.
  358. *
  359. * @param string $EOL
  360. * @return string
  361. */
  362. public function getContent($EOL = Mime::LINEEND)
  363. {
  364. if ($this->isStream) {
  365. $encodedStream = $this->getEncodedStream($EOL);
  366. $encodedStreamContents = stream_get_contents($encodedStream);
  367. $streamMetaData = stream_get_meta_data($encodedStream);
  368. if (isset($streamMetaData['seekable']) && $streamMetaData['seekable']) {
  369. rewind($encodedStream);
  370. }
  371. return $encodedStreamContents;
  372. }
  373. return Mime::encode($this->content, $this->encoding, $EOL);
  374. }
  375. /**
  376. * Get the RAW unencoded content from this part
  377. * @return string
  378. */
  379. public function getRawContent()
  380. {
  381. if ($this->isStream) {
  382. return stream_get_contents($this->content);
  383. }
  384. return $this->content;
  385. }
  386. /**
  387. * Create and return the array of headers for this MIME part
  388. *
  389. * @access public
  390. * @param string $EOL
  391. * @return array
  392. */
  393. public function getHeadersArray($EOL = Mime::LINEEND)
  394. {
  395. $headers = array();
  396. $contentType = $this->type;
  397. if ($this->charset) {
  398. $contentType .= '; charset=' . $this->charset;
  399. }
  400. if ($this->boundary) {
  401. $contentType .= ';' . $EOL
  402. . " boundary=\"" . $this->boundary . '"';
  403. }
  404. $headers[] = array('Content-Type', $contentType);
  405. if ($this->encoding) {
  406. $headers[] = array('Content-Transfer-Encoding', $this->encoding);
  407. }
  408. if ($this->id) {
  409. $headers[] = array('Content-ID', '<' . $this->id . '>');
  410. }
  411. if ($this->disposition) {
  412. $disposition = $this->disposition;
  413. if ($this->filename) {
  414. $disposition .= '; filename="' . $this->filename . '"';
  415. }
  416. $headers[] = array('Content-Disposition', $disposition);
  417. }
  418. if ($this->description) {
  419. $headers[] = array('Content-Description', $this->description);
  420. }
  421. if ($this->location) {
  422. $headers[] = array('Content-Location', $this->location);
  423. }
  424. if ($this->language) {
  425. $headers[] = array('Content-Language', $this->language);
  426. }
  427. return $headers;
  428. }
  429. /**
  430. * Return the headers for this part as a string
  431. *
  432. * @param string $EOL
  433. * @return String
  434. */
  435. public function getHeaders($EOL = Mime::LINEEND)
  436. {
  437. $res = '';
  438. foreach ($this->getHeadersArray($EOL) as $header) {
  439. $res .= $header[0] . ': ' . $header[1] . $EOL;
  440. }
  441. return $res;
  442. }
  443. }