cd33ec11 |
1 | <?php |
2 | |
3 | /** |
4 | * imap_search.php |
5 | * |
6 | * Copyright (c) 1999-2003 The SquirrelMail Project Team |
7 | * Licensed under the GNU GPL. For full terms see the file COPYING. |
8 | * |
9 | * IMAP asearch routines |
d6c32258 |
10 | * @author Alex Lemaresquier - Brainstorm - alex at brainstorm.fr |
cd33ec11 |
11 | * See README file for infos. |
d6c32258 |
12 | * @package squirrelmail |
cd33ec11 |
13 | * |
14 | */ |
15 | |
d6c32258 |
16 | /** This functionality requires the IMAP and date functions */ |
ff6f916c |
17 | require_once(SM_PATH . 'functions/imap_general.php'); |
cd33ec11 |
18 | require_once(SM_PATH . 'functions/date.php'); |
19 | |
d6c32258 |
20 | /** Set to TRUE to dump the imap dialogue */ |
cd33ec11 |
21 | $imap_asearch_debug_dump = FALSE; |
22 | |
23 | $imap_asearch_opcodes = array( |
24 | /* <message set> => 'asequence', */ |
25 | /*'ALL' is binary operator */ |
26 | 'ANSWERED' => '', |
27 | 'BCC' => 'astring', |
28 | 'BEFORE' => 'adate', |
29 | 'BODY' => 'astring', |
30 | 'CC' => 'astring', |
31 | 'DELETED' => '', |
32 | 'DRAFT' => '', |
33 | 'FLAGGED' => '', |
34 | 'FROM' => 'astring', |
35 | 'HEADER' => 'afield', /* Special syntax for this one, see below */ |
36 | 'KEYWORD' => 'akeyword', |
37 | 'LARGER' => 'anum', |
38 | 'NEW' => '', |
39 | /*'NOT' is unary operator */ |
40 | 'OLD' => '', |
41 | 'ON' => 'adate', |
42 | /*'OR' is binary operator */ |
43 | 'RECENT' => '', |
44 | 'SEEN' => '', |
45 | 'SENTBEFORE' => 'adate', |
46 | 'SENTON' => 'adate', |
47 | 'SENTSINCE' => 'adate', |
48 | 'SINCE' => 'adate', |
49 | 'SMALLER' => 'anum', |
50 | 'SUBJECT' => 'astring', |
51 | 'TEXT' => 'astring', |
52 | 'TO' => 'astring', |
53 | 'UID' => 'asequence', |
54 | 'UNANSWERED' => '', |
55 | 'UNDELETED' => '', |
56 | 'UNDRAFT' => '', |
57 | 'UNFLAGGED' => '', |
58 | 'UNKEYWORD' => 'akeyword', |
59 | 'UNSEEN' => '' |
60 | ); |
61 | |
62 | $imap_asearch_months = array( |
63 | '01' => 'jan', |
64 | '02' => 'feb', |
65 | '03' => 'mar', |
66 | '04' => 'apr', |
67 | '05' => 'may', |
68 | '06' => 'jun', |
69 | '07' => 'jul', |
70 | '08' => 'aug', |
71 | '09' => 'sep', |
72 | '10' => 'oct', |
73 | '11' => 'nov', |
74 | '12' => 'dec' |
75 | ); |
76 | |
ff6f916c |
77 | $imap_error_titles = array( |
78 | 'OK' => '', |
79 | 'NO' => _("ERROR : Could not complete request."), |
80 | 'BAD' => _("ERROR : Bad or malformed request."), |
81 | 'BYE' => _("ERROR : Imap server closed the connection.") |
82 | ); |
83 | |
48af4b64 |
84 | // why can't this just use sqimap_error_box() ? |
c2d47d51 |
85 | // It does, indeed I isolated sqimap_error_box() as a stand-alone function just for this purpose ;) |
ff6f916c |
86 | function sqimap_asearch_error_box($response, $query, $message) |
87 | { |
88 | global $imap_error_titles; |
89 | |
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."); |
93 | else |
94 | $title = $imap_error_titles[$response]; |
b28bec15 |
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 |
797784f9 |
99 | global $color; |
b28bec15 |
100 | require_once(SM_PATH . 'functions/display_messages.php'); |
101 | $string = "<font color=$color[2]><b>\n" . $title . "</b><br>\n"; |
102 | if ($query != '') |
103 | $string .= _("Query:") . ' ' . htmlspecialchars($query) . '<br>'; |
104 | if ($message_title != '') |
105 | $string .= $message_title; |
106 | if ($message != '') |
107 | $string .= htmlspecialchars($message); |
108 | $string .= "</font><br>\n"; |
109 | error_box($string,$color); |
110 | } |
ff6f916c |
111 | } |
112 | |
48af4b64 |
113 | /** |
114 | * This is to avoid the E_NOTICE warnings signaled by marc AT squirrelmail.org. Thanks Marc! |
115 | */ |
cd33ec11 |
116 | function asearch_nz(&$var) |
117 | { |
118 | if (isset($var)) |
119 | return $var; |
120 | return ''; |
121 | } |
122 | |
48af4b64 |
123 | /** |
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 |
126 | */ |
cd33ec11 |
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); |
135 | */ |
136 | } |
137 | |
ff6f916c |
138 | function s_debug_dump($var_name, $var_var) |
cd33ec11 |
139 | { |
140 | global $imap_asearch_debug_dump; |
f9fb0d38 |
141 | if ($imap_asearch_debug_dump) { |
b28bec15 |
142 | if (function_exists('sm_print_r')) //Only exists since 1.4.2 |
f9fb0d38 |
143 | sm_print_r($var_name, $var_var); //Better be the 'varargs' version ;) |
144 | else { |
145 | echo '<pre>'; |
146 | echo htmlentities($var_name); |
147 | print_r($var_var); |
148 | echo '</pre>'; |
149 | } |
150 | } |
cd33ec11 |
151 | } |
152 | |
375b552d |
153 | /* |
154 | 4.3 String: |
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. |
157 | 9. Formal Syntax: |
158 | quoted-specials = DQUOTE / "\" |
159 | */ |
f945228f |
160 | function sqimap_asearch_encode_string($what, $charset) |
cd33ec11 |
161 | { |
f945228f |
162 | if (strtoupper($charset) == 'ISO-2022-JP') // This should be now handled in imap_utf7_local? |
cd33ec11 |
163 | $what = mb_convert_encoding($what, 'JIS', 'auto'); |
ff6f916c |
164 | //if (ereg("[\"\\\r\n\x80-\xff]", $what)) |
165 | if (preg_match('/["\\\\\r\n\x80-\xff]/', $what)) |
797784f9 |
166 | return '{' . strlen($what) . "}\r\n" . $what; // 4.3 literal form |
167 | return '"' . $what . '"'; // 4.3 quoted string form |
cd33ec11 |
168 | } |
169 | |
48af4b64 |
170 | /** |
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 ;=) |
176 | */ |
cd33ec11 |
177 | function sqimap_asearch_parse_date($what) |
178 | { |
179 | global $imap_asearch_months; |
180 | |
181 | $what = trim($what); |
375b552d |
182 | $what = ereg_replace('[ /\\.,]+', '-', $what); |
cd33ec11 |
183 | if ($what) { |
375b552d |
184 | preg_match('/^([0-9]+)-+([^\-]+)-+([0-9]+)$/', $what, $what_parts); |
cd33ec11 |
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)))) |
193 | ) { |
194 | $what_parts[2] = $month_number; |
195 | $what_parts[0] = $what_parts[1] . '-' . $month_code . '-' . $what_parts[3]; |
196 | break; |
197 | } |
198 | } |
199 | /* }*/ |
200 | } |
201 | } |
202 | else |
203 | $what_parts = array(); |
204 | return $what_parts; |
205 | } |
206 | |
f945228f |
207 | function sqimap_asearch_build_criteria($opcode, $what, $charset) |
cd33ec11 |
208 | { |
209 | global $imap_asearch_opcodes; |
210 | |
375b552d |
211 | $criteria = ''; |
cd33ec11 |
212 | switch ($imap_asearch_opcodes[$opcode]) { |
213 | default: |
214 | case 'anum': |
215 | /* $what = str_replace(' ', '', $what);*/ |
216 | $what = ereg_replace('[^0-9]+', '', $what); |
217 | if ($what != '') |
218 | $criteria = $opcode . ' ' . $what . ' '; |
219 | break; |
220 | case '': /* aflag */ |
221 | $criteria = $opcode . ' '; |
222 | break; |
223 | case 'afield': /* HEADER field-name: field-body */ |
224 | preg_match('/^([^:]+):(.*)$/', $what, $what_parts); |
225 | if (count($what_parts) == 3) |
226 | $criteria = $opcode . ' ' . |
f945228f |
227 | sqimap_asearch_encode_string($what_parts[1], $charset) . ' ' . |
228 | sqimap_asearch_encode_string($what_parts[2], $charset) . ' '; |
cd33ec11 |
229 | break; |
230 | case 'adate': |
231 | $what_parts = sqimap_asearch_parse_date($what); |
375b552d |
232 | if (isset($what_parts[0])) |
cd33ec11 |
233 | $criteria = $opcode . ' ' . $what_parts[0] . ' '; |
234 | break; |
235 | case 'akeyword': |
236 | case 'astring': |
f945228f |
237 | $criteria = $opcode . ' ' . sqimap_asearch_encode_string($what, $charset) . ' '; |
cd33ec11 |
238 | break; |
239 | case 'asequence': |
240 | $what = ereg_replace('[^0-9:\(\)]+', '', $what); |
241 | if ($what != '') |
242 | $criteria = $opcode . ' ' . $what . ' '; |
243 | break; |
244 | } |
245 | return $criteria; |
246 | } |
247 | |
75d24fd2 |
248 | // equivalent to : $to = array_values(array_unique(array_merge($to, $from))); |
249 | function sqimap_array_merge_unique($to, $from) |
250 | { |
251 | if (empty($to)) |
252 | return $from; |
253 | $count = count($from); |
254 | for ($i = 0; $i < $count; $i++) { |
255 | if (!in_array($from[$i], $to)) |
256 | $to[] = $from[$i]; |
257 | } |
258 | return $to; |
259 | } |
260 | |
cd33ec11 |
261 | function sqimap_run_search($imapConnection, $search_string, $search_charset) |
262 | { |
f945228f |
263 | global $uid_support; |
cd33ec11 |
264 | |
265 | /* 6.4.4 try OPTIONAL [CHARSET] specification first */ |
f945228f |
266 | if ($search_charset != '') |
267 | $query = 'SEARCH CHARSET "' . strtoupper($search_charset) . '" ALL ' . $search_string; |
cd33ec11 |
268 | else |
ff6f916c |
269 | $query = 'SEARCH ALL ' . $search_string; |
270 | s_debug_dump('C:', $query); |
ff6f916c |
271 | $readin = sqimap_run_command($imapConnection, $query, false, $response, $message, $uid_support); |
cd33ec11 |
272 | |
f945228f |
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; |
ff6f916c |
276 | s_debug_dump('C:', $query); |
f945228f |
277 | $readin = sqimap_run_command($imapConnection, $query, false, $response, $message, $uid_support); |
ff6f916c |
278 | } |
279 | if (strtoupper($response) != 'OK') { |
280 | sqimap_asearch_error_box($response, $query, $message); |
281 | return array(); |
cd33ec11 |
282 | } |
283 | |
284 | unset($messagelist); |
285 | |
75d24fd2 |
286 | // Keep going till we find the * SEARCH response |
cd33ec11 |
287 | foreach ($readin as $readin_part) { |
ff6f916c |
288 | s_debug_dump('S:', $readin_part); |
cd33ec11 |
289 | if (substr($readin_part, 0, 9) == '* SEARCH ') { |
75d24fd2 |
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))); |
cd33ec11 |
294 | } |
295 | } |
296 | |
ff6f916c |
297 | if (empty($messagelist)) //Empty search response, ie '* SEARCH' |
3f075f6c |
298 | return array(); |
299 | |
cd33ec11 |
300 | $cnt = count($messagelist); |
301 | for ($q = 0; $q < $cnt; $q++) |
302 | $id[$q] = trim($messagelist[$q]); |
303 | return $id; |
304 | } |
305 | |
c2d47d51 |
306 | function sqimap_run_sort($imapConnection, $search_string, $search_charset, $sort_criteria) |
307 | { |
308 | global $uid_support; |
309 | |
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); |
315 | |
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); |
321 | } |
322 | |
323 | if (strtoupper($response) != 'OK') { |
75d24fd2 |
324 | // sqimap_asearch_error_box($response, $query, $message); |
325 | // return array(); |
326 | return sqimap_run_search($imapConnection, $search_string, $search_charset); // Fell back to standard search |
c2d47d51 |
327 | } |
328 | |
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 |
335 | } |
336 | } |
337 | |
338 | if (empty($messagelist)) //Empty search response, ie '* SORT' |
339 | return array(); |
340 | |
341 | $cnt = count($messagelist); |
342 | for ($q = 0; $q < $cnt; $q++) |
343 | $id[$q] = trim($messagelist[$q]); |
344 | return $id; |
345 | } |
346 | |
347 | function sqimap_run_thread($imapConnection, $search_string, $search_charset, $thread_algorithm) |
348 | { |
349 | global $thread_new, $server_sort_array; |
350 | |
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'); |
355 | |
356 | $thread_new = array(); |
357 | $thread_new[0] = ""; |
358 | |
359 | $server_sort_array = array(); |
360 | |
361 | global $uid_support; |
362 | |
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); |
368 | |
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); |
374 | } |
375 | |
376 | if (strtoupper($response) != 'OK') { |
75d24fd2 |
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 |
381 | */ |
c2d47d51 |
382 | // sqimap_asearch_error_box($response, $query, $message); |
383 | // return array(); |
384 | return sqimap_run_search($imapConnection, $search_string, $search_charset); // Fell back to standard search |
385 | } |
386 | |
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 |
393 | } |
394 | } |
395 | |
396 | if (empty($thread_temp)) //Empty search response, ie '* THREAD' |
397 | return array(); |
398 | |
399 | $char_count = count($thread_temp); |
400 | $counter = 0; |
401 | $k = 0; |
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]; |
405 | } |
406 | elseif ($thread_temp[$i] == '(') { |
407 | $thread_new[$k] .= $thread_temp[$i]; |
408 | $counter++; |
409 | } |
410 | elseif ($thread_temp[$i] == ')') { |
411 | if ($counter > 1) { |
412 | $thread_new[$k] .= $thread_temp[$i]; |
413 | $counter = $counter - 1; |
414 | } |
415 | else { |
416 | $thread_new[$k] .= $thread_temp[$i]; |
417 | $k++; |
418 | $thread_new[$k] = ""; |
419 | $counter = $counter - 1; |
420 | } |
421 | } |
422 | } |
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'); |
431 | return $thread_list; |
432 | } |
433 | |
f945228f |
434 | function sqimap_asearch_get_charset() |
435 | { |
436 | global $allow_charset_search, $languages, $squirrelmail_language; |
437 | |
438 | if ($allow_charset_search) |
439 | return $languages[$squirrelmail_language]['CHARSET']; |
440 | return ''; |
441 | } |
442 | |
c2d47d51 |
443 | function sqimap_asearch_get_sort_criteria($mailbox, $sort_by) |
444 | { |
445 | global $internal_date_sort, $sent_folder; |
446 | |
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]; |
455 | } |
456 | |
cd33ec11 |
457 | function sqimap_asearch($imapConnection, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $mboxes_array) |
458 | { |
c2d47d51 |
459 | global $allow_server_sort, $sort, $allow_thread_sort, $thread_sort_messages; |
460 | global $data_dir, $username; |
461 | |
f945228f |
462 | $search_charset = sqimap_asearch_get_charset(); |
cd33ec11 |
463 | $mbox_msgs = array(); |
cd33ec11 |
464 | $search_string = ''; |
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; |
475 | else |
476 | $search_mboxes = array($cur_mailbox); |
477 | foreach ($search_mboxes as $cur_mailbox) { |
ff6f916c |
478 | s_debug_dump('C:SELECT:', $cur_mailbox); |
27907482 |
479 | sqimap_mailbox_select($imapConnection, $cur_mailbox); |
c2d47d51 |
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); |
484 | } |
485 | else |
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); |
489 | } |
490 | else |
491 | $found_msgs = sqimap_run_search($imapConnection, $search_string, $search_charset); |
5ab3c3fe |
492 | if (isset($mbox_msgs[$cur_mailbox])) { |
493 | if ($cur_biop == 'OR') /* Merge with previous results */ |
c2d47d51 |
494 | $mbox_msgs[$cur_mailbox] = sqimap_array_merge_unique($mbox_msgs[$cur_mailbox], $found_msgs); |
5ab3c3fe |
495 | else /* Intersect previous results */ |
c2d47d51 |
496 | $mbox_msgs[$cur_mailbox] = array_values(array_intersect($found_msgs, $mbox_msgs[$cur_mailbox])); |
cd33ec11 |
497 | } |
5ab3c3fe |
498 | else /* No previous results */ |
c2d47d51 |
499 | $mbox_msgs[$cur_mailbox] = $found_msgs; |
cd33ec11 |
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]); |
502 | } |
503 | $cur_mailbox = $next_mailbox; |
504 | $search_string = ''; |
505 | } |
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]; |
510 | if (!empty($unop)) |
511 | $criteria = $unop . ' ' . $criteria; |
512 | /* We need to infix the next non-excluded criteria's biop if it's the same mailbox */ |
513 | $next_biop = ''; |
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) |
5ab3c3fe |
517 | $next_biop = asearch_nz($biop_array[$next_crit]); |
cd33ec11 |
518 | break; |
519 | } |
520 | } |
521 | if ($next_biop == 'OR') |
522 | $criteria = $next_biop . ' ' . $criteria; |
523 | $search_string .= $criteria; |
524 | $cur_biop = asearch_nz($biop_array[$cur_crit]); |
525 | } |
526 | } |
527 | } |
528 | } |
529 | return $mbox_msgs; |
530 | } |
531 | |
d6c32258 |
532 | ?> |