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