fix for search function trying ASCII when CHARSET fails. bug #552288
[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;
25
26 $pos = $search_position;
27
28 $urlMailbox = urlencode($mailbox);
29
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) {
37 $search_string .= $search_where . ' {' . strlen($multi_search_part)
38 . "}\r\n" . $multi_search_part . ' ';
39 }
40
41 $search_string = trim($search_string);
42
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";
49 } else {
50 $ss = "SEARCH ALL $search_string";
51 }
52
53 /* read data back from IMAP */
54 $readin = sqimap_run_command($imapConnection, $ss, false, $result, $message);
55
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,
61 $result, $message);
62 }
63
64 unset($messagelist);
65 $msgs = '';
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 }
77 }
78
79 /* If nothing is found * SEARCH should be the first error else echo errors */
80 if (isset($errors)) {
81 if (strstr($errors,'* SEARCH')) {
82 if ($search_all != 'all') {
83 echo '<br><CENTER>' . _("No Messages Found") . '</CENTER>';
84 }
85 return;
86 }
87 echo "<!-- $errors -->";
88 }
89
90 /*
91 * HACKED CODE FROM ANOTHER FUNCTION, could probably dump this and modify
92 * existing code with a search true/false variable.
93 */
94
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
103 foreach ($hdr_list as $hdr) {
104 $from[] = $hdr->from;
105 $date[] = $hdr->date;
106 $subject[] = $hdr->subject;
107 $to[] = $hdr->to;
108 $priority[] = $hdr->priority;
109 $cc[] = $hdr->cc;
110 $size[] = $hdr->size;
111 $type[] = $hdr->type0;
112 $flag_deleted[] = $hdr->flag_deleted;
113 $flag_answered[] = $hdr->flag_answered;
114 $flag_seen[] = $hdr->flag_seen;
115 $flag_flagged[] = $hdr->flag_flagged;
116 }
117
118 $j = 0;
119 while ($j < count($messagelist)) {
120 $date[$j] = str_replace(' ', ' ', $date[$j]);
121 $tmpdate = explode(' ', trim($date[$j]));
122
123 $messages[$j]["TIME_STAMP"] = getTimeStamp($tmpdate);
124 $messages[$j]["DATE_STRING"] = getDateString($messages[$j]["TIME_STAMP"]);
125 $messages[$j]["ID"] = $id[$j];
126 $messages[$j]["FROM"] = decodeHeader($from[$j]);
127 $messages[$j]["FROM-SORT"] = strtolower(sqimap_find_displayable_name(decodeHeader($from[$j])));
128 $messages[$j]["SUBJECT"] = decodeHeader($subject[$j]);
129 $messages[$j]["SUBJECT-SORT"] = strtolower(decodeHeader($subject[$j]));
130 $messages[$j]["TO"] = decodeHeader($to[$j]);
131 $messages[$j]["PRIORITY"] = $priority[$j];
132 $messages[$j]["CC"] = $cc[$j];
133 $messages[$j]["SIZE"] = $size[$j];
134 $messages[$j]["TYPE0"] = $type[$j];
135 $messages[$j]['FLAG_DELETED'] = $flag_deleted[$j];
136 $messages[$j]['FLAG_ANSWERED'] = $flag_answered[$j];
137 $messages[$j]['FLAG_SEEN'] = $flag_seen[$j];
138 $messages[$j]['FLAG_FLAGGED'] = $flag_flagged[$j];
139 /*
140 $num = 0;
141 while ($num < count($flags[$j])) {
142 if ($flags[$j][$num] == 'Deleted') {
143 $messages[$j]['FLAG_DELETED'] = true;
144 } else if ($flags[$j][$num] == 'Answered') {
145 $messages[$j]['FLAG_ANSWERED'] = true;
146 } else if ($flags[$j][$num] == 'Seen') {
147 $messages[$j]['FLAG_SEEN'] = true;
148 } else if ($flags[$j][$num] == 'Flagged') {
149 $messages[$j]['FLAG_FLAGGED'] = true;
150 }
151 $num++;
152 }
153 */
154 $j++;
155
156 }
157
158 /* Find and remove the ones that are deleted */
159 $i = 0;
160 $j = 0;
161 while ($j < count($messagelist)) {
162 if (isset($messages[$j]['FLAG_DELETED']) && $messages[$j]['FLAG_DELETED']) {
163 $j++;
164 continue;
165 }
166 $msgs[$i] = $messages[$j];
167
168 $i++;
169 $j++;
170 }
171 $numMessages = $i;
172
173 /* There's gotta be messages in the array for it to sort them. */
174
175 if (count($messagelist) > 0) {
176 $j=0;
177 if (!isset ($msg)) {
178 $msg = '';
179 }
180 if ($search_all != 'all') {
181 if ( !isset( $start_msg ) ) {
182 $start_msg =0;
183 }
184 if ( !isset( $sort ) ) {
185 $sort = 0;
186 }
187 mail_message_listing_beginning( $imapConnection,
188 "move_messages.php?msg=$msg&mailbox=$urlMailbox&pos=$pos&where=" . urlencode($search_where) . "&what=".urlencode($search_what),
189 $mailbox,
190 -1,
191 '<b>' . _("Found") . ' ' . count($messagelist) . ' ' . _("messages") . '</b></tr><tr>'.
192 get_selectall_link($start_msg, $sort));
193 }
194 else {
195 mail_message_listing_beginning( $imapConnection,
196 "move_messages.php?msg=$msg&mailbox=$urlMailbox&pos=$pos&where=" . urlencode($search_where) . "&what=".urlencode($search_what),
197 $mailbox,
198 -1,
199 '<b>' . _("Found") . ' ' . count($messagelist) . ' ' . _("messages") . '</b></tr><tr>');
200 }
201 if ( $mailbox == 'INBOX' ) {
202 $showbox = _("INBOX");
203 } else {
204 $showbox = $mailbox;
205 }
206 echo '<b><big>' . _("Folder:") . " $showbox</big></b>";
207 while ($j < count($msgs)) {
208 printMessageInfo($imapConnection, $msgs[$j]["ID"], 0, $j, $mailbox, '', 0, $search_where, $search_what);
209 $j++;
210 echo '</td></tr>';
211 }
212 echo '</table></td></tr></table></form>';
213 $count_all = count($msgs);
214 }
215 return $count_all;
216 }
217
218 ?>