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 &copy; 1999-2006 The SquirrelMail Project Team
  10.  * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  11.  * @version $Id: mailbox_display.php,v 1.465 2006/07/15 12:00:45 tokul Exp $
  12.  * @package squirrelmail
  13.  */
  14.  
  15.  
  16. /**
  17.  * Selects a mailbox for header retrieval.
  18.  * Cache control for message headers is embedded.
  19.  *
  20.  * @param resource $imapConnection imap socket handle
  21.  * @param string   $mailbox mailbox to select and retrieve message headers from
  22.  * @param array    $aConfig array with system config settings and incoming vars
  23.  * @param array    $aProps mailbox specific properties
  24.  * @return array   $aMailbox mailbox array with all relevant information
  25.  * @since 1.5.1
  26.  * @author Marc Groot Koerkamp
  27.  */
  28. function sqm_api_mailbox_select($imapConnection,$account,$mailbox,$aConfig,$aProps) {
  29.  
  30.     /**
  31.      * NB: retrieve this from the session before accessing this function
  32.      * and make sure you write it back at the end of the script after
  33.      * the aMailbox var is added so that the headers are added to the cache
  34.      */
  35.     global $mailbox_cache;
  36.  
  37.     $aDefaultConfigProps = array(
  38. //                'charset'           => 'US-ASCII',
  39.                 'user'              => false, /* no pref storage if false */
  40.                 'setindex'          => 0,
  41. //                'search'            => 'ALL',
  42.                 'max_cache_size'    => SQM_MAX_MBX_IN_CACHE
  43.                 );
  44.  
  45.     $aConfig = array_merge($aDefaultConfigProps,$aConfig);
  46.  
  47.     $iSetIndx = $aConfig['setindex'];
  48.  
  49.     $aMbxResponse = sqimap_mailbox_select($imapConnection, $mailbox);
  50.  
  51.     if ($mailbox_cache) {
  52.         if (isset($mailbox_cache[$account.'_'.$mailbox])) {
  53.             $aCachedMailbox = $mailbox_cache[$account.'_'.$mailbox];
  54.         } else {
  55.             $aCachedMailbox = false;
  56.         }
  57.             /* cleanup cache */
  58.         if (count($mailbox_cache) > $aConfig['max_cache_size'] -1) {
  59.             $aTime = array();
  60.             foreach($mailbox_cache as $cachedmailbox => $aVal) {
  61.                 $aTime[$aVal['TIMESTAMP']] = $cachedmailbox;
  62.             }
  63.             if (ksort($aTime,SORT_NUMERIC)) {
  64.                 for ($i=0,$iCnt=count($mailbox_cache);$i<($iCnt-$aConfig['max_cache_size']);++$i) {
  65.                     $sOldestMbx = array_shift($aTime);
  66.                     /**
  67.                      * Remove only the UIDSET and MSG_HEADERS from cache because those can
  68.                      * contain large amounts of data.
  69.                      */
  70.                     if (isset($mailbox_cache[$sOldestMbx]['UIDSET'])) {
  71.                         $mailbox_cache[$sOldestMbx]['UIDSET']= false;
  72.                     }
  73.                     if (isset($mailbox_cache[$sOldestMbx]['MSG_HEADERS'])) {
  74.                         $mailbox_cache[$sOldestMbx]['MSG_HEADERS'] = false;
  75.                     }
  76.                 }
  77.             }
  78.         }
  79.  
  80.     } else {
  81.         $aCachedMailbox = false;
  82.     }
  83.  
  84.     /**
  85.      * Deal with imap servers that do not return the required UIDNEXT or
  86.      * UIDVALIDITY response
  87.      * from a SELECT call (since rfc 3501 it's required).
  88.      */
  89.     if (!isset($aMbxResponse['UIDNEXT']) || !isset($aMbxResponse['UIDVALIDITY'])) {
  90.         $aStatus = sqimap_status_messages($imapConnection,$mailbox,
  91.                                         array('UIDNEXT','UIDVALIDITY'));
  92.         $aMbxResponse['UIDNEXT'] = $aStatus['UIDNEXT'];
  93.         $aMbxResponse['UIDVALIDTY'] = $aStatus['UIDVALIDITY'];
  94.     }
  95.  
  96.     $aMailbox['ACCOUNT'] = $account;
  97.     $aMailbox['UIDSET'][$iSetIndx] = false;
  98.     $aMailbox['ID'] = false;
  99.     $aMailbox['SETINDEX'] = $iSetIndx;
  100.     $aMailbox['MSG_HEADERS'] = false;
  101.  
  102.     if ($aCachedMailbox) {
  103.         /**
  104.          * Validate integrity of cached data
  105.          */
  106.         if ($aCachedMailbox['EXISTS'] == $aMbxResponse['EXISTS'] &&
  107.             $aMbxResponse['EXISTS'] &&
  108.             $aCachedMailbox['UIDVALIDITY'] == $aMbxResponse['UIDVALIDITY'] &&
  109.             $aCachedMailbox['UIDNEXT']  == $aMbxResponse['UIDNEXT'] &&
  110.             isset($aCachedMailbox['SEARCH'][$iSetIndx]) &&
  111.             (!isset($aConfig['search']) || /* always set search from the searchpage */
  112.              $aCachedMailbox['SEARCH'][$iSetIndx] == $aConfig['search'])) {
  113.             if (isset($aCachedMailbox['MSG_HEADERS'])) {
  114.                 $aMailbox['MSG_HEADERS'] = $aCachedMailbox['MSG_HEADERS'];
  115.             }
  116.             $aMailbox['ID'] =  $aCachedMailbox['ID'];
  117.             if (isset($aCachedMailbox['UIDSET'][$iSetIndx]) && $aCachedMailbox['UIDSET'][$iSetIndx]) {
  118.                 if (isset($aProps[MBX_PREF_SORT]) &&  $aProps[MBX_PREF_SORT] != $aCachedMailbox['SORT'] ) {
  119.                     $newsort = $aProps[MBX_PREF_SORT];
  120.                     $oldsort = $aCachedMailbox['SORT'];
  121.                     /**
  122.                      * If it concerns a reverse sort we do not need to invalidate
  123.                      * the cached sorted UIDSET, a reverse is sufficient.
  124.                      */
  125.                     if ((($newsort % 2) && ($newsort + 1 == $oldsort)) ||
  126.                         (!($newsort % 2) && ($newsort - 1 == $oldsort))) {
  127.                         $aMailbox['UIDSET'][$iSetIndx] = array_reverse($aCachedMailbox['UIDSET'][$iSetIndx]);
  128.                     } else {
  129.                         $server_sort_array = false;
  130.                         $aMailbox['MSG_HEADERS'] = false;
  131.                         $aMailbox['ID'] = false;
  132.                     }
  133.                     // store the new sort value in the mailbox pref
  134.                     if ($aConfig['user']) {
  135.                         // FIXME, in ideal situation, we write back the
  136.                         // prefs at the end of the script
  137.                         setUserPref($aConfig['user'],'pref_'.$account.'_'.$mailbox,serialize($aProps));
  138.                     }
  139.                 } else {
  140.                     $aMailbox['UIDSET'][$iSetIndx] = $aCachedMailbox['UIDSET'][$iSetIndx];
  141.                 }
  142.             }
  143.         }
  144.     }
  145.     /**
  146.      * Restore the offset in the paginator if no new offset is provided.
  147.      */
  148.     if (isset($aMailbox['UIDSET'][$iSetIndx]) && !isset($aConfig['offset']) && $aCachedMailbox['OFFSET']) {
  149.         $aMailbox['OFFSET'] =  $aCachedMailbox['OFFSET'];
  150.         $aMailbox['PAGEOFFSET'] =  $aCachedMailbox['PAGEOFFSET'];
  151.     } else {
  152.         $aMailbox['OFFSET'] = (isset($aConfig['offset']) && $aConfig['offset']) ? $aConfig['offset'] -1 : 0;
  153.         $aMailbox['PAGEOFFSET'] = (isset($aConfig['offset']) && $aConfig['offset']) ? $aConfig['offset'] : 1;
  154.     }
  155.     /**
  156.      * Restore the number of messages in the result set
  157.      */
  158.     if (isset($aCachedMailbox['TOTAL'][$iSetIndx]) && $aCachedMailbox['TOTAL'][$iSetIndx]) {
  159.         $aMailbox['TOTAL'][$iSetIndx] =  $aCachedMailbox['TOTAL'][$iSetIndx];
  160.     }
  161.  
  162.     /**
  163.      * Restore the showall value no new showall value is provided.
  164.      */
  165.     if (isset($aMailbox['UIDSET'][$iSetIndx]) && !isset($aConfig['showall']) &&
  166.         isset($aCachedMailbox['SHOWALL'][$iSetIndx]) && $aCachedMailbox['SHOWALL'][$iSetIndx]) {
  167.         $aMailbox['SHOWALL'][$iSetIndx] =  $aCachedMailbox['SHOWALL'][$iSetIndx];
  168.     } else {
  169.         $aMailbox['SHOWALL'][$iSetIndx] = (isset($aConfig['showall']) && $aConfig['showall']) ? 1 : 0;
  170.     }
  171.  
  172.     /**
  173.      * Restore the sort order if no new sort order is provided.
  174.      */
  175.     if (!isset($aProps[MBX_PREF_SORT]) && isset($aCachedMailbox['SORT'])) {
  176.         $aMailbox['SORT'] = $aCachedMailbox['SORT'];
  177.     } else {
  178.         $aMailbox['SORT'] =  (isset($aProps[MBX_PREF_SORT])) ? $aProps[MBX_PREF_SORT] : 0;
  179.     }
  180.  
  181.     /**
  182.      * Restore the number of message to show per page when no new limit is provided
  183.      */
  184.     if (!isset($aProps[MBX_PREF_LIMIT]) && isset($aCachedMailbox['LIMIT'])) {
  185.         $aMailbox['LIMIT'] = $aCachedMailbox['LIMIT'];
  186.     } else {
  187.         $aMailbox['LIMIT'] =  (isset($aProps[MBX_PREF_LIMIT])) ? $aProps[MBX_PREF_LIMIT] : 15;
  188.     }
  189.  
  190.     /**
  191.      * Restore the ordered columns to show when no new ordered columns are provided
  192.      */
  193.     if (!isset($aProps[MBX_PREF_COLUMNS]) && isset($aCachedMailbox['COLUMNS'])) {
  194.         $aMailbox['COLUMNS'] = $aCachedMailbox['COLUMNS'];
  195.     } else {
  196.         $aMailbox['COLUMNS'] =  (isset($aProps[MBX_PREF_COLUMNS])) ? $aProps[MBX_PREF_COLUMNS] :
  197.             array(SQM_COL_FLAGS,SQM_COL_FROM, SQM_COL_SUBJ, SQM_COL_FLAGS);
  198.     }
  199.  
  200.     /**
  201.      * Restore the headers we fetch the last time. Saves intitialisation stuff in read_body.
  202.      */
  203.     $aMailbox['FETCHHEADERS'] = (isset($aCachedMailbox['FETCHHEADERS'])) ? $aCachedMailbox['FETCHHEADERS'] : null;
  204.  
  205.     if (!isset($aProps[MBX_PREF_AUTO_EXPUNGE]) && isset($aCachedMailbox['AUTO_EXPUNGE'])) {
  206.         $aMailbox['AUTO_EXPUNGE'] = $aCachedMailbox['AUTO_EXPUNGE'];
  207.     } else {
  208.         $aMailbox['AUTO_EXPUNGE'] =  (isset($aProps[MBX_PREF_AUTO_EXPUNGE])) ? $aProps[MBX_PREF_AUTO_EXPUNGE] : false;
  209.     }
  210.     if (!isset($aConfig['search']) && isset($aCachedMailbox['SEARCH'][$iSetIndx])) {
  211.         $aMailbox['SEARCH'][$iSetIndx] = $aCachedMailbox['SEARCH'][$iSetIndx];
  212.     } else if (isset($aConfig['search']) && isset($aCachedMailbox['SEARCH'][$iSetIndx]) &&
  213.         $aConfig['search'] != $aCachedMailbox['SEARCH'][$iSetIndx]) {
  214.         // reset the pageindex
  215.         $aMailbox['SEARCH'][$iSetIndx] = $aConfig['search'];
  216.         $aMailbox['OFFSET'] = 0;
  217.         $aMailbox['PAGEOFFSET'] = 1;
  218.     } else {
  219.         $aMailbox['SEARCH'][$iSetIndx] =  (isset($aConfig['search'])) ? $aConfig['search'] : 'ALL';
  220.     }
  221.     if (!isset($aConfig['charset']) && isset($aCachedMailbox['CHARSET'][$iSetIndx])) {
  222.         $aMailbox['CHARSET'][$iSetIndx] = $aCachedMailbox['CHARSET'][$iSetIndx];
  223.     } else {
  224.         $aMailbox['CHARSET'][$iSetIndx] =  (isset($aConfig['charset'])) ? $aConfig['charset'] : 'US-ASCII';
  225.     }
  226.  
  227.     $aMailbox['NAME'] = $mailbox;
  228.     $aMailbox['EXISTS'] = $aMbxResponse['EXISTS'];
  229.     $aMailbox['SEEN'] = (isset($aMbxResponse['SEEN'])) ? $aMbxResponse['SEEN'] : $aMbxResponse['EXISTS'];
  230.     $aMailbox['RECENT'] = (isset($aMbxResponse['RECENT'])) ? $aMbxResponse['RECENT'] : 0;
  231.     $aMailbox['UIDVALIDITY'] = $aMbxResponse['UIDVALIDITY'];
  232.     $aMailbox['UIDNEXT'] = $aMbxResponse['UIDNEXT'];
  233.     $aMailbox['PERMANENTFLAGS'] = $aMbxResponse['PERMANENTFLAGS'];
  234.     $aMailbox['RIGHTS'] = $aMbxResponse['RIGHTS'];
  235.  
  236.     /* decide if we are thread sorting or not */
  237.     if ($aMailbox['SORT'] & SQSORT_THREAD) {
  238.         if (!sqimap_capability($imapConnection,'THREAD')) {
  239.             $aMailbox['SORT'] ^= SQSORT_THREAD;
  240.         } else {
  241.             $aMailbox['THREAD_INDENT'] = $aCachedMailbox['THREAD_INDENT'];
  242.         }
  243.     } else {
  244.         $aMailbox['THREAD_INDENT'] = false;
  245.     }
  246.  
  247.     /* set a timestamp for cachecontrol */
  248.     $aMailbox['TIMESTAMP'] = time();
  249.     return $aMailbox;
  250. }
  251.  
  252. /**
  253.  * Fetch the message headers for a mailbox. Settings are part of the aMailbox
  254.  * array. Dependent of the mailbox settings it deals with sort, thread and search
  255.  * If server sort is supported then SORT is also used for retrieving sorted search results
  256.  *
  257.  * @param resource $imapConnection imap socket handle
  258.  * @param array    $aMailbox (reference) mailbox retrieved from sqm_api_mailbox_select
  259.  * @return error   $error error number
  260.  * @since 1.5.1
  261.  * @author Marc Groot Koerkamp
  262.  */
  263. function fetchMessageHeaders($imapConnection, &$aMailbox) {
  264.  
  265.     /* FIX ME, this function is kind of big, maybe we can split it up in
  266.        a couple of functions. Make sure the functions are private and starts with _
  267.        Also make sure that the error codes are propagated */
  268.  
  269.     /**
  270.      * Retrieve the UIDSET.
  271.      * Setindex is used to be able to store multiple uid sets. That will make it
  272.      * possible to display the mailbox multiple times in different sort order
  273.      * or to store serach results separate from normal mailbox view.
  274.      */
  275.     $iSetIndx =  (isset($aMailbox['SETINDEX'])) ? $aMailbox['SETINDEX'] : 0;
  276.  
  277.     $iLimit = ($aMailbox['SHOWALL'][$iSetIndx]) ? $aMailbox['EXISTS'] : $aMailbox['LIMIT'];
  278.     /**
  279.      * Adjust the start_msg
  280.      */
  281.     $start_msg = $aMailbox['PAGEOFFSET'];
  282.     if($aMailbox['PAGEOFFSET'] > $aMailbox['EXISTS']) {
  283.         $start_msg -= $aMailbox['LIMIT'];
  284.         if($start_msg < 1) {
  285.             $start_msg = 1;
  286.         }
  287.     }
  288.  
  289.     if (is_array($aMailbox['UIDSET'])) {
  290.         $aUid =& $aMailbox['UIDSET'][$iSetIndx];
  291.     } else {
  292.         $aUid = false;
  293.     }
  294.     $aFetchHeaders = $aMailbox['FETCHHEADERS'];
  295.  
  296.     $iError = 0;
  297.     $aFetchItems = $aHeaderItems = array();
  298.     // initialize the fields we want to retrieve:
  299.     $aHeaderFields = array();
  300.     foreach ($aFetchHeaders as $v) {
  301.       switch ($v) {
  302.         case SQM_COL_DATE:       $aHeaderFields[] = 'Date';         break;
  303.         case SQM_COL_TO:         $aHeaderFields[] = 'To';           break;
  304.         case SQM_COL_CC:         $aHeaderFields[] = 'Cc';           break;
  305.         case SQM_COL_FROM:       $aHeaderFields[] = 'From';         break;
  306.         case SQM_COL_SUBJ:       $aHeaderFields[] = 'Subject';      break;
  307.         case SQM_COL_PRIO:       $aHeaderFields[] = 'X-Priority';   break;
  308.         case SQM_COL_ATTACHMENT: $aHeaderFields[] = 'Content-Type'; break;
  309.         case SQM_COL_INT_DATE:   $aFetchItems[]   = 'INTERNALDATE'; break;
  310.         case SQM_COL_FLAGS:      $aFetchItems[]   = 'FLAGS';        break;
  311.         case SQM_COL_SIZE:       $aFetchItems[]   = 'RFC822.SIZE';  break;
  312.         default: break;
  313.       }
  314.     }
  315.  
  316.     /**
  317.      * A uidset with sorted uid's is available. We can use the cache
  318.      */
  319.     if (isset($aUid) && $aUid ) {
  320.         // limit the cache to SQM_MAX_PAGES_IN_CACHE
  321.         if (!$aMailbox['SHOWALL'][$iSetIndx] && isset($aMailbox['MSG_HEADERS'])) {
  322.             $iMaxMsgs = $iLimit * SQM_MAX_PAGES_IN_CACHE;
  323.             $iCacheSize = count($aMailbox['MSG_HEADERS']);
  324.             if ($iCacheSize > $iMaxMsgs) {
  325.                 $iReduce = $iCacheSize - $iMaxMsgs;
  326.                 foreach ($aMailbox['MSG_HEADERS'] as $iUid => $value) {
  327.                     if ($iReduce) {
  328.                         unset($aMailbox['MSG_HEADERS'][$iUid]);
  329.                     } else {
  330.                         break;
  331.                     }
  332.                     --$iReduce;
  333.                 }
  334.             }
  335.         }
  336.  
  337.         $id_slice = array_slice($aUid,$start_msg-1,$iLimit);
  338.         /* do some funky cache checks */
  339.         if (isset($aMailbox['MSG_HEADERS']) && is_array($aMailbox['MSG_HEADERS'])) {
  340.             // temp code, read_body del / next links fo not update fields.
  341.             foreach ($aMailbox['MSG_HEADERS'] as $iUid => $aValue) {
  342.                 if (!isset($aValue['UID'])) {
  343.                     unset($aMailbox['MSG_HEADERS'][$iUid]);
  344.                 }
  345.             }
  346.             $aUidCached = array_keys($aMailbox['MSG_HEADERS']);
  347.         } else {
  348.             $aMailbox['MSG_HEADERS'] = array();
  349.             $aUidCached = array();
  350.         }
  351.         $aUidNotCached = array_values(array_diff($id_slice,$aUidCached));
  352.  
  353.         /**
  354.          * $aUidNotCached contains an array with UID's which need to be fetched to
  355.          * complete the needed message headers.
  356.          */
  357.         if (count($aUidNotCached)) {
  358.             $aMsgs = sqimap_get_small_header_list($imapConnection,$aUidNotCached,
  359.                                                     $aHeaderFields,$aFetchItems);
  360.             // append the msgs to the existend headers
  361.             $aMailbox['MSG_HEADERS'] += $aMsgs;
  362.         }
  363.     } else {
  364.         /**
  365.          * Initialize the sorted UID list or initiate a UID list with search
  366.          * results and fetch the visible message headers
  367.          */
  368.  
  369.         if ($aMailbox['SEARCH'][$iSetIndx] != 'ALL') { // in case of a search request
  370.  
  371.             if ($aMailbox['SEARCH'][$iSetIndx] && $aMailbox['SORT'] == 0) {
  372.                 $aUid = sqimap_run_search($imapConnection, $aMailbox['SEARCH'][$iSetIndx], $aMailbox['CHARSET'][$iSetIndx]);
  373.             } else {
  374.  
  375.                 $iError = 0;
  376.                 $iError = _get_sorted_msgs_list($imapConnection,$aMailbox,$iError);
  377.                 $aUid = $aMailbox['UIDSET'][$iSetIndx];
  378.             }
  379.             if (!$iError) {
  380.                 /**
  381.                  * Number of messages is the resultset
  382.                  */
  383.                 $aMailbox['TOTAL'][$iSetIndx] = count($aUid);
  384.                 $id_slice = array_slice($aUid,$aMailbox['OFFSET'], $iLimit);
  385.                 if (count($id_slice)) {
  386.                     $aMailbox['MSG_HEADERS'] = sqimap_get_small_header_list($imapConnection,$id_slice,
  387.                         $aHeaderFields,$aFetchItems);
  388.                 } else {
  389.                     $iError = 1; // FIX ME, define an error code
  390.                 }
  391.             }
  392.         } else { //
  393.             $iError = 0;
  394.             $iError = _get_sorted_msgs_list($imapConnection,$aMailbox,$iError);
  395.             $aUid = $aMailbox['UIDSET'][$iSetIndx];
  396.  
  397.             if (!$iError) {
  398.                 /**
  399.                  * Number of messages is the resultset
  400.                  */
  401.                 $aMailbox['TOTAL'][$iSetIndx] = count($aUid);
  402.                 $id_slice = array_slice($aUid,$aMailbox['OFFSET'], $iLimit);
  403.                 if (count($id_slice)) {
  404.                     $aMailbox['MSG_HEADERS'] = sqimap_get_small_header_list($imapConnection,$id_slice,
  405.                         $aHeaderFields,$aFetchItems);
  406.                 } else {
  407.                     $iError = 1; // FIX ME, define an error code
  408.                 }
  409.             }
  410.         }
  411.     }
  412.     return $iError;
  413. }
  414.  
  415. /**
  416.  * Prepares the message headers for display inside a template. The links are calculated,
  417.  * color for row highlighting is calculated and optionally the strings are truncated.
  418.  *
  419.  * @param array    $aMailbox (reference) mailbox retrieved from sqm_api_mailbox_select
  420.  * @param array    $aProps properties
  421.  * @return array   $aFormattedMessages array with message headers and format info
  422.  * @since 1.5.1
  423.  * @author Marc Groot Koerkamp
  424.  */
  425. function prepareMessageList(&$aMailbox, $aProps) {
  426.  
  427.     /* Globalize link attributes so plugins can share in modifying them */
  428.     global $link, $title, $target, $onclick, $link_extra;
  429.  
  430.     /* retrieve the properties */
  431.     $my_email_address = (isset($aProps['email'])) ? $aProps['email'] : false;
  432.     $highlight_list   = (isset($aProps['config']['highlight_list'])) ? $aProps['config']['highlight_list'] : false;
  433.     $aColumnDesc      = (isset($aProps['columns'])) ? $aProps['columns'] : false;
  434.     $aExtraColumns    = (isset($aProps['extra_columns'])) ? $aProps['extra_columns'] : array();
  435.     $iAccount         = (isset($aProps['account'])) ? (int) $aProps['account'] : 0;
  436.     $sMailbox         = (isset($aProps['mailbox'])) ? $aProps['mailbox'] : false;
  437.     $sTargetModule    = (isset($aProps['module'])) ? $aProps['module'] : 'read_body';
  438.  
  439.     /*
  440.      * TODO 1, retrieve array with identity email addresses in order to match against to,cc and set a flag
  441.      * $aFormattedMessages[$iUid]['match_identity'] = true
  442.      * The template can show some image if there is a match.
  443.      * TODO 2, makes sure the matching is done fast by doing a strpos call on the returned $value
  444.      */
  445.  
  446.     /**
  447.      * Only retrieve values for displayable columns
  448.      */
  449.     foreach ($aColumnDesc as $k => $v) {
  450.         switch ($k) {
  451.           case SQM_COL_FROM:       $aCol[SQM_COL_FROM]       = 'from';         break;
  452.           case SQM_COL_DATE:       $aCol[SQM_COL_DATE]       = 'date';         break;
  453.           case SQM_COL_SUBJ:       $aCol[SQM_COL_SUBJ]       = 'subject';      break;
  454.           case SQM_COL_FLAGS:      $aCol[SQM_COL_FLAGS]      = 'FLAGS';        break;
  455.           case SQM_COL_SIZE:       $aCol[SQM_COL_SIZE]       = 'SIZE';         break;
  456.           case SQM_COL_PRIO:       $aCol[SQM_COL_PRIO]       = 'x-priority';   break;
  457.           case SQM_COL_ATTACHMENT: $aCol[SQM_COL_ATTACHMENT] = 'content-type'; break;
  458.           case SQM_COL_INT_DATE:   $aCol[SQM_COL_INT_DATE]   = 'INTERNALDATE'; break;
  459.           case SQM_COL_TO:         $aCol[SQM_COL_TO]         = 'to';           break;
  460.           case SQM_COL_CC:         $aCol[SQM_COL_CC]         = 'cc';           break;
  461.           case SQM_COL_BCC:        $aCol[SQM_COL_BCC]        = 'bcc';          break;
  462.           default: break;
  463.         }
  464.     }
  465.     $aExtraHighLightColumns = array();
  466.     foreach ($aExtraColumns as $v) {
  467.         switch ($v) {
  468.           case SQM_COL_FROM:       $aExtraHighLightColumns[] = 'from';         break;
  469.           case SQM_COL_SUBJ:       $aExtraHighLightColumns[] = 'subject';      break;
  470.           case SQM_COL_TO:         $aExtraHighLightColumns[] = 'to';           break;
  471.           case SQM_COL_CC:         $aExtraHighLightColumns[] = 'cc';           break;
  472.           case SQM_COL_BCC:        $aExtraHighLightColumns[] = 'bcc';          break;
  473.           default: break;
  474.         }
  475.     }
  476.     $aFormattedMessages = array();
  477.  
  478.  
  479.     $iSetIndx    =  $aMailbox['SETINDEX'];
  480.     $aId         =  $aMailbox['UIDSET'][$iSetIndx];
  481.     $aHeaders    =& $aMailbox['MSG_HEADERS']; /* use a reference to avoid a copy.
  482.                                                  MSG_HEADERS can contain large amounts of data */
  483.     $iOffset     =  $aMailbox['OFFSET'];
  484.     $sort        =  $aMailbox['SORT'];
  485.     $iPageOffset =  $aMailbox['PAGEOFFSET'];
  486.     $sMailbox    =  $aMailbox['NAME'];
  487.     $sSearch     =  (isset($aMailbox['SEARCH'][$aMailbox['SETINDEX']]) &&
  488.                     $aMailbox['SEARCH'][$aMailbox['SETINDEX']] != 'ALL') ? $aMailbox['SEARCH'][$aMailbox['SETINDEX']] : false;
  489.     $aSearch     =  ($sSearch) ? array('search.php',$aMailbox['SETINDEX']) : null;
  490.     /* avoid improper usage */
  491.     if ($sMailbox && isset($iAccount) && $sTargetModule) {
  492.         $aInitQuery  = array("account=$iAccount",'mailbox='.urlencode($sMailbox));
  493.     } else {
  494.         $aInitQuery = false;
  495.     }
  496.  
  497.     if ($aMailbox['SORT'] & SQSORT_THREAD) {
  498.         $aIndentArray =& $aMailbox['THREAD_INDENT'][$aMailbox['SETINDEX']];
  499.         $bThread = true;
  500.     } else {
  501.         $bThread = false;
  502.     }
  503.     /*
  504.      * Retrieve value for checkbox column
  505.      */
  506.     if (!sqgetGlobalVar('checkall',$checkall,SQ_GET)) {
  507.         $checkall = false;
  508.     }
  509.  
  510.     /*
  511.      * Loop through and display the info for each message.
  512.      */
  513.     $iEnd = ($aMailbox['SHOWALL'][$iSetIndx]) ? $aMailbox['EXISTS'] : $iOffset + $aMailbox['LIMIT'];
  514.     for ($i=$iOffset,$t=0;$i<$iEnd;++$i) {
  515.         if (isset($aId[$i])) {
  516.  
  517.             $bHighLight = false;
  518.             $value = $title = $link = $target = $onclick = $link_extra = '';
  519.             $aQuery = ($aInitQuery !== false) ? $aInitQuery : false;
  520.             $aMsg = $aHeaders[$aId[$i]];
  521.             if (isset($aSearch) && count($aSearch) > 1 && $aQuery) {
  522.                 $aQuery[] = "where=". $aSearch[0];
  523.                 $aQuery[] = "what=" . $aSearch[1];
  524.             }
  525.             $iUid      = (isset($aMsg['UID'])) ? $aMsg['UID'] : $aId[$i];
  526.             if ($aQuery) {
  527.                 $aQuery[] = "passed_id=$aId[$i]";
  528.                 $aQuery[] = "startMessage=$iPageOffset";
  529.             }
  530.  
  531.             foreach ($aCol as $k => $v) {
  532.                 $title = $link = $target = $onclick = $link_extra = '';
  533.                 $aColumns[$k] = array();
  534.                 $value = (isset($aMsg[$v]))  ? $aMsg[$v]  : '';
  535.                 $sUnknown = _("Unknown recipient");
  536.                 switch ($k) {
  537.                 case SQM_COL_FROM:
  538.                     $sUnknown = _("Unknown sender");
  539.                 case SQM_COL_TO:
  540.                 case SQM_COL_CC:
  541.                 case SQM_COL_BCC:
  542.                     $sTmp = false;
  543.                     if ($value) {
  544.                         if ($highlight_list && !$bHighLight) {
  545.                             $bHighLight = highlightMessage($aCol[$k], $value, $highlight_list,$aFormattedMessages[$iUid]);
  546.                         }
  547.                         $aAddressList = parseRFC822Address($value);
  548.                         $sTmp = getAddressString($aAddressList,array('best' => true));
  549.                         $title = $title_maybe = '';
  550.                         foreach ($aAddressList as $aAddr) {
  551.                             $sPersonal = (isset($aAddr[SQM_ADDR_PERSONAL])) ? $aAddr[SQM_ADDR_PERSONAL] : '';
  552.                             $sMailbox  = (isset($aAddr[SQM_ADDR_MAILBOX]))  ? $aAddr[SQM_ADDR_MAILBOX]  : '';
  553.                             $sHost     = (isset($aAddr[SQM_ADDR_HOST]))     ? $aAddr[SQM_ADDR_HOST]     : '';
  554.                             if ($sPersonal) {
  555.                                 $title .= htmlspecialchars($sMailbox.'@'.$sHost).', ';
  556.                             } else {
  557.                                 // if $value gets truncated we need to add the addresses with no
  558.                                 // personal name as well
  559.                                 $title_maybe .= htmlspecialchars($sMailbox.'@'.$sHost).', ';
  560.                             }
  561.                         }
  562.                         if ($title) {
  563.                             $title = substr($title,0,-2); // strip ', ';
  564.                         }
  565.                         $sTmp = decodeHeader($sTmp);
  566.                         if (isset($aColumnDesc[$k]['truncate']) && $aColumnDesc[$k]['truncate']) {
  567.                             $sTrunc = truncateWithEntities($sTmp, $aColumnDesc[$k]['truncate']);
  568.                             if ($sTrunc != $sTmp) {
  569.                                 if (!$title) {
  570.                                     $title = htmlspecialchars($sTmp);
  571.                                 } else if ($title_maybe) {
  572.                                     $title = $title .', '.$title_maybe;
  573.                                     $title = substr($title,0,-2); // strip ', ';
  574.                                 }
  575.                             }
  576.                             $sTmp = $sTrunc;
  577.                         }
  578.                     }
  579.                     $value = ($sTmp) ? $sTmp : $sUnknown;
  580.                     break;
  581.                 case SQM_COL_SUBJ:
  582.                     // subject is mime encoded, decode it.
  583.                     // value is sanitized in decoding function.
  584.                     // TODO, verify if it should be done before or after the highlighting
  585.                     $value=decodeHeader($value);
  586.                     if ($highlight_list && !$bHighLight) {
  587.                         $bHighLight = highlightMessage('SUBJECT', $value, $highlight_list, $aFormattedMessages[$iUid]);
  588.                     }
  589.                     $iIndent = (isset($aIndentArray[$aId[$i]])) ? $aIndentArray[$aId[$i]] : 0;
  590.                     // FIXME: don't break 8bit symbols and html entities during truncation
  591.                     if (isset($aColumnDesc[$k]['truncate']) && $aColumnDesc[$k]['truncate']) {
  592.                         $sTmp = truncateWithEntities($value, $aColumnDesc[$k]['truncate']-$iIndent);
  593.                         // drop any double spaces since these will be displayed in the title
  594.                         $title = ($sTmp != $value) ? preg_replace('/\s{2,}/', ' ', $value) : '';
  595.                         $value = $sTmp;
  596.                     }
  597.                     /* generate the link to the message */
  598.                     if ($aQuery) {
  599.                         // TODO, $sTargetModule should be a query parameter so that we can use a single entrypoint
  600.                         $link = $sTargetModule.'.php?' . implode('&amp;',$aQuery);
  601.  
  602.                         // see top of this function for which attributes are available
  603.                         // in the global scope for plugin use (like $link, $target,
  604.                         // $onclick, $link_extra, $title, and so forth)
  605.                         // plugins are responsible for sharing nicely (such as for
  606.                         // setting the target, etc)
  607.                         do_hook('subject_link', array($iPageOffset, $sSearch, $aSearch));
  608.                     }
  609.                     $value = (trim($value)) ? $value : _("(no subject)");
  610.                     /* add thread indentation */
  611.                     $aColumns[$k]['indent']  = $iIndent;
  612.                     break;
  613.                 case SQM_COL_SIZE:
  614.                     $value = show_readable_size($value);
  615.                     break;
  616.                 case SQM_COL_DATE:
  617.                 case SQM_COL_INT_DATE:
  618.                     $value = getDateString(getTimeStamp(explode(' ',trim($value))));
  619.                     break;
  620.                 case SQM_COL_FLAGS:
  621.                     $aFlagColumn = array('seen' => false,
  622.                                          'deleted'=>false,
  623.                                          'answered'=>false,
  624.                                          'flagged' => false,
  625.                                          'draft' => false);
  626.  
  627.                     if(!is_array($value)) $value = array();
  628.                     foreach ($value as $sFlag => $value) {
  629.                         switch ($sFlag) {
  630.                           case '\\seen'    : $aFlagColumn['seen']     = true; break;
  631.                           case '\\deleted' : $aFlagColumn['deleted']  = true; break;
  632.                           case '\\answered': $aFlagColumn['answered'] = true; break;
  633.                           case '\\flagged' : $aFlagColumn['flagged']  = true; break;
  634.                           case '\\draft'   : $aFlagColumn['draft']    = true; break;
  635.                           default:  break;
  636.                         }
  637.                     }
  638.                     $value = $aFlagColumn;
  639.                     break;
  640.                 case SQM_COL_PRIO:
  641.                     $value = ($value) ? (int) $value : 3;
  642.                     break;
  643.                 case SQM_COL_ATTACHMENT:
  644.                     $value = (is_array($value) && $value[0] == 'multipart' && $value[1] == 'mixed') ? true : false;
  645.                     break;
  646.                 case SQM_COL_CHECK:
  647.                     $value = $checkall;
  648.                     break;
  649.                 default : break;
  650.                 }
  651.                 if ($title)      { $aColumns[$k]['title']      = $title;      }
  652.                 if ($link)       { $aColumns[$k]['link']       = $link;       }
  653.                 if ($link_extra) { $aColumns[$k]['link_extra'] = $link_extra; }
  654.                 if ($onclick)    { $aColumns[$k]['onclick']    = $onclick;    }
  655.                 if ($target)     { $aColumns[$k]['target']     = $target;     }
  656.                 $aColumns[$k]['value']  = $value;
  657.             }
  658.             /* columns which will not be displayed but should be inspected
  659.                because the highlight list contains rules with those columns */
  660.             foreach ($aExtraHighLightColumns as $v) {
  661.                 if ($highlight_list && !$bHighLight && isset($aMsg[$v])) {
  662.                     $bHighLight = highlightMessage($v, $aMsg[$v], $highlight_list,$aFormattedMessages[$iUid]);
  663.                 }
  664.             }
  665.             $aFormattedMessages[$iUid]['columns'] = $aColumns;
  666.  
  667.         } else {
  668.             break;
  669.         }
  670.     }
  671.     return $aFormattedMessages;
  672. }
  673.  
  674.  
  675. /**
  676.  * Sets the row color if the provided column value pair  matches a hightlight rule
  677.  *
  678.  * @param string   $sCol column name
  679.  * @param string   $sVal column value
  680.  * @param array    $highlight_list highlight rules
  681.  * @param array    $aFormat (reference) array where row color info is stored
  682.  * @return bool     match found
  683.  * @since 1.5.1
  684.  * @author Marc Groot Koerkamp
  685.  */
  686. function highlightMessage($sCol, $sVal, $highlight_list, &$aFormat) {
  687.     if (!is_array($highlight_list) && count($highlight_list) == 0) {
  688.         return false;
  689.     }
  690.     $hlt_color = false;
  691.     $sCol = strtoupper($sCol);
  692.  
  693.     foreach ($highlight_list as $highlight_list_part) {
  694.         if (trim($highlight_list_part['value'])) {
  695.             $high_val   = strtolower($highlight_list_part['value']);
  696.             $match_type = strtoupper($highlight_list_part['match_type']);
  697.             if($match_type == 'TO_CC') {
  698.                 if ($sCol == 'TO' || $sCol == 'CC') {
  699.                     $match_type = $sCol;
  700.                 } else {
  701.                     continue;
  702.                 }
  703.             } else {
  704.                 if ($match_type != $sCol) {
  705.                     continue;
  706.                 }
  707.             }
  708.             if (strpos(strtolower($sVal),$high_val) !== false) {
  709.                  $hlt_color = $highlight_list_part['color'];
  710.                  break;
  711.             }
  712.         }
  713.     }
  714.     if ($hlt_color) {
  715.         // Bug in highlight color???
  716.         if ($hlt_color{0} != '#') {
  717.             $hlt_color = '#'. $hlt_color;
  718.         }
  719.         $aFormat['row']['color'] = $hlt_color;
  720.         return true;
  721.     } else {
  722.         return false;
  723.     }
  724. }
  725.  
  726. function setUserPref($username, $pref, $value) {
  727.     global $data_dir;
  728.     setPref($data_dir,$username,$pref,$value);
  729. }
  730.  
  731. /**
  732.  * Execute the sorting for a mailbox
  733.  *
  734.  * @param  resource $imapConnection Imap connection
  735.  * @param  array    $aMailbox (reference) Mailbox retrieved with sqm_api_mailbox_select
  736.  * @return int      $error (reference) Error number
  737.  * @private
  738.  * @since 1.5.1
  739.  * @author Marc Groot Koerkamp
  740.  */
  741. function _get_sorted_msgs_list($imapConnection,&$aMailbox) {
  742.     $iSetIndx = (isset($aMailbox['SETINDEX'])) ? $aMailbox['SETINDEX'] : 0;
  743.     $bDirection = !($aMailbox['SORT'] % 2);
  744.     $error = 0;
  745.     if (!$aMailbox['SEARCH'][$iSetIndx]) {
  746.         $aMailbox['SEARCH'][$iSetIndx] = 'ALL';
  747.     }
  748.     if (($aMailbox['SORT'] & SQSORT_THREAD) && sqimap_capability($imapConnection,'THREAD')) {
  749.         $aRes = get_thread_sort($imapConnection,$aMailbox['SEARCH'][$iSetIndx]);
  750.         if ($aRes === false) {
  751.             $aMailbox['SORT'] -= SQSORT_THREAD;
  752.             $error = 1; // fix me, define an error code;
  753.         } else {
  754.             $aMailbox['UIDSET'][$iSetIndx] = $aRes[0];
  755.             $aMailbox['THREAD_INDENT'][$iSetIndx] = $aRes[1];
  756.         }
  757.     } else if ($aMailbox['SORT'] === SQSORT_NONE) {
  758.         $id = sqimap_run_search($imapConnection, 'ALL' , '');
  759.         if ($id === false) {
  760.             $error = 1; // fix me, define an error code
  761.         } else {
  762.             $aMailbox['UIDSET'][$iSetIndx] = array_reverse($id);
  763.             $aMailbox['TOTAL'][$iSetIndx] = $aMailbox['EXISTS'];
  764.         }
  765.     } else {
  766.         if (sqimap_capability($imapConnection,'SORT')) {
  767.              $sSortField = _getSortField($aMailbox['SORT'],true);
  768.              $id = sqimap_get_sort_order($imapConnection, $sSortField, $bDirection, $aMailbox['SEARCH'][$iSetIndx]);
  769.              if ($id === false) {
  770.                  $error = 1; // fix me, define an error code
  771.              } else {
  772.                 $aMailbox['UIDSET'][$iSetIndx] = $id;
  773.              }
  774.         } else {
  775.              $id = NULL;
  776.              if ($aMailbox['SEARCH'][$iSetIndx] != 'ALL') {
  777.                 $id = sqimap_run_search($imapConnection, $aMailbox['SEARCH'][$iSetIndx], $aMailbox['CHARSET'][$iSetIndx]);
  778.              }
  779.              $sSortField = _getSortField($aMailbox['SORT'],false);
  780.              $aMailbox['UIDSET'][$iSetIndx] = get_squirrel_sort($imapConnection, $sSortField, $bDirection, $id);
  781.         }
  782.     }
  783.     return $error;
  784. }
  785.  
  786. /**
  787.  * Does the $srt $_GET var to field mapping
  788.  *
  789.  * @param int $srt Field to sort on
  790.  * @param bool $bServerSort Server sorting is true
  791.  * @return string $sSortField Field to sort on
  792.  * @since 1.5.1
  793.  * @private
  794.  */
  795. function _getSortField($sort,$bServerSort) {
  796.     switch($sort) {
  797.         case SQSORT_NONE:
  798.             $sSortField = 'UID';
  799.             break;
  800.         case SQSORT_DATE_ASC:
  801.         case SQSORT_DATE_DESC:
  802.             $sSortField = 'DATE';
  803.             break;
  804.         case SQSORT_FROM_ASC:
  805.         case SQSORT_FROM_DESC:
  806.             $sSortField = 'FROM';
  807.             break;
  808.         case SQSORT_SUBJ_ASC:
  809.         case SQSORT_SUBJ_DESC:
  810.             $sSortField = 'SUBJECT';
  811.             break;
  812.         case SQSORT_SIZE_ASC:
  813.         case SQSORT_SIZE_DESC:
  814.             $sSortField = ($bServerSort) ? 'SIZE' : 'RFC822.SIZE';
  815.             break;
  816.         case SQSORT_TO_ASC:
  817.         case SQSORT_TO_DESC:
  818.             $sSortField = 'TO';
  819.             break;
  820.         case SQSORT_CC_ASC:
  821.         case SQSORT_CC_DESC:
  822.             $sSortField = 'CC';
  823.             break;
  824.         case SQSORT_INT_DATE_ASC:
  825.         case SQSORT_INT_DATE_DESC:
  826.             $sSortField = ($bServerSort) ? 'ARRIVAL' : 'INTERNALDATE';
  827.             break;
  828.         case SQSORT_THREAD:
  829.             break;
  830.         default: $sSortField = 'UID';
  831.             break;
  832.  
  833.     }
  834.     return $sSortField;
  835. }
  836.  
  837. /**
  838.  * This function is a utility function for setting which headers should be
  839.  * fetched. It takes into account the highlight list which requires extra
  840.  * headers to be fetch in order to make those rules work. It's called before
  841.  * the headers are fetched which happens in showMessagesForMailbox and when
  842.  * the next and prev links in read_body.php are used.
  843.  *
  844.  * @param array    $aMailbox associative array with mailbox related vars
  845.  * @param array    $aProps 
  846.  * @return void 
  847.  * @since 1.5.1
  848.  */
  849.  
  850. function calcFetchColumns(&$aMailbox, &$aProps) {
  851.  
  852.     $highlight_list    = (isset($aProps['config']['highlight_list'])) ? $aProps['config']['highlight_list'] : false;
  853.     $aColumnsDesc      = (isset($aProps['columns'])) ? $aProps['columns'] : false;
  854.  
  855.     $aFetchColumns = $aColumnsDesc;
  856.     if (isset($aFetchColumns[SQM_COL_CHECK])) {
  857.         unset($aFetchColumns[SQM_COL_CHECK]);
  858.     }
  859.  
  860.     /*
  861.      * Before we fetch the message headers, check if we need to fetch extra columns
  862.      * to make the message highlighting work
  863.      */
  864.     if (is_array($highlight_list) && count($highlight_list)) {
  865.         $aHighlightColumns = array();
  866.         foreach ($highlight_list as $highlight_list_part) {
  867.             if (trim($highlight_list_part['value'])) {
  868.                 $match_type = strtoupper($highlight_list_part['match_type']);
  869.                 switch ($match_type) {
  870.                     case 'TO_CC':
  871.                         $aHighlightColumns[SQM_COL_TO] = true;
  872.                         $aHighlightColumns[SQM_COL_CC] = true;
  873.                         break;
  874.                     case 'TO':     $aHighlightColumns[SQM_COL_TO] = true; break;
  875.                     case 'CC':     $aHighlightColumns[SQM_COL_CC] = true; break;
  876.                     case 'FROM':   $aHighlightColumns[SQM_COL_FROM] = true; break;
  877.                     case 'SUBJECT':$aHighlightColumns[SQM_COL_SUBJ] = true; break;
  878.                 }
  879.             }
  880.         }
  881.         $aExtraColumns = array();
  882.         foreach ($aHighlightColumns as $k => $v) {
  883.             if (!isset($aFetchColumns[$k])) {
  884.                 $aExtraColumns[]  = $k;
  885.                 $aFetchColumns[$k] = true;
  886.             }
  887.         }
  888.         if (count($aExtraColumns)) {
  889.             $aProps['extra_columns'] = $aExtraColumns;
  890.         }
  891.     }
  892.     $aMailbox['FETCHHEADERS'] =  array_keys($aFetchColumns);
  893. }
  894.  
  895.  
  896. /**
  897.  * This function loops through a group of messages in the mailbox
  898.  * and shows them to the user.
  899.  *
  900.  * @param resource $imapConnection 
  901.  * @param array    $aMailbox associative array with mailbox related vars
  902.  * @param array    $aProps 
  903.  * @param int      $iError error code, 0 is no error
  904.  */
  905. function showMessagesForMailbox($imapConnection, &$aMailbox,$aProps, &$iError) {
  906.     global $PHP_SELF;
  907.     global $boxes, $show_copy_buttons;
  908.  
  909.     $highlight_list    = (isset($aProps['config']['highlight_list'])) ? $aProps['config']['highlight_list'] : false;
  910.     $fancy_index_highlite = (isset($aProps['config']['fancy_index_highlite'])) ? $aProps['config']['fancy_index_highlite'] : true;
  911.     $aColumnsDesc      = (isset($aProps['columns'])) ? $aProps['columns'] : false;
  912.     $iAccount          = (isset($aProps['account'])) ? (int) $aProps['account'] : 0;
  913.     $sMailbox          = (isset($aProps['mailbox'])) ? $aProps['mailbox'] : false;
  914.     $sTargetModule     = (isset($aProps['module'])) ? $aProps['module'] : 'read_body';
  915.     $show_flag_buttons = (isset($aProps['config']['show_flag_buttons'])) ? $aProps['config']['show_flag_buttons'] : true;
  916.  
  917.     /* allows to control copy button in function call. If array key is not set, code follows user preferences */
  918.     if (isset($aProps['config']['show_copy_buttons']))
  919.         $show_copy_buttons = $aProps['config']['show_copy_buttons'];
  920.  
  921.     $lastTargetMailbox = (isset($aProps['config']['lastTargetMailbox'])) ? $aProps['config']['lastTargetMailbox'] : '';
  922.     $aOrder = array_keys($aProps['columns']);
  923.     $trash_folder      = (isset($aProps['config']['trash_folder']) && $aProps['config']['trash_folder'])
  924.                           ? $aProps['config']['trash_folder'] : false;
  925.     $sent_folder       = (isset($aProps['config']['sent_folder']) && $aProps['config']['sent_folder'])
  926.                           ? $aProps['config']['sent_folder'] : false;
  927.     $draft_folder      = (isset($aProps['config']['draft_folder']) && $aProps['config']['draft_folder'])
  928.                           ? $aProps['config']['draft_folder'] : false;
  929.     $page_selector     = (isset($aProps['config']['page_selector'])) ? $aProps['config']['page_selector'] : false;
  930.     $page_selector_max = (isset($aProps['config']['page_selector_max'])) ? $aProps['config']['page_selector_max'] : 10;
  931.     $color             = $aProps['config']['color'];
  932.  
  933.  
  934.     /*
  935.      * Form ID
  936.      */
  937.     static $iFormId;
  938.  
  939.     if (!isset($iFormId)) {
  940.         $iFormId=1;
  941.     } else {
  942.         ++$iFormId;
  943.     }
  944.     // store the columns to fetch so we can pick them up in read_body
  945.     // where we validate the cache.
  946.     calcFetchColumns($aMailbox  ,$aProps);
  947.  
  948.     $iError = fetchMessageHeaders($imapConnection, $aMailbox);
  949.     if ($iError) {
  950.         return array();
  951.     } else {
  952.         $aMessages = prepareMessageList($aMailbox, $aProps);
  953.     }
  954.  
  955.     $iSetIndx = $aMailbox['SETINDEX'];
  956.     $iLimit = ($aMailbox['SHOWALL'][$iSetIndx]) ? $aMailbox['EXISTS'] : $aMailbox['LIMIT'];
  957.     $iEnd = ($aMailbox['PAGEOFFSET'] + ($iLimit - 1) < $aMailbox['EXISTS']) ?
  958.              $aMailbox['PAGEOFFSET'] + $iLimit - 1 : $aMailbox['EXISTS'];
  959.  
  960.     $iNumberOfMessages = $aMailbox['TOTAL'][$iSetIndx];
  961.     $iEnd = min ( $iEnd, $iNumberOfMessages );
  962.  
  963.     $php_self = $PHP_SELF;
  964.  
  965.     $urlMailbox = urlencode($aMailbox['NAME']);
  966.  
  967.     if (preg_match('/^(.+)\?.+$/',$php_self,$regs)) {
  968.         $source_url = $regs[1];
  969.     } else {
  970.         $source_url = $php_self;
  971.     }
  972.  
  973.     $baseurl = $source_url.'?mailbox=' . urlencode($aMailbox['NAME']) .'&amp;account='.$aMailbox['ACCOUNT'];
  974.     $where = urlencode($aMailbox['SEARCH'][$iSetIndx][0]);
  975.     $what = urlencode($aMailbox['SEARCH'][$iSetIndx][1]);
  976.     $baseurl .= '&amp;where=' . $where .  '&amp;what=' .  $what;
  977.  
  978.     /* build thread sorting links */
  979.     $newsort = $aMailbox['SORT'];
  980.     if (sqimap_capability($imapConnection,'THREAD')) {
  981.         if ($aMailbox['SORT'] & SQSORT_THREAD) {
  982.             $newsort -= SQSORT_THREAD;
  983.             $thread_name = _("Unthread View");
  984.         } else {
  985.             $thread_name = _("Thread View");
  986.             $newsort = $aMailbox['SORT'] + SQSORT_THREAD;
  987.         }
  988.         $thread_link_str = '<small>[<a href="' . $baseurl . '&amp;srt='
  989.             . $newsort . '&amp;startMessage=1">' . $thread_name
  990.             . '</a>]</small>';
  991.     } else {
  992.         $thread_link_str ='';
  993.     }
  994.     $sort = $aMailbox['SORT'];
  995.  
  996.     /* FIX ME ADD CHECKBOX CONTROL. No checkbox => no buttons */
  997.  
  998.  
  999.  
  1000.     /* future admin control over displayable buttons */
  1001.     $aAdminControl = array(
  1002.                            'markUnflagged' => 1,
  1003.                            'markFlagged'   => 1,
  1004.                            'markRead'      => 1,
  1005.                            'markUnread'    => 1,
  1006.                            'forward'       => 1,
  1007.                            'delete'        => 1,
  1008.                            'undeleteButton'=> 1,
  1009.                            'bypass_trash'  => 1,
  1010.                            'expungeButton' => 1,
  1011.                            'moveButton'    => 1,
  1012.                            'copyButton'    => 1
  1013.                            );
  1014.  
  1015.     /* user prefs control */
  1016.     $aUserControl = array (
  1017.  
  1018.                            'markUnflagged' => $show_flag_buttons,
  1019.                            'markFlagged'   => $show_flag_buttons,
  1020.                            'markRead'      => 1,
  1021.                            'markUnread'    => 1,
  1022.                            'forward'       => 1,
  1023.                            'delete'        => 1,
  1024.                            'undeleteButton'=> 1,
  1025.                            'bypass_trash'  => 1,
  1026.                            'expungeButton' => 1,
  1027.                            'moveButton'    => 1,
  1028.                            'copyButton'    => $show_copy_buttons
  1029.  
  1030.                           );
  1031.  
  1032.     $showDelete = ($aMailbox['RIGHTS'] != 'READ-ONLY' &&
  1033.                    in_array('\\deleted',$aMailbox['PERMANENTFLAGS'], true)) ? true : false;
  1034.     $showByPassTrash = (($aMailbox['AUTO_EXPUNGE'] && $aMailbox['RIGHTS'] != 'READ-ONLY' &&
  1035.                    in_array('\\deleted',$aMailbox['PERMANENTFLAGS'], true)) &&
  1036.                    $trash_folder) ? true : false; //
  1037.  
  1038.     $showUndelete = (!$aMailbox['AUTO_EXPUNGE'] && $aMailbox['RIGHTS'] != 'READ-ONLY' &&
  1039.                    in_array('\\deleted',$aMailbox['PERMANENTFLAGS'], true) && !$trash_folder) ? true : false;
  1040.     $showMove   = ($aMailbox['RIGHTS'] != 'READ-ONLY') ? true : false;
  1041.     $showExpunge = (!$aMailbox['AUTO_EXPUNGE'] && $aMailbox['RIGHTS'] != 'READ-ONLY' &&
  1042.                    in_array('\\deleted',$aMailbox['PERMANENTFLAGS'], true)) ? true : false;
  1043.  
  1044.     /* Button options that depend on IMAP server and selected folder */
  1045.     $aImapControl = array (
  1046.                            'markUnflagged' => in_array('\\flagged',$aMailbox['PERMANENTFLAGS'], true),
  1047.                            'markFlagged'   => in_array('\\flagged',$aMailbox['PERMANENTFLAGS'], true),
  1048.                            'markRead'      => in_array('\\seen',$aMailbox['PERMANENTFLAGS'], true),
  1049.                            'markUnread'    => in_array('\\seen',$aMailbox['PERMANENTFLAGS'], true),
  1050.                            'forward'       => 1,
  1051.                            'delete'        => $showDelete,
  1052.                            'undeleteButton'=> $showUndelete,
  1053.                            'bypass_trash'  => $showByPassTrash,
  1054.                            'expungeButton' => $showExpunge,
  1055.                            'moveButton'    => $showMove,
  1056.                            'copyButton'    => 1
  1057.                           );
  1058.     /* Button strings */
  1059.     $aButtonStrings = array(
  1060.                            'markUnflagged' => _("Unflag"),
  1061.                            'markFlagged'   => _("Flag"),
  1062.                            'markRead'      => _("Read"),
  1063.                            'markUnread'    => _("Unread"),
  1064.                            'forward'       => _("Forward"),
  1065.                            'delete'    => _("Delete"),
  1066.                            'undeleteButton'  => _("Undelete"),
  1067.                            'bypass_trash'  => _("Bypass Trash"),
  1068.                            'expungeButton' => _("Expunge"),
  1069.                            'moveButton'          => _("Move"),
  1070.                            'copyButton'          => _("Copy")
  1071.                            );
  1072.  
  1073.  
  1074.     /**
  1075.      * Register buttons in order to an array
  1076.      * The key is the "name", the first element of the value array is the "value", second argument is the type.
  1077.      */
  1078.     $aFormElements = array();
  1079.     foreach($aAdminControl as $k => $v) {
  1080.         if ($v & $aUserControl[$k] & $aImapControl[$k]) {
  1081.             switch ($k) {
  1082.               case 'markUnflagged':
  1083.               case 'markFlagged':
  1084.               case 'markRead':
  1085.               case 'markUnread':
  1086.               case 'delete':
  1087.               case 'undeleteButton':
  1088.               case 'expungeButton':
  1089.               case 'forward':
  1090.                 $aFormElements[$k] = array($aButtonStrings[$k],'submit');
  1091.                 break;
  1092.               case 'bypass_trash':
  1093.                 $aFormElements[$k] = array($aButtonStrings[$k],'checkbox');
  1094.                 break;
  1095.               case 'moveButton':
  1096.               case 'copyButton':
  1097.                 $aFormElements['targetMailbox'] =
  1098.                    array(sqimap_mailbox_option_list($imapConnection, array(strtolower($lastTargetMailbox)), 0, $boxes),'select');
  1099.                 $aFormElements['mailbox']       = array($aMailbox['NAME'],'hidden');
  1100.                 $aFormElements['startMessage']  = array($aMailbox['PAGEOFFSET'],'hidden');
  1101.                 $aFormElements[$k]              = array($aButtonStrings[$k],'submit');
  1102.                 break;
  1103.             }
  1104.         }
  1105.         $aFormElements['account']  = array($iAccount,'hidden');
  1106.     }
  1107.  
  1108.     /*
  1109.      * This is the beginning of the message list table.
  1110.      * It wraps around all messages
  1111.      */
  1112.     $safe_name = preg_replace("/[^0-9A-Za-z_]/", '_', $aMailbox['NAME']);
  1113.     $form_name = "FormMsgs" . $safe_name;
  1114.  
  1115.     //if (!sqgetGlobalVar('align',$align,SQ_SESSION)) {
  1116.         $align = array('left' => 'left', 'right' => 'right');
  1117.     //}
  1118.     //sm_print_r($align);
  1119.  
  1120.     /* finally set the template vars */
  1121.  
  1122.     // FIX ME, before we support multiple templates we must review the names of the vars
  1123.  
  1124.  
  1125.     $aTemplate['color']     = $color;
  1126.     $aTemplate['form_name'] = "FormMsgs" . $safe_name;
  1127.     $aTemplate['form_id']   = 'mbx_'.$iFormId;
  1128.     $aTemplate['page_selector'] = $page_selector;
  1129.     $aTemplate['page_selector_max'] = $page_selector_max;
  1130.     $aTemplate['messagesPerPage'] = $aMailbox['LIMIT'];
  1131.     $aTemplate['showall'] = $aMailbox['SHOWALL'][$iSetIndx];
  1132.     $aTemplate['end_msg'] = $iEnd;
  1133.     $aTemplate['align'] = $align;
  1134.     $aTemplate['iNumberOfMessages'] = $iNumberOfMessages;
  1135.     $aTemplate['aOrder'] = $aOrder;
  1136.     $aTemplate['aFormElements'] = $aFormElements;
  1137.     $aTemplate['sort'] = $sort;
  1138.     $aTemplate['pageOffset'] = $aMailbox['PAGEOFFSET'];
  1139.     $aTemplate['baseurl'] = $baseurl;
  1140.     $aTemplate['aMessages'] =& $aMessages;
  1141.     $aTemplate['trash_folder'] = $trash_folder;
  1142.     $aTemplate['sent_folder'] = $sent_folder;
  1143.     $aTemplate['draft_folder'] = $draft_folder;
  1144.     $aTemplate['thread_link_str'] = $thread_link_str;
  1145.     $aTemplate['php_self'] = str_replace('&','&amp;',$php_self);
  1146.     $aTemplate['mailbox'] = $sMailbox;
  1147.     $aTemplate['javascript_on'] = (isset($aProps['config']['javascript_on'])) ? $aProps['config']['javascript_on'] : false;
  1148.     $aTemplate['enablesort'] = (isset($aProps['config']['enablesort'])) ? $aProps['config']['enablesort'] : false;
  1149.     $aTemplate['icon_theme'] = (isset($aProps['config']['icon_theme'])) ? $aProps['config']['icon_theme'] : false;
  1150.     $aTemplate['use_icons'] = (isset($aProps['config']['use_icons'])) ? $aProps['config']['use_icons'] : false;
  1151.     $aTemplate['alt_index_colors'] = (isset($aProps['config']['alt_index_colors'])) ? $aProps['config']['alt_index_colors'] : false;
  1152.     $aTemplate['fancy_index_highlite'] = $fancy_index_highlite;
  1153.  
  1154.  
  1155.     return $aTemplate;
  1156. }
  1157.  
  1158.  
  1159. /**
  1160.  * Truncates a string and take care of html encoded characters
  1161.  *
  1162.  * @param string  $s string to truncate
  1163.  * @param int $iTrimAt Trim at nn characters
  1164.  * @return string  Trimmed string
  1165.  */
  1166. function truncateWithEntities($s, $iTrimAt) {
  1167.     global $languages, $squirrelmail_language;
  1168.  
  1169.     $ent_strlen = strlen($s);
  1170.     if (($iTrimAt <= 0) || ($ent_strlen <= $iTrimAt))
  1171.         return $s;
  1172.  
  1173.     if (isset($languages[$squirrelmail_language]['XTRA_CODE']) &&
  1174.         function_exists($languages[$squirrelmail_language]['XTRA_CODE'] . '_strimwidth')) {
  1175.         return call_user_func($languages[$squirrelmail_language]['XTRA_CODE'] . '_strimwidth', $s, $iTrimAt);
  1176.     } else {
  1177.         /*
  1178.          * see if this is entities-encoded string
  1179.          * If so, Iterate through the whole string, find out
  1180.          * the real number of characters, and if more
  1181.          * than $iTrimAt, substr with an updated trim value.
  1182.          */
  1183.         $trim_val = $iTrimAt;
  1184.         $ent_offset = 0;
  1185.         $ent_loc = 0;
  1186.         while ( $ent_loc < $trim_val && (($ent_loc = strpos($s, '&', $ent_offset)) !== false) &&
  1187.                 (($ent_loc_end = strpos($s, ';', $ent_loc+3)) !== false) ) {
  1188.             $trim_val += ($ent_loc_end-$ent_loc);
  1189.             $ent_offset  = $ent_loc_end+1;
  1190.         }
  1191.  
  1192.         if (($trim_val > $iTrimAt) && ($ent_strlen > $trim_val) && (strpos($s,';',$trim_val) < ($trim_val + 6))) {
  1193.             $i = strpos($s,';',$trim_val);
  1194.             if ($i !== false) {
  1195.                 $trim_val = strpos($s,';',$trim_val)+1;
  1196.             }
  1197.         }
  1198.         // only print '...' when we're actually dropping part of the subject
  1199.         if ($ent_strlen <= $trim_val)
  1200.             return $s;
  1201.     }
  1202.     return substr_replace($s, '...', $trim_val);
  1203. }
  1204.  
  1205.  
  1206. /**
  1207.  * This should go in imap_mailbox.php
  1208.  * @param string $mailbox 
  1209.  */
  1210. function handleAsSent($mailbox) {
  1211.     global $handleAsSent_result;
  1212.  
  1213.     /* First check if this is the sent or draft folder. */
  1214.     $handleAsSent_result = isSentMailbox($mailbox) || isDraftMailbox($mailbox);
  1215.  
  1216.     /* Then check the result of the handleAsSent hook. */
  1217.     do_hook('check_handleAsSent_result', $mailbox);
  1218.  
  1219.     /* And return the result. */
  1220.     return $handleAsSent_result;
  1221. }
  1222.  
  1223. /**
  1224.  * Process messages list form and handle the cache gracefully. If $sButton and
  1225.  * $aUid are provided as argument then you can fake a message list submit and
  1226.  * use it i.e. in read_body.php for del move next and update the cache
  1227.  *
  1228.  * @param  resource $imapConnection imap connection
  1229.  * @param  array $aMailbox (reference) cached mailbox
  1230.  * @param  string $sButton fake a submit button
  1231.  * @param  array  $aUid    fake the $msg array
  1232.  * @return string $sError error string in case of an error
  1233.  * @since 1.5.1
  1234.  * @author Marc Groot Koerkamp
  1235.  */
  1236. function handleMessageListForm($imapConnection,&$aMailbox,$sButton='',$aUid = array()) {
  1237.     /* incoming formdata */
  1238.     $sButton = (sqgetGlobalVar('moveButton',      $sTmp, SQ_POST)) ? 'move'         : $sButton;
  1239.     $sButton = (sqgetGlobalVar('copyButton',      $sTmp, SQ_POST)) ? 'copy'         : $sButton;
  1240.     $sButton = (sqgetGlobalVar('expungeButton',   $sTmp, SQ_POST)) ? 'expunge'      : $sButton;
  1241.     $sButton = (sqgetGlobalVar('forward',         $sTmp, SQ_POST)) ? 'forward'      : $sButton;
  1242.     $sButton = (sqgetGlobalVar('delete',          $sTmp, SQ_POST)) ? 'setDeleted'   : $sButton;
  1243.     $sButton = (sqgetGlobalVar('undeleteButton',  $sTmp, SQ_POST)) ? 'unsetDeleted'   : $sButton;
  1244.     $sButton = (sqgetGlobalVar('markRead',        $sTmp, SQ_POST)) ? 'setSeen'      : $sButton;
  1245.     $sButton = (sqgetGlobalVar('markUnread',      $sTmp, SQ_POST)) ? 'unsetSeen'    : $sButton;
  1246.     $sButton = (sqgetGlobalVar('markFlagged',     $sTmp, SQ_POST)) ? 'setFlagged'   : $sButton;
  1247.     $sButton = (sqgetGlobalVar('markUnflagged',   $sTmp, SQ_POST)) ? 'unsetFlagged' : $sButton;
  1248.     sqgetGlobalVar('targetMailbox', $targetMailbox,   SQ_POST);
  1249.     sqgetGlobalVar('bypass_trash',  $bypass_trash,    SQ_POST);
  1250.     sqgetGlobalVar('msg',           $msg,             SQ_POST);
  1251.     if (sqgetGlobalVar('account',       $iAccount,        SQ_POST) === false) {
  1252.         $iAccount = 0;
  1253.     }
  1254.     $sError = '';
  1255.     $mailbox = $aMailbox['NAME'];
  1256.  
  1257.     /* retrieve the check boxes */
  1258.     $aUid = (isset($msg) && is_array($msg)) ? array_values($msg) : $aUid;
  1259.     if (count($aUid) && $sButton != 'expunge') {
  1260.         $aUpdatedMsgs = false;
  1261.         $bExpunge = false;
  1262.         switch ($sButton) {
  1263.           case 'setDeleted':
  1264.             // check if id exists in case we come from read_body
  1265.             if (count($aUid) == 1 && is_array($aMailbox['UIDSET'][$aMailbox['SETINDEX']]) &&
  1266.                 !in_array($aUid[0],$aMailbox['UIDSET'][$aMailbox['SETINDEX']])) {
  1267.                 break;
  1268.             }
  1269.             $aUpdatedMsgs = sqimap_msgs_list_delete($imapConnection, $mailbox, $aUid,$bypass_trash);
  1270.             $bExpunge = true;
  1271.             //}
  1272.             break;
  1273.           case 'unsetDeleted':
  1274.           case 'setSeen':
  1275.           case 'unsetSeen':
  1276.           case 'setFlagged':
  1277.           case 'unsetFlagged':
  1278.             // get flag
  1279.             $sFlag = (substr($sButton,0,3) == 'set') ? '\\'.substr($sButton,3) : '\\'.substr($sButton,5);
  1280.             $bSet  = (substr($sButton,0,3) == 'set') ? true : false;
  1281.             $aUpdatedMsgs = sqimap_toggle_flag($imapConnection, $aUid, $sFlag, $bSet, true);
  1282.             break;
  1283.           case 'move':
  1284.             $aUpdatedMsgs = sqimap_msgs_list_move($imapConnection,$aUid,$targetMailbox,true,$mailbox);
  1285.             sqsession_register($targetMailbox,'lastTargetMailbox');
  1286.             $bExpunge = true;
  1287.             break;
  1288.           case 'copy':
  1289.             // sqimap_msgs_list_copy returns true or false.
  1290.             // If error happens - fourth argument handles it inside function.
  1291.             sqimap_msgs_list_copy($imapConnection,$aUid,$targetMailbox,true);
  1292.             sqsession_register($targetMailbox,'lastTargetMailbox');
  1293.             break;
  1294.           case 'forward':
  1295.             $aMsgHeaders = array();
  1296.             foreach ($aUid as $iUid) {
  1297.                 $aMsgHeaders[$iUid] = $aMailbox['MSG_HEADERS'][$iUid];
  1298.             }
  1299.             if (count($aMsgHeaders)) {
  1300.                 $composesession = attachSelectedMessages($imapConnection,$aMsgHeaders);
  1301.                 // dirty hack, add info to $aMailbox
  1302.                 $aMailbox['FORWARD_SESSION'] = $composesession;
  1303.             }
  1304.             break;
  1305.           default:
  1306.              // Hook for plugin buttons
  1307.              do_hook_function('mailbox_display_button_action', $aUid);
  1308.              break;
  1309.         }
  1310.         /**
  1311.          * Updates messages is an array containing the result of the untagged
  1312.          * fetch responses send by the imap server due to a flag change. That
  1313.          * response is parsed in a array with msg arrays by the parseFetch function
  1314.          */
  1315.         if ($aUpdatedMsgs) {
  1316.             // Update the message headers cache
  1317.             $aDeleted = array();
  1318.             foreach ($aUpdatedMsgs as $iUid => $aMsg) {
  1319.                 if (isset($aMsg['FLAGS'])) {
  1320.                     /**
  1321.                      * Only update the cached headers if the header is
  1322.                      * cached.
  1323.                      */
  1324.                     if (isset($aMailbox['MSG_HEADERS'][$iUid])) {
  1325.                         $aMailbox['MSG_HEADERS'][$iUid]['FLAGS'] = $aMsg['FLAGS'];
  1326.                     }
  1327.                     /**
  1328.                      * Count the messages with the \Delete flag set so we can determine
  1329.                      * if the number of expunged messages equals the number of flagged
  1330.                      * messages for deletion.
  1331.                      */
  1332.                     if (isset($aMsg['FLAGS']['\\deleted']) && $aMsg['FLAGS']['\\deleted']) {
  1333.                         $aDeleted[] = $iUid;
  1334.                     }
  1335.                 }
  1336.             }
  1337.             if ($bExpunge && $aMailbox['AUTO_EXPUNGE'] &&
  1338.                 $iExpungedMessages = sqimap_mailbox_expunge($imapConnection, $aMailbox['NAME'], true))
  1339.                 {
  1340.                 if (count($aDeleted) != $iExpungedMessages) {
  1341.                     // there are more messages deleted permanently then we expected
  1342.                     // invalidate the cache
  1343.                     $aMailbox['UIDSET'][$aMailbox['SETINDEX']] = false;
  1344.                     $aMailbox['MSG_HEADERS'] = false;
  1345.                 } else {
  1346.                     // remove expunged messages from cache
  1347.                     $aUidSet = $aMailbox['UIDSET'][$aMailbox['SETINDEX']];
  1348.                     if (is_array($aUidSet)) {
  1349.                         // create a UID => array index temp array
  1350.                         $aUidSetDummy = array_flip($aUidSet);
  1351.                         foreach ($aDeleted as $iUid) {
  1352.                             // get the id as well in case of SQM_SORT_NONE
  1353.                             if ($aMailbox['SORT'] == SQSORT_NONE) {
  1354.                                 $aMailbox['ID'] = false;
  1355.                                 //$iId = $aMailbox['MSG_HEADERS'][$iUid]['ID'];
  1356.                                 //unset($aMailbox['ID'][$iId]);
  1357.                             }
  1358.                             // unset the UID and message header
  1359.                             unset($aUidSetDummy[$iUid]);
  1360.                             unset($aMailbox['MSG_HEADERS'][$iUid]);
  1361.                         }
  1362.                         $aMailbox['UIDSET'][$aMailbox['SETINDEX']] = array_keys($aUidSetDummy);
  1363.                     }
  1364.                 }
  1365.                 // update EXISTS info
  1366.                 if ($iExpungedMessages) {
  1367.                     $aMailbox['EXISTS'] -= (int) $iExpungedMessages;
  1368.                     $aMailbox['TOTAL'][$aMailbox['SETINDEX']] -= (int) $iExpungedMessages;
  1369.                 }
  1370.                 if (($aMailbox['PAGEOFFSET']-1) >= $aMailbox['EXISTS']) {
  1371.                     $aMailbox['PAGEOFFSET'] = ($aMailbox['PAGEOFFSET'] > $aMailbox['LIMIT']) ?
  1372.                         $aMailbox['PAGEOFFSET'] - $aMailbox['LIMIT'] : 1;
  1373.                      $aMailbox['OFFSET'] = $aMailbox['PAGEOFFSET'] - 1 ;
  1374.                 }
  1375.             }
  1376.         }
  1377.     } else {
  1378.         if ($sButton == 'expunge') {
  1379.             /**
  1380.              * on expunge we do not know which messages will be deleted
  1381.              * so it's useless to try to sync the cache
  1382.              *
  1383.              * Close the mailbox so we do not need to parse the untagged expunge
  1384.              * responses which do not contain uid info.
  1385.              * NB: Closing a mailbox is faster then expunge because the imap
  1386.              * server does not need to generate the untagged expunge responses
  1387.              */
  1388.             sqimap_run_command($imapConnection,'CLOSE',false,$result,$message);
  1389.             $aMailbox = sqm_api_mailbox_select($imapConnection,$iAccount, $aMailbox['NAME'],array(),array());
  1390.         } else {
  1391.             if ($sButton) {
  1392.                 $sError = _("No messages were selected.");
  1393.             }
  1394.         }
  1395.     }
  1396.     return $sError;
  1397. }
  1398.  
  1399. /**
  1400.  * Attach messages to a compose session
  1401.  *
  1402.  * @param  resource $imapConnection imap connection
  1403.  * @param  array $aMsgHeaders 
  1404.  * @return int $composesession unique compose_session_id where the attached messages belong to
  1405.  * @author Marc Groot Koerkamp
  1406.  */
  1407. function attachSelectedMessages($imapConnection,$aMsgHeaders) {
  1408.     global $username, $attachment_dir,
  1409.            $data_dir;
  1410.  
  1411.  
  1412.     sqgetGlobalVar('composesession', $composesession, SQ_SESSION);
  1413.     sqgetGlobalVar('compose_messages', $compose_messages, SQ_SESSION);
  1414.     if (!isset($compose_messages)|| is_null($compose_messages)) {
  1415.         $compose_messages = array();
  1416.         sqsession_register($compose_messages,'compose_messages');
  1417.     }
  1418.  
  1419.     if (!$composesession) {
  1420.         $composesession = 1;
  1421.         sqsession_register($composesession,'composesession');
  1422.     } else {
  1423.         $composesession++;
  1424.         sqsession_register($composesession,'composesession');
  1425.     }
  1426.  
  1427.     $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
  1428.  
  1429.     $composeMessage = new Message();
  1430.     $rfc822_header = new Rfc822Header();
  1431.     $composeMessage->rfc822_header = $rfc822_header;
  1432.     $composeMessage->reply_rfc822_header = '';
  1433.  
  1434.     foreach($aMsgHeaders as $iUid => $aMsgHeader) {
  1435.         /**
  1436.          * Retrieve the full message
  1437.          */
  1438.         $body_a = sqimap_run_command($imapConnection, "FETCH $iUid RFC822", true, $response, $readmessage, TRUE);
  1439.         if ($response == 'OK') {
  1440.  
  1441.             $subject = (isset($aMsgHeader['subject'])) ? $aMsgHeader['subject'] : $iUid;
  1442.  
  1443.             array_shift($body_a);
  1444.             array_pop($body_a);
  1445.             $body = implode('', $body_a);
  1446.             $body .= "\r\n";
  1447.  
  1448.             $localfilename = GenerateRandomString(32, 'FILE', 7);
  1449.             $full_localfilename = "$hashed_attachment_dir/$localfilename";
  1450.  
  1451.             $fp = fopen( $full_localfilename, 'wb');
  1452.             fwrite ($fp, $body);
  1453.             fclose($fp);
  1454.             $composeMessage->initAttachment('message/rfc822',$subject.'.msg',
  1455.                  $full_localfilename);
  1456.         }
  1457.     }
  1458.  
  1459.     $compose_messages[$composesession] = $composeMessage;
  1460.     sqsession_register($compose_messages,'compose_messages');
  1461.     return $composesession;
  1462. }

Documentation generated on Sat, 07 Oct 2006 16:12:20 +0300 by phpDocumentor 1.3.0RC6