Part.php 12 KB

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