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