You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

rcube_result_index.php 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. <?php
  2. /**
  3. +-----------------------------------------------------------------------+
  4. | This file is part of the Roundcube Webmail client |
  5. | Copyright (C) 2005-2011, The Roundcube Dev Team |
  6. | Copyright (C) 2011, Kolab Systems AG |
  7. | |
  8. | Licensed under the GNU General Public License version 3 or |
  9. | any later version with exceptions for skins & plugins. |
  10. | See the README file for a full license statement. |
  11. | |
  12. | PURPOSE: |
  13. | SORT/SEARCH/ESEARCH response handler |
  14. +-----------------------------------------------------------------------+
  15. | Author: Thomas Bruederli <roundcube@gmail.com> |
  16. | Author: Aleksander Machniak <alec@alec.pl> |
  17. +-----------------------------------------------------------------------+
  18. */
  19. /**
  20. * Class for accessing IMAP's SORT/SEARCH/ESEARCH result
  21. *
  22. * @package Framework
  23. * @subpackage Storage
  24. */
  25. class rcube_result_index
  26. {
  27. public $incomplete = false;
  28. protected $raw_data;
  29. protected $mailbox;
  30. protected $meta = array();
  31. protected $params = array();
  32. protected $order = 'ASC';
  33. const SEPARATOR_ELEMENT = ' ';
  34. /**
  35. * Object constructor.
  36. */
  37. public function __construct($mailbox = null, $data = null, $order = null)
  38. {
  39. $this->mailbox = $mailbox;
  40. $this->order = $order == 'DESC' ? 'DESC' : 'ASC';
  41. $this->init($data);
  42. }
  43. /**
  44. * Initializes object with SORT command response
  45. *
  46. * @param string $data IMAP response string
  47. */
  48. public function init($data = null)
  49. {
  50. $this->meta = array();
  51. $data = explode('*', (string)$data);
  52. // ...skip unilateral untagged server responses
  53. for ($i=0, $len=count($data); $i<$len; $i++) {
  54. $data_item = &$data[$i];
  55. if (preg_match('/^ SORT/i', $data_item)) {
  56. // valid response, initialize raw_data for is_error()
  57. $this->raw_data = '';
  58. $data_item = substr($data_item, 5);
  59. break;
  60. }
  61. else if (preg_match('/^ (E?SEARCH)/i', $data_item, $m)) {
  62. // valid response, initialize raw_data for is_error()
  63. $this->raw_data = '';
  64. $data_item = substr($data_item, strlen($m[0]));
  65. if (strtoupper($m[1]) == 'ESEARCH') {
  66. $data_item = trim($data_item);
  67. // remove MODSEQ response
  68. if (preg_match('/\(MODSEQ ([0-9]+)\)$/i', $data_item, $m)) {
  69. $data_item = substr($data_item, 0, -strlen($m[0]));
  70. $this->params['MODSEQ'] = $m[1];
  71. }
  72. // remove TAG response part
  73. if (preg_match('/^\(TAG ["a-z0-9]+\)\s*/i', $data_item, $m)) {
  74. $data_item = substr($data_item, strlen($m[0]));
  75. }
  76. // remove UID
  77. $data_item = preg_replace('/^UID\s*/i', '', $data_item);
  78. // ESEARCH parameters
  79. while (preg_match('/^([a-z]+) ([0-9:,]+)\s*/i', $data_item, $m)) {
  80. $param = strtoupper($m[1]);
  81. $value = $m[2];
  82. $this->params[$param] = $value;
  83. $data_item = substr($data_item, strlen($m[0]));
  84. if (in_array($param, array('COUNT', 'MIN', 'MAX'))) {
  85. $this->meta[strtolower($param)] = (int) $value;
  86. }
  87. }
  88. // @TODO: Implement compression using compressMessageSet() in __sleep() and __wakeup() ?
  89. // @TODO: work with compressed result?!
  90. if (isset($this->params['ALL'])) {
  91. $data_item = implode(self::SEPARATOR_ELEMENT,
  92. rcube_imap_generic::uncompressMessageSet($this->params['ALL']));
  93. }
  94. }
  95. break;
  96. }
  97. unset($data[$i]);
  98. }
  99. $data = array_filter($data);
  100. if (empty($data)) {
  101. return;
  102. }
  103. $data = array_shift($data);
  104. $data = trim($data);
  105. $data = preg_replace('/[\r\n]/', '', $data);
  106. $data = preg_replace('/\s+/', ' ', $data);
  107. $this->raw_data = $data;
  108. }
  109. /**
  110. * Checks the result from IMAP command
  111. *
  112. * @return bool True if the result is an error, False otherwise
  113. */
  114. public function is_error()
  115. {
  116. return $this->raw_data === null;
  117. }
  118. /**
  119. * Checks if the result is empty
  120. *
  121. * @return bool True if the result is empty, False otherwise
  122. */
  123. public function is_empty()
  124. {
  125. return empty($this->raw_data);
  126. }
  127. /**
  128. * Returns number of elements in the result
  129. *
  130. * @return int Number of elements
  131. */
  132. public function count()
  133. {
  134. if ($this->meta['count'] !== null)
  135. return $this->meta['count'];
  136. if (empty($this->raw_data)) {
  137. $this->meta['count'] = 0;
  138. $this->meta['length'] = 0;
  139. }
  140. else {
  141. $this->meta['count'] = 1 + substr_count($this->raw_data, self::SEPARATOR_ELEMENT);
  142. }
  143. return $this->meta['count'];
  144. }
  145. /**
  146. * Returns number of elements in the result.
  147. * Alias for count() for compatibility with rcube_result_thread
  148. *
  149. * @return int Number of elements
  150. */
  151. public function count_messages()
  152. {
  153. return $this->count();
  154. }
  155. /**
  156. * Returns maximal message identifier in the result
  157. *
  158. * @return int Maximal message identifier
  159. */
  160. public function max()
  161. {
  162. if (!isset($this->meta['max'])) {
  163. $this->meta['max'] = (int) @max($this->get());
  164. }
  165. return $this->meta['max'];
  166. }
  167. /**
  168. * Returns minimal message identifier in the result
  169. *
  170. * @return int Minimal message identifier
  171. */
  172. public function min()
  173. {
  174. if (!isset($this->meta['min'])) {
  175. $this->meta['min'] = (int) @min($this->get());
  176. }
  177. return $this->meta['min'];
  178. }
  179. /**
  180. * Slices data set.
  181. *
  182. * @param $offset Offset (as for PHP's array_slice())
  183. * @param $length Number of elements (as for PHP's array_slice())
  184. */
  185. public function slice($offset, $length)
  186. {
  187. $data = $this->get();
  188. $data = array_slice($data, $offset, $length);
  189. $this->meta = array();
  190. $this->meta['count'] = count($data);
  191. $this->raw_data = implode(self::SEPARATOR_ELEMENT, $data);
  192. }
  193. /**
  194. * Filters data set. Removes elements not listed in $ids list.
  195. *
  196. * @param array $ids List of IDs to remove.
  197. */
  198. public function filter($ids = array())
  199. {
  200. $data = $this->get();
  201. $data = array_intersect($data, $ids);
  202. $this->meta = array();
  203. $this->meta['count'] = count($data);
  204. $this->raw_data = implode(self::SEPARATOR_ELEMENT, $data);
  205. }
  206. /**
  207. * Reverts order of elements in the result
  208. */
  209. public function revert()
  210. {
  211. $this->order = $this->order == 'ASC' ? 'DESC' : 'ASC';
  212. if (empty($this->raw_data)) {
  213. return;
  214. }
  215. $data = $this->get();
  216. $data = array_reverse($data);
  217. $this->raw_data = implode(self::SEPARATOR_ELEMENT, $data);
  218. $this->meta['pos'] = array();
  219. }
  220. /**
  221. * Check if the given message ID exists in the object
  222. *
  223. * @param int $msgid Message ID
  224. * @param bool $get_index When enabled element's index will be returned.
  225. * Elements are indexed starting with 0
  226. *
  227. * @return mixed False if message ID doesn't exist, True if exists or
  228. * index of the element if $get_index=true
  229. */
  230. public function exists($msgid, $get_index = false)
  231. {
  232. if (empty($this->raw_data)) {
  233. return false;
  234. }
  235. $msgid = (int) $msgid;
  236. $begin = implode('|', array('^', preg_quote(self::SEPARATOR_ELEMENT, '/')));
  237. $end = implode('|', array('$', preg_quote(self::SEPARATOR_ELEMENT, '/')));
  238. if (preg_match("/($begin)$msgid($end)/", $this->raw_data, $m,
  239. $get_index ? PREG_OFFSET_CAPTURE : null)
  240. ) {
  241. if ($get_index) {
  242. $idx = 0;
  243. if ($m[0][1]) {
  244. $idx = 1 + substr_count($this->raw_data, self::SEPARATOR_ELEMENT, 0, $m[0][1]);
  245. }
  246. // cache position of this element, so we can use it in get_element()
  247. $this->meta['pos'][$idx] = (int)$m[0][1];
  248. return $idx;
  249. }
  250. return true;
  251. }
  252. return false;
  253. }
  254. /**
  255. * Return all messages in the result.
  256. *
  257. * @return array List of message IDs
  258. */
  259. public function get()
  260. {
  261. if (empty($this->raw_data)) {
  262. return array();
  263. }
  264. return explode(self::SEPARATOR_ELEMENT, $this->raw_data);
  265. }
  266. /**
  267. * Return all messages in the result.
  268. *
  269. * @return array List of message IDs
  270. */
  271. public function get_compressed()
  272. {
  273. if (empty($this->raw_data)) {
  274. return '';
  275. }
  276. return rcube_imap_generic::compressMessageSet($this->get());
  277. }
  278. /**
  279. * Return result element at specified index
  280. *
  281. * @param int|string $index Element's index or "FIRST" or "LAST"
  282. *
  283. * @return int Element value
  284. */
  285. public function get_element($index)
  286. {
  287. $count = $this->count();
  288. if (!$count) {
  289. return null;
  290. }
  291. // first element
  292. if ($index === 0 || $index === '0' || $index === 'FIRST') {
  293. $pos = strpos($this->raw_data, self::SEPARATOR_ELEMENT);
  294. if ($pos === false)
  295. $result = (int) $this->raw_data;
  296. else
  297. $result = (int) substr($this->raw_data, 0, $pos);
  298. return $result;
  299. }
  300. // last element
  301. if ($index === 'LAST' || $index == $count-1) {
  302. $pos = strrpos($this->raw_data, self::SEPARATOR_ELEMENT);
  303. if ($pos === false)
  304. $result = (int) $this->raw_data;
  305. else
  306. $result = (int) substr($this->raw_data, $pos);
  307. return $result;
  308. }
  309. // do we know the position of the element or the neighbour of it?
  310. if (!empty($this->meta['pos'])) {
  311. if (isset($this->meta['pos'][$index]))
  312. $pos = $this->meta['pos'][$index];
  313. else if (isset($this->meta['pos'][$index-1]))
  314. $pos = strpos($this->raw_data, self::SEPARATOR_ELEMENT,
  315. $this->meta['pos'][$index-1] + 1);
  316. else if (isset($this->meta['pos'][$index+1]))
  317. $pos = strrpos($this->raw_data, self::SEPARATOR_ELEMENT,
  318. $this->meta['pos'][$index+1] - $this->length() - 1);
  319. if (isset($pos) && preg_match('/([0-9]+)/', $this->raw_data, $m, null, $pos)) {
  320. return (int) $m[1];
  321. }
  322. }
  323. // Finally use less effective method
  324. $data = explode(self::SEPARATOR_ELEMENT, $this->raw_data);
  325. return $data[$index];
  326. }
  327. /**
  328. * Returns response parameters, e.g. ESEARCH's MIN/MAX/COUNT/ALL/MODSEQ
  329. * or internal data e.g. MAILBOX, ORDER
  330. *
  331. * @param string $param Parameter name
  332. *
  333. * @return array|string Response parameters or parameter value
  334. */
  335. public function get_parameters($param=null)
  336. {
  337. $params = $this->params;
  338. $params['MAILBOX'] = $this->mailbox;
  339. $params['ORDER'] = $this->order;
  340. if ($param !== null) {
  341. return $params[$param];
  342. }
  343. return $params;
  344. }
  345. /**
  346. * Returns length of internal data representation
  347. *
  348. * @return int Data length
  349. */
  350. protected function length()
  351. {
  352. if (!isset($this->meta['length'])) {
  353. $this->meta['length'] = strlen($this->raw_data);
  354. }
  355. return $this->meta['length'];
  356. }
  357. }