fa2b6a95e6fbbcd280991bc12c9e0f6d93c8eb49
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 $msgs, $message_highlight_list, $squirrelmail_language, $languages,
24 $index_order, $pos, $allow_charset_search, $imap_server_type;
25 $pos = $search_position;
27 $urlMailbox = urlencode($mailbox);
29 /* construct the search query, taking multiple search terms into account */
30 $multi_search = array();
31 $search_what = trim($search_what);
32 $search_what = ereg_replace('[ ]{2,}', ' ', $search_what);
33 $multi_search = explode(' ', $search_what);
36 /* it seems macosx does not support the prefered search
37 syntax so we fall back to the older style. This IMAP
38 server has a problem with multiple search terms. Instead
39 of returning the messages that match all the terms it
40 returns the messages that match each term. Could be fixed
41 on the client side, but should be fixed on the server
44 if ($imap_server_type == 'macosx') {
45 foreach ($multi_search as $multi_search_part) {
46 $search_string .= $search_where . ' ' .$multi_search_part. ' ';
50 foreach ($multi_search as $multi_search_part) {
51 $search_string .= $search_where . ' {' . strlen($multi_search_part)
52 . "}\r\n" . $multi_search_part . ' ';
56 $search_string = trim($search_string);
58 /* now use $search_string in the imap search */
59 if ($allow_charset_search && isset($languages[$squirrelmail_language]['CHARSET']) &&
60 $languages[$squirrelmail_language]['CHARSET']) {
61 $ss = "SEARCH CHARSET "
62 . strtoupper($languages[$squirrelmail_language]['CHARSET'])
63 . " ALL $search_string";
65 $ss = "SEARCH ALL $search_string";
68 /* read data back from IMAP */
69 $readin = sqimap_run_command($imapConnection, $ss, false, $result, $message);
71 /* try US-ASCII charset if search fails */
72 if (isset($languages[$squirrelmail_language]['CHARSET'])
73 && strtolower($result) == 'no') {
74 $ss = "SEARCH CHARSET \"US-ASCII\" ALL $search_string";
75 $readin = sqimap_run_command ($imapConnection, $ss, true,
82 /* Keep going till we find the SEARCH response */
83 foreach ($readin as $readin_part) {
84 /* Check to see if a SEARCH response was received */
85 if (substr($readin_part, 0, 9) == '* SEARCH ') {
86 $messagelist = explode(' ', substr($readin_part, 9));
87 } else if (isset($errors)) {
88 $errors = $errors.$readin_part;
90 $errors = $readin_part;
94 /* If nothing is found * SEARCH should be the first error else echo errors */
96 if (strstr($errors,'* SEARCH')) {
97 if ($search_all != 'all') {
98 echo '<br><CENTER>' . _("No Messages Found") . '</CENTER>';
102 echo "<!-- $errors -->";
106 * HACKED CODE FROM ANOTHER FUNCTION, could probably dump this and modify
107 * existing code with a search true/false variable.
111 for ($q = 0; $q < count($messagelist); $q++
) {
112 $id[$q] = trim($messagelist[$q]);
114 $issent = ($mailbox == $sent_folder);
115 $hdr_list = sqimap_get_small_header_list($imapConnection, $id, $issent);
116 // $flags = sqimap_get_flags_list($imapConnection, $id, $issent);
118 foreach ($hdr_list as $hdr) {
119 $from[] = $hdr->from
;
120 $date[] = $hdr->date
;
121 $subject[] = $hdr->subject
;
123 $priority[] = $hdr->priority
;
125 $size[] = $hdr->size
;
126 $type[] = $hdr->type0
;
127 $flag_deleted[] = $hdr->flag_deleted
;
128 $flag_answered[] = $hdr->flag_answered
;
129 $flag_seen[] = $hdr->flag_seen
;
130 $flag_flagged[] = $hdr->flag_flagged
;
134 while ($j < count($messagelist)) {
135 $date[$j] = str_replace(' ', ' ', $date[$j]);
136 $tmpdate = explode(' ', trim($date[$j]));
138 $messages[$j]["TIME_STAMP"] = getTimeStamp($tmpdate);
139 $messages[$j]["DATE_STRING"] = getDateString($messages[$j]["TIME_STAMP"]);
140 $messages[$j]["ID"] = $id[$j];
141 $messages[$j]["FROM"] = decodeHeader($from[$j]);
142 $messages[$j]["FROM-SORT"] = strtolower(sqimap_find_displayable_name(decodeHeader($from[$j])));
143 $messages[$j]["SUBJECT"] = decodeHeader($subject[$j]);
144 $messages[$j]["SUBJECT-SORT"] = strtolower(decodeHeader($subject[$j]));
145 $messages[$j]["TO"] = decodeHeader($to[$j]);
146 $messages[$j]["PRIORITY"] = $priority[$j];
147 $messages[$j]["CC"] = $cc[$j];
148 $messages[$j]["SIZE"] = $size[$j];
149 $messages[$j]["TYPE0"] = $type[$j];
150 $messages[$j]['FLAG_DELETED'] = $flag_deleted[$j];
151 $messages[$j]['FLAG_ANSWERED'] = $flag_answered[$j];
152 $messages[$j]['FLAG_SEEN'] = $flag_seen[$j];
153 $messages[$j]['FLAG_FLAGGED'] = $flag_flagged[$j];
156 while ($num < count($flags[$j])) {
157 if ($flags[$j][$num] == 'Deleted') {
158 $messages[$j]['FLAG_DELETED'] = true;
159 } else if ($flags[$j][$num] == 'Answered') {
160 $messages[$j]['FLAG_ANSWERED'] = true;
161 } else if ($flags[$j][$num] == 'Seen') {
162 $messages[$j]['FLAG_SEEN'] = true;
163 } else if ($flags[$j][$num] == 'Flagged') {
164 $messages[$j]['FLAG_FLAGGED'] = true;
173 /* Find and remove the ones that are deleted */
176 while ($j < count($messagelist)) {
177 if (isset($messages[$j]['FLAG_DELETED']) && $messages[$j]['FLAG_DELETED']) {
181 $msgs[$i] = $messages[$j];
188 /* There's gotta be messages in the array for it to sort them. */
190 if (count($messagelist) > 0) {
195 if ($search_all != 'all') {
196 if ( !isset( $start_msg ) ) {
199 if ( !isset( $sort ) ) {
202 mail_message_listing_beginning( $imapConnection,
203 "move_messages.php?msg=$msg&mailbox=$urlMailbox&pos=$pos&where=" . urlencode($search_where) . "&what=".urlencode($search_what),
206 '<b>' . _("Found") . ' ' . count($messagelist) . ' ' . _("messages") . '</b></tr><tr>'.
207 get_selectall_link($start_msg, $sort));
210 mail_message_listing_beginning( $imapConnection,
211 "move_messages.php?msg=$msg&mailbox=$urlMailbox&pos=$pos&where=" . urlencode($search_where) . "&what=".urlencode($search_what),
214 '<b>' . _("Found") . ' ' . count($messagelist) . ' ' . _("messages") . '</b></tr><tr>');
216 if ( $mailbox == 'INBOX' ) {
217 $showbox = _("INBOX");
221 echo '<b><big>' . _("Folder:") . " $showbox</big></b>";
222 while ($j < count($msgs)) {
223 printMessageInfo($imapConnection, $msgs[$j]["ID"], 0, $j, $mailbox, '', 0, $search_where, $search_what);
227 echo '</table></td></tr></table></form>';
228 $count_all = count($msgs);