Fix for infinite loop when trying to decode multi-part mime attachments
[squirrelmail.git] / functions / mailbox_display.php
index 79954364b39ed3b005ae1fa74b6397e14476d271..e098e3b3fa0797a3def0c8f9c373f65cd15767cb 100644 (file)
@@ -37,7 +37,7 @@ function elapsed($start)
 function printMessageInfo($imapConnection, $t, $not_last=true, $key, $mailbox,
                           $start_msg, $where, $what) {
     global $checkall,
-           $color, $msgs, $msort,
+           $color, $msgs, $msort, $td_str, $msg, 
            $default_use_priority,
            $message_highlight_list,
            $index_order,
@@ -93,8 +93,8 @@ function printMessageInfo($imapConnection, $t, $not_last=true, $key, $mailbox,
         }
     } 
     
-
-    $subject = processSubject(decodeHeader($msg['SUBJECT']), $indent_array[$msg['ID']]);
+    $subject_full = decodeHeader($msg['SUBJECT']);
+    $subject = processSubject($subject_full, $indent_array[$msg['ID']]);
 
     echo html_tag( 'tr','','','','VALIGN="top"') . "\n";
 
@@ -210,10 +210,10 @@ function printMessageInfo($imapConnection, $t, $not_last=true, $key, $mailbox,
                         .  '&passed_id='. $msg["ID"]
                         .  '&startMessage='.$start_msg.$searchstr.'"';
                 do_hook("subject_link");
-                if ($subject != $msg['SUBJECT']) {
+                if ($subject != $subject_full) {
                     $title = get_html_translation_table(HTML_SPECIALCHARS);
                     $title = array_flip($title);
-                    $title = strtr($msg['SUBJECT'], $title);
+                    $title = strtr($subject_full, $title);
                     $title = str_replace('"', "''", $title);
                     $td_str .= " title=\"$title\"";
                 }
@@ -249,6 +249,7 @@ function printMessageInfo($imapConnection, $t, $not_last=true, $key, $mailbox,
                 if (!$stuff) {
                     $td_str .= ' ';
                 }
+                do_hook("msg_envelope");
                 $td_str .= '</small></b>';
                 echo html_tag( 'td',
                                $td_str,
@@ -334,7 +335,7 @@ function getSelfSortMessages($imapConnection, $start_msg, $show_num,
                 $end_loop = $show_num;
             }
         }
-        $msgs = fillMessageArray($imapConnection,$id,$end_loop, $mailbox);
+        $msgs = fillMessageArray($imapConnection,$id,$end_loop);
     }
     return $msgs;
 }
@@ -351,6 +352,20 @@ function showMessagesForMailbox($imapConnection, $mailbox, $num_msgs,
     global $msgs, $msort, $auto_expunge, $thread_sort_messages,
            $allow_server_sort, $server_sort_order;
 
+    /*
+     * For some reason, on PHP 4.3+, this being unset, and set in the session causes havoc
+     * so setting it to an empty array beforehand seems to clean up the issue, and stopping the
+     * "Your script possibly relies on a session side-effect which existed until PHP 4.2.3" error
+     */
+
+    if (!isset($msort)) {
+        $msort = array();
+    }
+
+    if (!isset($msgs)) {
+        $msgs = array();
+    }
+
     $start = microtime();
     /* If autoexpunge is turned on, then do it now. */
     $mbxresponse = sqimap_mailbox_select($imapConnection, $mailbox);
@@ -466,7 +481,7 @@ function showMessagesForMailbox($imapConnection, $mailbox, $num_msgs,
     mail_message_listing_end($num_msgs, $paginator_str, $msg_cnt_str, $color); 
     echo '</td></tr></table>';
     $t = elapsed($start);
-    echo("elapsed time = $t seconds\n");
+    //echo("elapsed time = $t seconds\n");
 }
 
 function calc_msort($msgs, $sort) {
@@ -1152,23 +1167,29 @@ function processSubject($subject, $threadlevel = 0) {
     if (strlen($subject) <= $trim_at)
         return $subject;
 
-    $ent_strlen = strlen($subject);
+    $ent_strlen = $orig_len = strlen($subject);
     $trim_val = $trim_at - 5;
     $ent_offset = 0;
     /*
      * see if this is entities-encoded string
      * If so, Iterate through the whole string, find out
      * the real number of characters, and if more
-     * than 55, substr with an updated trim value.
+     * than 55, substr with an updated trim value. 
      */
-
-    while ( (($ent_loc = strpos(substr($subject,0,$trim_val), '&', $ent_offset)) !== false) &&
-            (($ent_loc_end = strpos(substr($subject,0,$trim_val+5), ';', $ent_loc)) !== false) ) {
-        $trim_val   += ($ent_loc_end-$ent_loc)+1;
-        $ent_strlen -= $ent_loc_end-$ent_loc;
+    $step = $ent_loc = 0;
+    while ( $ent_loc < $trim_val && (($ent_loc = strpos($subject, '&', $ent_offset)) !== false) &&
+            (($ent_loc_end = strpos($subject, ';', $ent_loc+3)) !== false) ) {
+        $trim_val += ($ent_loc_end-$ent_loc);
         $ent_offset  = $ent_loc_end+1;
+        ++$step;
+    }
+    
+    if (($trim_val > 50) && (strlen($subject) > ($trim_val))&& (strpos($subject,';',$trim_val) < ($trim_val +6))) {
+        $i = strpos($subject,';',$trim_val);
+        if ($i) {
+            $trim_val = strpos($subject,';',$trim_val);
+        }
     }
-
     if ($ent_strlen <= $trim_at){
         return $subject;
     }
@@ -1177,7 +1198,6 @@ function processSubject($subject, $threadlevel = 0) {
         function_exists($languages[$squirrelmail_language]['XTRA_CODE'])) {
         return $languages[$squirrelmail_language]['XTRA_CODE']('strimwidth', $subject, $trim_val);
     }
-
     return substr($subject, 0, $trim_val) . '...';
 }