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 | |
53828ec5 |
20 | function sqimap_search($imapConnection,$search_where,$search_what,$mailbox,$color, $search_position = '') { |
2ba13803 |
21 | |
29eb5486 |
22 | global $msgs, $message_highlight_list, $squirrelmail_language, $languages, $index_order; |
53828ec5 |
23 | global $pos; |
24 | |
25 | $pos = $search_position; |
2ba13803 |
26 | |
29eb5486 |
27 | $urlMailbox = urlencode($mailbox); |
2ba13803 |
28 | |
29eb5486 |
29 | /* Construct the Search QuERY */ |
c92ef720 |
30 | |
31 | # account for multiple search terms |
32 | |
33 | $multi_search = array (); |
34 | $search_what = ereg_replace("[ ]{2,}", " ", $search_what); |
35 | $multi_search = split (" ", $search_what); |
36 | if (count($multi_search)==1) { |
37 | $search_string = $search_where . " " . "\"" . $multi_search[0] . "\""; |
38 | } |
39 | else { |
40 | $search_string = ""; |
41 | $count = count($multi_search); |
42 | for ($x=0;$x<$count;$x++) { |
43 | $search_string = $search_string . " " . $search_where . " " . "\"" . $multi_search[$x] . "\""; |
44 | } |
45 | } |
46 | $search_string = ereg_replace("^ ", "", $search_string); |
47 | |
48 | # now use $search_string in the imap search |
49 | |
29eb5486 |
50 | if (isset($languages[$squirrelmail_language]['CHARSET']) && |
51 | $languages[$squirrelmail_language]['CHARSET']) { |
c92ef720 |
52 | $ss = "SEARCH CHARSET ".$languages[$squirrelmail_language]['CHARSET']." ALL $search_string"; |
29eb5486 |
53 | } else { |
c92ef720 |
54 | $ss .= "SEARCH ALL $search_string\""; |
29eb5486 |
55 | } |
29eb5486 |
56 | |
57 | /* Read Data Back From IMAP */ |
1c72b151 |
58 | $readin = sqimap_run_command ($imapConnection, $ss, true, $result, $message); |
29eb5486 |
59 | if (isset($languages[$squirrelmail_language]['CHARSET']) && strtolower($result) == 'no') { |
1c72b151 |
60 | $ss = "SEARCH CHARSET \"US-ASCII\" ALL $search_where \"$search_what\""; |
61 | $readin = sqimap_run_command ($imapConnection, $ss, true, $result, $message); |
29eb5486 |
62 | } |
2ba13803 |
63 | |
29eb5486 |
64 | unset($messagelist); $msgs=""; $c = 0; |
2ba13803 |
65 | |
29eb5486 |
66 | /* Keep going till we find the SEARCH responce */ |
67 | while ($c < count( $readin )) { |
2ba13803 |
68 | |
29eb5486 |
69 | /* Check to see if a SEARCH Responce was recived */ |
70 | if (substr($readin[$c],0,9) == "* SEARCH ") |
71 | $messagelist = explode(" ",substr($readin[$c],9)); |
72 | else if (isset($errors)) |
73 | $errors = $errors.$readin[$c]; |
74 | else |
75 | $errors = $readin[$c]; |
76 | $c++; |
77 | } |
2ba13803 |
78 | |
29eb5486 |
79 | /* If nothing is found * SEARCH should be the first error else echo errors */ |
80 | if (isset($errors) && strstr($errors,"* SEARCH")) { |
81 | echo '<br><CENTER>' . _("No Messages Found") . '</CENTER>'; |
82 | return; |
83 | } else if (isset($errors)) { |
84 | echo "<!-- ".$errors." -->"; |
85 | } |
2ba13803 |
86 | |
29eb5486 |
87 | /* |
88 | HACKED CODED FROM ANOTHER FUNCTION, Could Probably dump this and mondify |
89 | exsitising code with a search true/false varible. |
90 | */ |
2ba13803 |
91 | |
29eb5486 |
92 | global $sent_folder; |
93 | for ($q = 0; $q < count($messagelist); $q++) { |
94 | $id[$q] = trim($messagelist[$q]); |
95 | } |
96 | $issent = ($mailbox == $sent_folder); |
97 | $hdr_list = sqimap_get_small_header_list($imapConnection, $id, $issent); |
98 | $flags = sqimap_get_flags_list($imapConnection, $id, $issent); |
99 | foreach ($hdr_list as $hdr) { |
100 | $from[] = $hdr->from; |
101 | $date[] = $hdr->date; |
102 | $subject[] = $hdr->subject; |
103 | $to[] = $hdr->to; |
104 | $priority[] = $hdr->priority; |
105 | $cc[] = $hdr->cc; |
106 | $size[] = $hdr->size; |
107 | $type[] = $hdr->type0; |
108 | } |
2ba13803 |
109 | |
29eb5486 |
110 | $j = 0; |
111 | while ($j < count($messagelist)) { |
112 | $date[$j] = ereg_replace(' ', ' ', $date[$j]); |
113 | $tmpdate = explode(" ", trim($date[$j])); |
114 | |
115 | $messages[$j]["TIME_STAMP"] = getTimeStamp($tmpdate); |
116 | $messages[$j]["DATE_STRING"] = getDateString($messages[$j]["TIME_STAMP"]); |
117 | $messages[$j]["ID"] = $id[$j]; |
118 | $messages[$j]["FROM"] = decodeHeader($from[$j]); |
119 | $messages[$j]["FROM-SORT"] = strtolower(sqimap_find_displayable_name(decodeHeader($from[$j]))); |
120 | $messages[$j]["SUBJECT"] = decodeHeader($subject[$j]); |
121 | $messages[$j]["SUBJECT-SORT"] = strtolower(decodeHeader($subject[$j])); |
122 | $messages[$j]["TO"] = decodeHeader($to[$j]); |
123 | $messages[$j]["PRIORITY"] = $priority[$j]; |
124 | $messages[$j]["CC"] = $cc[$j]; |
125 | $messages[$j]["SIZE"] = $size[$j]; |
126 | $messages[$j]["TYPE0"] = $type[$j]; |
127 | |
128 | $num = 0; |
129 | while ($num < count($flags[$j])) { |
130 | if ($flags[$j][$num] == 'Deleted') { |
131 | $messages[$j]['FLAG_DELETED'] = true; |
132 | } else if ($flags[$j][$num] == 'Answered') { |
133 | $messages[$j]['FLAG_ANSWERED'] = true; |
134 | } else if ($flags[$j][$num] == 'Seen') { |
135 | $messages[$j]['FLAG_SEEN'] = true; |
136 | } else if ($flags[$j][$num] == 'Flagged') { |
137 | $messages[$j]['FLAG_FLAGGED'] = true; |
2ba13803 |
138 | } |
29eb5486 |
139 | $num++; |
fc05541b |
140 | } |
29eb5486 |
141 | $j++; |
142 | } |
2ba13803 |
143 | |
29eb5486 |
144 | /* Find and remove the ones that are deleted */ |
145 | $i = 0; |
146 | $j = 0; |
147 | while ($j < count($messagelist)) { |
148 | if (isset($messages[$j]["FLAG_DELETED"]) && $messages[$j]["FLAG_DELETED"] == true) { |
1809bad8 |
149 | $j++; |
29eb5486 |
150 | continue; |
2ba13803 |
151 | } |
29eb5486 |
152 | $msgs[$i] = $messages[$j]; |
2ba13803 |
153 | |
29eb5486 |
154 | $i++; |
155 | $j++; |
156 | } |
157 | $numMessages = $i; |
2ba13803 |
158 | |
29eb5486 |
159 | /* There's gotta be messages in the array for it to sort them. */ |
2ba13803 |
160 | |
29eb5486 |
161 | if (count($messagelist) > 0) { |
162 | $j=0; |
53828ec5 |
163 | if (!isset ($msg)) { |
164 | $msg = ''; |
165 | } |
7c0060c9 |
166 | |
29eb5486 |
167 | mail_message_listing_beginning( $imapConnection, |
53828ec5 |
168 | "move_messages.php?msg=$msg&mailbox=$urlMailbox&pos=$pos&where=" . urlencode($search_where) . "&what=".urlencode($search_what), |
7c0060c9 |
169 | $mailbox, |
170 | -1, |
29eb5486 |
171 | '<b>' . _("Found") . ' ' . count($messagelist) . ' ' . _("messages") . '</b>', |
172 | get_selectall_link($start_msg, $sort) ); |
2ba13803 |
173 | |
29eb5486 |
174 | |
175 | while ($j < count($msgs)) { |
176 | printMessageInfo($imapConnection, $msgs[$j]["ID"], 0, $j, $mailbox, '', 0, $search_where, $search_what); |
177 | $j++; |
2ba13803 |
178 | } |
7c0060c9 |
179 | echo '</table></td></tr></table></form>'; |
180 | |
2ba13803 |
181 | } |
29eb5486 |
182 | } |
fc05541b |
183 | |
1c72b151 |
184 | ?> |