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, |
24 | $index_order, $pos; |
53828ec5 |
25 | |
26 | $pos = $search_position; |
2ba13803 |
27 | |
29eb5486 |
28 | $urlMailbox = urlencode($mailbox); |
2ba13803 |
29 | |
b1e29794 |
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); |
35 | $search_string = ''; |
36 | foreach ($multi_search as $multi_search_part) { |
484a2e6a |
37 | $search_string .= $search_where . ' {' . strlen($multi_search_part) |
38 | . "}\r\n" . $multi_search_part . ' '; |
b1e29794 |
39 | } |
484a2e6a |
40 | |
70c4fd84 |
41 | $search_string = trim($search_string); |
c92ef720 |
42 | |
b1e29794 |
43 | /* now use $search_string in the imap search */ |
29eb5486 |
44 | if (isset($languages[$squirrelmail_language]['CHARSET']) && |
45 | $languages[$squirrelmail_language]['CHARSET']) { |
8ceffac2 |
46 | $ss = "SEARCH CHARSET " |
47 | . strtoupper($languages[$squirrelmail_language]['CHARSET']) |
48 | . " ALL $search_string"; |
29eb5486 |
49 | } else { |
b1e29794 |
50 | $ss = "SEARCH ALL $search_string"; |
29eb5486 |
51 | } |
b1e29794 |
52 | |
53 | /* read data back from IMAP */ |
54 | $readin = sqimap_run_command($imapConnection, $ss, true, $result, $message); |
55 | |
56 | /* try US-ASCII charset if search fails */ |
8ceffac2 |
57 | if (isset($languages[$squirrelmail_language]['CHARSET']) |
58 | && strtolower($result) == 'no') { |
fee5ad96 |
59 | $ss = "SEARCH CHARSET \"US-ASCII\" ALL $search_string"; |
8ceffac2 |
60 | $readin = sqimap_run_command ($imapConnection, $ss, true, |
61 | $result, $message); |
29eb5486 |
62 | } |
2ba13803 |
63 | |
70c4fd84 |
64 | unset($messagelist); |
65 | $msgs = ''; |
b1e29794 |
66 | |
67 | /* Keep going till we find the SEARCH response */ |
68 | foreach ($readin as $readin_part) { |
69 | /* Check to see if a SEARCH response was received */ |
70 | if (substr($readin_part, 0, 9) == '* SEARCH ') { |
71 | $messagelist = explode(' ', substr($readin_part, 9)); |
72 | } else if (isset($errors)) { |
73 | $errors = $errors.$readin_part; |
74 | } else { |
75 | $errors = $readin_part; |
76 | } |
29eb5486 |
77 | } |
2ba13803 |
78 | |
29eb5486 |
79 | /* If nothing is found * SEARCH should be the first error else echo errors */ |
b1e29794 |
80 | if (isset($errors)) { |
81 | if (strstr($errors,'* SEARCH')) { |
82 | if ($search_all != 'all') { |
83 | echo '<br><CENTER>' . _("No Messages Found") . '</CENTER>'; |
84 | } |
70c4fd84 |
85 | return; |
86 | } |
b1e29794 |
87 | echo "<!-- $errors -->"; |
29eb5486 |
88 | } |
2ba13803 |
89 | |
29eb5486 |
90 | /* |
b1e29794 |
91 | * HACKED CODE FROM ANOTHER FUNCTION, could probably dump this and modify |
92 | * existing code with a search true/false variable. |
93 | */ |
2ba13803 |
94 | |
29eb5486 |
95 | global $sent_folder; |
96 | for ($q = 0; $q < count($messagelist); $q++) { |
97 | $id[$q] = trim($messagelist[$q]); |
98 | } |
99 | $issent = ($mailbox == $sent_folder); |
100 | $hdr_list = sqimap_get_small_header_list($imapConnection, $id, $issent); |
101 | $flags = sqimap_get_flags_list($imapConnection, $id, $issent); |
102 | foreach ($hdr_list as $hdr) { |
103 | $from[] = $hdr->from; |
104 | $date[] = $hdr->date; |
105 | $subject[] = $hdr->subject; |
106 | $to[] = $hdr->to; |
107 | $priority[] = $hdr->priority; |
108 | $cc[] = $hdr->cc; |
109 | $size[] = $hdr->size; |
110 | $type[] = $hdr->type0; |
111 | } |
2ba13803 |
112 | |
29eb5486 |
113 | $j = 0; |
114 | while ($j < count($messagelist)) { |
0f8a1ce9 |
115 | $date[$j] = str_replace(' ', ' ', $date[$j]); |
b1e29794 |
116 | $tmpdate = explode(' ', trim($date[$j])); |
29eb5486 |
117 | |
118 | $messages[$j]["TIME_STAMP"] = getTimeStamp($tmpdate); |
119 | $messages[$j]["DATE_STRING"] = getDateString($messages[$j]["TIME_STAMP"]); |
120 | $messages[$j]["ID"] = $id[$j]; |
121 | $messages[$j]["FROM"] = decodeHeader($from[$j]); |
122 | $messages[$j]["FROM-SORT"] = strtolower(sqimap_find_displayable_name(decodeHeader($from[$j]))); |
123 | $messages[$j]["SUBJECT"] = decodeHeader($subject[$j]); |
124 | $messages[$j]["SUBJECT-SORT"] = strtolower(decodeHeader($subject[$j])); |
125 | $messages[$j]["TO"] = decodeHeader($to[$j]); |
126 | $messages[$j]["PRIORITY"] = $priority[$j]; |
127 | $messages[$j]["CC"] = $cc[$j]; |
128 | $messages[$j]["SIZE"] = $size[$j]; |
129 | $messages[$j]["TYPE0"] = $type[$j]; |
130 | |
131 | $num = 0; |
132 | while ($num < count($flags[$j])) { |
133 | if ($flags[$j][$num] == 'Deleted') { |
134 | $messages[$j]['FLAG_DELETED'] = true; |
135 | } else if ($flags[$j][$num] == 'Answered') { |
136 | $messages[$j]['FLAG_ANSWERED'] = true; |
137 | } else if ($flags[$j][$num] == 'Seen') { |
138 | $messages[$j]['FLAG_SEEN'] = true; |
139 | } else if ($flags[$j][$num] == 'Flagged') { |
140 | $messages[$j]['FLAG_FLAGGED'] = true; |
2ba13803 |
141 | } |
29eb5486 |
142 | $num++; |
fc05541b |
143 | } |
29eb5486 |
144 | $j++; |
145 | } |
2ba13803 |
146 | |
29eb5486 |
147 | /* Find and remove the ones that are deleted */ |
148 | $i = 0; |
149 | $j = 0; |
150 | while ($j < count($messagelist)) { |
70c4fd84 |
151 | if (isset($messages[$j]['FLAG_DELETED']) && $messages[$j]['FLAG_DELETED']) { |
1809bad8 |
152 | $j++; |
29eb5486 |
153 | continue; |
2ba13803 |
154 | } |
29eb5486 |
155 | $msgs[$i] = $messages[$j]; |
2ba13803 |
156 | |
29eb5486 |
157 | $i++; |
158 | $j++; |
159 | } |
160 | $numMessages = $i; |
2ba13803 |
161 | |
29eb5486 |
162 | /* There's gotta be messages in the array for it to sort them. */ |
2ba13803 |
163 | |
29eb5486 |
164 | if (count($messagelist) > 0) { |
165 | $j=0; |
70c4fd84 |
166 | if (!isset ($msg)) { |
167 | $msg = ''; |
53828ec5 |
168 | } |
8d93176b |
169 | if ($search_all != 'all') { |
70c4fd84 |
170 | if ( !isset( $start_msg ) ) { |
171 | $start_msg =0; |
172 | } |
173 | if ( !isset( $sort ) ) { |
174 | $sort = 0; |
175 | } |
8d93176b |
176 | mail_message_listing_beginning( $imapConnection, |
177 | "move_messages.php?msg=$msg&mailbox=$urlMailbox&pos=$pos&where=" . urlencode($search_where) . "&what=".urlencode($search_what), |
b1e29794 |
178 | $mailbox, |
8d93176b |
179 | -1, |
180 | '<b>' . _("Found") . ' ' . count($messagelist) . ' ' . _("messages") . '</b></tr><tr>'. |
b1e29794 |
181 | get_selectall_link($start_msg, $sort)); |
8d93176b |
182 | } |
183 | else { |
184 | mail_message_listing_beginning( $imapConnection, |
185 | "move_messages.php?msg=$msg&mailbox=$urlMailbox&pos=$pos&where=" . urlencode($search_where) . "&what=".urlencode($search_what), |
b1e29794 |
186 | $mailbox, |
8d93176b |
187 | -1, |
188 | '<b>' . _("Found") . ' ' . count($messagelist) . ' ' . _("messages") . '</b></tr><tr>'); |
189 | } |
f8a2d9ee |
190 | if ( $mailbox == 'INBOX' ) { |
191 | $showbox = _("INBOX"); |
192 | } else { |
193 | $showbox = $mailbox; |
194 | } |
195 | echo '<b><big>' . _("Folder:") . " $showbox</big></b>"; |
29eb5486 |
196 | while ($j < count($msgs)) { |
197 | printMessageInfo($imapConnection, $msgs[$j]["ID"], 0, $j, $mailbox, '', 0, $search_where, $search_what); |
198 | $j++; |
70c4fd84 |
199 | echo '</td></tr>'; |
2ba13803 |
200 | } |
7c0060c9 |
201 | echo '</table></td></tr></table></form>'; |
70c4fd84 |
202 | $count_all = count($msgs); |
2ba13803 |
203 | } |
70c4fd84 |
204 | return $count_all; |
29eb5486 |
205 | } |
fc05541b |
206 | |
1c72b151 |
207 | ?> |