Handle multiple SORT responses, and PhpDocumentor homework ;)
[squirrelmail.git] / functions / imap_asearch.php
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
10 * @author Alex Lemaresquier - Brainstorm - alex at brainstorm.fr
11 * @package squirrelmail
12 * @see search.php
13 *
14 */
15
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');
19
20 /** Set to TRUE to dump the imap dialogue
21 * @global bool $imap_asearch_debug_dump
22 */
23 $imap_asearch_debug_dump = FALSE;
24
25 /** Array of imap SEARCH opcodes
26 * @global array $imap_asearch_opcodes
27 */
28 $imap_asearch_opcodes = array(
29 /* <message set> => 'asequence', */
30 /*'ALL' is binary operator */
31 'ANSWERED' => '',
32 'BCC' => 'astring',
33 'BEFORE' => 'adate',
34 'BODY' => 'astring',
35 'CC' => 'astring',
36 'DELETED' => '',
37 'DRAFT' => '',
38 'FLAGGED' => '',
39 'FROM' => 'astring',
40 'HEADER' => 'afield', /* Special syntax for this one, see below */
41 'KEYWORD' => 'akeyword',
42 'LARGER' => 'anum',
43 'NEW' => '',
44 /*'NOT' is unary operator */
45 'OLD' => '',
46 'ON' => 'adate',
47 /*'OR' is binary operator */
48 'RECENT' => '',
49 'SEEN' => '',
50 'SENTBEFORE' => 'adate',
51 'SENTON' => 'adate',
52 'SENTSINCE' => 'adate',
53 'SINCE' => 'adate',
54 'SMALLER' => 'anum',
55 'SUBJECT' => 'astring',
56 'TEXT' => 'astring',
57 'TO' => 'astring',
58 'UID' => 'asequence',
59 'UNANSWERED' => '',
60 'UNDELETED' => '',
61 'UNDRAFT' => '',
62 'UNFLAGGED' => '',
63 'UNKEYWORD' => 'akeyword',
64 'UNSEEN' => ''
65 );
66
67 /** Imap SEARCH month names encoding
68 * @global array $imap_asearch_months
69 */
70 $imap_asearch_months = array(
71 '01' => 'jan',
72 '02' => 'feb',
73 '03' => 'mar',
74 '04' => 'apr',
75 '05' => 'may',
76 '06' => 'jun',
77 '07' => 'jul',
78 '08' => 'aug',
79 '09' => 'sep',
80 '10' => 'oct',
81 '11' => 'nov',
82 '12' => 'dec'
83 );
84
85 /** Error message titles according to imap server returned code
86 * @global array $imap_error_titles
87 */
88 $imap_error_titles = array(
89 'OK' => '',
90 'NO' => _("ERROR : Could not complete request."),
91 'BAD' => _("ERROR : Bad or malformed request."),
92 'BYE' => _("ERROR : Imap server closed the connection.")
93 );
94
95 /**
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
103 */
104 //@global array color sm colors array
105 function sqimap_asearch_error_box($response, $query, $message)
106 {
107 global $imap_error_titles;
108
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.");
112 else
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
118 global $color;
119 require_once(SM_PATH . 'functions/display_messages.php');
120 $string = "<font color=$color[2]><b>\n" . $title . "</b><br>\n";
121 if ($query != '')
122 $string .= _("Query:") . ' ' . htmlspecialchars($query) . '<br>';
123 if ($message_title != '')
124 $string .= $message_title;
125 if ($message != '')
126 $string .= htmlspecialchars($message);
127 $string .= "</font><br>\n";
128 error_box($string,$color);
129 }
130 }
131
132 /**
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
136 */
137 function asearch_nz(&$var)
138 {
139 if (isset($var))
140 return $var;
141 return '';
142 }
143
144 /**
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
149 */
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);
158 */
159 }
160
161 /**
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
166 */
167 function s_debug_dump($var_name, $var_var)
168 {
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 ;)
173 else {
174 echo '<pre>';
175 echo htmlentities($var_name);
176 print_r($var_var);
177 echo '</pre>';
178 }
179 }
180 }
181
182 /** Encode a string to quoted or literal as defined in rfc 3501
183 *
184 * - § 4.3 String:
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
192 */
193 function sqimap_asearch_encode_string($what, $charset)
194 {
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
201 }
202
203 /**
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>)
210 * - [1] = day
211 * - [2] = month
212 * - [3] = year
213 */
214 function sqimap_asearch_parse_date($what)
215 {
216 global $imap_asearch_months;
217
218 $what = trim($what);
219 $what = ereg_replace('[ /\\.,]+', '-', $what);
220 if ($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))))
230 ) {
231 $what_parts[2] = $month_number;
232 $what_parts[0] = $what_parts[1] . '-' . $month_code . '-' . $what_parts[3];
233 break;
234 }
235 }
236 /* }*/
237 }
238 }
239 else
240 $what_parts = array();
241 return $what_parts;
242 }
243
244 /**
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
251 */
252 function sqimap_asearch_build_criteria($opcode, $what, $charset)
253 {
254 global $imap_asearch_opcodes;
255
256 $criteria = '';
257 switch ($imap_asearch_opcodes[$opcode]) {
258 default:
259 case 'anum':
260 // $what = str_replace(' ', '', $what);
261 $what = ereg_replace('[^0-9]+', '', $what);
262 if ($what != '')
263 $criteria = $opcode . ' ' . $what . ' ';
264 break;
265 case '': //aflag
266 $criteria = $opcode . ' ';
267 break;
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) . ' ';
274 break;
275 case 'adate':
276 $what_parts = sqimap_asearch_parse_date($what);
277 if (isset($what_parts[0]))
278 $criteria = $opcode . ' ' . $what_parts[0] . ' ';
279 break;
280 case 'akeyword':
281 case 'astring':
282 $criteria = $opcode . ' ' . sqimap_asearch_encode_string($what, $charset) . ' ';
283 break;
284 case 'asequence':
285 $what = ereg_replace('[^0-9:\(\)]+', '', $what);
286 if ($what != '')
287 $criteria = $opcode . ' ' . $what . ' ';
288 break;
289 }
290 return $criteria;
291 }
292
293 /**
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
298 */
299 function sqimap_array_merge_unique($to, $from)
300 {
301 if (empty($to))
302 return $from;
303 $count = count($from);
304 for ($i = 0; $i < $count; $i++) {
305 if (!in_array($from[$i], $to))
306 $to[] = $from[$i];
307 }
308 return $to;
309 }
310
311 /**
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
318 */
319 function sqimap_run_search($imapConnection, $search_string, $search_charset)
320 {
321 global $uid_support;
322
323 /* 6.4.4 try OPTIONAL [CHARSET] specification first */
324 if ($search_charset != '')
325 $query = 'SEARCH CHARSET "' . strtoupper($search_charset) . '" ALL ' . $search_string;
326 else
327 $query = 'SEARCH ALL ' . $search_string;
328 s_debug_dump('C:', $query);
329 $readin = sqimap_run_command($imapConnection, $query, false, $response, $message, $uid_support);
330
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);
336 }
337 if (strtoupper($response) != 'OK') {
338 sqimap_asearch_error_box($response, $query, $message);
339 return array();
340 }
341
342 unset($messagelist);
343
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)));
350 }
351 }
352
353 if (empty($messagelist)) //Empty search response, ie '* SEARCH'
354 return array();
355
356 $cnt = count($messagelist);
357 for ($q = 0; $q < $cnt; $q++)
358 $id[$q] = trim($messagelist[$q]);
359 return $id;
360 }
361
362 /**
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
370 */
371 function sqimap_run_sort($imapConnection, $search_string, $search_charset, $sort_criteria)
372 {
373 global $uid_support;
374
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);
380
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);
386 }
387
388 if (strtoupper($response) != 'OK') {
389 // sqimap_asearch_error_box($response, $query, $message);
390 // return array();
391 return sqimap_run_search($imapConnection, $search_string, $search_charset); // Fell back to standard search
392 }
393
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)));
400 }
401 }
402
403 if (empty($messagelist)) //Empty search response, ie '* SORT'
404 return array();
405
406 $cnt = count($messagelist);
407 for ($q = 0; $q < $cnt; $q++)
408 $id[$q] = trim($messagelist[$q]);
409 return $id;
410 }
411
412 /**
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
422 */
423 function sqimap_run_thread($imapConnection, $search_string, $search_charset, $thread_algorithm)
424 {
425 global $thread_new, $server_sort_array;
426
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');
431
432 $thread_new = array();
433 $thread_new[0] = "";
434
435 $server_sort_array = array();
436
437 global $uid_support;
438
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);
444
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);
450 }
451
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
457 */
458 // sqimap_asearch_error_box($response, $query, $message);
459 // return array();
460 return sqimap_run_search($imapConnection, $search_string, $search_charset); // Fell back to standard search
461 }
462
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
469 }
470 }
471
472 if (empty($thread_temp)) //Empty search response, ie '* THREAD'
473 return array();
474
475 $char_count = count($thread_temp);
476 $counter = 0;
477 $k = 0;
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];
481 }
482 elseif ($thread_temp[$i] == '(') {
483 $thread_new[$k] .= $thread_temp[$i];
484 $counter++;
485 }
486 elseif ($thread_temp[$i] == ')') {
487 if ($counter > 1) {
488 $thread_new[$k] .= $thread_temp[$i];
489 $counter = $counter - 1;
490 }
491 else {
492 $thread_new[$k] .= $thread_temp[$i];
493 $k++;
494 $thread_new[$k] = "";
495 $counter = $counter - 1;
496 }
497 }
498 }
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');
507 return $thread_list;
508 }
509
510 /**
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 ('')
515 */
516 function sqimap_asearch_get_charset()
517 {
518 global $allow_charset_search, $languages, $squirrelmail_language;
519
520 if ($allow_charset_search)
521 return $languages[$squirrelmail_language]['CHARSET'];
522 return '';
523 }
524
525 /**
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
533 */
534 function sqimap_asearch_get_sort_criteria($mailbox, $sort_by)
535 {
536 global $internal_date_sort, $sent_folder;
537
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];
546 }
547
548 /**
549 * Performs the search, given all the criteria, merging results for every mailbox
550 * @return array array(mailbox => array(UIDs))
551 */
552 function sqimap_asearch($imapConnection, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $mboxes_array)
553 {
554 global $allow_server_sort, $sort, $allow_thread_sort, $thread_sort_messages;
555 global $data_dir, $username;
556
557 $search_charset = sqimap_asearch_get_charset();
558 $mbox_msgs = array();
559 $search_string = '';
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;
570 else
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);
579 }
580 else
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);
584 }
585 else
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]));
592 }
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]);
597 }
598 $cur_mailbox = $next_mailbox;
599 $search_string = '';
600 }
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];
605 if (!empty($unop))
606 $criteria = $unop . ' ' . $criteria;
607 /* We need to infix the next non-excluded criteria's biop if it's the same mailbox */
608 $next_biop = '';
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]);
613 break;
614 }
615 }
616 if ($next_biop == 'OR')
617 $criteria = $next_biop . ' ' . $criteria;
618 $search_string .= $criteria;
619 $cur_biop = asearch_nz($biop_array[$cur_crit]);
620 }
621 }
622 }
623 }
624 return $mbox_msgs;
625 }
626
627 ?>