Source for file mailbox_display.php

Documentation is available at mailbox_display.php

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

Documentation generated on Sun, 23 Nov 2014 04:18:40 +0100 by phpDocumentor 1.4.3