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