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