Bug fix in date sorting
[squirrelmail.git] / functions / mailbox_display.php
1 <?
2
3 /**
4 ** mailbox_display.php
5 **
6 ** This contains functions that display mailbox information, such as the
7 ** table row that has sender, date, subject, etc...
8 **
9 **/
10
11 function printMessageInfo($imapConnection, $t, $i, $from, $subject, $dateString, $answered, $seen) {
12 $senderName = getSenderName($from);
13 if (strlen(Chop($subject)) == 0)
14 $subject = "(no subject)";
15
16 echo "<TR>\n";
17 if ($seen == false) {
18 echo " <TD><FONT FACE=\"Arial,Helvetica\"><nobr><B><input type=checkbox name=\"msg[$t]\" value=$i></B></nobr></FONT></TD>\n";
19 echo " <TD><FONT FACE=\"Arial,Helvetica\"><B>$senderName</B></FONT></TD>\n";
20 echo " <TD><CENTER><B><FONT FACE=\"Arial,Helvetica\">$dateString</FONT></B></CENTER></TD>\n";
21 echo " <TD><FONT FACE=\"Arial,Helvetica\"><B>$subject</B></FONT></TD>\n";
22 } else {
23 echo " <TD><FONT FACE=\"Arial,Helvetica\"><nobr><input type=checkbox name=\"msg[$t]\" value=$i></nobr></FONT></TD>\n";
24 echo " <TD><FONT FACE=\"Arial,Helvetica\">$senderName</FONT></TD>\n";
25 echo " <TD><FONT FACE=\"Arial,Helvetica\"><CENTER>$dateString</CENTER></FONT></TD>\n";
26 echo " <TD><FONT FACE=\"Arial,Helvetica\">$subject</FONT></TD>\n";
27 }
28 echo "</TR>\n";
29 }
30
31 /**
32 ** This function loops through a group of messages in the mailbox and shows them
33 **/
34 function showMessagesForMailbox($imapConnection, $mailbox, $numMessages, $startMessage, $sort) {
35 include("../config/config.php");
36
37 if (1 <= $numMessages) {
38 getMessageHeaders($imapConnection, 1, $numMessages, $from, $subject, $date);
39 }
40
41 $j = 0;
42 while ($j < $numMessages) {
43 $date[$j] = ereg_replace(" ", " ", $date[$j]);
44 $tmpdate = explode(" ", trim($date[$j]));
45
46 $messages[$j]["TIME_STAMP"] = getTimeStamp($tmpdate);
47 $messages[$j]["DATE_STRING"] = getDateString($tmpdate);
48 $messages[$j]["ID"] = $j+1;
49 $messages[$j]["FROM"] = getSenderName($from[$j]);
50 $messages[$j]["SUBJECT"] = $subject[$j];
51 $messages[$j]["FLAG_DELETED"] = false;
52 $messages[$j]["FLAG_ANSWERED"] = false;
53 $messages[$j]["FLAG_SEEN"] = false;
54
55 $num = 0;
56 getMessageFlags($imapConnection, $j+1, $flags);
57 while ($num < count($flags)) {
58 if ($flags[$num] == "Deleted") {
59 $messages[$j]["FLAG_DELETED"] = true;
60 }
61 else if ($flags[$num] == "Answered") {
62 $messages[$j]["FLAG_ANSWERED"] = true;
63 }
64 else if ($flags[$num] == "Seen") {
65 $messages[$j]["FLAG_SEEN"] = true;
66 }
67 $num++;
68 }
69 $j++;
70 }
71
72 /** Find and remove the ones that are deleted */
73 $i = 0;
74 $j = 0;
75 while ($j < $numMessages) {
76 if ($messages[$j]["FLAG_DELETED"] == true) {
77 $j++;
78 continue;
79 }
80 $msgs[$i]["TIME_STAMP"] = $messages[$j]["TIME_STAMP"];
81 $msgs[$i]["DATE_STRING"] = $messages[$j]["DATE_STRING"];
82 $msgs[$i]["ID"] = $messages[$j]["ID"];
83 $msgs[$i]["FROM"] = $messages[$j]["FROM"];
84 $msgs[$i]["SUBJECT"] = $messages[$j]["SUBJECT"];
85 $msgs[$i]["FLAG_DELETED"] = $messages[$j]["FLAG_DELETED"];
86 $msgs[$i]["FLAG_ANSWERED"] = $messages[$j]["FLAG_ANSWERED"];
87 $msgs[$i]["FLAG_SEEN"] = $messages[$j]["FLAG_SEEN"];
88 $i++;
89 $j++;
90 }
91
92 $numMessages = $i;
93
94 // There's gotta be messages in the array for it to sort them.
95 if ($numMessages > 0) {
96 /** 0 = Date (up) 4 = Subject (up)
97 ** 1 = Date (dn) 5 = Subject (dn)
98 ** 2 = Name (up)
99 ** 3 = Name (dn)
100 **/
101 if ($sort == 0)
102 $msgs = ary_sort($msgs, "TIME_STAMP", -1);
103 else if ($sort == 1)
104 $msgs = ary_sort($msgs, "TIME_STAMP", 1);
105 else {
106 $original = $msgs;
107 $i = 0;
108 while ($i < count($msgs)) {
109 $msgs[$i]["FROM"] = strtolower($msgs[$i]["FROM"]);
110 $msgs[$i]["SUBJECT"] = strtolower($msgs[$i]["SUBJECT"]);
111 $i++;
112 }
113
114 if ($sort == 2)
115 $msgs = ary_sort($msgs, "FROM", -1);
116 else if ($sort == 3)
117 $msgs = ary_sort($msgs, "FROM", 1);
118 else if ($sort == 4)
119 $msgs = ary_sort($msgs, "SUBJECT", -1);
120 else if ($sort == 5)
121 $msgs = ary_sort($msgs, "SUBJECT", 1);
122 else
123 $msgs = ary_sort($msgs, "TIME_STAMP", -1);
124
125 $i = 0;
126 while ($i < count($msgs)) {
127 $j = 0;
128 while ($j < count($original)) {
129 if ($msgs[$i]["ID"] == $original[$j]["ID"]) {
130 $msgs[$i]["FROM"] = $original[$j]["FROM"];
131 $msgs[$i]["SUBJECT"] = $original[$j]["SUBJECT"];
132 }
133 $j++;
134 }
135 $i++;
136 }
137 }
138 }
139
140 if ($startMessage + 24 < $numMessages) {
141 $endMessage = $startMessage + 24;
142 } else {
143 $endMessage = $numMessages;
144 }
145
146 $nextGroup = $startMessage + 25;
147 $prevGroup = $startMessage - 25;
148 $urlMailbox = urlencode($mailbox);
149
150 /** This is the beginning of the message list table. It wraps around all messages */
151 echo "<TABLE WIDTH=100% BORDER=0 CELLPADDING=2 CELLSPACING=1>";
152
153 echo "<TR BGCOLOR=FFFFFF><TD>";
154 echo "<CENTER><FONT FACE=\"Arial,Helvetica\">Viewing messages <B>$startMessage</B> to <B>$endMessage</B> ($numMessages total)</FONT></CENTER>\n";
155 echo "</TD></TR>\n";
156
157 echo "<TR BGCOLOR=FFFFFF><TD>";
158 if (($nextGroup <= $numMessages) && ($prevGroup >= 0)) {
159 echo "<A HREF=\"right_main.php?sort=$sort&startMessage=$prevGroup&mailbox=$urlMailbox\" TARGET=\"right\"><FONT FACE=\"Arial,Helvetica\">Previous</FONT></A>\n";
160 echo "<A HREF=\"right_main.php?sort=$sort&startMessage=$nextGroup&mailbox=$urlMailbox\" TARGET=\"right\"><FONT FACE=\"Arial,Helvetica\">Next</FONT></A>\n";
161 }
162 else if (($nextGroup > $numMessages) && ($prevGroup >= 0)) {
163 echo "<A HREF=\"right_main.php?sort=$sort&startMessage=$prevGroup&mailbox=$urlMailbox\" TARGET=\"right\"><FONT FACE=\"Arial,Helvetica\">Previous</FONT></A>\n";
164 echo "<FONT FACE=\"Arial,Helvetica\" COLOR=ACACAC>Next</FONT>\n";
165 }
166 else if (($nextGroup <= $numMessages) && ($prevGroup < 0)) {
167 echo "<FONT FACE=\"Arial,Helvetica\" COLOR=ACACAC>Previous</FONT>\n";
168 echo "<A HREF=\"right_main.php?sort=$sort&startMessage=$nextGroup&mailbox=$urlMailbox\" TARGET=\"right\"><FONT FACE=\"Arial,Helvetica\">Next</FONT></A>\n";
169 }
170 echo "</TD></TR>\n";
171
172 /** The "DELETE" button */
173 echo "<TR><TD BGCOLOR=DCDCDC>";
174 echo "<FORM name=messageList method=post action=\"move_messages.php?mailbox=$urlMailbox&sort=$sort&startMessage=$startMessage\">";
175 echo "<NOBR><INPUT TYPE=SUBMIT VALUE=\"Delete Selected\">";
176 if (($move_to_trash == true) && ($mailbox == $trash_folder))
177 echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<A HREF=\"empty_trash.php?numMessages=$numMessages&mailbox=$urlMailbox\">Empty Trash</A></NOBR>";
178 echo "</TD></TR>";
179
180 echo "<TR><TD BGCOLOR=DCDCDC>";
181 echo "<TABLE WIDTH=100% BORDER=0 CELLPADDING=2 CELLSPACING=1 BGCOLOR=FFFFFF>";
182 echo "<TR BGCOLOR=FFFFCC ALIGN=\"center\">";
183 echo " <TD WIDTH=5%><FONT FACE=\"Arial,Helvetica\"><B>Num</B></FONT></TD>";
184 /** FROM HEADER **/
185 echo " <TD WIDTH=25%><FONT FACE=\"Arial,Helvetica\"><B>From</B></FONT>";
186 if ($sort == 2)
187 echo " <A HREF=\"right_main.php?sort=3&startMessage=1&mailbox=$urlMailbox\" TARGET=\"right\"><IMG SRC=\"../images/up_pointer.gif\" BORDER=0></A></TD>\n";
188 else if ($sort == 3)
189 echo " <A HREF=\"right_main.php?sort=2&startMessage=1&mailbox=$urlMailbox\" TARGET=\"right\"><IMG SRC=\"../images/down_pointer.gif\" BORDER=0></A></TD>\n";
190 else
191 echo " <A HREF=\"right_main.php?sort=3&startMessage=1&mailbox=$urlMailbox\" TARGET=\"right\"><IMG SRC=\"../images/sort_none.gif\" BORDER=0></A></TD>\n";
192 /** DATE HEADER **/
193 echo " <TD WIDTH=15%><FONT FACE=\"Arial,Helvetica\"><B>Date</B></FONT>";
194 if ($sort == 0)
195 echo " <A HREF=\"right_main.php?sort=1&startMessage=1&mailbox=$urlMailbox\" TARGET=\"right\"><IMG SRC=\"../images/up_pointer.gif\" BORDER=0></A></TD>\n";
196 else if ($sort == 1)
197 echo " <A HREF=\"right_main.php?sort=0&startMessage=1&mailbox=$urlMailbox\" TARGET=\"right\"><IMG SRC=\"../images/down_pointer.gif\" BORDER=0></A></TD>\n";
198 else
199 echo " <A HREF=\"right_main.php?sort=0&startMessage=1&mailbox=$urlMailbox\" TARGET=\"right\"><IMG SRC=\"../images/sort_none.gif\" BORDER=0></A></TD>\n";
200 /** SUBJECT HEADER **/
201 echo " <TD WIDTH=*><FONT FACE=\"Arial,Helvetica\"><B>Subject</B></FONT>\n";
202 if ($sort == 4)
203 echo " <A HREF=\"right_main.php?sort=5&startMessage=1&mailbox=$urlMailbox\" TARGET=\"right\"><IMG SRC=\"../images/up_pointer.gif\" BORDER=0></A></TD>\n";
204 else if ($sort == 5)
205 echo " <A HREF=\"right_main.php?sort=4&startMessage=1&mailbox=$urlMailbox\" TARGET=\"right\"><IMG SRC=\"../images/down_pointer.gif\" BORDER=0></A></TD>\n";
206 else
207 echo " <A HREF=\"right_main.php?sort=5&startMessage=1&mailbox=$urlMailbox\" TARGET=\"right\"><IMG SRC=\"../images/sort_none.gif\" BORDER=0></A></TD>\n";
208 echo "</TR>";
209
210 // loop through and display the info for each message.
211 $t = 0; // $t is used for the checkbox number
212 if ($numMessages == 0) { // if there's no messages in this folder
213 echo "<TR><TD BGCOLOR=FFFFFF COLSPAN=4><CENTER><BR><B>THIS FOLDER IS EMPTY</B><BR>&nbsp</CENTER></TD></TR>";
214 } else if ($startMessage == $endMessage) { // if there's only one message in the box, handle it different.
215 $i = $startMessage - 1;
216 printMessageInfo($imapConnection, $t, $msgs[$i]["ID"], $msgs[$i]["FROM"], $msgs[$i]["SUBJECT"], $msgs[$i]["DATE_STRING"], $msgs[$i]["FLAG_ANSWERED"], $msgs[$i]["FLAG_SEEN"]);
217 } else {
218 for ($i = $startMessage - 1;$i <= $endMessage - 1; $i++) {
219 printMessageInfo($imapConnection, $t, $msgs[$i]["ID"], $msgs[$i]["FROM"], $msgs[$i]["SUBJECT"], $msgs[$i]["DATE_STRING"], $msgs[$i]["FLAG_ANSWERED"], $msgs[$i]["FLAG_SEEN"]);
220 $t++;
221 }
222 }
223 echo "</TABLE>";
224
225 /** The "DELETE" button */
226 echo "<TR><TD BGCOLOR=DCDCDC>";
227 echo "<INPUT TYPE=SUBMIT VALUE=\"Delete Selected\">";
228 if (($move_to_trash == true) && ($mailbox == $trash_folder))
229 echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<A HREF=\"empty_trash.php\">Empty Trash</A></NOBR>";
230 echo "</TD></TR>";
231
232 echo "</FORM></TABLE>\n";
233 echo "</TD></TR>\n";
234
235 echo "<TR BGCOLOR=FFFFFF><TD>";
236 if (($nextGroup <= $numMessages) && ($prevGroup >= 0)) {
237 echo "<A HREF=\"right_main.php?sort=$sort&startMessage=$prevGroup&mailbox=$urlMailbox\" TARGET=\"right\"><FONT FACE=\"Arial,Helvetica\">Previous</FONT></A>\n";
238 echo "<A HREF=\"right_main.php?sort=$sort&startMessage=$nextGroup&mailbox=$urlMailbox\" TARGET=\"right\"><FONT FACE=\"Arial,Helvetica\">Next</FONT></A>\n";
239 }
240 else if (($nextGroup > $numMessages) && ($prevGroup >= 0)) {
241 echo "<A HREF=\"right_main.php?sort=$sort&startMessage=$prevGroup&mailbox=$urlMailbox\" TARGET=\"right\"><FONT FACE=\"Arial,Helvetica\">Previous</FONT></A>\n";
242 echo "<FONT FACE=\"Arial,Helvetica\" COLOR=ACACAC>Next</FONT>\n";
243 }
244 else if (($nextGroup <= $numMessages) && ($prevGroup < 0)) {
245 echo "<FONT FACE=\"Arial,Helvetica\" COLOR=ACACAC>Previous</FONT>\n";
246 echo "<A HREF=\"right_main.php?sort=$sort&startMessage=$nextGroup&mailbox=$urlMailbox\" TARGET=\"right\"><FONT FACE=\"Arial,Helvetica\">Next</FONT></A>\n";
247 }
248 echo "</TD></TR></TABLE>"; /** End of message-list table */
249 }
250 ?>