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 * @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 * @global bool $imap_asearch_debug_dump
23 $imap_asearch_debug_dump = FALSE;
25 /** Array of imap SEARCH opcodes
26 * @global array $imap_asearch_opcodes
28 $imap_asearch_opcodes = array(
29 /* <message set> => 'asequence', */
30 /*'ALL' is binary operator */
40 'HEADER' => 'afield', /* Special syntax for this one, see below */
41 'KEYWORD' => 'akeyword',
44 /*'NOT' is unary operator */
47 /*'OR' is binary operator */
50 'SENTBEFORE' => 'adate',
52 'SENTSINCE' => 'adate',
55 'SUBJECT' => 'astring',
63 'UNKEYWORD' => 'akeyword',
67 /** Imap SEARCH month names encoding
68 * @global array $imap_asearch_months
70 $imap_asearch_months = array(
85 /** Error message titles according to imap server returned code
86 * @global array $imap_error_titles
88 $imap_error_titles = array(
90 'NO' => _("ERROR : Could not complete request."),
91 'BAD' => _("ERROR : Bad or malformed request."),
92 'BYE' => _("ERROR : Imap server closed the connection.")
96 * Function to display an error related to an IMAP-query.
97 * We need to do our own error management since we may receive NO responses on purpose (even BAD with SORT or THREAD)
98 * so we call sqimap_error_box() if the function exists (sm >= 1.5) or use our own embedded code
99 * @global array imap_error_titles
100 * @param string $response the imap server response code
101 * @param string $query the failed query
102 * @param string $message the error message
104 //@global array color sm colors array
105 function sqimap_asearch_error_box($response, $query, $message)
107 global $imap_error_titles;
109 //if (!array_key_exists($response, $imap_error_titles)) //php 4.0.6 compatibility
110 if (!in_array($response, array_keys($imap_error_titles)))
111 $title = _("ERROR : Unknown imap response.");
113 $title = $imap_error_titles[$response];
114 $message_title = _("Reason Given: ");
115 if (function_exists('sqimap_error_box'))
116 sqimap_error_box($title, $query, $message_title, $message);
117 else { //Straight copy of 1.5 imap_general.php:sqimap_error_box(). Can be removed at a later time
119 require_once(SM_PATH
. 'functions/display_messages.php');
120 $string = "<font color=$color[2]><b>\n" . $title . "</b><br>\n";
122 $string .= _("Query:") . ' ' . htmlspecialchars($query) . '<br>';
123 if ($message_title != '')
124 $string .= $message_title;
126 $string .= htmlspecialchars($message);
127 $string .= "</font><br>\n";
128 error_box($string,$color);
133 * This is to avoid the E_NOTICE warnings signaled by marc AT squirrelmail.org. Thanks Marc!
134 * @param mixed $var any variable
135 * @return mixed zls ('') if $var is not defined, otherwise $var
137 function asearch_nz(&$var)
145 * This should give the same results as PHP 4 >= 4.3.0's html_entity_decode(),
146 * except it doesn't handle hex constructs
147 * @param string $string string to unhtmlentity()
148 * @return string decoded string
150 function asearch_unhtmlentities($string) {
151 $trans_tbl = array_flip(get_html_translation_table(HTML_ENTITIES
));
152 for ($i=127; $i<255; $i++
) /* Add &#<dec>; entities */
153 $trans_tbl['&#' . $i . ';'] = chr($i);
154 return strtr($string, $trans_tbl);
155 /* I think the one above is quicker, though it should be benchmarked
156 $string = strtr($string, array_flip(get_html_translation_table(HTML_ENTITIES)));
157 return preg_replace("/&#([0-9]+);/E", "chr('\\1')", $string);
162 * Provide an easy way to dump the imap dialogue if $imap_asearch_debug_dump is TRUE
163 * @global imap_asearch_debug_dump
164 * @param string $var_name
165 * @param string $var_var
167 function s_debug_dump($var_name, $var_var)
169 global $imap_asearch_debug_dump;
170 if ($imap_asearch_debug_dump) {
171 if (function_exists('sm_print_r')) //Only exists since 1.4.2
172 sm_print_r($var_name, $var_var); //Better be the 'varargs' version ;)
175 echo htmlentities($var_name);
182 /** Encode a string to quoted or literal as defined in rfc 3501
185 * A quoted string is a sequence of zero or more 7-bit characters,
186 * excluding CR and LF, with double quote (<">) characters at each end.
187 * - § 9. Formal Syntax:
188 * quoted-specials = DQUOTE / "\"
189 * @param string $what string to encode
190 * @param string $charset search charset used
191 * @return string encoded string
193 function sqimap_asearch_encode_string($what, $charset)
195 if (strtoupper($charset) == 'ISO-2022-JP') // This should be now handled in imap_utf7_local?
196 $what = mb_convert_encoding($what, 'JIS', 'auto');
197 //if (ereg("[\"\\\r\n\x80-\xff]", $what))
198 if (preg_match('/["\\\\\r\n\x80-\xff]/', $what))
199 return '{' . strlen($what) . "}\r\n" . $what; // 4.3 literal form
200 return '"' . $what . '"'; // 4.3 quoted string form
204 * Parses a user date string into an rfc 3501 date string
205 * Handles space, slash, backslash, dot and comma as separators (and dash of course ;=)
206 * @global imap_asearch_months
207 * @param string user date
208 * @return array a preg_match-style array:
209 * - [0] = fully formatted rfc 3501 date string (<day number>-<US month TLA>-<4 digit year>)
214 function sqimap_asearch_parse_date($what)
216 global $imap_asearch_months;
219 $what = ereg_replace('[ /\\.,]+', '-', $what);
221 preg_match('/^([0-9]+)-+([^\-]+)-+([0-9]+)$/', $what, $what_parts);
222 if (count($what_parts) == 4) {
223 $what_month = strtolower(asearch_unhtmlentities($what_parts[2]));
224 /* if (!in_array($what_month, $imap_asearch_months)) {*/
225 foreach ($imap_asearch_months as $month_number => $month_code) {
226 if (($what_month == $month_number)
227 ||
($what_month == $month_code)
228 ||
($what_month == strtolower(asearch_unhtmlentities(getMonthName($month_number))))
229 ||
($what_month == strtolower(asearch_unhtmlentities(getMonthAbrv($month_number))))
231 $what_parts[2] = $month_number;
232 $what_parts[0] = $what_parts[1] . '-' . $month_code . '-' . $what_parts[3];
240 $what_parts = array();
245 * Build one criteria sequence
246 * @global array imap_asearch_opcodes
247 * @param string $opcode search opcode
248 * @param string $what opcode argument
249 * @param string $charset search charset
250 * @return string one full criteria sequence
252 function sqimap_asearch_build_criteria($opcode, $what, $charset)
254 global $imap_asearch_opcodes;
257 switch ($imap_asearch_opcodes[$opcode]) {
260 // $what = str_replace(' ', '', $what);
261 $what = ereg_replace('[^0-9]+', '', $what);
263 $criteria = $opcode . ' ' . $what . ' ';
266 $criteria = $opcode . ' ';
268 case 'afield': /* HEADER field-name: field-body */
269 preg_match('/^([^:]+):(.*)$/', $what, $what_parts);
270 if (count($what_parts) == 3)
271 $criteria = $opcode . ' ' .
272 sqimap_asearch_encode_string($what_parts[1], $charset) . ' ' .
273 sqimap_asearch_encode_string($what_parts[2], $charset) . ' ';
276 $what_parts = sqimap_asearch_parse_date($what);
277 if (isset($what_parts[0]))
278 $criteria = $opcode . ' ' . $what_parts[0] . ' ';
282 $criteria = $opcode . ' ' . sqimap_asearch_encode_string($what, $charset) . ' ';
285 $what = ereg_replace('[^0-9:\(\)]+', '', $what);
287 $criteria = $opcode . ' ' . $what . ' ';
294 * Another way to do array_values(array_unique(array_merge($to, $from)));
295 * @param array $to to array
296 * @param array $from from array
297 * @return array uniquely merged array
299 function sqimap_array_merge_unique($to, $from)
303 $count = count($from);
304 for ($i = 0; $i < $count; $i++
) {
305 if (!in_array($from[$i], $to))
312 * Run the imap SEARCH command as defined in rfc 3501
313 * @link ftp://ftp.rfc-editor.org/in-notes/rfc3501.txt
314 * @param resource $imapConnection the current imap stream
315 * @param string $search_string the full search expression eg "ALL RECENT"
316 * @param string $search_charset charset to use or zls ('')
317 * @return array an IDs or UIDs array of matching messages or an empty array
319 function sqimap_run_search($imapConnection, $search_string, $search_charset)
323 /* 6.4.4 try OPTIONAL [CHARSET] specification first */
324 if ($search_charset != '')
325 $query = 'SEARCH CHARSET "' . strtoupper($search_charset) . '" ALL ' . $search_string;
327 $query = 'SEARCH ALL ' . $search_string;
328 s_debug_dump('C:', $query);
329 $readin = sqimap_run_command($imapConnection, $query, false, $response, $message, $uid_support);
331 /* 6.4.4 try US-ASCII charset if we tried an OPTIONAL [CHARSET] and received a tagged NO response (SHOULD be [BADCHARSET]) */
332 if (($search_charset != '') && (strtoupper($response) == 'NO')) {
333 $query = 'SEARCH CHARSET US-ASCII ALL ' . $search_string;
334 s_debug_dump('C:', $query);
335 $readin = sqimap_run_command($imapConnection, $query, false, $response, $message, $uid_support);
337 if (strtoupper($response) != 'OK') {
338 sqimap_asearch_error_box($response, $query, $message);
344 // Keep going till we find the * SEARCH response
345 foreach ($readin as $readin_part) {
346 s_debug_dump('S:', $readin_part);
347 if (substr($readin_part, 0, 9) == '* SEARCH ') {
348 //EIMS returns multiple SEARCH responses, and this allowed according to Mark Crispin
349 $messagelist = sqimap_array_merge_unique($message_list, preg_split("/ /", substr($readin_part, 9)));
353 if (empty($messagelist)) //Empty search response, ie '* SEARCH'
356 $cnt = count($messagelist);
357 for ($q = 0; $q < $cnt; $q++
)
358 $id[$q] = trim($messagelist[$q]);
363 * Run the imap SORT command as defined in
364 * @link http://www.ietf.org/internet-drafts/draft-ietf-imapext-sort-13.txt
365 * @param resource $imapConnection the current imap stream
366 * @param string $search_string the full search expression as defined in rfc 3501
367 * @param string $search_charset mandatory charset
368 * @param string $sort_criteria the full sort criteria expression eg "SUBJECT REVERSE DATE"
369 * @return array an IDs or UIDs array of matching messages or an empty array
371 function sqimap_run_sort($imapConnection, $search_string, $search_charset, $sort_criteria)
375 if ($search_charset == '')
376 $search_charset = 'US-ASCII';
377 $query = 'SORT (' . $sort_criteria . ') ' . strtoupper($search_charset) . ' ALL ' . $search_string;
378 s_debug_dump('C:', $query);
379 $readin = sqimap_run_command($imapConnection, $query, false, $response, $message, $uid_support);
381 /* 6.4 try US-ASCII charset if we received a tagged NO response (SHOULD be [BADCHARSET]) */
382 if (($search_charset != 'US-ASCII') && (strtoupper($response) == 'NO')) {
383 $query = 'SORT (' . $sort_criteria . ') US-ASCII ALL ' . $search_string;
384 s_debug_dump('C:', $query);
385 $readin = sqimap_run_command($imapConnection, $query, false, $response, $message, $uid_support);
388 if (strtoupper($response) != 'OK') {
389 // sqimap_asearch_error_box($response, $query, $message);
391 return sqimap_run_search($imapConnection, $search_string, $search_charset); // Fell back to standard search
394 /* Keep going till we find the * SORT response */
395 foreach ($readin as $readin_part) {
396 s_debug_dump('S:', $readin_part);
397 if (substr($readin_part, 0, 7) == '* SORT ') {
398 //SORT returns untagged responses
399 $messagelist = sqimap_array_merge_unique($message_list, preg_split("/ /", substr($readin_part, 7)));
403 if (empty($messagelist)) //Empty search response, ie '* SORT'
406 $cnt = count($messagelist);
407 for ($q = 0; $q < $cnt; $q++
)
408 $id[$q] = trim($messagelist[$q]);
413 * Run the imap THREAD command as defined in
414 * @link http://www.ietf.org/internet-drafts/draft-ietf-imapext-sort-13.txt
415 * @param resource $imapConnection the current imap stream
416 * @param string $search_string the full search expression as defined in rfc 3501
417 * @param string $search_charset mandatory charset
418 * @param string $thread_algorithm the threading algorithm "ORDEREDSUBJECT" or "REFERENCES"
419 * @return array an IDs or UIDs array of matching messages or an empty array
420 * @global array $thread_new will be used by thread view in mailbox_display
421 * @global array $server_sort_array will be used by thread view in mailbox_display
423 function sqimap_run_thread($imapConnection, $search_string, $search_charset, $thread_algorithm)
425 global $thread_new, $server_sort_array;
427 if (sqsession_is_registered('thread_new'))
428 sqsession_unregister('thread_new');
429 if (sqsession_is_registered('server_sort_array'))
430 sqsession_unregister('server_sort_array');
432 $thread_new = array();
435 $server_sort_array = array();
439 if ($search_charset == '')
440 $search_charset = 'US-ASCII';
441 $query = 'THREAD ' . $thread_algorithm . ' ' . strtoupper($search_charset) . ' ALL ' . $search_string;
442 s_debug_dump('C:', $query);
443 $readin = sqimap_run_command($imapConnection, $query, false, $response, $message, $uid_support);
445 /* 6.4 try US-ASCII charset if we received a tagged NO response (SHOULD be [BADCHARSET]) */
446 if (($search_charset != 'US-ASCII') && (strtoupper($response) == 'NO')) {
447 $query = 'THREAD ' . $thread_algorithm . ' US-ASCII ALL ' . $search_string;
448 s_debug_dump('C:', $query);
449 $readin = sqimap_run_command($imapConnection, $query, false, $response, $message, $uid_support);
452 if (strtoupper($response) != 'OK') {
453 /* we should at this point:
454 - warn the user that the THREAD call has failed
455 - (offer him a way to) disconnect it permanently in the prefs
456 - perform the regular search instead or provide a way to do it in one click
458 // sqimap_asearch_error_box($response, $query, $message);
460 return sqimap_run_search($imapConnection, $search_string, $search_charset); // Fell back to standard search
463 /* Keep going till we find the * THREAD response */
464 foreach ($readin as $readin_part) {
465 s_debug_dump('S:', $readin_part);
466 if (substr($readin_part, 0, 9) == '* THREAD ') {
467 $thread_temp = preg_split("//", substr($readin_part, 9), -1, PREG_SPLIT_NO_EMPTY
);
468 break; // Should be the last anyway
472 if (empty($thread_temp)) //Empty search response, ie '* THREAD'
475 $char_count = count($thread_temp);
478 for ($i=0;$i<$char_count;$i++
) {
479 if ($thread_temp[$i] != ')' && $thread_temp[$i] != '(') {
480 $thread_new[$k] = $thread_new[$k] . $thread_temp[$i];
482 elseif ($thread_temp[$i] == '(') {
483 $thread_new[$k] .= $thread_temp[$i];
486 elseif ($thread_temp[$i] == ')') {
488 $thread_new[$k] .= $thread_temp[$i];
489 $counter = $counter - 1;
492 $thread_new[$k] .= $thread_temp[$i];
494 $thread_new[$k] = "";
495 $counter = $counter - 1;
499 sqsession_register($thread_new, 'thread_new');
500 $thread_new = array_reverse($thread_new);
501 $thread_list = implode(" ", $thread_new);
502 $thread_list = str_replace("(", " ", $thread_list);
503 $thread_list = str_replace(")", " ", $thread_list);
504 $thread_list = preg_split("/\s/", $thread_list, -1, PREG_SPLIT_NO_EMPTY
);
505 $server_sort_array = $thread_list;
506 sqsession_register($server_sort_array, 'server_sort_array');
511 * @global bool allow_charset_search user setting
512 * @global array languages sm languages array
513 * @global string squirrelmail_language user language setting
514 * @return string the user defined charset if $allow_charset_search is TRUE else zls ('')
516 function sqimap_asearch_get_charset()
518 global $allow_charset_search, $languages, $squirrelmail_language;
520 if ($allow_charset_search)
521 return $languages[$squirrelmail_language]['CHARSET'];
526 * Convert sm internal sort to imap sort taking care of:
527 * - user defined date sorting (ARRIVAL vs DATE)
528 * - if the searched mailbox is the sent folder then TO is being used instead of FROM
529 * - reverse order by using REVERSE
530 * @param string $mailbox mailbox name to sort
531 * @param integer $sort_by sm sort criteria
532 * @return string imap sort criteria
534 function sqimap_asearch_get_sort_criteria($mailbox, $sort_by)
536 global $internal_date_sort, $sent_folder;
538 $sort_opcodes = array ('DATE', 'FROM', 'SUBJECT');
539 if ($internal_date_sort == true)
540 $sort_opcodes[0] = 'ARRIVAL';
541 // if (handleAsSent($mailbox))
542 // if (isSentFolder($mailbox))
543 if ($mailbox == $sent_folder)
544 $sort_opcodes[1] = 'TO';
545 return (($sort_by %
2) ?
'' : 'REVERSE ') . $sort_opcodes[$sort_by >> 1];
549 * Performs the search, given all the criteria, merging results for every mailbox
550 * @return array array(mailbox => array(UIDs))
552 function sqimap_asearch($imapConnection, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $mboxes_array)
554 global $allow_server_sort, $sort, $allow_thread_sort, $thread_sort_messages;
555 global $data_dir, $username;
557 $search_charset = sqimap_asearch_get_charset();
558 $mbox_msgs = array();
560 $cur_mailbox = $mailbox_array[0];
561 $cur_biop = ''; /* Start with ALL */
562 /* We loop one more time than the real array count, so the last search gets fired */
563 for ($cur_crit = 0; $cur_crit <= count($where_array); $cur_crit++
) {
564 if (empty($exclude_array[$cur_crit])) {
565 $next_mailbox = $mailbox_array[$cur_crit];
566 if ($next_mailbox != $cur_mailbox) {
567 $search_string = trim($search_string); /* Trim out last space */
568 if (($cur_mailbox == 'All Folders') && (!empty($mboxes_array)))
569 $search_mboxes = $mboxes_array;
571 $search_mboxes = array($cur_mailbox);
572 foreach ($search_mboxes as $cur_mailbox) {
573 s_debug_dump('C:SELECT:', $cur_mailbox);
574 sqimap_mailbox_select($imapConnection, $cur_mailbox);
575 $thread_sort_messages = $allow_thread_sort && getPref($data_dir, $username, 'thread_' . $cur_mailbox);
576 if ($thread_sort_messages) {
577 $thread_algorithm = 'REFERENCES';
578 $found_msgs = sqimap_run_thread($imapConnection, $search_string, $search_charset, $thread_algorithm);
581 if (($allow_server_sort) && ($sort < 6)) {
582 $sort_criteria = sqimap_asearch_get_sort_criteria($cur_mailbox, $sort);
583 $found_msgs = sqimap_run_sort($imapConnection, $search_string, $search_charset, $sort_criteria);
586 $found_msgs = sqimap_run_search($imapConnection, $search_string, $search_charset);
587 if (isset($mbox_msgs[$cur_mailbox])) {
588 if ($cur_biop == 'OR') /* Merge with previous results */
589 $mbox_msgs[$cur_mailbox] = sqimap_array_merge_unique($mbox_msgs[$cur_mailbox], $found_msgs);
590 else /* Intersect previous results */
591 $mbox_msgs[$cur_mailbox] = array_values(array_intersect($found_msgs, $mbox_msgs[$cur_mailbox]));
593 else /* No previous results */
594 $mbox_msgs[$cur_mailbox] = $found_msgs;
595 if (empty($mbox_msgs[$cur_mailbox])) /* Can happen with intersect, and we need at the end a contiguous array */
596 unset($mbox_msgs[$cur_mailbox]);
598 $cur_mailbox = $next_mailbox;
601 if (isset($where_array[$cur_crit])) {
602 $criteria = sqimap_asearch_build_criteria($where_array[$cur_crit], $what_array[$cur_crit], $search_charset);
603 if (!empty($criteria)) {
604 $unop = $unop_array[$cur_crit];
606 $criteria = $unop . ' ' . $criteria;
607 /* We need to infix the next non-excluded criteria's biop if it's the same mailbox */
609 for ($next_crit = $cur_crit+
1; $next_crit <= count($where_array); $next_crit++
) {
610 if (empty($exclude_array[$next_crit])) {
611 if (asearch_nz($mailbox_array[$next_crit]) == $cur_mailbox)
612 $next_biop = asearch_nz($biop_array[$next_crit]);
616 if ($next_biop == 'OR')
617 $criteria = $next_biop . ' ' . $criteria;
618 $search_string .= $criteria;
619 $cur_biop = asearch_nz($biop_array[$cur_crit]);