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