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
11 * Subfolder search idea from Patch #806075 by Thomas Pohl xraven at users.sourceforge.net. Thanks Thomas!
14 * @package squirrelmail
17 * @link http://www.ietf.org/rfc/rfc3501.txt
18 * @author Alex Lemaresquier - Brainstorm - alex at brainstorm.fr
21 /** This functionality requires the IMAP and date functions
23 require_once(SM_PATH
. 'functions/imap_general.php');
24 require_once(SM_PATH
. 'functions/date.php');
26 /** Set to TRUE to dump the imap dialogue
27 * @global bool $imap_asearch_debug_dump
29 $imap_asearch_debug_dump = FALSE;
32 * @global array $imap_asearch_opcodes
34 $imap_asearch_opcodes = array(
35 /* <sequence-set> => 'asequence', */ // Special handling, @see sqimap_asearch_build_criteria()
36 /*'ALL' is binary operator */
46 'HEADER' => 'afield', // Special syntax for this one, @see sqimap_asearch_build_criteria()
47 'KEYWORD' => 'akeyword',
50 /*'NOT' is unary operator */
53 /*'OR' is binary operator */
56 'SENTBEFORE' => 'adate',
58 'SENTSINCE' => 'adate',
61 'SUBJECT' => 'astring',
69 'UNKEYWORD' => 'akeyword',
73 /** Imap SEARCH month names encoding
74 * @global array $imap_asearch_months
76 $imap_asearch_months = array(
91 /** Error message titles according to imap server returned code
92 * @global array $imap_error_titles
94 $imap_error_titles = array(
96 'NO' => _("ERROR : Could not complete request."),
97 'BAD' => _("ERROR : Bad or malformed request."),
98 'BYE' => _("ERROR : Imap server closed the connection."),
99 '' => _("ERROR : Connection dropped by imap-server.")
103 * Function to display an error related to an IMAP-query.
104 * We need to do our own error management since we may receive NO responses on purpose (even BAD with SORT or THREAD)
105 * so we call sqimap_error_box() if the function exists (sm >= 1.5) or use our own embedded code
106 * @global array imap_error_titles
107 * @param string $response the imap server response code
108 * @param string $query the failed query
109 * @param string $message an optional error message
110 * @param string $link an optional link to try again
112 //@global array color sm colors array
113 function sqimap_asearch_error_box($response, $query, $message, $link = '')
115 global $imap_error_titles;
117 if (!array_key_exists($response, $imap_error_titles))
118 $title = _("ERROR : Unknown imap response.");
120 $title = $imap_error_titles[$response];
122 $message_title = _("Reason Given: ");
124 $message_title = _("Possible reason : ");
125 if (function_exists('sqimap_error_box'))
126 sqimap_error_box($title, $query, $message_title, $message, $link);
127 else { //Straight copy of 1.5 imap_general.php:sqimap_error_box(). Can be removed at a later time
129 require_once(SM_PATH
. 'functions/display_messages.php');
130 $string = "<font color=$color[2]><b>\n" . $title . "</b><br>\n";
132 $string .= _("Query:") . ' ' . htmlspecialchars($query) . '<br>';
133 if ($message_title != '')
134 $string .= $message_title;
136 $string .= htmlspecialchars($message);
139 $string .= "</font><br>\n";
140 error_box($string,$color);
145 * This is a convenient way to avoid spreading if (isset(... all over the code
146 * @param mixed $var any variable (reference)
147 * @param mixed $def default value to return if unset (default is zls (''), pass 0 or array() when appropriate)
148 * @return mixed $def if $var is unset, otherwise $var
150 function asearch_nz(&$var, $def = '')
158 * This should give the same results as PHP 4 >= 4.3.0's html_entity_decode(),
159 * except it doesn't handle hex constructs
160 * @param string $string string to unhtmlentity()
161 * @return string decoded string
163 function asearch_unhtmlentities($string) {
164 $trans_tbl = array_flip(get_html_translation_table(HTML_ENTITIES
));
165 for ($i=127; $i<255; $i++
) /* Add &#<dec>; entities */
166 $trans_tbl['&#' . $i . ';'] = chr($i);
167 return strtr($string, $trans_tbl);
168 /* I think the one above is quicker, though it should be benchmarked
169 $string = strtr($string, array_flip(get_html_translation_table(HTML_ENTITIES)));
170 return preg_replace("/&#([0-9]+);/E", "chr('\\1')", $string);
175 * Provide an easy way to dump the imap dialogue if $imap_asearch_debug_dump is TRUE
176 * @global bool imap_asearch_debug_dump
177 * @param string $var_name
178 * @param string $var_var
180 function s_debug_dump($var_name, $var_var)
182 global $imap_asearch_debug_dump;
183 if ($imap_asearch_debug_dump) {
184 if (function_exists('sm_print_r')) //Only exists since 1.4.2
185 sm_print_r($var_name, $var_var); //Better be the 'varargs' version ;)
188 echo htmlentities($var_name);
195 /** Encode a string to quoted or literal as defined in rfc 3501
198 * A quoted string is a sequence of zero or more 7-bit characters,
199 * excluding CR and LF, with double quote (<">) characters at each end.
200 * - 9. Formal Syntax:
201 * quoted-specials = DQUOTE / "\"
202 * @param string $what string to encode
203 * @param string $charset search charset used
204 * @return string encoded string
206 function sqimap_asearch_encode_string($what, $charset)
208 if (strtoupper($charset) == 'ISO-2022-JP') // This should be now handled in imap_utf7_local?
209 $what = mb_convert_encoding($what, 'JIS', 'auto');
210 if (preg_match('/["\\\\\r\n\x80-\xff]/', $what))
211 return '{' . strlen($what) . "}\r\n" . $what; // 4.3 literal form
212 return '"' . $what . '"'; // 4.3 quoted string form
216 * Parses a user date string into an rfc 3501 date string
217 * Handles space, slash, backslash, dot and comma as separators (and dash of course ;=)
218 * @global array imap_asearch_months
219 * @param string user date
220 * @return array a preg_match-style array:
221 * - [0] = fully formatted rfc 3501 date string (<day number>-<US month TLA>-<4 digit year>)
226 function sqimap_asearch_parse_date($what)
228 global $imap_asearch_months;
231 $what = ereg_replace('[ /\\.,]+', '-', $what);
233 preg_match('/^([0-9]+)-+([^\-]+)-+([0-9]+)$/', $what, $what_parts);
234 if (count($what_parts) == 4) {
235 $what_month = strtolower(asearch_unhtmlentities($what_parts[2]));
236 /* if (!in_array($what_month, $imap_asearch_months)) {*/
237 foreach ($imap_asearch_months as $month_number => $month_code) {
238 if (($what_month == $month_number)
239 ||
($what_month == $month_code)
240 ||
($what_month == strtolower(asearch_unhtmlentities(getMonthName($month_number))))
241 ||
($what_month == strtolower(asearch_unhtmlentities(getMonthAbrv($month_number))))
243 $what_parts[2] = $month_number;
244 $what_parts[0] = $what_parts[1] . '-' . $month_code . '-' . $what_parts[3];
252 $what_parts = array();
257 * Build one criteria sequence
258 * @global array imap_asearch_opcodes
259 * @param string $opcode search opcode
260 * @param string $what opcode argument
261 * @param string $charset search charset
262 * @return string one full criteria sequence
264 function sqimap_asearch_build_criteria($opcode, $what, $charset)
266 global $imap_asearch_opcodes;
269 switch ($imap_asearch_opcodes[$opcode]) {
272 $what = str_replace(' ', '', $what);
273 $what = ereg_replace('[^0-9]+[^KMG]$', '', strtoupper($what));
275 switch (substr($what, -1)) {
277 $what = substr($what, 0, -1) << 30;
280 $what = substr($what, 0, -1) << 20;
283 $what = substr($what, 0, -1) << 10;
286 $criteria = $opcode . ' ' . $what . ' ';
290 $criteria = $opcode . ' ';
292 case 'afield': /* HEADER field-name: field-body */
293 preg_match('/^([^:]+):(.*)$/', $what, $what_parts);
294 if (count($what_parts) == 3)
295 $criteria = $opcode . ' ' .
296 sqimap_asearch_encode_string($what_parts[1], $charset) . ' ' .
297 sqimap_asearch_encode_string($what_parts[2], $charset) . ' ';
300 $what_parts = sqimap_asearch_parse_date($what);
301 if (isset($what_parts[0]))
302 $criteria = $opcode . ' ' . $what_parts[0] . ' ';
306 $criteria = $opcode . ' ' . sqimap_asearch_encode_string($what, $charset) . ' ';
309 $what = ereg_replace('[^0-9:\(\)]+', '', $what);
311 $criteria = $opcode . ' ' . $what . ' ';
318 * Another way to do array_values(array_unique(array_merge($to, $from)));
319 * @param array $to to array (reference)
320 * @param array $from from array
321 * @return array uniquely merged array
323 function sqimap_array_merge_unique(&$to, $from)
327 $count = count($from);
328 for ($i = 0; $i < $count; $i++
) {
329 if (!in_array($from[$i], $to))
336 * Run the imap SEARCH command as defined in rfc 3501
337 * @link http://www.ietf.org/rfc/rfc3501.txt
338 * @param resource $imapConnection the current imap stream
339 * @param string $search_string the full search expression eg "ALL RECENT"
340 * @param string $search_charset charset to use or zls ('')
341 * @return array an IDs or UIDs array of matching messages or an empty array
343 function sqimap_run_search($imapConnection, $search_string, $search_charset)
345 //For some reason, this seems to happen and forbids searching servers not allowing OPTIONAL [CHARSET]
346 if (strtoupper($search_charset) == 'US-ASCII')
347 $search_charset = '';
348 /* 6.4.4 try OPTIONAL [CHARSET] specification first */
349 if ($search_charset != '')
350 $query = 'SEARCH CHARSET "' . strtoupper($search_charset) . '" ' . $search_string;
352 $query = 'SEARCH ' . $search_string;
353 s_debug_dump('C:', $query);
354 $readin = sqimap_run_command($imapConnection, $query, false, $response, $message, TRUE);
356 /* 6.4.4 try US-ASCII charset if we tried an OPTIONAL [CHARSET] and received a tagged NO response (SHOULD be [BADCHARSET]) */
357 if (($search_charset != '') && (strtoupper($response) == 'NO')) {
358 $query = 'SEARCH CHARSET US-ASCII ' . $search_string;
359 s_debug_dump('C:', $query);
360 $readin = sqimap_run_command($imapConnection, $query, false, $response, $message, TRUE);
362 if (strtoupper($response) != 'OK') {
363 sqimap_asearch_error_box($response, $query, $message);
366 $messagelist = parseUidList($readin,'SEARCH');
368 if (empty($messagelist)) //Empty search response, ie '* SEARCH'
371 $cnt = count($messagelist);
372 for ($q = 0; $q < $cnt; $q++
)
373 $id[$q] = trim($messagelist[$q]);
378 * @global bool allow_charset_search user setting
379 * @global array languages sm languages array
380 * @global string squirrelmail_language user language setting
381 * @return string the user defined charset if $allow_charset_search is TRUE else zls ('')
383 function sqimap_asearch_get_charset()
385 global $allow_charset_search, $languages, $squirrelmail_language;
387 if ($allow_charset_search)
388 return $languages[$squirrelmail_language]['CHARSET'];
393 * Convert sm internal sort to imap sort taking care of:
394 * - user defined date sorting (ARRIVAL vs DATE)
395 * - if the searched mailbox is the sent folder then TO is being used instead of FROM
396 * - reverse order by using REVERSE
397 * @param string $mailbox mailbox name to sort
398 * @param integer $sort_by sm sort criteria index
399 * @global bool internal_date_sort sort by arrival date instead of message date
400 * @global string sent_folder sent folder name
401 * @return string imap sort criteria
403 function sqimap_asearch_get_sort_criteria($mailbox, $sort_by)
405 global $internal_date_sort, $sent_folder;
407 $sort_opcodes = array ('DATE', 'FROM', 'SUBJECT', 'SIZE');
408 if ($internal_date_sort == true)
409 $sort_opcodes[0] = 'ARRIVAL';
410 // if (handleAsSent($mailbox))
411 // if (isSentFolder($mailbox))
412 if ($mailbox == $sent_folder)
413 $sort_opcodes[1] = 'TO';
414 return (($sort_by %
2) ?
'' : 'REVERSE ') . $sort_opcodes[($sort_by >> 1) & 3];
418 * @param string $cur_mailbox unformatted mailbox name
419 * @param array $boxes_unformatted selectable mailbox unformatted names array (reference)
420 * @return array sub mailboxes unformatted names
422 function sqimap_asearch_get_sub_mailboxes($cur_mailbox, &$mboxes_array)
424 $sub_mboxes_array = array();
425 $boxcount = count($mboxes_array);
426 for ($boxnum=0; $boxnum < $boxcount; $boxnum++
) {
427 if (isBoxBelow($mboxes_array[$boxnum], $cur_mailbox))
428 $sub_mboxes_array[] = $mboxes_array[$boxnum];
430 return $sub_mboxes_array;
434 * Create the search query strings for all given criteria and merge results for every mailbox
435 * @param resource $imapConnection
436 * @param array $mailbox_array (reference)
437 * @param array $biop_array (reference)
438 * @param array $unop_array (reference)
439 * @param array $where_array (reference)
440 * @param array $what_array (reference)
441 * @param array $exclude_array (reference)
442 * @param array $sub_array (reference)
443 * @param array $mboxes_array selectable unformatted mailboxes names (reference)
444 * @return array array(mailbox => array(UIDs))
446 function sqimap_asearch($imapConnection, &$mailbox_array, &$biop_array, &$unop_array, &$where_array, &$what_array, &$exclude_array, &$sub_array, &$mboxes_array)
449 $search_charset = sqimap_asearch_get_charset();
450 $mbox_msgs = array();
451 $mbox_search = array();
453 $cur_mailbox = $mailbox_array[0];
454 $cur_biop = ''; /* Start with ALL */
455 /* We loop one more time than the real array count, so the last search gets fired */
456 for ($cur_crit=0,$iCnt=count($where_array); $cur_crit <= $iCnt; ++
$cur_crit) {
457 if (empty($exclude_array[$cur_crit])) {
458 $next_mailbox = (isset($mailbox_array[$cur_crit])) ?
$mailbox_array[$cur_crit] : false;
459 if ($next_mailbox != $cur_mailbox) {
460 $search_string = trim($search_string); /* Trim out last space */
461 if ($cur_mailbox == 'All Folders')
462 $search_mboxes = $mboxes_array;
463 else if ((!empty($sub_array[$cur_crit - 1])) ||
(!in_array($cur_mailbox, $mboxes_array)))
464 $search_mboxes = sqimap_asearch_get_sub_mailboxes($cur_mailbox, $mboxes_array);
466 $search_mboxes = array($cur_mailbox);
467 foreach ($search_mboxes as $cur_mailbox) {
468 if (isset($mbox_search[$cur_mailbox])) {
469 $mbox_search[$cur_mailbox]['search'] .= ' ' . $search_string;
471 $mbox_search[$cur_mailbox]['search'] = $search_string;
473 $mbox_search[$cur_mailbox]['charset'] = $search_charset;
475 $cur_mailbox = $next_mailbox;
479 if (isset($where_array[$cur_crit]) && empty($exclude_array[$cur_crit])) {
480 for ($crit = $cur_crit; $crit < count($where_array); $crit++
) {
481 $criteria = trim(sqimap_asearch_build_criteria($where_array[$crit], $what_array[$crit], $search_charset));
482 if (!empty($criteria) && empty($exclude_array[$crit])) {
483 if (asearch_nz($mailbox_array[$crit]) == $cur_mailbox) {
484 $unop = $unop_array[$crit];
486 $criteria = $unop . ' ' . $criteria;
488 $aCriteria[] = array($biop_array[$crit], $criteria);
492 $exclude_array[$crit] = true;
495 for($i=0,$iCnt=count($aCriteria);$i<$iCnt;++
$i) {
496 $cur_biop = $aCriteria[$i][0];
497 $next_biop = (isset($aCriteria[$i+
1][0])) ?
$aCriteria[$i+
1][0] : false;
498 if ($next_biop != $cur_biop && $next_biop == 'OR') {
499 $aSearch[] = 'OR '.$aCriteria[$i][1];
500 } else if ($cur_biop != 'OR') {
501 $aSearch[] = 'ALL '.$aCriteria[$i][1];
502 } else { // or only supports 2 search keys so we need to create a parenthized list
503 $prev_biop = (isset($aCriteria[$i-1][0])) ?
$aCriteria[$i-1][0] : false;
504 if ($prev_biop == $cur_biop) {
505 $last = $aSearch[$i-1];
506 if (!substr($last,-1) == ')') {
507 $aSearch[$i-1] = "(OR $last";
508 $aSearch[] = $aCriteria[$i][1].')';
511 while ($last && substr($last,-1) == ')') {
512 $last = substr($last,0,-1);
515 $aSearch[$i-1] = "(OR $last";
516 $aSearch[] = $aCriteria[$i][1].$sEnd.')';
519 $aSearch[] = $aCriteria[$i][1];
524 $search_string .= implode(' ',$aSearch);
529 return ($mbox_search);