- improved date parsing
[squirrelmail.git] / functions / mailbox_display.php
1 <?php
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 $mailbox_display_php = true;
12
13 function printMessageInfo($imapConnection, $t, $i, $key, $mailbox, $sort, $startMessage, $where, $what) {
14 global $color, $msgs, $msort;
15 global $sent_folder;
16 global $message_highlight_list;
17 global $index_order;
18
19 $msg = $msgs[$key];
20
21 $senderName = sqimap_find_displayable_name($msg["FROM"]);
22 $urlMailbox = urlencode($mailbox);
23 $subject = trim($msg["SUBJECT"]);
24 if ($subject == "")
25 $subject = _("(no subject)");
26
27 echo "<TR>\n";
28
29 if ($msg["FLAG_FLAGGED"] == true) { $flag = "<font color=$color[2]>"; $flag_end = "</font>"; }
30 if ($msg["FLAG_SEEN"] == false) { $bold = "<b>"; $bold_end = "</b>"; }
31 if ($mailbox == $sent_folder) { $italic = "<i>"; $italic_end = "</i>"; }
32
33 for ($i=0; $i < count($message_highlight_list); $i++) {
34 if (trim($message_highlight_list[$i]["value"]) != "") {
35 if ($message_highlight_list[$i]["match_type"] == "to_cc") {
36 if (strpos("^^".strtolower($msg["TO"]), strtolower($message_highlight_list[$i]["value"])) || strpos("^^".strtolower($msg["CC"]), strtolower($message_highlight_list[$i]["value"]))) {
37 $hlt_color = $message_highlight_list[$i]["color"];
38 continue;
39 }
40 } else if (strpos("^^".strtolower($msg[strtoupper($message_highlight_list[$i]["match_type"])]),strtolower($message_highlight_list[$i]["value"]))) {
41 $hlt_color = $message_highlight_list[$i]["color"];
42 continue;
43 }
44 }
45 }
46
47 if (!$hlt_color)
48 $hlt_color = $color[4];
49
50 if ($where && $what) {
51 $search_stuff = "&where=".urlencode($where)."&what=".urlencode($what);
52 }
53
54 for ($i=1; $i <= count($index_order); $i++) {
55 switch ($index_order[$i]) {
56 case 1: # checkbox
57 echo " <td width=1% bgcolor=$hlt_color align=center><input type=checkbox name=\"msg[$t]\" value=".$msg["ID"]."$checked></TD>\n";
58 break;
59 case 2: # from
60 echo " <td width=30% bgcolor=$hlt_color>$italic$bold$flag$senderName$flag_end$bold_end$italic_end</td>\n";
61 break;
62 case 3: # date
63 echo " <td nowrap width=1% bgcolor=$hlt_color><center>$bold$flag".$msg["DATE_STRING"]."$flag_end$bold_end</center></td>\n";
64 break;
65 case 4: # subject
66 echo " <td bgcolor=$hlt_color>$bold<a href=\"read_body.php?mailbox=$urlMailbox&passed_id=".$msg["ID"]."&startMessage=$startMessage&show_more=0$search_stuff\">$flag$subject$flag_end</a>$bold_end</td>\n";
67 break;
68 case 5: # flags
69 if ($msg["FLAG_ANSWERED"] == true) echo " <td bgcolor=$hlt_color width=1%><b><small>A</small></b></td>\n";
70 elseif (ereg("(1|2)",substr($msg["PRIORITY"],0,1))) echo " <td bgcolor=$hlt_color width=1%><b><small><font color=$color[1]>!</font></small></b></td>\n";
71 else echo " <td bgcolor=$hlt_color width=1%>&nbsp;</td>\n";
72 break;
73 case 6: # size
74 echo " <td bgcolor=$hlt_color width=1%>$bold".$msg["SIZE"]."k$bold_end</td>\n";
75 break;
76 }
77 }
78
79
80 echo "</tr>\n";
81 }
82
83 /**
84 ** This function loops through a group of messages in the mailbox and shows them
85 **/
86 function showMessagesForMailbox($imapConnection, $mailbox, $numMessages, $startMessage, $sort, $color,$show_num, $use_cache) {
87 global $msgs, $msort;
88 global $sent_folder;
89 global $message_highlight_list;
90 global $auto_expunge;
91
92 if ($auto_expunge) {
93 sqimap_mailbox_expunge($imapConnection, $mailbox);
94 sqimap_mailbox_select($imapConnection, $mailbox);
95 }
96
97 if (!$use_cache) {
98 if ($numMessages >= 1) {
99 for ($q = 0; $q < $numMessages; $q++) {
100 if ($mailbox == $sent_folder)
101 $hdr = sqimap_get_small_header ($imapConnection, $q+1, true);
102 else
103 $hdr = sqimap_get_small_header ($imapConnection, $q+1, false);
104
105 $from[$q] = $hdr->from;
106 $date[$q] = $hdr->date;
107 $subject[$q] = $hdr->subject;
108 $to[$q] = $hdr->to;
109 $priority[$q] = $hdr->priority;
110 $cc[$q] = $hdr->cc;
111 $size[$q] = $hdr->size;
112
113 $flags[$q] = sqimap_get_flags ($imapConnection, $q+1);
114 }
115 }
116
117 $j = 0;
118 while ($j < $numMessages) {
119 $date[$j] = ereg_replace(" ", " ", $date[$j]);
120 $tmpdate = explode(" ", trim($date[$j]));
121
122 $messages[$j]["TIME_STAMP"] = getTimeStamp($tmpdate);
123 $messages[$j]["DATE_STRING"] = getDateString($messages[$j]["TIME_STAMP"]);
124 $messages[$j]["ID"] = $j+1;
125 $messages[$j]["FROM"] = decodeHeader($from[$j]);
126 $messages[$j]["FROM-SORT"] = strtolower(sqimap_find_displayable_name(decodeHeader($from[$j])));
127 $messages[$j]["SUBJECT"] = decodeHeader($subject[$j]);
128 $messages[$j]["SUBJECT-SORT"] = strtolower(decodeHeader($subject[$j]));
129 $messages[$j]["TO"] = decodeHeader($to[$j]);
130 $messages[$j]["PRIORITY"] = $priority[$j];
131 $messages[$j]["CC"] = $cc[$j];
132 $messages[$j]["SIZE"] = $size[$j];
133
134 # fix SUBJECT-SORT to remove Re:
135 if (substr($messages[$j]["SUBJECT-SORT"], 0, 3) == "re:" ||
136 substr($messages[$j]["SUBJECT-SORT"], 0, 3) == "aw:")
137 $messages[$j]["SUBJECT-SORT"] = trim(substr($messages[$j]["SUBJECT-SORT"], 3));
138
139 $num = 0;
140 while ($num < count($flags[$j])) {
141 if ($flags[$j][$num] == "Deleted") {
142 $messages[$j]["FLAG_DELETED"] = true;
143 }
144 else if ($flags[$j][$num] == "Answered") {
145 $messages[$j]["FLAG_ANSWERED"] = true;
146 }
147 else if ($flags[$j][$num] == "Seen") {
148 $messages[$j]["FLAG_SEEN"] = true;
149 }
150 else if ($flags[$j][$num] == "Flagged") {
151 $messages[$j]["FLAG_FLAGGED"] = true;
152 }
153 $num++;
154 }
155 $j++;
156 }
157
158 /** Find and remove the ones that are deleted */
159 $i = 0;
160 $j = 0;
161 while ($j < $numMessages) {
162 if ($messages[$j]["FLAG_DELETED"] == true) {
163 $j++;
164 continue;
165 }
166 $msgs[$i] = $messages[$j];
167
168 $i++;
169 $j++;
170 }
171 $numMessages = $i;
172 }
173
174 // There's gotta be messages in the array for it to sort them.
175 if (($numMessages > 0) && (!$use_cache)) {
176 /** 0 = Date (up) 4 = Subject (up)
177 ** 1 = Date (dn) 5 = Subject (dn)
178 ** 2 = Name (up)
179 ** 3 = Name (dn)
180 **/
181 session_unregister("msgs");
182 if (($sort == 0) || ($sort == 1))
183 $msort = array_cleave ($msgs, "TIME_STAMP");
184 if (($sort == 2) || ($sort == 3))
185 $msort = array_cleave ($msgs, "FROM-SORT");
186 if (($sort == 4) || ($sort == 5))
187 $msort = array_cleave ($msgs, "SUBJECT-SORT");
188
189 if(($sort % 2) == 1) {
190 asort($msort);
191 } else {
192 arsort($msort);
193 }
194 session_register("msort");
195 }
196 displayMessageArray($imapConnection, $numMessages, $startMessage, $msgs, $msort, $mailbox, $sort, $color,$show_num);
197 session_register("msgs");
198 }
199
200 // generic function to convert the msgs array into an HTML table
201 function displayMessageArray($imapConnection, $numMessages, $startMessage, &$msgs, $msort, $mailbox, $sort, $color,$show_num) {
202 global $folder_prefix, $sent_folder;
203 global $imapServerAddress;
204 global $index_order;
205
206 // if cache isn't already set, do it now
207 if (!session_is_registered("msgs"))
208 session_register("msgs");
209 if (!session_is_registered("msort"))
210 session_register("msort");
211
212
213 if ($startMessage + ($show_num - 1) < $numMessages) {
214 $endMessage = $startMessage + ($show_num-1);
215 } else {
216 $endMessage = $numMessages;
217 }
218
219 if ($endMessage < $startMessage) {
220 $startMessage = $startMessage - $show_num;
221 if ($startMessage < 1)
222 $startMessage = 1;
223 }
224
225 $nextGroup = $startMessage + $show_num;
226 $prevGroup = $startMessage - $show_num;
227 $urlMailbox = urlencode($mailbox);
228
229 do_hook("mailbox_index_before");
230 /** This is the beginning of the message list table. It wraps around all messages */
231 echo "<TABLE WIDTH=100% BORDER=0 CELLPADDING=2 CELLSPACING=0>";
232
233 if ($startMessage < $endMessage) {
234 echo "<TR BGCOLOR=\"$color[4]\"><TD>";
235 echo "<CENTER>". _("Viewing messages") ." <B>$startMessage</B> ". _("to") ." <B>$endMessage</B> ($numMessages " . _("total") . ")</CENTER>\n";
236 echo "</TD></TR>\n";
237 } else if ($startMessage == $endMessage) {
238 echo "<TR BGCOLOR=\"$color[4]\"><TD>";
239 echo "<CENTER>". _("Viewing message") ." <B>$startMessage</B> ($numMessages " . _("total") . ")</CENTER>\n";
240 echo "</TD></TR>\n";
241 }
242
243 echo "<TR BGCOLOR=\"$color[4]\"><TD>";
244 if (($nextGroup <= $numMessages) && ($prevGroup >= 0)) {
245 echo "<A HREF=\"right_main.php?use_mailbox_cache=1&startMessage=$prevGroup&mailbox=$urlMailbox\" TARGET=\"right\">". _("Previous") ."</A> | \n";
246 echo "<A HREF=\"right_main.php?use_mailbox_cache=1&&startMessage=$nextGroup&mailbox=$urlMailbox\" TARGET=\"right\">". _("Next") ."</A>\n";
247 }
248 else if (($nextGroup > $numMessages) && ($prevGroup >= 0)) {
249 echo "<A HREF=\"right_main.php?use_mailbox_cache=1&startMessage=$prevGroup&mailbox=$urlMailbox\" TARGET=\"right\">". _("Previous") ."</A> | \n";
250 echo "<FONT COLOR=\"$color[9]\">"._("Next")."</FONT>\n";
251 }
252 else if (($nextGroup <= $numMessages) && ($prevGroup < 0)) {
253 echo "<FONT COLOR=\"$color[9]\">"._("Previous")."</FONT> | \n";
254 echo "<A HREF=\"right_main.php?use_mailbox_cache=1&startMessage=$nextGroup&mailbox=$urlMailbox\" TARGET=\"right\">". _("Next") ."</A>\n";
255 }
256 echo "</TD></TR>\n";
257
258 /** The delete and move options */
259 echo "<TR><TD BGCOLOR=\"$color[0]\">";
260
261 echo "\n\n\n<FORM name=messageList method=post action=\"move_messages.php?msg=$msg&mailbox=$urlMailbox&startMessage=$startMessage\">";
262 do_hook("mailbox_form_before");
263 echo "<TABLE BGCOLOR=\"$color[0]\" COLS=2 BORDER=0 cellpadding=0 cellspacing=0 width=100%>\n";
264 echo " <TR>\n";
265 echo " <TD WIDTH=60% ALIGN=LEFT VALIGN=CENTER>\n";
266 echo " <NOBR><SMALL>". _("Move selected to:") ."</SMALL>";
267 echo " <TT><SMALL><SELECT NAME=\"targetMailbox\">";
268
269 $boxes = sqimap_mailbox_list($imapConnection);
270 for ($i = 0; $i < count($boxes); $i++) {
271 if ($boxes[$i]["flags"][0] != "noselect" && $boxes[$i]["flags"][1] != "noselect" && $boxes[$i]["flags"][2] != "noselect") {
272 $box = $boxes[$i]["unformatted"];
273 $box2 = replace_spaces($boxes[$i]["formatted"]);
274 echo " <OPTION VALUE=\"$box\">$box2\n";
275 }
276 }
277 echo " </SELECT></SMALL></TT>";
278 echo " <SMALL><INPUT TYPE=SUBMIT NAME=\"moveButton\" VALUE=\"". _("Move") ."\"></SMALL></NOBR>\n";
279
280 echo " </TD>\n";
281 echo " <TD WIDTH=40% ALIGN=RIGHT>\n";
282 echo " <NOBR><SMALL><INPUT TYPE=SUBMIT VALUE=\"". _("Delete") ."\">&nbsp;". _("checked messages") ."</SMALL></NOBR>\n";
283 echo " </TD>";
284 echo " </TR>\n";
285
286 echo "</TABLE>\n\n\n";
287 echo "</TD></TR>";
288
289 echo "<TR><TD BGCOLOR=\"$color[0]\">";
290 echo "<TABLE WIDTH=100% BORDER=0 CELLPADDING=2 CELLSPACING=1 BGCOLOR=\"$color[0]\">";
291 echo "<TR BGCOLOR=\"$color[5]\" ALIGN=\"center\">";
292
293 for ($i=1; $i <= count($index_order); $i++) {
294 switch ($index_order[$i]) {
295 case 1: # checkbox
296 echo " <TD WIDTH=1%><B>&nbsp;</B></TD>";
297 break;
298 case 2: # from
299 if ($mailbox == $sent_folder)
300 echo " <TD WIDTH=30%><B>". _("To") ."</B>";
301 else
302 echo " <TD WIDTH=30%><B>". _("From") ."</B>";
303
304 if ($sort == 2)
305 echo " <A HREF=\"right_main.php?newsort=3&startMessage=1&mailbox=$urlMailbox\" TARGET=\"right\"><IMG SRC=\"../images/up_pointer.gif\" BORDER=0></A></TD>\n";
306 else if ($sort == 3)
307 echo " <A HREF=\"right_main.php?newsort=2&startMessage=1&mailbox=$urlMailbox\" TARGET=\"right\"><IMG SRC=\"../images/down_pointer.gif\" BORDER=0></A></TD>\n";
308 else
309 echo " <A HREF=\"right_main.php?newsort=3&startMessage=1&mailbox=$urlMailbox\" TARGET=\"right\"><IMG SRC=\"../images/sort_none.gif\" BORDER=0></A></TD>\n";
310 break;
311 case 3: # date
312 echo " <TD nowrap WIDTH=1%><B>". _("Date") ."</B>";
313 if ($sort == 0)
314 echo " <A HREF=\"right_main.php?newsort=1&startMessage=1&mailbox=$urlMailbox\" TARGET=\"right\"><IMG SRC=\"../images/up_pointer.gif\" BORDER=0></A></TD>\n";
315 else if ($sort == 1)
316 echo " <A HREF=\"right_main.php?newsort=0&startMessage=1&mailbox=$urlMailbox\" TARGET=\"right\"><IMG SRC=\"../images/down_pointer.gif\" BORDER=0></A></TD>\n";
317 else
318 echo " <A HREF=\"right_main.php?newsort=0&startMessage=1&mailbox=$urlMailbox\" TARGET=\"right\"><IMG SRC=\"../images/sort_none.gif\" BORDER=0></A></TD>\n";
319 break;
320 case 4: # subject
321 echo " <TD WIDTH=%><B>". _("Subject") ."</B>\n";
322 if ($sort == 4)
323 echo " <A HREF=\"right_main.php?newsort=5&startMessage=1&mailbox=$urlMailbox\" TARGET=\"right\"><IMG SRC=\"../images/up_pointer.gif\" BORDER=0></A></TD>\n";
324 else if ($sort == 5)
325 echo " <A HREF=\"right_main.php?newsort=4&startMessage=1&mailbox=$urlMailbox\" TARGET=\"right\"><IMG SRC=\"../images/down_pointer.gif\" BORDER=0></A></TD>\n";
326 else
327 echo " <A HREF=\"right_main.php?newsort=5&startMessage=1&mailbox=$urlMailbox\" TARGET=\"right\"><IMG SRC=\"../images/sort_none.gif\" BORDER=0></A></TD>\n";
328 break;
329 case 5: # flags
330 echo " <TD WIDTH=1%>&nbsp;</TD>\n";
331 break;
332 case 6: # size
333 echo " <TD WIDTH=1%><b>" . _("Size")."</b></TD>\n";
334 break;
335 }
336 }
337 echo "</TR>";
338
339
340
341 // loop through and display the info for each message.
342 $t = 0; // $t is used for the checkbox number
343 if ($numMessages == 0) { // if there's no messages in this folder
344 echo "<TR><TD BGCOLOR=\"$color[4]\" COLSPAN=5><CENTER><BR><B>". _("THIS FOLDER IS EMPTY") ."</B><BR>&nbsp;</CENTER></TD></TR>";
345 } else if ($startMessage == $endMessage) { // if there's only one message in the box, handle it different.
346 $i = $startMessage;
347 reset($msort);
348 do {
349 $key = key($msort);
350 next($msort);
351 $k++;
352 } while (isset ($key) && ($k < $i));
353 printMessageInfo($imapConnection, $t, $i, $key, $mailbox, $sort, $startMessage, 0, 0);
354 } else {
355 $i = $startMessage;
356 reset($msort);
357 do {
358 $key = key($msort);
359 next($msort);
360 $k++;
361 } while (isset ($key) && ($k < $i));
362
363 do {
364 printMessageInfo($imapConnection, $t, $i, $key, $mailbox, $sort, $startMessage, 0, 0);
365 $key = key($msort);
366 $t++;
367 $i++;
368 next($msort);
369 } while ($i < ($endMessage+1));
370 }
371 echo "</FORM></TABLE>";
372
373 echo "</TABLE>\n";
374 echo "</TD></TR>\n";
375
376 echo "<TR BGCOLOR=\"$color[4]\"><TD>";
377 if (($nextGroup <= $numMessages) && ($prevGroup >= 0)) {
378 echo "<A HREF=\"right_main.php?use_mailbox_cache=1&startMessage=$prevGroup&mailbox=$urlMailbox\" TARGET=\"right\">" . _("Previous") . "</A> | \n";
379 echo "<A HREF=\"right_main.php?use_mailbox_cache=1&startMessage=$nextGroup&mailbox=$urlMailbox\" TARGET=\"right\">" . _("Next") . "</A>\n";
380 }
381 else if (($nextGroup > $numMessages) && ($prevGroup >= 0)) {
382 echo "<A HREF=\"right_main.php?use_mailbox_cache=1&startMessage=$prevGroup&mailbox=$urlMailbox\" TARGET=\"right\">" . _("Previous") . "</A> | \n";
383 echo "<FONT COLOR=\"$color[9]\">" . _("Next") . "</FONT>\n";
384 }
385 else if (($nextGroup <= $numMessages) && ($prevGroup < 0)) {
386 echo "<FONT COLOR=\"$color[9]\">" . _("Previous"). "</FONT> | \n";
387 echo "<A HREF=\"right_main.php?use_mailbox_cache=1&startMessage=$nextGroup&mailbox=$urlMailbox\" TARGET=\"right\">" . _("Next") . "</A>\n";
388 }
389 echo "</TD></TR></table>"; /** End of message-list table */
390
391 do_hook("mailbox_index_after");
392 }
393 ?>