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_imap_search.php 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <?php
  2. /**
  3. +-----------------------------------------------------------------------+
  4. | This file is part of the Roundcube Webmail client |
  5. | |
  6. | Copyright (C) 2013, The Roundcube Dev Team |
  7. | Copyright (C) 2014, Kolab Systems AG |
  8. | |
  9. | Licensed under the GNU General Public License version 3 or |
  10. | any later version with exceptions for skins & plugins. |
  11. | See the README file for a full license statement. |
  12. | |
  13. | PURPOSE: |
  14. | Execute (multi-threaded) searches in multiple IMAP folders |
  15. +-----------------------------------------------------------------------+
  16. | Author: Thomas Bruederli <roundcube@gmail.com> |
  17. +-----------------------------------------------------------------------+
  18. */
  19. /**
  20. * Class to control search jobs on multiple IMAP folders.
  21. *
  22. * @package Framework
  23. * @subpackage Storage
  24. * @author Thomas Bruederli <roundcube@gmail.com>
  25. */
  26. class rcube_imap_search
  27. {
  28. public $options = array();
  29. protected $jobs = array();
  30. protected $timelimit = 0;
  31. protected $results;
  32. protected $conn;
  33. /**
  34. * Default constructor
  35. */
  36. public function __construct($options, $conn)
  37. {
  38. $this->options = $options;
  39. $this->conn = $conn;
  40. }
  41. /**
  42. * Invoke search request to IMAP server
  43. *
  44. * @param array $folders List of IMAP folders to search in
  45. * @param string $str Search criteria
  46. * @param string $charset Search charset
  47. * @param string $sort_field Header field to sort by
  48. * @param boolean $threading True if threaded listing is active
  49. */
  50. public function exec($folders, $str, $charset = null, $sort_field = null, $threading=null)
  51. {
  52. $start = floor(microtime(true));
  53. $results = new rcube_result_multifolder($folders);
  54. // start a search job for every folder to search in
  55. foreach ($folders as $folder) {
  56. // a complete result for this folder already exists
  57. $result = $this->results ? $this->results->get_set($folder) : false;
  58. if ($result && !$result->incomplete) {
  59. $results->add($result);
  60. }
  61. else {
  62. $search = is_array($str) && $str[$folder] ? $str[$folder] : $str;
  63. $job = new rcube_imap_search_job($folder, $search, $charset, $sort_field, $threading);
  64. $job->worker = $this;
  65. $this->jobs[] = $job;
  66. }
  67. }
  68. // execute jobs and gather results
  69. foreach ($this->jobs as $job) {
  70. // only run search if within the configured time limit
  71. // TODO: try to estimate the required time based on folder size and previous search performance
  72. if (!$this->timelimit || floor(microtime(true)) - $start < $this->timelimit) {
  73. $job->run();
  74. }
  75. // add result (may have ->incomplete flag set)
  76. $results->add($job->get_result());
  77. }
  78. return $results;
  79. }
  80. /**
  81. * Setter for timelimt property
  82. */
  83. public function set_timelimit($seconds)
  84. {
  85. $this->timelimit = $seconds;
  86. }
  87. /**
  88. * Setter for previous (potentially incomplete) search results
  89. */
  90. public function set_results($res)
  91. {
  92. $this->results = $res;
  93. }
  94. /**
  95. * Get connection to the IMAP server
  96. * (used for single-thread mode)
  97. */
  98. public function get_imap()
  99. {
  100. return $this->conn;
  101. }
  102. }
  103. /**
  104. * Stackable item to run the search on a specific IMAP folder
  105. */
  106. class rcube_imap_search_job /* extends Stackable */
  107. {
  108. private $folder;
  109. private $search;
  110. private $charset;
  111. private $sort_field;
  112. private $threading;
  113. private $result;
  114. public function __construct($folder, $str, $charset = null, $sort_field = null, $threading=false)
  115. {
  116. $this->folder = $folder;
  117. $this->search = $str;
  118. $this->charset = $charset;
  119. $this->sort_field = $sort_field;
  120. $this->threading = $threading;
  121. $this->result = new rcube_result_index($folder);
  122. $this->result->incomplete = true;
  123. }
  124. public function run()
  125. {
  126. $this->result = $this->search_index();
  127. }
  128. /**
  129. * Copy of rcube_imap::search_index()
  130. */
  131. protected function search_index()
  132. {
  133. $criteria = $this->search;
  134. $charset = $this->charset;
  135. $imap = $this->worker->get_imap();
  136. if (!$imap->connected()) {
  137. trigger_error("No IMAP connection for $this->folder", E_USER_WARNING);
  138. if ($this->threading) {
  139. return new rcube_result_thread($this->folder);
  140. }
  141. else {
  142. return new rcube_result_index($this->folder);
  143. }
  144. }
  145. if ($this->worker->options['skip_deleted'] && !preg_match('/UNDELETED/', $criteria)) {
  146. $criteria = 'UNDELETED '.$criteria;
  147. }
  148. // unset CHARSET if criteria string is ASCII, this way
  149. // SEARCH won't be re-sent after "unsupported charset" response
  150. if ($charset && $charset != 'US-ASCII' && is_ascii($criteria)) {
  151. $charset = 'US-ASCII';
  152. }
  153. if ($this->threading) {
  154. $threads = $imap->thread($this->folder, $this->threading, $criteria, true, $charset);
  155. // Error, try with US-ASCII (RFC5256: SORT/THREAD must support US-ASCII and UTF-8,
  156. // but I've seen that Courier doesn't support UTF-8)
  157. if ($threads->is_error() && $charset && $charset != 'US-ASCII') {
  158. $threads = $imap->thread($this->folder, $this->threading,
  159. rcube_imap::convert_criteria($criteria, $charset), true, 'US-ASCII');
  160. }
  161. return $threads;
  162. }
  163. if ($this->sort_field) {
  164. $messages = $imap->sort($this->folder, $this->sort_field, $criteria, true, $charset);
  165. // Error, try with US-ASCII (RFC5256: SORT/THREAD must support US-ASCII and UTF-8,
  166. // but I've seen Courier with disabled UTF-8 support)
  167. if ($messages->is_error() && $charset && $charset != 'US-ASCII') {
  168. $messages = $imap->sort($this->folder, $this->sort_field,
  169. rcube_imap::convert_criteria($criteria, $charset), true, 'US-ASCII');
  170. }
  171. }
  172. if (!$messages || $messages->is_error()) {
  173. $messages = $imap->search($this->folder,
  174. ($charset && $charset != 'US-ASCII' ? "CHARSET $charset " : '') . $criteria, true);
  175. // Error, try with US-ASCII (some servers may support only US-ASCII)
  176. if ($messages->is_error() && $charset && $charset != 'US-ASCII') {
  177. $messages = $imap->search($this->folder,
  178. rcube_imap::convert_criteria($criteria, $charset), true);
  179. }
  180. }
  181. return $messages;
  182. }
  183. public function get_search_set()
  184. {
  185. return array(
  186. $this->search,
  187. $this->result,
  188. $this->charset,
  189. $this->sort_field,
  190. $this->threading,
  191. );
  192. }
  193. public function get_result()
  194. {
  195. return $this->result;
  196. }
  197. }