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
);
209 * Restore the headers we fetch the last time. Saves intitialisation stuff in read_body.
211 $aMailbox['FETCHHEADERS'] = (isset($aCachedMailbox['FETCHHEADERS'])) ?
$aCachedMailbox['FETCHHEADERS'] : null;
213 if (!isset($aProps[MBX_PREF_AUTO_EXPUNGE
]) && isset($aCachedMailbox['AUTO_EXPUNGE'])) {
214 $aMailbox['AUTO_EXPUNGE'] = $aCachedMailbox['AUTO_EXPUNGE'];
216 $aMailbox['AUTO_EXPUNGE'] = (isset($aProps[MBX_PREF_AUTO_EXPUNGE
])) ?
$aProps[MBX_PREF_AUTO_EXPUNGE
] : false;
218 if (!isset($aConfig['search']) && isset($aCachedMailbox['SEARCH'][$iSetIndx])) {
219 $aMailbox['SEARCH'][$iSetIndx] = $aCachedMailbox['SEARCH'][$iSetIndx];
221 $aMailbox['SEARCH'][$iSetIndx] = (isset($aConfig['search'])) ?
$aConfig['search'] : 'ALL';
223 if (!isset($aConfig['charset']) && isset($aCachedMailbox['CHARSET'][$iSetIndx])) {
224 $aMailbox['CHARSET'][$iSetIndx] = $aCachedMailbox['CHARSET'][$iSetIndx];
226 $aMailbox['CHARSET'][$iSetIndx] = (isset($aConfig['charset'])) ?
$aConfig['charset'] : 'US-ASCII';
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'];
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
;
243 $aMailbox['THREAD_INDENT'] = $aCachedMailbox['THREAD_INDENT'];
246 $aMailbox['THREAD_INDENT'] = false;
249 /* set a timestamp for cachecontrol */
250 $aMailbox['TIMESTAMP'] = time();
255 * Fetch the message headers for a mailbox. Settings are part of the aMailbox
258 * @param resource $imapConnection imap socket handle
259 * @param array $aMailbox (reference) mailbox retrieved from sqm_api_mailbox_select
260 * @return error $error error number
261 * @author Marc Groot Koerkamp
263 function fetchMessageHeaders($imapConnection, &$aMailbox) {
265 /* FIX ME, this function is kind of big, maybe we can split it up in
266 a couple of functions. Make sure the functions are private and starts with _
267 Also make sure that the error codes are propagated */
270 * Retrieve the UIDSET.
271 * Setindex is used to be able to store multiple uid sets. That will make it
272 * possible to display the mailbox multiple times in different sort order
273 * or to store serach results separate from normal mailbox view.
275 $iSetIndx = (isset($aMailbox['SETINDEX'])) ?
$aMailbox['SETINDEX'] : 0;
277 $iLimit = ($aMailbox['SHOWALL'][$iSetIndx]) ?
$aMailbox['EXISTS'] : $aMailbox['LIMIT'];
279 * Adjust the start_msg
281 $start_msg = $aMailbox['PAGEOFFSET'];
282 if($aMailbox['PAGEOFFSET'] > $aMailbox['EXISTS']) {
283 $start_msg -= $aMailbox['LIMIT'];
289 if (is_array($aMailbox['UIDSET'])) {
290 $aUid =& $aMailbox['UIDSET'][$iSetIndx];
294 $aFetchHeaders = $aMailbox['FETCHHEADERS'];
297 $aFetchItems = $aHeaderItems = array();
298 // initialize the fields we want to retrieve:
299 foreach ($aFetchHeaders as $v) {
301 case SQM_COL_DATE
: $aHeaderFields[] = 'Date'; break;
302 case SQM_COL_TO
: $aHeaderFields[] = 'To'; break;
303 case SQM_COL_CC
: $aHeaderFields[] = 'Cc'; break;
304 case SQM_COL_FROM
: $aHeaderFields[] = 'From'; break;
305 case SQM_COL_SUBJ
: $aHeaderFields[] = 'Subject'; break;
306 case SQM_COL_PRIO
: $aHeaderFields[] = 'X-Priority'; break;
307 case SQM_COL_ATTACHMENT
: $aHeaderFields[] = 'Content-Type'; break;
308 case SQM_COL_INT_DATE
: $aFetchItems[] = 'INTERNALDATE'; break;
309 case SQM_COL_FLAGS
: $aFetchItems[] = 'FLAGS'; break;
310 case SQM_COL_SIZE
: $aFetchItems[] = 'RFC822.SIZE'; break;
316 * A uidset with sorted uid's is available. We can use the cache
318 if (isset($aUid) && $aUid ) {
319 // limit the cache to SQM_MAX_PAGES_IN_CACHE
320 if (!$aMailbox['SHOWALL'][$iSetIndx] && isset($aMailbox['MSG_HEADERS'])) {
321 $iMaxMsgs = $iLimit * SQM_MAX_PAGES_IN_CACHE
;
322 $iCacheSize = count($aMailbox['MSG_HEADERS']);
323 if ($iCacheSize > $iMaxMsgs) {
324 $iReduce = $iCacheSize - $iMaxMsgs;
325 foreach ($aMailbox['MSG_HEADERS'] as $iUid => $value) {
327 unset($aMailbox['MSG_HEADERS'][$iUid]);
336 $id_slice = array_slice($aUid,$start_msg-1,$iLimit);
337 /* do some funky cache checks */
338 if (isset($aMailbox['MSG_HEADERS'])) {
339 $aUidCached = array_keys($aMailbox['MSG_HEADERS']);
341 $aMailbox['MSG_HEADERS'] = array();
342 $aUidCached = array();
344 $aUidNotCached = array_values(array_diff($id_slice,$aUidCached));
347 * $aUidNotCached contains an array with UID's which need to be fetched to
348 * complete the needed message headers.
350 if (count($aUidNotCached)) {
351 $aMsgs = sqimap_get_small_header_list($imapConnection,$aUidNotCached,
352 $aHeaderFields,$aFetchItems);
353 // append the msgs to the existend headers
354 $aMailbox['MSG_HEADERS'] +
= $aMsgs;
358 * Initialize the sorted UID list or initiate a UID list with search
359 * results and fetch the visible message headers
362 if ($aMailbox['SEARCH'][$iSetIndx] != 'ALL') { // in case of a search request
364 if ($aMailbox['SEARCH'][$iSetIndx] && $aMailbox['SORT'] == 0) {
365 $aUid = sqimap_run_search($imapConnection, $aMailbox['SEARCH'][$iSetIndx], $aMailbox['CHARSET'][$iSetIndx]);
369 $iError = _get_sorted_msgs_list($imapConnection,$aMailbox,$iError);
370 $aUid = $aMailbox['UIDSET'][$iSetIndx];
374 * Number of messages is the resultset
376 $aMailbox['TOTAL'][$iSetIndx] = count($aUid);
377 $id_slice = array_slice($aUid,$aMailbox['OFFSET'], $iLimit);
378 if (count($id_slice)) {
379 $aMailbox['MSG_HEADERS'] = sqimap_get_small_header_list($imapConnection,$id_slice,
380 $aHeaderFields,$aFetchItems);
382 $iError = 1; // FIX ME, define an error code
387 $iError = _get_sorted_msgs_list($imapConnection,$aMailbox,$iError);
388 $aUid = $aMailbox['UIDSET'][$iSetIndx];
392 * Number of messages is the resultset
394 $aMailbox['TOTAL'][$iSetIndx] = count($aUid);
395 $id_slice = array_slice($aUid,$aMailbox['OFFSET'], $iLimit);
396 if (count($id_slice)) {
397 $aMailbox['MSG_HEADERS'] = sqimap_get_small_header_list($imapConnection,$id_slice,
398 $aHeaderFields,$aFetchItems);
400 $iError = 1; // FIX ME, define an error code
408 function prepareMessageList(&$aMailbox, $aProps) {
409 /* retrieve the properties */
410 $my_email_address = (isset($aProps['email'])) ?
$aProps['email'] : false;
411 $highlight_list = (isset($aProps['config']['highlight_list'])) ?
$aProps['config']['highlight_list'] : false;
412 $aColumnDesc = (isset($aProps['columns'])) ?
$aProps['columns'] : false;
413 $aExtraColumns = (isset($aProps['extra_columns'])) ?
$aProps['extra_columns'] : array();
414 $iAccount = (isset($aProps['account'])) ?
(int) $aProps['account'] : 0;
415 $sMailbox = (isset($aProps['mailbox'])) ?
$aProps['mailbox'] : false;
416 $sTargetModule = (isset($aProps['module'])) ?
$aProps['module'] : 'read_body';
419 * TODO 1, retrieve array with identity email addresses in order to match against to,cc and set a flag
420 * $aFormattedMessages[$iUid]['match_identity'] = true
421 * The template can show some image if there is a match.
422 * TODO 2, makes sure the matching is done fast by doing a strpos call on the returned $value
426 * Only retrieve values for displayable columns
428 foreach ($aColumnDesc as $k => $v) {
430 case SQM_COL_FROM
: $aCol[SQM_COL_FROM
] = 'from'; break;
431 case SQM_COL_DATE
: $aCol[SQM_COL_DATE
] = 'date'; break;
432 case SQM_COL_SUBJ
: $aCol[SQM_COL_SUBJ
] = 'subject'; break;
433 case SQM_COL_FLAGS
: $aCol[SQM_COL_FLAGS
] = 'FLAGS'; break;
434 case SQM_COL_SIZE
: $aCol[SQM_COL_SIZE
] = 'SIZE'; break;
435 case SQM_COL_PRIO
: $aCol[SQM_COL_PRIO
] = 'x-priority'; break;
436 case SQM_COL_ATTACHMENT
: $aCol[SQM_COL_ATTACHMENT
] = 'content-type'; break;
437 case SQM_COL_INT_DATE
: $aCol[SQM_COL_INT_DATE
] = 'INTERNALDATE'; break;
438 case SQM_COL_TO
: $aCol[SQM_COL_TO
] = 'to'; break;
439 case SQM_COL_CC
: $aCol[SQM_COL_CC
] = 'cc'; break;
440 case SQM_COL_BCC
: $aCol[SQM_COL_BCC
] = 'bcc'; break;
444 $aExtraHighLightColumns = array();
445 foreach ($aExtraColumns as $v) {
447 case SQM_COL_FROM
: $aExtraHighLightColumns[] = 'from'; break;
448 case SQM_COL_SUBJ
: $aExtraHighLightColumns[] = 'subject'; break;
449 case SQM_COL_TO
: $aExtraHighLightColumns[] = 'to'; break;
450 case SQM_COL_CC
: $aExtraHighLightColumns[] = 'cc'; break;
451 case SQM_COL_BCC
: $aExtraHighLightColumns[] = 'bcc'; break;
455 $aFormattedMessages = array();
458 $iSetIndx = $aMailbox['SETINDEX'];
459 $aId = $aMailbox['UIDSET'][$iSetIndx];
460 $aHeaders =& $aMailbox['MSG_HEADERS']; /* use a reference to avoid a copy.
461 MSG_HEADERS can contain large amounts of data */
462 $iOffset = $aMailbox['OFFSET'];
463 $sort = $aMailbox['SORT'];
464 $iPageOffset = $aMailbox['PAGEOFFSET'];
465 $sMailbox = $aMailbox['NAME'];
466 $sSearch = (isset($aMailbox['SEARCH'][$aMailbox['SETINDEX']]) &&
467 $aMailbox['SEARCH'][$aMailbox['SETINDEX']] != 'ALL') ?
$aMailbox['SEARCH'][$aMailbox['SETINDEX']] : false;
468 $aSearch = ($sSearch) ?
array('search.php',$aMailbox['SETINDEX']) : null;
469 /* avoid improper usage */
470 if ($sMailbox && isset($iAccount) && $sTargetModule) {
471 $aInitQuery = array("account=$iAccount",'mailbox='.urlencode($sMailbox));
476 if ($aMailbox['SORT'] & SQSORT_THREAD
) {
477 $aIndentArray =& $aMailbox['THREAD_INDENT'][$aMailbox['SETINDEX']];
483 * Retrieve value for checkbox column
485 if (!sqgetGlobalVar('checkall',$checkall,SQ_GET
)) {
490 * Loop through and display the info for each message.
492 $iEnd = ($aMailbox['SHOWALL'][$iSetIndx]) ?
$aMailbox['EXISTS'] : $iOffset +
$aMailbox['LIMIT'];
493 for ($i=$iOffset,$t=0;$i<$iEnd;++
$i) {
494 if (isset($aId[$i])) {
497 $value = $title = $link = $target = '';
498 $aQuery = ($aInitQuery !== false) ?
$aInitQuery : false;
499 $aMsg = $aHeaders[$aId[$i]];
500 if (isset($aSearch) && count($aSearch) > 1 && $aQuery) {
501 $aQuery[] = "where=". $aSearch[0];
502 $aQuery[] = "what=" . $aSearch[1];
504 $iUid = (isset($aMsg['UID'])) ?
$aMsg['UID'] : $aId[$i];
506 $aQuery[] = "passed_id=$aId[$i]";
507 $aQuery[] = "startMessage=$iPageOffset";
510 foreach ($aCol as $k => $v) {
511 $link = $target = $title = '';
512 $aColumns[$k] = array();
513 $value = (isset($aMsg[$v])) ?
$aMsg[$v] : '';
514 $sUnknown = _("Unknown recipient");
517 $sUnknown = _("Unknown sender");
523 if ($highlight_list && !$bHighLight) {
524 $bHighLight = highlightMessage($aCol[$k], $value, $highlight_list,$aFormattedMessages[$iUid]);
526 $aAddressList = parseRFC822Address($value);
527 $sTmp = getAddressString($aAddressList,array('best' => true));
528 $title = $title_maybe = '';
529 foreach ($aAddressList as $aAddr) {
530 $sPersonal = (isset($aAddr[SQM_ADDR_PERSONAL
])) ?
$aAddr[SQM_ADDR_PERSONAL
] : '';
531 $sMailbox = (isset($aAddr[SQM_ADDR_MAILBOX
])) ?
$aAddr[SQM_ADDR_MAILBOX
] : '';
532 $sHost = (isset($aAddr[SQM_ADDR_HOST
])) ?
$aAddr[SQM_ADDR_HOST
] : '';
534 $title .= htmlspecialchars($sMailbox.'@'.$sHost).', ';
536 // if $value gets truncated we need to add the addresses with no
537 // personal name as well
538 $title_maybe .= htmlspecialchars($sMailbox.'@'.$sHost).', ';
542 $title = substr($title,0,-2); // strip ', ';
544 $sTmp = decodeHeader($sTmp);
545 if (isset($aColumnDesc[$k]['truncate']) && $aColumnDesc[$k]['truncate']) {
546 $sTrunc = truncateWithEntities($sTmp, $aColumnDesc[$k]['truncate']);
547 if ($sTrunc != $sTmp) {
549 $title = htmlspecialchars($sTmp);
550 } else if ($title_maybe) {
551 $title = $title .', '.$title_maybe;
552 $title = substr($title,0,-2); // strip ', ';
558 $value = ($sTmp) ?
$sTmp : $sUnknown;
561 // subject is mime encoded, decode it.
562 // value is sanitized in decoding function.
563 // TODO, verify if it should be done before or after the highlighting
564 $value=decodeHeader($value);
565 if ($highlight_list && !$bHighLight) {
566 $bHighLight = highlightMessage('SUBJECT', $value, $highlight_list, $aFormattedMessages[$iUid]);
568 $iIndent = (isset($aIndentArray[$aId[$i]])) ?
$aIndentArray[$aId[$i]] : 0;
569 // FIXME: don't break 8bit symbols and html entities during truncation
570 if (isset($aColumnDesc[$k]['truncate']) && $aColumnDesc[$k]['truncate']) {
571 $sTmp = truncateWithEntities($value, $aColumnDesc[$k]['truncate']-$iIndent);
572 $title = ($sTmp != $value) ?
$value : '';
575 /* generate the link to the message */
577 // TODO, $sTargetModule should be a query parameter so that we can use a single entrypoint
578 $link = $sTargetModule.'.php?' . implode('&',$aQuery);
580 $value = (trim($value)) ?
$value : _("(no subject)");
581 /* add thread indentation */
582 $aColumns[$k]['indent'] = $iIndent;
585 $value = show_readable_size($value);
588 case SQM_COL_INT_DATE
:
589 $value = getDateString(getTimeStamp(explode(' ',trim($value))));
592 $aFlagColumn = array('seen' => false,
597 foreach ($value as $sFlag => $value) {
599 case '\\seen' : $aFlagColumn['seen'] = true; break;
600 case '\\deleted' : $aFlagColumn['deleted'] = true; break;
601 case '\\answered': $aFlagColumn['answered'] = true; break;
602 case '\\flagged' : $aFlagColumn['flagged'] = true; break;
603 case '\\draft' : $aFlagColumn['draft'] = true; break;
607 $value = $aFlagColumn;
610 $value = ($value) ?
(int) $value : 3;
612 case SQM_COL_ATTACHMENT
:
613 $value = (is_array($value) && $value[0] == 'multipart' && $value[1] == 'mixed') ?
true : false;
620 if ($title) { $aColumns[$k]['title'] = $title; }
621 if ($link) { $aColumns[$k]['link'] = $link; }
622 if ($target) { $aColumns[$k]['target'] = $target; }
623 $aColumns[$k]['value'] = $value;
625 /* columns which will not be displayed but should be inspected
626 because the highlight list contains rules with those columns */
627 foreach ($aExtraHighLightColumns as $v) {
628 if ($highlight_list && !$bHighLight && isset($aMsg[$v])) {
629 $bHighLight = highlightMessage($v, $aMsg[$v], $highlight_list,$aFormattedMessages[$iUid]);
632 $aFormattedMessages[$iUid]['columns'] = $aColumns;
638 return $aFormattedMessages;
643 function highlightMessage($sCol, $sVal, $highlight_list, &$aFormat) {
644 if (!is_array($highlight_list) && count($highlight_list) == 0) {
648 $sCol = strtoupper($sCol);
650 foreach ($highlight_list as $highlight_list_part) {
651 if (trim($highlight_list_part['value'])) {
652 $high_val = strtolower($highlight_list_part['value']);
653 $match_type = strtoupper($highlight_list_part['match_type']);
654 if($match_type == 'TO_CC') {
655 if ($sCol == 'TO' ||
$sCol == 'CC') {
661 if ($match_type != $sCol) {
665 if (strpos(strtolower($sVal),$high_val) !== false) {
666 $hlt_color = $highlight_list_part['color'];
672 // Bug in highlight color???
673 if ($hlt_color{0} != '#') {
674 $hlt_color = '#'. $hlt_color;
676 $aFormat['row']['color'] = $hlt_color;
683 function setUserPref($username, $pref, $value) {
685 setPref($data_dir,$username,$pref,$value);
689 * Execute the sorting for a mailbox
691 * @param resource $imapConnection Imap connection
692 * @param array $aMailbox (reference) Mailbox retrieved with sqm_api_mailbox_select
693 * @return int $error (reference) Error number
695 * @author Marc Groot Koerkamp
697 function _get_sorted_msgs_list($imapConnection,&$aMailbox) {
698 $iSetIndx = (isset($aMailbox['SETINDEX'])) ?
$aMailbox['SETINDEX'] : 0;
699 $bDirection = ($aMailbox['SORT'] %
2);
701 if (!$aMailbox['SEARCH'][$iSetIndx]) {
702 $aMailbox['SEARCH'][$iSetIndx] = 'ALL';
704 if (($aMailbox['SORT'] & SQSORT_THREAD
) && sqimap_capability($imapConnection,'THREAD')) {
705 $aRes = get_thread_sort($imapConnection,$aMailbox['SEARCH'][$iSetIndx]);
706 if ($aRes === false) {
707 $aMailbox['SORT'] -= SQSORT_THREAD
;
708 $error = 1; // fix me, define an error code;
710 $aMailbox['UIDSET'][$iSetIndx] = $aRes[0];
711 $aMailbox['THREAD_INDENT'][$iSetIndx] = $aRes[1];
713 } else if ($aMailbox['SORT'] === SQSORT_NONE
) {
714 $id = sqimap_run_search($imapConnection, 'ALL' , '');
716 $error = 1; // fix me, define an error code
718 $aMailbox['UIDSET'][$iSetIndx] = array_reverse($id);
719 $aMailbox['TOTAL'][$iSetIndx] = $aMailbox['EXISTS'];
722 if (sqimap_capability($imapConnection,'SORT')) {
723 $sSortField = _getSortField($aMailbox['SORT'],true);
724 $id = sqimap_get_sort_order($imapConnection, $sSortField, $bDirection, $aMailbox['SEARCH'][$iSetIndx]);
726 $error = 1; // fix me, define an error code
728 $aMailbox['UIDSET'][$iSetIndx] = $id;
732 if ($aMailbox['SEARCH'][$iSetIndx] != 'ALL') {
733 $id = sqimap_run_search($imapConnection, $aMailbox['SEARCH'][$iSetIndx], $aMailbox['CHARSET'][$iSetIndx]);
735 $sSortField = _getSortField($aMailbox['SORT'],false);
736 $aMailbox['UIDSET'][$iSetIndx] = get_squirrel_sort($imapConnection, $sSortField, $bDirection, $id);
743 * Does the $srt $_GET var to field mapping
745 * @param int $srt Field to sort on
746 * @param bool $bServerSort Server sorting is true
747 * @return string $sSortField Field to sort on
750 function _getSortField($sort,$bServerSort) {
755 case SQSORT_DATE_ASC
:
756 case SQSORT_DATE_DESC
:
757 $sSortField = 'DATE';
759 case SQSORT_FROM_ASC
:
760 case SQSORT_FROM_DESC
:
761 $sSortField = 'FROM';
763 case SQSORT_SUBJ_ASC
:
764 case SQSORT_SUBJ_DESC
:
765 $sSortField = 'SUBJECT';
767 case SQSORT_SIZE_ASC
:
768 case SQSORT_SIZE_DESC
:
769 $sSortField = ($bServerSort) ?
'SIZE' : 'RFC822.SIZE';
779 case SQSORT_INT_DATE_ASC
:
780 case SQSORT_INT_DATE_DESC
:
781 $sSortField = ($bServerSort) ?
'ARRIVAL' : 'INTERNALDATE';
785 default: $sSortField = 'UID';
796 * This function loops through a group of messages in the mailbox
797 * and shows them to the user.
799 * @param resource $imapConnection
800 * @param array $aMailbox associative array with mailbox related vars
801 * @param array $aProps
802 * @param int $iError error code, 0 is no error
804 function showMessagesForMailbox($imapConnection, &$aMailbox,$aProps, &$iError) {
808 $highlight_list = (isset($aProps['config']['highlight_list'])) ?
$aProps['config']['highlight_list'] : false;
809 $fancy_index_highlite = (isset($aProps['config']['fancy_index_highlite'])) ?
$aProps['config']['fancy_index_highlite'] : true;
810 $aColumnsDesc = (isset($aProps['columns'])) ?
$aProps['columns'] : false;
811 $iAccount = (isset($aProps['account'])) ?
(int) $aProps['account'] : 0;
812 $sMailbox = (isset($aProps['mailbox'])) ?
$aProps['mailbox'] : false;
813 $sTargetModule = (isset($aProps['module'])) ?
$aProps['module'] : 'read_body';
814 $show_flag_buttons = (isset($aProps['config']['show_flag_buttons'])) ?
$aProps['config']['show_flag_buttons'] : true;
815 $lastTargetMailbox = (isset($aProps['config']['lastTargetMailbox'])) ?
$aProps['config']['lastTargetMailbox'] : '';
816 $aOrder = array_keys($aProps['columns']);
817 $trash_folder = (isset($aProps['config']['trash_folder']) && $aProps['config']['trash_folder'])
818 ?
$aProps['config']['trash_folder'] : false;
819 $sent_folder = (isset($aProps['config']['sent_folder']) && $aProps['config']['sent_folder'])
820 ?
$aProps['config']['sent_folder'] : false;
821 $draft_folder = (isset($aProps['config']['draft_folder']) && $aProps['config']['draft_folder'])
822 ?
$aProps['config']['draft_folder'] : false;
823 $page_selector = (isset($aProps['config']['page_selector'])) ?
$aProps['config']['page_selector'] : false;
824 $page_selector_max = (isset($aProps['config']['page_selector_max'])) ?
$aProps['config']['page_selector_max'] : 10;
825 $color = $aProps['config']['color'];
833 if (!isset($iFormId)) {
839 * Remove the checkbox column because we cannot fetch it from the imap server
841 $aFetchColumns = $aColumnsDesc;
842 if (isset($aFetchColumns[SQM_COL_CHECK
])) {
843 unset($aFetchColumns[SQM_COL_CHECK
]);
847 * Before we fetch the message headers, check if we need to fetch extra columns
848 * to make the message highlighting work
850 if (is_array($highlight_list) && count($highlight_list)) {
851 $aHighlightColumns = array();
852 foreach ($highlight_list as $highlight_list_part) {
853 if (trim($highlight_list_part['value'])) {
854 $match_type = strtoupper($highlight_list_part['match_type']);
855 switch ($match_type) {
857 $aHighlightColumns[SQM_COL_TO
] = true;
858 $aHighlightColumns[SQM_COL_CC
] = true;
860 case 'TO': $aHighlightColumns[SQM_COL_TO
] = true; break;
861 case 'CC': $aHighlightColumns[SQM_COL_CC
] = true; break;
862 case 'FROM': $aHighlightColumns[SQM_COL_FROM
] = true; break;
863 case 'SUBJECT':$aHighlightColumns[SQM_COL_SUBJ
] = true; break;
867 $aExtraColumns = array();
868 foreach ($aHighlightColumns as $k => $v) {
869 if (!isset($aFetchColumns[$k])) {
870 $aExtraColumns[] = $k;
871 $aFetchColumns[$k] = true;
874 if (count($aExtraColumns)) {
875 $aProps['extra_columns'] = $aExtraColumns;
878 $aFetchColumns = array_keys($aFetchColumns);
879 // store the columns to fetch so we can pick them up in read_body
880 // where we validate the cache.
881 $aMailbox['FETCHHEADERS'] = $aFetchColumns;
883 $iError = fetchMessageHeaders($imapConnection, $aMailbox);
887 $aMessages = prepareMessageList($aMailbox, $aProps);
890 $iSetIndx = $aMailbox['SETINDEX'];
891 $iLimit = ($aMailbox['SHOWALL'][$iSetIndx]) ?
$aMailbox['EXISTS'] : $aMailbox['LIMIT'];
892 $iEnd = ($aMailbox['PAGEOFFSET'] +
($iLimit - 1) < $aMailbox['EXISTS']) ?
893 $aMailbox['PAGEOFFSET'] +
$iLimit - 1 : $aMailbox['EXISTS'];
895 $iNumberOfMessages = $aMailbox['TOTAL'][$iSetIndx];
897 $php_self = $PHP_SELF;
899 $urlMailbox = urlencode($aMailbox['NAME']);
901 if (preg_match('/^(.+)\?.+$/',$php_self,$regs)) {
902 $source_url = $regs[1];
904 $source_url = $php_self;
907 $baseurl = $source_url.'?mailbox=' . urlencode($aMailbox['NAME']) .'&account='.$aMailbox['ACCOUNT'];
908 $where = urlencode($aMailbox['SEARCH'][$iSetIndx][0]);
909 $what = urlencode($aMailbox['SEARCH'][$iSetIndx][1]);
910 $baseurl .= '&where=' . $where . '&what=' . $what;
912 /* build thread sorting links */
913 $newsort = $aMailbox['SORT'];
914 if (sqimap_capability($imapConnection,'THREAD')) {
915 if ($aMailbox['SORT'] & SQSORT_THREAD
) {
916 $newsort -= SQSORT_THREAD
;
917 $thread_name = _("Unthread View");
919 $thread_name = _("Thread View");
920 $newsort = $aMailbox['SORT'] + SQSORT_THREAD
;
922 $thread_link_str = '<small>[<a href="' . $baseurl . '&srt='
923 . $newsort . '&startMessage=1">' . $thread_name
926 $thread_link_str ='';
928 $sort = $aMailbox['SORT'];
930 /* FIX ME ADD CHECKBOX CONTROL. No checkbox => no buttons */
934 /* future admin control over displayable buttons */
936 $aAdminControl = array(
937 'markUnflagged' => 1,
943 'undeleteButton'=> 1,
945 'expungeButton' => 1,
948 /* user prefs control */
949 $aUserControl = array (
951 'markUnflagged' => $show_flag_buttons,
952 'markFlagged' => $show_flag_buttons,
957 'undeleteButton'=> 1,
959 'expungeButton' => 1,
964 $showDelete = ($aMailbox['RIGHTS'] != 'READ-ONLY' &&
965 in_array('\\deleted',$aMailbox['PERMANENTFLAGS'], true)) ?
true : false;
966 $showByPassTrash = (($aMailbox['AUTO_EXPUNGE'] && $aMailbox['RIGHTS'] != 'READ-ONLY' &&
967 in_array('\\deleted',$aMailbox['PERMANENTFLAGS'], true)) &&
968 $trash_folder) ?
true : false; //
970 $showUndelete = (!$aMailbox['AUTO_EXPUNGE'] && $aMailbox['RIGHTS'] != 'READ-ONLY' &&
971 in_array('\\deleted',$aMailbox['PERMANENTFLAGS'], true) && !$trash_folder) ?
true : false;
972 $showMove = ($aMailbox['RIGHTS'] != 'READ-ONLY') ?
true : false;
973 $showExpunge = (!$aMailbox['AUTO_EXPUNGE'] && $aMailbox['RIGHTS'] != 'READ-ONLY' &&
974 in_array('\\deleted',$aMailbox['PERMANENTFLAGS'], true)) ?
true : false;
975 $aImapControl = array (
976 'markUnflagged' => in_array('\\flagged',$aMailbox['PERMANENTFLAGS'], true),
977 'markFlagged' => in_array('\\flagged',$aMailbox['PERMANENTFLAGS'], true),
978 'markRead' => in_array('\\seen',$aMailbox['PERMANENTFLAGS'], true),
979 'markUnread' => in_array('\\seen',$aMailbox['PERMANENTFLAGS'], true),
981 'delete' => $showDelete,
982 'undeleteButton'=> $showUndelete,
983 'bypass_trash' => $showByPassTrash,
984 'expungeButton' => $showExpunge,
985 'moveButton' => $showMove
987 $aButtonStrings = array(
988 'markUnflagged' => _("Unflag"),
989 'markFlagged' => _("Flag"),
990 'markRead' => _("Read"),
991 'markUnread' => _("Unread"),
992 'forward' => _("Forward"),
993 'delete' => _("Delete"),
994 'undeleteButton' => _("Undelete"),
995 'bypass_trash' => _("Bypass Trash"),
996 'expungeButton' => _("Expunge"),
997 'moveButton' => _("Move")
1002 * Register buttons in order to an array
1003 * The key is the "name", the first element of the value array is the "value", second argument is the type.
1005 $aFormElements = array();
1006 foreach($aAdminControl as $k => $v) {
1007 if ($v & $aUserControl[$k] & $aImapControl[$k]) {
1009 case 'markUnflagged':
1014 case 'undeleteButton':
1015 case 'expungeButton':
1017 $aFormElements[$k] = array($aButtonStrings[$k],'submit');
1019 case 'bypass_trash':
1020 $aFormElements[$k] = array($aButtonStrings[$k],'checkbox');
1023 $aFormElements['targetMailbox'] =
1024 array(sqimap_mailbox_option_list($imapConnection, array(strtolower($lastTargetMailbox)), 0, $boxes),'select');
1025 $aFormElements['mailbox'] = array($aMailbox['NAME'],'hidden');
1026 $aFormElements['startMessage'] = array($aMailbox['PAGEOFFSET'],'hidden');
1027 $aFormElements[$k] = array($aButtonStrings[$k],'submit');
1031 $aFormElements['account'] = array($iAccount,'hidden');
1035 * This is the beginning of the message list table.
1036 * It wraps around all messages
1038 $safe_name = preg_replace("/[^0-9A-Za-z_]/", '_', $aMailbox['NAME']);
1039 $form_name = "FormMsgs" . $safe_name;
1041 //if (!sqgetGlobalVar('align',$align,SQ_SESSION)) {
1042 $align = array('left' => 'left', 'right' => 'right');
1044 //sm_print_r($align);
1046 /* finally set the template vars */
1048 // FIX ME, before we support multiple templates we must review the names of the vars
1052 $aTemplate['color'] = $color;
1053 $aTemplate['form_name'] = "FormMsgs" . $safe_name;
1054 $aTemplate['form_id'] = 'mbx_'.$iFormId;
1055 $aTemplate['page_selector'] = $page_selector;
1056 $aTemplate['page_selector_max'] = $page_selector_max;
1057 $aTemplate['messagesPerPage'] = $aMailbox['LIMIT'];
1058 $aTemplate['showall'] = $aMailbox['SHOWALL'][$iSetIndx];
1059 $aTemplate['end_msg'] = $iEnd;
1060 $aTemplate['align'] = $align;
1061 $aTemplate['iNumberOfMessages'] = $iNumberOfMessages;
1062 $aTemplate['aOrder'] = $aOrder;
1063 $aTemplate['aFormElements'] = $aFormElements;
1064 $aTemplate['sort'] = $sort;
1065 $aTemplate['pageOffset'] = $aMailbox['PAGEOFFSET'];
1066 $aTemplate['baseurl'] = $baseurl;
1067 $aTemplate['aMessages'] =& $aMessages;
1068 $aTemplate['trash_folder'] = $trash_folder;
1069 $aTemplate['sent_folder'] = $sent_folder;
1070 $aTemplate['draft_folder'] = $draft_folder;
1071 $aTemplate['thread_link_str'] = $thread_link_str;
1072 $aTemplate['php_self'] = str_replace('&','&',$php_self);
1073 $aTemplate['mailbox'] = $sMailbox;
1074 $aTemplate['javascript_on'] = (isset($aProps['config']['javascript_on'])) ?
$aProps['config']['javascript_on'] : false;
1075 $aTemplate['enablesort'] = (isset($aProps['config']['enablesort'])) ?
$aProps['config']['enablesort'] : false;
1076 $aTemplate['icon_theme'] = (isset($aProps['config']['icon_theme'])) ?
$aProps['config']['icon_theme'] : false;
1077 $aTemplate['use_icons'] = (isset($aProps['config']['use_icons'])) ?
$aProps['config']['use_icons'] : false;
1078 $aTemplate['alt_index_colors'] = (isset($aProps['config']['alt_index_colors'])) ?
$aProps['config']['alt_index_colors'] : false;
1079 $aTemplate['fancy_index_highlite'] = $fancy_index_highlite;
1086 * Truncates a string and take care of html encoded characters
1088 * @param string $s string to truncate
1089 * @param int $iTrimAt Trim at nn characters
1090 * @return string Trimmed string
1092 function truncateWithEntities($s, $iTrimAt) {
1093 global $languages, $squirrelmail_language;
1095 $ent_strlen = strlen($s);
1096 if (($iTrimAt <= 0) ||
($ent_strlen <= $iTrimAt))
1099 if (isset($languages[$squirrelmail_language]['XTRA_CODE']) &&
1100 function_exists($languages[$squirrelmail_language]['XTRA_CODE'] . '_strimwidth')) {
1101 return call_user_func($languages[$squirrelmail_language]['XTRA_CODE'] . '_strimwidth', $s, $iTrimAt);
1104 * see if this is entities-encoded string
1105 * If so, Iterate through the whole string, find out
1106 * the real number of characters, and if more
1107 * than $iTrimAt, substr with an updated trim value.
1109 $trim_val = $iTrimAt;
1112 while ( $ent_loc < $trim_val && (($ent_loc = strpos($s, '&', $ent_offset)) !== false) &&
1113 (($ent_loc_end = strpos($s, ';', $ent_loc+
3)) !== false) ) {
1114 $trim_val +
= ($ent_loc_end-$ent_loc);
1115 $ent_offset = $ent_loc_end+
1;
1118 if (($trim_val > $iTrimAt) && ($ent_strlen > $trim_val) && (strpos($s,';',$trim_val) < ($trim_val +
6))) {
1119 $i = strpos($s,';',$trim_val);
1121 $trim_val = strpos($s,';',$trim_val)+
1;
1124 // only print '...' when we're actually dropping part of the subject
1125 if ($ent_strlen <= $trim_val)
1128 return substr_replace($s, '...', $trim_val);
1133 * This should go in imap_mailbox.php
1134 * @param string $mailbox
1136 function handleAsSent($mailbox) {
1137 global $handleAsSent_result;
1139 /* First check if this is the sent or draft folder. */
1140 $handleAsSent_result = isSentMailbox($mailbox) ||
isDraftMailbox($mailbox);
1142 /* Then check the result of the handleAsSent hook. */
1143 do_hook('check_handleAsSent_result', $mailbox);
1145 /* And return the result. */
1146 return $handleAsSent_result;
1150 * Process messages list form and handle the cache gracefully. If $sButton and
1151 * $aUid are provided as argument then you can fake a message list submit and
1152 * use it i.e. in read_body.php for del move next and update the cache
1154 * @param resource $imapConnection imap connection
1155 * @param array $aMailbox (reference) cached mailbox
1156 * @param string $sButton fake a submit button
1157 * @param array $aUid fake the $msg array
1158 * @return string $sError error string in case of an error
1159 * @author Marc Groot Koerkamp
1161 function handleMessageListForm($imapConnection,&$aMailbox,$sButton='',$aUid = array()) {
1162 /* incoming formdata */
1163 $sButton = (sqgetGlobalVar('moveButton', $sTmp, SQ_POST
)) ?
'move' : $sButton;
1164 $sButton = (sqgetGlobalVar('expungeButton', $sTmp, SQ_POST
)) ?
'expunge' : $sButton;
1165 $sButton = (sqgetGlobalVar('forward', $sTmp, SQ_POST
)) ?
'forward' : $sButton;
1166 $sButton = (sqgetGlobalVar('delete', $sTmp, SQ_POST
)) ?
'setDeleted' : $sButton;
1167 $sButton = (sqgetGlobalVar('undeleteButton', $sTmp, SQ_POST
)) ?
'unsetDeleted' : $sButton;
1168 $sButton = (sqgetGlobalVar('markRead', $sTmp, SQ_POST
)) ?
'setSeen' : $sButton;
1169 $sButton = (sqgetGlobalVar('markUnread', $sTmp, SQ_POST
)) ?
'unsetSeen' : $sButton;
1170 $sButton = (sqgetGlobalVar('markFlagged', $sTmp, SQ_POST
)) ?
'setFlagged' : $sButton;
1171 $sButton = (sqgetGlobalVar('markUnflagged', $sTmp, SQ_POST
)) ?
'unsetFlagged' : $sButton;
1172 sqgetGlobalVar('targetMailbox', $targetMailbox, SQ_POST
);
1173 sqgetGlobalVar('bypass_trash', $bypass_trash, SQ_POST
);
1174 sqgetGlobalVar('msg', $msg, SQ_POST
);
1175 if (sqgetGlobalVar('account', $iAccount, SQ_POST
) === false) {
1179 $mailbox = $aMailbox['NAME'];
1181 /* retrieve the check boxes */
1182 $aUid = (isset($msg) && is_array($msg)) ?
array_values($msg) : $aUid;
1183 if (count($aUid) && $sButton != 'expunge') {
1184 $aUpdatedMsgs = false;
1188 // check if id exists in case we come from read_body
1189 if (count($aUid) == 1 && is_array($aMailbox['UIDSET'][$aMailbox['SETINDEX']]) &&
1190 !in_array($aUid[0],$aMailbox['UIDSET'][$aMailbox['SETINDEX']])) {
1193 $aUpdatedMsgs = sqimap_msgs_list_delete($imapConnection, $mailbox, $aUid,$bypass_trash);
1197 case 'unsetDeleted':
1201 case 'unsetFlagged':
1203 $sFlag = (substr($sButton,0,3) == 'set') ?
'\\'.substr($sButton,3) : '\\'.substr($sButton,5);
1204 $bSet = (substr($sButton,0,3) == 'set') ?
true : false;
1205 $aUpdatedMsgs = sqimap_toggle_flag($imapConnection, $aUid, $sFlag, $bSet, true);
1208 $aUpdatedMsgs = sqimap_msgs_list_move($imapConnection,$aUid,$targetMailbox);
1209 sqsession_register($targetMailbox,'lastTargetMailbox');
1213 $aMsgHeaders = array();
1214 foreach ($aUid as $iUid) {
1215 $aMsgHeaders[$iUid] = $aMailbox['MSG_HEADERS'][$iUid];
1217 if (count($aMsgHeaders)) {
1218 $composesession = attachSelectedMessages($imapConnection,$aMsgHeaders);
1219 // dirty hack, add info to $aMailbox
1220 $aMailbox['FORWARD_SESSION'] = $composesession;
1224 // Hook for plugin buttons
1225 do_hook_function('mailbox_display_button_action', $aUid);
1229 * Updates messages is an array containing the result of the untagged
1230 * fetch responses send by the imap server due to a flag change. That
1231 * response is parsed in a array with msg arrays by the parseFetch function
1233 if ($aUpdatedMsgs) {
1234 // Update the message headers cache
1235 $aDeleted = array();
1236 foreach ($aUpdatedMsgs as $iUid => $aMsg) {
1237 if (isset($aMsg['FLAGS'])) {
1239 * Only update the cached headers if the header is
1242 if (isset($aMailbox['MSG_HEADERS'][$iUid])) {
1243 $aMailbox['MSG_HEADERS'][$iUid]['FLAGS'] = $aMsg['FLAGS'];
1246 * Count the messages with the \Delete flag set so we can determine
1247 * if the number of expunged messages equals the number of flagged
1248 * messages for deletion.
1250 if (isset($aMsg['FLAGS']['\\deleted']) && $aMsg['FLAGS']['\\deleted']) {
1251 $aDeleted[] = $iUid;
1255 if ($bExpunge && $aMailbox['AUTO_EXPUNGE'] &&
1256 $iExpungedMessages = sqimap_mailbox_expunge($imapConnection, $aMailbox['NAME'], true))
1258 if (count($aDeleted) != $iExpungedMessages) {
1259 // there are more messages deleted permanently then we expected
1260 // invalidate the cache
1261 $aMailbox['UIDSET'][$aMailbox['SETINDEX']] = false;
1262 $aMailbox['MSG_HEADERS'] = false;
1264 // remove expunged messages from cache
1265 $aUidSet = $aMailbox['UIDSET'][$aMailbox['SETINDEX']];
1266 if (is_array($aUidSet)) {
1267 // create a UID => array index temp array
1268 $aUidSetDummy = array_flip($aUidSet);
1269 foreach ($aDeleted as $iUid) {
1270 // get the id as well in case of SQM_SORT_NONE
1271 if ($aMailbox['SORT'] == SQSORT_NONE
) {
1272 $aMailbox['ID'] = false;
1273 //$iId = $aMailbox['MSG_HEADERS'][$iUid]['ID'];
1274 //unset($aMailbox['ID'][$iId]);
1276 // unset the UID and message header
1277 unset($aUidSetDummy[$iUid]);
1278 unset($aMailbox['MSG_HEADERS'][$iUid]);
1280 $aMailbox['UIDSET'][$aMailbox['SETINDEX']] = array_keys($aUidSetDummy);
1283 // update EXISTS info
1284 if ($iExpungedMessages) {
1285 $aMailbox['EXISTS'] -= (int) $iExpungedMessages;
1286 $aMailbox['TOTAL'][$aMailbox['SETINDEX']] -= (int) $iExpungedMessages;
1288 if (($aMailbox['PAGEOFFSET']-1) >= $aMailbox['EXISTS']) {
1289 $aMailbox['PAGEOFFSET'] = ($aMailbox['PAGEOFFSET'] > $aMailbox['LIMIT']) ?
1290 $aMailbox['PAGEOFFSET'] - $aMailbox['LIMIT'] : 1;
1291 $aMailbox['OFFSET'] = $aMailbox['PAGEOFFSET'] - 1 ;
1296 if ($sButton == 'expunge') {
1298 * on expunge we do not know which messages will be deleted
1299 * so it's useless to try to sync the cache
1301 * Close the mailbox so we do not need to parse the untagged expunge
1302 * responses which do not contain uid info.
1303 * NB: Closing a mailbox is faster then expunge because the imap
1304 * server does not need to generate the untagged expunge responses
1306 sqimap_run_command($imapConnection,'CLOSE',false,$result,$message);
1307 $aMailbox = sqm_api_mailbox_select($imapConnection,$iAccount, $aMailbox['NAME'],array(),array());
1310 $sError = _("No messages were selected.");
1317 function attachSelectedMessages($imapConnection,$aMsgHeaders) {
1318 global $username, $attachment_dir,
1322 sqgetGlobalVar('composesession', $composesession, SQ_SESSION
);
1323 sqgetGlobalVar('compose_messages', $compose_messages, SQ_SESSION
);
1324 if (!isset($compose_messages)||
is_null($compose_messages)) {
1325 $compose_messages = array();
1326 sqsession_register($compose_messages,'compose_messages');
1329 if (!$composesession) {
1330 $composesession = 1;
1331 sqsession_register($composesession,'composesession');
1334 sqsession_register($composesession,'composesession');
1337 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
1339 $composeMessage = new Message();
1340 $rfc822_header = new Rfc822Header();
1341 $composeMessage->rfc822_header
= $rfc822_header;
1342 $composeMessage->reply_rfc822_header
= '';
1344 foreach($aMsgHeaders as $iUid => $aMsgHeader) {
1346 * Retrieve the full message
1348 $body_a = sqimap_run_command($imapConnection, "FETCH $iUid RFC822", true, $response, $readmessage, TRUE);
1349 if ($response == 'OK') {
1351 $subject = (isset($aMsgHeader['subject'])) ?
$aMsgHeader['subject'] : $iUid;
1353 array_shift($body_a);
1355 $body = implode('', $body_a);
1358 $localfilename = GenerateRandomString(32, 'FILE', 7);
1359 $full_localfilename = "$hashed_attachment_dir/$localfilename";
1361 $fp = fopen( $full_localfilename, 'wb');
1362 fwrite ($fp, $body);
1364 $composeMessage->initAttachment('message/rfc822',$subject.'.msg',
1365 $full_localfilename);
1369 $compose_messages[$composesession] = $composeMessage;
1370 sqsession_register($compose_messages,'compose_messages');
1371 return $composesession;