Move $sqimap_session_id from global to static scope.
[squirrelmail.git] / functions / imap_asearch.php
1 <?php
2
3 /**
4 * imap_search.php
5 *
6 * Copyright (c) 1999-2004 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * IMAP asearch routines
10 *
11 * $Id$
12 * @package squirrelmail
13 * @see search.php
14 * @link ftp://ftp.rfc-editor.org/in-notes/rfc3501.txt
15 * @author Alex Lemaresquier - Brainstorm - alex at brainstorm.fr
16 *
17 * Subfolder search idea from Patch #806075 by Thomas Pohl xraven at users.sourceforge.net. Thanks Thomas!
18 */
19
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');
23
24 /** Set to TRUE to dump the imap dialogue
25 * @global bool $imap_asearch_debug_dump
26 */
27 $imap_asearch_debug_dump = FALSE;
28
29 /** Imap SEARCH keys
30 * @global array $imap_asearch_opcodes
31 */
32 $imap_asearch_opcodes = array(
33 /* <message set> => 'asequence', */
34 /*'ALL' is binary operator */
35 'ANSWERED' => '',
36 'BCC' => 'astring',
37 'BEFORE' => 'adate',
38 'BODY' => 'astring',
39 'CC' => 'astring',
40 'DELETED' => '',
41 'DRAFT' => '',
42 'FLAGGED' => '',
43 'FROM' => 'astring',
44 'HEADER' => 'afield', /* Special syntax for this one, see below */
45 'KEYWORD' => 'akeyword',
46 'LARGER' => 'anum',
47 'NEW' => '',
48 /*'NOT' is unary operator */
49 'OLD' => '',
50 'ON' => 'adate',
51 /*'OR' is binary operator */
52 'RECENT' => '',
53 'SEEN' => '',
54 'SENTBEFORE' => 'adate',
55 'SENTON' => 'adate',
56 'SENTSINCE' => 'adate',
57 'SINCE' => 'adate',
58 'SMALLER' => 'anum',
59 'SUBJECT' => 'astring',
60 'TEXT' => 'astring',
61 'TO' => 'astring',
62 'UID' => 'asequence',
63 'UNANSWERED' => '',
64 'UNDELETED' => '',
65 'UNDRAFT' => '',
66 'UNFLAGGED' => '',
67 'UNKEYWORD' => 'akeyword',
68 'UNSEEN' => ''
69 );
70
71 /** Imap SEARCH month names encoding
72 * @global array $imap_asearch_months
73 */
74 $imap_asearch_months = array(
75 '01' => 'jan',
76 '02' => 'feb',
77 '03' => 'mar',
78 '04' => 'apr',
79 '05' => 'may',
80 '06' => 'jun',
81 '07' => 'jul',
82 '08' => 'aug',
83 '09' => 'sep',
84 '10' => 'oct',
85 '11' => 'nov',
86 '12' => 'dec'
87 );
88
89 /** Error message titles according to imap server returned code
90 * @global array $imap_error_titles
91 */
92 $imap_error_titles = array(
93 'OK' => '',
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.")
98 );
99
100 /**
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
109 */
110 //@global array color sm colors array
111 function sqimap_asearch_error_box($response, $query, $message, $link = '')
112 {
113 global $imap_error_titles;
114
115 if (!array_key_exists($response, $imap_error_titles))
116 $title = _("ERROR : Unknown imap response.");
117 else
118 $title = $imap_error_titles[$response];
119 if ($link == '')
120 $message_title = _("Reason Given: ");
121 else
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
126 global $color;
127 require_once(SM_PATH . 'functions/display_messages.php');
128 $string = "<font color=$color[2]><b>\n" . $title . "</b><br>\n";
129 if ($query != '')
130 $string .= _("Query:") . ' ' . htmlspecialchars($query) . '<br>';
131 if ($message_title != '')
132 $string .= $message_title;
133 if ($message != '')
134 $string .= htmlspecialchars($message);
135 if ($link != '')
136 $string .= $link;
137 $string .= "</font><br>\n";
138 error_box($string,$color);
139 }
140 }
141
142 /**
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
146 */
147 function asearch_nz(&$var)
148 {
149 if (isset($var))
150 return $var;
151 return '';
152 }
153
154 /**
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
159 */
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);
168 */
169 }
170
171 /**
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
176 */
177 function s_debug_dump($var_name, $var_var)
178 {
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 ;)
183 else {
184 echo '<pre>';
185 echo htmlentities($var_name);
186 print_r($var_var);
187 echo '</pre>';
188 }
189 }
190 }
191
192 /** Encode a string to quoted or literal as defined in rfc 3501
193 *
194 * - § 4.3 String:
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
202 */
203 function sqimap_asearch_encode_string($what, $charset)
204 {
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
211 }
212
213 /**
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>)
220 * - [1] = day
221 * - [2] = month
222 * - [3] = year
223 */
224 function sqimap_asearch_parse_date($what)
225 {
226 global $imap_asearch_months;
227
228 $what = trim($what);
229 $what = ereg_replace('[ /\\.,]+', '-', $what);
230 if ($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))))
240 ) {
241 $what_parts[2] = $month_number;
242 $what_parts[0] = $what_parts[1] . '-' . $month_code . '-' . $what_parts[3];
243 break;
244 }
245 }
246 /* }*/
247 }
248 }
249 else
250 $what_parts = array();
251 return $what_parts;
252 }
253
254 /**
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
261 */
262 function sqimap_asearch_build_criteria($opcode, $what, $charset)
263 {
264 global $imap_asearch_opcodes;
265
266 $criteria = '';
267 switch ($imap_asearch_opcodes[$opcode]) {
268 default:
269 case 'anum':
270 // $what = str_replace(' ', '', $what);
271 $what = ereg_replace('[^0-9]+', '', $what);
272 if ($what != '')
273 $criteria = $opcode . ' ' . $what . ' ';
274 break;
275 case '': //aflag
276 $criteria = $opcode . ' ';
277 break;
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) . ' ';
284 break;
285 case 'adate':
286 $what_parts = sqimap_asearch_parse_date($what);
287 if (isset($what_parts[0]))
288 $criteria = $opcode . ' ' . $what_parts[0] . ' ';
289 break;
290 case 'akeyword':
291 case 'astring':
292 $criteria = $opcode . ' ' . sqimap_asearch_encode_string($what, $charset) . ' ';
293 break;
294 case 'asequence':
295 $what = ereg_replace('[^0-9:\(\)]+', '', $what);
296 if ($what != '')
297 $criteria = $opcode . ' ' . $what . ' ';
298 break;
299 }
300 return $criteria;
301 }
302
303 /**
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
308 */
309 function sqimap_array_merge_unique(&$to, $from)
310 {
311 if (empty($to))
312 return $from;
313 $count = count($from);
314 for ($i = 0; $i < $count; $i++) {
315 if (!in_array($from[$i], $to))
316 $to[] = $from[$i];
317 }
318 return $to;
319 }
320
321 /**
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
328 */
329 function sqimap_run_search($imapConnection, $search_string, $search_charset)
330 {
331 global $uid_support;
332
333 /* 6.4.4 try OPTIONAL [CHARSET] specification first */
334 if ($search_charset != '')
335 $query = 'SEARCH CHARSET "' . strtoupper($search_charset) . '" ALL ' . $search_string;
336 else
337 $query = 'SEARCH ALL ' . $search_string;
338 s_debug_dump('C:', $query);
339 $readin = sqimap_run_command($imapConnection, $query, false, $response, $message, $uid_support);
340
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);
346 }
347 if (strtoupper($response) != 'OK') {
348 sqimap_asearch_error_box($response, $query, $message);
349 return array();
350 }
351
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)));
358 }
359 }
360
361 if (empty($messagelist)) //Empty search response, ie '* SEARCH'
362 return array();
363
364 $cnt = count($messagelist);
365 for ($q = 0; $q < $cnt; $q++)
366 $id[$q] = trim($messagelist[$q]);
367 return $id;
368 }
369
370 /**
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
378 */
379 function sqimap_run_sort($imapConnection, $search_string, $search_charset, $sort_criteria)
380 {
381 global $uid_support;
382
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);
389
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);
397 }
398
399 if (strtoupper($response) != 'OK') {
400 s_debug_dump('S:', $readin);
401 // sqimap_asearch_error_box($response, $query, $message);
402 // return array();
403 return sqimap_run_search($imapConnection, $search_string, $search_charset); // Fell back to standard search
404 }
405
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)));
412 }
413 }
414
415 if (empty($messagelist)) //Empty search response, ie '* SORT'
416 return array();
417
418 $cnt = count($messagelist);
419 for ($q = 0; $q < $cnt; $q++)
420 $id[$q] = trim($messagelist[$q]);
421 return $id;
422 }
423
424 /**
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
434 */
435 function sqimap_run_thread($imapConnection, $search_string, $search_charset, $thread_algorithm)
436 {
437 global $thread_new, $server_sort_array;
438
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');
443
444 $thread_new = array();
445 $thread_new[0] = "";
446
447 $server_sort_array = array();
448
449 global $uid_support;
450
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);
457
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);
465 }
466
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
474 */
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];
480 else
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);
484 return array();
485 }
486 return sqimap_run_search($imapConnection, $search_string, $search_charset); // Fell back to standard search
487 }
488
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
495 }
496 }
497
498 if (empty($thread_temp)) //Empty search response, ie '* THREAD'
499 return array();
500
501 $char_count = count($thread_temp);
502 $counter = 0;
503 $k = 0;
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];
507 }
508 elseif ($thread_temp[$i] == '(') {
509 $thread_new[$k] .= $thread_temp[$i];
510 $counter++;
511 }
512 elseif ($thread_temp[$i] == ')') {
513 if ($counter > 1) {
514 $thread_new[$k] .= $thread_temp[$i];
515 $counter = $counter - 1;
516 }
517 else {
518 $thread_new[$k] .= $thread_temp[$i];
519 $k++;
520 $thread_new[$k] = "";
521 $counter = $counter - 1;
522 }
523 }
524 }
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');
533 return $thread_list;
534 }
535
536 /**
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 ('')
541 */
542 function sqimap_asearch_get_charset()
543 {
544 global $allow_charset_search, $languages, $squirrelmail_language;
545
546 if ($allow_charset_search)
547 return $languages[$squirrelmail_language]['CHARSET'];
548 return '';
549 }
550
551 /**
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
561 */
562 function sqimap_asearch_get_sort_criteria($mailbox, $sort_by)
563 {
564 global $internal_date_sort, $sent_folder;
565
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];
574 }
575
576 /**
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
580 */
581 function sqimap_asearch_get_sub_mailboxes($cur_mailbox, $mboxes_array)
582 {
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];
588 }
589 return $sub_mboxes_array;
590 }
591
592 /**
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))
611 */
612 function sqimap_asearch($imapConnection, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array, $mboxes_array)
613 {
614 global $allow_server_sort, $sort, $allow_thread_sort, $thread_sort_messages, $sort_by_ref;
615 global $data_dir, $username;
616
617 $search_charset = sqimap_asearch_get_charset();
618 $mbox_msgs = array();
619 $search_string = '';
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);
632 else
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';
641 else
642 $thread_algorithm = 'ORDEREDSUBJECT';
643 $found_msgs = sqimap_run_thread($imapConnection, $search_string, $search_charset, $thread_algorithm);
644 }
645 else
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);
649 }
650 else
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]));
657 }
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]);
662 }
663 $cur_mailbox = $next_mailbox;
664 $search_string = '';
665 }
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];
670 if (!empty($unop))
671 $criteria = $unop . ' ' . $criteria;
672 /* We need to infix the next non-excluded criteria's biop if it's the same mailbox */
673 $next_biop = '';
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]);
678 break;
679 }
680 }
681 if ($next_biop == 'OR')
682 $criteria = $next_biop . ' ' . $criteria;
683 $search_string .= $criteria;
684 $cur_biop = asearch_nz($biop_array[$cur_crit]);
685 }
686 }
687 }
688 }
689 return $mbox_msgs;
690 }
691
692 ?>