6 * Copyright (c) 1999-2004 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
9 * IMAP asearch routines
12 * @package squirrelmail
14 * @link ftp://ftp.rfc-editor.org/in-notes/rfc3501.txt
15 * @author Alex Lemaresquier - Brainstorm - alex at brainstorm.fr
17 * Subfolder search idea from Patch #806075 by Thomas Pohl xraven at users.sourceforge.net. Thanks Thomas!
20 /** This functionality requires the IMAP and date functions */
21 require_once(SM_PATH
. 'functions/imap_general.php');
22 require_once(SM_PATH
. 'functions/date.php');
24 /** Set to TRUE to dump the imap dialogue
25 * @global bool $imap_asearch_debug_dump
27 $imap_asearch_debug_dump = FALSE;
30 * @global array $imap_asearch_opcodes
32 $imap_asearch_opcodes = array(
33 /* <message set> => 'asequence', */
34 /*'ALL' is binary operator */
44 'HEADER' => 'afield', /* Special syntax for this one, see below */
45 'KEYWORD' => 'akeyword',
48 /*'NOT' is unary operator */
51 /*'OR' is binary operator */
54 'SENTBEFORE' => 'adate',
56 'SENTSINCE' => 'adate',
59 'SUBJECT' => 'astring',
67 'UNKEYWORD' => 'akeyword',
71 /** Imap SEARCH month names encoding
72 * @global array $imap_asearch_months
74 $imap_asearch_months = array(
89 /** Error message titles according to imap server returned code
90 * @global array $imap_error_titles
92 $imap_error_titles = array(
94 'NO' => _("ERROR : Could not complete request."),
95 'BAD' => _("ERROR : Bad or malformed request."),
96 'BYE' => _("ERROR : Imap server closed the connection."),
97 '' => _("ERROR : Connection dropped by imap-server.")
101 * Function to display an error related to an IMAP-query.
102 * We need to do our own error management since we may receive NO responses on purpose (even BAD with SORT or THREAD)
103 * so we call sqimap_error_box() if the function exists (sm >= 1.5) or use our own embedded code
104 * @global array imap_error_titles
105 * @param string $response the imap server response code
106 * @param string $query the failed query
107 * @param string $message an optional error message
108 * @param string $link an optional link to try again
110 //@global array color sm colors array
111 function sqimap_asearch_error_box($response, $query, $message, $link = '')
113 global $imap_error_titles;
115 if (!array_key_exists($response, $imap_error_titles))
116 $title = _("ERROR : Unknown imap response.");
118 $title = $imap_error_titles[$response];
120 $message_title = _("Reason Given: ");
122 $message_title = _("Possible reason : ");
123 if (function_exists('sqimap_error_box'))
124 sqimap_error_box($title, $query, $message_title, $message, $link);
125 else { //Straight copy of 1.5 imap_general.php:sqimap_error_box(). Can be removed at a later time
127 require_once(SM_PATH
. 'functions/display_messages.php');
128 $string = "<font color=$color[2]><b>\n" . $title . "</b><br>\n";
130 $string .= _("Query:") . ' ' . htmlspecialchars($query) . '<br>';
131 if ($message_title != '')
132 $string .= $message_title;
134 $string .= htmlspecialchars($message);
137 $string .= "</font><br>\n";
138 error_box($string,$color);
143 * This is to avoid the E_NOTICE warnings signaled by marc AT squirrelmail.org. Thanks Marc!
144 * @param mixed $var any variable (reference)
145 * @return mixed zls ('') if $var is not defined, otherwise $var
147 function asearch_nz(&$var)
155 * This should give the same results as PHP 4 >= 4.3.0's html_entity_decode(),
156 * except it doesn't handle hex constructs
157 * @param string $string string to unhtmlentity()
158 * @return string decoded string
160 function asearch_unhtmlentities($string) {
161 $trans_tbl = array_flip(get_html_translation_table(HTML_ENTITIES
));
162 for ($i=127; $i<255; $i++
) /* Add &#<dec>; entities */
163 $trans_tbl['&#' . $i . ';'] = chr($i);
164 return strtr($string, $trans_tbl);
165 /* I think the one above is quicker, though it should be benchmarked
166 $string = strtr($string, array_flip(get_html_translation_table(HTML_ENTITIES)));
167 return preg_replace("/&#([0-9]+);/E", "chr('\\1')", $string);
172 * Provide an easy way to dump the imap dialogue if $imap_asearch_debug_dump is TRUE
173 * @global imap_asearch_debug_dump
174 * @param string $var_name
175 * @param string $var_var
177 function s_debug_dump($var_name, $var_var)
179 global $imap_asearch_debug_dump;
180 if ($imap_asearch_debug_dump) {
181 if (function_exists('sm_print_r')) //Only exists since 1.4.2
182 sm_print_r($var_name, $var_var); //Better be the 'varargs' version ;)
185 echo htmlentities($var_name);
192 /** Encode a string to quoted or literal as defined in rfc 3501
195 * A quoted string is a sequence of zero or more 7-bit characters,
196 * excluding CR and LF, with double quote (<">) characters at each end.
197 * - § 9. Formal Syntax:
198 * quoted-specials = DQUOTE / "\"
199 * @param string $what string to encode
200 * @param string $charset search charset used
201 * @return string encoded string
203 function sqimap_asearch_encode_string($what, $charset)
205 if (strtoupper($charset) == 'ISO-2022-JP') // This should be now handled in imap_utf7_local?
206 $what = mb_convert_encoding($what, 'JIS', 'auto');
207 //if (ereg("[\"\\\r\n\x80-\xff]", $what))
208 if (preg_match('/["\\\\\r\n\x80-\xff]/', $what))
209 return '{' . strlen($what) . "}\r\n" . $what; // 4.3 literal form
210 return '"' . $what . '"'; // 4.3 quoted string form
214 * Parses a user date string into an rfc 3501 date string
215 * Handles space, slash, backslash, dot and comma as separators (and dash of course ;=)
216 * @global imap_asearch_months
217 * @param string user date
218 * @return array a preg_match-style array:
219 * - [0] = fully formatted rfc 3501 date string (<day number>-<US month TLA>-<4 digit year>)
224 function sqimap_asearch_parse_date($what)
226 global $imap_asearch_months;
229 $what = ereg_replace('[ /\\.,]+', '-', $what);
231 preg_match('/^([0-9]+)-+([^\-]+)-+([0-9]+)$/', $what, $what_parts);
232 if (count($what_parts) == 4) {
233 $what_month = strtolower(asearch_unhtmlentities($what_parts[2]));
234 /* if (!in_array($what_month, $imap_asearch_months)) {*/
235 foreach ($imap_asearch_months as $month_number => $month_code) {
236 if (($what_month == $month_number)
237 ||
($what_month == $month_code)
238 ||
($what_month == strtolower(asearch_unhtmlentities(getMonthName($month_number))))
239 ||
($what_month == strtolower(asearch_unhtmlentities(getMonthAbrv($month_number))))
241 $what_parts[2] = $month_number;
242 $what_parts[0] = $what_parts[1] . '-' . $month_code . '-' . $what_parts[3];
250 $what_parts = array();
255 * Build one criteria sequence
256 * @global array imap_asearch_opcodes
257 * @param string $opcode search opcode
258 * @param string $what opcode argument
259 * @param string $charset search charset
260 * @return string one full criteria sequence
262 function sqimap_asearch_build_criteria($opcode, $what, $charset)
264 global $imap_asearch_opcodes;
267 switch ($imap_asearch_opcodes[$opcode]) {
270 // $what = str_replace(' ', '', $what);
271 $what = ereg_replace('[^0-9]+', '', $what);
273 $criteria = $opcode . ' ' . $what . ' ';
276 $criteria = $opcode . ' ';
278 case 'afield': /* HEADER field-name: field-body */
279 preg_match('/^([^:]+):(.*)$/', $what, $what_parts);
280 if (count($what_parts) == 3)
281 $criteria = $opcode . ' ' .
282 sqimap_asearch_encode_string($what_parts[1], $charset) . ' ' .
283 sqimap_asearch_encode_string($what_parts[2], $charset) . ' ';
286 $what_parts = sqimap_asearch_parse_date($what);
287 if (isset($what_parts[0]))
288 $criteria = $opcode . ' ' . $what_parts[0] . ' ';
292 $criteria = $opcode . ' ' . sqimap_asearch_encode_string($what, $charset) . ' ';
295 $what = ereg_replace('[^0-9:\(\)]+', '', $what);
297 $criteria = $opcode . ' ' . $what . ' ';
304 * Another way to do array_values(array_unique(array_merge($to, $from)));
305 * @param array $to to array (reference)
306 * @param array $from from array
307 * @return array uniquely merged array
309 function sqimap_array_merge_unique(&$to, $from)
313 $count = count($from);
314 for ($i = 0; $i < $count; $i++
) {
315 if (!in_array($from[$i], $to))
322 * Run the imap SEARCH command as defined in rfc 3501
323 * @link ftp://ftp.rfc-editor.org/in-notes/rfc3501.txt
324 * @param resource $imapConnection the current imap stream
325 * @param string $search_string the full search expression eg "ALL RECENT"
326 * @param string $search_charset charset to use or zls ('')
327 * @return array an IDs or UIDs array of matching messages or an empty array
329 function sqimap_run_search($imapConnection, $search_string, $search_charset)
333 /* 6.4.4 try OPTIONAL [CHARSET] specification first */
334 if ($search_charset != '')
335 $query = 'SEARCH CHARSET "' . strtoupper($search_charset) . '" ALL ' . $search_string;
337 $query = 'SEARCH ALL ' . $search_string;
338 s_debug_dump('C:', $query);
339 $readin = sqimap_run_command($imapConnection, $query, false, $response, $message, $uid_support);
341 /* 6.4.4 try US-ASCII charset if we tried an OPTIONAL [CHARSET] and received a tagged NO response (SHOULD be [BADCHARSET]) */
342 if (($search_charset != '') && (strtoupper($response) == 'NO')) {
343 $query = 'SEARCH CHARSET US-ASCII ALL ' . $search_string;
344 s_debug_dump('C:', $query);
345 $readin = sqimap_run_command($imapConnection, $query, false, $response, $message, $uid_support);
347 if (strtoupper($response) != 'OK') {
348 sqimap_asearch_error_box($response, $query, $message);
352 // Keep going till we find the * SEARCH response
353 foreach ($readin as $readin_part) {
354 s_debug_dump('S:', $readin_part);
355 if (substr($readin_part, 0, 9) == '* SEARCH ') {
356 //EIMS returns multiple SEARCH responses, and this allowed according to Mark Crispin
357 $messagelist = sqimap_array_merge_unique($messagelist, preg_split("/ /", substr($readin_part, 9)));
361 if (empty($messagelist)) //Empty search response, ie '* SEARCH'
364 $cnt = count($messagelist);
365 for ($q = 0; $q < $cnt; $q++
)
366 $id[$q] = trim($messagelist[$q]);
371 * Run the imap SORT command as defined in
372 * @link http://www.ietf.org/internet-drafts/draft-ietf-imapext-sort-13.txt
373 * @param resource $imapConnection the current imap stream
374 * @param string $search_string the full search expression as defined in rfc 3501
375 * @param string $search_charset mandatory charset
376 * @param string $sort_criteria the full sort criteria expression eg "SUBJECT REVERSE DATE"
377 * @return array an IDs or UIDs array of matching messages or an empty array
379 function sqimap_run_sort($imapConnection, $search_string, $search_charset, $sort_criteria)
383 if ($search_charset == '')
384 $search_charset = 'US-ASCII';
385 $query = 'SORT (' . $sort_criteria . ') "' . strtoupper($search_charset) . '" ALL ' . $search_string;
386 s_debug_dump('C:', $query);
387 $readin = sqimap_run_command($imapConnection, $query, false, $response, $message, $uid_support);
388 s_debug_dump('S:', $response);
390 /* 6.4 try US-ASCII charset if we received a tagged NO response (SHOULD be [BADCHARSET]) */
391 if (($search_charset != 'US-ASCII') && (strtoupper($response) == 'NO')) {
392 s_debug_dump('S:', $readin);
393 $query = 'SORT (' . $sort_criteria . ') US-ASCII ALL ' . $search_string;
394 s_debug_dump('C:', $query);
395 $readin = sqimap_run_command($imapConnection, $query, false, $response, $message, $uid_support);
396 s_debug_dump('S:', $response);
399 if (strtoupper($response) != 'OK') {
400 s_debug_dump('S:', $readin);
401 // sqimap_asearch_error_box($response, $query, $message);
403 return sqimap_run_search($imapConnection, $search_string, $search_charset); // Fell back to standard search
406 /* Keep going till we find the * SORT response */
407 foreach ($readin as $readin_part) {
408 s_debug_dump('S:', $readin_part);
409 if (substr($readin_part, 0, 7) == '* SORT ') {
410 //SORT returns untagged responses
411 $messagelist = sqimap_array_merge_unique($messagelist, preg_split("/ /", substr($readin_part, 7)));
415 if (empty($messagelist)) //Empty search response, ie '* SORT'
418 $cnt = count($messagelist);
419 for ($q = 0; $q < $cnt; $q++
)
420 $id[$q] = trim($messagelist[$q]);
425 * Run the imap THREAD command as defined in
426 * @link http://www.ietf.org/internet-drafts/draft-ietf-imapext-sort-13.txt
427 * @param resource $imapConnection the current imap stream
428 * @param string $search_string the full search expression as defined in rfc 3501
429 * @param string $search_charset mandatory charset
430 * @param string $thread_algorithm the threading algorithm "ORDEREDSUBJECT" or "REFERENCES"
431 * @return array an IDs or UIDs array of matching messages or an empty array
432 * @global array thread_new will be used by thread view in mailbox_display
433 * @global array server_sort_array will be used by thread view in mailbox_display
435 function sqimap_run_thread($imapConnection, $search_string, $search_charset, $thread_algorithm)
437 global $thread_new, $server_sort_array;
439 if (sqsession_is_registered('thread_new'))
440 sqsession_unregister('thread_new');
441 if (sqsession_is_registered('server_sort_array'))
442 sqsession_unregister('server_sort_array');
444 $thread_new = array();
447 $server_sort_array = array();
451 if ($search_charset == '')
452 $search_charset = 'US-ASCII';
453 $query = 'THREAD ' . $thread_algorithm . ' "' . strtoupper($search_charset) . '" ALL ' . $search_string;
454 s_debug_dump('C:', $query);
455 $readin = sqimap_run_command($imapConnection, $query, false, $response, $message, $uid_support);
456 s_debug_dump('S:', $response);
458 /* 6.4 try US-ASCII charset if we received a tagged NO response (SHOULD be [BADCHARSET]) */
459 if (($search_charset != 'US-ASCII') && (strtoupper($response) == 'NO')) {
460 s_debug_dump('S:', $readin);
461 $query = 'THREAD ' . $thread_algorithm . ' US-ASCII ALL ' . $search_string;
462 s_debug_dump('C:', $query);
463 $readin = sqimap_run_command($imapConnection, $query, false, $response, $message, $uid_support);
464 s_debug_dump('S:', $response);
467 if (strtoupper($response) != 'OK') {
468 s_debug_dump('S:', $readin);
469 if (empty($response)) { //imap server closed connection. We can't go further.
470 /* we should at this point:
471 - warn the user that the THREAD call has failed
472 - (offer him a way to) disconnect it permanently in the prefs
473 - perform the regular search instead or provide a way to do it in one click
475 global $sort, $mailbox, $php_self;
476 $message = _("The imap server failed to handle threading.");
477 $unthread = _("Click here to unset thread view for this mailbox and start again.");
478 if (preg_match('/^(.+)\?.+$/', $php_self, $regs))
479 $source_url = $regs[1];
481 $source_url = $php_self;
482 $link = '<a href=' . $source_url . '?sort=' . $sort . '&start_messages=1&set_thread=0&mailbox=' . urlencode($mailbox) . '>' . $unthread . '</a>';
483 sqimap_asearch_error_box($response, $query, $message, $link);
486 return sqimap_run_search($imapConnection, $search_string, $search_charset); // Fell back to standard search
489 /* Keep going till we find the * THREAD response */
490 foreach ($readin as $readin_part) {
491 s_debug_dump('S:', $readin_part);
492 if (substr($readin_part, 0, 9) == '* THREAD ') {
493 $thread_temp = preg_split("//", substr($readin_part, 9), -1, PREG_SPLIT_NO_EMPTY
);
494 break; // Should be the last anyway
498 if (empty($thread_temp)) //Empty search response, ie '* THREAD'
501 $char_count = count($thread_temp);
504 for ($i=0;$i<$char_count;$i++
) {
505 if ($thread_temp[$i] != ')' && $thread_temp[$i] != '(') {
506 $thread_new[$k] = $thread_new[$k] . $thread_temp[$i];
508 elseif ($thread_temp[$i] == '(') {
509 $thread_new[$k] .= $thread_temp[$i];
512 elseif ($thread_temp[$i] == ')') {
514 $thread_new[$k] .= $thread_temp[$i];
515 $counter = $counter - 1;
518 $thread_new[$k] .= $thread_temp[$i];
520 $thread_new[$k] = "";
521 $counter = $counter - 1;
525 sqsession_register($thread_new, 'thread_new');
526 $thread_new = array_reverse($thread_new);
527 $thread_list = implode(" ", $thread_new);
528 $thread_list = str_replace("(", " ", $thread_list);
529 $thread_list = str_replace(")", " ", $thread_list);
530 $thread_list = preg_split("/\s/", $thread_list, -1, PREG_SPLIT_NO_EMPTY
);
531 $server_sort_array = $thread_list;
532 sqsession_register($server_sort_array, 'server_sort_array');
537 * @global bool allow_charset_search user setting
538 * @global array languages sm languages array
539 * @global string squirrelmail_language user language setting
540 * @return string the user defined charset if $allow_charset_search is TRUE else zls ('')
542 function sqimap_asearch_get_charset()
544 global $allow_charset_search, $languages, $squirrelmail_language;
546 if ($allow_charset_search)
547 return $languages[$squirrelmail_language]['CHARSET'];
552 * Convert sm internal sort to imap sort taking care of:
553 * - user defined date sorting (ARRIVAL vs DATE)
554 * - if the searched mailbox is the sent folder then TO is being used instead of FROM
555 * - reverse order by using REVERSE
556 * @param string $mailbox mailbox name to sort
557 * @param integer $sort_by sm sort criteria index
558 * @global bool internal_date_sort sort by arrival date instead of message date
559 * @global string sent_folder sent folder name
560 * @return string imap sort criteria
562 function sqimap_asearch_get_sort_criteria($mailbox, $sort_by)
564 global $internal_date_sort, $sent_folder;
566 $sort_opcodes = array ('DATE', 'FROM', 'SUBJECT', 'SIZE');
567 if ($internal_date_sort == true)
568 $sort_opcodes[0] = 'ARRIVAL';
569 // if (handleAsSent($mailbox))
570 // if (isSentFolder($mailbox))
571 if ($mailbox == $sent_folder)
572 $sort_opcodes[1] = 'TO';
573 return (($sort_by %
2) ?
'' : 'REVERSE ') . $sort_opcodes[($sort_by >> 1) & 3];
577 * @param string $cur_mailbox unformatted mailbox name
578 * @param array $boxes_unformatted selectable mailbox unformatted names array (reference)
579 * @return array sub mailboxes unformatted names
581 function sqimap_asearch_get_sub_mailboxes($cur_mailbox, $mboxes_array)
583 $sub_mboxes_array = array();
584 $boxcount = count($mboxes_array);
585 for ($boxnum=0; $boxnum < $boxcount; $boxnum++
) {
586 if (isBoxBelow($mboxes_array[$boxnum], $cur_mailbox))
587 $sub_mboxes_array[] = $mboxes_array[$boxnum];
589 return $sub_mboxes_array;
593 * Performs the search, given all the criteria, merging results for every mailbox
594 * @param resource $imapConnection
595 * @param array $mailbox_array
596 * @param array $biop_array
597 * @param array $unop_array
598 * @param array $where_array
599 * @param array $what_array
600 * @param array $exclude_array
601 * @param array $sub_array
602 * @param array $mboxes_array selectable unformatted mailboxes names
603 * @global bool allow_server_sort comes from config.php
604 * @global integer sort sm internal sort order
605 * @global bool allow_thread_sort comes from config.php
606 * @global bool thread_sort_messages does it really need to global?
607 * @global integer sort_by_ref thread by references
608 * @global string data_dir
609 * @global string username
610 * @return array $mbox_msgs array(mailbox => array(UIDs))
612 function sqimap_asearch($imapConnection, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array, $mboxes_array)
614 global $allow_server_sort, $sort, $allow_thread_sort, $thread_sort_messages, $sort_by_ref;
615 global $data_dir, $username;
617 $search_charset = sqimap_asearch_get_charset();
618 $mbox_msgs = array();
620 $cur_mailbox = $mailbox_array[0];
621 $cur_biop = ''; /* Start with ALL */
622 /* We loop one more time than the real array count, so the last search gets fired */
623 for ($cur_crit = 0; $cur_crit <= count($where_array); $cur_crit++
) {
624 if (empty($exclude_array[$cur_crit])) {
625 $next_mailbox = $mailbox_array[$cur_crit];
626 if ($next_mailbox != $cur_mailbox) {
627 $search_string = trim($search_string); /* Trim out last space */
628 if ($cur_mailbox == 'All Folders')
629 $search_mboxes = $mboxes_array;
630 else if ((!empty($sub_array[$cur_crit - 1])) ||
(!in_array($cur_mailbox, $mboxes_array)))
631 $search_mboxes = sqimap_asearch_get_sub_mailboxes($cur_mailbox, $mboxes_array);
633 $search_mboxes = array($cur_mailbox);
634 foreach ($search_mboxes as $cur_mailbox) {
635 s_debug_dump('C:SELECT:', $cur_mailbox);
636 sqimap_mailbox_select($imapConnection, $cur_mailbox);
637 $thread_sort_messages = $allow_thread_sort && getPref($data_dir, $username, 'thread_' . $cur_mailbox);
638 if ($thread_sort_messages) {
639 if ($sort_by_ref == 1)
640 $thread_algorithm = 'REFERENCES';
642 $thread_algorithm = 'ORDEREDSUBJECT';
643 $found_msgs = sqimap_run_thread($imapConnection, $search_string, $search_charset, $thread_algorithm);
646 if (($allow_server_sort) && ($sort < 6)) {
647 $sort_criteria = sqimap_asearch_get_sort_criteria($cur_mailbox, $sort);
648 $found_msgs = sqimap_run_sort($imapConnection, $search_string, $search_charset, $sort_criteria);
651 $found_msgs = sqimap_run_search($imapConnection, $search_string, $search_charset);
652 if (isset($mbox_msgs[$cur_mailbox])) {
653 if ($cur_biop == 'OR') /* Merge with previous results */
654 $mbox_msgs[$cur_mailbox] = sqimap_array_merge_unique($mbox_msgs[$cur_mailbox], $found_msgs);
655 else /* Intersect previous results */
656 $mbox_msgs[$cur_mailbox] = array_values(array_intersect($found_msgs, $mbox_msgs[$cur_mailbox]));
658 else /* No previous results */
659 $mbox_msgs[$cur_mailbox] = $found_msgs;
660 if (empty($mbox_msgs[$cur_mailbox])) /* Can happen with intersect, and we need at the end a contiguous array */
661 unset($mbox_msgs[$cur_mailbox]);
663 $cur_mailbox = $next_mailbox;
666 if (isset($where_array[$cur_crit])) {
667 $criteria = sqimap_asearch_build_criteria($where_array[$cur_crit], $what_array[$cur_crit], $search_charset);
668 if (!empty($criteria)) {
669 $unop = $unop_array[$cur_crit];
671 $criteria = $unop . ' ' . $criteria;
672 /* We need to infix the next non-excluded criteria's biop if it's the same mailbox */
674 for ($next_crit = $cur_crit+
1; $next_crit <= count($where_array); $next_crit++
) {
675 if (empty($exclude_array[$next_crit])) {
676 if (asearch_nz($mailbox_array[$next_crit]) == $cur_mailbox)
677 $next_biop = asearch_nz($biop_array[$next_crit]);
681 if ($next_biop == 'OR')
682 $criteria = $next_biop . ' ' . $criteria;
683 $search_string .= $criteria;
684 $cur_biop = asearch_nz($biop_array[$cur_crit]);