6e189ce2 |
1 | <?php |
2ba13803 |
2 | |
35586184 |
3 | /** |
15e6162e |
4 | * imap_search.php |
35586184 |
5 | * |
15e6162e |
6 | * Copyright (c) 1999-2002 The SquirrelMail Project Team |
7 | * Licensed under the GNU GPL. For full terms see the file COPYING. |
35586184 |
8 | * |
15e6162e |
9 | * IMAP search routines |
35586184 |
10 | * |
15e6162e |
11 | * $Id$ |
35586184 |
12 | */ |
13 | |
35586184 |
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'); |
2ba13803 |
19 | |
b1e29794 |
20 | function sqimap_search($imapConnection, $search_where, $search_what, $mailbox, |
21 | $color, $search_position = '', $search_all, $count_all) { |
2ba13803 |
22 | |
b1e29794 |
23 | global $msgs, $message_highlight_list, $squirrelmail_language, $languages, |
5c9a316d |
24 | $index_order, $pos, $allow_charset_search, $imap_server_type; |
53828ec5 |
25 | $pos = $search_position; |
2ba13803 |
26 | |
29eb5486 |
27 | $urlMailbox = urlencode($mailbox); |
2ba13803 |
28 | |
b1e29794 |
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 = ''; |
484a2e6a |
35 | |
5c9a316d |
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 | |
70c4fd84 |
56 | $search_string = trim($search_string); |
c92ef720 |
57 | |
b1e29794 |
58 | /* now use $search_string in the imap search */ |
65ffb3ce |
59 | if ($allow_charset_search && isset($languages[$squirrelmail_language]['CHARSET']) && |
29eb5486 |
60 | $languages[$squirrelmail_language]['CHARSET']) { |
8ceffac2 |
61 | $ss = "SEARCH CHARSET " |
62 | . strtoupper($languages[$squirrelmail_language]['CHARSET']) |
63 | . " ALL $search_string"; |
29eb5486 |
64 | } else { |
b1e29794 |
65 | $ss = "SEARCH ALL $search_string"; |
29eb5486 |
66 | } |
b1e29794 |
67 | |
68 | /* read data back from IMAP */ |
632a9a19 |
69 | $readin = sqimap_run_command($imapConnection, $ss, false, $result, $message); |
b1e29794 |
70 | |
71 | /* try US-ASCII charset if search fails */ |
8ceffac2 |
72 | if (isset($languages[$squirrelmail_language]['CHARSET']) |
73 | && strtolower($result) == 'no') { |
fee5ad96 |
74 | $ss = "SEARCH CHARSET \"US-ASCII\" ALL $search_string"; |
8ceffac2 |
75 | $readin = sqimap_run_command ($imapConnection, $ss, true, |
76 | $result, $message); |
29eb5486 |
77 | } |
2ba13803 |
78 | |
70c4fd84 |
79 | unset($messagelist); |
80 | $msgs = ''; |
b1e29794 |
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 | } |
29eb5486 |
92 | } |
2ba13803 |
93 | |
29eb5486 |
94 | /* If nothing is found * SEARCH should be the first error else echo errors */ |
b1e29794 |
95 | if (isset($errors)) { |
96 | if (strstr($errors,'* SEARCH')) { |
97 | if ($search_all != 'all') { |
98 | echo '<br><CENTER>' . _("No Messages Found") . '</CENTER>'; |
99 | } |
70c4fd84 |
100 | return; |
101 | } |
b1e29794 |
102 | echo "<!-- $errors -->"; |
29eb5486 |
103 | } |
2ba13803 |
104 | |
29eb5486 |
105 | /* |
b1e29794 |
106 | * HACKED CODE FROM ANOTHER FUNCTION, could probably dump this and modify |
107 | * existing code with a search true/false variable. |
108 | */ |
2ba13803 |
109 | |
29eb5486 |
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); |
f6b58812 |
116 | // $flags = sqimap_get_flags_list($imapConnection, $id, $issent); |
117 | |
29eb5486 |
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; |
f6b58812 |
127 | $flag_deleted[] = $hdr->flag_deleted; |
128 | $flag_answered[] = $hdr->flag_answered; |
129 | $flag_seen[] = $hdr->flag_seen; |
130 | $flag_flagged[] = $hdr->flag_flagged; |
29eb5486 |
131 | } |
2ba13803 |
132 | |
29eb5486 |
133 | $j = 0; |
134 | while ($j < count($messagelist)) { |
0f8a1ce9 |
135 | $date[$j] = str_replace(' ', ' ', $date[$j]); |
b1e29794 |
136 | $tmpdate = explode(' ', trim($date[$j])); |
29eb5486 |
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]; |
f6b58812 |
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 | /* |
29eb5486 |
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; |
2ba13803 |
165 | } |
29eb5486 |
166 | $num++; |
fc05541b |
167 | } |
f6b58812 |
168 | */ |
29eb5486 |
169 | $j++; |
f6b58812 |
170 | |
29eb5486 |
171 | } |
2ba13803 |
172 | |
29eb5486 |
173 | /* Find and remove the ones that are deleted */ |
174 | $i = 0; |
175 | $j = 0; |
176 | while ($j < count($messagelist)) { |
70c4fd84 |
177 | if (isset($messages[$j]['FLAG_DELETED']) && $messages[$j]['FLAG_DELETED']) { |
1809bad8 |
178 | $j++; |
29eb5486 |
179 | continue; |
2ba13803 |
180 | } |
29eb5486 |
181 | $msgs[$i] = $messages[$j]; |
2ba13803 |
182 | |
29eb5486 |
183 | $i++; |
184 | $j++; |
185 | } |
186 | $numMessages = $i; |
2ba13803 |
187 | |
29eb5486 |
188 | /* There's gotta be messages in the array for it to sort them. */ |
2ba13803 |
189 | |
29eb5486 |
190 | if (count($messagelist) > 0) { |
191 | $j=0; |
70c4fd84 |
192 | if (!isset ($msg)) { |
193 | $msg = ''; |
53828ec5 |
194 | } |
8d93176b |
195 | if ($search_all != 'all') { |
70c4fd84 |
196 | if ( !isset( $start_msg ) ) { |
197 | $start_msg =0; |
198 | } |
199 | if ( !isset( $sort ) ) { |
200 | $sort = 0; |
201 | } |
8d93176b |
202 | mail_message_listing_beginning( $imapConnection, |
203 | "move_messages.php?msg=$msg&mailbox=$urlMailbox&pos=$pos&where=" . urlencode($search_where) . "&what=".urlencode($search_what), |
b1e29794 |
204 | $mailbox, |
8d93176b |
205 | -1, |
206 | '<b>' . _("Found") . ' ' . count($messagelist) . ' ' . _("messages") . '</b></tr><tr>'. |
b1e29794 |
207 | get_selectall_link($start_msg, $sort)); |
8d93176b |
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), |
b1e29794 |
212 | $mailbox, |
8d93176b |
213 | -1, |
214 | '<b>' . _("Found") . ' ' . count($messagelist) . ' ' . _("messages") . '</b></tr><tr>'); |
215 | } |
f8a2d9ee |
216 | if ( $mailbox == 'INBOX' ) { |
217 | $showbox = _("INBOX"); |
218 | } else { |
219 | $showbox = $mailbox; |
220 | } |
221 | echo '<b><big>' . _("Folder:") . " $showbox</big></b>"; |
29eb5486 |
222 | while ($j < count($msgs)) { |
223 | printMessageInfo($imapConnection, $msgs[$j]["ID"], 0, $j, $mailbox, '', 0, $search_where, $search_what); |
224 | $j++; |
70c4fd84 |
225 | echo '</td></tr>'; |
2ba13803 |
226 | } |
7c0060c9 |
227 | echo '</table></td></tr></table></form>'; |
70c4fd84 |
228 | $count_all = count($msgs); |
2ba13803 |
229 | } |
70c4fd84 |
230 | return $count_all; |
29eb5486 |
231 | } |
fc05541b |
232 | |
1c72b151 |
233 | ?> |