/framework/Imap_Client/lib/Horde/Imap/Client/Data/Fetch.php

https://github.com/finger2000/horde · PHP · 518 lines · 215 code · 46 blank · 257 comment · 8 complexity · 9411769219cfd7430e948511dde83ff0 MD5 · raw file

  1. <?php
  2. /**
  3. * Object containing data returned by the Horde_Imap_Client_Base#fetch()
  4. * command.
  5. *
  6. * Copyright 2011 Horde LLC (http://www.horde.org/)
  7. *
  8. * See the enclosed file COPYING for license information (LGPL). If you
  9. * did not receive this file, see http://www.horde.org/licenses/lgpl21.
  10. *
  11. * @author Michael Slusarz <slusarz@horde.org>
  12. * @category Horde
  13. * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
  14. * @package Imap_Client
  15. */
  16. class Horde_Imap_Client_Data_Fetch
  17. {
  18. /* Constants. */
  19. const HEADER_PARSE = 1;
  20. const HEADER_STREAM = 2;
  21. /**
  22. * Internal data array.
  23. *
  24. * @var array
  25. */
  26. protected $_data = array();
  27. /**
  28. * Set the full message property.
  29. *
  30. * @param mixed $msg The full message text, as either a string or stream
  31. * resource.
  32. */
  33. public function setFullMsg($msg)
  34. {
  35. $this->_data[Horde_Imap_Client::FETCH_FULLMSG] = $msg;
  36. }
  37. /**
  38. * Returns the full message.
  39. *
  40. * @param boolean $stream Return as a stream?
  41. *
  42. * @return mixed The full text of the entire message.
  43. */
  44. public function getFullMsg($stream = false)
  45. {
  46. return $this->_msgText($stream, isset($this->_data[Horde_Imap_Client::FETCH_FULLMSG]) ? $this->_data[Horde_Imap_Client::FETCH_FULLMSG] : null);
  47. }
  48. /**
  49. * Set the message structure.
  50. *
  51. * @param Horde_Mime_Part $structure The base MIME part of the message.
  52. */
  53. public function setStructure(Horde_Mime_Part $structure)
  54. {
  55. $this->_data[Horde_Imap_Client::FETCH_STRUCTURE] = $structure;
  56. }
  57. /**
  58. * Get the message structure.
  59. *
  60. * @return Horde_Mime_Part $structure The base MIME part of the message.
  61. */
  62. public function getStructure()
  63. {
  64. return isset($this->_data[Horde_Imap_Client::FETCH_STRUCTURE])
  65. ? clone $this->_data[Horde_Imap_Client::FETCH_STRUCTURE]
  66. : new Horde_Mime_Part();
  67. }
  68. /**
  69. * Set a header entry.
  70. *
  71. * @param string $label The search label.
  72. * @param mixed $data Either a Horde_Mime_Headers object or the raw
  73. * header text.
  74. */
  75. public function setHeaders($label, $data)
  76. {
  77. $this->_data[Horde_Imap_Client::FETCH_HEADERS][$label] = $data;
  78. }
  79. /**
  80. * Get a header entry.
  81. *
  82. * @param string $label The search label.
  83. * @param integer $format The return format. If self::HEADER_PARSE,
  84. * returns a Horde_Mime_Headers object. If
  85. * self::HEADER_STREAM, returns a stream.
  86. * Otherwise, returns header text.
  87. *
  88. * @return mixed See $format.
  89. */
  90. public function getHeaders($label, $format = 0)
  91. {
  92. return $this->_getHeaders($label, $format, Horde_Imap_Client::FETCH_HEADERS);
  93. }
  94. /**
  95. * Set a header text entry.
  96. *
  97. * @param string $id The MIME ID.
  98. * @param string $text The header text.
  99. */
  100. public function setHeaderText($id, $text)
  101. {
  102. $this->_data[Horde_Imap_Client::FETCH_HEADERTEXT][$id] = $text;
  103. }
  104. /**
  105. * Get a header text entry.
  106. *
  107. * @param string $id The MIME ID.
  108. * @param integer $format The return format. If self::HEADER_PARSE,
  109. * returns a Horde_Mime_Headers object. If
  110. * self::HEADER_STREAM, returns a stream.
  111. * Otherwise, returns header text.
  112. *
  113. * @return mixed See $format.
  114. */
  115. public function getHeaderText($id = 0, $format = 0)
  116. {
  117. return $this->_getHeaders($id, $format, Horde_Imap_Client::FETCH_HEADERTEXT);
  118. }
  119. /**
  120. * Set a MIME header entry.
  121. *
  122. * @param string $id The MIME ID.
  123. * @param string $text The header text.
  124. */
  125. public function setMimeHeader($id, $text)
  126. {
  127. $this->_data[Horde_Imap_Client::FETCH_MIMEHEADER][$id] = $text;
  128. }
  129. /**
  130. * Get a MIME header entry.
  131. *
  132. * @param string $id The MIME ID.
  133. * @param integer $format The return format. If self::HEADER_PARSE,
  134. * returns a Horde_Mime_Headers object. If
  135. * self::HEADER_STREAM, returns a stream.
  136. * Otherwise, returns header text.
  137. *
  138. * @return mixed See $format.
  139. */
  140. public function getMimeHeader($id, $format = 0)
  141. {
  142. return $this->_getHeaders($id, $format, Horde_Imap_Client::FETCH_MIMEHEADER);
  143. }
  144. /**
  145. * Set a body part entry.
  146. *
  147. * @param string $id The MIME ID.
  148. * @param mixed $text The body part text, as either a string or stream
  149. * resource.
  150. * @param string $decode Either '8bit', 'binary', or null.
  151. */
  152. public function setBodyPart($id, $text, $decode = null)
  153. {
  154. $this->_data[Horde_Imap_Client::FETCH_BODYPART][$id] = array(
  155. 'd' => $decode,
  156. 't' => $text
  157. );
  158. }
  159. /**
  160. * Set the body part size for a body part.
  161. *
  162. * @param string $id The MIME ID.
  163. * @param integer $size The size (in bytes).
  164. */
  165. public function setBodyPartSize($id, $size)
  166. {
  167. $this->_data[Horde_Imap_Client::FETCH_BODYPARTSIZE][$id] = intval($size);
  168. }
  169. /**
  170. * Get a body part entry.
  171. *
  172. * @param string $id The MIME ID.
  173. * @param boolean $stream Return as a stream?
  174. *
  175. * @return mixed The full text of the body part.
  176. */
  177. public function getBodyPart($id, $stream = false)
  178. {
  179. return $this->_msgText($stream, isset($this->_data[Horde_Imap_Client::FETCH_BODYPART][$id]) ? $this->_data[Horde_Imap_Client::FETCH_BODYPART][$id]['t'] : null);
  180. }
  181. /**
  182. * Determines if/how a body part was MIME decoded on the server.
  183. *
  184. * @param string $id The MIME ID.
  185. *
  186. * @return string Either '8bit', 'binary', or null.
  187. */
  188. public function getBodyPartDecode($id)
  189. {
  190. return isset($this->_data[Horde_Imap_Client::FETCH_BODYPART][$id])
  191. ? $this->_data[Horde_Imap_Client::FETCH_BODYPART][$id]['d']
  192. : null;
  193. }
  194. /**
  195. * Returns the body part size, if returned by the server.
  196. *
  197. * @param string $id The MIME ID.
  198. *
  199. * @return integer The body part size, in bytes.
  200. */
  201. public function getBodyPartSize($id)
  202. {
  203. return isset($this->_data[Horde_Imap_Client::FETCH_BODYPARTSIZE][$id])
  204. ? $this->_data[Horde_Imap_Client::FETCH_BODYPARTSIZE][$id]
  205. : null;
  206. }
  207. /**
  208. * Set a body text entry.
  209. *
  210. * @param string $id The MIME ID.
  211. * @param mixed $text The body part text, as either a string or stream
  212. * resource.
  213. */
  214. public function setBodyText($id, $text)
  215. {
  216. $this->_data[Horde_Imap_Client::FETCH_BODYTEXT][$id] = $text;
  217. }
  218. /**
  219. * Get a body text entry.
  220. *
  221. * @param string $id The MIME ID.
  222. * @param boolean $stream Return as a stream?
  223. *
  224. * @return mixed The full text of the body text.
  225. */
  226. public function getBodyText($id = 0, $stream = false)
  227. {
  228. return $this->_msgText($stream, isset($this->_data[Horde_Imap_Client::FETCH_BODYTEXT][$id]) ? $this->_data[Horde_Imap_Client::FETCH_BODYTEXT][$id] : null);
  229. }
  230. /**
  231. * Set envelope data.
  232. *
  233. * @param array $data The envelope data to pass to the Envelope object
  234. * constructor, or an Envelope object.
  235. */
  236. public function setEnvelope($data)
  237. {
  238. $this->_data[Horde_Imap_Client::FETCH_ENVELOPE] = is_array($data)
  239. ? new Horde_Imap_Client_Data_Envelope($data)
  240. : $data;
  241. }
  242. /**
  243. * Get envelope data.
  244. *
  245. * @return Horde_Imap_Client_Data_Envelope An envelope object.
  246. */
  247. public function getEnvelope()
  248. {
  249. return isset($this->_data[Horde_Imap_Client::FETCH_ENVELOPE])
  250. ? clone $this->_data[Horde_Imap_Client::FETCH_ENVELOPE]
  251. : new Horde_Imap_Client_Data_Envelope();
  252. }
  253. /**
  254. * Set IMAP flags.
  255. *
  256. * @param array $flags An array of IMAP flags.
  257. */
  258. public function setFlags(array $flags)
  259. {
  260. $this->_data[Horde_Imap_Client::FETCH_FLAGS] = array_map('strtolower', $flags);
  261. }
  262. /**
  263. * Get IMAP flags.
  264. *
  265. * @return array An array of IMAP flags (all flags in lowercase).
  266. */
  267. public function getFlags()
  268. {
  269. return isset($this->_data[Horde_Imap_Client::FETCH_FLAGS])
  270. ? $this->_data[Horde_Imap_Client::FETCH_FLAGS]
  271. : array();
  272. }
  273. /**
  274. * Set IMAP internal date.
  275. *
  276. * @param mixed $date Either a Horde_Imap_Client_DateTime object or a
  277. * date string.
  278. */
  279. public function setImapDate($date)
  280. {
  281. $this->_data[Horde_Imap_Client::FETCH_IMAPDATE] = is_object($date)
  282. ? $date
  283. : new Horde_Imap_Client_DateTime($date);
  284. }
  285. /**
  286. * Get internal IMAP date.
  287. *
  288. * @return Horde_Imap_Client_DateTime A date object.
  289. */
  290. public function getImapDate()
  291. {
  292. return isset($this->_data[Horde_Imap_Client::FETCH_IMAPDATE])
  293. ? clone $this->_data[Horde_Imap_Client::FETCH_IMAPDATE]
  294. : new Horde_Imap_Client_DateTime();
  295. }
  296. /**
  297. * Set message size.
  298. *
  299. * @param integer $size The size of the message, in bytes.
  300. */
  301. public function setSize($size)
  302. {
  303. $this->_data[Horde_Imap_Client::FETCH_SIZE] = intval($size);
  304. }
  305. /**
  306. * Get message size.
  307. *
  308. * @return integer The size of the message, in bytes.
  309. */
  310. public function getSize()
  311. {
  312. return isset($this->_data[Horde_Imap_Client::FETCH_SIZE])
  313. ? $this->_data[Horde_Imap_Client::FETCH_SIZE]
  314. : 0;
  315. }
  316. /**
  317. * Set UID.
  318. *
  319. * @param integer $uid The message UID.
  320. */
  321. public function setUid($uid)
  322. {
  323. $this->_data[Horde_Imap_Client::FETCH_UID] = intval($uid);
  324. }
  325. /**
  326. * Get UID.
  327. *
  328. * @return integer The message UID.
  329. */
  330. public function getUid()
  331. {
  332. return isset($this->_data[Horde_Imap_Client::FETCH_UID])
  333. ? $this->_data[Horde_Imap_Client::FETCH_UID]
  334. : null;
  335. }
  336. /**
  337. * Set message sequence number.
  338. *
  339. * @param integer $seq The message sequence number.
  340. */
  341. public function setSeq($seq)
  342. {
  343. $this->_data[Horde_Imap_Client::FETCH_SEQ] = intval($seq);
  344. }
  345. /**
  346. * Get message sequence number.
  347. *
  348. * @return integer The message sequence number.
  349. */
  350. public function getSeq()
  351. {
  352. return isset($this->_data[Horde_Imap_Client::FETCH_SEQ])
  353. ? $this->_data[Horde_Imap_Client::FETCH_SEQ]
  354. : null;
  355. }
  356. /**
  357. * Set the modified sequence value for the message.
  358. *
  359. * @param integer $modseq The modseq value.
  360. */
  361. public function setModSeq($modseq)
  362. {
  363. $this->_data[Horde_Imap_Client::FETCH_MODSEQ] = intval($modseq);
  364. }
  365. /**
  366. * Get the modified sequence value for the message.
  367. *
  368. * @return integer The modseq value.
  369. */
  370. public function getModSeq()
  371. {
  372. return isset($this->_data[Horde_Imap_Client::FETCH_MODSEQ])
  373. ? $this->_data[Horde_Imap_Client::FETCH_MODSEQ]
  374. : null;
  375. }
  376. /**
  377. * Return the internal representation of the data.
  378. *
  379. * @return array The data array.
  380. */
  381. public function getRawData()
  382. {
  383. return $this->_data;
  384. }
  385. /**
  386. * Merge a fetch object into this one.
  387. *
  388. * @param Horde_Imap_Client_Data_Fetch $data A fetch object.
  389. */
  390. public function merge(Horde_Imap_Client_Data_Fetch $data)
  391. {
  392. $this->_data = Horde_Array::replaceRecursive($this->_data, $data->getRawData());
  393. }
  394. /**
  395. * Does this object containing cacheable data of the given type?
  396. *
  397. * @param integer $type The type to query.
  398. *
  399. * @return boolean True if the type is cacheable.
  400. */
  401. public function exists($type)
  402. {
  403. return isset($this->_data[$type]);
  404. }
  405. /**
  406. * Return text representation of a field.
  407. *
  408. * @param boolean $stream Return as a stream?
  409. * @param mixed $data The field data (string or resource) or null if
  410. * field does not exist.
  411. *
  412. * @return mixed Requested text representation.
  413. */
  414. protected function _msgText($stream, $data)
  415. {
  416. if ($stream) {
  417. if (is_resource($data)) {
  418. rewind($data);
  419. return $data;
  420. }
  421. $tmp = fopen('php://temp', 'w+');
  422. if (!is_null($data)) {
  423. fwrite($tmp, $data);
  424. rewind($tmp);
  425. }
  426. return $tmp;
  427. }
  428. if (is_resource($data)) {
  429. rewind($data);
  430. return stream_get_contents($data);
  431. }
  432. return strval($data);
  433. }
  434. /**
  435. * Return representation of a header field.
  436. *
  437. * @param string $id The header id.
  438. * @param integer $format The return format. If self::HEADER_PARSE,
  439. * returns a Horde_Mime_Headers object. If
  440. * self::HEADER_STREAM, returns a stream.
  441. * Otherwise, returns header text.
  442. * @param integer $key The array key where the data is stored in the
  443. * internal array.
  444. *
  445. * @return mixed The data in the format specified by $format.
  446. */
  447. protected function _getHeaders($id, $format, $key)
  448. {
  449. switch ($format) {
  450. case self::HEADER_STREAM:
  451. if (!isset($this->_data[$key][$id])) {
  452. return $this->_msgText(true, null);
  453. } elseif (is_object($this->_data[$key][$id])) {
  454. return $this->_getHeaders($id, 0, $key);
  455. }
  456. return $this->_msgText(true, $this->_data[$key][$id]);
  457. case self::HEADER_PARSE:
  458. if (!isset($this->_data[$key][$id])) {
  459. return new Horde_Mime_Headers();
  460. } elseif (is_object($this->_data[$key][$id])) {
  461. return clone $this->_data[$key][$id];
  462. }
  463. return Horde_Mime_Headers::parseHeaders($this->_getHeaders($id, 0, $key));
  464. }
  465. if (!isset($this->_data[$key][$id])) {
  466. return '';
  467. }
  468. return is_object($this->_data[$key][$id])
  469. ? $this->_data[$key][$id]->toString(array('nowrap' => true))
  470. : $this->_msgText(false, $this->_data[$key][$id]);
  471. }
  472. }