fixed double folder problem
[squirrelmail.git] / functions / imap_search.php
CommitLineData
fc05541b 1<?php
2/******************************************************************
3 ** IMAP SEARCH ROUTIES
4 *****************************************************************/
5 if (!isset($imap_php))
6 include("../functions/imap.php");
7 if (!isset($date_php))
8 include("../functions/date.php");
9 if (!isset($array_php))
10 include("../functions/array.php");
11 if (!isset($mailbox_display_php))
12 include("../functions/mailbox_display.php");
13 if (!isset($mime_php))
14 include("../functions/mime.php");
15
99fa2b21 16 $imap_search_php = true;
17
fc05541b 18function sqimap_search($imapConnection,$search_where,$search_what,$mailbox,$color) {
99fa2b21 19 global $msgs, $message_highlight_list;
1809bad8 20 $urlMailbox = urlencode($mailbox);
21
22 # Construct the Search QuERY
23
24 $ss = "a001 SEARCH ALL $search_where \"$search_what\"\r\n";
25 fputs($imapConnection,$ss);
26
27 # Read Data Back From IMAP
28 $readin = sqimap_read_data ($imapConnection, "a001", true, $result, $message);
29 unset($messagelist); $msgs=""; $c = 0;
30
31 #Keep going till we find the SEARCH responce
32 while ($c < count($readin)) {
33
34 #Check to see if a SEARCH Responce was recived
35 if (substr($readin[$c],0,9) == "* SEARCH ")
36 $messagelist = explode(" ",substr($readin[$c],9));
37 else
38 $errors = $errors.$readin[$c];
39 $c++;
40 }
41
42 #If nothing is found * SEARCH should be the first error else echo errors
43 if (strstr($errors,"* SEARCH")) {
44 echo "<br><CENTER>No Messages Found</CENTER>";
45 return;
46 } else {
47 echo "<!-- ".$errors." -->";
48 }
49
50 # HACKED CODED FROM ANOTHER FUNCTION, Could Probably dump this and mondify
51 # exsitising code with a search true/false varible.
52
53 global $sent_folder;
54 for ($q = 0; $q < count($messagelist); $q++) {
55 $messagelist[$q] = trim($messagelist[$q]);
56 if ($mailbox == $sent_folder)
57 $hdr = sqimap_get_small_header ($imapConnection, $messagelist[$q], true);
58 else
59 $hdr = sqimap_get_small_header ($imapConnection, $messagelist[$q], false);
99fa2b21 60
61 $from[$q] = $hdr->from;
62 $date[$q] = $hdr->date;
63 $subject[$q] = $hdr->subject;
64 $to[$q] = $hdr->to;
65 $priority[$q] = $hdr->priority;
66 $cc[$q] = $hdr->cc;
1809bad8 67 $id[$q] = $messagelist[$q];
fc05541b 68
1809bad8 69 $flags[$q] = sqimap_get_flags ($imapConnection, $messagelist[$q]);
70 }
fc05541b 71
1809bad8 72 $j = 0;
73 while ($j < count($messagelist)) {
74 $date[$j] = ereg_replace(" ", " ", $date[$j]);
75 $tmpdate = explode(" ", trim($date[$j]));
76
77 $messages[$j]["TIME_STAMP"] = getTimeStamp($tmpdate);
78 $messages[$j]["DATE_STRING"] = getDateString($messages[$j]["TIME_STAMP"]);
99fa2b21 79 $messages[$j]["ID"] = $id[$j];
1809bad8 80 $messages[$j]["FROM"] = decodeHeader($from[$j]);
99fa2b21 81 $messages[$j]["FROM-SORT"] = strtolower(sqimap_find_displayable_name(decodeHeader($from[$j])));
1809bad8 82 $messages[$j]["SUBJECT"] = decodeHeader($subject[$j]);
99fa2b21 83 $messages[$j]["SUBJECT-SORT"] = strtolower(decodeHeader($subject[$j]));
84 $messages[$j]["TO"] = decodeHeader($to[$j]);
85 $messages[$j]["PRIORITY"] = $priority[$j];
86 $messages[$j]["CC"] = $cc[$j];
1809bad8 87
88 $num = 0;
89 while ($num < count($flags[$j])) {
90 if ($flags[$j][$num] == "Deleted") {
91 $messages[$j]["FLAG_DELETED"] = true;
92 }
93 else if ($flags[$j][$num] == "Answered") {
94 $messages[$j]["FLAG_ANSWERED"] = true;
95 }
96 else if ($flags[$j][$num] == "Seen") {
97 $messages[$j]["FLAG_SEEN"] = true;
fc05541b 98 }
1809bad8 99 else if ($flags[$j][$num] == "Flagged") {
100 $messages[$j]["FLAG_FLAGGED"] = true;
101 }
102 $num++;
103 }
104 $j++;
105 }
fc05541b 106
1809bad8 107 /** Find and remove the ones that are deleted */
108 $i = 0;
109 $j = 0;
110 while ($j < count($messagelist)) {
111 if ($messages[$j]["FLAG_DELETED"] == true) {
fc05541b 112 $j++;
1809bad8 113 continue;
fc05541b 114 }
1809bad8 115 $msgs[$i] = $messages[$j];
fc05541b 116
1809bad8 117 $i++;
118 $j++;
119 }
120 $numMessages = $i;
121
122 // There's gotta be messages in the array for it to sort them.
123
1809bad8 124 if (count($messagelist) > 0) {
125 $j=0;
126 echo "<center><b>" . _("Found") . " " . count($messagelist) . " " . _("messages") . "</b></center>\n";
127 echo "<br>\n";
128 echo "<TABLE WIDTH=100% BORDER=0 CELLPADDING=2 CELLSPACING=0>";
129 echo "<TR><TD BGCOLOR=\"$color[0]\">";
130
131 echo "\n\n\n<FORM name=messageList method=post action=\"move_messages.php?msg=$msg&mailbox=$urlMailbox&where=".urlencode($search_where)."&what=".urlencode($search_what)."\">";
132 echo "<TABLE BGCOLOR=\"$color[0]\" COLS=2 BORDER=0 cellpadding=0 cellspacing=0>\n";
133 echo " <TR>\n";
98fe1e9f 134 echo " <TD WIDTH=60% ALIGN=LEFT VALIGN=CENTER>\n";
1809bad8 135 echo " <NOBR><SMALL>". _("Move selected to:") ."</SMALL>";
136 echo " <TT><SMALL><SELECT NAME=\"targetMailbox\">";
137
138 $boxes = sqimap_mailbox_list($imapConnection);
139 for ($i = 0; $i < count($boxes); $i++) {
140 if ($boxes[$i]["flags"][0] != "noselect" && $boxes[$i]["flags"][1] != "noselect" && $boxes[$i]["flags"][2] != "noselect") {
141 $box = $boxes[$i]["unformatted"];
142 $box2 = replace_spaces($boxes[$i]["formatted"]);
143 echo " <OPTION VALUE=\"$box\">$box2\n";
144 }
145 }
146 echo " </SELECT></SMALL></TT>";
147 echo " <SMALL><INPUT TYPE=SUBMIT NAME=\"moveButton\" VALUE=\"". _("Move") ."\"></SMALL></NOBR>\n";
148
149 echo " </TD>\n";
150 echo " <TD WIDTH=40% ALIGN=RIGHT>\n";
151 echo " <NOBR><SMALL><INPUT TYPE=SUBMIT VALUE=\"". _("Delete") ."\">&nbsp;". _("checked messages") ."</SMALL></NOBR>\n";
152 echo " </TD>";
153 echo " </TR>\n";
154
155 echo "</TABLE>\n\n\n";
156 echo "</TD></TR>";
157 echo "<TR><TD BGCOLOR=\"$color[0]\">";
98fe1e9f 158 echo "<TABLE WIDTH=100% BORDER=0 CELLPADDING=2 CELLSPACING=1 BGCOLOR=\"$color[0]\">";
1809bad8 159 echo "<TR BGCOLOR=\"$color[5]\" ALIGN=\"center\">";
160 echo " <TD WIDTH=1%><B>&nbsp;</B></TD>";
161 /** FROM HEADER **/
162 if ($mailbox == $sent_folder)
163 echo " <TD WIDTH=30%><B>". _("To") ."</B></td>";
164 else
165 echo " <TD WIDTH=30%><B>". _("From") ."</B></td>";
166 /** DATE HEADER **/
167 echo " <TD nowrap WIDTH=1%><B>". _("Date") ."</B></td>";
168 echo " <TD WIDTH=1%>&nbsp;</TD>\n";
169 /** SUBJECT HEADER **/
170 echo " <TD WIDTH=%><B>". _("Subject") ."</B></td>\n";
171 echo "</TR>";
172
173 while ($j < count($msgs)) {
174 printMessageInfo($imapConnection, $msgs[$j]["ID"], 0, $j, $mailbox, "", 0, $search_where, $search_what);
1809bad8 175 $j++;
fc05541b 176 }
1809bad8 177 echo "</table>";
178 echo "</tr></td></table>";
fc05541b 179 }
1809bad8 180 }
fc05541b 181
182?>