added a macosx friendly search function.
[squirrelmail.git] / functions / imap_search.php
1 <?php
2
3 /**
4 * imap_search.php
5 *
6 * Copyright (c) 1999-2002 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * IMAP search routines
10 *
11 * $Id$
12 */
13
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');
19
20 function sqimap_search($imapConnection, $search_where, $search_what, $mailbox,
21 $color, $search_position = '', $search_all, $count_all) {
22
23 global $msgs, $message_highlight_list, $squirrelmail_language, $languages,
24 $index_order, $pos, $allow_charset_search, $imap_server_type;
25 $pos = $search_position;
26
27 $urlMailbox = urlencode($mailbox);
28
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);
34 $search_string = '';
35
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
42 as per the RFC */
43
44 if ($imap_server_type == 'macosx') {
45 foreach ($multi_search as $multi_search_part) {
46 $search_string .= $search_where . ' ' .$multi_search_part. ' ';
47 }
48 }
49 else {
50 foreach ($multi_search as $multi_search_part) {
51 $search_string .= $search_where . ' {' . strlen($multi_search_part)
52 . "}\r\n" . $multi_search_part . ' ';
53 }
54 }
55
56 $search_string = trim($search_string);
57
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";
64 } else {
65 $ss = "SEARCH ALL $search_string";
66 }
67
68 /* read data back from IMAP */
69 $readin = sqimap_run_command($imapConnection, $ss, false, $result, $message);
70
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,
76 $result, $message);
77 }
78
79 unset($messagelist);
80 $msgs = '';
81
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;
89 } else {
90 $errors = $readin_part;
91 }
92 }
93
94 /* If nothing is found * SEARCH should be the first error else echo errors */
95 if (isset($errors)) {
96 if (strstr($errors,'* SEARCH')) {
97 if ($search_all != 'all') {
98 echo '<br><CENTER>' . _("No Messages Found") . '</CENTER>';
99 }
100 return;
101 }
102 echo "<!-- $errors -->";
103 }
104
105 /*
106 * HACKED CODE FROM ANOTHER FUNCTION, could probably dump this and modify
107 * existing code with a search true/false variable.
108 */
109
110 global $sent_folder;
111 for ($q = 0; $q < count($messagelist); $q++) {
112 $id[$q] = trim($messagelist[$q]);
113 }
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);
117
118 foreach ($hdr_list as $hdr) {
119 $from[] = $hdr->from;
120 $date[] = $hdr->date;
121 $subject[] = $hdr->subject;
122 $to[] = $hdr->to;
123 $priority[] = $hdr->priority;
124 $cc[] = $hdr->cc;
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;
131 }
132
133 $j = 0;
134 while ($j < count($messagelist)) {
135 $date[$j] = str_replace(' ', ' ', $date[$j]);
136 $tmpdate = explode(' ', trim($date[$j]));
137
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];
154 /*
155 $num = 0;
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;
165 }
166 $num++;
167 }
168 */
169 $j++;
170
171 }
172
173 /* Find and remove the ones that are deleted */
174 $i = 0;
175 $j = 0;
176 while ($j < count($messagelist)) {
177 if (isset($messages[$j]['FLAG_DELETED']) && $messages[$j]['FLAG_DELETED']) {
178 $j++;
179 continue;
180 }
181 $msgs[$i] = $messages[$j];
182
183 $i++;
184 $j++;
185 }
186 $numMessages = $i;
187
188 /* There's gotta be messages in the array for it to sort them. */
189
190 if (count($messagelist) > 0) {
191 $j=0;
192 if (!isset ($msg)) {
193 $msg = '';
194 }
195 if ($search_all != 'all') {
196 if ( !isset( $start_msg ) ) {
197 $start_msg =0;
198 }
199 if ( !isset( $sort ) ) {
200 $sort = 0;
201 }
202 mail_message_listing_beginning( $imapConnection,
203 "move_messages.php?msg=$msg&mailbox=$urlMailbox&pos=$pos&where=" . urlencode($search_where) . "&what=".urlencode($search_what),
204 $mailbox,
205 -1,
206 '<b>' . _("Found") . ' ' . count($messagelist) . ' ' . _("messages") . '</b></tr><tr>'.
207 get_selectall_link($start_msg, $sort));
208 }
209 else {
210 mail_message_listing_beginning( $imapConnection,
211 "move_messages.php?msg=$msg&mailbox=$urlMailbox&pos=$pos&where=" . urlencode($search_where) . "&what=".urlencode($search_what),
212 $mailbox,
213 -1,
214 '<b>' . _("Found") . ' ' . count($messagelist) . ' ' . _("messages") . '</b></tr><tr>');
215 }
216 if ( $mailbox == 'INBOX' ) {
217 $showbox = _("INBOX");
218 } else {
219 $showbox = $mailbox;
220 }
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);
224 $j++;
225 echo '</td></tr>';
226 }
227 echo '</table></td></tr></table></form>';
228 $count_all = count($msgs);
229 }
230 return $count_all;
231 }
232
233 ?>