Source for file mailbox_display.php

Documentation is available at mailbox_display.php

  1. <?php
  2.  
  3. /**
  4.  * mailbox_display.php
  5.  *
  6.  * This contains functions that display mailbox information, such as the
  7.  * table row that has sender, date, subject, etc...
  8.  *
  9.  * @copyright 1999-2020 The SquirrelMail Project Team
  10.  * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  11.  * @version $Id: mailbox_display.php 14840 2020-01-07 07:42:38Z pdontthink $
  12.  * @package squirrelmail
  13.  */
  14.  
  15. /** The standard includes.. */
  16. require_once(SM_PATH . 'functions/strings.php');
  17. require_once(SM_PATH . 'functions/html.php');
  18. require_once(SM_PATH . 'class/html.class.php');
  19. require_once(SM_PATH . 'functions/imap_mailbox.php');
  20.  
  21. /* Constants:
  22.  *   PG_SEL_MAX:   default value for page_selector_max
  23.  *   SUBJ_TRIM_AT: the length at which we trim off subjects
  24.  */
  25. define('PG_SEL_MAX', 10);
  26. define('SUBJ_TRIM_AT', 55);
  27.  
  28. function elapsed($start)
  29. {
  30.    $end = microtime();
  31.    list($start2, $start1) = explode(" ", $start);
  32.    list($end2, $end1) = explode(" ", $end);
  33.    $diff1 = $end1 - $start1;
  34.    $diff2 = $end2 - $start2;
  35.    if( $diff2 < 0 ){
  36.        $diff1 -= 1;
  37.        $diff2 += 1.0;
  38.    }
  39.    return $diff2 + $diff1;
  40. }
  41.  
  42. function printMessageInfo($imapConnection, $t, $not_last=true, $key, $mailbox,
  43.                           $start_msg, $where, $what) {
  44.     global $checkall, $preselected,
  45.            $color, $msgs, $msort, $td_str, $msg,
  46.            $default_use_priority,
  47.            $message_highlight_list,
  48.            $index_order,
  49.            $indent_array,   /* indent subject by */
  50.            $pos,            /* Search postion (if any)  */
  51.            $thread_sort_messages, /* thread sorting on/off */
  52.            $server_sort_order, /* sort value when using server-sorting */
  53.            $row_count,
  54.            $allow_server_sort, /* enable/disable server-side sorting */
  55.            $truncate_subject,
  56.            $truncate_sender,
  57.            $internal_date_sort;
  58.  
  59.     sqgetGlobalVar('sort', $sort, SQ_SESSION);
  60.  
  61.     $color_string = $color[4];
  62.  
  63.     if ($GLOBALS['alt_index_colors']) {
  64.         if (!isset($row_count)) {
  65.             $row_count = 0;
  66.         }
  67.         $row_count++;
  68.         if ($row_count % 2) {
  69.             if (!isset($color[12])) {
  70.                 $color[12] = '#EAEAEA';
  71.             }
  72.             $color_string = $color[12];
  73.         }
  74.     }
  75.     $msg = $msgs[$key];
  76.  
  77.     if($mailbox == 'None') {
  78.         $boxes   = sqimap_mailbox_list($imapConnection);
  79.         $mailbox = $boxes[0]['unformatted'];
  80.         unset($boxes);
  81.     }
  82.     $urlMailbox = urlencode($mailbox);
  83.  
  84.     if (handleAsSent($mailbox)) {
  85.        $msg['FROM'] = $msg['TO'];
  86.     }
  87.     $msg['FROM'] = parseAddress($msg['FROM'],1);
  88.  
  89.  
  90.        /*
  91.         * This is done in case you're looking into Sent folders,
  92.         * because you can have multiple receivers.
  93.         */
  94.  
  95.     $senderNames = $msg['FROM'];
  96.     $senderName  = '';
  97.     $senderFrom = '';
  98.  
  99.  
  100.     if (sizeof($senderNames)){
  101.         foreach ($senderNames as $senderNames_part) {
  102.             if ($senderName != '') {
  103.                 $senderName .= ', ';
  104.             }
  105.  
  106.             if ($senderFrom != '') {
  107.                 $senderFrom .= ', ';
  108.             }
  109.  
  110.             if ($senderNames_part[1]) {
  111.                 $senderName .= decodeHeader($senderNames_part[1]);
  112.             } else {
  113.                 $senderName .= sm_encode_html_special_chars($senderNames_part[0]);
  114.             }
  115.  
  116.             $senderFrom .= sm_encode_html_special_chars($senderNames_part[0]);
  117.         }
  118.     }
  119.     $senderName = str_replace('&nbsp;',' ',$senderName);
  120.     if (substr($senderName, 0, 6) == '&quot;'
  121.      && substr($senderName, -6) == '&quot;')
  122.         $senderName = substr(substr($senderName, 0, -6), 6);
  123.     echo html_tag( 'tr','','','','valign="top"') . "\n";
  124.  
  125.     if (isset($msg['FLAG_FLAGGED']) && ($msg['FLAG_FLAGGED'] == true)) {
  126.         $flag = "<font color=\"$color[2]\">";
  127.         $flag_end = '</font>';
  128.     } else {
  129.         $flag = '';
  130.         $flag_end = '';
  131.     }
  132.     if (!isset($msg['FLAG_SEEN']) || ($msg['FLAG_SEEN'] == false)) {
  133.         $bold = '<b>';
  134.         $bold_end = '</b>';
  135.     } else {
  136.         $bold = '';
  137.         $bold_end = '';
  138.     }
  139.     if (handleAsSent($mailbox)) {
  140.         $italic = '<i>';
  141.         $italic_end = '</i>';
  142.     } else {
  143.         $italic = '';
  144.         $italic_end = '';
  145.     }
  146.     if (isset($msg['FLAG_DELETED']) && $msg['FLAG_DELETED']) {
  147.         $fontstr = "<font color=\"$color[9]\">";
  148.         $fontstr_end = '</font>';
  149.     } else {
  150.         $fontstr = '';
  151.         $fontstr_end = '';
  152.     }
  153.  
  154.     if ($where && $what) {
  155.         $searchstr = '&amp;where='.$where.'&amp;what='.$what;
  156.     } else {
  157.         $searchstr = '';
  158.     }
  159.  
  160.     if (is_array($message_highlight_list) && count($message_highlight_list)) {
  161.         $msg['TO'] = parseAddress($msg['TO']);
  162.         $msg['CC'] = parseAddress($msg['CC']);
  163.         $decoded_addresses = array();
  164.         foreach ($message_highlight_list as $message_highlight_list_part) {
  165.             if (trim($message_highlight_list_part['value']) != '') {
  166.                 $high_val   = strtolower($message_highlight_list_part['value']);
  167.                 $match_type = strtoupper($message_highlight_list_part['match_type']);
  168.                 if($match_type == 'TO_CC') {
  169.                     $match = array('TO', 'CC');
  170.                 } else {
  171.                     $match = array($match_type);
  172.                 }
  173.                 foreach($match as $match_type) {
  174.                     switch($match_type) {
  175.                         case('TO'):
  176.                         case('CC'):
  177.                         case('FROM'):
  178.                             foreach ($msg[$match_type] as $i => $address) {
  179.                                 if (empty($decoded_addresses[$match_type][$i])) {
  180.                                     $decoded_addresses[$match_type][$i][0] = decodeHeader($address[0], true, false);
  181.                                     $decoded_addresses[$match_type][$i][1] = decodeHeader($address[1], true, false);
  182.                                 }
  183.                                 $address = $decoded_addresses[$match_type][$i];
  184.                                 if (strstr('^^' . strtolower($address[0]), $high_val) ||
  185.                                     strstr('^^' . strtolower($address[1]), $high_val)) {
  186.                                     $hlt_color = $message_highlight_list_part['color'];
  187.                                     break 4;
  188.                                 }
  189.                             }
  190.                             break;
  191.                         default:
  192.                             $headertest = strtolower(decodeHeader($msg[$match_type], true, false));
  193.                             if (strstr('^^' . $headertest, $high_val)) {
  194.                                 $hlt_color = $message_highlight_list_part['color'];
  195.                                 break 3;
  196.                             }
  197.                             break;
  198.                     }
  199.                 }
  200.             }
  201.         }
  202.     }
  203.  
  204.     if (!isset($hlt_color)) {
  205.         $hlt_color = $color_string;
  206.     }
  207.     if ($checkall == 1 || in_array($msg['ID'], $preselected))
  208.         $checked = ' checked="checked"';
  209.     else
  210.         $checked = '';
  211.     $col = 0;
  212.     $msg['SUBJECT'] = decodeHeader($msg['SUBJECT']);
  213.     $subject = sm_truncate_string(str_replace('&nbsp;',' ',$msg['SUBJECT']), $truncate_subject, '...', TRUE);
  214.     if (sizeof($index_order)) {
  215.         foreach ($index_order as $index_order_part) {
  216.             switch ($index_order_part) {
  217.             case 1: /* checkbox */
  218.                 echo html_tag( 'td',
  219.                                "<input type=\"checkbox\" name=\"msg[$t]\" id=\"msg".$msg['ID'].
  220.                                    "\" value=\"".$msg['ID']."\"$checked>",
  221.                                'center',
  222.                                $hlt_color );
  223.                 break;
  224.             case 2: /* from */
  225.                 $from_xtra = '';
  226.                 $from_xtra = 'title="' . $senderFrom . '"';
  227.                 if (trim($senderName) == '') {
  228.                     $senderName = '(' . _("unknown") . ')';
  229.                 }
  230.                 echo html_tag( 'td',
  231.                     html_tag('label',
  232.                                $italic . $bold . $flag . $fontstr . sm_truncate_string($senderName, $truncate_sender, '...', TRUE) .
  233.                                $fontstr_end . $flag_end . $bold_end . $italic_end,
  234.                            '','','for="msg'.$msg['ID'].'"'),
  235.                            'left',
  236.                            $hlt_color, $from_xtra );
  237.                 break;
  238.             case 3: /* date */
  239.                 // show internal date if using it to sort
  240.                 if ($internal_date_sort && ($sort == 0 || $sort == 1)) {
  241.                     $date_string = $msg['RECEIVED_DATE_STRING'] . '';
  242.                     $date_title_string = $msg['RECEIVED_DATE_STRING_FULL'] . '';
  243.                 } else {
  244.                     $date_string = $msg['DATE_STRING'] . '';
  245.                     $date_title_string = $msg['DATE_STRING_FULL'] . '';
  246.                 }
  247.                 if ($date_string == '') {
  248.                     $date_string = _("Unknown date");
  249.                 }
  250.                 echo html_tag( 'td',
  251.                                $bold . $flag . $fontstr . $date_string .
  252.                                $fontstr_end . $flag_end . $bold_end,
  253.                                'center',
  254.                                $hlt_color,
  255.                                'nowrap title="' . $date_title_string . '"' );
  256.                 break;
  257.             case 4: /* subject */
  258.                 if (trim($subject) == '') {
  259.                     $subject = '(' . _("unknown") . ')';
  260.                 }
  261.                 $td_str = $bold;
  262.                 if ($thread_sort_messages == 1) {
  263.                     if (isset($indent_array[$msg['ID']])) {
  264.                         $td_str .= str_repeat("&nbsp;&nbsp;&nbsp;&nbsp;",$indent_array[$msg['ID']]);
  265.                     }
  266.                 }
  267.                 $td_str .= '<a href="read_body.php?mailbox='.$urlMailbox
  268.                         .  '&amp;passed_id='. $msg["ID"]
  269.                         .  '&amp;startMessage='.$start_msg.$searchstr.'"';
  270.                 $td_str .= ' ' .concat_hook_function('subject_link', array($start_msg, $searchstr));
  271.                 // Nah, it's nice to always have a roll-over
  272.                 //if ($subject != $msg['SUBJECT']) {
  273.                     $title = get_html_translation_table(HTML_SPECIALCHARS);
  274.                     $title = array_flip($title);
  275.                     $title = strtr($msg['SUBJECT'], $title);
  276.                     $title = str_replace('"', "''", $title);
  277.                     $td_str .= " title=\"$title\"";
  278.                 //}
  279.                 $td_str .= ">$flag$subject$flag_end</a>$bold_end";
  280.                 echo html_tag( 'td', $td_str, 'left', $hlt_color );
  281.                 break;
  282.             case 5: /* flags */
  283.                 $stuff = false;
  284.                 $td_str = "<b><small>";
  285.  
  286.                 if (isset($msg['FLAG_ANSWERED']) && $msg['FLAG_ANSWERED'] == true) {
  287.                     // i18n: "A" is short for "Answered". Make sure that two icon strings aren't translated to the same character (only in 1.5).
  288.                     $td_str .= _("A");
  289.                     $stuff = true;
  290.                 }
  291.                 if ($msg['TYPE0'] == 'multipart' && $msg['TYPE1'] == 'mixed') {
  292.                     $td_str .= '+';
  293.                     $stuff = true;
  294.                 }
  295.                 if ($default_use_priority) {
  296.                     if ( ($msg['PRIORITY'] == 1) || ($msg['PRIORITY'] == 2) ) {
  297.                         $td_str .= "<font color=\"$color[1]\">!</font>";
  298.                         $stuff = true;
  299.                     }
  300.                     if ($msg['PRIORITY'] == 5) {
  301.                         $td_str .= "<font color=\"$color[8]\">?</font>";
  302.                         $stuff = true;
  303.                     }
  304.                 }
  305.                 if (isset($msg['FLAG_DELETED']) && $msg['FLAG_DELETED'] == true) {
  306.                     $td_str .= "<font color=\"$color[1]\">D</font>";
  307.                     $stuff = true;
  308.                 }
  309.                 if (!$stuff) {
  310.                     $td_str .= '&nbsp;';
  311.                 }
  312.                 do_hook("msg_envelope");
  313.                 $td_str .= '</small></b>';
  314.                 echo html_tag( 'td',
  315.                                $td_str,
  316.                                'center',
  317.                                $hlt_color,
  318.                                'nowrap' );
  319.                 break;
  320.             case 6: /* size */
  321.                 echo html_tag( 'td',
  322.                                $bold . $fontstr . show_readable_size($msg['SIZE']) .
  323.                                $fontstr_end . $bold_end,
  324.                                'right',
  325.                                $hlt_color );
  326.                 break;
  327.             }
  328.             ++$col;
  329.         }
  330.     }
  331.     if ($not_last) {
  332.         echo '</tr>' . "\n" . '<tr><td colspan="' . $col . '" bgcolor="' .
  333.              $color[0] . '" height="1"></td></tr>' . "\n";
  334.     } else {
  335.         echo '</tr>'."\n";
  336.     }
  337. }
  338.  
  339. function getServerMessages($imapConnection, $start_msg, $show_num, $num_msgs, $id) {
  340.     if ($id != 'no') {
  341.         $id = array_slice($id, ($start_msg-1), $show_num);
  342.         $end = $start_msg + $show_num - 1;
  343.         if ($num_msgs < $show_num) {
  344.             $end_loop = $num_msgs;
  345.         } else if ($end > $num_msgs) {
  346.             $end_loop = $num_msgs - $start_msg + 1;
  347.         } else {
  348.             $end_loop = $show_num;
  349.         }
  350.         return fillMessageArray($imapConnection,$id,$end_loop,$show_num);
  351.     } else {
  352.         return false;
  353.     }
  354. }
  355.  
  356. function getThreadMessages($imapConnection, $start_msg, $show_num, $num_msgs) {
  357.     $id = get_thread_sort($imapConnection);
  358.     return getServerMessages($imapConnection, $start_msg, $show_num, $num_msgs, $id);
  359. }
  360.  
  361. function getServerSortMessages($imapConnection, $start_msg, $show_num,
  362.                                $num_msgs, $server_sort_order, $mbxresponse) {
  363.     $id = sqimap_get_sort_order($imapConnection, $server_sort_order,$mbxresponse);
  364.     return getServerMessages($imapConnection, $start_msg, $show_num, $num_msgs, $id);
  365. }
  366.  
  367. function getSelfSortMessages($imapConnection, $start_msg, $show_num,
  368.                               $num_msgs, $sort, $mbxresponse) {
  369.     $msgs = array();
  370.     if ($num_msgs >= 1) {
  371.         $id = sqimap_get_php_sort_order ($imapConnection, $mbxresponse);
  372.         if ($sort != 6 ) {
  373.             $end = $num_msgs;
  374.             $end_loop = $end;
  375.             /* set shownum to 999999 to fool sqimap_get_small_header_list
  376.                and rebuild the msgs_str to 1:* */
  377.             $show_num = 999999;
  378.         } else {
  379.             /* if it's not sorted */
  380.             if ($start_msg + ($show_num - 1) < $num_msgs) {
  381.                 $end_msg = $start_msg + ($show_num - 1);
  382.             } else {
  383.                 $end_msg = $num_msgs;
  384.             }
  385.             if ($end_msg < $start_msg) {
  386.                 $start_msg = $start_msg - $show_num;
  387.                 if ($start_msg < 1) {
  388.                     $start_msg = 1;
  389.                 }
  390.             }
  391.             $id = array_slice(array_reverse($id), ($start_msg-1), $show_num);
  392.             $end = $start_msg + $show_num - 1;
  393.             if ($num_msgs < $show_num) {
  394.                 $end_loop = $num_msgs;
  395.             } else if ($end > $num_msgs) {
  396.                 $end_loop = $num_msgs - $start_msg + 1;
  397.             } else {
  398.                 $end_loop = $show_num;
  399.             }
  400.         }
  401.         $msgs = fillMessageArray($imapConnection,$id,$end_loop, $show_num);
  402.     }
  403.     return $msgs;
  404. }
  405.  
  406.  
  407.  
  408. /*
  409.  * This function loops through a group of messages in the mailbox
  410.  * and shows them to the user.
  411.  */
  412. function showMessagesForMailbox($imapConnection, $mailbox, $num_msgs,
  413.                                 $start_msg, $sort, $color, $show_num,
  414.                                 $use_cache, $mode='') {
  415.     global $msgs, $msort, $auto_expunge, $thread_sort_messages,
  416.            $allow_server_sort, $server_sort_order;
  417.  
  418.     /*
  419.      * For some reason, on PHP 4.3+, this being unset, and set in the session causes havoc
  420.      * so setting it to an empty array beforehand seems to clean up the issue, and stopping the
  421.      * "Your script possibly relies on a session side-effect which existed until PHP 4.2.3" error
  422.      */
  423.  
  424.     if (!isset($msort)) {
  425.         $msort = array();
  426.     }
  427.  
  428.     if (!isset($msgs)) {
  429.         $msgs = array();
  430.     }
  431.  
  432.     //$start = microtime();
  433.     /* If autoexpunge is turned on, then do it now. */
  434.     $mbxresponse = sqimap_mailbox_select($imapConnection, $mailbox);
  435.     $srt = $sort;
  436.     /* If autoexpunge is turned on, then do it now. */
  437.     if ($auto_expunge == true) {
  438.         $exp_cnt = sqimap_mailbox_expunge($imapConnection, $mailbox, false, '');
  439.         $mbxresponse['EXISTS'] = $mbxresponse['EXISTS'] - $exp_cnt;
  440.         $num_msgs = $mbxresponse['EXISTS'];
  441.     }
  442.  
  443.     if ($mbxresponse['EXISTS'] > 0) {
  444.         /* if $start_msg is lower than $num_msgs, we probably deleted all messages
  445.          * in the last page. We need to re-adjust the start_msg
  446.          */
  447.  
  448.         if($start_msg > $num_msgs) {
  449.             $start_msg -= $show_num;
  450.             if($start_msg < 1) {
  451.                 $start_msg = 1;
  452.             }
  453.         }
  454.  
  455.         /* This code and the next if() block check for
  456.          * server-side sorting methods. The $id array is
  457.          * formatted and $sort is set to 6 to disable
  458.          * SM internal sorting
  459.          */
  460.  
  461.         if ($thread_sort_messages == 1) {
  462.             $mode = 'thread';
  463.         } elseif ($allow_server_sort == 1) {
  464.             $mode = 'serversort';
  465.         } else {
  466.             $mode = '';
  467.         }
  468.  
  469.         if ($use_cache) {
  470.             sqgetGlobalVar('msgs', $msgs, SQ_SESSION);
  471.             sqgetGlobalVar('msort', $msort, SQ_SESSION);
  472.         } else {
  473.             sqsession_unregister('msort');
  474.             sqsession_unregister('msgs');
  475.         }
  476.         switch ($mode) {
  477.             case 'thread':
  478.                 $id   = get_thread_sort($imapConnection);
  479.                 $msgs = getServerMessages($imapConnection, $start_msg, $show_num, $num_msgs, $id);
  480.                 if ($msgs === false) {
  481.                     echo '<b><small><center><font color="red">' .
  482.                         _("Thread sorting is not supported by your IMAP server.") . '<br />' .
  483.                         _("Please contact your system administrator and report this error.") .
  484.                         '</font></center></small></b>';
  485.                     $thread_sort_messages = 0;
  486.                     $msort = $msgs = array();
  487.                 } else {
  488.                     $msort= $msgs;
  489.                     $sort = 6;
  490.                 }
  491.                 break;
  492.             case 'serversort':
  493.                 $id   = sqimap_get_sort_order($imapConnection, $sort, $mbxresponse);
  494.                 $msgs = getServerMessages($imapConnection, $start_msg, $show_num, $num_msgs, $id);
  495.                 if ($msgs === false) {
  496.                     echo '<b><small><center><font color="red">' .
  497.                         _( "Server-side sorting is not supported by your IMAP server.") . '<br />' .
  498.                         _("Please contact your system administrator and report this error.") .
  499.                         '</font></center></small></b>';
  500.                     $sort = $server_sort_order;
  501.                     $allow_server_sort = FALSE;
  502.                     $msort = $msgs = array();
  503.                     $id = array();
  504.                 } else {
  505.                     $msort = $msgs;
  506.                     $sort = 6;
  507.                 }
  508.                 break;
  509.             default:
  510.                 if (!$use_cache) {
  511.                     $msgs = getSelfSortMessages($imapConnection, $start_msg, $show_num,
  512.                                                 $num_msgs, $sort, $mbxresponse);
  513.                     $msort = calc_msort($msgs, $sort, $mailbox);
  514.                 } /* !use cache */
  515.                 break;
  516.         } // switch
  517.         sqsession_register($msort, 'msort');
  518.         sqsession_register($msgs,  'msgs');
  519.  
  520.     } /* if exists > 0 */
  521.  
  522.     $res = getEndMessage($start_msg, $show_num, $num_msgs);
  523.     $start_msg = $res[0];
  524.     $end_msg   = $res[1];
  525.  
  526.     if ($num_msgs > 0) {
  527.         $paginator_str = get_paginator_str($mailbox, $start_msg, $end_msg,
  528.                                            $num_msgs, $show_num, $sort);
  529.     } else {
  530.         $paginator_str = '';
  531.     }
  532.  
  533.     $msg_cnt_str = get_msgcnt_str($start_msg, $end_msg, $num_msgs);
  534.  
  535.     do_hook('mailbox_index_before');
  536.  
  537.     $safe_name = preg_replace("/[^0-9A-Za-z_]/", '_', $mailbox);
  538.     $form_name = "FormMsgs" . $safe_name;
  539.     echo '<form name="' . $form_name . '" method="post" action="move_messages.php">' ."\n" .
  540.         '<input type="hidden" name="smtoken" value="'.sm_generate_security_token().'">' . "\n" .
  541.         '<input type="hidden" name="mailbox" value="'.sm_encode_html_special_chars($mailbox).'">' . "\n" .
  542.         '<input type="hidden" name="startMessage" value="'.sm_encode_html_special_chars($start_msg).'">' . "\n";
  543.     
  544.     echo '<table border="0" width="100%" cellpadding="0" cellspacing="0">';
  545.     echo '<tr><td>';
  546.  
  547.     mail_message_listing_beginning($imapConnection, $mailbox, $sort,
  548.                                   $msg_cnt_str, $paginator_str, $start_msg);
  549.     /* line between the button area and the list */
  550.     echo '<tr><td height="5" bgcolor="'.$color[4].'"></td></tr>';
  551.  
  552.     echo '<tr><td>';
  553.     echo '    <table width="100%" cellpadding="1" cellspacing="0" align="center"'.' border="0" bgcolor="'.$color[9].'">';
  554.     echo '     <tr><td>';
  555.     echo '       <table width="100%" cellpadding="1" cellspacing="0" align="center" border="0" bgcolor="'.$color[5].'">';
  556.     printHeader($mailbox, $srt, $color, !$thread_sort_messages);
  557.  
  558.     displayMessageArray($imapConnection, $num_msgs, $start_msg,
  559.                         $msort, $mailbox, $sort, $color, $show_num,0,0);
  560.     echo '</table></td></tr></table>';
  561.  
  562.     mail_message_listing_end($num_msgs, $paginator_str, $msg_cnt_str, $color);
  563.     echo '</table>';
  564.  
  565.     echo "\n</form>\n\n";
  566.     
  567.     //$t = elapsed($start);
  568.     //echo("elapsed time = $t seconds\n");
  569. }
  570.  
  571. function calc_msort($msgs, $sort, $mailbox = 'INBOX') {
  572.  
  573.     /*
  574.      * 0 = Date (up)
  575.      * 1 = Date (dn)
  576.      * 2 = Name (up)
  577.      * 3 = Name (dn)
  578.      * 4 = Subject (up)
  579.      * 5 = Subject (dn)
  580.      * 6 = default no sort
  581.      * 7 - UNUSED
  582.      * 8 = Size (up)
  583.      * 9 = Size (dn)
  584.      */
  585.  
  586.     global $internal_date_sort;
  587.  
  588.     if (($sort == 0) || ($sort == 1)) {
  589.         foreach ($msgs as $item) {
  590.             if ($internal_date_sort)
  591.                 $msort[] = $item['RECEIVED_TIME_STAMP'];
  592.             else
  593.                 $msort[] = $item['TIME_STAMP'];
  594.         }
  595.     } elseif (($sort == 2) || ($sort == 3)) {
  596.         $fld_sort = (handleAsSent($mailbox)?'TO-SORT':'FROM-SORT');
  597.         foreach ($msgs as $item) {
  598.             $msort[] = $item[$fld_sort];
  599.         }
  600.     } elseif (($sort == 4) || ($sort == 5)) {
  601.         foreach ($msgs as $item) {
  602.             $msort[] = $item['SUBJECT-SORT'];
  603.         }
  604.     } elseif (($sort == 8) || ($sort == 9)) {
  605.        foreach ($msgs as $item) {
  606.            $msort[] = $item['SIZE'];
  607.        }
  608.     } else {
  609.         $msort = $msgs;
  610.     }
  611.     if ($sort != 6) {
  612.         if ($sort % 2) {
  613.             asort($msort);
  614.         } else {
  615.             arsort($msort);
  616.         }
  617.     }
  618.     return $msort;
  619. }
  620.  
  621. function fillMessageArray($imapConnection, $id, $count, $show_num=false) {
  622.     return sqimap_get_small_header_list($imapConnection, $id, $show_num);
  623. }
  624.  
  625.  
  626. /* Generic function to convert the msgs array into an HTML table. */
  627. function displayMessageArray($imapConnection, $num_msgs, $start_msg,
  628.                              $msort, $mailbox, $sort, $color,
  629.                              $show_num, $where=0, $what=0) {
  630.     global $imapServerAddress, $use_mailbox_cache, $index_order,
  631.            $indent_array, $thread_sort_messages, $allow_server_sort,
  632.            $server_sort_order, $PHP_SELF;
  633.  
  634.     $res = getEndMessage($start_msg, $show_num, $num_msgs);
  635.     $start_msg = $res[0];
  636.     $end_msg   = $res[1];
  637.  
  638.     $urlMailbox = urlencode($mailbox);
  639.  
  640.     /* get indent level for subject display */
  641.     if ($thread_sort_messages == 1 && $num_msgs) {
  642.         $indent_array = get_parent_level($imapConnection);
  643.     }
  644.  
  645.     $real_startMessage = $start_msg;
  646.     if ($sort == 6) {
  647.         if ($end_msg - $start_msg < $show_num - 1) {
  648.             $end_msg = $end_msg - $start_msg + 1;
  649.             $start_msg = 1;
  650.         } else if ($start_msg > $show_num) {
  651.             $end_msg = $show_num;
  652.             $start_msg = 1;
  653.         }
  654.     }
  655.     $endVar = $end_msg + 1;
  656.  
  657.     /*
  658.      * Loop through and display the info for each message.
  659.      * ($t is used for the checkbox number)
  660.      */
  661.     $t = 0;
  662.  
  663.     /* messages display */
  664.  
  665.     if (!$num_msgs) {
  666.     /* if there's no messages in this folder */
  667.         echo html_tag( 'tr',
  668.                 html_tag( 'td',
  669.                           "<br><b>" . _("THIS FOLDER IS EMPTY") . "</b><br>&nbsp;",
  670.                           'center',
  671.                           $color[4],
  672.                           'colspan="' . count($index_order) . '"'
  673.                 )
  674.         );
  675.     } elseif ($start_msg == $end_msg) {
  676.     /* if there's only one message in the box, handle it differently. */
  677.         if ($sort != 6) {
  678.             $i = $start_msg;
  679.         } else {
  680.             $i = 1;
  681.         }
  682.         reset($msort);
  683.         $k = 0;
  684.         do {
  685.             $key = key($msort);
  686.             next($msort);
  687.             $k++;
  688.         } while (isset ($key) && ($k < $i));
  689.         printMessageInfo($imapConnection, $t, true, $key, $mailbox,
  690.                          $real_startMessage, $where, $what);
  691.     } else {
  692.         $i = $start_msg;
  693.         reset($msort);
  694.         $k = 0;
  695.         do {
  696.             $key = key($msort);
  697.             next($msort);
  698.             $k++;
  699.         } while (isset ($key) && ($k < $i));
  700.         $not_last = true;
  701.         do {
  702.             if (!$i || $i == $endVar-1) $not_last = false;
  703.                 printMessageInfo($imapConnection, $t, $not_last, $key, $mailbox,
  704.                                  $real_startMessage, $where, $what);
  705.             $key = key($msort);
  706.             $t++;
  707.             $i++;
  708.             next($msort);
  709.         } while ($i && $i < $endVar);
  710.     }
  711. }
  712.  
  713. /*
  714.  * Displays the standard message list header. To finish the table,
  715.  * you need to do a "</table></table>";
  716.  *
  717.  * $moveURL is the URL to submit the delete/move form to
  718.  * $mailbox is the current mailbox
  719.  * $sort is the current sorting method (-1 for no sorting available [searches])
  720.  * $Message is a message that is centered on top of the list
  721.  * $More is a second line that is left aligned
  722.  */
  723.  
  724. function mail_message_listing_beginning ($imapConnection,
  725.                                          $mailbox = '', $sort = -1,
  726.                                          $msg_cnt_str = '',
  727.                                          $paginator = '&nbsp;',
  728.                                          $start_msg = 1) {
  729.     global $color, $auto_expunge, $base_uri, $thread_sort_messages,
  730.            $allow_thread_sort, $allow_server_sort, $server_sort_order,
  731.            $PHP_SELF, $javascript_on;
  732.  
  733.     $php_self = $PHP_SELF;
  734.     /* fix for incorrect $PHP_SELF */
  735.     if (strpos($php_self, 'move_messages.php')) {
  736.         $php_self = str_replace('move_messages.php', 'right_main.php', $php_self);
  737.     }
  738.     $urlMailbox = urlencode($mailbox);
  739.  
  740.     if (preg_match('/^(.+)\?.+$/',$php_self,$regs)) {
  741.         $source_url = $regs[1];
  742.     } else {
  743.         $source_url = $php_self;
  744.     }
  745.  
  746.     if ($javascript_on) {
  747.         $safe_name = preg_replace("/[^0-9A-Za-z_]/", '_', $mailbox);
  748.         $form_name = "FormMsgs" . $safe_name;
  749.         $func_name = "CheckAll" . $safe_name;
  750.         echo '<script language="JavaScript" type="text/javascript">'
  751.                 . "\n<!-- \n"
  752.                 . "function " . $func_name . "() {\n"
  753.                 . "  var c;\n"
  754.                 . "  for (var i = 0; i < document." . $form_name . ".elements.length; i++) {\n"
  755.                 . "    c = document." . $form_name . ".elements[i];\n"
  756.                 . "    if(c.type == 'checkbox' && c.name.substring(0,3) == 'msg'){\n"
  757.                 . "      c.checked = !c.checked;\n"
  758.                 . "    }\n"
  759.                 . "  }\n"
  760.                 . "}\n"
  761.                 . "//-->\n"
  762.                 . '</script>';
  763.     }
  764.  
  765.     /*
  766.      * This is the beginning of the message list table.
  767.      * It wraps around all messages
  768.      */
  769.  
  770.     if (!empty($paginator) && !empty($msg_cnt_str)) {
  771.  
  772.         echo html_tag( 'table' ,
  773.             html_tag( 'tr',
  774.                 html_tag( 'td' ,
  775.                     html_tag( 'table' ,
  776.                         html_tag( 'tr',
  777.                             html_tag( 'td', $paginator, 'left' ) .
  778.                             html_tag( 'td', $msg_cnt_str, 'right' )
  779.                         )
  780.                     , '', $color[4], 'border="0" width="100%" cellpadding="1"  cellspacing="0"' )
  781.                 , 'left', '', '' )
  782.             , '', $color[0] )
  783.          , '', '', 'border="0" width="100%" cellpadding="1"  cellspacing="0"' );
  784.     }
  785.     /* line between header and button area */
  786.         echo '</td></tr><tr><td height="5" bgcolor="'.$color[4].'"></td></tr>';
  787.  
  788.         echo html_tag( 'tr' ) . "\n"
  789.         . html_tag( 'td' ,'' , 'left', '', '' )
  790.          . html_tag( 'table' ,'' , '', $color[9], 'border="0" width="100%" cellpadding="1"  cellspacing="0"' )
  791.           . '<tr><td>'
  792.            . html_tag( 'table' ,'' , '', $color[0], 'border="0" width="100%" cellpadding="1"  cellspacing="0"' )
  793.             . html_tag( 'tr',
  794.              getSmallStringCell(_("Move Selected To"), 'left', 'nowrap') .
  795.              getSmallStringCell(_("Transform Selected Messages"), 'right')
  796.             )
  797.             . html_tag( 'tr' ) ."\n"
  798.             . html_tag( 'td', '', 'left', '', 'valign="middle" nowrap' );
  799.             getMbxList($imapConnection);
  800.             echo getButton('SUBMIT', 'moveButton',_("Move")) . "\n";
  801.             echo getButton('SUBMIT', 'attache',_("Forward")) . "\n";
  802.             do_hook('mailbox_display_buttons');
  803.  
  804.     echo "      </td>\n"
  805.          . html_tag( 'td', '', 'right', '', 'nowrap' );
  806.  
  807.     if (!$auto_expunge) {
  808.         echo getButton('SUBMIT', 'expungeButton',_("Expunge")) ."\n";
  809.     }
  810.  
  811.     echo getButton('SUBMIT', 'markRead',_("Read")) . "\n";
  812.     echo getButton('SUBMIT', 'markUnread',_("Unread")) . "\n";
  813.     echo getButton('SUBMIT', 'delete',_("Delete")) ."&nbsp;\n";
  814.     if (!strpos($php_self,'mailbox')) {
  815.         $location = $php_self.'?mailbox=INBOX&amp;startMessage=1';
  816.     } else {
  817.         $location = $php_self;
  818.     }
  819.  
  820. //    $location = urlencode($location);
  821.  
  822.     echo '<input type="hidden" name="location" value="'.$location.'">';
  823.     echo "</td>\n"
  824.          . "   </tr>\n";
  825.  
  826.     /* draws thread sorting links */
  827.     if ($allow_thread_sort == TRUE) {
  828.         if ($thread_sort_messages == 1 ) {
  829.             $set_thread = 2;
  830.             $thread_name = _("Unthread View");
  831.         } elseif ($thread_sort_messages == 0) {
  832.             $set_thread = 1;
  833.             $thread_name = _("Thread View");
  834.         }
  835.         echo html_tag( 'tr' ,
  836.                     html_tag( 'td' ,
  837.                               '&nbsp;<small><a href="' . $source_url . '?sort='
  838.                               . "$sort" . '&amp;start_messages=1&amp;set_thread=' . "$set_thread"
  839.                               . '&amp;mailbox=' . urlencode($mailbox) . '">' . $thread_name
  840.                               . '</a></small>&nbsp;'
  841.                      , '', '', ' colspan="2" ' )
  842.                  , '', '', '' );
  843.     }
  844.  
  845.     echo "</table></td></tr></table></td></tr>\n";
  846.  
  847.     do_hook('mailbox_form_before');
  848.  
  849.     /* if using server sort we highjack the
  850.      * the $sort var and use $server_sort_order
  851.      * instead. but here we reset sort for a bit
  852.      * since its easy
  853.      */
  854.     if ($allow_server_sort == TRUE) {
  855.         $sort = $server_sort_order;
  856.     }
  857. }
  858.  
  859. function mail_message_listing_end($num_msgs, $paginator_str, $msg_cnt_str, $color) {
  860.   if ($num_msgs) {
  861.     /* space between list and footer */
  862.     echo '<tr><td height="5" bgcolor="'.$color[4].'" colspan="1">';
  863.  
  864.     echo '</td></tr><tr><td>';
  865.     echo html_tag( 'table',
  866.             html_tag( 'tr',
  867.                 html_tag( 'td',
  868.                     html_tag( 'table',
  869.                         html_tag( 'tr',
  870.                             html_tag( 'td', $paginator_str ) .
  871.                             html_tag( 'td', $msg_cnt_str, 'right' )
  872.                         )
  873.                     , '', $color[4], 'width="100%" border="0" cellpadding="1" cellspacing="0"' )
  874.                 )
  875.             )
  876.         , '', $color[9], 'width="100%" border="0" cellpadding="1"  cellspacing="0"' );
  877.     echo '</td></tr>';
  878.   }
  879.     /* End of message-list table */
  880.  
  881.     do_hook('mailbox_index_after');
  882. }
  883.  
  884. function printHeader($mailbox, $sort, $color, $showsort=true) {
  885.  
  886.     global $index_order, $internal_date_sort, $imap_server_type;
  887.  
  888.     // gmail doesn't support custom sorting, so we always
  889.     // hide the sort buttons when using gmail
  890.     if ($imap_server_type == 'gmail') $showsort = false;
  891.  
  892.     echo html_tag( 'tr' ,'' , 'center', $color[5] );
  893.  
  894.     /* calculate the width of the subject column based on the
  895.      * widths of the other columns */
  896.     $widths = array(1=>1,2=>25,3=>5,4=>0,5=>1,6=>5);
  897.     $subjectwidth = 100;
  898.     foreach($index_order as $item) {
  899.         $subjectwidth -= $widths[$item];
  900.     }
  901.  
  902.     foreach ($index_order as $item) {
  903.         switch ($item) {
  904.         case 1: /* checkbox */
  905.         case 5: /* flags */
  906.             echo html_tag( 'td' ,'&nbsp;' , '', '', 'width="1%"' );
  907.             break;
  908.         case 2: /* from */
  909.             if (handleAsSent($mailbox)) {
  910.                 echo html_tag( 'td' ,'' , 'left', '', 'width="25%"' )
  911.                      . '<b>' . _("To") . '</b>';
  912.             } else {
  913.                 echo html_tag( 'td' ,'' , 'left', '', 'width="25%"' )
  914.                      . '<b>' . _("From") . '</b>';
  915.             }
  916.             if ($showsort) {
  917.                 ShowSortButton($sort, $mailbox, 2, 3);
  918.             }
  919.             echo "</td>\n";
  920.             break;
  921.         case 3: /* date */
  922.             echo html_tag( 'td' ,'' , 'left', '', 'width="5%" nowrap' )
  923.                  . '<b>'
  924.                  . ($internal_date_sort && ($sort == 0 || $sort == 1) ? _("Received") : _("Date"))
  925.                  . '</b>';
  926.             if ($showsort) {
  927.                 ShowSortButton($sort, $mailbox, 0, 1);
  928.             }
  929.             echo "</td>\n";
  930.             break;
  931.         case 4: /* subject */
  932.             echo html_tag( 'td' ,'' , 'left', '', 'width="'.$subjectwidth.'%"' )
  933.                  . '<b>' . _("Subject") . '</b>';
  934.             if ($showsort) {
  935.                 ShowSortButton($sort, $mailbox, 4, 5);
  936.             }
  937.             echo "</td>\n";
  938.             break;
  939.         case 6: /* size */
  940.             echo html_tag( 'td' ,'' , 'left', '', 'width="5%" nowrap' )
  941.                  . '<b>' . _("Size") . '</b>';
  942.             if ($showsort) {
  943.                 ShowSortButton($sort, $mailbox, 8, 9);
  944.             }
  945.             echo "</td>\n";
  946.             break;
  947.         }
  948.     }
  949.     echo "</tr>\n";
  950. }
  951.  
  952.  
  953. /*
  954.  * This function shows the sort button. Isn't this a good comment?
  955.  */
  956. function ShowSortButton($sort, $mailbox, $Up, $Down ) {
  957.     global $PHP_SELF;
  958.     /* Figure out which image we want to use. */
  959.     if ($sort != $Up && $sort != $Down) {
  960.         $img = 'sort_none.png';
  961.         $which = $Up;
  962.     } elseif ($sort == $Up) {
  963.         $img = 'up_pointer.png';
  964.         $which = $Down;
  965.     } else {
  966.         $img = 'down_pointer.png';
  967.         $which = 6;
  968.     }
  969.  
  970.     if (preg_match('/^(.+)\?.+$/',$PHP_SELF,$regs)) {
  971.         $source_url = $regs[1];
  972.     } else {
  973.         $source_url = $PHP_SELF;
  974.     }
  975.  
  976.     /* Now that we have everything figured out, show the actual button. */
  977.     echo ' <a href="' . $source_url .'?newsort=' . $which
  978.          . '&amp;startMessage=1&amp;mailbox=' . urlencode($mailbox)
  979.          . '"><img src="../images/' . $img
  980.          . '" border="0" width="12" height="10" alt="sort"></a>';
  981. }
  982.  
  983. function get_selectall_link($start_msg, $sort) {
  984.     global $checkall, $what, $where, $mailbox, $javascript_on;
  985.     global $PHP_SELF, $PG_SHOWNUM;
  986.  
  987.     $whitespace_style = ' style="white-space:nowrap" ';
  988.     $result = '';
  989.     if ($javascript_on) {
  990.         $safe_name = preg_replace("/[^0-9A-Za-z_]/", '_', $mailbox);
  991.         $func_name = "CheckAll" . $safe_name;
  992.         $result = '<a href="javascript:void(0)" onClick="' . $func_name . '();"' . $whitespace_style . '>' . _("Toggle All")
  993. /*                . '</script><a href="javascript:' . $func_name . '()">' . _("Toggle All")*/
  994.                 . "</a>\n";
  995.     } else {
  996.         if (strpos($PHP_SELF, "?")) {
  997.             $result .= "<a href=\"$PHP_SELF&amp;mailbox=" . urlencode($mailbox)
  998.                     .  "&amp;startMessage=$start_msg&amp;sort=$sort&amp;checkall=";
  999.         } else {
  1000.             $result .= "<a href=\"$PHP_SELF?mailbox=" . urlencode($mailbox)
  1001.                     .  "&amp;startMessage=$start_msg&amp;sort=$sort&amp;checkall=";
  1002.         }
  1003.         if (isset($checkall) && $checkall == '1') {
  1004.             $result .= '0';
  1005.         } else {
  1006.             $result .= '1';
  1007.         }
  1008.  
  1009.         if (isset($where) && isset($what)) {
  1010.             $result .= '&amp;where=' . urlencode($where)
  1011.                     .  '&amp;what=' . urlencode($what);
  1012.         }
  1013.         $result .= "\"$whitespace_style>";
  1014.  
  1015.         if (isset($checkall) && ($checkall == '1')) {
  1016.             $result .= _("Unselect All");
  1017.         } else {
  1018.             $result .= _("Select All");
  1019.         }
  1020.         $result .= "</a>\n";
  1021.     }
  1022.  
  1023.     /* Return our final result. */
  1024.     return ($result);
  1025. }
  1026.  
  1027. /*
  1028.  * This function computes the "Viewing Messages..." string.
  1029.  */
  1030. function get_msgcnt_str($start_msg, $end_msg, $num_msgs) {
  1031.     /* Compute the $msg_cnt_str. */
  1032.     $result = '';
  1033.     if ($start_msg < $end_msg) {
  1034.         $result = sprintf(_("Viewing Messages: %s to %s (%s total)"),
  1035.                   '<b>'.$start_msg.'</b>', '<b>'.$end_msg.'</b>', $num_msgs);
  1036.     } else if ($start_msg == $end_msg) {
  1037.         $result = sprintf(_("Viewing Message: %s (%s total)"), '<b>'.$start_msg.'</b>', $num_msgs);
  1038.     } else {
  1039.         $result = '<br>';
  1040.     }
  1041.     /* Return our result string. */
  1042.     return ($result);
  1043. }
  1044.  
  1045. /*
  1046.  * Generate a paginator link.
  1047.  */
  1048. function get_paginator_link($box, $start_msg, $use, $text) {
  1049.     global $PHP_SELF;
  1050.  
  1051.     $result = "<a href=\"right_main.php?use_mailbox_cache=$use"
  1052.             . "&amp;startMessage=$start_msg&amp;mailbox=$box\" "
  1053.             . ">$text</a>";
  1054.     return ($result);
  1055. /*
  1056.     if (preg_match('/^(.+)\?.+$/',$PHP_SELF,$regs)) {
  1057.         $source_url = $regs[1];
  1058.     } else {
  1059.         $source_url = $PHP_SELF;
  1060.     }
  1061.  
  1062.     $result = '<A HREF="'. $source_url . "?use_mailbox_cache=$use"
  1063.             . "&amp;startMessage=$start_msg&amp;mailbox=$box\" "
  1064.             . ">$text</A>";
  1065.     return ($result);
  1066. */
  1067. }
  1068.  
  1069. /*
  1070.  * This function computes the paginator string.
  1071.  */
  1072. function get_paginator_str($box, $start_msg, $end_msg, $num_msgs,
  1073.                            $show_num, $sort) {
  1074.     global $username, $data_dir, $use_mailbox_cache, $color, $PG_SHOWNUM;
  1075.  
  1076.     /* Initialize paginator string chunks. */
  1077.     $prv_str = '';
  1078.     $nxt_str = '';
  1079.     $pg_str  = '';
  1080.     $all_str = '';
  1081.     $tgl_str = '';
  1082.  
  1083.     $box = urlencode($box);
  1084.  
  1085.     /* Create simple strings that will be creating the paginator. */
  1086.     $spc = '&nbsp;';     /* This will be used as a space. */
  1087.     $sep = '|';          /* This will be used as a seperator. */
  1088.  
  1089.     /* Get some paginator preference values. */
  1090.     $pg_sel = getPref($data_dir, $username, 'page_selector', SMPREF_ON);
  1091.     $pg_max = getPref($data_dir, $username, 'page_selector_max', PG_SEL_MAX);
  1092.  
  1093.     /* Make sure that our start message number is not too big. */
  1094.     $start_msg = min($start_msg, $num_msgs);
  1095.  
  1096.     /* Decide whether or not we will use the mailbox cache. */
  1097.     /* Not sure why $use_mailbox_cache is even passed in.   */
  1098.     if ($sort == 6) {
  1099.         $use = 0;
  1100.     } else {
  1101.         $use = 1;
  1102.     }
  1103.  
  1104.     /* Compute the starting message of the previous and next page group. */
  1105.     $next_grp = $start_msg + $show_num;
  1106.     $prev_grp = $start_msg - $show_num;
  1107.  
  1108.     /* Compute the basic previous and next strings. */
  1109.     if (($next_grp <= $num_msgs) && ($prev_grp >= 0)) {
  1110.         $prv_str = get_paginator_link($box, $prev_grp, $use, _("Previous"));
  1111.         $nxt_str = get_paginator_link($box, $next_grp, $use, _("Next"));
  1112.     } else if (($next_grp > $num_msgs) && ($prev_grp >= 0)) {
  1113.         $prv_str = get_paginator_link($box, $prev_grp, $use, _("Previous"));
  1114.         $nxt_str = "<font color=\"$color[9]\">"._("Next")."</font>\n";
  1115.     } else if (($next_grp <= $num_msgs) && ($prev_grp < 0)) {
  1116.         $prv_str = "<font color=\"$color[9]\">"._("Previous") . '</font>';
  1117.         $nxt_str = get_paginator_link($box, $next_grp, $use, _("Next"));
  1118.     }
  1119.  
  1120.     /* Page selector block. Following code computes page links. */
  1121.     if ($pg_sel && ($num_msgs > $show_num)) {
  1122.         /* Most importantly, what is the current page!!! */
  1123.         $cur_pg = intval($start_msg / $show_num) + 1;
  1124.  
  1125.         /* Compute total # of pages and # of paginator page links. */
  1126.         $tot_pgs = ceil($num_msgs / $show_num);  /* Total number of Pages */
  1127.         $vis_pgs = min($pg_max, $tot_pgs - 1);   /* Visible Pages    */
  1128.  
  1129.         /* Compute the size of the four quarters of the page links. */
  1130.  
  1131.         /* If we can, just show all the pages. */
  1132.         if (($tot_pgs - 1) <= $pg_max) {
  1133.             $q1_pgs = $cur_pg - 1;
  1134.             $q2_pgs = $q3_pgs = 0;
  1135.             $q4_pgs = $tot_pgs - $cur_pg;
  1136.  
  1137.         /* Otherwise, compute some magic to choose the four quarters. */
  1138.         } else {
  1139.             /*
  1140.              * Compute the magic base values. Added together,
  1141.              * these values will always equal to the $pag_pgs.
  1142.              * NOTE: These are DEFAULT values and do not take
  1143.              * the current page into account. That is below.
  1144.              */
  1145.             $q1_pgs = floor($vis_pgs/4);
  1146.             $q2_pgs = round($vis_pgs/4, 0);
  1147.             $q3_pgs = ceil($vis_pgs/4);
  1148.             $q4_pgs = round(($vis_pgs - $q2_pgs)/3, 0);
  1149.  
  1150.             /* Adjust if the first quarter contains the current page. */
  1151.             if (($cur_pg - $q1_pgs) < 1) {
  1152.                 $extra_pgs = ($q1_pgs - ($cur_pg - 1)) + $q2_pgs;
  1153.                 $q1_pgs = $cur_pg - 1;
  1154.                 $q2_pgs = 0;
  1155.                 $q3_pgs += ceil($extra_pgs / 2);
  1156.                 $q4_pgs += floor($extra_pgs / 2);
  1157.  
  1158.             /* Adjust if the first and second quarters intersect. */
  1159.             } else if (($cur_pg - $q2_pgs - ceil($q2_pgs/3)) <= $q1_pgs) {
  1160.                 $extra_pgs = $q2_pgs;
  1161.                 $extra_pgs -= ceil(($cur_pg - $q1_pgs - 1) * 3/4);
  1162.                 $q2_pgs = ceil(($cur_pg - $q1_pgs - 1) * 3/4);
  1163.                 $q3_pgs += ceil($extra_pgs / 2);
  1164.                 $q4_pgs += floor($extra_pgs / 2);
  1165.  
  1166.             /* Adjust if the fourth quarter contains the current page. */
  1167.             } else if (($cur_pg + $q4_pgs) >= $tot_pgs) {
  1168.                 $extra_pgs = ($q4_pgs - ($tot_pgs - $cur_pg)) + $q3_pgs;
  1169.                 $q3_pgs = 0;
  1170.                 $q4_pgs = $tot_pgs - $cur_pg;
  1171.                 $q1_pgs += floor($extra_pgs / 2);
  1172.                 $q2_pgs += ceil($extra_pgs / 2);
  1173.  
  1174.             /* Adjust if the third and fourth quarter intersect. */
  1175.             } else if (($cur_pg + $q3_pgs) >= ($tot_pgs - $q4_pgs)) {
  1176.                 $extra_pgs = $q3_pgs;
  1177.                 $extra_pgs -= ceil(($tot_pgs - $cur_pg - $q4_pgs) * 3/4);
  1178.                 $q3_pgs = ceil(($tot_pgs - $cur_pg - $q4_pgs) * 3/4);
  1179.                 $q1_pgs += floor($extra_pgs / 2);
  1180.                 $q2_pgs += ceil($extra_pgs / 2);
  1181.             }
  1182.         }
  1183.  
  1184.         /*
  1185.          * I am leaving this debug code here, commented out, because
  1186.          * it is a really nice way to see what the above code is doing.
  1187.          */
  1188.          // echo "qts =  $q1_pgs/$q2_pgs/$q3_pgs/$q4_pgs = "
  1189.          //     . ($q1_pgs + $q2_pgs + $q3_pgs + $q4_pgs) . '<br>';
  1190.  
  1191.  
  1192.         /* Print out the page links from the compute page quarters. */
  1193.  
  1194.         /* Start with the first quarter. */
  1195.         if (($q1_pgs == 0) && ($cur_pg > 1)) {
  1196.             $pg_str .= "...$spc";
  1197.         } else {
  1198.             for ($pg = 1; $pg <= $q1_pgs; ++$pg) {
  1199.                 $start = (($pg-1) * $show_num) + 1;
  1200.                 $pg_str .= get_paginator_link($box, $start, $use, $pg) . $spc;
  1201.             }
  1202.             if ($cur_pg - $q2_pgs - $q1_pgs > 1) {
  1203.                 $pg_str .= "...$spc";
  1204.             }
  1205.         }
  1206.  
  1207.         /* Continue with the second quarter. */
  1208.         for ($pg = $cur_pg - $q2_pgs; $pg < $cur_pg; ++$pg) {
  1209.             $start = (($pg-1) * $show_num) + 1;
  1210.             $pg_str .= get_paginator_link($box, $start, $use, $pg) . $spc;
  1211.         }
  1212.  
  1213.         /* Now print the current page. */
  1214.         $pg_str .= $cur_pg . $spc;
  1215.  
  1216.         /* Next comes the third quarter. */
  1217.         for ($pg = $cur_pg + 1; $pg <= $cur_pg + $q3_pgs; ++$pg) {
  1218.             $start = (($pg-1) * $show_num) + 1;
  1219.             $pg_str .= get_paginator_link($box, $start, $use, $pg) . $spc;
  1220.         }
  1221.  
  1222.         /* And last, print the forth quarter page links. */
  1223.         if (($q4_pgs == 0) && ($cur_pg < $tot_pgs)) {
  1224.             $pg_str .= "...$spc";
  1225.         } else {
  1226.             if (($tot_pgs - $q4_pgs) > ($cur_pg + $q3_pgs)) {
  1227.                 $pg_str .= "...$spc";
  1228.             }
  1229.             for ($pg = $tot_pgs - $q4_pgs + 1; $pg <= $tot_pgs; ++$pg) {
  1230.                 $start = (($pg-1) * $show_num) + 1;
  1231.                 $pg_str .= get_paginator_link($box, $start, $use, $pg) . $spc;
  1232.             }
  1233.         }
  1234.     } else if ($PG_SHOWNUM == 999999) {
  1235.         $pg_str = "<a href=\"right_main.php?PG_SHOWALL=0"
  1236.                 . "&amp;use_mailbox_cache=$use&amp;startMessage=1&amp;mailbox=$box\" "
  1237.                 . ">" ._("Paginate") . '</a>' . $spc;
  1238.     }
  1239.  
  1240.     /* If necessary, compute the 'show all' string. */
  1241.     if (($prv_str != '') || ($nxt_str != '')) {
  1242.         $all_str = "<a href=\"right_main.php?PG_SHOWALL=1"
  1243.                  . "&amp;use_mailbox_cache=$use&amp;startMessage=1&amp;mailbox=$box\" "
  1244.                  . "style=\"white-space:nowrap\">" . _("Show All") . '</a>';
  1245.     }
  1246.  
  1247.     /* Last but not least, get the value for the toggle all link. */
  1248.     $tgl_str = get_selectall_link($start_msg, $sort);
  1249.  
  1250.     /* Put all the pieces of the paginator string together. */
  1251.     /**
  1252.      * Hairy code... But let's leave it like it is since I am not certain
  1253.      * a different approach would be any easier to read. ;)
  1254.      */
  1255.     $result = '';
  1256.     $result .= ($prv_str != '' ? $prv_str . $spc . $sep . ' ' : '');
  1257.     $result .= ($nxt_str != '' ? $nxt_str . $spc . $sep . ' ' : '');
  1258.     $result .= ($pg_str  != '' ? $pg_str : '');
  1259.     $result .= ($all_str != '' ? $sep . ' ' . $all_str . $spc : '');
  1260.     $result .= ($result  != '' ? $sep . ' ' . $tgl_str: $tgl_str);
  1261.  
  1262.     /* If the resulting string is blank, return a non-breaking space. */
  1263.     if ($result == '') {
  1264.         $result = '&nbsp;';
  1265.     }
  1266.  
  1267.     /* Return our final magical paginator string. */
  1268.     return ($result);
  1269. }
  1270.  
  1271. function processSubject($subject, $threadlevel = 0) {
  1272.     global $languages, $squirrelmail_language;
  1273.     /* Shouldn't ever happen -- caught too many times in the IMAP functions */
  1274.     if ($subject == '') {
  1275.         return _("(no subject)");
  1276.     }
  1277.  
  1278.     $trim_at = SUBJ_TRIM_AT;
  1279.  
  1280.     /* if this is threaded, subtract two chars per indentlevel */
  1281.     if($threadlevel > 0 && $threadlevel <= 10) {
  1282.         $trim_at -= (2*$threadlevel);
  1283.     }
  1284.  
  1285.     if (strlen($subject) <= $trim_at) {
  1286.         return $subject;
  1287.     }
  1288.  
  1289.     $ent_strlen = $orig_len = strlen($subject);
  1290.     $trim_val = $trim_at - 5;
  1291.     $ent_offset = 0;
  1292.     /*
  1293.      * see if this is entities-encoded string
  1294.      * If so, Iterate through the whole string, find out
  1295.      * the real number of characters, and if more
  1296.      * than 55, substr with an updated trim value.
  1297.      */
  1298.     $step = $ent_loc = 0;
  1299.     while ( $ent_loc < $trim_val && (($ent_loc = strpos($subject, '&', $ent_offset)) !== false) &&
  1300.             (($ent_loc_end = strpos($subject, ';', $ent_loc+3)) !== false) ) {
  1301.         $trim_val += ($ent_loc_end-$ent_loc);
  1302.         $ent_offset  = $ent_loc_end+1;
  1303.         ++$step;
  1304.     }
  1305.  
  1306.     if (($trim_val > 50) && (strlen($subject) > ($trim_val))&& (strpos($subject,';',$trim_val) < ($trim_val +6))) {
  1307.         $i = strpos($subject,';',$trim_val);
  1308.         if ($i) {
  1309.             $trim_val = strpos($subject,';',$trim_val);
  1310.         }
  1311.     }
  1312.     if ($ent_strlen <= $trim_at){
  1313.         return $subject;
  1314.     }
  1315.  
  1316.     if (isset($languages[$squirrelmail_language]['XTRA_CODE']) &&
  1317.         function_exists($languages[$squirrelmail_language]['XTRA_CODE'])) {
  1318.         return $languages[$squirrelmail_language]['XTRA_CODE']('strimwidth', $subject, $trim_val);
  1319.     }
  1320.  
  1321.     // only print '...' when we're actually dropping part of the subject
  1322.     if(strlen($subject) <= $trim_val) {
  1323.         return $subject;
  1324.     } else {
  1325.         return substr($subject, 0, $trim_val) . '...';
  1326.     }
  1327. }
  1328.  
  1329. function getMbxList($imapConnection) {
  1330.     global $lastTargetMailbox;
  1331.     echo  '         &nbsp;<tt><select name="targetMailbox">';
  1332.     echo sqimap_mailbox_option_list($imapConnection, array(strtolower($lastTargetMailbox)) );
  1333.     echo '         </select></tt>&nbsp;';
  1334. }
  1335.  
  1336. function getButton($type, $name, $value) {
  1337.     return '<input type="'.$type.'" name="'.$name.'" value="'.$value . '">';
  1338. }
  1339.  
  1340. function getSmallStringCell($string, $align) {
  1341.     return html_tag('td',
  1342.                     '<small>' . $string . ':&nbsp; </small>',
  1343.                     $align,
  1344.                     '',
  1345.                     'nowrap' );
  1346. }
  1347.  
  1348. function getEndMessage($start_msg, $show_num, $num_msgs) {
  1349.     if ($start_msg + ($show_num - 1) < $num_msgs){
  1350.         $end_msg = $start_msg + ($show_num - 1);
  1351.     } else {
  1352.         $end_msg = $num_msgs;
  1353.     }
  1354.  
  1355.     if ($end_msg < $start_msg) {
  1356.         $start_msg = $start_msg - $show_num;
  1357.         if ($start_msg < 1) {
  1358.             $start_msg = 1;
  1359.         }
  1360.     }
  1361.     return (array($start_msg,$end_msg));
  1362. }
  1363.  
  1364. function handleAsSent($mailbox) {
  1365.     global $handleAsSent_result;
  1366.  
  1367.     /* First check if this is the sent or draft folder. */
  1368.     $handleAsSent_result = isSentMailbox($mailbox) || isDraftMailbox($mailbox);
  1369.  
  1370.     /* Then check the result of the handleAsSent hook. */
  1371.     do_hook('check_handleAsSent_result', $mailbox);
  1372.  
  1373.     /* And return the result. */
  1374.     return $handleAsSent_result;
  1375. }

Documentation generated on Mon, 13 Jan 2020 04:24:54 +0100 by phpDocumentor 1.4.3