6c0cc5a3119ff87afeedace0565582026af6d65a
6 * Copyright (c) 1999-2003 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
9 * IMAP asearch routines
10 * @author Alex Lemaresquier - Brainstorm - alex at brainstorm.fr
11 * See README file for infos.
12 * @package squirrelmail
16 /** This functionality requires the IMAP and date functions */
17 require_once(SM_PATH
. 'functions/imap_general.php');
18 require_once(SM_PATH
. 'functions/date.php');
20 /** Set to TRUE to dump the imap dialogue */
21 $imap_asearch_debug_dump = FALSE;
23 $imap_asearch_opcodes = array(
24 /* <message set> => 'asequence', */
25 /*'ALL' is binary operator */
35 'HEADER' => 'afield', /* Special syntax for this one, see below */
36 'KEYWORD' => 'akeyword',
39 /*'NOT' is unary operator */
42 /*'OR' is binary operator */
45 'SENTBEFORE' => 'adate',
47 'SENTSINCE' => 'adate',
50 'SUBJECT' => 'astring',
58 'UNKEYWORD' => 'akeyword',
62 $imap_asearch_months = array(
77 $imap_error_titles = array(
79 'NO' => _("ERROR : Could not complete request."),
80 'BAD' => _("ERROR : Bad or malformed request."),
81 'BYE' => _("ERROR : Imap server closed the connection.")
84 // why can't this just use sqimap_error_box() ?
85 // It does, indeed I isolated sqimap_error_box() as a stand-alone function just for this purpose ;)
86 function sqimap_asearch_error_box($response, $query, $message)
88 global $imap_error_titles;
90 //if (!array_key_exists($response, $imap_error_titles)) //php 4.0.6 compatibility
91 if (!in_array($response, array_keys($imap_error_titles)))
92 $title = _("ERROR : Unknown imap response.");
94 $title = $imap_error_titles[$response];
95 $message_title = _("Reason Given: ");
96 if (function_exists('sqimap_error_box'))
97 sqimap_error_box($title, $query, $message_title, $message);
98 else { //Straight copy of 1.5 imap_general.php:sqimap_error_box(). Can be removed at a later time
100 require_once(SM_PATH
. 'functions/display_messages.php');
101 $string = "<font color=$color[2]><b>\n" . $title . "</b><br>\n";
103 $string .= _("Query:") . ' ' . htmlspecialchars($query) . '<br>';
104 if ($message_title != '')
105 $string .= $message_title;
107 $string .= htmlspecialchars($message);
108 $string .= "</font><br>\n";
109 error_box($string,$color);
114 * This is to avoid the E_NOTICE warnings signaled by marc AT squirrelmail.org. Thanks Marc!
116 function asearch_nz(&$var)
124 * This should give the same results as PHP 4 >= 4.3.0's html_entity_decode(),
125 * except it doesn't handle hex constructs
127 function asearch_unhtmlentities($string) {
128 $trans_tbl = array_flip(get_html_translation_table(HTML_ENTITIES
));
129 for ($i=127; $i<255; $i++
) /* Add &#<dec>; entities */
130 $trans_tbl['&#' . $i . ';'] = chr($i);
131 return strtr($string, $trans_tbl);
132 /* I think the one above is quicker, though it should be benchmarked
133 $string = strtr($string, array_flip(get_html_translation_table(HTML_ENTITIES)));
134 return preg_replace("/&#([0-9]+);/E", "chr('\\1')", $string);
138 function s_debug_dump($var_name, $var_var)
140 global $imap_asearch_debug_dump;
141 if ($imap_asearch_debug_dump) {
142 if (function_exists('sm_print_r')) //Only exists since 1.4.2
143 sm_print_r($var_name, $var_var); //Better be the 'varargs' version ;)
146 echo htmlentities($var_name);
155 A quoted string is a sequence of zero or more 7-bit characters,
156 excluding CR and LF, with double quote (<">) characters at each end.
158 quoted-specials = DQUOTE / "\"
160 function sqimap_asearch_encode_string($what, $charset)
162 if (strtoupper($charset) == 'ISO-2022-JP') // This should be now handled in imap_utf7_local?
163 $what = mb_convert_encoding($what, 'JIS', 'auto');
164 //if (ereg("[\"\\\r\n\x80-\xff]", $what))
165 if (preg_match('/["\\\\\r\n\x80-\xff]/', $what))
166 return '{' . strlen($what) . "}\r\n" . $what; // 4.3 literal form
167 return '"' . $what . '"'; // 4.3 quoted string form
171 * Parses a user date string into an rfc2060 date string
172 * (<day number>-<US month TLA>-<4 digit year>).
173 * Returns a preg_match-style array: [0]: fully formatted date,
174 * [1]: day, [2]: month, [3]: year
175 * Handles space, slash, backslash, dot and comma as separators (and dash of course ;=)
177 function sqimap_asearch_parse_date($what)
179 global $imap_asearch_months;
182 $what = ereg_replace('[ /\\.,]+', '-', $what);
184 preg_match('/^([0-9]+)-+([^\-]+)-+([0-9]+)$/', $what, $what_parts);
185 if (count($what_parts) == 4) {
186 $what_month = strtolower(asearch_unhtmlentities($what_parts[2]));
187 /* if (!in_array($what_month, $imap_asearch_months)) {*/
188 foreach ($imap_asearch_months as $month_number => $month_code) {
189 if (($what_month == $month_number)
190 ||
($what_month == $month_code)
191 ||
($what_month == strtolower(asearch_unhtmlentities(getMonthName($month_number))))
192 ||
($what_month == strtolower(asearch_unhtmlentities(getMonthAbrv($month_number))))
194 $what_parts[2] = $month_number;
195 $what_parts[0] = $what_parts[1] . '-' . $month_code . '-' . $what_parts[3];
203 $what_parts = array();
207 function sqimap_asearch_build_criteria($opcode, $what, $charset)
209 global $imap_asearch_opcodes;
212 switch ($imap_asearch_opcodes[$opcode]) {
215 /* $what = str_replace(' ', '', $what);*/
216 $what = ereg_replace('[^0-9]+', '', $what);
218 $criteria = $opcode . ' ' . $what . ' ';
221 $criteria = $opcode . ' ';
223 case 'afield': /* HEADER field-name: field-body */
224 preg_match('/^([^:]+):(.*)$/', $what, $what_parts);
225 if (count($what_parts) == 3)
226 $criteria = $opcode . ' ' .
227 sqimap_asearch_encode_string($what_parts[1], $charset) . ' ' .
228 sqimap_asearch_encode_string($what_parts[2], $charset) . ' ';
231 $what_parts = sqimap_asearch_parse_date($what);
232 if (isset($what_parts[0]))
233 $criteria = $opcode . ' ' . $what_parts[0] . ' ';
237 $criteria = $opcode . ' ' . sqimap_asearch_encode_string($what, $charset) . ' ';
240 $what = ereg_replace('[^0-9:\(\)]+', '', $what);
242 $criteria = $opcode . ' ' . $what . ' ';
248 // equivalent to : $to = array_values(array_unique(array_merge($to, $from)));
249 function sqimap_array_merge_unique($to, $from)
253 $count = count($from);
254 for ($i = 0; $i < $count; $i++
) {
255 if (!in_array($from[$i], $to))
261 function sqimap_run_search($imapConnection, $search_string, $search_charset)
265 /* 6.4.4 try OPTIONAL [CHARSET] specification first */
266 if ($search_charset != '')
267 $query = 'SEARCH CHARSET "' . strtoupper($search_charset) . '" ALL ' . $search_string;
269 $query = 'SEARCH ALL ' . $search_string;
270 s_debug_dump('C:', $query);
271 $readin = sqimap_run_command($imapConnection, $query, false, $response, $message, $uid_support);
273 /* 6.4.4 try US-ASCII charset if we tried an OPTIONAL [CHARSET] and received a tagged NO response (SHOULD be [BADCHARSET]) */
274 if (($search_charset != '') && (strtoupper($response) == 'NO')) {
275 $query = 'SEARCH CHARSET US-ASCII ALL ' . $search_string;
276 s_debug_dump('C:', $query);
277 $readin = sqimap_run_command($imapConnection, $query, false, $response, $message, $uid_support);
279 if (strtoupper($response) != 'OK') {
280 sqimap_asearch_error_box($response, $query, $message);
286 // Keep going till we find the * SEARCH response
287 foreach ($readin as $readin_part) {
288 s_debug_dump('S:', $readin_part);
289 if (substr($readin_part, 0, 9) == '* SEARCH ') {
290 //workaround for an EIMS singularity (to say the least)
291 //$messagelist = preg_split("/ /", substr($readin_part, 9));
292 //break; //this should be the last anyway
293 $messagelist = sqimap_array_merge_unique($message_list, preg_split("/ /", substr($readin_part, 9)));
297 if (empty($messagelist)) //Empty search response, ie '* SEARCH'
300 $cnt = count($messagelist);
301 for ($q = 0; $q < $cnt; $q++
)
302 $id[$q] = trim($messagelist[$q]);
306 function sqimap_run_sort($imapConnection, $search_string, $search_charset, $sort_criteria)
310 if ($search_charset == '')
311 $search_charset = 'US-ASCII';
312 $query = 'SORT (' . $sort_criteria . ') ' . strtoupper($search_charset) . ' ALL ' . $search_string;
313 s_debug_dump('C:', $query);
314 $readin = sqimap_run_command($imapConnection, $query, false, $response, $message, $uid_support);
316 /* 6.4 try US-ASCII charset if we received a tagged NO response (SHOULD be [BADCHARSET]) */
317 if (($search_charset != 'US-ASCII') && (strtoupper($response) == 'NO')) {
318 $query = 'SORT (' . $sort_criteria . ') US-ASCII ALL ' . $search_string;
319 s_debug_dump('C:', $query);
320 $readin = sqimap_run_command($imapConnection, $query, false, $response, $message, $uid_support);
323 if (strtoupper($response) != 'OK') {
324 // sqimap_asearch_error_box($response, $query, $message);
326 return sqimap_run_search($imapConnection, $search_string, $search_charset); // Fell back to standard search
329 /* Keep going till we find the * SORT response */
330 foreach ($readin as $readin_part) {
331 s_debug_dump('S:', $readin_part);
332 if (substr($readin_part, 0, 7) == '* SORT ') {
333 $messagelist = preg_split("/ /", substr($readin_part, 7));
334 break; // Should be the last anyway
338 if (empty($messagelist)) //Empty search response, ie '* SORT'
341 $cnt = count($messagelist);
342 for ($q = 0; $q < $cnt; $q++
)
343 $id[$q] = trim($messagelist[$q]);
347 function sqimap_run_thread($imapConnection, $search_string, $search_charset, $thread_algorithm)
349 global $thread_new, $server_sort_array;
351 if (sqsession_is_registered('thread_new'))
352 sqsession_unregister('thread_new');
353 if (sqsession_is_registered('server_sort_array'))
354 sqsession_unregister('server_sort_array');
356 $thread_new = array();
359 $server_sort_array = array();
363 if ($search_charset == '')
364 $search_charset = 'US-ASCII';
365 $query = 'THREAD ' . $thread_algorithm . ' ' . strtoupper($search_charset) . ' ALL ' . $search_string;
366 s_debug_dump('C:', $query);
367 $readin = sqimap_run_command($imapConnection, $query, false, $response, $message, $uid_support);
369 /* 6.4 try US-ASCII charset if we received a tagged NO response (SHOULD be [BADCHARSET]) */
370 if (($search_charset != 'US-ASCII') && (strtoupper($response) == 'NO')) {
371 $query = 'THREAD ' . $thread_algorithm . ' US-ASCII ALL ' . $search_string;
372 s_debug_dump('C:', $query);
373 $readin = sqimap_run_command($imapConnection, $query, false, $response, $message, $uid_support);
376 if (strtoupper($response) != 'OK') {
377 /* we should at this point:
378 - warn the user that the THREAD call has failed
379 - (offer him a way to) disconnect it permanently in the prefs
380 - perform the regular search instead or provide a way to do it in one click
382 // sqimap_asearch_error_box($response, $query, $message);
384 return sqimap_run_search($imapConnection, $search_string, $search_charset); // Fell back to standard search
387 /* Keep going till we find the * THREAD response */
388 foreach ($readin as $readin_part) {
389 s_debug_dump('S:', $readin_part);
390 if (substr($readin_part, 0, 9) == '* THREAD ') {
391 $thread_temp = preg_split("//", substr($readin_part, 9), -1, PREG_SPLIT_NO_EMPTY
);
392 break; // Should be the last anyway
396 if (empty($thread_temp)) //Empty search response, ie '* THREAD'
399 $char_count = count($thread_temp);
402 for ($i=0;$i<$char_count;$i++
) {
403 if ($thread_temp[$i] != ')' && $thread_temp[$i] != '(') {
404 $thread_new[$k] = $thread_new[$k] . $thread_temp[$i];
406 elseif ($thread_temp[$i] == '(') {
407 $thread_new[$k] .= $thread_temp[$i];
410 elseif ($thread_temp[$i] == ')') {
412 $thread_new[$k] .= $thread_temp[$i];
413 $counter = $counter - 1;
416 $thread_new[$k] .= $thread_temp[$i];
418 $thread_new[$k] = "";
419 $counter = $counter - 1;
423 sqsession_register($thread_new, 'thread_new');
424 $thread_new = array_reverse($thread_new);
425 $thread_list = implode(" ", $thread_new);
426 $thread_list = str_replace("(", " ", $thread_list);
427 $thread_list = str_replace(")", " ", $thread_list);
428 $thread_list = preg_split("/\s/", $thread_list, -1, PREG_SPLIT_NO_EMPTY
);
429 $server_sort_array = $thread_list;
430 sqsession_register($server_sort_array, 'server_sort_array');
434 function sqimap_asearch_get_charset()
436 global $allow_charset_search, $languages, $squirrelmail_language;
438 if ($allow_charset_search)
439 return $languages[$squirrelmail_language]['CHARSET'];
443 function sqimap_asearch_get_sort_criteria($mailbox, $sort_by)
445 global $internal_date_sort, $sent_folder;
447 $sort_opcodes = array ('DATE', 'FROM', 'SUBJECT');
448 if ($internal_date_sort == true)
449 $sort_opcodes[0] = 'ARRIVAL';
450 // if (handleAsSent($mailbox))
451 // if (isSentFolder($mailbox))
452 if ($mailbox == $sent_folder)
453 $sort_opcodes[1] = 'TO';
454 return (($sort_by %
2) ?
'' : 'REVERSE ') . $sort_opcodes[$sort_by >> 1];
457 function sqimap_asearch($imapConnection, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $mboxes_array)
459 global $allow_server_sort, $sort, $allow_thread_sort, $thread_sort_messages;
460 global $data_dir, $username;
462 $search_charset = sqimap_asearch_get_charset();
463 $mbox_msgs = array();
465 $cur_mailbox = $mailbox_array[0];
466 $cur_biop = ''; /* Start with ALL */
467 /* We loop one more time than the real array count, so the last search gets fired */
468 for ($cur_crit = 0; $cur_crit <= count($where_array); $cur_crit++
) {
469 if (empty($exclude_array[$cur_crit])) {
470 $next_mailbox = $mailbox_array[$cur_crit];
471 if ($next_mailbox != $cur_mailbox) {
472 $search_string = trim($search_string); /* Trim out last space */
473 if (($cur_mailbox == 'All Folders') && (!empty($mboxes_array)))
474 $search_mboxes = $mboxes_array;
476 $search_mboxes = array($cur_mailbox);
477 foreach ($search_mboxes as $cur_mailbox) {
478 s_debug_dump('C:SELECT:', $cur_mailbox);
479 sqimap_mailbox_select($imapConnection, $cur_mailbox);
480 $thread_sort_messages = $allow_thread_sort && getPref($data_dir, $username, 'thread_' . $cur_mailbox);
481 if ($thread_sort_messages) {
482 $thread_algorithm = 'REFERENCES';
483 $found_msgs = sqimap_run_thread($imapConnection, $search_string, $search_charset, $thread_algorithm);
486 if (($allow_server_sort) && ($sort < 6)) {
487 $sort_criteria = sqimap_asearch_get_sort_criteria($cur_mailbox, $sort);
488 $found_msgs = sqimap_run_sort($imapConnection, $search_string, $search_charset, $sort_criteria);
491 $found_msgs = sqimap_run_search($imapConnection, $search_string, $search_charset);
492 if (isset($mbox_msgs[$cur_mailbox])) {
493 if ($cur_biop == 'OR') /* Merge with previous results */
494 $mbox_msgs[$cur_mailbox] = sqimap_array_merge_unique($mbox_msgs[$cur_mailbox], $found_msgs);
495 else /* Intersect previous results */
496 $mbox_msgs[$cur_mailbox] = array_values(array_intersect($found_msgs, $mbox_msgs[$cur_mailbox]));
498 else /* No previous results */
499 $mbox_msgs[$cur_mailbox] = $found_msgs;
500 if (empty($mbox_msgs[$cur_mailbox])) /* Can happen with intersect, and we need at the end a contiguous array */
501 unset($mbox_msgs[$cur_mailbox]);
503 $cur_mailbox = $next_mailbox;
506 if (isset($where_array[$cur_crit])) {
507 $criteria = sqimap_asearch_build_criteria($where_array[$cur_crit], $what_array[$cur_crit], $search_charset);
508 if (!empty($criteria)) {
509 $unop = $unop_array[$cur_crit];
511 $criteria = $unop . ' ' . $criteria;
512 /* We need to infix the next non-excluded criteria's biop if it's the same mailbox */
514 for ($next_crit = $cur_crit+
1; $next_crit <= count($where_array); $next_crit++
) {
515 if (empty($exclude_array[$next_crit])) {
516 if (asearch_nz($mailbox_array[$next_crit]) == $cur_mailbox)
517 $next_biop = asearch_nz($biop_array[$next_crit]);
521 if ($next_biop == 'OR')
522 $criteria = $next_biop . ' ' . $criteria;
523 $search_string .= $criteria;
524 $cur_biop = asearch_nz($biop_array[$cur_crit]);