PageRenderTime 49ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/wrobel/horde
PHP | 372 lines | 136 code | 33 blank | 203 comment | 4 complexity | 0e2d2436d7d884a61144096311366fe9 MD5 | raw file
Possible License(s): BSD-2-Clause, AGPL-1.0, LGPL-2.1, LGPL-3.0, BSD-3-Clause, LGPL-2.0, GPL-2.0
  1. <?php
  2. /**
  3. * Fetch query object for use with Horde_Imap_Client_Base#query().
  4. *
  5. * Copyright 2011-2012 Horde LLC (http://www.horde.org/)
  6. *
  7. * See the enclosed file COPYING for license information (LGPL). If you
  8. * did not receive this file, see http://www.horde.org/licenses/lgpl21.
  9. *
  10. * @author Michael Slusarz <slusarz@horde.org>
  11. * @category Horde
  12. * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
  13. * @package Imap_Client
  14. */
  15. class Horde_Imap_Client_Fetch_Query implements ArrayAccess, Countable, Iterator
  16. {
  17. /**
  18. * Internal data array.
  19. *
  20. * @var array
  21. */
  22. protected $_data = array();
  23. /**
  24. * Get the full text of the message.
  25. *
  26. * @param array $opts The following options are available:
  27. * - length: (integer) The length of the substring to return.
  28. * DEFAULT: The entire text is returned.
  29. * - peek: (boolean) If set, does not set the '\Seen' flag on the
  30. * message.
  31. * DEFAULT: The seen flag is set.
  32. * - start: (integer) If a portion of the full text is desired to be
  33. * returned, the starting position is identified here.
  34. * DEFAULT: The entire text is returned.
  35. */
  36. public function fullText(array $opts = array())
  37. {
  38. $this->_data[Horde_Imap_Client::FETCH_FULLMSG] = $opts;
  39. }
  40. /**
  41. * Return header text.
  42. *
  43. * Header text is defined only for the base RFC 2822 message or
  44. * message/rfc822 parts.
  45. *
  46. * @param array $opts The following options are available:
  47. * - id: (string) The MIME ID to obtain the header text for.
  48. * DEFAULT: The header text for the base message will be
  49. * returned.
  50. * - length: (integer) The length of the substring to return.
  51. * DEFAULT: The entire text is returned.
  52. * - peek: (boolean) If set, does not set the '\Seen' flag on the
  53. * message.
  54. * DEFAULT: The seen flag is set.
  55. * - start: (integer) If a portion of the full text is desired to be
  56. * returned, the starting position is identified here.
  57. * DEFAULT: The entire text is returned.
  58. */
  59. public function headerText(array $opts = array())
  60. {
  61. $id = isset($opts['id'])
  62. ? $opts['id']
  63. : 0;
  64. $this->_data[Horde_Imap_Client::FETCH_HEADERTEXT][$id] = $opts;
  65. }
  66. /**
  67. * Return body text.
  68. *
  69. * Body text is defined only for the base RFC 2822 message or
  70. * message/rfc822 parts.
  71. *
  72. * @param array $opts The following options are available:
  73. * - id: (string) The MIME ID to obtain the body text for.
  74. * DEFAULT: The body text for the entire message will be
  75. * returned.
  76. * - length: (integer) The length of the substring to return.
  77. * DEFAULT: The entire text is returned.
  78. * - peek: (boolean) If set, does not set the '\Seen' flag on the
  79. * message.
  80. * DEFAULT: The seen flag is set.
  81. * - start: (integer) If a portion of the full text is desired to be
  82. * returned, the starting position is identified here.
  83. * DEFAULT: The entire text is returned.
  84. */
  85. public function bodyText(array $opts = array())
  86. {
  87. $id = isset($opts['id'])
  88. ? $opts['id']
  89. : 0;
  90. $this->_data[Horde_Imap_Client::FETCH_BODYTEXT][$id] = $opts;
  91. }
  92. /**
  93. * Return MIME header text.
  94. *
  95. * MIME header text is defined only for non-RFC 2822 messages and
  96. * non-message/rfc822 parts.
  97. *
  98. * @param string $id The MIME ID to obtain the MIME header text for.
  99. * @param array $opts The following options are available:
  100. * - length: (integer) The length of the substring to return.
  101. * DEFAULT: The entire text is returned.
  102. * - peek: (boolean) If set, does not set the '\Seen' flag on the
  103. * message.
  104. * DEFAULT: The seen flag is set.
  105. * - start: (integer) If a portion of the full text is desired to be
  106. * returned, the starting position is identified here.
  107. * DEFAULT: The entire text is returned.
  108. */
  109. public function mimeHeader($id, array $opts = array())
  110. {
  111. $this->_data[Horde_Imap_Client::FETCH_MIMEHEADER][$id] = $opts;
  112. }
  113. /**
  114. * Return the body part data for a MIME ID.
  115. *
  116. * @param string $id The MIME ID to obtain the body part text for.
  117. * @param array $opts The following options are available:
  118. * - decode: (boolean) Attempt to server-side decode the bodypart data
  119. * if it is MIME transfer encoded.
  120. * DEFAULT: false
  121. * - length: (integer) The length of the substring to return.
  122. * DEFAULT: The entire text is returned.
  123. * - peek: (boolean) If set, does not set the '\Seen' flag on the
  124. * message.
  125. * DEFAULT: The seen flag is set.
  126. * - start: (integer) If a portion of the full text is desired to be
  127. * returned, the starting position is identified here.
  128. * DEFAULT: The entire text is returned.
  129. */
  130. public function bodyPart($id, array $opts = array())
  131. {
  132. $this->_data[Horde_Imap_Client::FETCH_BODYPART][$id] = $opts;
  133. }
  134. /**
  135. * Returns the decoded body part size for a MIME ID.
  136. *
  137. * @param string $id The MIME ID to obtain the decoded body part size
  138. * for.
  139. */
  140. public function bodyPartSize($id)
  141. {
  142. $this->_data[Horde_Imap_Client::FETCH_BODYPARTSIZE][$id] = true;
  143. }
  144. /**
  145. * Returns RFC 2822 header text that matches a search string.
  146. *
  147. * This header search work only with the base RFC 2822 message or
  148. * message/rfc822 parts.
  149. *
  150. * @param string $label A unique label associated with this particular
  151. * search. This is how the results are stored.
  152. * @param array $search The search string(s) (case-insensitive).
  153. * @param array $opts The following options are available:
  154. * - cache: (boolean) If true, and 'peek' is also true, will cache
  155. * the result of this call.
  156. * DEFAULT: false
  157. * - id: (string) The MIME ID to search.
  158. * DEFAULT: The base message part
  159. * - length: (integer) The length of the substring to return.
  160. * DEFAULT: The entire text is returned.
  161. * - notsearch: (boolean) Do a 'NOT' search on the headers.
  162. * DEFAULT: false
  163. * - peek: (boolean) If set, does not set the '\Seen' flag on the
  164. * message.
  165. * DEFAULT: The seen flag is set.
  166. * - start: (integer) If a portion of the full text is desired to be
  167. * returned, the starting position is identified here.
  168. * DEFAULT: The entire text is returned.
  169. */
  170. public function headers($label, $search, array $opts = array())
  171. {
  172. $this->_data[Horde_Imap_Client::FETCH_HEADERS][$label] = array_merge($opts, array(
  173. 'headers' => $search
  174. ));
  175. }
  176. /**
  177. * Return MIME structure information.
  178. */
  179. public function structure()
  180. {
  181. $this->_data[Horde_Imap_Client::FETCH_STRUCTURE] = true;
  182. }
  183. /**
  184. * Return envelope header data.
  185. */
  186. public function envelope()
  187. {
  188. $this->_data[Horde_Imap_Client::FETCH_ENVELOPE] = true;
  189. }
  190. /**
  191. * Return flags set for the message.
  192. */
  193. public function flags()
  194. {
  195. $this->_data[Horde_Imap_Client::FETCH_FLAGS] = true;
  196. }
  197. /**
  198. * Return the internal (IMAP) date of the message.
  199. */
  200. public function imapDate()
  201. {
  202. $this->_data[Horde_Imap_Client::FETCH_IMAPDATE] = true;
  203. }
  204. /**
  205. * Return the size (in bytes) of the message.
  206. */
  207. public function size()
  208. {
  209. $this->_data[Horde_Imap_Client::FETCH_SIZE] = true;
  210. }
  211. /**
  212. * Return the unique ID of the message.
  213. */
  214. public function uid()
  215. {
  216. $this->_data[Horde_Imap_Client::FETCH_UID] = true;
  217. }
  218. /**
  219. * Return the sequence number of the message.
  220. */
  221. public function seq()
  222. {
  223. $this->_data[Horde_Imap_Client::FETCH_SEQ] = true;
  224. }
  225. /**
  226. * Return the mod-sequence value for the message.
  227. *
  228. * The server must support the CONDSTORE IMAP extension, and the mailbox
  229. * must support mod-sequences.
  230. */
  231. public function modseq()
  232. {
  233. $this->_data[Horde_Imap_Client::FETCH_MODSEQ] = true;
  234. }
  235. /**
  236. * Does the query contain the given criteria?
  237. *
  238. * @param integer $criteria The criteria to remove.
  239. *
  240. * @return boolean True if the query contains the given criteria.
  241. */
  242. public function contains($criteria)
  243. {
  244. return isset($this->_data[$criteria]);
  245. }
  246. /**
  247. * Remove an entry under a given criteria.
  248. *
  249. * @param integer $criteria Criteria ID.
  250. * @param string $key The key to remove.
  251. */
  252. public function remove($criteria, $key)
  253. {
  254. if (isset($this->_data[$criteria]) &&
  255. is_array($this->_data[$criteria])) {
  256. unset($this->_data[$criteria][$key]);
  257. if (empty($this->_data[$criteria])) {
  258. unset($this->_data[$criteria]);
  259. }
  260. }
  261. }
  262. /**
  263. * Returns a MD5 hash of the current query object.
  264. *
  265. * @return string MD5 hash.
  266. */
  267. public function hash()
  268. {
  269. return hash('md5', serialize($this));
  270. }
  271. /* ArrayAccess methods. */
  272. /**
  273. */
  274. public function offsetExists($offset)
  275. {
  276. return isset($this->_data[$offset]);
  277. }
  278. /**
  279. */
  280. public function offsetGet($offset)
  281. {
  282. return isset($this->_data[$offset])
  283. ? $this->_data[$offset]
  284. : null;
  285. }
  286. /**
  287. */
  288. public function offsetSet($offset, $value)
  289. {
  290. $this->_data[$offset] = $value;
  291. }
  292. /**
  293. */
  294. public function offsetUnset($offset)
  295. {
  296. unset($this->_data[$offset]);
  297. }
  298. /* Countable methods. */
  299. /**
  300. */
  301. public function count()
  302. {
  303. return count($this->_data);
  304. }
  305. /* Iterator methods. */
  306. /**
  307. */
  308. public function current()
  309. {
  310. $opts = current($this->_data);
  311. return (!empty($opts) && ($this->key() == Horde_Imap_Client::FETCH_BODYPARTSIZE))
  312. ? array_keys($opts)
  313. : $opts;
  314. }
  315. /**
  316. */
  317. public function key()
  318. {
  319. return key($this->_data);
  320. }
  321. /**
  322. */
  323. public function next()
  324. {
  325. next($this->_data);
  326. }
  327. /**
  328. */
  329. public function rewind()
  330. {
  331. reset($this->_data);
  332. }
  333. /**
  334. */
  335. public function valid()
  336. {
  337. return !is_null($this->key());
  338. }
  339. }