6 * Copyright (c) 1999-2005 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
9 * This contains functions that display mailbox information, such as the
10 * table row that has sender, date, subject, etc...
13 * @package squirrelmail
16 /** The standard includes.. */
17 require_once(SM_PATH
. 'functions/strings.php');
18 require_once(SM_PATH
. 'functions/html.php');
19 require_once(SM_PATH
. 'functions/imap_mailbox.php');
20 require_once(SM_PATH
. 'functions/imap_messages.php');
21 require_once(SM_PATH
. 'functions/imap_asearch.php');
22 require_once(SM_PATH
. 'functions/mime.php');
23 require_once(SM_PATH
. 'functions/forms.php');
27 * Selects a mailbox for header retrieval.
28 * Cache control for message headers is embedded.
30 * @param resource $imapConnection imap socket handle
31 * @param string $mailbox mailbox to select and retrieve message headers from
32 * @param array $aConfig array with system config settings and incoming vars
33 * @param array $aProps mailbox specific properties
34 * @return array $aMailbox mailbox array with all relevant information
35 * @author Marc Groot Koerkamp
37 function sqm_api_mailbox_select($imapConnection,$account,$mailbox,$aConfig,$aProps) {
40 * NB: retrieve this from the session before accessing this function
41 * and make sure you write it back at the end of the script after
42 * the aMailbox var is added so that the headers are added to the cache
44 global $mailbox_cache;
46 $aDefaultConfigProps = array(
47 // 'charset' => 'US-ASCII',
48 'user' => false, /* no pref storage if false */
51 'max_cache_size' => SQM_MAX_MBX_IN_CACHE
54 $aConfig = array_merge($aDefaultConfigProps,$aConfig);
56 $iSetIndx = $aConfig['setindex'];
58 $aMbxResponse = sqimap_mailbox_select($imapConnection, $mailbox);
61 if (isset($mailbox_cache[$account.'_'.$mailbox])) {
62 $aCachedMailbox = $mailbox_cache[$account.'_'.$mailbox];
64 $aCachedMailbox = false;
67 if (count($mailbox_cache) > $aConfig['max_cache_size'] -1) {
69 foreach($mailbox_cache as $cachedmailbox => $aVal) {
70 $aTime[$aVal['TIMESTAMP']] = $cachedmailbox;
72 if (ksort($aTime,SORT_NUMERIC
)) {
73 for ($i=0,$iCnt=count($mailbox_cache);$i<($iCnt-$aConfig['max_cache_size']);++
$i) {
74 $sOldestMbx = array_shift($aTime);
76 * Remove only the UIDSET and MSG_HEADERS from cache because those can
77 * contain large amounts of data.
79 if (isset($mailbox_cache[$sOldestMbx]['UIDSET'])) {
80 $mailbox_cache[$sOldestMbx]['UIDSET']= false;
82 if (isset($mailbox_cache[$sOldestMbx]['MSG_HEADERS'])) {
83 $mailbox_cache[$sOldestMbx]['MSG_HEADERS'] = false;
90 $aCachedMailbox = false;
94 * Deal with imap servers that do not return the required UIDNEXT or
95 * UIDVALIDITY response
96 * from a SELECT call (since rfc 3501 it's required).
98 if (!isset($aMbxResponse['UIDNEXT']) ||
!isset($aMbxResponse['UIDVALIDITY'])) {
99 $aStatus = sqimap_status_messages($imapConnection,$mailbox,
100 array('UIDNEXT','UIDVALIDITY'));
101 $aMbxResponse['UIDNEXT'] = $aStatus['UIDNEXT'];
102 $aMbxResponse['UIDVALIDTY'] = $aStatus['UIDVALIDITY'];
105 $aMailbox['ACCOUNT'] = $account;
106 $aMailbox['UIDSET'][$iSetIndx] = false;
107 $aMailbox['ID'] = false;
108 $aMailbox['SETINDEX'] = $iSetIndx;
110 if ($aCachedMailbox) {
112 * Validate integrity of cached data
114 if ($aCachedMailbox['EXISTS'] == $aMbxResponse['EXISTS'] &&
115 $aMbxResponse['EXISTS'] &&
116 $aCachedMailbox['UIDVALIDITY'] == $aMbxResponse['UIDVALIDITY'] &&
117 $aCachedMailbox['UIDNEXT'] == $aMbxResponse['UIDNEXT'] &&
118 isset($aCachedMailbox['SEARCH'][$iSetIndx]) &&
119 (!isset($aConfig['search']) ||
/* always set search from the searchpage */
120 $aCachedMailbox['SEARCH'][$iSetIndx] == $aConfig['search'])) {
121 if (isset($aCachedMailbox['MSG_HEADERS'])) {
122 $aMailbox['MSG_HEADERS'] = $aCachedMailbox['MSG_HEADERS'];
124 $aMailbox['ID'] = $aCachedMailbox['ID'];
125 if (isset($aCachedMailbox['UIDSET'][$iSetIndx]) && $aCachedMailbox['UIDSET'][$iSetIndx]) {
126 if (isset($aProps[MBX_PREF_SORT
]) && $aProps[MBX_PREF_SORT
] != $aCachedMailbox['SORT'] ) {
127 $newsort = $aProps[MBX_PREF_SORT
];
128 $oldsort = $aCachedMailbox['SORT'];
130 * If it concerns a reverse sort we do not need to invalidate
131 * the cached sorted UIDSET, a reverse is sufficient.
133 if ((($newsort %
2) && ($newsort +
1 == $oldsort)) ||
134 (!($newsort %
2) && ($newsort - 1 == $oldsort))) {
135 $aMailbox['UIDSET'][$iSetIndx] = array_reverse($aCachedMailbox['UIDSET'][$iSetIndx]);
137 $server_sort_array = false;
138 $aMailbox['MSG_HEADERS'] = false;
139 $aMailbox['ID'] = false;
141 // store the new sort value in the mailbox pref
142 if ($aConfig['user']) {
143 // FIXME, in ideal situation, we write back the
144 // prefs at the end of the script
145 setUserPref($aConfig['user'],'pref_'.$account.'_'.$mailbox,serialize($aProps));
148 $aMailbox['UIDSET'][$iSetIndx] = $aCachedMailbox['UIDSET'][$iSetIndx];
154 * Restore the offset in the paginator if no new offset is provided.
156 if (isset($aMailbox['UIDSET'][$iSetIndx]) && !isset($aConfig['offset']) && $aCachedMailbox['OFFSET']) {
157 $aMailbox['OFFSET'] = $aCachedMailbox['OFFSET'];
158 $aMailbox['PAGEOFFSET'] = $aCachedMailbox['PAGEOFFSET'];
160 $aMailbox['OFFSET'] = (isset($aConfig['offset']) && $aConfig['offset']) ?
$aConfig['offset'] -1 : 0;
161 $aMailbox['PAGEOFFSET'] = (isset($aConfig['offset']) && $aConfig['offset']) ?
$aConfig['offset'] : 1;
164 * Restore the number of messages in the result set
166 if (isset($aCachedMailbox['TOTAL'][$iSetIndx]) && $aCachedMailbox['TOTAL'][$iSetIndx]) {
167 $aMailbox['TOTAL'][$iSetIndx] = $aCachedMailbox['TOTAL'][$iSetIndx];
171 * Restore the showall value no new showall value is provided.
173 if (isset($aMailbox['UIDSET'][$iSetIndx]) && !isset($aConfig['showall']) &&
174 isset($aCachedMailbox['SHOWALL'][$iSetIndx]) && $aCachedMailbox['SHOWALL'][$iSetIndx]) {
175 $aMailbox['SHOWALL'][$iSetIndx] = $aCachedMailbox['SHOWALL'][$iSetIndx];
177 $aMailbox['SHOWALL'][$iSetIndx] = (isset($aConfig['showall']) && $aConfig['showall']) ?
1 : 0;
181 * Restore the sort order if no new sort order is provided.
183 if (!isset($aProps[MBX_PREF_SORT
]) && isset($aCachedMailbox['SORT'])) {
184 $aMailbox['SORT'] = $aCachedMailbox['SORT'];
186 $aMailbox['SORT'] = (isset($aProps[MBX_PREF_SORT
])) ?
$aProps[MBX_PREF_SORT
] : 0;
190 * Restore the number of message to show per page when no new limit is provided
192 if (!isset($aProps[MBX_PREF_LIMIT
]) && isset($aCachedMailbox['LIMIT'])) {
193 $aMailbox['LIMIT'] = $aCachedMailbox['LIMIT'];
195 $aMailbox['LIMIT'] = (isset($aProps[MBX_PREF_LIMIT
])) ?
$aProps[MBX_PREF_LIMIT
] : 15;
199 * Restore the ordered columns to show when no new ordered columns are provided
201 if (!isset($aProps[MBX_PREF_COLUMNS
]) && isset($aCachedMailbox['COLUMNS'])) {
202 $aMailbox['COLUMNS'] = $aCachedMailbox['COLUMNS'];
204 $aMailbox['COLUMNS'] = (isset($aProps[MBX_PREF_COLUMNS
])) ?
$aProps[MBX_PREF_COLUMNS
] :
205 array(SQM_COL_FLAGS
,SQM_COL_FROM
, SQM_COL_SUBJ
, SQM_COL_FLAGS
);
208 if (!isset($aProps[MBX_PREF_AUTO_EXPUNGE
]) && isset($aCachedMailbox['AUTO_EXPUNGE'])) {
209 $aMailbox['AUTO_EXPUNGE'] = $aCachedMailbox['AUTO_EXPUNGE'];
211 $aMailbox['AUTO_EXPUNGE'] = (isset($aProps[MBX_PREF_AUTO_EXPUNGE
])) ?
$aProps[MBX_PREF_AUTO_EXPUNGE
] : false;
213 if (!isset($aConfig['search']) && isset($aCachedMailbox['SEARCH'][$iSetIndx])) {
214 $aMailbox['SEARCH'][$iSetIndx] = $aCachedMailbox['SEARCH'][$iSetIndx];
216 $aMailbox['SEARCH'][$iSetIndx] = (isset($aConfig['search'])) ?
$aConfig['search'] : 'ALL';
218 if (!isset($aConfig['charset']) && isset($aCachedMailbox['CHARSET'][$iSetIndx])) {
219 $aMailbox['CHARSET'][$iSetIndx] = $aCachedMailbox['CHARSET'][$iSetIndx];
221 $aMailbox['CHARSET'][$iSetIndx] = (isset($aConfig['charset'])) ?
$aConfig['charset'] : 'US-ASCII';
224 $aMailbox['NAME'] = $mailbox;
225 $aMailbox['EXISTS'] = $aMbxResponse['EXISTS'];
226 $aMailbox['SEEN'] = (isset($aMbxResponse['SEEN'])) ?
$aMbxResponse['SEEN'] : $aMbxResponse['EXISTS'];
227 $aMailbox['RECENT'] = (isset($aMbxResponse['RECENT'])) ?
$aMbxResponse['RECENT'] : 0;
228 $aMailbox['UIDVALIDITY'] = $aMbxResponse['UIDVALIDITY'];
229 $aMailbox['UIDNEXT'] = $aMbxResponse['UIDNEXT'];
230 $aMailbox['PERMANENTFLAGS'] = $aMbxResponse['PERMANENTFLAGS'];
231 $aMailbox['RIGHTS'] = $aMbxResponse['RIGHTS'];
233 /* decide if we are thread sorting or not */
234 if ($aMailbox['SORT'] & SQSORT_THREAD
) {
235 if (!sqimap_capability($imapConnection,'THREAD')) {
236 $aMailbox['SORT'] ^
= SQSORT_THREAD
;
238 $aMailbox['THREAD_INDENT'] = $aCachedMailbox['THREAD_INDENT'];
241 $aMailbox['THREAD_INDENT'] = false;
244 /* set a timestamp for cachecontrol */
245 $aMailbox['TIMESTAMP'] = time();
250 * Fetch the message headers for a mailbox. Settings are part of the aMailbox
253 * @param resource $imapConnection imap socket handle
254 * @param array $aMailbox (reference) mailbox retrieved from sqm_api_mailbox_select
255 * @return error $error error number
256 * @author Marc Groot Koerkamp
258 function fetchMessageHeaders($imapConnection, &$aMailbox, $aFetchHeaders) {
260 /* FIX ME, this function is kind of big, maybe we can split it up in
261 a couple of functions. Make sure the functions are private and starts with _
262 Also make sure that the error codes are propagated */
265 * Retrieve the UIDSET.
266 * Setindex is used to be able to store multiple uid sets. That will make it
267 * possible to display the mailbox multiple times in different sort order
268 * or to store serach results separate from normal mailbox view.
270 $iSetIndx = (isset($aMailbox['SETINDEX'])) ?
$aMailbox['SETINDEX'] : 0;
272 $iLimit = ($aMailbox['SHOWALL'][$iSetIndx]) ?
$aMailbox['EXISTS'] : $aMailbox['LIMIT'];
274 * Adjust the start_msg
276 $start_msg = $aMailbox['PAGEOFFSET'];
277 if($aMailbox['PAGEOFFSET'] > $aMailbox['EXISTS']) {
278 $start_msg -= $aMailbox['LIMIT'];
283 //sm_print_r($aMailbox);
284 if (is_array($aMailbox['UIDSET'])) {
285 $aUid =& $aMailbox['UIDSET'][$iSetIndx];
291 $aFetchItems = $aHeaderItems = array();
292 // initialize the fields we want to retrieve:
293 foreach ($aFetchHeaders as $v) {
295 case SQM_COL_DATE
: $aHeaderFields[] = 'Date'; break;
296 case SQM_COL_TO
: $aHeaderFields[] = 'To'; break;
297 case SQM_COL_CC
: $aHeaderFields[] = 'Cc'; break;
298 case SQM_COL_FROM
: $aHeaderFields[] = 'From'; break;
299 case SQM_COL_SUBJ
: $aHeaderFields[] = 'Subject'; break;
300 case SQM_COL_PRIO
: $aHeaderFields[] = 'X-Priority'; break;
301 case SQM_COL_ATTACHMENT
: $aHeaderFields[] = 'Content-Type'; break;
302 case SQM_COL_INT_DATE
: $aFetchItems[] = 'INTERNALDATE'; break;
303 case SQM_COL_FLAGS
: $aFetchItems[] = 'FLAGS'; break;
304 case SQM_COL_SIZE
: $aFetchItems[] = 'RFC822.SIZE'; break;
310 * A uidset with sorted uid's is available. We can use the cache
312 if (isset($aUid) && $aUid ) {
314 // limit the cache to SQM_MAX_PAGES_IN_CACHE
315 if (!$aMailbox['SHOWALL'][$iSetIndx] && isset($aMailbox['MSG_HEADERS'])) {
316 $iMaxMsgs = $iLimit * SQM_MAX_PAGES_IN_CACHE
;
317 $iCacheSize = count($aMailbox['MSG_HEADERS']);
318 if ($iCacheSize > $iMaxMsgs) {
319 $iReduce = $iCacheSize - $iMaxMsgs;
320 foreach ($aMailbox['MSG_HEADERS'] as $iUid => $value) {
322 unset($aMailbox['MSG_HEADERS'][$iUid]);
331 $id_slice = array_slice($aUid,$start_msg-1,$iLimit);
332 /* do some funky cache checks */
333 if (isset($aMailbox['MSG_HEADERS'])) {
334 $aUidCached = array_keys($aMailbox['MSG_HEADERS']);
336 $aMailbox['MSG_HEADERS'] = array();
337 $aUidCached = array();
339 $aUidNotCached = array_values(array_diff($id_slice,$aUidCached));
342 * $aUidNotCached contains an array with UID's which need to be fetched to
343 * complete the needed message headers.
345 if (count($aUidNotCached)) {
346 $aMsgs = sqimap_get_small_header_list($imapConnection,$aUidNotCached,
347 $aHeaderFields,$aFetchItems);
348 // append the msgs to the existend headers
349 $aMailbox['MSG_HEADERS'] +
= $aMsgs;
353 * Initialize the sorted UID list or initiate a UID list with search
354 * results and fetch the visible message headers
357 if ($aMailbox['SEARCH'][$iSetIndx] != 'ALL') { // in case of a search request
359 if ($aMailbox['SEARCH'][$iSetIndx] && $aMailbox['SORT'] == 0) {
360 $aUid = sqimap_run_search($imapConnection, $aMailbox['SEARCH'][$iSetIndx], $aMailbox['CHARSET'][$iSetIndx]);
364 $iError = _get_sorted_msgs_list($imapConnection,$aMailbox,$iError);
365 $aUid = $aMailbox['UIDSET'][$iSetIndx];
369 * Number of messages is the resultset
371 $aMailbox['TOTAL'][$iSetIndx] = count($aUid);
372 $id_slice = array_slice($aUid,$aMailbox['OFFSET'], $iLimit);
373 if (count($id_slice)) {
374 $aMailbox['MSG_HEADERS'] = sqimap_get_small_header_list($imapConnection,$id_slice,
375 $aHeaderFields,$aFetchItems);
377 $iError = 1; // FIX ME, define an error code
382 $iError = _get_sorted_msgs_list($imapConnection,$aMailbox,$iError);
383 $aUid = $aMailbox['UIDSET'][$iSetIndx];
387 * Number of messages is the resultset
389 $aMailbox['TOTAL'][$iSetIndx] = count($aUid);
390 $id_slice = array_slice($aUid,$aMailbox['OFFSET'], $iLimit);
391 if (count($id_slice)) {
392 $aMailbox['MSG_HEADERS'] = sqimap_get_small_header_list($imapConnection,$id_slice,
393 $aHeaderFields,$aFetchItems);
395 $iError = 1; // FIX ME, define an error code
403 function prepareMessageList(&$aMailbox, $aProps) {
404 /* retrieve the properties */
405 $my_email_address = (isset($aProps['email'])) ?
$aProps['email'] : false;
406 $highlight_list = (isset($aProps['config']['highlight_list'])) ?
$aProps['config']['highlight_list'] : false;
407 $aColumnDesc = (isset($aProps['columns'])) ?
$aProps['columns'] : false;
408 $aExtraColumns = (isset($aProps['extra_columns'])) ?
$aProps['extra_columns'] : array();
409 $iAccount = (isset($aProps['account'])) ?
(int) $aProps['account'] : 0;
410 $sMailbox = (isset($aProps['mailbox'])) ?
$aProps['mailbox'] : false;
411 $sTargetModule = (isset($aProps['module'])) ?
$aProps['module'] : 'read_body';
414 * TODO 1, retrieve array with identity email addresses in order to match against to,cc and set a flag
415 * $aFormattedMessages[$iUid]['match_identity'] = true
416 * The template can show some image if there is a match.
417 * TODO 2, makes sure the matching is done fast by doing a strpos call on the returned $value
421 * Only retrieve values for displayable columns
423 foreach ($aColumnDesc as $k => $v) {
425 case SQM_COL_FROM
: $aCol[SQM_COL_FROM
] = 'from'; break;
426 case SQM_COL_DATE
: $aCol[SQM_COL_DATE
] = 'date'; break;
427 case SQM_COL_SUBJ
: $aCol[SQM_COL_SUBJ
] = 'subject'; break;
428 case SQM_COL_FLAGS
: $aCol[SQM_COL_FLAGS
] = 'FLAGS'; break;
429 case SQM_COL_SIZE
: $aCol[SQM_COL_SIZE
] = 'SIZE'; break;
430 case SQM_COL_PRIO
: $aCol[SQM_COL_PRIO
] = 'x-priority'; break;
431 case SQM_COL_ATTACHMENT
: $aCol[SQM_COL_ATTACHMENT
] = 'content-type'; break;
432 case SQM_COL_INT_DATE
: $aCol[SQM_COL_INT_DATE
] = 'INTERNALDATE'; break;
433 case SQM_COL_TO
: $aCol[SQM_COL_TO
] = 'to'; break;
434 case SQM_COL_CC
: $aCol[SQM_COL_CC
] = 'cc'; break;
435 case SQM_COL_BCC
: $aCol[SQM_COL_BCC
] = 'bcc'; break;
439 $aFormattedMessages = array();
442 $iSetIndx = $aMailbox['SETINDEX'];
443 $aId = $aMailbox['UIDSET'][$iSetIndx];
444 $aHeaders =& $aMailbox['MSG_HEADERS']; /* use a reference to avoid a copy.
445 MSG_HEADERS can contain large amounts of data */
446 $iOffset = $aMailbox['OFFSET'];
447 $sort = $aMailbox['SORT'];
448 $iPageOffset = $aMailbox['PAGEOFFSET'];
449 $sMailbox = $aMailbox['NAME'];
450 $sSearch = (isset($aMailbox['SEARCH'][$aMailbox['SETINDEX']]) &&
451 $aMailbox['SEARCH'][$aMailbox['SETINDEX']] != 'ALL') ?
$aMailbox['SEARCH'][$aMailbox['SETINDEX']] : false;
452 $aSearch = ($sSearch) ?
array('search.php',$aMailbox['SETINDEX']) : null;
453 /* avoid improper usage */
454 if ($sMailbox && isset($iAccount) && $sTargetModule) {
455 $aInitQuery = array("account=$iAccount",'mailbox='.urlencode($sMailbox));
460 if ($aMailbox['SORT'] & SQSORT_THREAD
) {
461 $aIndentArray =& $aMailbox['THREAD_INDENT'][$aMailbox['SETINDEX']];
467 * Retrieve value for checkbox column
469 if (!sqgetGlobalVar('checkall',$checkall,SQ_GET
)) {
474 * Loop through and display the info for each message.
476 $iEnd = ($aMailbox['SHOWALL'][$iSetIndx]) ?
$aMailbox['EXISTS'] : $iOffset +
$aMailbox['LIMIT'];
477 for ($i=$iOffset,$t=0;$i<$iEnd;++
$i) {
478 if (isset($aId[$i])) {
481 $value = $title = $link = $target = '';
482 $aQuery = ($aInitQuery !== false) ?
$aInitQuery : false;
483 $aMsg = $aHeaders[$aId[$i]];
484 if (isset($aSearch) && count($aSearch) > 1 && $aQuery) {
485 $aQuery[] = "where=". $aSearch[0];
486 $aQuery[] = "what=" . $aSearch[1];
488 $iUid = (isset($aMsg['UID'])) ?
$aMsg['UID'] : $aId[$i];
490 $aQuery[] = "passed_id=$aId[$i]";
491 $aQuery[] = "startMessage=$iPageOffset";
494 foreach ($aCol as $k => $v) {
495 $link = $target = $title = '';
496 $aColumns[$k] = array();
497 $value = (isset($aMsg[$v])) ?
$aMsg[$v] : '';
498 $sUnknown = _("Unknown recipient");
501 $sUnknown = _("Unknown sender");
507 if ($highlight_list && !$bHighLight) {
508 $bHighLight = highlightMessage($aCol[$k], $value, $highlight_list,$aFormattedMessages[$iUid]);
510 $sTmp = getAddressString(parseRFC822Address($value),array('best' => true));
511 if (isset($aColumnDesc[$k]['truncate']) && $aColumnDesc[$k]['truncate']) {
512 $sTrunc = truncateWithEntities($sTmp, $aColumnDesc[$k]['truncate']);
513 $title = ($sTrunc != $sTmp) ?
htmlspecialchars($sTmp) : '';
517 $value = ($sTmp) ?
htmlspecialchars($sTmp) : $sUnknown;
520 if ($highlight_list && !$bHighLight) {
521 $bHighLight = highlightMessage('SUBJECT', $value, $highlight_list, $aFormattedMessages[$iUid]);
523 $iIndent = (isset($aIndentArray[$aId[$i]])) ?
$aIndentArray[$aId[$i]] : 0;
524 if (isset($aColumnDesc[$k]['truncate']) && $aColumnDesc[$k]['truncate']) {
525 $sTmp = truncateWithEntities($value, $aColumnDesc[$k]['truncate']-$iIndent);
526 $title = ($sTmp != $value) ?
htmlspecialchars($value) : '';
529 /* generate the link to the message */
531 // TODO, $sTargetModule should be a query parameter so that we can use a single entrypoint
532 $link = $sTargetModule.'.php?' . implode('&',$aQuery);
535 $value = (trim($value)) ?
$value : _("(no subject)");
536 /* add thread indentation */
537 $aColumns[$k]['indent'] = $iIndent;
538 $value = htmlspecialchars($value);
541 $value = show_readable_size($value);
544 case SQM_COL_INT_DATE
:
545 $value = getDateString(getTimeStamp(explode(' ',trim($value))));
548 $aFlagColumn = array('seen' => false,
553 foreach ($value as $sFlag => $value) {
555 case '\\seen' : $aFlagColumn['seen'] = true; break;
556 case '\\deleted' : $aFlagColumn['deleted'] = true; break;
557 case '\\answered': $aFlagColumn['answered'] = true; break;
558 case '\\flagged' : $aFlagColumn['flagged'] = true; break;
559 case '\\draft' : $aFlagColumn['draft'] = true; break;
563 $value = $aFlagColumn;
566 $value = ($value) ?
(int) $value : 3;
568 case SQM_COL_ATTACHMENT
:
569 $value = (is_array($value) && $value[0] == 'multipart' && $value[1] == 'mixed') ?
true : false;
576 if ($title) { $aColumns[$k]['title'] = $title; }
577 if ($link) { $aColumns[$k]['link'] = $link; }
578 if ($target) { $aColumns[$k]['target'] = $target; }
579 $aColumns[$k]['value'] = $value;
581 /* columns which will not be displayed but should be inspected
582 because the highlight list contains rules with those columns */
583 foreach ($aExtraColumns as $k) {
584 if ($highlight_list && !$bHighLight) {
585 $bHighLight = highlightMessage($k, $value, $highlight_list,$aFormattedMessages[$iUid]);
588 $aFormattedMessages[$iUid]['columns'] = $aColumns;
594 return $aFormattedMessages;
599 function highlightMessage($sCol, $sVal, $highlight_list, &$aFormat) {
602 if (!is_array($highlight_list) && count($highlight_list) == 0) {
606 $sCol = strtoupper($sCol);
607 //echo "$sCol $sVal<br>";
608 foreach ($highlight_list as $highlight_list_part) {
609 if (trim($highlight_list_part['value'])) {
610 $high_val = strtolower($highlight_list_part['value']);
611 $match_type = strtoupper($highlight_list_part['match_type']);
612 if($match_type == 'TO_CC') {
613 if ($sCol == 'TO' ||
$sCol == 'CC') {
619 if ($match_type != $sCol) {
623 if (strpos(strtolower($sVal),$high_val) !== false) {
624 $hlt_color = $highlight_list_part['color'];
630 $aFormat['row']['color'] = $hlt_color;
637 function setUserPref($username, $pref, $value) {
639 setPref($data_dir,$username,$pref,$value);
643 * Execute the sorting for a mailbox
645 * @param resource $imapConnection Imap connection
646 * @param array $aMailbox (reference) Mailbox retrieved with sqm_api_mailbox_select
647 * @return int $error (reference) Error number
649 * @author Marc Groot Koerkamp
651 function _get_sorted_msgs_list($imapConnection,&$aMailbox) {
652 $iSetIndx = (isset($aMailbox['SETINDEX'])) ?
$aMailbox['SETINDEX'] : 0;
653 $bDirection = ($aMailbox['SORT'] %
2);
655 if (!$aMailbox['SEARCH'][$iSetIndx]) {
656 $aMailbox['SEARCH'][$iSetIndx] = 'ALL';
658 if (($aMailbox['SORT'] & SQSORT_THREAD
) && sqimap_capability($imapConnection,'THREAD')) {
659 $aRes = get_thread_sort($imapConnection,$aMailbox['SEARCH'][$iSetIndx]);
660 if ($aRes === false) {
661 $aMailbox['SORT'] -= SQSORT_THREAD
;
662 $error = 1; // fix me, define an error code;
664 $aMailbox['UIDSET'][$iSetIndx] = $aRes[0];
665 $aMailbox['THREAD_INDENT'][$iSetIndx] = $aRes[1];
667 } else if ($aMailbox['SORT'] === SQSORT_NONE
) {
668 $id = sqimap_run_search($imapConnection, 'ALL' , '');
670 $error = 1; // fix me, define an error code
672 $aMailbox['UIDSET'][$iSetIndx] = array_reverse($id);
673 $aMailbox['TOTAL'][$iSetIndx] = $aMailbox['EXISTS'];
676 if (sqimap_capability($imapConnection,'SORT')) {
677 $sSortField = _getSortField($aMailbox['SORT'],true);
678 $id = sqimap_get_sort_order($imapConnection, $sSortField, $bDirection, $aMailbox['SEARCH'][$iSetIndx]);
680 $error = 1; // fix me, define an error code
682 $aMailbox['UIDSET'][$iSetIndx] = $id;
686 if ($aMailbox['SEARCH'][$iSetIndx] != 'ALL') {
687 $id = sqimap_run_search($imapConnection, $aMailbox['SEARCH'][$iSetIndx], $aMailbox['CHARSET'][$iSetIndx]);
689 $sSortField = _getSortField($aMailbox['SORT'],false);
690 $aMailbox['UIDSET'][$iSetIndx] = get_squirrel_sort($imapConnection, $sSortField, $bDirection, $id);
697 * Does the $srt $_GET var to field mapping
699 * @param int $srt Field to sort on
700 * @param bool $bServerSort Server sorting is true
701 * @return string $sSortField Field to sort on
704 function _getSortField($sort,$bServerSort) {
709 case SQSORT_DATE_ASC
:
710 case SQSORT_DATE_DESC
:
711 $sSortField = 'DATE';
713 case SQSORT_FROM_ASC
:
714 case SQSORT_FROM_DESC
:
715 $sSortField = 'FROM';
717 case SQSORT_SUBJ_ASC
:
718 case SQSORT_SUBJ_DESC
:
719 $sSortField = 'SUBJECT';
721 case SQSORT_SIZE_ASC
:
722 case SQSORT_SIZE_DESC
:
723 $sSortField = ($bServerSort) ?
'SIZE' : 'RFC822.SIZE';
733 case SQSORT_INT_DATE_ASC
:
734 case SQSORT_INT_DATE_DESC
:
735 $sSortField = ($bServerSort) ?
'ARRIVAL' : 'INTERNALDATE';
739 default: $sSortField = 'UID';
750 * This function loops through a group of messages in the mailbox
751 * and shows them to the user.
753 * @param resource $imapConnection
754 * @param array $aMailbox associative array with mailbox related vars
755 * @param array $aProps
756 * @param int $iError error code, 0 is no error
758 function showMessagesForMailbox($imapConnection, &$aMailbox,$aProps, &$iError) {
762 $highlight_list = (isset($aProps['config']['highlight_list'])) ?
$aProps['config']['highlight_list'] : false;
763 $fancy_index_highlite = (isset($aProps['config']['fancy_index_highlite'])) ?
$aProps['config']['fancy_index_highlite'] : true;
764 $aColumnsDesc = (isset($aProps['columns'])) ?
$aProps['columns'] : false;
765 $iAccount = (isset($aProps['account'])) ?
(int) $aProps['account'] : 0;
766 $sMailbox = (isset($aProps['mailbox'])) ?
$aProps['mailbox'] : false;
767 $sTargetModule = (isset($aProps['module'])) ?
$aProps['module'] : 'read_body';
768 $show_flag_buttons = (isset($aProps['config']['show_flag_buttons'])) ?
$aProps['config']['show_flag_buttons'] : true;
769 $lastTargetMailbox = (isset($aProps['config']['lastTargetMailbox'])) ?
$aProps['config']['lastTargetMailbox'] : '';
770 $aOrder = array_keys($aProps['columns']);
771 $trash_folder = (isset($aProps['config']['trash_folder']) && $aProps['config']['trash_folder'])
772 ?
$aProps['config']['trash_folder'] : false;
773 $sent_folder = (isset($aProps['config']['sent_folder']) && $aProps['config']['sent_folder'])
774 ?
$aProps['config']['sent_folder'] : false;
775 $draft_folder = (isset($aProps['config']['draft_folder']) && $aProps['config']['draft_folder'])
776 ?
$aProps['config']['draft_folder'] : false;
777 $page_selector = (isset($aProps['config']['page_selector'])) ?
$aProps['config']['page_selector'] : false;
778 $page_selector_max = (isset($aProps['config']['page_selector_max'])) ?
$aProps['config']['page_selector_max'] : 10;
779 $color = $aProps['config']['color'];
787 if (!isset($iFormId)) {
793 * Remove the checkbox column because we cannot fetch it from the imap server
795 $aFetchColumns = $aColumnsDesc;
796 if (isset($aFetchColumns[SQM_COL_CHECK
])) {
797 unset($aFetchColumns[SQM_COL_CHECK
]);
801 * Before we fetch the message headers, check if we need to fetch extra columns
802 * to make the message highlighting work
804 if (is_array($highlight_list) && count($highlight_list)) {
805 $aHighlightColumns = array();
806 foreach ($highlight_list as $highlight_list_part) {
807 if (trim($highlight_list_part['value'])) {
808 $match_type = strtoupper($highlight_list_part['match_type']);
809 $aHighlightColumns = array();
810 switch ($match_type) {
812 $aHighlightColumns[SQM_COL_TO
] = true;
813 $aHighlightColumns[SQM_COL_CC
] = true;
815 case 'TO': $aHighlightColumns[SQM_COL_TO
] = true; break;
816 case 'CC': $aHighlightColumns[SQM_COL_CC
] = true; break;
817 case 'FROM': $aHighlightColumns[SQM_COL_FROM
] = true; break;
818 case 'SUBJECT':$aHighlightColumns[SQM_COL_SUBJ
] = true; break;
822 $aExtraColumns = array();
823 foreach ($aHighlightColumns as $k => $v) {
824 if (!isset($aFetchColumns[$k])) {
825 $aExtraColumns[] = $k;
826 $aFetchColumns[$k] = true;
829 if (count($aExtraColumns)) {
830 $aProps['extra_columns'] = $aExtraColumns;
833 $aFetchColumns = array_keys($aFetchColumns);
834 // store the columns to fetch to the session so we can pick them up in read_body
835 // where we validate the cache.
836 /////// sqsession_register($aFetchColumns,'aFetchColumns');
838 $iError = fetchMessageHeaders($imapConnection, $aMailbox, $aFetchColumns);
842 $aMessages = prepareMessageList($aMailbox, $aProps);
845 $iSetIndx = $aMailbox['SETINDEX'];
846 $iLimit = ($aMailbox['SHOWALL'][$iSetIndx]) ?
$aMailbox['EXISTS'] : $aMailbox['LIMIT'];
847 $iEnd = ($aMailbox['PAGEOFFSET'] +
($iLimit - 1) < $aMailbox['EXISTS']) ?
848 $aMailbox['PAGEOFFSET'] +
$iLimit - 1 : $aMailbox['EXISTS'];
850 $iNumberOfMessages = $aMailbox['TOTAL'][$iSetIndx];
852 $php_self = $PHP_SELF;
854 $urlMailbox = urlencode($aMailbox['NAME']);
856 if (preg_match('/^(.+)\?.+$/',$php_self,$regs)) {
857 $source_url = $regs[1];
859 $source_url = $php_self;
862 $baseurl = $source_url.'?mailbox=' . urlencode($aMailbox['NAME']) .'&account='.$aMailbox['ACCOUNT'];
863 $where = urlencode($aMailbox['SEARCH'][$iSetIndx][0]);
864 $what = urlencode($aMailbox['SEARCH'][$iSetIndx][1]);
865 $baseurl .= '&where=' . $where . '&what=' . $what;
867 /* build thread sorting links */
868 $newsort = $aMailbox['SORT'];
869 if (sqimap_capability($imapConnection,'THREAD')) {
870 if ($aMailbox['SORT'] & SQSORT_THREAD
) {
871 $newsort -= SQSORT_THREAD
;
872 $thread_name = _("Unthread View");
874 $thread_name = _("Thread View");
875 $newsort = $aMailbox['SORT'] + SQSORT_THREAD
;
877 $thread_link_str = '<small>[<a href="' . $baseurl . '&srt='
878 . $newsort . '&startMessage=1">' . $thread_name
881 $thread_link_str ='';
883 $sort = $aMailbox['SORT'];
885 /* FIX ME ADD CHECKBOX CONTROL. No checkbox => no buttons */
889 /* future admin control over displayable buttons */
891 $aAdminControl = array(
892 'markUnflagged' => 1,
897 'undeleteButton'=> 1,
899 'expungeButton' => 1,
903 /* user prefs control */
904 $aUserControl = array (
905 'markUnflagged' => $show_flag_buttons,
906 'markFlagged' => $show_flag_buttons,
910 'undeleteButton'=> 1,
912 'expungeButton' => 1,
917 $showDelete = ($aMailbox['RIGHTS'] != 'READ-ONLY' &&
918 in_array('\\deleted',$aMailbox['PERMANENTFLAGS'], true)) ?
true : false;
919 $showByPassTrash = (!$aMailbox['AUTO_EXPUNGE'] && $aMailbox['RIGHTS'] != 'READ-ONLY' &&
920 in_array('\\deleted',$aMailbox['PERMANENTFLAGS'], true)) &&
921 $trash_folder ?
true : false; //
922 $showUndelete = (!$aMailbox['AUTO_EXPUNGE'] && $aMailbox['RIGHTS'] != 'READ-ONLY' &&
923 in_array('\\deleted',$aMailbox['PERMANENTFLAGS'], true) && !$trash_folder) ?
true : false;
924 $showMove = ($aMailbox['RIGHTS'] != 'READ-ONLY') ?
true : false;
925 $showExpunge = (!$aMailbox['AUTO_EXPUNGE'] && $aMailbox['RIGHTS'] != 'READ-ONLY' &&
926 in_array('\\deleted',$aMailbox['PERMANENTFLAGS'], true)) ?
true : false;
927 $aImapControl = array (
928 'markUnflagged' => in_array('\\flagged',$aMailbox['PERMANENTFLAGS'], true),
929 'markFlagged' => in_array('\\flagged',$aMailbox['PERMANENTFLAGS'], true),
930 'markRead' => in_array('\\seen',$aMailbox['PERMANENTFLAGS'], true),
931 'markUnread' => in_array('\\seen',$aMailbox['PERMANENTFLAGS'], true),
932 'delete' => $showDelete,
933 'undeleteButton'=> $showUndelete,
934 'bypass_trash' => $showByPassTrash,
935 'expungeButton' => $showExpunge,
936 'moveButton' => $showMove,
939 $aButtonStrings = array(
940 'markUnflagged' => _("Unflag"),
941 'markFlagged' => _("Flag"),
942 'markRead' => _("Read"),
943 'markUnread' => _("Unread"),
944 'delete' => _("Delete"),
945 'undeleteButton' => _("Undelete"),
946 'bypass_trash' => _("Bypass Trash"),
947 'expungeButton' => _("Expunge"),
948 'moveButton' => _("Move"),
949 'forward' => _("Forward")
952 * Register buttons in order to an array
953 * The key is the "name", the first element of the value array is the "value", second argument is the type.
955 $aFormElements = array();
956 foreach($aAdminControl as $k => $v) {
957 if ($v & $aUserControl[$k] & $aImapControl[$k]) {
959 case 'markUnflagged':
964 case 'undeleteButton':
965 case 'expungeButton':
967 $aFormElements[$k] = array($aButtonStrings[$k],'submit');
970 $aFormElements[$k] = array($aButtonStrings[$k],'checkbox');
973 $aFormElements['targetMailbox'] =
974 array(sqimap_mailbox_option_list($imapConnection, array(strtolower($lastTargetMailbox)), 0, $boxes),'select');
975 $aFormElements['mailbox'] = array($aMailbox['NAME'],'hidden');
976 $aFormElements['startMessage'] = array($aMailbox['PAGEOFFSET'],'hidden');
977 $aFormElements[$k] = array($aButtonStrings[$k],'submit');
981 $aFormElements['account'] = array($iAccount,'hidden');
985 * This is the beginning of the message list table.
986 * It wraps around all messages
988 $safe_name = preg_replace("/[^0-9A-Za-z_]/", '_', $aMailbox['NAME']);
989 $form_name = "FormMsgs" . $safe_name;
991 //if (!sqgetGlobalVar('align',$align,SQ_SESSION)) {
992 $align = array('left' => 'left', 'right' => 'right');
994 //sm_print_r($align);
996 /* finally set the template vars */
998 // FIX ME, before we support multiple templates we must review the names of the vars
1002 $aTemplate['color'] = $color;
1003 $aTemplate['form_name'] = "FormMsgs" . $safe_name;
1004 $aTemplate['form_id'] = 'mbx_'.$iFormId;
1005 $aTemplate['page_selector'] = $page_selector;
1006 $aTemplate['page_selector_max'] = $page_selector_max;
1007 $aTemplate['messagesPerPage'] = $aMailbox['LIMIT'];
1008 $aTemplate['showall'] = $aMailbox['SHOWALL'][$iSetIndx];
1009 $aTemplate['end_msg'] = $iEnd;
1010 $aTemplate['align'] = $align;
1011 $aTemplate['iNumberOfMessages'] = $iNumberOfMessages;
1012 $aTemplate['aOrder'] = $aOrder;
1013 $aTemplate['aFormElements'] = $aFormElements;
1014 $aTemplate['sort'] = $sort;
1015 $aTemplate['pageOffset'] = $aMailbox['PAGEOFFSET'];
1016 $aTemplate['baseurl'] = $baseurl;
1017 $aTemplate['aMessages'] =& $aMessages;
1018 $aTemplate['trash_folder'] = $trash_folder;
1019 $aTemplate['sent_folder'] = $sent_folder;
1020 $aTemplate['draft_folder'] = $draft_folder;
1021 $aTemplate['thread_link_str'] = $thread_link_str;
1022 $aTemplate['php_self'] = str_replace('&','&',$php_self);
1023 $aTemplate['mailbox'] = $sMailbox;
1024 $aTemplate['javascript_on'] = (isset($aProps['config']['javascript_on'])) ?
$aProps['config']['javascript_on'] : false;
1025 $aTemplate['enablesort'] = (isset($aProps['config']['enablesort'])) ?
$aProps['config']['enablesort'] : false;
1026 $aTemplate['icon_theme'] = (isset($aProps['config']['icon_theme'])) ?
$aProps['config']['icon_theme'] : false;
1027 $aTemplate['use_icons'] = (isset($aProps['config']['use_icons'])) ?
$aProps['config']['use_icons'] : false;
1028 $aTemplate['alt_index_colors'] = (isset($aProps['config']['alt_index_colors'])) ?
$aProps['config']['alt_index_colors'] : false;
1029 $aTemplate['fancy_index_highlite'] = $fancy_index_highlite;
1036 * FIXME: Undocumented function
1038 function truncateWithEntities($subject, $trim_at)
1040 $ent_strlen = strlen($subject);
1041 if (($trim_at <= 0) ||
($ent_strlen <= $trim_at))
1044 global $languages, $squirrelmail_language;
1047 * see if this is entities-encoded string
1048 * If so, Iterate through the whole string, find out
1049 * the real number of characters, and if more
1050 * than $trim_at, substr with an updated trim value.
1052 $trim_val = $trim_at;
1055 while ( $ent_loc < $trim_val && (($ent_loc = strpos($subject, '&', $ent_offset)) !== false) &&
1056 (($ent_loc_end = strpos($subject, ';', $ent_loc+
3)) !== false) ) {
1057 $trim_val +
= ($ent_loc_end-$ent_loc);
1058 $ent_offset = $ent_loc_end+
1;
1060 if (($trim_val > $trim_at) && ($ent_strlen > $trim_val) && (strpos($subject,';',$trim_val) < ($trim_val +
6))) {
1061 $i = strpos($subject,';',$trim_val);
1063 $trim_val = strpos($subject,';',$trim_val);
1066 // only print '...' when we're actually dropping part of the subject
1067 if ($ent_strlen <= $trim_val)
1070 if (isset($languages[$squirrelmail_language]['XTRA_CODE']) &&
1071 function_exists($languages[$squirrelmail_language]['XTRA_CODE'] . '_strimwidth')) {
1072 return call_user_func($languages[$squirrelmail_language]['XTRA_CODE'] . '_strimwidth', $subject, $trim_val);
1075 return substr_replace($subject, '...', $trim_val);
1079 * This should go in imap_mailbox.php
1080 * @param string $mailbox
1082 function handleAsSent($mailbox) {
1083 global $handleAsSent_result;
1085 /* First check if this is the sent or draft folder. */
1086 $handleAsSent_result = isSentMailbox($mailbox) ||
isDraftMailbox($mailbox);
1088 /* Then check the result of the handleAsSent hook. */
1089 do_hook('check_handleAsSent_result', $mailbox);
1091 /* And return the result. */
1092 return $handleAsSent_result;
1096 * Process messages list form and handle the cache gracefully. If $sButton and
1097 * $aUid are provided as argument then you can fake a message list submit and
1098 * use it i.e. in read_body.php for del move next and update the cache
1100 * @param resource $imapConnection imap connection
1101 * @param array $aMailbox (reference) cached mailbox
1102 * @param string $sButton fake a submit button
1103 * @param array $aUid fake the $msg array
1104 * @return string $sError error string in case of an error
1105 * @author Marc Groot Koerkamp
1107 function handleMessageListForm($imapConnection,&$aMailbox,$sButton='',$aUid = array()) {
1108 /* incoming formdata */
1109 $sButton = (sqgetGlobalVar('moveButton', $sTmp, SQ_POST
)) ?
'move' : $sButton;
1110 $sButton = (sqgetGlobalVar('expungeButton', $sTmp, SQ_POST
)) ?
'expunge' : $sButton;
1111 $sButton = (sqgetGlobalVar('forward', $sTmp, SQ_POST
)) ?
'forward' : $sButton;
1112 $sButton = (sqgetGlobalVar('delete', $sTmp, SQ_POST
)) ?
'setDeleted' : $sButton;
1113 $sButton = (sqgetGlobalVar('undeleteButton', $sTmp, SQ_POST
)) ?
'unsetDeleted' : $sButton;
1114 $sButton = (sqgetGlobalVar('markRead', $sTmp, SQ_POST
)) ?
'setSeen' : $sButton;
1115 $sButton = (sqgetGlobalVar('markUnread', $sTmp, SQ_POST
)) ?
'unsetSeen' : $sButton;
1116 $sButton = (sqgetGlobalVar('markFlagged', $sTmp, SQ_POST
)) ?
'setFlagged' : $sButton;
1117 $sButton = (sqgetGlobalVar('markUnflagged', $sTmp, SQ_POST
)) ?
'unsetFlagged' : $sButton;
1118 sqgetGlobalVar('targetMailbox', $targetMailbox, SQ_POST
);
1119 sqgetGlobalVar('bypass_trash', $bypass_trash, SQ_POST
);
1120 sqgetGlobalVar('msg', $msg, SQ_POST
);
1121 if (sqgetGlobalVar('account', $iAccount, SQ_POST
) === false) {
1125 $mailbox = $aMailbox['NAME'];
1127 /* retrieve the check boxes */
1128 $aUid = (isset($msg) && is_array($msg)) ?
array_values($msg) : $aUid;
1129 if (count($aUid) && $sButton != 'expunge') {
1130 $aUpdatedMsgs = false;
1134 // check if id exists in case we come from read_body
1135 if (count($aUid) == 1 && is_array($aMailbox['UIDSET'][$aMailbox['SETINDEX']]) &&
1136 !in_array($aUid[0],$aMailbox['UIDSET'][$aMailbox['SETINDEX']])) {
1139 $aUpdatedMsgs = sqimap_msgs_list_delete($imapConnection, $mailbox, $aUid,$bypass_trash);
1143 case 'unsetDeleted':
1147 case 'unsetFlagged':
1149 $sFlag = (substr($sButton,0,3) == 'set') ?
'\\'.substr($sButton,3) : '\\'.substr($sButton,5);
1150 $bSet = (substr($sButton,0,3) == 'set') ?
true : false;
1151 $aUpdatedMsgs = sqimap_toggle_flag($imapConnection, $aUid, $sFlag, $bSet, true);
1154 $aUpdatedMsgs = sqimap_msgs_list_move($imapConnection,$aUid,$targetMailbox);
1155 sqsession_register($targetMailbox,'lastTargetMailbox');
1159 $aMsgHeaders = array();
1160 foreach ($aUid as $iUid) {
1161 $aMsgHeaders[$iUid] = $aMailbox['MSG_HEADERS'][$iUid];
1163 if (count($aMsgHeaders)) {
1164 $composesession = attachSelectedMessages($imapConnection,$aMsgHeaders);
1165 // dirty hack, add info to $aMailbox
1166 $aMailbox['FORWARD_SESSION'] = $composesession;
1170 // Hook for plugin buttons
1171 do_hook_function('mailbox_display_button_action', $aUid);
1175 * Updates messages is an array containing the result of the untagged
1176 * fetch responses send by the imap server due to a flag change. That
1177 * response is parsed in a array with msg arrays by the parseFetch function
1179 if ($aUpdatedMsgs) {
1180 // Update the message headers cache
1181 $aDeleted = array();
1182 foreach ($aUpdatedMsgs as $iUid => $aMsg) {
1183 if (isset($aMsg['FLAGS'])) {
1185 * Only update the cached headers if the header is
1188 if (isset($aMailbox['MSG_HEADERS'][$iUid])) {
1189 $aMailbox['MSG_HEADERS'][$iUid]['FLAGS'] = $aMsg['FLAGS'];
1192 * Count the messages with the \Delete flag set so we can determine
1193 * if the number of expunged messages equals the number of flagged
1194 * messages for deletion.
1196 if (isset($aMsg['FLAGS']['\\deleted']) && $aMsg['FLAGS']['\\deleted']) {
1197 $aDeleted[] = $iUid;
1201 if ($bExpunge && $aMailbox['AUTO_EXPUNGE'] &&
1202 $iExpungedMessages = sqimap_mailbox_expunge($imapConnection, $aMailbox['NAME'], true))
1204 if (count($aDeleted) != $iExpungedMessages) {
1205 // there are more messages deleted permanently then we expected
1206 // invalidate the cache
1207 $aMailbox['UIDSET'][$aMailbox['SETINDEX']] = false;
1208 $aMailbox['MSG_HEADERS'] = false;
1210 // remove expunged messages from cache
1211 $aUidSet = $aMailbox['UIDSET'][$aMailbox['SETINDEX']];
1212 if (is_array($aUidSet)) {
1213 // create a UID => array index temp array
1214 $aUidSetDummy = array_flip($aUidSet);
1215 foreach ($aDeleted as $iUid) {
1216 // get the id as well in case of SQM_SORT_NONE
1217 if ($aMailbox['SORT'] == SQSORT_NONE
) {
1218 $aMailbox['ID'] = false;
1219 //$iId = $aMailbox['MSG_HEADERS'][$iUid]['ID'];
1220 //unset($aMailbox['ID'][$iId]);
1222 // unset the UID and message header
1223 unset($aUidSetDummy[$iUid]);
1224 unset($aMailbox['MSG_HEADERS'][$iUid]);
1226 $aMailbox['UIDSET'][$aMailbox['SETINDEX']] = array_keys($aUidSetDummy);
1229 // update EXISTS info
1230 if ($iExpungedMessages) {
1231 $aMailbox['EXISTS'] -= (int) $iExpungedMessages;
1232 $aMailbox['TOTAL'][$aMailbox['SETINDEX']] -= (int) $iExpungedMessages;
1234 if (($aMailbox['PAGEOFFSET']-1) >= $aMailbox['EXISTS']) {
1235 $aMailbox['PAGEOFFSET'] = ($aMailbox['PAGEOFFSET'] > $aMailbox['LIMIT']) ?
1236 $aMailbox['PAGEOFFSET'] - $aMailbox['LIMIT'] : 1;
1237 $aMailbox['OFFSET'] = $aMailbox['PAGEOFFSET'] - 1 ;
1242 if ($sButton == 'expunge') {
1244 * on expunge we do not know which messages will be deleted
1245 * so it's useless to try to sync the cache
1247 * Close the mailbox so we do not need to parse the untagged expunge
1248 * responses which do not contain uid info.
1249 * NB: Closing a mailbox is faster then expunge because the imap
1250 * server does not need to generate the untagged expunge responses
1252 sqimap_run_command($imapConnection,'CLOSE',false,$result,$message);
1253 $aMailbox = sqm_api_mailbox_select($imapConnection,$iAccount, $aMailbox['NAME'],array(),array());
1256 $sError = _("No messages were selected.");
1263 function attachSelectedMessages($imapConnection,$aMsgHeaders) {
1264 global $username, $attachment_dir,
1268 sqgetGlobalVar('composesession', $composesession, SQ_SESSION
);
1269 sqgetGlobalVar('compose_messages', $compose_messages, SQ_SESSION
);
1270 if (!isset($compose_messages)||
is_null($compose_messages)) {
1271 $compose_messages = array();
1272 sqsession_register($compose_messages,'compose_messages');
1275 if (!$composesession) {
1276 $composesession = 1;
1277 sqsession_register($composesession,'composesession');
1280 sqsession_register($composesession,'composesession');
1283 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
1285 $composeMessage = new Message();
1286 $rfc822_header = new Rfc822Header();
1287 $composeMessage->rfc822_header
= $rfc822_header;
1288 $composeMessage->reply_rfc822_header
= '';
1290 foreach($aMsgHeaders as $iUid => $aMsgHeader) {
1292 * Retrieve the full message
1294 $body_a = sqimap_run_command($imapConnection, "FETCH $iUid RFC822", true, $response, $readmessage, TRUE);
1295 if ($response == 'OK') {
1297 $subject = (isset($aMsgHeader['subject'])) ?
$aMsgHeader['subject'] : $iUid;
1299 array_shift($body_a);
1301 $body = implode('', $body_a);
1304 $localfilename = GenerateRandomString(32, 'FILE', 7);
1305 $full_localfilename = "$hashed_attachment_dir/$localfilename";
1307 $fp = fopen( $full_localfilename, 'wb');
1308 fwrite ($fp, $body);
1310 $composeMessage->initAttachment('message/rfc822',$subject.'.msg',
1311 $full_localfilename);
1315 $compose_messages[$composesession] = $composeMessage;
1316 sqsession_register($compose_messages,'compose_messages');
1317 return $composesession;