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 * Alex Lemaresquier - Brainstorm - alex at brainstorm.fr
11 * See README file for infos.
15 require_once(SM_PATH
. 'functions/imap.php');
16 require_once(SM_PATH
. 'functions/date.php');
18 /* Set to TRUE to dump the imap dialogue */
19 $imap_asearch_debug_dump = FALSE;
21 $imap_asearch_opcodes = array(
22 /* <message set> => 'asequence', */
23 /*'ALL' is binary operator */
33 'HEADER' => 'afield', /* Special syntax for this one, see below */
34 'KEYWORD' => 'akeyword',
37 /*'NOT' is unary operator */
40 /*'OR' is binary operator */
43 'SENTBEFORE' => 'adate',
45 'SENTSINCE' => 'adate',
48 'SUBJECT' => 'astring',
56 'UNKEYWORD' => 'akeyword',
60 $imap_asearch_months = array(
75 /* This is to avoid the E_NOTICE warnings signaled by marc AT squirrelmail.org. Thanks Marc! */
76 function asearch_nz(&$var)
83 /* This should give the same results as PHP 4 >= 4.3.0's html_entity_decode() */
84 function asearch_unhtmlentities($string) {
85 $trans_tbl = array_flip(get_html_translation_table(HTML_ENTITIES
));
86 for ($i=127; $i<255; $i++
) /* Add &#<dec>; entities */
87 $trans_tbl['&#' . $i . ';'] = chr($i);
88 return strtr($string, $trans_tbl);
89 /* I think the one above is quicker, though it should be benchmarked
90 $string = strtr($string, array_flip(get_html_translation_table(HTML_ENTITIES)));
91 return preg_replace("/&#([0-9]+);/E", "chr('\\1')", $string);
95 function s_dump($var_name, $var_var, $compact = FALSE)
99 echo htmlentities($var_name) . '=';
107 function s_debug_dump($var_name, $var_var, $compact = FALSE)
109 global $imap_asearch_debug_dump;
110 if ($imap_asearch_debug_dump)
111 s_dump($var_name, $var_var, $compact);
116 A quoted string is a sequence of zero or more 7-bit characters,
117 excluding CR and LF, with double quote (<">) characters at each end.
119 quoted-specials = DQUOTE / "\"
121 function sqimap_asearch_encode_string($what, $search_charset)
123 if (strtoupper($search_charset) == 'ISO-2022-JP')
124 $what = mb_convert_encoding($what, 'JIS', 'auto');
125 if (ereg("[\"\\\r\n]", $what))
126 return '{' . strlen($what) . "}\r\n" . $what; /* 4.3 literal form */
127 return '"' . $what . '"'; /* 4.3 quoted string form */
131 Parses a user date string into an rfc2060 date string (<day number>-<US month TLA>-<4 digit year>)
132 Returns a preg_match-style array: [0]: fully formatted date, [1]: day, [2]: month, [3]: year
133 Handles space, slash, backslash, dot and comma as separators (and dash of course ;=)
135 function sqimap_asearch_parse_date($what)
137 global $imap_asearch_months;
140 $what = ereg_replace('[ /\\.,]+', '-', $what);
142 preg_match('/^([0-9]+)-+([^\-]+)-+([0-9]+)$/', $what, $what_parts);
143 if (count($what_parts) == 4) {
144 $what_month = strtolower(asearch_unhtmlentities($what_parts[2]));
145 /* if (!in_array($what_month, $imap_asearch_months)) {*/
146 foreach ($imap_asearch_months as $month_number => $month_code) {
147 if (($what_month == $month_number)
148 ||
($what_month == $month_code)
149 ||
($what_month == strtolower(asearch_unhtmlentities(getMonthName($month_number))))
150 ||
($what_month == strtolower(asearch_unhtmlentities(getMonthAbrv($month_number))))
152 $what_parts[2] = $month_number;
153 $what_parts[0] = $what_parts[1] . '-' . $month_code . '-' . $what_parts[3];
161 $what_parts = array();
165 function sqimap_asearch_build_criteria($opcode, $what, $search_charset)
167 global $imap_asearch_opcodes;
170 switch ($imap_asearch_opcodes[$opcode]) {
173 /* $what = str_replace(' ', '', $what);*/
174 $what = ereg_replace('[^0-9]+', '', $what);
176 $criteria = $opcode . ' ' . $what . ' ';
179 $criteria = $opcode . ' ';
181 case 'afield': /* HEADER field-name: field-body */
182 preg_match('/^([^:]+):(.*)$/', $what, $what_parts);
183 if (count($what_parts) == 3)
184 $criteria = $opcode . ' ' .
185 sqimap_asearch_encode_string($what_parts[1], $search_charset) . ' ' .
186 sqimap_asearch_encode_string($what_parts[2], $search_charset) . ' ';
189 $what_parts = sqimap_asearch_parse_date($what);
190 if (isset($what_parts[0]))
191 $criteria = $opcode . ' ' . $what_parts[0] . ' ';
195 $criteria = $opcode . ' ' . sqimap_asearch_encode_string($what, $search_charset) . ' ';
198 $what = ereg_replace('[^0-9:\(\)]+', '', $what);
200 $criteria = $opcode . ' ' . $what . ' ';
206 function sqimap_run_search($imapConnection, $search_string, $search_charset)
208 global $allow_charset_search, $uid_support;
210 /* 6.4.4 try OPTIONAL [CHARSET] specification first */
211 if ($allow_charset_search && (!empty($search_charset)))
212 $ss = 'SEARCH CHARSET ' . strtoupper($search_charset) . ' ALL ' . $search_string;
214 $ss = 'SEARCH ALL ' . $search_string;
215 s_debug_dump('O', $ss);
217 /* read data back from IMAP */
218 $readin = sqimap_run_command($imapConnection, $ss, false, $result, $message, $uid_support);
220 /* 6.4.4 try US-ASCII charset if we receive a tagged NO response */
221 if (!empty($charset) && strtolower($result) == 'no') {
222 $ss = 'SEARCH CHARSET "US-ASCII" ALL ' . $search_string;
223 s_debug_dump('O', $ss);
224 $readin = sqimap_run_command ($imapConnection, $ss, true, $result, $message); /* no $uid_support? */
229 /* Keep going till we find the SEARCH response */
230 foreach ($readin as $readin_part) {
231 s_debug_dump('I', $readin_part);
232 /* Check to see if a SEARCH response was received */
233 if (substr($readin_part, 0, 9) == '* SEARCH ') {
234 $messagelist = preg_split("/ /", substr($readin_part, 9));
235 } else if (isset($errors)) {
236 $errors = $errors . $readin_part;
238 $errors = $readin_part;
242 /* If nothing is found * SEARCH should be the first error else echo errors */
243 if (isset($errors)) {
244 if (strstr($errors,'* SEARCH'))
246 echo '<!-- ' . htmlspecialchars($errors) . ' -->';
249 if (empty($messagelist))
252 $cnt = count($messagelist);
253 for ($q = 0; $q < $cnt; $q++
)
254 $id[$q] = trim($messagelist[$q]);
258 /* replaces $mbox_msgs[$search_mailbox] = array_values(array_unique(array_merge($mbox_msgs[$search_mailbox], sqimap_run_search($imapConnection, $search_string, $search_charset))));*/
259 function sqimap_array_merge_unique($to, $from)
263 for ($i=0; $i<count($from); $i++
) {
264 if (!in_array($from[$i], $to))
270 function sqimap_asearch($imapConnection, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $mboxes_array)
272 global $languages, $squirrelmail_language;
274 /* ??? what are those for ?? */
275 /* $pos = $search_position;*/
277 $mbox_msgs = array();
278 $search_charset = $languages[$squirrelmail_language]['CHARSET'];
280 $cur_mailbox = $mailbox_array[0];
281 $cur_biop = ''; /* Start with ALL */
282 /* We loop one more time than the real array count, so the last search gets fired */
283 for ($cur_crit = 0; $cur_crit <= count($where_array); $cur_crit++
) {
284 if (empty($exclude_array[$cur_crit])) {
285 $next_mailbox = $mailbox_array[$cur_crit];
286 if ($next_mailbox != $cur_mailbox) {
287 $search_string = trim($search_string); /* Trim out last space */
288 if (($cur_mailbox == 'All Folders') && (!empty($mboxes_array)))
289 $search_mboxes = $mboxes_array;
291 $search_mboxes = array($cur_mailbox);
292 foreach ($search_mboxes as $cur_mailbox) {
293 sqimap_mailbox_select($imapConnection, $cur_mailbox);
294 s_debug_dump('SELECT',$cur_mailbox);
295 if (isset($mbox_msgs[$cur_mailbox])) {
296 if ($cur_biop == 'OR') /* Merge with previous results */
297 $mbox_msgs[$cur_mailbox] = sqimap_array_merge_unique($mbox_msgs[$cur_mailbox], sqimap_run_search($imapConnection, $search_string, $search_charset));
298 else /* Intersect previous results */
299 $mbox_msgs[$cur_mailbox] = array_values(array_intersect(sqimap_run_search($imapConnection, $search_string, $search_charset), $mbox_msgs[$cur_mailbox]));
301 else /* No previous results */
302 $mbox_msgs[$cur_mailbox] = sqimap_run_search($imapConnection, $search_string, $search_charset);
303 if (empty($mbox_msgs[$cur_mailbox])) /* Can happen with intersect, and we need at the end a contiguous array */
304 unset($mbox_msgs[$cur_mailbox]);
306 $cur_mailbox = $next_mailbox;
309 if (isset($where_array[$cur_crit])) {
310 $criteria = sqimap_asearch_build_criteria($where_array[$cur_crit], $what_array[$cur_crit], $search_charset);
311 if (!empty($criteria)) {
312 $unop = $unop_array[$cur_crit];
314 $criteria = $unop . ' ' . $criteria;
315 /* We need to infix the next non-excluded criteria's biop if it's the same mailbox */
317 for ($next_crit = $cur_crit+
1; $next_crit <= count($where_array); $next_crit++
) {
318 if (empty($exclude_array[$next_crit])) {
319 if (asearch_nz($mailbox_array[$next_crit]) == $cur_mailbox)
320 $next_biop = asearch_nz($biop_array[$next_crit]);
324 if ($next_biop == 'OR')
325 $criteria = $next_biop . ' ' . $criteria;
326 $search_string .= $criteria;
327 $cur_biop = asearch_nz($biop_array[$cur_crit]);