60b208576f71863845ac74202ba2eb622a693abb
[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 $stuff = false;
70 echo " <td bgcolor=$hlt_color width=1% nowrap><b><small>\n";
71 if ($msg["FLAG_ANSWERED"] == true) {
72 echo "A\n";
73 $stuff = true;
74 }
75 if ($msg["TYPE0"] == "multipart") {
76 echo "+\n";
77 $stuff = true;
78 }
79 if (ereg("(1|2)",substr($msg["PRIORITY"],0,1))) {
80 echo "<font color=$color[1]>!</font>\n";
81 $stuff = true;
82 }
83 if ($msg["FLAG_DELETED"]) {
84 echo "<font color=\"$color[1]\">D</font>\n";
85 $stuff = true;
86 }
87
88 if (!$stuff) echo "&nbsp;\n";
89 echo "</small></b></td>\n";
90 break;
91 case 6: # size
92 echo " <td bgcolor=$hlt_color width=1%>$bold".show_readable_size($msg['SIZE'])."$bold_end</td>\n";
93 break;
94 }
95 }
96
97
98 echo "</tr>\n";
99 }
100
101 /**
102 ** This function loops through a group of messages in the mailbox and shows them
103 **/
104 function showMessagesForMailbox($imapConnection, $mailbox, $numMessages, $startMessage, $sort, $color,$show_num, $use_cache) {
105 global $msgs, $msort;
106 global $sent_folder;
107 global $message_highlight_list;
108 global $auto_expunge;
109
110 sqimap_mailbox_expunge($imapConnection, $mailbox);
111 sqimap_mailbox_select($imapConnection, $mailbox);
112
113 if (!$use_cache) {
114 // if it's sorted
115 if ($numMessages >= 1) {
116 if ($sort < 6) {
117 for ($q = 0; $q < $numMessages; $q++) {
118 if($mailbox == $sent_folder)
119 $hdr = sqimap_get_small_header ($imapConnection, $q+1, true);
120 else
121 $hdr = sqimap_get_small_header ($imapConnection, $q+1, false);
122
123 $from[$q] = $hdr->from;
124 $date[$q] = $hdr->date;
125 $subject[$q] = $hdr->subject;
126 $to[$q] = $hdr->to;
127 $priority[$q] = $hdr->priority;
128 $cc[$q] = $hdr->cc;
129 $size[$q] = $hdr->size;
130 $type[$q] = $hdr->type0;
131 $flags[$q] = sqimap_get_flags ($imapConnection, $q+1);
132 $id[$q] = $q + 1;
133 }
134 } else {
135 // if it's not sorted
136 if ($startMessage + ($show_num - 1) < $numMessages) {
137 $endMessage = $startMessage + ($show_num-1);
138 } else {
139 $endMessage = $numMessages;
140 }
141
142 if ($endMessage < $startMessage) {
143 $startMessage = $startMessage - $show_num;
144 if ($startMessage < 1)
145 $startMessage = 1;
146 }
147
148
149 $real_startMessage = $numMessages - $startMessage + 1;
150 $real_endMessage = $numMessages - $startMessage - $show_num;
151
152 $j = 0;
153 for ($q = $real_startMessage; $q >= $real_endMessage; $q--) {
154 if($mailbox == $sent_folder)
155 $hdr = sqimap_get_small_header ($imapConnection, $q, true);
156 else
157 $hdr = sqimap_get_small_header ($imapConnection, $q, false);
158
159 $from[$j] = $hdr->from;
160 $date[$j] = $hdr->date;
161 $subject[$j] = $hdr->subject;
162 $to[$j] = $hdr->to;
163 $priority[$j] = $hdr->priority;
164 $cc[$j] = $hdr->cc;
165 $size[$j] = $hdr->size;
166 $type[$j] = $hdr->type0;
167 $flags[$j] = sqimap_get_flags ($imapConnection, $q);
168 $id[$j] = $q;
169 $j++;
170 }
171 }
172 }
173
174 $j = 0;
175 if ($sort == 6) {
176 $end = $startMessage + $show_num - 1;
177 } else {
178 $end = $numMessages;
179 }
180 while ($j < $end) {
181 $date[$j] = ereg_replace(" ", " ", $date[$j]);
182 $tmpdate = explode(" ", trim($date[$j]));
183
184 $messages[$j]["TIME_STAMP"] = getTimeStamp($tmpdate);
185 $messages[$j]["DATE_STRING"] = getDateString($messages[$j]["TIME_STAMP"]);
186 $messages[$j]["ID"] = $id[$j];
187 $messages[$j]["FROM"] = decodeHeader($from[$j]);
188 $messages[$j]["FROM-SORT"] = strtolower(sqimap_find_displayable_name(decodeHeader($from[$j])));
189 $messages[$j]["SUBJECT"] = decodeHeader($subject[$j]);
190 $messages[$j]["SUBJECT-SORT"] = strtolower(decodeHeader($subject[$j]));
191 $messages[$j]["TO"] = decodeHeader($to[$j]);
192 $messages[$j]["PRIORITY"] = $priority[$j];
193 $messages[$j]["CC"] = $cc[$j];
194 $messages[$j]["SIZE"] = $size[$j];
195 $messages[$j]["TYPE0"] = $type[$j];
196
197 # fix SUBJECT-SORT to remove Re:
198 $re_abbr = # Add more here!
199 "vedr|sv|" . # Danish
200 "re|aw"; # English
201 if (eregi("^($re_abbr):[ ]*(.*)$", $messages[$j]['SUBJECT-SORT'], $regs))
202 $messages[$j]['SUBJECT-SORT'] = $regs[2];
203
204 $num = 0;
205 while ($num < count($flags[$j])) {
206 if ($flags[$j][$num] == "Deleted") {
207 $messages[$j]["FLAG_DELETED"] = true;
208 }
209 elseif ($flags[$j][$num] == "Answered") {
210 $messages[$j]["FLAG_ANSWERED"] = true;
211 }
212 elseif ($flags[$j][$num] == "Seen") {
213 $messages[$j]["FLAG_SEEN"] = true;
214 }
215 elseif ($flags[$j][$num] == "Flagged") {
216 $messages[$j]["FLAG_FLAGGED"] = true;
217 }
218 $num++;
219 }
220 $j++;
221 }
222
223 /* Only ignore messages flagged as deleted if we are using a
224 * trash folder or auto_expunge */
225 if (($move_to_trash || $auto_expunge) && $sort != 6)
226 {
227 /** Find and remove the ones that are deleted */
228 $i = 0;
229 $j = 0;
230 while ($j < $numMessages) {
231 if ($messages[$j]["FLAG_DELETED"] == true) {
232 $j++;
233 continue;
234 }
235 $msgs[$i] = $messages[$j];
236
237 $i++;
238 $j++;
239 }
240 $numMessages = $i;
241 } else {
242 $msgs = $messages;
243 }
244 }
245
246 // There's gotta be messages in the array for it to sort them.
247 if ($numMessages > 0 && ! $use_cache) {
248 /** 0 = Date (up) 4 = Subject (up)
249 ** 1 = Date (dn) 5 = Subject (dn)
250 ** 2 = Name (up)
251 ** 3 = Name (dn)
252 **/
253 session_unregister("msgs");
254 if (($sort == 0) || ($sort == 1))
255 $msort = array_cleave ($msgs, "TIME_STAMP");
256 if (($sort == 2) || ($sort == 3))
257 $msort = array_cleave ($msgs, "FROM-SORT");
258 if (($sort == 4) || ($sort == 5))
259 $msort = array_cleave ($msgs, "SUBJECT-SORT");
260 if ($sort == 6)
261 $msort = $msgs;
262
263 if ($sort < 6) {
264 if($sort % 2) {
265 asort($msort);
266 } else {
267 arsort($msort);
268 }
269 }
270 session_register("msort");
271 }
272 displayMessageArray($imapConnection, $numMessages, $startMessage, $msgs, $msort, $mailbox, $sort, $color,$show_num);
273 session_register("msgs");
274 }
275
276 // generic function to convert the msgs array into an HTML table
277 function displayMessageArray($imapConnection, $numMessages, $startMessage, &$msgs, $msort, $mailbox, $sort, $color,$show_num) {
278 global $folder_prefix, $sent_folder;
279 global $imapServerAddress;
280 global $index_order;
281
282 // if cache isn't already set, do it now
283 if (!session_is_registered("msgs"))
284 session_register("msgs");
285 if (!session_is_registered("msort"))
286 session_register("msort");
287
288
289 if ($startMessage + ($show_num - 1) < $numMessages) {
290 $endMessage = $startMessage + ($show_num-1);
291 } else {
292 $endMessage = $numMessages;
293 }
294
295 if ($endMessage < $startMessage) {
296 $startMessage = $startMessage - $show_num;
297 if ($startMessage < 1)
298 $startMessage = 1;
299 }
300
301 $nextGroup = $startMessage + $show_num;
302 $prevGroup = $startMessage - $show_num;
303 $urlMailbox = urlencode($mailbox);
304
305 do_hook("mailbox_index_before");
306
307 $Message = '';
308 if ($startMessage < $endMessage) {
309 $Message = _("Viewing messages") ." <B>$startMessage</B> ". _("to") ." <B>$endMessage</B> ($numMessages " . _("total") . ")\n";
310 } elseif ($startMessage == $endMessage) {
311 $Message = _("Viewing message") ." <B>$startMessage</B> ($numMessages " . _("total") . ")\n";
312 }
313
314 $More = '';
315 if ($sort == 6) {
316 $use = 0;
317 } else {
318 $use = 1;
319 }
320 if (($nextGroup <= $numMessages) && ($prevGroup >= 0)) {
321 $More = "<A HREF=\"right_main.php?use_mailbox_cache=$use&startMessage=$prevGroup&mailbox=$urlMailbox\" TARGET=\"right\">". _("Previous") ."</A> | \n";
322 $More .= "<A HREF=\"right_main.php?use_mailbox_cache=$use&&startMessage=$nextGroup&mailbox=$urlMailbox\" TARGET=\"right\">". _("Next") ."</A>\n";
323 }
324 elseif (($nextGroup > $numMessages) && ($prevGroup >= 0)) {
325 $More = "<A HREF=\"right_main.php?use_mailbox_cache=$use&startMessage=$prevGroup&mailbox=$urlMailbox\" TARGET=\"right\">". _("Previous") ."</A> | \n";
326 $More .= "<FONT COLOR=\"$color[9]\">"._("Next")."</FONT>\n";
327 }
328 elseif (($nextGroup <= $numMessages) && ($prevGroup < 0)) {
329 $More = "<FONT COLOR=\"$color[9]\">"._("Previous")."</FONT> | \n";
330 $More .= "<A HREF=\"right_main.php?use_mailbox_cache=$use&startMessage=$nextGroup&mailbox=$urlMailbox\" TARGET=\"right\">". _("Next") ."</A>\n";
331 }
332
333 mail_message_listing_beginning($imapConnection,
334 "move_messages.php?msg=$msg&mailbox=$urlMailbox&startMessage=$startMessage",
335 $mailbox, $sort, $Message, $More);
336
337 if ($sort == 6)
338 $endVar = $show_num;
339 else
340 $endVar = $endMessage + 1;
341
342 // loop through and display the info for each message.
343 $t = 0; // $t is used for the checkbox number
344 if ($numMessages == 0) { // if there's no messages in this folder
345 echo "<TR><TD BGCOLOR=\"$color[4]\" COLSPAN=" . count($index_order);
346 echo "><CENTER><BR><B>". _("THIS FOLDER IS EMPTY") ."</B><BR>&nbsp;</CENTER></TD></TR>";
347 } elseif ($startMessage == $endMessage) { // if there's only one message in the box, handle it different.
348 if ($sort != 6)
349 $i = $startMessage;
350 else
351 $i = 1;
352 reset($msort);
353 do {
354 $key = key($msort);
355 next($msort);
356 $k++;
357 } while (isset ($key) && ($k < $i));
358 printMessageInfo($imapConnection, $t, $i, $key, $mailbox, $sort, $startMessage, 0, 0);
359 } else {
360 if ($sort != 6)
361 $i = $startMessage;
362 else
363 $i = 1;
364 reset($msort);
365 do {
366 $key = key($msort);
367 next($msort);
368 $k++;
369 } while (isset ($key) && ($k < $i));
370
371 do {
372 printMessageInfo($imapConnection, $t, $i, $key, $mailbox, $sort, $startMessage, 0, 0);
373 $key = key($msort);
374 $t++;
375 $i++;
376 next($msort);
377 } while ($i < $endVar);
378 }
379 echo "</TABLE>";
380
381 echo "</td></tr>\n";
382
383 if ($More)
384 {
385 echo "<TR BGCOLOR=\"$color[4]\"><TD>$More</td></tr>\n";
386 }
387 echo "</table>"; /** End of message-list table */
388
389 do_hook("mailbox_index_after");
390 }
391
392 /* Displays the standard message list header.
393 * To finish the table, you need to do a "</table></table>";
394 * $moveURL is the URL to submit the delete/move form to
395 * $mailbox is the current mailbox
396 * $sort is the current sorting method (-1 for no sorting available [searches])
397 * $Message is a message that is centered on top of the list
398 * $More is a second line that is left aligned
399 */
400 function mail_message_listing_beginning($imapConnection, $moveURL,
401 $mailbox = '', $sort = -1, $Message = '', $More = '')
402 {
403 global $color, $index_order, $auto_expunge, $move_to_trash;
404
405 /** This is the beginning of the message list table. It wraps around all messages */
406 echo "<TABLE WIDTH=100% BORDER=0 CELLPADDING=2 CELLSPACING=0>";
407
408 if ($Message)
409 {
410 echo "<TR BGCOLOR=\"$color[4]\"><TD align=center>$Message</td></tr>\n";
411 }
412
413 if ($More)
414 {
415 echo "<TR BGCOLOR=\"$color[4]\"><TD>$More</td></tr>\n";
416 }
417
418 /** The delete and move options */
419 echo "<TR><TD BGCOLOR=\"$color[0]\">";
420
421 echo "\n\n\n<FORM name=messageList method=post action=\"$moveURL\">\n";
422 echo "<TABLE BGCOLOR=\"$color[0]\" COLS=2 BORDER=0 cellpadding=0 cellspacing=0 width=100%>\n";
423 echo " <TR>\n";
424 echo " <TD WIDTH=60% ALIGN=LEFT VALIGN=CENTER>\n";
425 echo " <NOBR><SMALL>". _("Move selected to:") ."</SMALL>";
426 echo " <TT><SMALL><SELECT NAME=\"targetMailbox\">";
427
428 $boxes = sqimap_mailbox_list($imapConnection);
429 for ($i = 0; $i < count($boxes); $i++) {
430 if ($boxes[$i]["flags"][0] != "noselect" &&
431 $boxes[$i]["flags"][1] != "noselect" &&
432 $boxes[$i]["flags"][2] != "noselect") {
433 $box = $boxes[$i]["unformatted"];
434 $box2 = replace_spaces($boxes[$i]["formatted"]);
435 echo " <OPTION VALUE=\"$box\">$box2</option>\n";
436 }
437 }
438 echo " </SELECT></SMALL></TT>";
439 echo " <SMALL><INPUT TYPE=SUBMIT NAME=\"moveButton\" VALUE=\"". _("Move") ."\"></SMALL></NOBR>\n";
440 echo " </TD>\n";
441 echo " <TD WIDTH=40% ALIGN=RIGHT>\n";
442 if (! $move_to_trash && ! $auto_expunge) {
443 echo " <NOBR><SMALL><INPUT TYPE=SUBMIT NAME=\"expungeButton\" VALUE=\"". _("Expunge") ."\">&nbsp;". _("mailbox") ."</SMALL></NOBR>&nbsp;&nbsp;\n";
444 }
445 echo " <NOBR><SMALL><INPUT TYPE=SUBMIT VALUE=\"". _("Delete") ."\">&nbsp;". _("checked messages") ."</SMALL></NOBR>\n";
446 echo " </TD>\n";
447 echo " </TR>\n";
448
449 echo "</TABLE>\n";
450 do_hook("mailbox_form_before");
451 echo "</TD></TR>";
452
453 echo "<TR><TD BGCOLOR=\"$color[0]\">";
454 echo "<TABLE WIDTH=100% BORDER=0 CELLPADDING=2 CELLSPACING=1 BGCOLOR=\"$color[0]\">";
455 echo "<TR BGCOLOR=\"$color[5]\" ALIGN=\"center\">";
456
457 $urlMailbox=urlencode($mailbox);
458
459 // Print the headers
460 for ($i=1; $i <= count($index_order); $i++) {
461 switch ($index_order[$i]) {
462 case 1: # checkbox
463 case 5: # flags
464 echo " <TD WIDTH=1%><B>&nbsp;</B></TD>";
465 break;
466
467 case 2: # from
468 if ($mailbox == $sent_folder)
469 echo " <TD WIDTH=30%><B>". _("To") ."</B>";
470 else
471 echo " <TD WIDTH=30%><B>". _("From") ."</B>";
472
473 if ($sort == 2)
474 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";
475 elseif ($sort == 3)
476 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";
477 elseif ($sort != -1)
478 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";
479 break;
480
481 case 3: # date
482 echo " <TD nowrap WIDTH=1%><B>". _("Date") ."</B>";
483 if ($sort == 0)
484 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";
485 elseif ($sort == 1)
486 echo " <A HREF=\"right_main.php?newsort=6&startMessage=1&mailbox=$urlMailbox\" TARGET=\"right\"><IMG SRC=\"../images/down_pointer.gif\" BORDER=0></A></TD>\n";
487 elseif ($sort == 6)
488 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";
489 elseif ($sort != -1)
490 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";
491 break;
492
493 case 4: # subject
494 echo " <TD WIDTH=%><B>". _("Subject") ."</B>\n";
495 if ($sort == 4)
496 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";
497 elseif ($sort == 5)
498 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";
499 elseif ($sort != -1)
500 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";
501 break;
502
503 case 6: # size
504 echo " <TD WIDTH=1%><b>" . _("Size")."</b></TD>\n";
505 break;
506 }
507 }
508 echo "</TR>\n";
509 }
510 ?>