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');
19 require_once(SM_PATH
. 'functions/imap_messages.php');
20 require_once(SM_PATH
. 'functions/mime.php');
23 * PG_SEL_MAX: default value for page_selector_max
25 define('PG_SEL_MAX', 10);
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 $truncate_sender, /* number of characters for From/To field (<= 0 for unchanged) */
57 $color_string = $color[4];
59 if ($GLOBALS['alt_index_colors']) {
60 if (!isset($row_count)) {
65 if (!isset($color[12])) {
66 $color[12] = '#EAEAEA';
68 $color_string = $color[12];
73 if($mailbox == 'None') {
74 $boxes = sqimap_mailbox_list($imapConnection);
75 $mailbox = $boxes[0]['unformatted'];
78 $urlMailbox = urlencode($mailbox);
80 $bSentFolder = handleAsSent($mailbox);
81 // If the From address is the same as $email_address, then handle as Sent
82 $from_array = parseAddress($msg['FROM'], 1);
83 if (!isset($email_address)) {
84 global $datadir, $username;
85 $email_address = getPref($datadir, $username, 'email_address');
87 $bHandleAsSent = ($bSentFolder) ||
((isset($from_array[0][0])) && ($from_array[0][0] == $email_address));
88 // If this is a Sent message, display To address instead of From
90 $msg['FROM'] = $msg['TO'];
91 // Passing 1 below results in only 1 address being parsed, thus defeating the following code
92 $msg['FROM'] = parseAddress($msg['FROM']/*,1*/);
95 * This is done in case you're looking into Sent folders,
96 * because you can have multiple receivers.
98 $senderNames = $msg['FROM'];
101 if (sizeof($senderNames)){
102 foreach ($senderNames as $senderNames_part) {
103 if ($senderName != '') {
105 $senderAddress .= ', ';
107 $sender_address_part = htmlspecialchars($senderNames_part[0]);
108 $sender_name_part = str_replace(' ',' ', decodeHeader($senderNames_part[1]));
109 if ($sender_name_part) {
110 $senderName .= $sender_name_part;
111 $senderAddress .= $sender_name_part . ' <' . $sender_address_part . '>';
113 $senderName .= $sender_address_part;
114 $senderAddress .= $sender_address_part;
118 // If Sent, prefix with To: but only if not Sent folder
119 if ($bHandleAsSent ^
$bSentFolder) {
120 $senderName = _("To:") . ' ' . $senderName;
121 $senderAddress = _("To:") . ' ' . $senderAddress;
124 if ($truncate_sender > 0)
125 $senderName = truncateWithEntities($senderName, $truncate_sender);
127 echo html_tag( 'tr','','','','VALIGN="top"') . "\n";
129 if (isset($msg['FLAG_FLAGGED']) && ($msg['FLAG_FLAGGED'] == true)) {
130 $flag = "<font color=\"$color[2]\">";
131 $flag_end = '</font>';
136 if (!isset($msg['FLAG_SEEN']) ||
($msg['FLAG_SEEN'] == false)) {
143 if ($bHandleAsSent) {
145 $italic_end = '</i>';
150 if (isset($msg['FLAG_DELETED']) && $msg['FLAG_DELETED']) {
151 $fontstr = "<font color=\"$color[9]\">";
152 $fontstr_end = '</font>';
158 if ($where && $what) {
159 $searchstr = '&where='.$where.'&what='.$what;
164 if (is_array($message_highlight_list) && count($message_highlight_list)) {
165 $msg['TO'] = parseAddress($msg['TO']);
166 $msg['CC'] = parseAddress($msg['CC']);
167 foreach ($message_highlight_list as $message_highlight_list_part) {
168 if (trim($message_highlight_list_part['value']) != '') {
169 $high_val = strtolower($message_highlight_list_part['value']);
170 $match_type = strtoupper($message_highlight_list_part['match_type']);
171 if($match_type == 'TO_CC') {
172 $match = array('TO', 'CC');
174 $match = array($match_type);
176 foreach($match as $match_type) {
177 switch($match_type) {
181 foreach ($msg[$match_type] as $address) {
182 $address[0] = decodeHeader($address[0], true, false);
183 $address[1] = decodeHeader($address[1], true, false);
184 if (strstr('^^' . strtolower($address[0]), $high_val) ||
185 strstr('^^' . strtolower($address[1]), $high_val)) {
186 $hlt_color = $message_highlight_list_part['color'];
192 $headertest = strtolower(decodeHeader($msg[$match_type], true, false));
193 if (strstr('^^' . $headertest, $high_val)) {
194 $hlt_color = $message_highlight_list_part['color'];
204 if (!isset($hlt_color)) {
205 $hlt_color = $color_string;
207 $checked = ($checkall == 1) ?
' CHECKED' : '';
209 $msg['SUBJECT'] = str_replace(' ', ' ', decodeHeader($msg['SUBJECT']));
210 $subject = processSubject($msg['SUBJECT'], $indent_array[$msg['ID']]);
211 if (sizeof($index_order)) {
212 foreach ($index_order as $index_order_part) {
213 switch ($index_order_part) {
214 case 1: /* checkbox */
216 "<input type=checkbox name=\"msg[$t]\" value=\"".$msg['ID']."\"$checked>",
221 if ($senderAddress != $senderName) {
222 $senderAddress = strtr($senderAddress, array_flip(get_html_translation_table(HTML_SPECIALCHARS
)));
223 $title = ' title="' . str_replace('"', "''", $senderAddress) . '"';
228 $italic . $bold . $flag . $fontstr . $senderName .
229 $fontstr_end . $flag_end . $bold_end . $italic_end,
231 $hlt_color, $title );
234 $date_string = $msg['DATE_STRING'] . '';
235 if ($date_string == '') {
236 $date_string = _("Unknown date");
239 $bold . $flag . $fontstr . $date_string .
240 $fontstr_end . $flag_end . $bold_end,
245 case 4: /* subject */
247 if ($thread_sort_messages == 1) {
248 if (isset($indent_array[$msg['ID']])) {
249 $td_str .= str_repeat(" ",$indent_array[$msg['ID']]);
252 $td_str .= '<a href="read_body.php?mailbox='.$urlMailbox
253 . '&passed_id='. $msg["ID"]
254 . '&startMessage='.$start_msg.$searchstr.'"';
255 $td_str .= ' ' .concat_hook_function('subject_link', array($start_msg, $searchstr));
256 if ($subject != $msg['SUBJECT']) {
257 $title = get_html_translation_table(HTML_SPECIALCHARS
);
258 $title = array_flip($title);
259 $title = strtr($msg['SUBJECT'], $title);
260 $title = str_replace('"', "''", $title);
261 $td_str .= " title=\"$title\"";
263 $td_str .= ">$flag$subject$flag_end</a>$bold_end";
264 echo html_tag( 'td', $td_str, 'left', $hlt_color );
268 $td_str = "<b><small>";
270 if (isset($msg['FLAG_ANSWERED']) && $msg['FLAG_ANSWERED'] == true) {
274 if ($msg['TYPE0'] == 'multipart') {
278 if ($default_use_priority) {
279 if ( ($msg['PRIORITY'] == 1) ||
($msg['PRIORITY'] == 2) ) {
280 $td_str .= "<font color=\"$color[1]\">!</font>";
283 if ($msg['PRIORITY'] == 5) {
284 $td_str .= "<font color=\"$color[8]\">?</font>";
288 if (isset($msg['FLAG_DELETED']) && $msg['FLAG_DELETED'] == true) {
289 $td_str .= "<font color=\"$color[1]\">D</font>";
295 do_hook("msg_envelope");
296 $td_str .= '</small></b>';
305 $bold . $fontstr . show_readable_size($msg['SIZE']) .
306 $fontstr_end . $bold_end,
315 echo '</tr>' . "\n" . '<tr><td COLSPAN="' . $col . '" BGCOLOR="' .
316 $color[0] . '" HEIGHT="1"></td></tr>' . "\n";
322 function getServerMessages($imapConnection, $start_msg, $show_num, $num_msgs, $id) {
324 $id = array_slice($id, ($start_msg-1), $show_num);
325 $end = $start_msg +
$show_num - 1;
326 if ($num_msgs < $show_num) {
327 $end_loop = $num_msgs;
328 } else if ($end > $num_msgs) {
329 $end_loop = $num_msgs - $start_msg +
1;
331 $end_loop = $show_num;
333 return fillMessageArray($imapConnection,$id,$end_loop,$show_num);
339 function getThreadMessages($imapConnection, $start_msg, $show_num, $num_msgs) {
340 $id = get_thread_sort($imapConnection);
341 return getServerMessages($imapConnection, $start_msg, $show_num, $num_msgs, $id);
344 function getServerSortMessages($imapConnection, $start_msg, $show_num,
345 $num_msgs, $server_sort_order, $mbxresponse) {
346 $id = sqimap_get_sort_order($imapConnection, $server_sort_order,$mbxresponse);
347 return getServerMessages($imapConnection, $start_msg, $show_num, $num_msgs, $id);
350 function getSelfSortMessages($imapConnection, $start_msg, $show_num,
351 $num_msgs, $sort, $mbxresponse) {
353 if ($num_msgs >= 1) {
354 $id = sqimap_get_php_sort_order ($imapConnection, $mbxresponse);
358 /* set shownum to 999999 to fool sqimap_get_small_header_list
359 and rebuild the msgs_str to 1:* */
362 /* if it's not sorted */
363 if ($start_msg +
($show_num - 1) < $num_msgs) {
364 $end_msg = $start_msg +
($show_num - 1);
366 $end_msg = $num_msgs;
368 if ($end_msg < $start_msg) {
369 $start_msg = $start_msg - $show_num;
370 if ($start_msg < 1) {
374 $id = array_slice(array_reverse($id), ($start_msg-1), $show_num);
375 $end = $start_msg +
$show_num - 1;
376 if ($num_msgs < $show_num) {
377 $end_loop = $num_msgs;
378 } else if ($end > $num_msgs) {
379 $end_loop = $num_msgs - $start_msg +
1;
381 $end_loop = $show_num;
384 $msgs = fillMessageArray($imapConnection,$id,$end_loop, $show_num);
392 * This function loops through a group of messages in the mailbox
393 * and shows them to the user.
395 function showMessagesForMailbox($imapConnection, $mailbox, $num_msgs,
396 $start_msg, $sort, $color, $show_num,
397 $use_cache, $mode='') {
398 global $msgs, $msort, $auto_expunge, $thread_sort_messages,
399 $allow_server_sort, $server_sort_order;
402 * For some reason, on PHP 4.3+, this being unset, and set in the session causes havoc
403 * so setting it to an empty array beforehand seems to clean up the issue, and stopping the
404 * "Your script possibly relies on a session side-effect which existed until PHP 4.2.3" error
407 if (!isset($msort)) {
415 //$start = microtime();
416 /* If autoexpunge is turned on, then do it now. */
417 $mbxresponse = sqimap_mailbox_select($imapConnection, $mailbox);
419 /* If autoexpunge is turned on, then do it now. */
420 if ($auto_expunge == true) {
421 $exp_cnt = sqimap_mailbox_expunge($imapConnection, $mailbox, false, '');
422 $mbxresponse['EXISTS'] = $mbxresponse['EXISTS'] - $exp_cnt;
423 $num_msgs = $mbxresponse['EXISTS'];
426 if ($mbxresponse['EXISTS'] > 0) {
427 /* if $start_msg is lower than $num_msgs, we probably deleted all messages
428 * in the last page. We need to re-adjust the start_msg
431 if($start_msg > $num_msgs) {
432 $start_msg -= $show_num;
438 /* This code and the next if() block check for
439 * server-side sorting methods. The $id array is
440 * formatted and $sort is set to 6 to disable
441 * SM internal sorting
444 if ($thread_sort_messages == 1) {
446 } elseif ($allow_server_sort == 1) {
447 $mode = 'serversort';
453 sqgetGlobalVar('msgs', $msgs, SQ_SESSION
);
454 sqgetGlobalVar('msort', $msort, SQ_SESSION
);
456 sqsession_unregister('msort');
457 sqsession_unregister('msgs'); }
460 $id = get_thread_sort($imapConnection);
461 $msgs = getServerMessages($imapConnection, $start_msg, $show_num, $num_msgs, $id);
462 if ($msgs === false) {
463 echo '<b><small><center><font color=red>' .
464 _("Thread sorting is not supported by your IMAP server.<br>Please report this to the system administrator.").
465 '</center></small></b>';
466 $thread_sort_messages = 0;
467 $msort = $msgs = array();
474 $id = sqimap_get_sort_order($imapConnection, $sort, $mbxresponse);
475 $msgs = getServerMessages($imapConnection, $start_msg, $show_num, $num_msgs, $id);
476 if ($msgs === false) {
477 echo '<b><small><center><font color=red>' .
478 _( "Server-side sorting is not supported by your IMAP server.<br>Please report this to the system administrator.").
479 '</center></small></b>';
480 $sort = $server_sort_order;
481 $allow_server_sort = FALSE;
482 $msort = $msgs = array();
491 $msgs = getSelfSortMessages($imapConnection, $start_msg, $show_num,
492 $num_msgs, $sort, $mbxresponse);
493 $msort = calc_msort($msgs, $sort);
497 sqsession_register($msort, 'msort');
498 sqsession_register($msgs, 'msgs');
500 } /* if exists > 0 */
502 $res = getEndMessage($start_msg, $show_num, $num_msgs);
503 $start_msg = $res[0];
506 $paginator_str = get_paginator_str($mailbox, $start_msg, $end_msg,
507 $num_msgs, $show_num, $sort);
509 $msg_cnt_str = get_msgcnt_str($start_msg, $end_msg, $num_msgs);
511 do_hook('mailbox_index_before');
512 echo '<table border="0" width="100%" cellpadding="0" cellspacing="0">';
515 mail_message_listing_beginning($imapConnection, $mailbox, $sort,
516 $msg_cnt_str, $paginator_str, $start_msg);
518 /* line between the button area and the list */
519 echo '<tr><td HEIGHT="5" BGCOLOR="'.$color[4].'"></td></tr>';
522 echo ' <table width="100%" cellpadding="1" cellspacing="0" align="center"'.' border="0" bgcolor="'.$color[9].'">';
524 echo ' <table width="100%" cellpadding="1" cellspacing="0" align="center" border="0" bgcolor="'.$color[5].'">';
526 printHeader($mailbox, $srt, $color, !$thread_sort_messages);
528 displayMessageArray($imapConnection, $num_msgs, $start_msg,
529 $msort, $mailbox, $sort, $color, $show_num,0,0);
530 echo '</td></tr></table></td></tr></table>';
532 mail_message_listing_end($num_msgs, $paginator_str, $msg_cnt_str, $color);
533 echo '</td></tr></table>';
534 //$t = elapsed($start);
535 //echo("elapsed time = $t seconds\n");
538 function calc_msort($msgs, $sort) {
549 if (($sort == 0) ||
($sort == 1)) {
550 foreach ($msgs as $item) {
551 $msort[] = $item['TIME_STAMP'];
553 } elseif (($sort == 2) ||
($sort == 3)) {
554 foreach ($msgs as $item) {
555 $msort[] = $item['FROM-SORT'];
557 } elseif (($sort == 4) ||
($sort == 5)) {
558 foreach ($msgs as $item) {
559 $msort[] = $item['SUBJECT-SORT'];
574 function fillMessageArray($imapConnection, $id, $count, $show_num=false) {
575 return sqimap_get_small_header_list($imapConnection, $id, $show_num);
579 /* Generic function to convert the msgs array into an HTML table. */
580 function displayMessageArray($imapConnection, $num_msgs, $start_msg,
581 $msort, $mailbox, $sort, $color,
582 $show_num, $where=0, $what=0) {
583 global $imapServerAddress, $use_mailbox_cache, $index_order,
584 $indent_array, $thread_sort_messages, $allow_server_sort,
585 $server_sort_order, $PHP_SELF;
587 $res = getEndMessage($start_msg, $show_num, $num_msgs);
588 $start_msg = $res[0];
591 $urlMailbox = urlencode($mailbox);
593 /* get indent level for subject display */
594 if ($thread_sort_messages == 1 && $num_msgs) {
595 $indent_array = get_parent_level($imapConnection);
598 $real_startMessage = $start_msg;
600 if ($end_msg - $start_msg < $show_num - 1) {
601 $end_msg = $end_msg - $start_msg +
1;
603 } else if ($start_msg > $show_num) {
604 $end_msg = $show_num;
608 $endVar = $end_msg +
1;
611 * Loop through and display the info for each message.
612 * ($t is used for the checkbox number)
616 /* messages display */
619 /* if there's no messages in this folder */
622 "<BR><b>" . _("THIS FOLDER IS EMPTY") . "</b><BR> ",
625 'COLSPAN="' . count($index_order) . '"'
628 } elseif ($start_msg == $end_msg) {
629 /* if there's only one message in the box, handle it differently. */
641 } while (isset ($key) && ($k < $i));
642 printMessageInfo($imapConnection, $t, true, $key, $mailbox,
643 $real_startMessage, $where, $what);
652 } while (isset ($key) && ($k < $i));
655 if (!$i ||
$i == $endVar-1) $not_last = false;
656 printMessageInfo($imapConnection, $t, $not_last, $key, $mailbox,
657 $real_startMessage, $where, $what);
662 } while ($i && $i < $endVar);
667 * Displays the standard message list header. To finish the table,
668 * you need to do a "</table></table>";
670 * $moveURL is the URL to submit the delete/move form to
671 * $mailbox is the current mailbox
672 * $sort is the current sorting method (-1 for no sorting available [searches])
673 * $Message is a message that is centered on top of the list
674 * $More is a second line that is left aligned
677 function mail_message_listing_beginning ($imapConnection,
678 $mailbox = '', $sort = -1,
680 $paginator = ' ',
682 global $color, $auto_expunge, $base_uri, $thread_sort_messages,
683 $allow_thread_sort, $allow_server_sort, $server_sort_order,
686 $php_self = $PHP_SELF;
687 /* fix for incorrect $PHP_SELF */
688 if (strpos($php_self, 'move_messages.php')) {
689 $php_self = str_replace('move_messages.php', 'right_main.php', $php_self);
691 $urlMailbox = urlencode($mailbox);
693 if (preg_match('/^(.+)\?.+$/',$php_self,$regs)) {
694 $source_url = $regs[1];
696 $source_url = $php_self;
702 $moveFields = '<input type="hidden" name="msg" value="'.htmlspecialchars($msg).'">' .
703 '<input type="hidden" name="mailbox" value="'.htmlspecialchars($mailbox).'">' .
704 '<input type="hidden" name="startMessage" value="'.htmlspecialchars($start_msg).'">';
706 // $moveURL = "move_messages.php?msg=$msg&mailbox=$urlMailbox"
707 // . "&startMessage=$start_msg";
709 * This is the beginning of the message list table.
710 * It wraps around all messages
712 $safe_name = preg_replace("/[^0-9A-Za-z_]/", '_', $mailbox);
713 $form_name = "FormMsgs" . $safe_name;
714 echo '<form name="' . $form_name . '" method="post" action="move_messages.php">' ."\n"
716 . html_tag( 'table' ,
721 html_tag( 'td', $paginator, 'left' ) .
722 html_tag( 'td', $msg_cnt_str, 'right' )
724 , '', $color[4], 'border="0" width="100%" cellpadding="1" cellspacing="0"' )
727 , '', '', 'border="0" width="100%" cellpadding="1" cellspacing="0"' );
728 /* line between header and button area */
729 echo '<tr><td HEIGHT="5" BGCOLOR="'.$color[4].'"></td></tr>';
732 echo html_tag( 'tr' ) . "\n"
733 . html_tag( 'td' ,'' , 'left', '', '' )
734 . html_tag( 'table' ,'' , '', $color[9], 'border="0" width="100%" cellpadding="1" cellspacing="0"' )
736 . html_tag( 'table' ,'' , '', $color[0], 'border="0" width="100%" cellpadding="1" cellspacing="0"' )
738 getSmallStringCell(_("Move Selected To"), 'left', 'nowrap') .
739 getSmallStringCell(_("Transform Selected Messages"), 'right')
741 . html_tag( 'tr' ) ."\n"
742 . html_tag( 'td', '', 'left', '', 'valign="middle" nowrap' );
743 getMbxList($imapConnection);
744 echo getButton('SUBMIT', 'moveButton',_("Move")) . "\n";
745 echo getButton('SUBMIT', 'attache',_("Forward")) . "\n";
748 . html_tag( 'td', '', 'right', '', 'nowrap' );
752 if (!$auto_expunge) {
753 echo getButton('SUBMIT', 'expungeButton',_("Expunge"))
754 .' ' . _("mailbox") . "\n";
756 do_hook('mailbox_display_buttons');
757 echo getButton('SUBMIT', 'markRead',_("Read"));
758 echo getButton('SUBMIT', 'markUnread',_("Unread"));
759 echo getButton('SUBMIT', 'delete',_("Delete")) ." \n";
760 if (!strpos($php_self,'mailbox')) {
761 $location = $php_self.'?mailbox=INBOX&startMessage=1';
763 $location = $php_self;
765 echo '<INPUT TYPE="HIDDEN" NAME="location" VALUE="'.$location.'">';
769 /* draws thread sorting links */
770 if ($allow_thread_sort == TRUE) {
771 if ($thread_sort_messages == 1 ) {
773 $thread_name = _("Unthread View");
774 } elseif ($thread_sort_messages == 0) {
776 $thread_name = _("Thread View");
778 echo html_tag( 'tr' ,
780 ' <a href=' . $source_url . '?sort='
781 . "$sort" . '&start_messages=1&set_thread=' . "$set_thread"
782 . '&mailbox=' . urlencode($mailbox) . '><small>' . $thread_name
783 . '</a></small> '
784 , '', '', '' ) . html_tag( 'td', _("Bypass Trash") . '<input type="checkbox" name="bypass_trash">','right','','')
789 echo "</TABLE></td></tr></table></td></tr>\n";
791 do_hook('mailbox_form_before');
793 /* if using server sort we highjack the
794 * the $sort var and use $server_sort_order
795 * instead. but here we reset sort for a bit
798 if ($allow_server_sort == TRUE) {
799 $sort = $server_sort_order;
803 function mail_message_listing_end($num_msgs, $paginator_str, $msg_cnt_str, $color) {
805 /* space between list and footer */
806 echo '<tr><td HEIGHT="5" BGCOLOR="'.$color[4].'" COLSPAN="1">';
808 echo '</td></tr><tr><td>';
809 echo html_tag( 'table',
814 html_tag( 'td', $paginator_str ) .
815 html_tag( 'td', $msg_cnt_str, 'right' )
817 , '', $color[4], 'width="100%" border="0" cellpadding="1" cellspacing="0"' )
820 , '', $color[9], 'width="100%" border="0" cellpadding="1" cellspacing="0"' );
823 /* End of message-list table */
825 do_hook('mailbox_index_after');
829 function printHeader($mailbox, $sort, $color, $showsort=true) {
831 echo html_tag( 'tr' ,'' , 'center', $color[5] );
833 /* calculate the width of the subject column based on the
834 * widths of the other columns */
835 $widths = array(1=>1,2=>25,3=>5,4=>0,5=>1,6=>5);
837 foreach($index_order as $item) {
838 $subjectwidth -= $widths[$item];
841 foreach ($index_order as $item) {
843 case 1: /* checkbox */
845 echo html_tag( 'td' ,' ' , '', '', 'width="1%"' );
848 if (handleAsSent($mailbox)) {
849 echo html_tag( 'td' ,'' , 'left', '', 'width="25%"' )
850 . '<b>' . _("To") . '</b>';
852 echo html_tag( 'td' ,'' , 'left', '', 'width="25%"' )
853 . '<b>' . _("From") . '</b>';
856 ShowSortButton($sort, $mailbox, 2, 3);
861 echo html_tag( 'td' ,'' , 'left', '', 'width="5%" nowrap' )
862 . '<b>' . _("Date") . '</b>';
864 ShowSortButton($sort, $mailbox, 0, 1);
868 case 4: /* subject */
869 echo html_tag( 'td' ,'' , 'left', '', 'width="'.$subjectwidth.'%"' )
870 . '<b>' . _("Subject") . '</b>';
872 ShowSortButton($sort, $mailbox, 4, 5);
877 echo html_tag( 'td', '<b>' . _("Size") . '</b>', 'center', '', 'width="5%" nowrap' );
886 * This function shows the sort button. Isn't this a good comment?
888 function ShowSortButton($sort, $mailbox, $Up, $Down ) {
890 /* Figure out which image we want to use. */
891 if ($sort != $Up && $sort != $Down) {
892 $img = 'sort_none.png';
894 } elseif ($sort == $Up) {
895 $img = 'up_pointer.png';
898 $img = 'down_pointer.png';
902 if (preg_match('/^(.+)\?.+$/',$PHP_SELF,$regs)) {
903 $source_url = $regs[1];
905 $source_url = $PHP_SELF;
908 /* Now that we have everything figured out, show the actual button. */
909 echo ' <a href="' . $source_url .'?newsort=' . $which
910 . '&startMessage=1&mailbox=' . urlencode($mailbox)
911 . '"><img src="../images/' . $img
912 . '" border="0" width="12" height="10" alt="sort" title="'
913 . _("Click here to change the sorting of the message list") .'"></a>';
916 function get_selectall_link($start_msg, $sort) {
917 global $checkall, $what, $where, $mailbox, $javascript_on;
918 global $PHP_SELF, $PG_SHOWNUM;
921 if ($javascript_on) {
922 $safe_name = preg_replace("/[^0-9A-Za-z_]/", '_', $mailbox);
923 $func_name = "CheckAll" . $safe_name;
924 $form_name = "FormMsgs" . $safe_name;
925 $result = '<script language="JavaScript" type="text/javascript">'
927 . "function " . $func_name . "() {\n"
928 . " for (var i = 0; i < document." . $form_name . ".elements.length; i++) {\n"
929 . " if(document." . $form_name . ".elements[i].type == 'checkbox' && "
930 . "document." . $form_name . ".elements[i].name != 'bypass_trash'){\n"
931 . " document." . $form_name . ".elements[i].checked = "
932 . " !(document." . $form_name . ".elements[i].checked);\n"
937 . '</script><a href="javascript:void(0)" onClick="' . $func_name . '();">' . _("Toggle All")
938 /* . '</script><a href="javascript:' . $func_name . '()">' . _("Toggle All")*/
941 if (strpos($PHP_SELF, "?")) {
942 $result .= "<a href=\"$PHP_SELF&mailbox=" . urlencode($mailbox)
943 . "&startMessage=$start_msg&sort=$sort&checkall=";
945 $result .= "<a href=\"$PHP_SELF?mailbox=" . urlencode($mailbox)
946 . "&startMessage=$start_msg&sort=$sort&checkall=";
948 if (isset($checkall) && $checkall == '1') {
954 if (isset($where) && isset($what)) {
955 $result .= '&where=' . urlencode($where)
956 . '&what=' . urlencode($what);
960 if (isset($checkall) && ($checkall == '1')) {
961 $result .= _("Unselect All");
963 $result .= _("Select All");
968 /* Return our final result. */
973 * This function computes the "Viewing Messages..." string.
975 function get_msgcnt_str($start_msg, $end_msg, $num_msgs) {
976 /* Compute the $msg_cnt_str. */
978 if ($start_msg < $end_msg) {
979 $result = sprintf(_("Viewing Messages: <B>%s</B> to <B>%s</B> (%s total)"),
980 $start_msg, $end_msg, $num_msgs);
981 } else if ($start_msg == $end_msg) {
982 $result = sprintf(_("Viewing Message: <B>%s</B> (1 total)"), $start_msg);
986 /* Return our result string. */
991 * Generate a paginator link.
993 function get_paginator_link($box, $start_msg, $use, $text) {
996 $result = "<A HREF=\"right_main.php?use_mailbox_cache=$use"
997 . "&startMessage=$start_msg&mailbox=$box\" "
998 . "TARGET=\"right\">$text</A>";
1001 if (preg_match('/^(.+)\?.+$/',$PHP_SELF,$regs)) {
1002 $source_url = $regs[1];
1004 $source_url = $PHP_SELF;
1007 $result = '<A HREF="'. $source_url . "?use_mailbox_cache=$use"
1008 . "&startMessage=$start_msg&mailbox=$box\" "
1009 . "TARGET=\"right\">$text</A>";
1015 * This function computes the paginator string.
1017 function get_paginator_str($box, $start_msg, $end_msg, $num_msgs,
1019 global $username, $data_dir, $use_mailbox_cache, $color, $PG_SHOWNUM;
1021 /* Initialize paginator string chunks. */
1028 $box = urlencode($box);
1030 /* Create simple strings that will be creating the paginator. */
1031 $spc = ' '; /* This will be used as a space. */
1032 $sep = '|'; /* This will be used as a seperator. */
1034 /* Get some paginator preference values. */
1035 $pg_sel = getPref($data_dir, $username, 'page_selector', SMPREF_ON
);
1036 $pg_max = getPref($data_dir, $username, 'page_selector_max', PG_SEL_MAX
);
1038 /* Make sure that our start message number is not too big. */
1039 $start_msg = min($start_msg, $num_msgs);
1041 /* Decide whether or not we will use the mailbox cache. */
1042 /* Not sure why $use_mailbox_cache is even passed in. */
1049 /* Compute the starting message of the previous and next page group. */
1050 $next_grp = $start_msg +
$show_num;
1051 $prev_grp = $start_msg - $show_num;
1053 /* Compute the basic previous and next strings. */
1054 if (($next_grp <= $num_msgs) && ($prev_grp >= 0)) {
1055 $prv_str = get_paginator_link($box, $prev_grp, $use, _("Previous"));
1056 $nxt_str = get_paginator_link($box, $next_grp, $use, _("Next"));
1057 } else if (($next_grp > $num_msgs) && ($prev_grp >= 0)) {
1058 $prv_str = get_paginator_link($box, $prev_grp, $use, _("Previous"));
1059 $nxt_str = "<FONT COLOR=\"$color[9]\">"._("Next")."</FONT>\n";
1060 } else if (($next_grp <= $num_msgs) && ($prev_grp < 0)) {
1061 $prv_str = "<FONT COLOR=\"$color[9]\">"._("Previous") . '</FONT>';
1062 $nxt_str = get_paginator_link($box, $next_grp, $use, _("Next"));
1065 /* Page selector block. Following code computes page links. */
1066 if ($pg_sel && ($num_msgs > $show_num)) {
1067 /* Most importantly, what is the current page!!! */
1068 $cur_pg = intval($start_msg / $show_num) +
1;
1070 /* Compute total # of pages and # of paginator page links. */
1071 $tot_pgs = ceil($num_msgs / $show_num); /* Total number of Pages */
1072 $vis_pgs = min($pg_max, $tot_pgs - 1); /* Visible Pages */
1074 /* Compute the size of the four quarters of the page links. */
1076 /* If we can, just show all the pages. */
1077 if (($tot_pgs - 1) <= $pg_max) {
1078 $q1_pgs = $cur_pg - 1;
1079 $q2_pgs = $q3_pgs = 0;
1080 $q4_pgs = $tot_pgs - $cur_pg;
1082 /* Otherwise, compute some magic to choose the four quarters. */
1085 * Compute the magic base values. Added together,
1086 * these values will always equal to the $pag_pgs.
1087 * NOTE: These are DEFAULT values and do not take
1088 * the current page into account. That is below.
1090 $q1_pgs = floor($vis_pgs/4);
1091 $q2_pgs = round($vis_pgs/4, 0);
1092 $q3_pgs = ceil($vis_pgs/4);
1093 $q4_pgs = round(($vis_pgs - $q2_pgs)/3, 0);
1095 /* Adjust if the first quarter contains the current page. */
1096 if (($cur_pg - $q1_pgs) < 1) {
1097 $extra_pgs = ($q1_pgs - ($cur_pg - 1)) +
$q2_pgs;
1098 $q1_pgs = $cur_pg - 1;
1100 $q3_pgs +
= ceil($extra_pgs / 2);
1101 $q4_pgs +
= floor($extra_pgs / 2);
1103 /* Adjust if the first and second quarters intersect. */
1104 } else if (($cur_pg - $q2_pgs - ceil($q2_pgs/3)) <= $q1_pgs) {
1105 $extra_pgs = $q2_pgs;
1106 $extra_pgs -= ceil(($cur_pg - $q1_pgs - 1) * 0.75);
1107 $q2_pgs = ceil(($cur_pg - $q1_pgs - 1) * 0.75);
1108 $q3_pgs +
= ceil($extra_pgs / 2);
1109 $q4_pgs +
= floor($extra_pgs / 2);
1111 /* Adjust if the fourth quarter contains the current page. */
1112 } else if (($cur_pg +
$q4_pgs) >= $tot_pgs) {
1113 $extra_pgs = ($q4_pgs - ($tot_pgs - $cur_pg)) +
$q3_pgs;
1115 $q4_pgs = $tot_pgs - $cur_pg;
1116 $q1_pgs +
= floor($extra_pgs / 2);
1117 $q2_pgs +
= ceil($extra_pgs / 2);
1119 /* Adjust if the third and fourth quarter intersect. */
1120 } else if (($cur_pg +
$q3_pgs +
1) >= ($tot_pgs - $q4_pgs +
1)) {
1121 $extra_pgs = $q3_pgs;
1122 $extra_pgs -= ceil(($tot_pgs - $cur_pg - $q4_pgs) * 0.75);
1123 $q3_pgs = ceil(($tot_pgs - $cur_pg - $q4_pgs) * 0.75);
1124 $q1_pgs +
= floor($extra_pgs / 2);
1125 $q2_pgs +
= ceil($extra_pgs / 2);
1130 * I am leaving this debug code here, commented out, because
1131 * it is a really nice way to see what the above code is doing.
1132 * echo "qts = $q1_pgs/$q2_pgs/$q3_pgs/$q4_pgs = "
1133 * . ($q1_pgs + $q2_pgs + $q3_pgs + $q4_pgs) . '<br>';
1136 /* Print out the page links from the compute page quarters. */
1138 /* Start with the first quarter. */
1139 if (($q1_pgs == 0) && ($cur_pg > 1)) {
1140 $pg_str .= "...$spc";
1142 for ($pg = 1; $pg <= $q1_pgs; ++
$pg) {
1143 $start = (($pg-1) * $show_num) +
1;
1144 $pg_str .= get_paginator_link($box, $start, $use, $pg) . $spc;
1146 if ($cur_pg - $q2_pgs - $q1_pgs > 1) {
1147 $pg_str .= "...$spc";
1151 /* Continue with the second quarter. */
1152 for ($pg = $cur_pg - $q2_pgs; $pg < $cur_pg; ++
$pg) {
1153 $start = (($pg-1) * $show_num) +
1;
1154 $pg_str .= get_paginator_link($box, $start, $use, $pg) . $spc;
1157 /* Now print the current page. */
1158 $pg_str .= $cur_pg . $spc;
1160 /* Next comes the third quarter. */
1161 for ($pg = $cur_pg +
1; $pg <= $cur_pg +
$q3_pgs; ++
$pg) {
1162 $start = (($pg-1) * $show_num) +
1;
1163 $pg_str .= get_paginator_link($box, $start, $use, $pg) . $spc;
1166 /* And last, print the forth quarter page links. */
1167 if (($q4_pgs == 0) && ($cur_pg < $tot_pgs)) {
1168 $pg_str .= "...$spc";
1170 if (($tot_pgs - $q4_pgs) > ($cur_pg +
$q3_pgs)) {
1171 $pg_str .= "...$spc";
1173 for ($pg = $tot_pgs - $q4_pgs +
1; $pg <= $tot_pgs; ++
$pg) {
1174 $start = (($pg-1) * $show_num) +
1;
1175 $pg_str .= get_paginator_link($box, $start, $use, $pg) . $spc;
1178 } else if ($PG_SHOWNUM == 999999) {
1179 $pg_str = "<A HREF=\"right_main.php?PG_SHOWALL=0"
1180 . "&use_mailbox_cache=$use&startMessage=1&mailbox=$box\" "
1181 . "TARGET=\"right\">" ._("Paginate") . '</A>' . $spc;
1184 /* If necessary, compute the 'show all' string. */
1185 if (($prv_str != '') ||
($nxt_str != '')) {
1186 $all_str = "<A HREF=\"right_main.php?PG_SHOWALL=1"
1187 . "&use_mailbox_cache=$use&startMessage=1&mailbox=$box\" "
1188 . "TARGET=\"right\">" . _("Show All") . '</A>';
1191 /* Last but not least, get the value for the toggle all link. */
1192 $tgl_str = get_selectall_link($start_msg, $sort);
1194 /* Put all the pieces of the paginator string together. */
1196 * Hairy code... But let's leave it like it is since I am not certain
1197 * a different approach would be any easier to read. ;)
1200 $result .= ($prv_str != '' ?
$prv_str . $spc . $sep . $spc : '');
1201 $result .= ($nxt_str != '' ?
$nxt_str . $spc . $sep . $spc : '');
1202 $result .= ($pg_str != '' ?
$pg_str : '');
1203 $result .= ($all_str != '' ?
$sep . $spc . $all_str . $spc : '');
1204 $result .= ($result != '' ?
$sep . $spc . $tgl_str: $tgl_str);
1206 /* If the resulting string is blank, return a non-breaking space. */
1207 if ($result == '') {
1211 /* Return our final magical paginator string. */
1215 function truncateWithEntities($subject, $trim_at)
1217 $ent_strlen = strlen($subject);
1218 if (($trim_at <= 0) ||
($ent_strlen <= $trim_at))
1221 global $languages, $squirrelmail_language;
1224 * see if this is entities-encoded string
1225 * If so, Iterate through the whole string, find out
1226 * the real number of characters, and if more
1227 * than $trim_at, substr with an updated trim value.
1229 $trim_val = $trim_at;
1232 while ( $ent_loc < $trim_val && (($ent_loc = strpos($subject, '&', $ent_offset)) !== false) &&
1233 (($ent_loc_end = strpos($subject, ';', $ent_loc+
3)) !== false) ) {
1234 $trim_val +
= ($ent_loc_end-$ent_loc);
1235 $ent_offset = $ent_loc_end+
1;
1237 if (($trim_val > $trim_at) && ($ent_strlen > $trim_val) && (strpos($subject,';',$trim_val) < ($trim_val +
6))) {
1238 $i = strpos($subject,';',$trim_val);
1240 $trim_val = strpos($subject,';',$trim_val);
1243 // only print '...' when we're actually dropping part of the subject
1244 if ($ent_strlen <= $trim_val)
1247 if (isset($languages[$squirrelmail_language]['XTRA_CODE']) &&
1248 function_exists($languages[$squirrelmail_language]['XTRA_CODE'])) {
1249 return $languages[$squirrelmail_language]['XTRA_CODE']('strimwidth', $subject, $trim_val);
1252 return substr_replace($subject, '...', $trim_val);
1255 function processSubject($subject, $threadlevel = 0) {
1256 /* Shouldn't ever happen -- caught too many times in the IMAP functions */
1257 if ($subject == '') {
1258 return _("(no subject)");
1261 global $truncate_subject; /* number of characters for Subject field (<= 0 for unchanged) */
1262 $trim_at = $truncate_subject;
1264 /* if this is threaded, subtract two chars per indentlevel */
1265 if (($threadlevel > 0) && ($threadlevel <= 10))
1266 $trim_at -= (2*$threadlevel);
1268 return truncateWithEntities($subject, $trim_at);
1271 function getMbxList($imapConnection, $boxes = 0) {
1272 global $lastTargetMailbox;
1273 echo ' <small> <tt><select name="targetMailbox">';
1274 echo sqimap_mailbox_option_list($imapConnection, array(strtolower($lastTargetMailbox)), 0, $boxes);
1275 echo ' </SELECT></TT> ';
1278 function getButton($type, $name, $value) {
1279 return '<INPUT TYPE="'.$type.'" NAME="'.$name.'" VALUE="'.$value . '">';
1282 function getSmallStringCell($string, $align) {
1283 return html_tag('td',
1284 '<small>' . $string . ': </small>',
1290 function getEndMessage($start_msg, $show_num, $num_msgs) {
1291 if ($start_msg +
($show_num - 1) < $num_msgs){
1292 $end_msg = $start_msg +
($show_num - 1);
1294 $end_msg = $num_msgs;
1297 if ($end_msg < $start_msg) {
1298 $start_msg = $start_msg - $show_num;
1299 if ($start_msg < 1) {
1303 return (array($start_msg,$end_msg));
1306 function handleAsSent($mailbox) {
1307 global $handleAsSent_result;
1309 /* First check if this is the sent or draft folder. */
1310 $handleAsSent_result = isSentMailbox($mailbox) ||
isDraftMailbox($mailbox);
1312 /* Then check the result of the handleAsSent hook. */
1313 do_hook('check_handleAsSent_result', $mailbox);
1315 /* And return the result. */
1316 return $handleAsSent_result;