6 * Copyright (c) 1999-2003 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
9 * This contains functions that display mailbox information, such as the
10 * table row that has sender, date, subject, etc...
15 require_once(SM_PATH
. 'functions/strings.php');
16 require_once(SM_PATH
. 'functions/html.php');
17 require_once(SM_PATH
. 'class/html.class.php');
18 require_once(SM_PATH
. 'functions/imap_mailbox.php');
21 * PG_SEL_MAX: default value for page_selector_max
22 * SUBJ_TRIM_AT: the length at which we trim off subjects
24 define('PG_SEL_MAX', 10);
25 define('SUBJ_TRIM_AT', 55);
27 function elapsed($start)
30 list($start2, $start1) = explode(" ", $start);
31 list($end2, $end1) = explode(" ", $end);
32 $diff1 = $end1 - $start1;
33 $diff2 = $end2 - $start2;
38 return $diff2 +
$diff1;
41 function printMessageInfo($imapConnection, $t, $not_last=true, $key, $mailbox,
42 $start_msg, $where, $what) {
44 $color, $msgs, $msort, $td_str, $msg,
45 $default_use_priority,
46 $message_highlight_list,
48 $indent_array, /* indent subject by */
49 $pos, /* Search postion (if any) */
50 $thread_sort_messages, /* thread sorting on/off */
51 $server_sort_order, /* sort value when using server-sorting */
53 $allow_server_sort; /* enable/disable server-side sorting */
54 $color_string = $color[4];
56 if ($GLOBALS['alt_index_colors']) {
57 if (!isset($row_count)) {
62 if (!isset($color[12])) {
63 $color[12] = '#EAEAEA';
65 $color_string = $color[12];
70 if($mailbox == 'None') {
71 $boxes = sqimap_mailbox_list($imapConnection);
72 $mailbox = $boxes[0]['unformatted'];
75 $urlMailbox = urlencode($mailbox);
77 if (handleAsSent($mailbox)) {
78 $msg['FROM'] = $msg['TO'];
80 $msg['FROM'] = parseAddress($msg['FROM'],1);
83 * This is done in case you're looking into Sent folders,
84 * because you can have multiple receivers.
87 $senderNames = $msg['FROM'];
89 if (sizeof($senderNames)){
90 foreach ($senderNames as $senderNames_part) {
91 if ($senderName != '') {
94 if ($senderNames_part[1]) {
95 $senderName .= decodeHeader($senderNames_part[1]);
97 $senderName .= htmlspecialchars($senderNames_part[0]);
101 $senderName = str_replace(' ',' ',$senderName);
102 echo html_tag( 'tr','','','','VALIGN="top"') . "\n";
104 if (isset($msg['FLAG_FLAGGED']) && ($msg['FLAG_FLAGGED'] == true)) {
105 $flag = "<font color=\"$color[2]\">";
106 $flag_end = '</font>';
111 if (!isset($msg['FLAG_SEEN']) ||
($msg['FLAG_SEEN'] == false)) {
118 if (handleAsSent($mailbox)) {
120 $italic_end = '</i>';
125 if (isset($msg['FLAG_DELETED']) && $msg['FLAG_DELETED']) {
126 $fontstr = "<font color=\"$color[9]\">";
127 $fontstr_end = '</font>';
133 if ($where && $what) {
134 $searchstr = '&where='.$where.'&what='.$what;
139 if (is_array($message_highlight_list) && count($message_highlight_list)) {
140 $msg['TO'] = parseAddress($msg['TO']);
141 $msg['CC'] = parseAddress($msg['CC']);
142 foreach ($message_highlight_list as $message_highlight_list_part) {
143 if (trim($message_highlight_list_part['value']) != '') {
144 $high_val = strtolower($message_highlight_list_part['value']);
145 $match_type = strtoupper($message_highlight_list_part['match_type']);
146 if($match_type == 'TO_CC') {
147 $match = array('TO', 'CC');
149 $match = array($match_type);
151 foreach($match as $match_type) {
152 switch($match_type) {
156 foreach ($msg[$match_type] as $address) {
157 $address[0] = decodeHeader($address[0], true, false);
158 $address[1] = decodeHeader($address[1], true, false);
159 if (strstr('^^' . strtolower($address[0]), $high_val) ||
160 strstr('^^' . strtolower($address[1]), $high_val)) {
161 $hlt_color = $message_highlight_list_part['color'];
167 $headertest = strtolower(decodeHeader($msg[$match_type], true, false));
168 if (strstr('^^' . $headertest, $high_val)) {
169 $hlt_color = $message_highlight_list_part['color'];
179 if (!isset($hlt_color)) {
180 $hlt_color = $color_string;
182 $checked = ($checkall == 1) ?
' CHECKED' : '';
184 $msg['SUBJECT'] = decodeHeader($msg['SUBJECT']);
185 $subject = processSubject($msg['SUBJECT'], $indent_array[$msg['ID']]);
186 $subject = str_replace(' ',' ',$subject);
187 if (sizeof($index_order)) {
188 foreach ($index_order as $index_order_part) {
189 switch ($index_order_part) {
190 case 1: /* checkbox */
192 "<input type=checkbox name=\"msg[$t]\" value=\"".$msg['ID']."\"$checked>",
198 $italic . $bold . $flag . $fontstr . $senderName .
199 $fontstr_end . $flag_end . $bold_end . $italic_end,
204 $date_string = $msg['DATE_STRING'] . '';
205 if ($date_string == '') {
206 $date_string = _("Unknown date");
209 $bold . $flag . $fontstr . $date_string .
210 $fontstr_end . $flag_end . $bold_end,
215 case 4: /* subject */
217 if ($thread_sort_messages == 1) {
218 if (isset($indent_array[$msg['ID']])) {
219 $td_str .= str_repeat(" ",$indent_array[$msg['ID']]);
222 $td_str .= '<a href="read_body.php?mailbox='.$urlMailbox
223 . '&passed_id='. $msg["ID"]
224 . '&startMessage='.$start_msg.$searchstr.'"';
225 $td_str .= ' ' .concat_hook_function('subject_link', array($start_msg, $searchstr));
226 if ($subject != $msg['SUBJECT']) {
227 $title = get_html_translation_table(HTML_SPECIALCHARS
);
228 $title = array_flip($title);
229 $title = strtr($msg['SUBJECT'], $title);
230 $title = str_replace('"', "''", $title);
231 $td_str .= " title=\"$title\"";
233 $td_str .= ">$flag$subject$flag_end</a>$bold_end";
234 echo html_tag( 'td', $td_str, 'left', $hlt_color );
238 $td_str = "<b><small>";
240 if (isset($msg['FLAG_ANSWERED']) && $msg['FLAG_ANSWERED'] == true) {
244 if ($msg['TYPE0'] == 'multipart') {
248 if ($default_use_priority) {
249 if ( ($msg['PRIORITY'] == 1) ||
($msg['PRIORITY'] == 2) ) {
250 $td_str .= "<font color=\"$color[1]\">!</font>";
253 if ($msg['PRIORITY'] == 5) {
254 $td_str .= "<font color=\"$color[8]\">?</font>";
258 if (isset($msg['FLAG_DELETED']) && $msg['FLAG_DELETED'] == true) {
259 $td_str .= "<font color=\"$color[1]\">D</font>";
265 do_hook("msg_envelope");
266 $td_str .= '</small></b>';
275 $bold . $fontstr . show_readable_size($msg['SIZE']) .
276 $fontstr_end . $bold_end,
285 echo '</tr>' . "\n" . '<tr><td COLSPAN="' . $col . '" BGCOLOR="' .
286 $color[0] . '" HEIGHT="1"></td></tr>' . "\n";
292 function getServerMessages($imapConnection, $start_msg, $show_num, $num_msgs, $id) {
294 $id = array_slice($id, ($start_msg-1), $show_num);
295 $end = $start_msg +
$show_num - 1;
296 if ($num_msgs < $show_num) {
297 $end_loop = $num_msgs;
298 } else if ($end > $num_msgs) {
299 $end_loop = $num_msgs - $start_msg +
1;
301 $end_loop = $show_num;
303 return fillMessageArray($imapConnection,$id,$end_loop,$show_num);
309 function getThreadMessages($imapConnection, $start_msg, $show_num, $num_msgs) {
310 $id = get_thread_sort($imapConnection);
311 return getServerMessages($imapConnection, $start_msg, $show_num, $num_msgs, $id);
314 function getServerSortMessages($imapConnection, $start_msg, $show_num,
315 $num_msgs, $server_sort_order, $mbxresponse) {
316 $id = sqimap_get_sort_order($imapConnection, $server_sort_order,$mbxresponse);
317 return getServerMessages($imapConnection, $start_msg, $show_num, $num_msgs, $id);
320 function getSelfSortMessages($imapConnection, $start_msg, $show_num,
321 $num_msgs, $sort, $mbxresponse) {
323 if ($num_msgs >= 1) {
324 $id = sqimap_get_php_sort_order ($imapConnection, $mbxresponse);
328 /* set shownum to 999999 to fool sqimap_get_small_header_list
329 and rebuild the msgs_str to 1:* */
332 /* if it's not sorted */
333 if ($start_msg +
($show_num - 1) < $num_msgs) {
334 $end_msg = $start_msg +
($show_num - 1);
336 $end_msg = $num_msgs;
338 if ($end_msg < $start_msg) {
339 $start_msg = $start_msg - $show_num;
340 if ($start_msg < 1) {
344 $id = array_slice(array_reverse($id), ($start_msg-1), $show_num);
345 $end = $start_msg +
$show_num - 1;
346 if ($num_msgs < $show_num) {
347 $end_loop = $num_msgs;
348 } else if ($end > $num_msgs) {
349 $end_loop = $num_msgs - $start_msg +
1;
351 $end_loop = $show_num;
354 $msgs = fillMessageArray($imapConnection,$id,$end_loop, $show_num);
362 * This function loops through a group of messages in the mailbox
363 * and shows them to the user.
365 function showMessagesForMailbox($imapConnection, $mailbox, $num_msgs,
366 $start_msg, $sort, $color, $show_num,
367 $use_cache, $mode='') {
368 global $msgs, $msort, $auto_expunge, $thread_sort_messages,
369 $allow_server_sort, $server_sort_order;
372 * For some reason, on PHP 4.3+, this being unset, and set in the session causes havoc
373 * so setting it to an empty array beforehand seems to clean up the issue, and stopping the
374 * "Your script possibly relies on a session side-effect which existed until PHP 4.2.3" error
377 if (!isset($msort)) {
385 //$start = microtime();
386 /* If autoexpunge is turned on, then do it now. */
387 $mbxresponse = sqimap_mailbox_select($imapConnection, $mailbox);
389 /* If autoexpunge is turned on, then do it now. */
390 if ($auto_expunge == true) {
391 $exp_cnt = sqimap_mailbox_expunge($imapConnection, $mailbox, false, '');
392 $mbxresponse['EXISTS'] = $mbxresponse['EXISTS'] - $exp_cnt;
393 $num_msgs = $mbxresponse['EXISTS'];
396 if ($mbxresponse['EXISTS'] > 0) {
397 /* if $start_msg is lower than $num_msgs, we probably deleted all messages
398 * in the last page. We need to re-adjust the start_msg
401 if($start_msg > $num_msgs) {
402 $start_msg -= $show_num;
408 /* This code and the next if() block check for
409 * server-side sorting methods. The $id array is
410 * formatted and $sort is set to 6 to disable
411 * SM internal sorting
414 if ($thread_sort_messages == 1) {
416 } elseif ($allow_server_sort == 1) {
417 $mode = 'serversort';
423 sqgetGlobalVar('msgs', $msgs, SQ_SESSION
);
424 sqgetGlobalVar('msort', $msort, SQ_SESSION
);
426 sqsession_unregister('msort');
427 sqsession_unregister('msgs'); }
430 $id = get_thread_sort($imapConnection);
431 $msgs = getServerMessages($imapConnection, $start_msg, $show_num, $num_msgs, $id);
432 if ($msgs === false) {
433 echo '<b><small><center><font color=red>' .
434 _("Thread sorting is not supported by your IMAP server.<br>Please report this to the system administrator.").
435 '</center></small></b>';
436 $thread_sort_messages = 0;
437 $msort = $msgs = array();
444 $id = sqimap_get_sort_order($imapConnection, $sort, $mbxresponse);
445 $msgs = getServerMessages($imapConnection, $start_msg, $show_num, $num_msgs, $id);
446 if ($msgs === false) {
447 echo '<b><small><center><font color=red>' .
448 _( "Server-side sorting is not supported by your IMAP server.<br>Please report this to the system administrator.").
449 '</center></small></b>';
450 $sort = $server_sort_order;
451 $allow_server_sort = FALSE;
452 $msort = $msgs = array();
461 $msgs = getSelfSortMessages($imapConnection, $start_msg, $show_num,
462 $num_msgs, $sort, $mbxresponse);
463 $msort = calc_msort($msgs, $sort);
467 sqsession_register($msort, 'msort');
468 sqsession_register($msgs, 'msgs');
470 } /* if exists > 0 */
472 $res = getEndMessage($start_msg, $show_num, $num_msgs);
473 $start_msg = $res[0];
476 $paginator_str = get_paginator_str($mailbox, $start_msg, $end_msg,
477 $num_msgs, $show_num, $sort);
479 $msg_cnt_str = get_msgcnt_str($start_msg, $end_msg, $num_msgs);
481 do_hook('mailbox_index_before');
482 echo '<table border="0" width="100%" cellpadding="0" cellspacing="0">';
485 mail_message_listing_beginning($imapConnection, $mailbox, $sort,
486 $msg_cnt_str, $paginator_str, $start_msg);
488 /* line between the button area and the list */
489 echo '<tr><td HEIGHT="5" BGCOLOR="'.$color[4].'"></td></tr>';
492 echo ' <table width="100%" cellpadding="1" cellspacing="0" align="center"'.' border="0" bgcolor="'.$color[9].'">';
494 echo ' <table width="100%" cellpadding="1" cellspacing="0" align="center" border="0" bgcolor="'.$color[5].'">';
496 printHeader($mailbox, $srt, $color, !$thread_sort_messages);
498 displayMessageArray($imapConnection, $num_msgs, $start_msg,
499 $msort, $mailbox, $sort, $color, $show_num,0,0);
500 echo '</td></tr></table></td></tr></table>';
502 mail_message_listing_end($num_msgs, $paginator_str, $msg_cnt_str, $color);
503 echo '</td></tr></table>';
504 //$t = elapsed($start);
505 //echo("elapsed time = $t seconds\n");
508 function calc_msort($msgs, $sort) {
519 if (($sort == 0) ||
($sort == 1)) {
520 foreach ($msgs as $item) {
521 $msort[] = $item['TIME_STAMP'];
523 } elseif (($sort == 2) ||
($sort == 3)) {
524 foreach ($msgs as $item) {
525 $msort[] = $item['FROM-SORT'];
527 } elseif (($sort == 4) ||
($sort == 5)) {
528 foreach ($msgs as $item) {
529 $msort[] = $item['SUBJECT-SORT'];
544 function fillMessageArray($imapConnection, $id, $count, $show_num=false) {
545 return sqimap_get_small_header_list($imapConnection, $id, $show_num);
549 /* Generic function to convert the msgs array into an HTML table. */
550 function displayMessageArray($imapConnection, $num_msgs, $start_msg,
551 $msort, $mailbox, $sort, $color,
552 $show_num, $where=0, $what=0) {
553 global $imapServerAddress, $use_mailbox_cache, $index_order,
554 $indent_array, $thread_sort_messages, $allow_server_sort,
555 $server_sort_order, $PHP_SELF;
557 $res = getEndMessage($start_msg, $show_num, $num_msgs);
558 $start_msg = $res[0];
561 $urlMailbox = urlencode($mailbox);
563 /* get indent level for subject display */
564 if ($thread_sort_messages == 1 && $num_msgs) {
565 $indent_array = get_parent_level($imapConnection);
568 $real_startMessage = $start_msg;
570 if ($end_msg - $start_msg < $show_num - 1) {
571 $end_msg = $end_msg - $start_msg +
1;
573 } else if ($start_msg > $show_num) {
574 $end_msg = $show_num;
578 $endVar = $end_msg +
1;
581 * Loop through and display the info for each message.
582 * ($t is used for the checkbox number)
586 /* messages display */
589 /* if there's no messages in this folder */
592 "<BR><b>" . _("THIS FOLDER IS EMPTY") . "</b><BR> ",
595 'COLSPAN="' . count($index_order) . '"'
598 } elseif ($start_msg == $end_msg) {
599 /* if there's only one message in the box, handle it differently. */
611 } while (isset ($key) && ($k < $i));
612 printMessageInfo($imapConnection, $t, true, $key, $mailbox,
613 $real_startMessage, $where, $what);
622 } while (isset ($key) && ($k < $i));
625 if (!$i ||
$i == $endVar-1) $not_last = false;
626 printMessageInfo($imapConnection, $t, $not_last, $key, $mailbox,
627 $real_startMessage, $where, $what);
632 } while ($i && $i < $endVar);
637 * Displays the standard message list header. To finish the table,
638 * you need to do a "</table></table>";
640 * $moveURL is the URL to submit the delete/move form to
641 * $mailbox is the current mailbox
642 * $sort is the current sorting method (-1 for no sorting available [searches])
643 * $Message is a message that is centered on top of the list
644 * $More is a second line that is left aligned
647 function mail_message_listing_beginning ($imapConnection,
648 $mailbox = '', $sort = -1,
650 $paginator = ' ',
652 global $color, $auto_expunge, $base_uri, $thread_sort_messages,
653 $allow_thread_sort, $allow_server_sort, $server_sort_order,
656 $php_self = $PHP_SELF;
657 /* fix for incorrect $PHP_SELF */
658 if (strpos($php_self, 'move_messages.php')) {
659 $php_self = str_replace('move_messages.php', 'right_main.php', $php_self);
661 $urlMailbox = urlencode($mailbox);
663 if (preg_match('/^(.+)\?.+$/',$php_self,$regs)) {
664 $source_url = $regs[1];
666 $source_url = $php_self;
672 $moveFields = '<input type="hidden" name="msg" value="'.htmlspecialchars($msg).'">' .
673 '<input type="hidden" name="mailbox" value="'.htmlspecialchars($mailbox).'">' .
674 '<input type="hidden" name="startMessage" value="'.htmlspecialchars($start_msg).'">';
676 // $moveURL = "move_messages.php?msg=$msg&mailbox=$urlMailbox"
677 // . "&startMessage=$start_msg";
679 * This is the beginning of the message list table.
680 * It wraps around all messages
682 $safe_name = preg_replace("/[^0-9A-Za-z_]/", '_', $mailbox);
683 $form_name = "FormMsgs" . $safe_name;
684 echo '<form name="' . $form_name . '" method="post" action="move_messages.php">' ."\n"
686 . html_tag( 'table' ,
691 html_tag( 'td', $paginator, 'left' ) .
692 html_tag( 'td', $msg_cnt_str, 'right' )
694 , '', $color[4], 'border="0" width="100%" cellpadding="1" cellspacing="0"' )
697 , '', '', 'border="0" width="100%" cellpadding="1" cellspacing="0"' );
698 /* line between header and button area */
699 echo '<tr><td HEIGHT="5" BGCOLOR="'.$color[4].'"></td></tr>';
702 echo html_tag( 'tr' ) . "\n"
703 . html_tag( 'td' ,'' , 'left', '', '' )
704 . html_tag( 'table' ,'' , '', $color[9], 'border="0" width="100%" cellpadding="1" cellspacing="0"' )
706 . html_tag( 'table' ,'' , '', $color[0], 'border="0" width="100%" cellpadding="1" cellspacing="0"' )
708 getSmallStringCell(_("Move Selected To"), 'left', 'nowrap') .
709 getSmallStringCell(_("Transform Selected Messages"), 'right')
711 . html_tag( 'tr' ) ."\n"
712 . html_tag( 'td', '', 'left', '', 'valign="middle" nowrap' );
713 getMbxList($imapConnection);
714 echo getButton('SUBMIT', 'moveButton',_("Move")) . "\n";
715 echo getButton('SUBMIT', 'attache',_("Forward")) . "\n";
718 . html_tag( 'td', '', 'right', '', 'nowrap' );
722 if (!$auto_expunge) {
723 echo getButton('SUBMIT', 'expungeButton',_("Expunge"))
724 .' ' . _("mailbox") . "\n";
726 do_hook('mailbox_display_buttons');
727 echo getButton('SUBMIT', 'markRead',_("Read"));
728 echo getButton('SUBMIT', 'markUnread',_("Unread"));
729 echo getButton('SUBMIT', 'delete',_("Delete")) ." \n";
730 if (!strpos($php_self,'mailbox')) {
731 $location = $php_self.'?mailbox=INBOX&startMessage=1';
733 $location = $php_self;
735 echo '<INPUT TYPE="HIDDEN" NAME="location" VALUE="'.$location.'">';
739 /* draws thread sorting links */
740 if ($allow_thread_sort == TRUE) {
741 if ($thread_sort_messages == 1 ) {
743 $thread_name = _("Unthread View");
744 } elseif ($thread_sort_messages == 0) {
746 $thread_name = _("Thread View");
748 echo html_tag( 'tr' ,
750 ' <a href=' . $source_url . '?sort='
751 . "$sort" . '&start_messages=1&set_thread=' . "$set_thread"
752 . '&mailbox=' . urlencode($mailbox) . '><small>' . $thread_name
753 . '</a></small> '
758 echo "</TABLE></td></tr></table></td></tr>\n";
760 do_hook('mailbox_form_before');
762 /* if using server sort we highjack the
763 * the $sort var and use $server_sort_order
764 * instead. but here we reset sort for a bit
767 if ($allow_server_sort == TRUE) {
768 $sort = $server_sort_order;
772 function mail_message_listing_end($num_msgs, $paginator_str, $msg_cnt_str, $color) {
774 /* space between list and footer */
775 echo '<tr><td HEIGHT="5" BGCOLOR="'.$color[4].'" COLSPAN="1">';
777 echo '</td></tr><tr><td>';
778 echo html_tag( 'table',
783 html_tag( 'td', $paginator_str ) .
784 html_tag( 'td', $msg_cnt_str, 'right' )
786 , '', $color[4], 'width="100%" border="0" cellpadding="1" cellspacing="0"' )
789 , '', $color[9], 'width="100%" border="0" cellpadding="1" cellspacing="0"' );
792 /* End of message-list table */
794 do_hook('mailbox_index_after');
798 function printHeader($mailbox, $sort, $color, $showsort=true) {
800 echo html_tag( 'tr' ,'' , 'center', $color[5] );
802 /* calculate the width of the subject column based on the
803 * widths of the other columns */
804 $widths = array(1=>1,2=>25,3=>5,4=>0,5=>1,6=>5);
806 foreach($index_order as $item) {
807 $subjectwidth -= $widths[$item];
810 foreach ($index_order as $item) {
812 case 1: /* checkbox */
814 echo html_tag( 'td' ,' ' , '', '', 'width="1%"' );
817 if (handleAsSent($mailbox)) {
818 echo html_tag( 'td' ,'' , 'left', '', 'width="25%"' )
819 . '<b>' . _("To") . '</b>';
821 echo html_tag( 'td' ,'' , 'left', '', 'width="25%"' )
822 . '<b>' . _("From") . '</b>';
825 ShowSortButton($sort, $mailbox, 2, 3);
830 echo html_tag( 'td' ,'' , 'left', '', 'width="5%" nowrap' )
831 . '<b>' . _("Date") . '</b>';
833 ShowSortButton($sort, $mailbox, 0, 1);
837 case 4: /* subject */
838 echo html_tag( 'td' ,'' , 'left', '', 'width="'.$subjectwidth.'%"' )
839 . '<b>' . _("Subject") . '</b>';
841 ShowSortButton($sort, $mailbox, 4, 5);
846 echo html_tag( 'td', '<b>' . _("Size") . '</b>', 'center', '', 'width="5%" nowrap' );
855 * This function shows the sort button. Isn't this a good comment?
857 function ShowSortButton($sort, $mailbox, $Up, $Down ) {
859 /* Figure out which image we want to use. */
860 if ($sort != $Up && $sort != $Down) {
861 $img = 'sort_none.png';
863 } elseif ($sort == $Up) {
864 $img = 'up_pointer.png';
867 $img = 'down_pointer.png';
871 if (preg_match('/^(.+)\?.+$/',$PHP_SELF,$regs)) {
872 $source_url = $regs[1];
874 $source_url = $PHP_SELF;
877 /* Now that we have everything figured out, show the actual button. */
878 echo ' <a href="' . $source_url .'?newsort=' . $which
879 . '&startMessage=1&mailbox=' . urlencode($mailbox)
880 . '"><IMG SRC="../images/' . $img
881 . '" BORDER=0 WIDTH=12 HEIGHT=10 ALT="sort"></a>';
884 function get_selectall_link($start_msg, $sort) {
885 global $checkall, $what, $where, $mailbox, $javascript_on;
886 global $PHP_SELF, $PG_SHOWNUM;
889 if ($javascript_on) {
890 $safe_name = preg_replace("/[^0-9A-Za-z_]/", '_', $mailbox);
891 $func_name = "CheckAll" . $safe_name;
892 $form_name = "FormMsgs" . $safe_name;
893 $result = '<script language="JavaScript" type="text/javascript">'
895 . "function " . $func_name . "() {\n"
896 . " for (var i = 0; i < document." . $form_name . ".elements.length; i++) {\n"
897 . " if(document." . $form_name . ".elements[i].type == 'checkbox'){\n"
898 . " document." . $form_name . ".elements[i].checked = "
899 . " !(document." . $form_name . ".elements[i].checked);\n"
904 . '</script><a href="javascript:void(0)" onClick="' . $func_name . '();">' . _("Toggle All")
905 /* . '</script><a href="javascript:' . $func_name . '()">' . _("Toggle All")*/
908 if (strpos($PHP_SELF, "?")) {
909 $result .= "<a href=\"$PHP_SELF&mailbox=" . urlencode($mailbox)
910 . "&startMessage=$start_msg&sort=$sort&checkall=";
912 $result .= "<a href=\"$PHP_SELF?mailbox=" . urlencode($mailbox)
913 . "&startMessage=$start_msg&sort=$sort&checkall=";
915 if (isset($checkall) && $checkall == '1') {
921 if (isset($where) && isset($what)) {
922 $result .= '&where=' . urlencode($where)
923 . '&what=' . urlencode($what);
927 if (isset($checkall) && ($checkall == '1')) {
928 $result .= _("Unselect All");
930 $result .= _("Select All");
935 /* Return our final result. */
940 * This function computes the "Viewing Messages..." string.
942 function get_msgcnt_str($start_msg, $end_msg, $num_msgs) {
943 /* Compute the $msg_cnt_str. */
945 if ($start_msg < $end_msg) {
946 $result = sprintf(_("Viewing Messages: <B>%s</B> to <B>%s</B> (%s total)"),
947 $start_msg, $end_msg, $num_msgs);
948 } else if ($start_msg == $end_msg) {
949 $result = sprintf(_("Viewing Message: <B>%s</B> (1 total)"), $start_msg);
953 /* Return our result string. */
958 * Generate a paginator link.
960 function get_paginator_link($box, $start_msg, $use, $text) {
963 $result = "<A HREF=\"right_main.php?use_mailbox_cache=$use"
964 . "&startMessage=$start_msg&mailbox=$box\" "
965 . "TARGET=\"right\">$text</A>";
968 if (preg_match('/^(.+)\?.+$/',$PHP_SELF,$regs)) {
969 $source_url = $regs[1];
971 $source_url = $PHP_SELF;
974 $result = '<A HREF="'. $source_url . "?use_mailbox_cache=$use"
975 . "&startMessage=$start_msg&mailbox=$box\" "
976 . "TARGET=\"right\">$text</A>";
982 * This function computes the paginator string.
984 function get_paginator_str($box, $start_msg, $end_msg, $num_msgs,
986 global $username, $data_dir, $use_mailbox_cache, $color, $PG_SHOWNUM;
988 /* Initialize paginator string chunks. */
995 $box = urlencode($box);
997 /* Create simple strings that will be creating the paginator. */
998 $spc = ' '; /* This will be used as a space. */
999 $sep = '|'; /* This will be used as a seperator. */
1001 /* Get some paginator preference values. */
1002 $pg_sel = getPref($data_dir, $username, 'page_selector', SMPREF_ON
);
1003 $pg_max = getPref($data_dir, $username, 'page_selector_max', PG_SEL_MAX
);
1005 /* Make sure that our start message number is not too big. */
1006 $start_msg = min($start_msg, $num_msgs);
1008 /* Decide whether or not we will use the mailbox cache. */
1009 /* Not sure why $use_mailbox_cache is even passed in. */
1016 /* Compute the starting message of the previous and next page group. */
1017 $next_grp = $start_msg +
$show_num;
1018 $prev_grp = $start_msg - $show_num;
1020 /* Compute the basic previous and next strings. */
1021 if (($next_grp <= $num_msgs) && ($prev_grp >= 0)) {
1022 $prv_str = get_paginator_link($box, $prev_grp, $use, _("Previous"));
1023 $nxt_str = get_paginator_link($box, $next_grp, $use, _("Next"));
1024 } else if (($next_grp > $num_msgs) && ($prev_grp >= 0)) {
1025 $prv_str = get_paginator_link($box, $prev_grp, $use, _("Previous"));
1026 $nxt_str = "<FONT COLOR=\"$color[9]\">"._("Next")."</FONT>\n";
1027 } else if (($next_grp <= $num_msgs) && ($prev_grp < 0)) {
1028 $prv_str = "<FONT COLOR=\"$color[9]\">"._("Previous") . '</FONT>';
1029 $nxt_str = get_paginator_link($box, $next_grp, $use, _("Next"));
1032 /* Page selector block. Following code computes page links. */
1033 if ($pg_sel && ($num_msgs > $show_num)) {
1034 /* Most importantly, what is the current page!!! */
1035 $cur_pg = intval($start_msg / $show_num) +
1;
1037 /* Compute total # of pages and # of paginator page links. */
1038 $tot_pgs = ceil($num_msgs / $show_num); /* Total number of Pages */
1039 $vis_pgs = min($pg_max, $tot_pgs - 1); /* Visible Pages */
1041 /* Compute the size of the four quarters of the page links. */
1043 /* If we can, just show all the pages. */
1044 if (($tot_pgs - 1) <= $pg_max) {
1045 $q1_pgs = $cur_pg - 1;
1046 $q2_pgs = $q3_pgs = 0;
1047 $q4_pgs = $tot_pgs - $cur_pg;
1049 /* Otherwise, compute some magic to choose the four quarters. */
1052 * Compute the magic base values. Added together,
1053 * these values will always equal to the $pag_pgs.
1054 * NOTE: These are DEFAULT values and do not take
1055 * the current page into account. That is below.
1057 $q1_pgs = floor($vis_pgs/4);
1058 $q2_pgs = round($vis_pgs/4, 0);
1059 $q3_pgs = ceil($vis_pgs/4);
1060 $q4_pgs = round(($vis_pgs - $q2_pgs)/3, 0);
1062 /* Adjust if the first quarter contains the current page. */
1063 if (($cur_pg - $q1_pgs) < 1) {
1064 $extra_pgs = ($q1_pgs - ($cur_pg - 1)) +
$q2_pgs;
1065 $q1_pgs = $cur_pg - 1;
1067 $q3_pgs +
= ceil($extra_pgs / 2);
1068 $q4_pgs +
= floor($extra_pgs / 2);
1070 /* Adjust if the first and second quarters intersect. */
1071 } else if (($cur_pg - $q2_pgs - ceil($q2_pgs/3)) <= $q1_pgs) {
1072 $extra_pgs = $q2_pgs;
1073 $extra_pgs -= ceil(($cur_pg - $q1_pgs - 1) * 0.75);
1074 $q2_pgs = ceil(($cur_pg - $q1_pgs - 1) * 0.75);
1075 $q3_pgs +
= ceil($extra_pgs / 2);
1076 $q4_pgs +
= floor($extra_pgs / 2);
1078 /* Adjust if the fourth quarter contains the current page. */
1079 } else if (($cur_pg +
$q4_pgs) >= $tot_pgs) {
1080 $extra_pgs = ($q4_pgs - ($tot_pgs - $cur_pg)) +
$q3_pgs;
1082 $q4_pgs = $tot_pgs - $cur_pg;
1083 $q1_pgs +
= floor($extra_pgs / 2);
1084 $q2_pgs +
= ceil($extra_pgs / 2);
1086 /* Adjust if the third and fourth quarter intersect. */
1087 } else if (($cur_pg +
$q3_pgs +
1) >= ($tot_pgs - $q4_pgs +
1)) {
1088 $extra_pgs = $q3_pgs;
1089 $extra_pgs -= ceil(($tot_pgs - $cur_pg - $q4_pgs) * 0.75);
1090 $q3_pgs = ceil(($tot_pgs - $cur_pg - $q4_pgs) * 0.75);
1091 $q1_pgs +
= floor($extra_pgs / 2);
1092 $q2_pgs +
= ceil($extra_pgs / 2);
1097 * I am leaving this debug code here, commented out, because
1098 * it is a really nice way to see what the above code is doing.
1099 * echo "qts = $q1_pgs/$q2_pgs/$q3_pgs/$q4_pgs = "
1100 * . ($q1_pgs + $q2_pgs + $q3_pgs + $q4_pgs) . '<br>';
1103 /* Print out the page links from the compute page quarters. */
1105 /* Start with the first quarter. */
1106 if (($q1_pgs == 0) && ($cur_pg > 1)) {
1107 $pg_str .= "...$spc";
1109 for ($pg = 1; $pg <= $q1_pgs; ++
$pg) {
1110 $start = (($pg-1) * $show_num) +
1;
1111 $pg_str .= get_paginator_link($box, $start, $use, $pg) . $spc;
1113 if ($cur_pg - $q2_pgs - $q1_pgs > 1) {
1114 $pg_str .= "...$spc";
1118 /* Continue with the second quarter. */
1119 for ($pg = $cur_pg - $q2_pgs; $pg < $cur_pg; ++
$pg) {
1120 $start = (($pg-1) * $show_num) +
1;
1121 $pg_str .= get_paginator_link($box, $start, $use, $pg) . $spc;
1124 /* Now print the current page. */
1125 $pg_str .= $cur_pg . $spc;
1127 /* Next comes the third quarter. */
1128 for ($pg = $cur_pg +
1; $pg <= $cur_pg +
$q3_pgs; ++
$pg) {
1129 $start = (($pg-1) * $show_num) +
1;
1130 $pg_str .= get_paginator_link($box, $start, $use, $pg) . $spc;
1133 /* And last, print the forth quarter page links. */
1134 if (($q4_pgs == 0) && ($cur_pg < $tot_pgs)) {
1135 $pg_str .= "...$spc";
1137 if (($tot_pgs - $q4_pgs) > ($cur_pg +
$q3_pgs)) {
1138 $pg_str .= "...$spc";
1140 for ($pg = $tot_pgs - $q4_pgs +
1; $pg <= $tot_pgs; ++
$pg) {
1141 $start = (($pg-1) * $show_num) +
1;
1142 $pg_str .= get_paginator_link($box, $start, $use, $pg) . $spc;
1145 } else if ($PG_SHOWNUM == 999999) {
1146 $pg_str = "<A HREF=\"right_main.php?PG_SHOWALL=0"
1147 . "&use_mailbox_cache=$use&startMessage=1&mailbox=$box\" "
1148 . "TARGET=\"right\">" ._("Paginate") . '</A>' . $spc;
1151 /* If necessary, compute the 'show all' string. */
1152 if (($prv_str != '') ||
($nxt_str != '')) {
1153 $all_str = "<A HREF=\"right_main.php?PG_SHOWALL=1"
1154 . "&use_mailbox_cache=$use&startMessage=1&mailbox=$box\" "
1155 . "TARGET=\"right\">" . _("Show All") . '</A>';
1158 /* Last but not least, get the value for the toggle all link. */
1159 $tgl_str = get_selectall_link($start_msg, $sort);
1161 /* Put all the pieces of the paginator string together. */
1163 * Hairy code... But let's leave it like it is since I am not certain
1164 * a different approach would be any easier to read. ;)
1167 $result .= ($prv_str != '' ?
$prv_str . $spc . $sep . $spc : '');
1168 $result .= ($nxt_str != '' ?
$nxt_str . $spc . $sep . $spc : '');
1169 $result .= ($pg_str != '' ?
$pg_str : '');
1170 $result .= ($all_str != '' ?
$sep . $spc . $all_str . $spc : '');
1171 $result .= ($result != '' ?
$sep . $spc . $tgl_str: $tgl_str);
1173 /* If the resulting string is blank, return a non-breaking space. */
1174 if ($result == '') {
1178 /* Return our final magical paginator string. */
1182 function processSubject($subject, $threadlevel = 0) {
1183 global $languages, $squirrelmail_language;
1184 /* Shouldn't ever happen -- caught too many times in the IMAP functions */
1185 if ($subject == '') {
1186 return _("(no subject)");
1189 $trim_at = SUBJ_TRIM_AT
;
1191 /* if this is threaded, subtract two chars per indentlevel */
1192 if($threadlevel > 0 && $threadlevel <= 10) {
1193 $trim_at -= (2*$threadlevel);
1196 if (strlen($subject) <= $trim_at) {
1200 $ent_strlen = $orig_len = strlen($subject);
1201 $trim_val = $trim_at - 5;
1204 * see if this is entities-encoded string
1205 * If so, Iterate through the whole string, find out
1206 * the real number of characters, and if more
1207 * than 55, substr with an updated trim value.
1209 $step = $ent_loc = 0;
1210 while ( $ent_loc < $trim_val && (($ent_loc = strpos($subject, '&', $ent_offset)) !== false) &&
1211 (($ent_loc_end = strpos($subject, ';', $ent_loc+
3)) !== false) ) {
1212 $trim_val +
= ($ent_loc_end-$ent_loc);
1213 $ent_offset = $ent_loc_end+
1;
1217 if (($trim_val > 50) && (strlen($subject) > ($trim_val))&& (strpos($subject,';',$trim_val) < ($trim_val +
6))) {
1218 $i = strpos($subject,';',$trim_val);
1220 $trim_val = strpos($subject,';',$trim_val);
1223 if ($ent_strlen <= $trim_at){
1227 if (isset($languages[$squirrelmail_language]['XTRA_CODE']) &&
1228 function_exists($languages[$squirrelmail_language]['XTRA_CODE'])) {
1229 return $languages[$squirrelmail_language]['XTRA_CODE']('strimwidth', $subject, $trim_val);
1232 // only print '...' when we're actually dropping part of the subject
1233 if(strlen($subject) <= $trim_val) {
1236 return substr($subject, 0, $trim_val) . '...';
1240 function getMbxList($imapConnection) {
1241 global $lastTargetMailbox;
1242 echo ' <small> <tt><select name="targetMailbox">';
1243 echo sqimap_mailbox_option_list($imapConnection, array(strtolower($lastTargetMailbox)) );
1244 echo ' </SELECT></TT> ';
1247 function getButton($type, $name, $value) {
1248 return '<INPUT TYPE="'.$type.'" NAME="'.$name.'" VALUE="'.$value . '">';
1251 function getSmallStringCell($string, $align) {
1252 return html_tag('td',
1253 '<small>' . $string . ': </small>',
1259 function getEndMessage($start_msg, $show_num, $num_msgs) {
1260 if ($start_msg +
($show_num - 1) < $num_msgs){
1261 $end_msg = $start_msg +
($show_num - 1);
1263 $end_msg = $num_msgs;
1266 if ($end_msg < $start_msg) {
1267 $start_msg = $start_msg - $show_num;
1268 if ($start_msg < 1) {
1272 return (array($start_msg,$end_msg));
1275 function handleAsSent($mailbox) {
1276 global $handleAsSent_result;
1278 /* First check if this is the sent or draft folder. */
1279 $handleAsSent_result = isSentMailbox($mailbox) ||
isDraftMailbox($mailbox);
1281 /* Then check the result of the handleAsSent hook. */
1282 do_hook('check_handleAsSent_result', $mailbox);
1284 /* And return the result. */
1285 return $handleAsSent_result;