Putting in Jon's cleanups (fixed up by me)
[squirrelmail.git] / functions / mailbox_display.php
1 <?php
2
3 /**
4 * mailbox_display.php
5 *
6 * Copyright (c) 1999-2002 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * This contains functions that display mailbox information, such as the
10 * table row that has sender, date, subject, etc...
11 *
12 * $Id$
13 */
14
15 require_once('../functions/strings.php');
16
17 /* Default value for page_selector_max. */
18 define('PG_SEL_MAX', 10);
19
20 function printMessageInfo($imapConnection, $t, $i, $key, $mailbox, $sort,
21 $start_msg, $where, $what){
22 global $checkall,
23 $color, $msgs, $msort,
24 $sent_folder, $draft_folder,
25 $default_use_priority,
26 $message_highlight_list,
27 $index_order,
28 $indent_array, /* indent subject by */
29 $pos, /* Search postion (if any) */
30 $thread_sort_messages, /* thread sorting on/off */
31 $row_count;
32 $color_string = $color[4];
33
34 if ($GLOBALS['alt_index_colors']) {
35 if (!isset($row_count)) {
36 $row_count = 0;
37 }
38 $row_count++;
39 if ($row_count % 2) {
40 if (!isset($color[12])) {
41 $color[12] = '#EAEAEA';
42 }
43 $color_string = $color[12];
44 }
45 }
46 $msg = $msgs[$key];
47
48 /*
49 * This is done in case you're looking into Sent folders,
50 * because you can have multiple receivers.
51 */
52 $senderNames = explode(',', $msg['FROM']);
53 $senderName = '';
54 if (sizeof($senderNames)){
55 foreach ($senderNames as $senderNames_part) {
56 if ($senderName != '') {
57 $senderName .= ', ';
58 }
59 $senderName .= sqimap_find_displayable_name($senderNames_part);
60 }
61 }
62
63 if( $mailbox == 'None' ) {
64 $boxes = sqimap_mailbox_list($imapConnection);
65 $mailbox = $boxes[0]['unformatted'];
66 unset( $boxes );
67 }
68 $urlMailbox = urlencode($mailbox);
69 $subject = processSubject($msg['SUBJECT']);
70 echo "<TR>\n";
71
72 if (isset($msg['FLAG_FLAGGED']) && ($msg['FLAG_FLAGGED'] == true)) {
73 $flag = "<font color=\"$color[2]\">";
74 $flag_end = '</font>';
75 } else {
76 $flag = '';
77 $flag_end = '';
78 }
79 if (!isset($msg['FLAG_SEEN']) || ($msg['FLAG_SEEN'] == false)) {
80 $bold = '<b>';
81 $bold_end = '</b>';
82 } else {
83 $bold = '';
84 $bold_end = '';
85 }
86 if (handleAsSent($mailbox)) {
87 $italic = '<i>';
88 $italic_end = '</i>';
89 } else {
90 $italic = '';
91 $italic_end = '';
92 }
93 if (isset($msg['FLAG_DELETED']) && $msg['FLAG_DELETED']) {
94 $fontstr = "<font color=\"$color[9]\">";
95 $fontstr_end = '</font>';
96 } else {
97 $fontstr = '';
98 $fontstr_end = '';
99 }
100
101 /**
102 * AAAAH! Make my eyes stop bleeding!
103 * Who wrote this?!
104 */
105 if (sizeof($message_highlight_list)){
106 foreach ($message_highlight_list as $message_highlight_list_part) {
107 if (trim($message_highlight_list_part['value']) != '') {
108 if ($message_highlight_list_part['match_type'] == 'to_cc') {
109 if (strstr('^^' . strtolower($msg['TO']),
110 strtolower($message_highlight_list_part['value']))
111 || strstr('^^'.strtolower($msg['CC']),
112 strtolower($message_highlight_list_part['value']))) {
113 $hlt_color = $message_highlight_list_part['color'];
114 continue;
115 }
116 } else
117 if (strstr('^^' . strtolower($msg[strtoupper($message_highlight_list_part['match_type'])]),
118 strtolower($message_highlight_list_part['value']))) {
119 $hlt_color = $message_highlight_list_part['color'];
120 continue;
121 }
122 }
123 }
124 }
125
126 if (!isset($hlt_color)) {
127 $hlt_color = $color_string;
128 }
129
130 if ($where && $what) {
131 if(!isset($pos) || $pos == '') {
132 $pos = '0';
133 }
134 $search_stuff = "&amp;pos=" . urlencode($pos)
135 . "&amp;where=" . urlencode($where) . '&amp;what=' . urlencode($what);
136 } else {
137 $search_stuff = '';
138 }
139
140 $checked = ($checkall == 1) ? ' checked' : '';
141
142 if (sizeof($index_order)){
143 foreach ($index_order as $index_order_part) {
144 switch ($index_order_part) {
145 case 1: /* checkbox */
146 echo " <td bgcolor=\"$hlt_color\" align=center>"
147 . "<input type=checkbox name=\"msg[$t]\" value="
148 . $msg["ID"]."$checked></TD>\n";
149 break;
150 case 2: /* from */
151 echo " <td bgcolor=\"$hlt_color\">$italic$bold$flag$fontstr"
152 . "$senderName$fontstr_end$flag_end$bold_end$italic_end</td>\n";
153 break;
154 case 3: /* date */
155 echo " <td nowrap bgcolor=\"$hlt_color\"><center>$bold$flag$fontstr"
156 . $msg["DATE_STRING"]
157 . "$fontstr_end$flag_end$bold_end</center></td>\n";
158 break;
159 case 4: /* subject */
160 echo " <td bgcolor=\"$hlt_color\">$bold";
161 if ($thread_sort_messages == 1) {
162 echo str_repeat("&nbsp;&nbsp;&nbsp;&nbsp;",$indent_array[$msg["ID"]]);
163 }
164 echo "<a href=\"read_body.php?mailbox=$urlMailbox&amp;passed_id="
165 . $msg["ID"]
166 . "&amp;startMessage=$start_msg&amp;show_more=0$search_stuff\"";
167 do_hook("subject_link");
168 if ($subject != $msg['SUBJECT']) {
169 $title = get_html_translation_table(HTML_SPECIALCHARS);
170 $title = array_flip($title);
171 $title = strtr($msg['SUBJECT'], $title);
172 $title = str_replace('"', "''", $title);
173 echo " title=\"$title\"";
174 }
175 echo ">$flag$subject$flag_end</a>$bold_end</td>\n";
176 break;
177 case 5: /* flags */
178 $stuff = false;
179 echo " <td bgcolor=\"$hlt_color\" align=center nowrap><b><small>\n";
180 if (isset($msg['FLAG_ANSWERED']) && $msg['FLAG_ANSWERED'] == true) {
181 echo _("A") . "\n";
182 $stuff = true;
183 }
184 if ($msg['TYPE0'] == 'multipart') {
185 echo "+\n";
186 $stuff = true;
187 }
188 if ($default_use_priority) {
189 if ( ($msg['PRIORITY'][0] == 1) || ($msg['PRIORITY'][0] == 2) ) {
190 echo "<font color=\"$color[1]\">!</font>\n";
191 $stuff = true;
192 }
193 if ($msg['PRIORITY'][0] == 5) {
194 echo "<font color=\"$color[8]\">?</font>\n";
195 $stuff = true;
196 }
197 }
198 if (isset($msg['FLAG_DELETED']) && $msg['FLAG_DELETED'] == true) {
199 echo "<font color=\"$color[1]\">D</font>\n";
200 $stuff = true;
201 }
202 if (!$stuff) {
203 echo "&nbsp;\n";
204 }
205 echo "</small></b></td>\n";
206 break;
207 case 6: /* size */
208 echo " <td bgcolor=\"$hlt_color\">$bold$fontstr"
209 . show_readable_size($msg['SIZE']) . "$fontstr_end$bold_end</td>\n";
210 break;
211 }
212 }
213 }
214 echo "</tr>\n";
215 }
216
217 /*
218 * This function loops through a group of messages in the mailbox
219 * and shows them to the user.
220 */
221 function showMessagesForMailbox($imapConnection, $mailbox, $num_msgs,
222 $start_msg, $sort, $color, $show_num,
223 $use_cache) {
224 global $msgs, $msort,
225 $sent_folder, $draft_folder,
226 $message_highlight_list,
227 $auto_expunge, $thread_sort_messages,
228 $data_dir, $username;
229 /* If autoexpunge is turned on, then do it now. */
230
231 if ($thread_sort_messages == 1) {
232 $id = get_thread_sort($imapConnection);
233 $sort = 6;
234 if ($start_msg + ($show_num - 1) < $num_msgs) {
235 $end_msg = $start_msg + ($show_num-1);
236 } else {
237 $end_msg = $num_msgs;
238 }
239 $id = array_slice($id, ($start_msg-1), ($end_msg));
240 }
241 if ($auto_expunge == true) {
242 sqimap_mailbox_expunge($imapConnection, $mailbox, false);
243 }
244 sqimap_mailbox_select($imapConnection, $mailbox);
245 $issent = handleAsSent($mailbox);
246 if (!$use_cache) {
247 /* If it is sorted... */
248 if ($num_msgs >= 1) {
249 if ($sort < 6 ) {
250 $id = range(1, $num_msgs);
251 }
252 elseif ($thread_sort_messages != 1) {
253 /* if it's not sorted */
254 if ($start_msg + ($show_num - 1) < $num_msgs){
255 $end_msg = $start_msg + ($show_num - 1);
256 } else {
257 $end_msg = $num_msgs;
258 }
259 if ($end_msg < $start_msg) {
260 $start_msg = $start_msg - $show_num;
261 if ($start_msg < 1) {
262 $start_msg = 1;
263 }
264 }
265
266 $real_startMessage = $num_msgs - $start_msg + 1;
267 $real_endMessage = $num_msgs - $start_msg - $show_num + 2;
268 if ($real_endMessage <= 0) {
269 $real_endMessage = 1;
270 }
271 $id = array_reverse(range($real_endMessage, $real_startMessage));
272 }
273 $msgs_list = sqimap_get_small_header_list($imapConnection, $id, $issent);
274 $flags = sqimap_get_flags_list($imapConnection, $id, $issent);
275 if (sizeof($msgs_list)){
276 foreach ($msgs_list as $hdr) {
277 $from[] = $hdr->from;
278 $date[] = $hdr->date;
279 $subject[] = $hdr->subject;
280 $to[] = $hdr->to;
281 $priority[] = $hdr->priority;
282 $cc[] = $hdr->cc;
283 $size[] = $hdr->size;
284 $type[] = $hdr->type0;
285 }
286 }
287 }
288 $j = 0;
289 if ($sort == 6) {
290 $end = $start_msg + $show_num - 1;
291 if ($num_msgs < $show_num) {
292 $end_loop = $num_msgs;
293 } else if ($end > $num_msgs) {
294 $end_loop = $num_msgs - $start_msg + 1;
295 } else {
296 $end_loop = $show_num;
297 }
298 } else {
299 $end = $num_msgs;
300 $end_loop = $end;
301 }
302 while ($j < $end_loop) {
303 if (isset($date[$j])) {
304 $date[$j] = str_replace(' ', ' ', $date[$j]);
305 $tmpdate = explode(' ', trim($date[$j]));
306 } else {
307 $tmpdate = $date = array('', '', '', '', '', '');
308 }
309
310 $messages[$j]['TIME_STAMP'] = getTimeStamp($tmpdate);
311 $messages[$j]['DATE_STRING'] =
312 getDateString($messages[$j]['TIME_STAMP']);
313 $messages[$j]['ID'] = $id[$j];
314 $messages[$j]['FROM'] = decodeHeader($from[$j]);
315 $messages[$j]['FROM-SORT'] =
316 strtolower(sqimap_find_displayable_name(decodeHeader($from[$j])));
317 $messages[$j]['SUBJECT'] = decodeHeader($subject[$j]);
318 $messages[$j]['SUBJECT-SORT'] = strtolower(decodeHeader($subject[$j]));
319 $messages[$j]['TO'] = decodeHeader($to[$j]);
320 $messages[$j]['PRIORITY'] = $priority[$j];
321 $messages[$j]['CC'] = $cc[$j];
322 $messages[$j]['SIZE'] = $size[$j];
323 $messages[$j]['TYPE0'] = $type[$j];
324
325 /*
326 * fix SUBJECT-SORT to remove Re:
327 * vedr|sv (Danish)
328 * re|aw (English)
329 *
330 * TODO: i18n should be incorporated here. E.g. we catch the ones
331 * we know about, but also define in i18n what the localized
332 * "Re: " is for this or that locale.
333 */
334 if (preg_match("/^(vedr|sv|re|aw):\s*(.*)$/si",
335 $messages[$j]['SUBJECT-SORT'], $matches)){
336 $messages[$j]['SUBJECT-SORT'] = $matches[2];
337 }
338
339 $num = 0;
340 while ($num < count($flags[$j])) {
341 if ($flags[$j][$num] == 'Deleted') {
342 $messages[$j]['FLAG_DELETED'] = true;
343 } else if ($flags[$j][$num] == 'Answered') {
344 $messages[$j]['FLAG_ANSWERED'] = true;
345 } else if ($flags[$j][$num] == 'Seen') {
346 $messages[$j]['FLAG_SEEN'] = true;
347 } else if ($flags[$j][$num] == 'Flagged') {
348 $messages[$j]['FLAG_FLAGGED'] = true;
349 }
350 $num++;
351 }
352 $j++;
353 }
354
355 /*
356 * Only ignore messages flagged as deleted if we are
357 * using a trash folder or auto_expunge
358 */
359 if (((isset($move_to_trash) && $move_to_trash)
360 || (isset($auto_expunge) && $auto_expunge))
361 && $sort != 6) {
362
363 /* Find and remove the ones that are deleted */
364 $i = 0;
365 $j = 0;
366
367 while ($j < $num_msgs) {
368 if (isset($messages[$j]['FLAG_DELETED'])
369 && $messages[$j]['FLAG_DELETED'] == true) {
370 $j++;
371 continue;
372 }
373 $msgs[$i] = $messages[$j];
374
375 $i++;
376 $j++;
377 }
378 $num_msgs = $i;
379 } else {
380 if (!isset($messages)) {
381 $messages = array();
382 }
383 $msgs = $messages;
384 }
385 }
386
387 /* There's gotta be messages in the array for it to sort them. */
388 if (($num_msgs > 0) && (!$use_cache)) {
389 /*
390 * 0 = Date (up)
391 * 1 = Date (dn)
392 * 2 = Name (up)
393 * 3 = Name (dn)
394 * 4 = Subject (up)
395 * 5 = Subject (dn)
396 */
397 session_unregister('msgs');
398 if (($sort == 0) || ($sort == 1)) {
399 $msort = array_cleave ($msgs, 'TIME_STAMP');
400 } elseif (($sort == 2) || ($sort == 3)) {
401 $msort = array_cleave ($msgs, 'FROM-SORT');
402 } elseif (($sort == 4) || ($sort == 5)) {
403 $msort = array_cleave ($msgs, 'SUBJECT-SORT');
404 } else {
405 $msort = $msgs;
406 }
407
408 if ($sort < 6) {
409 if ($sort % 2) {
410 asort($msort);
411 } else {
412 arsort($msort);
413 }
414 }
415 session_register('msort');
416 } elseif ($thread_sort_messages == 1 ) {
417 $msort = $msgs;
418 session_unregister('msgs');
419 session_register('msort');
420 }
421 displayMessageArray($imapConnection, $num_msgs, $start_msg, $msgs,
422 $msort, $mailbox, $sort, $color,$show_num);
423 /**
424 * TODO: Switch to using $_SESSION[] whenever we ditch the 4.0.x series.
425 */
426 session_register('msgs');
427 }
428
429 /* Generic function to convert the msgs array into an HTML table. */
430 function displayMessageArray($imapConnection, $num_msgs, $start_msg,
431 &$msgs, $msort, $mailbox, $sort, $color,
432 $show_num) {
433 global $folder_prefix, $sent_folder,
434 $imapServerAddress, $data_dir, $username, $use_mailbox_cache,
435 $index_order, $real_endMessage, $real_startMessage, $checkall,
436 $indent_array, $thread_sort_messages;
437
438 /* If cache isn't already set, do it now. */
439 if (!session_is_registered('msgs')) {
440 session_register('msgs');
441 }
442 if (!session_is_registered('msort')) {
443 session_register('msort');
444 }
445
446 if ($start_msg + ($show_num - 1) < $num_msgs){
447 $end_msg = $start_msg + ($show_num - 1);
448 } else {
449 $end_msg = $num_msgs;
450 }
451
452 if ($end_msg < $start_msg) {
453 $start_msg = $start_msg - $show_num;
454 if ($start_msg < 1) {
455 $start_msg = 1;
456 }
457 }
458
459 $urlMailbox = urlencode($mailbox);
460
461 do_hook('mailbox_index_before');
462
463 $msg_cnt_str = get_msgcnt_str($start_msg, $end_msg, $num_msgs);
464 $paginator_str = get_paginator_str($urlMailbox, $start_msg, $end_msg,
465 $num_msgs, $show_num, $sort);
466
467 if (!isset($msg)) {
468 $msg = '';
469 }
470
471 /* get indent level for subject display */
472 if ($thread_sort_messages == 1 ) {
473 $indent_array = get_parent_level($imapConnection);
474 }
475 $fstring = "move_messages.php?msg=$msg&amp;mailbox=$urlMailbox"
476 . "&amp;startMessage=$start_msg";
477 mail_message_listing_beginning($imapConnection, $fstring,
478 $mailbox, $sort, $msg_cnt_str,
479 $paginator_str, $start_msg);
480
481 $groupNum = $start_msg % ($show_num - 1);
482 $real_startMessage = $start_msg;
483 if ($sort == 6) {
484 if ($end_msg - $start_msg < $show_num - 1) {
485 $end_msg = $end_msg - $start_msg + 1;
486 $start_msg = 1;
487 } else if ($start_msg > $show_num) {
488 $end_msg = $show_num;
489 $start_msg = 1;
490 }
491 }
492 $endVar = $end_msg + 1;
493
494 /*
495 * Loop through and display the info for each message.
496 * ($t is used for the checkbox number)
497 */
498 $t = 0;
499 if ($num_msgs == 0) {
500 /* if there's no messages in this folder */
501 echo "<TR><TD BGCOLOR=\"$color[4]\" COLSPAN="
502 . count($index_order) . ">\n"
503 . " <CENTER><BR><B>". _("THIS FOLDER IS EMPTY")
504 . "</B><BR>&nbsp;</CENTER>\n"
505 . "</TD></TR>";
506 } elseif ($start_msg == $end_msg) {
507 /* if there's only one message in the box, handle it differently. */
508 if ($sort != 6){
509 $i = $start_msg;
510 } else {
511 $i = 1;
512 }
513 reset($msort);
514 $k = 0;
515 do {
516 $key = key($msort);
517 next($msort);
518 $k++;
519 } while (isset ($key) && ($k < $i));
520 printMessageInfo($imapConnection, $t, $i, $key, $mailbox, $sort,
521 $real_startMessage, 0, 0);
522 } else {
523 $i = $start_msg;
524 reset($msort);
525 $k = 0;
526 do {
527 $key = key($msort);
528 next($msort);
529 $k++;
530 } while (isset ($key) && ($k < $i));
531 do {
532 printMessageInfo($imapConnection, $t, $i, $key, $mailbox,
533 $sort, $real_startMessage, 0, 0);
534 $key = key($msort);
535 $t++;
536 $i++;
537 next($msort);
538 } while ($i && $i < $endVar);
539 }
540
541 echo '</table>'
542 . "<table bgcolor=\"$color[9]\" width=\"100%\" border=0 cellpadding=1 "
543 . "cellspacing=1><tr BGCOLOR=\"$color[4]\"><td>"
544 . "<table width=\"100%\" BGCOLOR=\"$color[4]\" border=0 cellpadding=1 "
545 . "cellspacing=0><tr><td>$paginator_str</td>"
546 . "<td align=right>$msg_cnt_str</td></tr></table>"
547 . "</td></tr></table>";
548 /* End of message-list table */
549
550 do_hook('mailbox_index_after');
551 echo "</TABLE></FORM>\n";
552 }
553
554 /*
555 * Displays the standard message list header. To finish the table,
556 * you need to do a "</table></table>";
557 *
558 * $moveURL is the URL to submit the delete/move form to
559 * $mailbox is the current mailbox
560 * $sort is the current sorting method (-1 for no sorting available [searches])
561 * $Message is a message that is centered on top of the list
562 * $More is a second line that is left aligned
563 */
564 function mail_message_listing_beginning ($imapConnection, $moveURL,
565 $mailbox = '', $sort = -1,
566 $msg_cnt_str = '',
567 $paginator = '&nbsp;',
568 $start_msg = 1) {
569 global $color, $index_order, $auto_expunge, $move_to_trash, $base_uri,
570 $checkall, $sent_folder, $draft_folder, $thread_sort_messages,
571 $allow_thread_sort;
572 $urlMailbox = urlencode($mailbox);
573
574 /*
575 * This is the beginning of the message list table.
576 * It wraps around all messages
577 */
578 echo "<FORM name=messageList method=post action=\"$moveURL\">\n"
579 . "<TABLE WIDTH=\"100%\" BORDER=\"0\" CELLPADDING=\"1\" "
580 . "CELLSPACING=\"0\">\n<TR BGCOLOR=\"$color[0]\"><TD>"
581 . " <TABLE BGCOLOR=\"$color[4]\" width=\"100%\" CELLPADDING=\"2\" "
582 . "CELLSPACING=\"0\" BORDER=\"0\"><TR>\n"
583 . " <TD ALIGN=LEFT>$paginator\n";
584
585 if ($allow_thread_sort == TRUE) {
586 if ($thread_sort_messages == 1 ) {
587 $set_thread = 2;
588 $thread_name = 'Unthread View';
589 } elseif ($thread_sort_messages == 0) {
590 $set_thread = 1;
591 $thread_name = 'Thread View';
592 }
593 echo '|&nbsp;<a href=' . "$base_uri" . 'src/right_main.php?sort='
594 . "$sort" . '&start_messages=1&set_thread=' . "$set_thread"
595 . '&mailbox=' . urlencode($mailbox) . '>' . _("$thread_name")
596 . '</a>&nbsp;';
597 }
598
599 echo " <TD ALIGN=RIGHT>$msg_cnt_str</TD>\n"
600 . " </TR></TABLE>\n"
601 . '</TD></TR>'
602 . "<TR><TD BGCOLOR=\"$color[0]\">\n"
603 . "<TABLE BGCOLOR=\"$color[0]\" COLS=2 BORDER=0 cellpadding=0"
604 . "cellspacing=0 width=\"100%\">\n"
605 . " <TR>\n"
606 . " <TD ALIGN=LEFT VALIGN=MIDDLE NOWRAP>\n"
607 . ' <SMALL>&nbsp;' . _("Move Selected To:") . "</SMALL>\n"
608 . " </TD>\n"
609 . " <TD ALIGN=RIGHT NOWRAP>\n"
610 . ' <SMALL>&nbsp;' . _("Transform Selected Messages")
611 . ": &nbsp; </SMALL><BR>\n"
612 . " </TD>\n"
613 . " </TR>\n"
614 . " <TR>\n"
615 . " <TD ALIGN=\"LEFT\" VALIGN=\"MIDDLE\" NOWRAP>\n"
616 . ' <SMALL>&nbsp;<TT><SELECT NAME="targetMailbox">';
617
618 $boxes = sqimap_mailbox_list($imapConnection);
619 foreach ($boxes as $boxes_part) {
620 if (!in_array('noselect', $boxes_part['flags'])) {
621 $box = $boxes_part['unformatted'];
622 $box2 = str_replace(' ', '&nbsp;', $boxes_part['unformatted-disp']);
623 if( $box2 == 'INBOX' ) {
624 $box2 = _("INBOX");
625 }
626 echo " <OPTION VALUE=\"$box\">$box2</option>\n";
627 }
628 }
629 echo ' </SELECT></TT>&nbsp;'
630 . '<INPUT TYPE="SUBMIT" NAME="moveButton" VALUE="' . _("Move") . '">&nbsp;'
631 . '<INPUT TYPE="SUBMIT" NAME="attache" VALUE="' . _("Forward")
632 . "\">&nbsp;\n" . "</SMALL>\n";
633
634 echo " </TD>\n"
635 . " <TD ALIGN=\"RIGHT\" NOWRAP>";
636 if (!$auto_expunge) {
637 echo '<INPUT TYPE=SUBMIT NAME="expungeButton" VALUE="' . _("Expunge")
638 . '">&nbsp;' . _("mailbox") . '&nbsp;';
639 }
640 echo '<INPUT TYPE="SUBMIT" NAME="markRead" VALUE="' . _("Read") . '">'
641 . '<INPUT TYPE="SUBMIT" NAME="markUnread" VALUE="' . _("Unread") . '">'
642 . '<INPUT TYPE="SUBMIT" VALUE="' . _("Delete") . '">&nbsp;'
643 . "</TD>\n"
644 . " </TR>\n"
645 . "</TABLE>\n";
646 do_hook('mailbox_form_before');
647 echo '</TD></TR>'
648 . "<TR><TD BGCOLOR=\"$color[0]\">"
649 . '<TABLE WIDTH="100%" BORDER=0 CELLPADDING=2 CELLSPACING=';
650 if ($GLOBALS['alt_index_colors']){
651 echo '0';
652 } else {
653 echo '1';
654 }
655 echo " BGCOLOR=\"$color[0]\">"
656 . "<TR BGCOLOR=\"$color[5]\" ALIGN=\"center\">";
657
658 /* Print the headers. */
659 for ($i=1; $i <= count($index_order); $i++) {
660 switch ($index_order[$i]) {
661 case 1: /* checkbox */
662 case 5: /* flags */
663 echo ' <TD WIDTH="1%"><B>&nbsp;</B></TD>';
664 break;
665 case 2: /* from */
666 if (handleAsSent($mailbox)) {
667 echo ' <TD WIDTH="25%"><B>' . _("To") . '</B>';
668 } else {
669 echo ' <TD WIDTH="25%"><B>' . _("From") . '</B>';
670 }
671 if ($thread_sort_messages != 1) {
672 ShowSortButton($sort, $mailbox, 2, 3);
673 }
674 echo "</TD>\n";
675 break;
676 case 3: /* date */
677 echo ' <TD NOWRAP WIDTH="5%"><B>' . _("Date") . '</B>';
678 if ($thread_sort_messages != 1) {
679 ShowSortButton($sort, $mailbox, 0, 1);
680 }
681 echo "</TD>\n";
682 break;
683 case 4: /* subject */
684 echo ' <TD><B>' . _("Subject") . '</B> ';
685 if ($thread_sort_messages != 1) {
686 ShowSortButton($sort, $mailbox, 4, 5);
687 }
688 echo "</TD>\n";
689 break;
690 case 6: /* size */
691 echo ' <TD WIDTH="5%"><b>' . _("Size") . "</b></TD>\n";
692 break;
693 }
694 }
695 echo "</TR>\n";
696 }
697
698 /*
699 * This function shows the sort button. Isn't this a good comment?
700 */
701 function ShowSortButton($sort, $mailbox, $Up, $Down) {
702 /* Figure out which image we want to use. */
703 if ($sort != $Up && $sort != $Down) {
704 $img = 'sort_none.png';
705 $which = $Up;
706 } elseif ($sort == $Up) {
707 $img = 'up_pointer.png';
708 $which = $Down;
709 } else {
710 $img = 'down_pointer.png';
711 $which = 6;
712 }
713
714 /* Now that we have everything figured out, show the actual button. */
715 echo ' <a href="right_main.php?newsort=' . $which
716 . '&amp;startMessage=1&amp;mailbox=' . urlencode($mailbox)
717 . '"><IMG SRC="../images/' . $img
718 . '" BORDER=0 WIDTH=12 HEIGHT=10></a>';
719 }
720
721 function get_selectall_link($start_msg, $sort) {
722 global $checkall, $what, $where, $mailbox, $javascript_on;
723 global $PHP_SELF, $PG_SHOWNUM;
724
725 $result = '';
726 if ($javascript_on) {
727 $result =
728 '<script language="JavaScript" type="text/javascript">'
729 . "\n<!-- \n"
730 . "function CheckAll() {\n"
731 . " for (var i = 0; i < document.messageList.elements.length; i++) {\n"
732 . " if(document.messageList.elements[i].type == 'checkbox'){\n"
733 . " document.messageList.elements[i].checked = "
734 . " !(document.messageList.elements[i].checked);\n"
735 . " }\n"
736 . " }\n"
737 . "}\n"
738 . "//-->\n"
739 . '</script><a href="#" onClick="CheckAll();">' . _("Toggle All")
740 . "</a>\n";
741 } else {
742 if (strpos($PHP_SELF, "?")) {
743 $result .= "<a href=\"$PHP_SELF&amp;mailbox=" . urlencode($mailbox)
744 . "&amp;startMessage=$start_msg&amp;sort=$sort&amp;checkall=";
745 } else {
746 $result .= "<a href=\"$PHP_SELF?mailbox=" . urlencode($mailbox)
747 . "&amp;startMessage=$start_msg&amp;sort=$sort&amp;checkall=";
748 }
749 if (isset($checkall) && $checkall == '1') {
750 $result .= '0';
751 } else {
752 $result .= '1';
753 }
754
755 if (isset($where) && isset($what)) {
756 $result .= '&amp;where=' . urlencode($where)
757 . '&amp;what=' . urlencode($what);
758 }
759
760 if ($PG_SHOWNUM == 999999) {
761 $result .= '&amp;PG_SHOWNUM=999999';
762 }
763
764 $result .= "\">";
765
766 if (isset($checkall) && ($checkall == '1')) {
767 $result .= _("Unselect All");
768 } else {
769 $result .= _("Select All");
770 }
771
772 $result .= "</A>\n";
773 }
774
775 /* Return our final result. */
776 return ($result);
777 }
778
779 /*
780 * This function computes the "Viewing Messages..." string.
781 */
782 function get_msgcnt_str($start_msg, $end_msg, $num_msgs) {
783 /* Compute the $msg_cnt_str. */
784 $result = '';
785 if ($start_msg < $end_msg) {
786 $result = sprintf(_("Viewing Messages: <B>%s</B> to <B>%s</B> (%s total)"),
787 $start_msg, $end_msg, $num_msgs);
788 } else if ($start_msg == $end_msg) {
789 $result = sprintf(_("Viewing Message: <B>%s</B> (1 total)"), $start_msg);
790 } else {
791 $result = '<br>';
792 }
793
794 /* Return our result string. */
795 return ($result);
796 }
797
798 /*
799 * Generate a paginator link.
800 */
801 function get_paginator_link($box, $start_msg, $use, $text) {
802 $result = "<A HREF=\"right_main.php?use_mailbox_cache=$use"
803 . "&amp;startMessage=$start_msg&amp;mailbox=$box\" "
804 . "TARGET=\"right\">$text</A>";
805 return ($result);
806 }
807
808 /*
809 * This function computes the paginator string.
810 */
811 function get_paginator_str($box, $start_msg, $end_msg, $num_msgs,
812 $show_num, $sort) {
813 global $username, $data_dir, $use_mailbox_cache, $color, $PG_SHOWNUM;
814
815 /* Initialize paginator string chunks. */
816 $prv_str = '';
817 $nxt_str = '';
818 $pg_str = '';
819 $all_str = '';
820 $tgl_str = '';
821
822 /* Create simple strings that will be creating the paginator. */
823 $spc = '&nbsp;'; /* This will be used as a space. */
824 $sep = '|'; /* This will be used as a seperator. */
825
826 /* Get some paginator preference values. */
827 $pg_sel = getPref($data_dir, $username, 'page_selector', SMPREF_ON);
828 $pg_max = getPref($data_dir, $username, 'page_selector_max', PG_SEL_MAX);
829
830 /* Make sure that our start message number is not too big. */
831 $start_msg = min($start_msg, $num_msgs);
832
833 /* Decide whether or not we will use the mailbox cache. */
834 /* Not sure why $use_mailbox_cache is even passed in. */
835 if ($sort == 6) {
836 $use = 0;
837 } else {
838 $use = 1;
839 }
840
841 /* Compute the starting message of the previous and next page group. */
842 $next_grp = $start_msg + $show_num;
843 $prev_grp = $start_msg - $show_num;
844
845 /* Compute the basic previous and next strings. */
846 if (($next_grp <= $num_msgs) && ($prev_grp >= 0)) {
847 $prv_str = get_paginator_link($box, $prev_grp, $use, _("Previous"));
848 $nxt_str = get_paginator_link($box, $next_grp, $use, _("Next"));
849 } else if (($next_grp > $num_msgs) && ($prev_grp >= 0)) {
850 $prv_str = get_paginator_link($box, $prev_grp, $use, _("Previous"));
851 $nxt_str = "<FONT COLOR=\"$color[9]\">"._("Next")."</FONT>\n";
852 } else if (($next_grp <= $num_msgs) && ($prev_grp < 0)) {
853 $prv_str = "<FONT COLOR=\"$color[9]\">"._("Previous") . '</FONT>';
854 $nxt_str = get_paginator_link($box, $next_grp, $use, _("Next"));
855 }
856
857 /* Page selector block. Following code computes page links. */
858 if ($pg_sel && ($num_msgs > $show_num)) {
859 /* Most importantly, what is the current page!!! */
860 $cur_pg = intval($start_msg / $show_num) + 1;
861
862 /* Compute total # of pages and # of paginator page links. */
863 $tot_pgs = ceil($num_msgs / $show_num); /* Total number of Pages */
864 $vis_pgs = min($pg_max, $tot_pgs - 1); /* Visible Pages */
865
866 /* Compute the size of the four quarters of the page links. */
867
868 /* If we can, just show all the pages. */
869 if (($tot_pgs - 1) <= $pg_max) {
870 $q1_pgs = $cur_pg - 1;
871 $q2_pgs = $q3_pgs = 0;
872 $q4_pgs = $tot_pgs - $cur_pg;
873
874 /* Otherwise, compute some magic to choose the four quarters. */
875 } else {
876 /*
877 * Compute the magic base values. Added together,
878 * these values will always equal to the $pag_pgs.
879 * NOTE: These are DEFAULT values and do not take
880 * the current page into account. That is below.
881 */
882 $q1_pgs = floor($vis_pgs/4);
883 $q2_pgs = round($vis_pgs/4, 0);
884 $q3_pgs = ceil($vis_pgs/4);
885 $q4_pgs = round(($vis_pgs - $q2_pgs)/3, 0);
886
887 /* Adjust if the first quarter contains the current page. */
888 if (($cur_pg - $q1_pgs) < 1) {
889 $extra_pgs = ($q1_pgs - ($cur_pg - 1)) + $q2_pgs;
890 $q1_pgs = $cur_pg - 1;
891 $q2_pgs = 0;
892 $q3_pgs += ceil($extra_pgs / 2);
893 $q4_pgs += floor($extra_pgs / 2);
894
895 /* Adjust if the first and second quarters intersect. */
896 } else if (($cur_pg - $q2_pgs - ceil($q2_pgs/3)) <= $q1_pgs) {
897 $extra_pgs = $q2_pgs;
898 $extra_pgs -= ceil(($cur_pg - $q1_pgs - 1) * 0.75);
899 $q2_pgs = ceil(($cur_pg - $q1_pgs - 1) * 0.75);
900 $q3_pgs += ceil($extra_pgs / 2);
901 $q4_pgs += floor($extra_pgs / 2);
902
903 /* Adjust if the fourth quarter contains the current page. */
904 } else if (($cur_pg + $q4_pgs) >= $tot_pgs) {
905 $extra_pgs = ($q4_pgs - ($tot_pgs - $cur_pg)) + $q3_pgs;
906 $q3_pgs = 0;
907 $q4_pgs = $tot_pgs - $cur_pg;
908 $q1_pgs += floor($extra_pgs / 2);
909 $q2_pgs += ceil($extra_pgs / 2);
910
911 /* Adjust if the third and fourth quarter intersect. */
912 } else if (($cur_pg + $q3_pgs + 1) >= ($tot_pgs - $q4_pgs + 1)) {
913 $extra_pgs = $q3_pgs;
914 $extra_pgs -= ceil(($tot_pgs - $cur_pg - $q4_pgs) * 0.75);
915 $q3_pgs = ceil(($tot_pgs - $cur_pg - $q4_pgs) * 0.75);
916 $q1_pgs += floor($extra_pgs / 2);
917 $q2_pgs += ceil($extra_pgs / 2);
918 }
919 }
920
921 /*
922 * I am leaving this debug code here, commented out, because
923 * it is a really nice way to see what the above code is doing.
924 * echo "qts = $q1_pgs/$q2_pgs/$q3_pgs/$q4_pgs = "
925 * . ($q1_pgs + $q2_pgs + $q3_pgs + $q4_pgs) . '<br>';
926 */
927
928 /* Print out the page links from the compute page quarters. */
929
930 /* Start with the first quarter. */
931 if (($q1_pgs == 0) && ($cur_pg > 1)) {
932 $pg_str .= "...$spc";
933 } else {
934 for ($pg = 1; $pg <= $q1_pgs; ++$pg) {
935 $start = (($pg-1) * $show_num) + 1;
936 $pg_str .= get_paginator_link($box, $start, $use, $pg) . $spc;
937 }
938 if ($cur_pg - $q2_pgs - $q1_pgs > 1) {
939 $pg_str .= "...$spc";
940 }
941 }
942
943 /* Continue with the second quarter. */
944 for ($pg = $cur_pg - $q2_pgs; $pg < $cur_pg; ++$pg) {
945 $start = (($pg-1) * $show_num) + 1;
946 $pg_str .= get_paginator_link($box, $start, $use, $pg) . $spc;
947 }
948
949 /* Now print the current page. */
950 $pg_str .= $cur_pg . $spc;
951
952 /* Next comes the third quarter. */
953 for ($pg = $cur_pg + 1; $pg <= $cur_pg + $q3_pgs; ++$pg) {
954 $start = (($pg-1) * $show_num) + 1;
955 $pg_str .= get_paginator_link($box, $start, $use, $pg) . $spc;
956 }
957
958 /* And last, print the forth quarter page links. */
959 if (($q4_pgs == 0) && ($cur_pg < $tot_pgs)) {
960 $pg_str .= "...$spc";
961 } else {
962 if (($tot_pgs - $q4_pgs) > ($cur_pg + $q3_pgs)) {
963 $pg_str .= "...$spc";
964 }
965 for ($pg = $tot_pgs - $q4_pgs + 1; $pg <= $tot_pgs; ++$pg) {
966 $start = (($pg-1) * $show_num) + 1;
967 $pg_str .= get_paginator_link($box, $start, $use, $pg) . $spc;
968 }
969 }
970 } else if ($PG_SHOWNUM == 999999) {
971 $pg_str = "<A HREF=\"right_main.php?use_mailbox_cache=$use"
972 . "&amp;startMessage=1&amp;mailbox=$box\" TARGET=\"right\">"
973 . _("Paginate") . '</A>' . $spc;
974 }
975
976 /* If necessary, compute the 'show all' string. */
977 if (($prv_str != '') || ($nxt_str != '')) {
978 $all_str = "<A HREF=\"right_main.php?PG_SHOWNUM=999999"
979 . "&amp;use_mailbox_cache=$use&amp;startMessage=1&amp;mailbox=$box\" "
980 . "TARGET=\"right\">" . _("Show All") . '</A>';
981 }
982
983 /* Last but not least, get the value for the toggle all link. */
984 $tgl_str = get_selectall_link($start_msg, $sort);
985
986 /* Put all the pieces of the paginator string together. */
987 /**
988 * Hairy code... But let's leave it like it is since I am not certain
989 * a different approach would be any easier to read. ;)
990 */
991 $result = '';
992 $result .= ($prv_str != '' ? $prv_str . $spc . $sep . $spc : '');
993 $result .= ($nxt_str != '' ? $nxt_str . $spc . $sep . $spc : '');
994 $result .= ($pg_str != '' ? $pg_str : '');
995 $result .= ($all_str != '' ? $sep . $spc . $all_str . $spc : '');
996 $result .= ($result != '' ? $sep . $spc . $tgl_str: $tgl_str);
997
998 /* If the resulting string is blank, return a non-breaking space. */
999 if ($result == '') {
1000 $result = '&nbsp;';
1001 }
1002
1003 /* Return our final magical paginator string. */
1004 return ($result);
1005 }
1006
1007 function processSubject($subject) {
1008 /* Shouldn't ever happen -- caught too many times in the IMAP functions */
1009 if ($subject == '')
1010 return _("(no subject)");
1011
1012 if (strlen($subject) <= 55)
1013 return $subject;
1014
1015 $ent_strlen=strlen($subject);
1016 $trim_val=50;
1017 $ent_offset=0;
1018 /*
1019 * see if this is entities-encoded string
1020 * If so, Iterate through the whole string, find out
1021 * the real number of characters, and if more
1022 * than 55, substr with an updated trim value.
1023 */
1024 while ( (($ent_loc = strpos($subject, '&', $ent_offset)) !== false) &&
1025 (($ent_loc_end = strpos($subject, ';', $ent_loc)) !== false) ) {
1026 $trim_val += ($ent_loc_end-$ent_loc)+1;
1027 $ent_strlen -= $ent_loc_end-$ent_loc;
1028 $ent_offset = $ent_loc_end+1;
1029 }
1030
1031 if ($ent_strlen <= 55){
1032 return $subject;
1033 }
1034
1035 return substr($subject, 0, $trim_val) . '...';
1036 }
1037
1038 function handleAsSent($mailbox) {
1039 global $sent_folder, $draft_folder, $handleAsSent_result;
1040
1041 /* First check if this is the sent or draft folder. */
1042 $handleAsSent_result = (($mailbox == $sent_folder)
1043 || ($mailbox == $draft_folder));
1044
1045 /* Then check the result of the handleAsSent hook. */
1046 do_hook('check_handleAsSent_result', $mailbox);
1047
1048 /* And return the result. */
1049 return ($handleAsSent_result);
1050 }
1051
1052 ?>