6 * Copyright (c) 1999-2002 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
14 require_once('../functions/imap.php');
15 require_once('../functions/date.php');
16 require_once('../functions/array.php');
17 require_once('../functions/mailbox_display.php');
18 require_once('../functions/mime.php');
20 function sqimap_search($imapConnection, $search_where, $search_what, $mailbox,
21 $color, $search_position = '', $search_all, $count_all) {
23 global $message_highlight_list, $squirrelmail_language, $languages,
24 $index_order, $pos, $allow_charset_search, $uid_support;
26 $pos = $search_position;
28 $urlMailbox = urlencode($mailbox);
30 /* construct the search query, taking multiple search terms into account */
31 $multi_search = array();
32 $search_what = trim($search_what);
33 $search_what = ereg_replace('[ ]{2,}', ' ', $search_what);
34 $multi_search = explode(' ', $search_what);
36 foreach ($multi_search as $multi_search_part) {
37 $search_string .= $search_where . ' {' . strlen($multi_search_part)
38 . "}\r\n" . $multi_search_part . ' ';
41 $search_string = trim($search_string);
43 /* now use $search_string in the imap search */
44 if ($allow_charset_search && isset($languages[$squirrelmail_language]['CHARSET']) &&
45 $languages[$squirrelmail_language]['CHARSET']) {
46 $ss = "SEARCH CHARSET "
47 . strtoupper($languages[$squirrelmail_language]['CHARSET'])
48 . " ALL $search_string";
50 $ss = "SEARCH ALL $search_string";
53 /* read data back from IMAP */
54 $readin = sqimap_run_command($imapConnection, $ss, false, $result, $message, $uid_support);
56 /* try US-ASCII charset if search fails */
57 if (isset($languages[$squirrelmail_language]['CHARSET'])
58 && strtolower($result) == 'no') {
59 $ss = "SEARCH CHARSET \"US-ASCII\" ALL $search_string";
60 $readin = sqimap_run_command ($imapConnection, $ss, true,
66 /* Keep going till we find the SEARCH response */
67 foreach ($readin as $readin_part) {
68 /* Check to see if a SEARCH response was received */
69 if (substr($readin_part, 0, 9) == '* SEARCH ') {
70 $messagelist = preg_split("/ /", substr($readin_part, 9));
71 } else if (isset($errors)) {
72 $errors = $errors.$readin_part;
74 $errors = $readin_part;
78 /* If nothing is found * SEARCH should be the first error else echo errors */
80 if (strstr($errors,'* SEARCH')) {
81 if ($search_all != 'all') {
82 echo '<br><CENTER>' . _("No Messages Found") . '</CENTER>';
86 echo "<!-- $errors -->";
92 $cnt = count($messagelist);
93 for ($q = 0; $q < $cnt; $q++
) {
94 $id[$q] = trim($messagelist[$q]);
96 $issent = ($mailbox == $sent_folder);
98 $msgs = fillMessageArray($imapConnection,$id,$issent,$cnt);