Initializing aHeaderFields to avoid notices
[squirrelmail.git] / functions / mailbox_display.php
index c6851131e915c5bdca8d1bf921465770887b9968..1ffe727c35c238d3663712f545e998dd761439bf 100644 (file)
@@ -217,6 +217,12 @@ function sqm_api_mailbox_select($imapConnection,$account,$mailbox,$aConfig,$aPro
     }
     if (!isset($aConfig['search']) && isset($aCachedMailbox['SEARCH'][$iSetIndx])) {
         $aMailbox['SEARCH'][$iSetIndx] = $aCachedMailbox['SEARCH'][$iSetIndx];
+    } else if (isset($aConfig['search']) && isset($aCachedMailbox['SEARCH'][$iSetIndx]) &&
+        $aConfig['search'] != $aCachedMailbox['SEARCH'][$iSetIndx]) {
+        // reset the pageindex
+        $aMailbox['SEARCH'][$iSetIndx] = $aConfig['search'];
+        $aMailbox['OFFSET'] = 0;
+        $aMailbox['PAGEOFFSET'] = 1;
     } else {
         $aMailbox['SEARCH'][$iSetIndx] =  (isset($aConfig['search'])) ? $aConfig['search'] : 'ALL';
     }
@@ -296,6 +302,7 @@ function fetchMessageHeaders($imapConnection, &$aMailbox) {
     $iError = 0;
     $aFetchItems = $aHeaderItems = array();
     // initialize the fields we want to retrieve:
+    $aHeaderFields = array();
     foreach ($aFetchHeaders as $v) {
       switch ($v) {
         case SQM_COL_DATE:       $aHeaderFields[] = 'Date';         break;
@@ -523,11 +530,35 @@ function prepareMessageList(&$aMailbox, $aProps) {
                         if ($highlight_list && !$bHighLight) {
                             $bHighLight = highlightMessage($aCol[$k], $value, $highlight_list,$aFormattedMessages[$iUid]);
                         }
-                        $sTmp = getAddressString(parseRFC822Address($value),array('best' => true));
+                        $aAddressList = parseRFC822Address($value);
+                        $sTmp = getAddressString($aAddressList,array('best' => true));
+                        $title = $title_maybe = '';
+                        foreach ($aAddressList as $aAddr) {
+                            $sPersonal = (isset($aAddr[SQM_ADDR_PERSONAL])) ? $aAddr[SQM_ADDR_PERSONAL] : '';
+                            $sMailbox  = (isset($aAddr[SQM_ADDR_MAILBOX]))  ? $aAddr[SQM_ADDR_MAILBOX]  : '';
+                            $sHost     = (isset($aAddr[SQM_ADDR_HOST]))     ? $aAddr[SQM_ADDR_HOST]     : '';
+                            if ($sPersonal) {
+                                $title .= htmlspecialchars($sMailbox.'@'.$sHost).', ';
+                            } else {
+                                // if $value gets truncated we need to add the addresses with no
+                                // personal name as well
+                                $title_maybe .= htmlspecialchars($sMailbox.'@'.$sHost).', ';
+                            }
+                        }
+                        if ($title) {
+                            $title = substr($title,0,-2); // strip ', ';
+                        }
                         $sTmp = decodeHeader($sTmp);
                         if (isset($aColumnDesc[$k]['truncate']) && $aColumnDesc[$k]['truncate']) {
                             $sTrunc = truncateWithEntities($sTmp, $aColumnDesc[$k]['truncate']);
-                            $title = ($sTrunc != $sTmp) ? htmlspecialchars($sTmp) : '';
+                            if ($sTrunc != $sTmp) {
+                                if (!$title) {
+                                    $title = htmlspecialchars($sTmp);
+                                } else if ($title_maybe) {
+                                    $title = $title .', '.$title_maybe;
+                                    $title = substr($title,0,-2); // strip ', ';
+                                }
+                            }
                             $sTmp = $sTrunc;
                         }
                     }
@@ -570,6 +601,8 @@ function prepareMessageList(&$aMailbox, $aProps) {
                                          'answered'=>false,
                                          'flagged' => false,
                                          'draft' => false);
+                                         
+                    if(!is_array($value)) $value = array();                     
                     foreach ($value as $sFlag => $value) {
                         switch ($sFlag) {
                           case '\\seen'    : $aFlagColumn['seen']     = true; break;
@@ -672,7 +705,7 @@ function setUserPref($username, $pref, $value) {
 */
 function _get_sorted_msgs_list($imapConnection,&$aMailbox) {
     $iSetIndx = (isset($aMailbox['SETINDEX'])) ? $aMailbox['SETINDEX'] : 0;
-    $bDirection = ($aMailbox['SORT'] % 2);
+    $bDirection = !($aMailbox['SORT'] % 2);
     $error = 0;
     if (!$aMailbox['SEARCH'][$iSetIndx]) {
         $aMailbox['SEARCH'][$iSetIndx] = 'ALL';
@@ -869,6 +902,7 @@ function showMessagesForMailbox($imapConnection, &$aMailbox,$aProps, &$iError) {
              $aMailbox['PAGEOFFSET'] + $iLimit - 1 : $aMailbox['EXISTS'];
 
     $iNumberOfMessages = $aMailbox['TOTAL'][$iSetIndx];
+    $iEnd = min ( $iEnd, $iNumberOfMessages );
 
     $php_self = $PHP_SELF;
 
@@ -914,32 +948,35 @@ function showMessagesForMailbox($imapConnection, &$aMailbox,$aProps, &$iError) {
                            'markFlagged'   => 1,
                            'markRead'      => 1,
                            'markUnread'    => 1,
+                           'forward'       => 1,
                            'delete'        => 1,
                            'undeleteButton'=> 1,
                            'bypass_trash'  => 1,
                            'expungeButton' => 1,
-                           'moveButton'    => 1,
-                           'forward'       => 1
+                           'moveButton'    => 1
                            );
     /* user prefs control */
     $aUserControl = array (
+
                            'markUnflagged' => $show_flag_buttons,
                            'markFlagged'   => $show_flag_buttons,
                            'markRead'      => 1,
                            'markUnread'    => 1,
+                           'forward'       => 1,
                            'delete'        => 1,
                            'undeleteButton'=> 1,
                            'bypass_trash'  => 1,
                            'expungeButton' => 1,
-                           'moveButton'    => 1,
-                           'forward'       => 1
+                           'moveButton'    => 1
+
                           );
 
     $showDelete = ($aMailbox['RIGHTS'] != 'READ-ONLY' &&
                    in_array('\\deleted',$aMailbox['PERMANENTFLAGS'], true)) ? true : false;
-    $showByPassTrash = (!$aMailbox['AUTO_EXPUNGE'] && $aMailbox['RIGHTS'] != 'READ-ONLY' &&
+    $showByPassTrash = (($aMailbox['AUTO_EXPUNGE'] && $aMailbox['RIGHTS'] != 'READ-ONLY' &&
                    in_array('\\deleted',$aMailbox['PERMANENTFLAGS'], true)) &&
-                   $trash_folder ? true : false; //
+                   $trash_folder) ? true : false; //
+
     $showUndelete = (!$aMailbox['AUTO_EXPUNGE'] && $aMailbox['RIGHTS'] != 'READ-ONLY' &&
                    in_array('\\deleted',$aMailbox['PERMANENTFLAGS'], true) && !$trash_folder) ? true : false;
     $showMove   = ($aMailbox['RIGHTS'] != 'READ-ONLY') ? true : false;
@@ -950,25 +987,27 @@ function showMessagesForMailbox($imapConnection, &$aMailbox,$aProps, &$iError) {
                            'markFlagged'   => in_array('\\flagged',$aMailbox['PERMANENTFLAGS'], true),
                            'markRead'      => in_array('\\seen',$aMailbox['PERMANENTFLAGS'], true),
                            'markUnread'    => in_array('\\seen',$aMailbox['PERMANENTFLAGS'], true),
+                           'forward'       => 1,
                            'delete'        => $showDelete,
                            'undeleteButton'=> $showUndelete,
                            'bypass_trash'  => $showByPassTrash,
                            'expungeButton' => $showExpunge,
-                           'moveButton'    => $showMove,
-                           'forward'       => 1
+                           'moveButton'    => $showMove
                           );
     $aButtonStrings = array(
                            'markUnflagged' => _("Unflag"),
                            'markFlagged'   => _("Flag"),
                            'markRead'      => _("Read"),
                            'markUnread'    => _("Unread"),
+                           'forward'       => _("Forward"),
                            'delete'    => _("Delete"),
                            'undeleteButton'  => _("Undelete"),
                            'bypass_trash'  => _("Bypass Trash"),
                            'expungeButton' => _("Expunge"),
-                           'moveButton'          => _("Move"),
-                           'forward'       => _("Forward")
+                           'moveButton'          => _("Move")
                            );
+
+
     /**
      * Register buttons in order to an array
      * The key is the "name", the first element of the value array is the "value", second argument is the type.
@@ -1019,7 +1058,6 @@ function showMessagesForMailbox($imapConnection, &$aMailbox,$aProps, &$iError) {
     // FIX ME, before we support multiple templates we must review the names of the vars
 
 
-
     $aTemplate['color']     = $color;
     $aTemplate['form_name'] = "FormMsgs" . $safe_name;
     $aTemplate['form_id']   = 'mbx_'.$iFormId;
@@ -1049,53 +1087,58 @@ function showMessagesForMailbox($imapConnection, &$aMailbox,$aProps, &$iError) {
     $aTemplate['alt_index_colors'] = (isset($aProps['config']['alt_index_colors'])) ? $aProps['config']['alt_index_colors'] : false;
     $aTemplate['fancy_index_highlite'] = $fancy_index_highlite;
 
+
     return $aTemplate;
 }
 
 
 /**
-* FIXME: Undocumented function
+* Truncates a string and take care of html encoded characters
+*
+* @param string  $s string to truncate
+* @param int $iTrimAt Trim at nn characters
+* @return string  Trimmed string
 */
-function truncateWithEntities($subject, $trim_at)
-{
-    $ent_strlen = strlen($subject);
-    if (($trim_at <= 0) || ($ent_strlen <= $trim_at))
-        return $subject;
-
+function truncateWithEntities($s, $iTrimAt) {
     global $languages, $squirrelmail_language;
 
-    /*
-    * 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 $trim_at, substr with an updated trim value.
-    */
-    $trim_val = $trim_at;
-    $ent_offset = 0;
-    $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;
-    }
-    if (($trim_val > $trim_at) && ($ent_strlen > $trim_val) && (strpos($subject,';',$trim_val) < ($trim_val + 6))) {
-        $i = strpos($subject,';',$trim_val);
-        if ($i) {
-            $trim_val = strpos($subject,';',$trim_val);
-        }
-    }
-    // only print '...' when we're actually dropping part of the subject
-    if ($ent_strlen <= $trim_val)
-        return $subject;
+    $ent_strlen = strlen($s);
+    if (($iTrimAt <= 0) || ($ent_strlen <= $iTrimAt))
+        return $s;
 
     if (isset($languages[$squirrelmail_language]['XTRA_CODE']) &&
         function_exists($languages[$squirrelmail_language]['XTRA_CODE'] . '_strimwidth')) {
-        return call_user_func($languages[$squirrelmail_language]['XTRA_CODE'] . '_strimwidth', $subject, $trim_val);
-    }
+        return call_user_func($languages[$squirrelmail_language]['XTRA_CODE'] . '_strimwidth', $s, $iTrimAt);
+    } else {
+        /*
+        * 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 $iTrimAt, substr with an updated trim value.
+        */
+        $trim_val = $iTrimAt;
+        $ent_offset = 0;
+        $ent_loc = 0;
+        while ( $ent_loc < $trim_val && (($ent_loc = strpos($s, '&', $ent_offset)) !== false) &&
+                (($ent_loc_end = strpos($s, ';', $ent_loc+3)) !== false) ) {
+            $trim_val += ($ent_loc_end-$ent_loc);
+            $ent_offset  = $ent_loc_end+1;
+        }
 
-    return substr_replace($subject, '...', $trim_val);
+        if (($trim_val > $iTrimAt) && ($ent_strlen > $trim_val) && (strpos($s,';',$trim_val) < ($trim_val + 6))) {
+            $i = strpos($s,';',$trim_val);
+            if ($i !== false) {
+                $trim_val = strpos($s,';',$trim_val)+1;
+            }
+        }
+        // only print '...' when we're actually dropping part of the subject
+        if ($ent_strlen <= $trim_val)
+            return $s;
+    }
+    return substr_replace($s, '...', $trim_val);
 }
 
+
 /**
 * This should go in imap_mailbox.php
 * @param string $mailbox