a82242fd207233a9b9406943cdb72984cd330dbb
[squirrelmail.git] / functions / imap_search.php
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
16 $imap_search_php = true;
17
18 function sqimap_search($imapConnection,$search_where,$search_what,$mailbox,$color) {
19 global $msgs, $message_highlight_list;
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);
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;
67 $id[$q] = $messagelist[$q];
68
69 $flags[$q] = sqimap_get_flags ($imapConnection, $messagelist[$q]);
70 }
71
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"]);
79 $messages[$j]["ID"] = $id[$j];
80 $messages[$j]["FROM"] = decodeHeader($from[$j]);
81 $messages[$j]["FROM-SORT"] = strtolower(sqimap_find_displayable_name(decodeHeader($from[$j])));
82 $messages[$j]["SUBJECT"] = decodeHeader($subject[$j]);
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];
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;
98 }
99 else if ($flags[$j][$num] == "Flagged") {
100 $messages[$j]["FLAG_FLAGGED"] = true;
101 }
102 $num++;
103 }
104 $j++;
105 }
106
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) {
112 $j++;
113 continue;
114 }
115 $msgs[$i] = $messages[$j];
116
117 $i++;
118 $j++;
119 }
120 $numMessages = $i;
121
122 // There's gotta be messages in the array for it to sort them.
123
124 # Carn't Use the Display messages function it assumes messages are in order.
125 # Again More code Hacked from else where
126 # AT THE MOMENT YOU CARN'T SORT SEARCH RESULTS
127 # ACTULLY THE CODE IS PROLLY BROKEN ANY HOW!
128
129 if (count($messagelist) > 0) {
130 $j=0;
131 echo "<center><b>" . _("Found") . " " . count($messagelist) . " " . _("messages") . "</b></center>\n";
132 echo "<br>\n";
133 echo "<TABLE WIDTH=100% BORDER=0 CELLPADDING=2 CELLSPACING=0>";
134 echo "<TR><TD BGCOLOR=\"$color[0]\">";
135
136 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)."\">";
137 echo "<TABLE BGCOLOR=\"$color[0]\" COLS=2 BORDER=0 cellpadding=0 cellspacing=0>\n";
138 echo " <TR>\n";
139 echo " <TD WIDTH=60% ALIGN=LEFT VALIGN=CENTER>\n";
140 echo " <NOBR><SMALL>". _("Move selected to:") ."</SMALL>";
141 echo " <TT><SMALL><SELECT NAME=\"targetMailbox\">";
142
143 $boxes = sqimap_mailbox_list($imapConnection);
144 for ($i = 0; $i < count($boxes); $i++) {
145 if ($boxes[$i]["flags"][0] != "noselect" && $boxes[$i]["flags"][1] != "noselect" && $boxes[$i]["flags"][2] != "noselect") {
146 $box = $boxes[$i]["unformatted"];
147 $box2 = replace_spaces($boxes[$i]["formatted"]);
148 echo " <OPTION VALUE=\"$box\">$box2\n";
149 }
150 }
151 echo " </SELECT></SMALL></TT>";
152 echo " <SMALL><INPUT TYPE=SUBMIT NAME=\"moveButton\" VALUE=\"". _("Move") ."\"></SMALL></NOBR>\n";
153
154 echo " </TD>\n";
155 echo " <TD WIDTH=40% ALIGN=RIGHT>\n";
156 echo " <NOBR><SMALL><INPUT TYPE=SUBMIT VALUE=\"". _("Delete") ."\">&nbsp;". _("checked messages") ."</SMALL></NOBR>\n";
157 echo " </TD>";
158 echo " </TR>\n";
159
160 echo "</TABLE>\n\n\n";
161 echo "</TD></TR>";
162 echo "<TR><TD BGCOLOR=\"$color[0]\">";
163 echo "<TABLE WIDTH=100% BORDER=0 CELLPADDING=2 CELLSPACING=1 BGCOLOR=\"$color[0]\">";
164 echo "<TR BGCOLOR=\"$color[5]\" ALIGN=\"center\">";
165 echo " <TD WIDTH=1%><B>&nbsp;</B></TD>";
166 /** FROM HEADER **/
167 if ($mailbox == $sent_folder)
168 echo " <TD WIDTH=30%><B>". _("To") ."</B></td>";
169 else
170 echo " <TD WIDTH=30%><B>". _("From") ."</B></td>";
171 /** DATE HEADER **/
172 echo " <TD nowrap WIDTH=1%><B>". _("Date") ."</B></td>";
173 echo " <TD WIDTH=1%>&nbsp;</TD>\n";
174 /** SUBJECT HEADER **/
175 echo " <TD WIDTH=%><B>". _("Subject") ."</B></td>\n";
176 echo "</TR>";
177
178 while ($j < count($msgs)) {
179 printMessageInfo($imapConnection, $msgs[$j]["ID"], 0, $j, $mailbox, "", 0, $search_where, $search_what);
180 $j++;
181 }
182 echo "</table>";
183 echo "</tr></td></table>";
184 }
185 }
186
187 ?>