fixed problem with usort getting bad data in some instances
[squirrelmail.git] / functions / mailbox_display.php
CommitLineData
59177427 1<?php
7c1b84d9 2
3302d0d4 3 /**
a09387f4 4 ** mailbox_display.php
3302d0d4 5 **
6 ** This contains functions that display mailbox information, such as the
7 ** table row that has sender, date, subject, etc...
8 **
9 **/
a4c2cd49 10
d068c0ec 11 $mailbox_info = true;
12
af9404d7 13 function printMessageInfo($imapConnection, $t, $i, $key, $mailbox, $sort, $startMessage) {
14 global $color, $msgs, $msort;
cbdc5621 15 global $sent_folder;
16
af9404d7 17 $msg = $msgs[$key];
d92b6f31 18
7151188f 19 $senderName = $msg["FROM"];
05207a68 20 $urlMailbox = urlencode($mailbox);
7151188f 21 $subject = trim(stripslashes($msg["SUBJECT"]));
926da13e 22 echo "<TR>\n";
7151188f 23
24 if ($msg["FLAG_FLAGGED"] == true) { $flag = "<font color=$color[2]>"; $flag_end = "</font>"; }
25 if ($msg["FLAG_SEEN"] == false) { $bold = "<b>"; $bold_end = "</b>"; }
cbdc5621 26 if ($mailbox == $sent_folder) { $italic = "<i>"; $italic_end = "</i>"; }
7151188f 27
af9404d7 28 echo " <td width=1% align=center><input type=checkbox name=\"msg[$t]\" value=".$msg["ID"]."></TD>\n";
cbdc5621 29 echo " <td width=30%>$italic$bold$flag$senderName$flag_end$bold_end$italic_end</td>\n";
7151188f 30 echo " <td nowrap width=1%><center>$bold$flag".$msg["DATE_STRING"]."$flag_end$bold_end</center></td>\n";
be8e07f8 31 if ($msg["FLAG_ANSWERED"] == true) echo " <td width=1%><b><small>A</small></b></td>";
32 else echo " <td width=1%>&nbsp;</td>";
33 echo " <td width=%>$bold<a href=\"read_body.php?mailbox=$urlMailbox&passed_id=".$msg["ID"]."&startMessage=$startMessage&show_more=0\">$flag$subject$flag_end</a>$bold_end</td>\n";
7151188f 34
35 echo "</tr>\n";
926da13e 36 }
37
38 /**
39 ** This function loops through a group of messages in the mailbox and shows them
40 **/
5b6ae78a 41 function showMessagesForMailbox($imapConnection, $mailbox, $numMessages, $startMessage, $sort, $color,$show_num, $use_cache) {
42 global $msgs, $msort;
cbdc5621 43 global $sent_folder;
61a4ac35 44
5b6ae78a 45 if (!$use_cache) {
46 if ($numMessages >= 1) {
15a9cd31 47 for ($q = 0; $q < $numMessages; $q++) {
4bbba0d8 48 if ($mailbox == $sent_folder)
49 $hdr = sqimap_get_small_header ($imapConnection, $q+1, true);
50 else
51 $hdr = sqimap_get_small_header ($imapConnection, $q+1, false);
52
53 $from[$q] = $hdr->from;
54 $date[$q] = $hdr->date;
55 $subject[$q] = $hdr->subject;
15a9cd31 56
5b6ae78a 57 $flags[$q] = sqimap_get_flags ($imapConnection, $q+1);
5b10f02a 58 }
5b6ae78a 59 }
60
61 $j = 0;
62 while ($j < $numMessages) {
63 $date[$j] = ereg_replace(" ", " ", $date[$j]);
64 $tmpdate = explode(" ", trim($date[$j]));
65
66 $messages[$j]["TIME_STAMP"] = getTimeStamp($tmpdate);
67 $messages[$j]["DATE_STRING"] = getDateString($messages[$j]["TIME_STAMP"]);
68 $messages[$j]["ID"] = $j+1;
69 $messages[$j]["FROM"] = decodeHeader($from[$j]);
70 $messages[$j]["SUBJECT"] = decodeHeader($subject[$j]);
71
72 $num = 0;
73 while ($num < count($flags[$j])) {
74 if ($flags[$j][$num] == "Deleted") {
75 $messages[$j]["FLAG_DELETED"] = true;
76 }
77 else if ($flags[$j][$num] == "Answered") {
78 $messages[$j]["FLAG_ANSWERED"] = true;
79 }
80 else if ($flags[$j][$num] == "Seen") {
81 $messages[$j]["FLAG_SEEN"] = true;
82 }
83 else if ($flags[$j][$num] == "Flagged") {
84 $messages[$j]["FLAG_FLAGGED"] = true;
85 }
86 $num++;
7151188f 87 }
5b6ae78a 88 $j++;
3302d0d4 89 }
5b6ae78a 90
91 /** Find and remove the ones that are deleted */
92 $i = 0;
93 $j = 0;
94 while ($j < $numMessages) {
95 if ($messages[$j]["FLAG_DELETED"] == true) {
96 $j++;
97 continue;
98 }
99 $msgs[$i] = $messages[$j];
100
101 $i++;
5b10f02a 102 $j++;
5b10f02a 103 }
5b6ae78a 104 $numMessages = $i;
105 }
926da13e 106
4c2d69ac 107 // There's gotta be messages in the array for it to sort them.
5b6ae78a 108 if (($numMessages > 0) && (!$use_cache)) {
0e919368 109 /** 0 = Date (up) 4 = Subject (up)
110 ** 1 = Date (dn) 5 = Subject (dn)
111 ** 2 = Name (up)
112 ** 3 = Name (dn)
113 **/
5e90d34a 114
1c292c82 115 if (($sort == 0) || ($sort == 1))
116 $msort = array_cleave ($msgs, "TIME_STAMP");
117 if (($sort == 2) || ($sort == 3))
118 $msort = array_cleave ($msgs, "FROM");
119 if (($sort == 4) || ($sort == 5))
120 $msort = array_cleave ($msgs, "SUBJECT");
121
5b6ae78a 122 if(($sort % 2) == 1) {
123 asort($msort);
124 } else {
125 arsort($msort);
0e919368 126 }
5b6ae78a 127 session_register("msort");
4c2d69ac 128 }
5b6ae78a 129 displayMessageArray($imapConnection, $numMessages, $startMessage, $msgs, $msort, $mailbox, $sort, $color,$show_num);
9f2215a1 130 }
131
132 // generic function to convert the msgs array into an HTML table
5b6ae78a 133 function displayMessageArray($imapConnection, $numMessages, $startMessage, &$msgs, $msort, $mailbox, $sort, $color,$show_num) {
cbdc5621 134 global $folder_prefix, $sent_folder;
135 global $imapServerAddress;
8e9e8afa 136
9f2215a1 137 // do a check to see if the config stuff has already been included or not
cbdc5621 138// if (!isset($imapServerAddress))
139// include("../config/config.php");
9f2215a1 140
141 // if cache isn't already set, do it now
90033b64 142 if (!session_is_registered("msgs"))
143 session_register("msgs");
5b6ae78a 144 if (!session_is_registered("msort"))
145 session_register("msort");
90033b64 146
9c83f905 147 if ($startMessage + ($show_num - 1) < $numMessages) {
148 $endMessage = $startMessage + ($show_num-1);
926da13e 149 } else {
926da13e 150 $endMessage = $numMessages;
151 }
152
9c83f905 153 $nextGroup = $startMessage + $show_num;
154 $prevGroup = $startMessage - $show_num;
20db5033 155 $urlMailbox = urlencode($mailbox);
926da13e 156
20db5033 157 /** This is the beginning of the message list table. It wraps around all messages */
158 echo "<TABLE WIDTH=100% BORDER=0 CELLPADDING=2 CELLSPACING=1>";
926da13e 159
0d1b4832 160 if ($startMessage < $endMessage) {
f8f9bed9 161 echo "<TR BGCOLOR=\"$color[4]\"><TD>";
aae41ae9 162 echo "<CENTER>". _("Viewing messages ") ."<B>$startMessage</B>". _(" to ") ."<B>$endMessage</B> ($numMessages total)</CENTER>\n";
0d1b4832 163 echo "</TD></TR>\n";
164 } else if ($startMessage == $endMessage) {
5cd6702a 165 echo "<TR BGCOLOR=\"$color[4]\"><TD>";
aae41ae9 166 echo "<CENTER>". _("Viewing message ") ."<B>$startMessage</B> ($numMessages ". _("total") .")</CENTER>\n";
0d1b4832 167 echo "</TD></TR>\n";
168 }
26b6c99c 169
f8f9bed9 170 echo "<TR BGCOLOR=\"$color[4]\"><TD>";
5b10f02a 171 if (($nextGroup <= $numMessages) && ($prevGroup >= 0)) {
e452ce9b 172 echo "<A HREF=\"right_main.php?use_mailbox_cache=1&startMessage=$prevGroup&mailbox=$urlMailbox\" TARGET=\"right\">". _("Previous") ."</A>\n";
173 echo "<A HREF=\"right_main.php?use_mailbox_cache=1&&startMessage=$nextGroup&mailbox=$urlMailbox\" TARGET=\"right\">". _("Next") ."</A>\n";
926da13e 174 }
5b10f02a 175 else if (($nextGroup > $numMessages) && ($prevGroup >= 0)) {
e452ce9b 176 echo "<A HREF=\"right_main.php?use_mailbox_cache=1&startMessage=$prevGroup&mailbox=$urlMailbox\" TARGET=\"right\">". _("Previous") ."</A>\n";
aae41ae9 177 echo "<FONT COLOR=\"$color[9]\">Next</FONT>\n";
3302d0d4 178 }
5b10f02a 179 else if (($nextGroup <= $numMessages) && ($prevGroup < 0)) {
aae41ae9 180 echo "<FONT COLOR=\"$color[9]\">Previous</FONT>\n";
e452ce9b 181 echo "<A HREF=\"right_main.php?use_mailbox_cache=1&startMessage=$nextGroup&mailbox=$urlMailbox\" TARGET=\"right\">". _("Next") ."</A>\n";
926da13e 182 }
20db5033 183 echo "</TD></TR>\n";
926da13e 184
68347a84 185 /** The delete and move options */
f8f9bed9 186 echo "<TR><TD BGCOLOR=\"$color[0]\">";
ed4332d1 187
e452ce9b 188 echo "\n\n\n<FORM name=messageList method=post action=\"move_messages.php?msg=$msg&mailbox=$urlMailbox&startMessage=$startMessage\">";
7ce342dc 189 echo "<TABLE BGCOLOR=\"$color[0]\" COLS=2 BORDER=0>\n";
ed4332d1 190 echo " <TR>\n";
7ce342dc 191 echo " <TD WIDTH=60% ALIGN=LEFT>\n";
aae41ae9 192 echo " <NOBR><SMALL>". _("Move selected to:") ."</SMALL>";
7ce342dc 193 echo " <TT><SMALL><SELECT NAME=\"targetMailbox\">";
194
9f2215a1 195 $boxes = sqimap_mailbox_list($imapConnection);
7ce342dc 196 for ($i = 0; $i < count($boxes); $i++) {
8e9e8afa 197 $box = $boxes[$i]["unformatted"];
198 $box2 = replace_spaces($boxes[$i]["formatted"]);
199 echo " <OPTION VALUE=\"$box\">$box2\n";
dd88d31f 200 }
7ce342dc 201 echo " </SELECT></SMALL></TT>";
aae41ae9 202 echo " <SMALL><INPUT TYPE=SUBMIT NAME=\"moveButton\" VALUE=\"". _("Move") ."\"></SMALL></NOBR>\n";
ed4332d1 203
204 echo " </TD>\n";
7ce342dc 205 echo " <TD WIDTH=40% ALIGN=RIGHT>\n";
aae41ae9 206 echo " <NOBR><SMALL><INPUT TYPE=SUBMIT VALUE=\"". _("Delete") ."\">&nbsp;". _("checked messages") ."</SMALL></NOBR>\n";
ed4332d1 207 echo " </TD>";
208 echo " </TR>\n";
209
ed4332d1 210 echo "</TABLE>\n\n\n";
aa4c3749 211 echo "</TD></TR>";
212
f8f9bed9 213 echo "<TR><TD BGCOLOR=\"$color[0]\">";
214 echo "<TABLE WIDTH=100% BORDER=0 CELLPADDING=2 CELLSPACING=1 BGCOLOR=\"$color[4]\">";
215 echo "<TR BGCOLOR=\"$color[5]\" ALIGN=\"center\">";
c85b38c9 216 echo " <TD WIDTH=1%><B>&nbsp;</B></TD>";
0e919368 217 /** FROM HEADER **/
cbdc5621 218 if ($mailbox == $sent_folder)
219 echo " <TD WIDTH=30%><B>". _("To") ."</B>";
220 else
221 echo " <TD WIDTH=30%><B>". _("From") ."</B>";
222
0e919368 223 if ($sort == 2)
e452ce9b 224 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";
0e919368 225 else if ($sort == 3)
e452ce9b 226 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";
0e919368 227 else
e452ce9b 228 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";
0e919368 229 /** DATE HEADER **/
c85b38c9 230 echo " <TD nowrap WIDTH=1%><B>". _("Date") ."</B>";
926da13e 231 if ($sort == 0)
e452ce9b 232 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";
0e919368 233 else if ($sort == 1)
e452ce9b 234 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";
0e919368 235 else
e452ce9b 236 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";
be8e07f8 237 echo " <TD WIDTH=1%>&nbsp;</TD>\n";
0e919368 238 /** SUBJECT HEADER **/
aae41ae9 239 echo " <TD WIDTH=%><B>". _("Subject") ."</B>\n";
0e919368 240 if ($sort == 4)
e452ce9b 241 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";
0e919368 242 else if ($sort == 5)
e452ce9b 243 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";
0e919368 244 else
e452ce9b 245 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";
be8e07f8 246
926da13e 247 echo "</TR>";
248
d29aac0e 249
926da13e 250 // loop through and display the info for each message.
aa4c3749 251 $t = 0; // $t is used for the checkbox number
4c2d69ac 252 if ($numMessages == 0) { // if there's no messages in this folder
15a9cd31 253 echo "<TR><TD BGCOLOR=\"$color[4]\" COLSPAN=5><CENTER><BR><B>". _("THIS FOLDER IS EMPTY") ."</B><BR>&nbsp</CENTER></TD></TR>";
4c2d69ac 254 } else if ($startMessage == $endMessage) { // if there's only one message in the box, handle it different.
3e054c43 255 $i = $startMessage - 1;
af9404d7 256 reset($msort);
257 do {
258 $key = key($msort);
259 next($msort);
260 $k++;
261 } while (isset ($key) && ($k < $i));
262 printMessageInfo($imapConnection, $t, $i, $key, $mailbox, $sort, $startMessage);
4c2d69ac 263 } else {
5b6ae78a 264 $i = $startMessage;
265 reset($msort);
266 do {
267 $key = key($msort);
268 next($msort);
269 $k++;
270 } while (isset ($key) && ($k < $i));
271
272 do {
af9404d7 273 printMessageInfo($imapConnection, $t, $i, $key, $mailbox, $sort, $startMessage);
5b6ae78a 274 $key = key($msort);
4c2d69ac 275 $t++;
5b6ae78a 276 $i++;
277 next($msort);
278 } while ($i < ($endMessage+1));
926da13e 279 }
68347a84 280 echo "</FORM></TABLE>";
aa4c3749 281
68347a84 282 echo "</TABLE>\n";
4c2d69ac 283 echo "</TD></TR>\n";
284
f8f9bed9 285 echo "<TR BGCOLOR=\"$color[4]\"><TD>";
20db5033 286 if (($nextGroup <= $numMessages) && ($prevGroup >= 0)) {
e452ce9b 287 echo "<A HREF=\"right_main.php?use_mailbox_cache=1&startMessage=$prevGroup&mailbox=$urlMailbox\" TARGET=\"right\">" . _("Previous") . "</A>\n";
288 echo "<A HREF=\"right_main.php?use_mailbox_cache=1&startMessage=$nextGroup&mailbox=$urlMailbox\" TARGET=\"right\">" . _("Next") . "</A>\n";
20db5033 289 }
290 else if (($nextGroup > $numMessages) && ($prevGroup >= 0)) {
e452ce9b 291 echo "<A HREF=\"right_main.php?use_mailbox_cache=1&startMessage=$prevGroup&mailbox=$urlMailbox\" TARGET=\"right\">" . _("Previous") . "</A>\n";
aae41ae9 292 echo "<FONT COLOR=\"$color[9]\">" . _("Next") . "</FONT>\n";
20db5033 293 }
294 else if (($nextGroup <= $numMessages) && ($prevGroup < 0)) {
aae41ae9 295 echo "<FONT COLOR=\"$color[9]\">Previous</FONT>\n";
e452ce9b 296 echo "<A HREF=\"right_main.php?use_mailbox_cache=1&startMessage=$nextGroup&mailbox=$urlMailbox\" TARGET=\"right\">" . _("Next") . "</A>\n";
20db5033 297 }
926da13e 298 echo "</TD></TR></TABLE>"; /** End of message-list table */
591000ec 299
3302d0d4 300 }
26b6c99c 301?>