6 * This contains functions that display mailbox information, such as the
7 * table row that has sender, date, subject, etc...
9 * @copyright © 1999-2006 The SquirrelMail Project Team
10 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
12 * @package squirrelmail
17 * Selects a mailbox for header retrieval.
18 * Cache control for message headers is embedded.
20 * @param resource $imapConnection imap socket handle
21 * @param string $mailbox mailbox to select and retrieve message headers from
22 * @param array $aConfig array with system config settings and incoming vars
23 * @param array $aProps mailbox specific properties
24 * @return array $aMailbox mailbox array with all relevant information
26 * @author Marc Groot Koerkamp
28 function sqm_api_mailbox_select($imapConnection,$account,$mailbox,$aConfig,$aProps) {
31 * NB: retrieve this from the session before accessing this function
32 * and make sure you write it back at the end of the script after
33 * the aMailbox var is added so that the headers are added to the cache
35 global $mailbox_cache;
37 $aDefaultConfigProps = array(
38 // 'charset' => 'US-ASCII',
39 'user' => false, /* no pref storage if false */
42 'max_cache_size' => SQM_MAX_MBX_IN_CACHE
45 $aConfig = array_merge($aDefaultConfigProps,$aConfig);
47 $iSetIndx = $aConfig['setindex'];
49 $aMbxResponse = sqimap_mailbox_select($imapConnection, $mailbox);
52 if (isset($mailbox_cache[$account.'_'.$mailbox])) {
53 $aCachedMailbox = $mailbox_cache[$account.'_'.$mailbox];
55 $aCachedMailbox = false;
58 if (count($mailbox_cache) > $aConfig['max_cache_size'] -1) {
60 foreach($mailbox_cache as $cachedmailbox => $aVal) {
61 $aTime[$aVal['TIMESTAMP']] = $cachedmailbox;
63 if (ksort($aTime,SORT_NUMERIC
)) {
64 for ($i=0,$iCnt=count($mailbox_cache);$i<($iCnt-$aConfig['max_cache_size']);++
$i) {
65 $sOldestMbx = array_shift($aTime);
67 * Remove only the UIDSET and MSG_HEADERS from cache because those can
68 * contain large amounts of data.
70 if (isset($mailbox_cache[$sOldestMbx]['UIDSET'])) {
71 $mailbox_cache[$sOldestMbx]['UIDSET']= false;
73 if (isset($mailbox_cache[$sOldestMbx]['MSG_HEADERS'])) {
74 $mailbox_cache[$sOldestMbx]['MSG_HEADERS'] = false;
81 $aCachedMailbox = false;
85 * Deal with imap servers that do not return the required UIDNEXT or
86 * UIDVALIDITY response
87 * from a SELECT call (since rfc 3501 it's required).
89 if (!isset($aMbxResponse['UIDNEXT']) ||
!isset($aMbxResponse['UIDVALIDITY'])) {
90 $aStatus = sqimap_status_messages($imapConnection,$mailbox,
91 array('UIDNEXT','UIDVALIDITY'));
92 $aMbxResponse['UIDNEXT'] = $aStatus['UIDNEXT'];
93 $aMbxResponse['UIDVALIDTY'] = $aStatus['UIDVALIDITY'];
96 $aMailbox['ACCOUNT'] = $account;
97 $aMailbox['UIDSET'][$iSetIndx] = false;
98 $aMailbox['ID'] = false;
99 $aMailbox['SETINDEX'] = $iSetIndx;
100 $aMailbox['MSG_HEADERS'] = false;
102 if ($aCachedMailbox) {
104 * Validate integrity of cached data
106 if ($aCachedMailbox['EXISTS'] == $aMbxResponse['EXISTS'] &&
107 $aMbxResponse['EXISTS'] &&
108 $aCachedMailbox['UIDVALIDITY'] == $aMbxResponse['UIDVALIDITY'] &&
109 $aCachedMailbox['UIDNEXT'] == $aMbxResponse['UIDNEXT'] &&
110 isset($aCachedMailbox['SEARCH'][$iSetIndx]) &&
111 (!isset($aConfig['search']) ||
/* always set search from the searchpage */
112 $aCachedMailbox['SEARCH'][$iSetIndx] == $aConfig['search'])) {
113 if (isset($aCachedMailbox['MSG_HEADERS'])) {
114 $aMailbox['MSG_HEADERS'] = $aCachedMailbox['MSG_HEADERS'];
116 $aMailbox['ID'] = $aCachedMailbox['ID'];
117 if (isset($aCachedMailbox['UIDSET'][$iSetIndx]) && $aCachedMailbox['UIDSET'][$iSetIndx]) {
118 if (isset($aProps[MBX_PREF_SORT
]) && $aProps[MBX_PREF_SORT
] != $aCachedMailbox['SORT'] ) {
119 $newsort = $aProps[MBX_PREF_SORT
];
120 $oldsort = $aCachedMailbox['SORT'];
122 * If it concerns a reverse sort we do not need to invalidate
123 * the cached sorted UIDSET, a reverse is sufficient.
125 if ((($newsort %
2) && ($newsort +
1 == $oldsort)) ||
126 (!($newsort %
2) && ($newsort - 1 == $oldsort))) {
127 $aMailbox['UIDSET'][$iSetIndx] = array_reverse($aCachedMailbox['UIDSET'][$iSetIndx]);
129 $server_sort_array = false;
130 $aMailbox['MSG_HEADERS'] = false;
131 $aMailbox['ID'] = false;
133 // store the new sort value in the mailbox pref
134 if ($aConfig['user']) {
135 // FIXME, in ideal situation, we write back the
136 // prefs at the end of the script
137 setUserPref($aConfig['user'],'pref_'.$account.'_'.$mailbox,serialize($aProps));
140 $aMailbox['UIDSET'][$iSetIndx] = $aCachedMailbox['UIDSET'][$iSetIndx];
146 * Restore the offset in the paginator if no new offset is provided.
148 if (isset($aMailbox['UIDSET'][$iSetIndx]) && !isset($aConfig['offset']) && $aCachedMailbox['OFFSET']) {
149 $aMailbox['OFFSET'] = $aCachedMailbox['OFFSET'];
150 $aMailbox['PAGEOFFSET'] = $aCachedMailbox['PAGEOFFSET'];
152 $aMailbox['OFFSET'] = (isset($aConfig['offset']) && $aConfig['offset']) ?
$aConfig['offset'] -1 : 0;
153 $aMailbox['PAGEOFFSET'] = (isset($aConfig['offset']) && $aConfig['offset']) ?
$aConfig['offset'] : 1;
156 * Restore the number of messages in the result set
158 if (isset($aCachedMailbox['TOTAL'][$iSetIndx]) && $aCachedMailbox['TOTAL'][$iSetIndx]) {
159 $aMailbox['TOTAL'][$iSetIndx] = $aCachedMailbox['TOTAL'][$iSetIndx];
163 * Restore the showall value no new showall value is provided.
165 if (isset($aMailbox['UIDSET'][$iSetIndx]) && !isset($aConfig['showall']) &&
166 isset($aCachedMailbox['SHOWALL'][$iSetIndx]) && $aCachedMailbox['SHOWALL'][$iSetIndx]) {
167 $aMailbox['SHOWALL'][$iSetIndx] = $aCachedMailbox['SHOWALL'][$iSetIndx];
169 $aMailbox['SHOWALL'][$iSetIndx] = (isset($aConfig['showall']) && $aConfig['showall']) ?
1 : 0;
173 * Restore the sort order if no new sort order is provided.
175 if (!isset($aProps[MBX_PREF_SORT
]) && isset($aCachedMailbox['SORT'])) {
176 $aMailbox['SORT'] = $aCachedMailbox['SORT'];
178 $aMailbox['SORT'] = (isset($aProps[MBX_PREF_SORT
])) ?
$aProps[MBX_PREF_SORT
] : 0;
182 * Restore the number of message to show per page when no new limit is provided
184 if (!isset($aProps[MBX_PREF_LIMIT
]) && isset($aCachedMailbox['LIMIT'])) {
185 $aMailbox['LIMIT'] = $aCachedMailbox['LIMIT'];
187 $aMailbox['LIMIT'] = (isset($aProps[MBX_PREF_LIMIT
])) ?
$aProps[MBX_PREF_LIMIT
] : 15;
191 * Restore the ordered columns to show when no new ordered columns are provided
193 if (!isset($aProps[MBX_PREF_COLUMNS
]) && isset($aCachedMailbox['COLUMNS'])) {
194 $aMailbox['COLUMNS'] = $aCachedMailbox['COLUMNS'];
196 $aMailbox['COLUMNS'] = (isset($aProps[MBX_PREF_COLUMNS
])) ?
$aProps[MBX_PREF_COLUMNS
] :
197 array(SQM_COL_FLAGS
,SQM_COL_FROM
, SQM_COL_SUBJ
, SQM_COL_FLAGS
);
201 * Restore the headers we fetch the last time. Saves intitialisation stuff in read_body.
203 $aMailbox['FETCHHEADERS'] = (isset($aCachedMailbox['FETCHHEADERS'])) ?
$aCachedMailbox['FETCHHEADERS'] : null;
205 if (!isset($aProps[MBX_PREF_AUTO_EXPUNGE
]) && isset($aCachedMailbox['AUTO_EXPUNGE'])) {
206 $aMailbox['AUTO_EXPUNGE'] = $aCachedMailbox['AUTO_EXPUNGE'];
208 $aMailbox['AUTO_EXPUNGE'] = (isset($aProps[MBX_PREF_AUTO_EXPUNGE
])) ?
$aProps[MBX_PREF_AUTO_EXPUNGE
] : false;
210 if (!isset($aConfig['search']) && isset($aCachedMailbox['SEARCH'][$iSetIndx])) {
211 $aMailbox['SEARCH'][$iSetIndx] = $aCachedMailbox['SEARCH'][$iSetIndx];
212 } else if (isset($aConfig['search']) && isset($aCachedMailbox['SEARCH'][$iSetIndx]) &&
213 $aConfig['search'] != $aCachedMailbox['SEARCH'][$iSetIndx]) {
214 // reset the pageindex
215 $aMailbox['SEARCH'][$iSetIndx] = $aConfig['search'];
216 $aMailbox['OFFSET'] = 0;
217 $aMailbox['PAGEOFFSET'] = 1;
219 $aMailbox['SEARCH'][$iSetIndx] = (isset($aConfig['search'])) ?
$aConfig['search'] : 'ALL';
221 if (!isset($aConfig['charset']) && isset($aCachedMailbox['CHARSET'][$iSetIndx])) {
222 $aMailbox['CHARSET'][$iSetIndx] = $aCachedMailbox['CHARSET'][$iSetIndx];
224 $aMailbox['CHARSET'][$iSetIndx] = (isset($aConfig['charset'])) ?
$aConfig['charset'] : 'US-ASCII';
227 $aMailbox['NAME'] = $mailbox;
228 $aMailbox['EXISTS'] = $aMbxResponse['EXISTS'];
229 $aMailbox['SEEN'] = (isset($aMbxResponse['SEEN'])) ?
$aMbxResponse['SEEN'] : $aMbxResponse['EXISTS'];
230 $aMailbox['RECENT'] = (isset($aMbxResponse['RECENT'])) ?
$aMbxResponse['RECENT'] : 0;
231 $aMailbox['UIDVALIDITY'] = $aMbxResponse['UIDVALIDITY'];
232 $aMailbox['UIDNEXT'] = $aMbxResponse['UIDNEXT'];
233 $aMailbox['PERMANENTFLAGS'] = $aMbxResponse['PERMANENTFLAGS'];
234 $aMailbox['RIGHTS'] = $aMbxResponse['RIGHTS'];
236 /* decide if we are thread sorting or not */
237 if ($aMailbox['SORT'] & SQSORT_THREAD
) {
238 if (!sqimap_capability($imapConnection,'THREAD')) {
239 $aMailbox['SORT'] ^
= SQSORT_THREAD
;
241 $aMailbox['THREAD_INDENT'] = $aCachedMailbox['THREAD_INDENT'];
244 $aMailbox['THREAD_INDENT'] = false;
247 /* set a timestamp for cachecontrol */
248 $aMailbox['TIMESTAMP'] = time();
253 * Fetch the message headers for a mailbox. Settings are part of the aMailbox
254 * array. Dependent of the mailbox settings it deals with sort, thread and search
255 * If server sort is supported then SORT is also used for retrieving sorted search results
257 * @param resource $imapConnection imap socket handle
258 * @param array $aMailbox (reference) mailbox retrieved from sqm_api_mailbox_select
259 * @return error $error error number
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 $aHeaderFields = array();
300 foreach ($aFetchHeaders as $v) {
302 case SQM_COL_DATE
: $aHeaderFields[] = 'Date'; break;
303 case SQM_COL_TO
: $aHeaderFields[] = 'To'; break;
304 case SQM_COL_CC
: $aHeaderFields[] = 'Cc'; break;
305 case SQM_COL_FROM
: $aHeaderFields[] = 'From'; break;
306 case SQM_COL_SUBJ
: $aHeaderFields[] = 'Subject'; break;
307 case SQM_COL_PRIO
: $aHeaderFields[] = 'X-Priority'; break;
308 case SQM_COL_ATTACHMENT
: $aHeaderFields[] = 'Content-Type'; break;
309 case SQM_COL_INT_DATE
: $aFetchItems[] = 'INTERNALDATE'; break;
310 case SQM_COL_FLAGS
: $aFetchItems[] = 'FLAGS'; break;
311 case SQM_COL_SIZE
: $aFetchItems[] = 'RFC822.SIZE'; break;
317 * A uidset with sorted uid's is available. We can use the cache
319 if (isset($aUid) && $aUid ) {
320 // limit the cache to SQM_MAX_PAGES_IN_CACHE
321 if (!$aMailbox['SHOWALL'][$iSetIndx] && isset($aMailbox['MSG_HEADERS'])) {
322 $iMaxMsgs = $iLimit * SQM_MAX_PAGES_IN_CACHE
;
323 $iCacheSize = count($aMailbox['MSG_HEADERS']);
324 if ($iCacheSize > $iMaxMsgs) {
325 $iReduce = $iCacheSize - $iMaxMsgs;
326 foreach ($aMailbox['MSG_HEADERS'] as $iUid => $value) {
328 unset($aMailbox['MSG_HEADERS'][$iUid]);
337 $id_slice = array_slice($aUid,$start_msg-1,$iLimit);
338 /* do some funky cache checks */
339 if (isset($aMailbox['MSG_HEADERS']) && is_array($aMailbox['MSG_HEADERS'])) {
340 // temp code, read_body del / next links fo not update fields.
341 foreach ($aMailbox['MSG_HEADERS'] as $iUid => $aValue) {
342 if (!isset($aValue['UID'])) {
343 unset($aMailbox['MSG_HEADERS'][$iUid]);
346 $aUidCached = array_keys($aMailbox['MSG_HEADERS']);
348 $aMailbox['MSG_HEADERS'] = array();
349 $aUidCached = array();
351 $aUidNotCached = array_values(array_diff($id_slice,$aUidCached));
354 * $aUidNotCached contains an array with UID's which need to be fetched to
355 * complete the needed message headers.
357 if (count($aUidNotCached)) {
358 $aMsgs = sqimap_get_small_header_list($imapConnection,$aUidNotCached,
359 $aHeaderFields,$aFetchItems);
360 // append the msgs to the existend headers
361 $aMailbox['MSG_HEADERS'] +
= $aMsgs;
365 * Initialize the sorted UID list or initiate a UID list with search
366 * results and fetch the visible message headers
369 if ($aMailbox['SEARCH'][$iSetIndx] != 'ALL') { // in case of a search request
371 if ($aMailbox['SEARCH'][$iSetIndx] && $aMailbox['SORT'] == 0) {
372 $aUid = sqimap_run_search($imapConnection, $aMailbox['SEARCH'][$iSetIndx], $aMailbox['CHARSET'][$iSetIndx]);
376 $iError = _get_sorted_msgs_list($imapConnection,$aMailbox,$iError);
377 $aUid = $aMailbox['UIDSET'][$iSetIndx];
381 * Number of messages is the resultset
383 $aMailbox['TOTAL'][$iSetIndx] = count($aUid);
384 $id_slice = array_slice($aUid,$aMailbox['OFFSET'], $iLimit);
385 if (count($id_slice)) {
386 $aMailbox['MSG_HEADERS'] = sqimap_get_small_header_list($imapConnection,$id_slice,
387 $aHeaderFields,$aFetchItems);
389 $iError = 1; // FIX ME, define an error code
394 $iError = _get_sorted_msgs_list($imapConnection,$aMailbox,$iError);
395 $aUid = $aMailbox['UIDSET'][$iSetIndx];
399 * Number of messages is the resultset
401 $aMailbox['TOTAL'][$iSetIndx] = count($aUid);
402 $id_slice = array_slice($aUid,$aMailbox['OFFSET'], $iLimit);
403 if (count($id_slice)) {
404 $aMailbox['MSG_HEADERS'] = sqimap_get_small_header_list($imapConnection,$id_slice,
405 $aHeaderFields,$aFetchItems);
407 $iError = 1; // FIX ME, define an error code
416 * Prepares the message headers for display inside a template. The links are calculated,
417 * color for row highlighting is calculated and optionally the strings are truncated.
419 * @param array $aMailbox (reference) mailbox retrieved from sqm_api_mailbox_select
420 * @param array $aProps properties
421 * @return array $aFormattedMessages array with message headers and format info
423 * @author Marc Groot Koerkamp
425 function prepareMessageList(&$aMailbox, $aProps) {
427 /* Globalize link attributes so plugins can share in modifying them */
428 global $link, $title, $target, $onclick, $link_extra;
430 /* retrieve the properties */
431 $my_email_address = (isset($aProps['email'])) ?
$aProps['email'] : false;
432 $highlight_list = (isset($aProps['config']['highlight_list'])) ?
$aProps['config']['highlight_list'] : false;
433 $aColumnDesc = (isset($aProps['columns'])) ?
$aProps['columns'] : false;
434 $aExtraColumns = (isset($aProps['extra_columns'])) ?
$aProps['extra_columns'] : array();
435 $iAccount = (isset($aProps['account'])) ?
(int) $aProps['account'] : 0;
436 $sMailbox = (isset($aProps['mailbox'])) ?
$aProps['mailbox'] : false;
437 $sTargetModule = (isset($aProps['module'])) ?
$aProps['module'] : 'read_body';
440 * TODO 1, retrieve array with identity email addresses in order to match against to,cc and set a flag
441 * $aFormattedMessages[$iUid]['match_identity'] = true
442 * The template can show some image if there is a match.
443 * TODO 2, makes sure the matching is done fast by doing a strpos call on the returned $value
447 * Only retrieve values for displayable columns
449 foreach ($aColumnDesc as $k => $v) {
451 case SQM_COL_FROM
: $aCol[SQM_COL_FROM
] = 'from'; break;
452 case SQM_COL_DATE
: $aCol[SQM_COL_DATE
] = 'date'; break;
453 case SQM_COL_SUBJ
: $aCol[SQM_COL_SUBJ
] = 'subject'; break;
454 case SQM_COL_FLAGS
: $aCol[SQM_COL_FLAGS
] = 'FLAGS'; break;
455 case SQM_COL_SIZE
: $aCol[SQM_COL_SIZE
] = 'SIZE'; break;
456 case SQM_COL_PRIO
: $aCol[SQM_COL_PRIO
] = 'x-priority'; break;
457 case SQM_COL_ATTACHMENT
: $aCol[SQM_COL_ATTACHMENT
] = 'content-type'; break;
458 case SQM_COL_INT_DATE
: $aCol[SQM_COL_INT_DATE
] = 'INTERNALDATE'; break;
459 case SQM_COL_TO
: $aCol[SQM_COL_TO
] = 'to'; break;
460 case SQM_COL_CC
: $aCol[SQM_COL_CC
] = 'cc'; break;
461 case SQM_COL_BCC
: $aCol[SQM_COL_BCC
] = 'bcc'; break;
465 $aExtraHighLightColumns = array();
466 foreach ($aExtraColumns as $v) {
468 case SQM_COL_FROM
: $aExtraHighLightColumns[] = 'from'; break;
469 case SQM_COL_SUBJ
: $aExtraHighLightColumns[] = 'subject'; break;
470 case SQM_COL_TO
: $aExtraHighLightColumns[] = 'to'; break;
471 case SQM_COL_CC
: $aExtraHighLightColumns[] = 'cc'; break;
472 case SQM_COL_BCC
: $aExtraHighLightColumns[] = 'bcc'; break;
476 $aFormattedMessages = array();
479 $iSetIndx = $aMailbox['SETINDEX'];
480 $aId = $aMailbox['UIDSET'][$iSetIndx];
481 $aHeaders =& $aMailbox['MSG_HEADERS']; /* use a reference to avoid a copy.
482 MSG_HEADERS can contain large amounts of data */
483 $iOffset = $aMailbox['OFFSET'];
484 $sort = $aMailbox['SORT'];
485 $iPageOffset = $aMailbox['PAGEOFFSET'];
486 $sMailbox = $aMailbox['NAME'];
487 $sSearch = (isset($aMailbox['SEARCH'][$aMailbox['SETINDEX']]) &&
488 $aMailbox['SEARCH'][$aMailbox['SETINDEX']] != 'ALL') ?
$aMailbox['SEARCH'][$aMailbox['SETINDEX']] : false;
489 $aSearch = ($sSearch) ?
array('search.php',$aMailbox['SETINDEX']) : null;
490 /* avoid improper usage */
491 if ($sMailbox && isset($iAccount) && $sTargetModule) {
492 $aInitQuery = array("account=$iAccount",'mailbox='.urlencode($sMailbox));
497 if ($aMailbox['SORT'] & SQSORT_THREAD
) {
498 $aIndentArray =& $aMailbox['THREAD_INDENT'][$aMailbox['SETINDEX']];
504 * Retrieve value for checkbox column
506 if (!sqgetGlobalVar('checkall',$checkall,SQ_GET
)) {
511 * Loop through and display the info for each message.
513 $iEnd = ($aMailbox['SHOWALL'][$iSetIndx]) ?
$aMailbox['EXISTS'] : $iOffset +
$aMailbox['LIMIT'];
514 for ($i=$iOffset,$t=0;$i<$iEnd;++
$i) {
515 if (isset($aId[$i])) {
518 $value = $title = $link = $target = $onclick = $link_extra = '';
519 $aQuery = ($aInitQuery !== false) ?
$aInitQuery : false;
520 $aMsg = $aHeaders[$aId[$i]];
521 if (isset($aSearch) && count($aSearch) > 1 && $aQuery) {
522 $aQuery[] = "where=". $aSearch[0];
523 $aQuery[] = "what=" . $aSearch[1];
525 $iUid = (isset($aMsg['UID'])) ?
$aMsg['UID'] : $aId[$i];
527 $aQuery[] = "passed_id=$aId[$i]";
528 $aQuery[] = "startMessage=$iPageOffset";
531 foreach ($aCol as $k => $v) {
532 $title = $link = $target = $onclick = $link_extra = '';
533 $aColumns[$k] = array();
534 $value = (isset($aMsg[$v])) ?
$aMsg[$v] : '';
535 $sUnknown = _("Unknown recipient");
538 $sUnknown = _("Unknown sender");
544 if ($highlight_list && !$bHighLight) {
545 $bHighLight = highlightMessage($aCol[$k], $value, $highlight_list,$aFormattedMessages[$iUid]);
547 $aAddressList = parseRFC822Address($value);
548 $sTmp = getAddressString($aAddressList,array('best' => true));
549 $title = $title_maybe = '';
550 foreach ($aAddressList as $aAddr) {
551 $sPersonal = (isset($aAddr[SQM_ADDR_PERSONAL
])) ?
$aAddr[SQM_ADDR_PERSONAL
] : '';
552 $sMailbox = (isset($aAddr[SQM_ADDR_MAILBOX
])) ?
$aAddr[SQM_ADDR_MAILBOX
] : '';
553 $sHost = (isset($aAddr[SQM_ADDR_HOST
])) ?
$aAddr[SQM_ADDR_HOST
] : '';
555 $title .= htmlspecialchars($sMailbox.'@'.$sHost).', ';
557 // if $value gets truncated we need to add the addresses with no
558 // personal name as well
559 $title_maybe .= htmlspecialchars($sMailbox.'@'.$sHost).', ';
563 $title = substr($title,0,-2); // strip ', ';
565 $sTmp = decodeHeader($sTmp);
566 if (isset($aColumnDesc[$k]['truncate']) && $aColumnDesc[$k]['truncate']) {
567 $sTrunc = truncateWithEntities($sTmp, $aColumnDesc[$k]['truncate']);
568 if ($sTrunc != $sTmp) {
570 $title = htmlspecialchars($sTmp);
571 } else if ($title_maybe) {
572 $title = $title .', '.$title_maybe;
573 $title = substr($title,0,-2); // strip ', ';
579 $value = ($sTmp) ?
$sTmp : $sUnknown;
582 // subject is mime encoded, decode it.
583 // value is sanitized in decoding function.
584 // TODO, verify if it should be done before or after the highlighting
585 $value=decodeHeader($value);
586 if ($highlight_list && !$bHighLight) {
587 $bHighLight = highlightMessage('SUBJECT', $value, $highlight_list, $aFormattedMessages[$iUid]);
589 $iIndent = (isset($aIndentArray[$aId[$i]])) ?
$aIndentArray[$aId[$i]] : 0;
590 // FIXME: don't break 8bit symbols and html entities during truncation
591 if (isset($aColumnDesc[$k]['truncate']) && $aColumnDesc[$k]['truncate']) {
592 $sTmp = truncateWithEntities($value, $aColumnDesc[$k]['truncate']-$iIndent);
593 // drop any double spaces since these will be displayed in the title
594 $title = ($sTmp != $value) ?
preg_replace('/\s{2,}/', ' ', $value) : '';
597 /* generate the link to the message */
599 // TODO, $sTargetModule should be a query parameter so that we can use a single entrypoint
600 $link = $sTargetModule.'.php?' . implode('&',$aQuery);
602 // see top of this function for which attributes are available
603 // in the global scope for plugin use (like $link, $target,
604 // $onclick, $link_extra, $title, and so forth)
605 // plugins are responsible for sharing nicely (such as for
606 // setting the target, etc)
607 do_hook('subject_link', array($iPageOffset, $sSearch, $aSearch));
609 $value = (trim($value)) ?
$value : _("(no subject)");
610 /* add thread indentation */
611 $aColumns[$k]['indent'] = $iIndent;
614 $value = show_readable_size($value);
617 case SQM_COL_INT_DATE
:
618 $value = getDateString(getTimeStamp(explode(' ',trim($value))));
621 $aFlagColumn = array('seen' => false,
627 if(!is_array($value)) $value = array();
628 foreach ($value as $sFlag => $value) {
630 case '\\seen' : $aFlagColumn['seen'] = true; break;
631 case '\\deleted' : $aFlagColumn['deleted'] = true; break;
632 case '\\answered': $aFlagColumn['answered'] = true; break;
633 case '\\flagged' : $aFlagColumn['flagged'] = true; break;
634 case '\\draft' : $aFlagColumn['draft'] = true; break;
638 $value = $aFlagColumn;
641 $value = ($value) ?
(int) $value : 3;
643 case SQM_COL_ATTACHMENT
:
644 $value = (is_array($value) && $value[0] == 'multipart' && $value[1] == 'mixed') ?
true : false;
651 if ($title) { $aColumns[$k]['title'] = $title; }
652 if ($link) { $aColumns[$k]['link'] = $link; }
653 if ($link_extra) { $aColumns[$k]['link_extra'] = $link_extra; }
654 if ($onclick) { $aColumns[$k]['onclick'] = $onclick; }
655 if ($target) { $aColumns[$k]['target'] = $target; }
656 $aColumns[$k]['value'] = $value;
658 /* columns which will not be displayed but should be inspected
659 because the highlight list contains rules with those columns */
660 foreach ($aExtraHighLightColumns as $v) {
661 if ($highlight_list && !$bHighLight && isset($aMsg[$v])) {
662 $bHighLight = highlightMessage($v, $aMsg[$v], $highlight_list,$aFormattedMessages[$iUid]);
665 $aFormattedMessages[$iUid]['columns'] = $aColumns;
671 return $aFormattedMessages;
676 * Sets the row color if the provided column value pair matches a hightlight rule
678 * @param string $sCol column name
679 * @param string $sVal column value
680 * @param array $highlight_list highlight rules
681 * @param array $aFormat (reference) array where row color info is stored
682 * @return bool match found
684 * @author Marc Groot Koerkamp
686 function highlightMessage($sCol, $sVal, $highlight_list, &$aFormat) {
687 if (!is_array($highlight_list) && count($highlight_list) == 0) {
691 $sCol = strtoupper($sCol);
693 foreach ($highlight_list as $highlight_list_part) {
694 if (trim($highlight_list_part['value'])) {
695 $high_val = strtolower($highlight_list_part['value']);
696 $match_type = strtoupper($highlight_list_part['match_type']);
697 if($match_type == 'TO_CC') {
698 if ($sCol == 'TO' ||
$sCol == 'CC') {
704 if ($match_type != $sCol) {
708 if (strpos(strtolower($sVal),$high_val) !== false) {
709 $hlt_color = $highlight_list_part['color'];
715 // Bug in highlight color???
716 if ($hlt_color{0} != '#') {
717 $hlt_color = '#'. $hlt_color;
719 $aFormat['row']['color'] = $hlt_color;
726 function setUserPref($username, $pref, $value) {
728 setPref($data_dir,$username,$pref,$value);
732 * Execute the sorting for a mailbox
734 * @param resource $imapConnection Imap connection
735 * @param array $aMailbox (reference) Mailbox retrieved with sqm_api_mailbox_select
736 * @return int $error (reference) Error number
739 * @author Marc Groot Koerkamp
741 function _get_sorted_msgs_list($imapConnection,&$aMailbox) {
742 $iSetIndx = (isset($aMailbox['SETINDEX'])) ?
$aMailbox['SETINDEX'] : 0;
743 $bDirection = !($aMailbox['SORT'] %
2);
745 if (!$aMailbox['SEARCH'][$iSetIndx]) {
746 $aMailbox['SEARCH'][$iSetIndx] = 'ALL';
748 if (($aMailbox['SORT'] & SQSORT_THREAD
) && sqimap_capability($imapConnection,'THREAD')) {
749 $aRes = get_thread_sort($imapConnection,$aMailbox['SEARCH'][$iSetIndx]);
750 if ($aRes === false) {
751 $aMailbox['SORT'] -= SQSORT_THREAD
;
752 $error = 1; // fix me, define an error code;
754 $aMailbox['UIDSET'][$iSetIndx] = $aRes[0];
755 $aMailbox['THREAD_INDENT'][$iSetIndx] = $aRes[1];
757 } else if ($aMailbox['SORT'] === SQSORT_NONE
) {
758 $id = sqimap_run_search($imapConnection, 'ALL' , '');
760 $error = 1; // fix me, define an error code
762 $aMailbox['UIDSET'][$iSetIndx] = array_reverse($id);
763 $aMailbox['TOTAL'][$iSetIndx] = $aMailbox['EXISTS'];
766 if (sqimap_capability($imapConnection,'SORT')) {
767 $sSortField = _getSortField($aMailbox['SORT'],true);
768 $id = sqimap_get_sort_order($imapConnection, $sSortField, $bDirection, $aMailbox['SEARCH'][$iSetIndx]);
770 $error = 1; // fix me, define an error code
772 $aMailbox['UIDSET'][$iSetIndx] = $id;
776 if ($aMailbox['SEARCH'][$iSetIndx] != 'ALL') {
777 $id = sqimap_run_search($imapConnection, $aMailbox['SEARCH'][$iSetIndx], $aMailbox['CHARSET'][$iSetIndx]);
779 $sSortField = _getSortField($aMailbox['SORT'],false);
780 $aMailbox['UIDSET'][$iSetIndx] = get_squirrel_sort($imapConnection, $sSortField, $bDirection, $id);
787 * Does the $srt $_GET var to field mapping
789 * @param int $srt Field to sort on
790 * @param bool $bServerSort Server sorting is true
791 * @return string $sSortField Field to sort on
795 function _getSortField($sort,$bServerSort) {
800 case SQSORT_DATE_ASC
:
801 case SQSORT_DATE_DESC
:
802 $sSortField = 'DATE';
804 case SQSORT_FROM_ASC
:
805 case SQSORT_FROM_DESC
:
806 $sSortField = 'FROM';
808 case SQSORT_SUBJ_ASC
:
809 case SQSORT_SUBJ_DESC
:
810 $sSortField = 'SUBJECT';
812 case SQSORT_SIZE_ASC
:
813 case SQSORT_SIZE_DESC
:
814 $sSortField = ($bServerSort) ?
'SIZE' : 'RFC822.SIZE';
824 case SQSORT_INT_DATE_ASC
:
825 case SQSORT_INT_DATE_DESC
:
826 $sSortField = ($bServerSort) ?
'ARRIVAL' : 'INTERNALDATE';
830 default: $sSortField = 'UID';
838 * This function is a utility function for setting which headers should be
839 * fetched. It takes into account the highlight list which requires extra
840 * headers to be fetch in order to make those rules work. It's called before
841 * the headers are fetched which happens in showMessagesForMailbox and when
842 * the next and prev links in read_body.php are used.
844 * @param array $aMailbox associative array with mailbox related vars
845 * @param array $aProps
850 function calcFetchColumns(&$aMailbox, &$aProps) {
852 $highlight_list = (isset($aProps['config']['highlight_list'])) ?
$aProps['config']['highlight_list'] : false;
853 $aColumnsDesc = (isset($aProps['columns'])) ?
$aProps['columns'] : false;
855 $aFetchColumns = $aColumnsDesc;
856 if (isset($aFetchColumns[SQM_COL_CHECK
])) {
857 unset($aFetchColumns[SQM_COL_CHECK
]);
861 * Before we fetch the message headers, check if we need to fetch extra columns
862 * to make the message highlighting work
864 if (is_array($highlight_list) && count($highlight_list)) {
865 $aHighlightColumns = array();
866 foreach ($highlight_list as $highlight_list_part) {
867 if (trim($highlight_list_part['value'])) {
868 $match_type = strtoupper($highlight_list_part['match_type']);
869 switch ($match_type) {
871 $aHighlightColumns[SQM_COL_TO
] = true;
872 $aHighlightColumns[SQM_COL_CC
] = true;
874 case 'TO': $aHighlightColumns[SQM_COL_TO
] = true; break;
875 case 'CC': $aHighlightColumns[SQM_COL_CC
] = true; break;
876 case 'FROM': $aHighlightColumns[SQM_COL_FROM
] = true; break;
877 case 'SUBJECT':$aHighlightColumns[SQM_COL_SUBJ
] = true; break;
881 $aExtraColumns = array();
882 foreach ($aHighlightColumns as $k => $v) {
883 if (!isset($aFetchColumns[$k])) {
884 $aExtraColumns[] = $k;
885 $aFetchColumns[$k] = true;
888 if (count($aExtraColumns)) {
889 $aProps['extra_columns'] = $aExtraColumns;
892 $aMailbox['FETCHHEADERS'] = array_keys($aFetchColumns);
897 * This function loops through a group of messages in the mailbox
898 * and shows them to the user.
900 * @param resource $imapConnection
901 * @param array $aMailbox associative array with mailbox related vars
902 * @param array $aProps
903 * @param int $iError error code, 0 is no error
905 function showMessagesForMailbox($imapConnection, &$aMailbox,$aProps, &$iError) {
907 global $boxes, $show_copy_buttons;
909 $highlight_list = (isset($aProps['config']['highlight_list'])) ?
$aProps['config']['highlight_list'] : false;
910 $fancy_index_highlite = (isset($aProps['config']['fancy_index_highlite'])) ?
$aProps['config']['fancy_index_highlite'] : true;
911 $aColumnsDesc = (isset($aProps['columns'])) ?
$aProps['columns'] : false;
912 $iAccount = (isset($aProps['account'])) ?
(int) $aProps['account'] : 0;
913 $sMailbox = (isset($aProps['mailbox'])) ?
$aProps['mailbox'] : false;
914 $sTargetModule = (isset($aProps['module'])) ?
$aProps['module'] : 'read_body';
915 $show_flag_buttons = (isset($aProps['config']['show_flag_buttons'])) ?
$aProps['config']['show_flag_buttons'] : true;
917 /* allows to control copy button in function call. If array key is not set, code follows user preferences */
918 if (isset($aProps['config']['show_copy_buttons']))
919 $show_copy_buttons = $aProps['config']['show_copy_buttons'];
921 $lastTargetMailbox = (isset($aProps['config']['lastTargetMailbox'])) ?
$aProps['config']['lastTargetMailbox'] : '';
922 $aOrder = array_keys($aProps['columns']);
923 $trash_folder = (isset($aProps['config']['trash_folder']) && $aProps['config']['trash_folder'])
924 ?
$aProps['config']['trash_folder'] : false;
925 $sent_folder = (isset($aProps['config']['sent_folder']) && $aProps['config']['sent_folder'])
926 ?
$aProps['config']['sent_folder'] : false;
927 $draft_folder = (isset($aProps['config']['draft_folder']) && $aProps['config']['draft_folder'])
928 ?
$aProps['config']['draft_folder'] : false;
929 $page_selector = (isset($aProps['config']['page_selector'])) ?
$aProps['config']['page_selector'] : false;
930 $page_selector_max = (isset($aProps['config']['page_selector_max'])) ?
$aProps['config']['page_selector_max'] : 10;
931 $color = $aProps['config']['color'];
939 if (!isset($iFormId)) {
944 // store the columns to fetch so we can pick them up in read_body
945 // where we validate the cache.
946 calcFetchColumns($aMailbox ,$aProps);
948 $iError = fetchMessageHeaders($imapConnection, $aMailbox);
952 $aMessages = prepareMessageList($aMailbox, $aProps);
955 $iSetIndx = $aMailbox['SETINDEX'];
956 $iLimit = ($aMailbox['SHOWALL'][$iSetIndx]) ?
$aMailbox['EXISTS'] : $aMailbox['LIMIT'];
957 $iEnd = ($aMailbox['PAGEOFFSET'] +
($iLimit - 1) < $aMailbox['EXISTS']) ?
958 $aMailbox['PAGEOFFSET'] +
$iLimit - 1 : $aMailbox['EXISTS'];
960 $iNumberOfMessages = $aMailbox['TOTAL'][$iSetIndx];
961 $iEnd = min ( $iEnd, $iNumberOfMessages );
963 $php_self = $PHP_SELF;
965 $urlMailbox = urlencode($aMailbox['NAME']);
967 if (preg_match('/^(.+)\?.+$/',$php_self,$regs)) {
968 $source_url = $regs[1];
970 $source_url = $php_self;
973 $baseurl = $source_url.'?mailbox=' . urlencode($aMailbox['NAME']) .'&account='.$aMailbox['ACCOUNT'];
974 $where = urlencode($aMailbox['SEARCH'][$iSetIndx][0]);
975 $what = urlencode($aMailbox['SEARCH'][$iSetIndx][1]);
976 $baseurl .= '&where=' . $where . '&what=' . $what;
978 /* build thread sorting links */
979 $newsort = $aMailbox['SORT'];
980 if (sqimap_capability($imapConnection,'THREAD')) {
981 if ($aMailbox['SORT'] & SQSORT_THREAD
) {
982 $newsort -= SQSORT_THREAD
;
983 $thread_name = _("Unthread View");
985 $thread_name = _("Thread View");
986 $newsort = $aMailbox['SORT'] + SQSORT_THREAD
;
988 $thread_link_str = '<small>[<a href="' . $baseurl . '&srt='
989 . $newsort . '&startMessage=1">' . $thread_name
992 $thread_link_str ='';
994 $sort = $aMailbox['SORT'];
996 /* FIX ME ADD CHECKBOX CONTROL. No checkbox => no buttons */
1000 /* future admin control over displayable buttons */
1001 $aAdminControl = array(
1002 'markUnflagged' => 1,
1008 'undeleteButton'=> 1,
1009 'bypass_trash' => 1,
1010 'expungeButton' => 1,
1015 /* user prefs control */
1016 $aUserControl = array (
1018 'markUnflagged' => $show_flag_buttons,
1019 'markFlagged' => $show_flag_buttons,
1024 'undeleteButton'=> 1,
1025 'bypass_trash' => 1,
1026 'expungeButton' => 1,
1028 'copyButton' => $show_copy_buttons
1032 $showDelete = ($aMailbox['RIGHTS'] != 'READ-ONLY' &&
1033 in_array('\\deleted',$aMailbox['PERMANENTFLAGS'], true)) ?
true : false;
1034 $showByPassTrash = (($aMailbox['AUTO_EXPUNGE'] && $aMailbox['RIGHTS'] != 'READ-ONLY' &&
1035 in_array('\\deleted',$aMailbox['PERMANENTFLAGS'], true)) &&
1036 $trash_folder) ?
true : false; //
1038 $showUndelete = (!$aMailbox['AUTO_EXPUNGE'] && $aMailbox['RIGHTS'] != 'READ-ONLY' &&
1039 in_array('\\deleted',$aMailbox['PERMANENTFLAGS'], true) && !$trash_folder) ?
true : false;
1040 $showMove = ($aMailbox['RIGHTS'] != 'READ-ONLY') ?
true : false;
1041 $showExpunge = (!$aMailbox['AUTO_EXPUNGE'] && $aMailbox['RIGHTS'] != 'READ-ONLY' &&
1042 in_array('\\deleted',$aMailbox['PERMANENTFLAGS'], true)) ?
true : false;
1044 /* Button options that depend on IMAP server and selected folder */
1045 $aImapControl = array (
1046 'markUnflagged' => in_array('\\flagged',$aMailbox['PERMANENTFLAGS'], true),
1047 'markFlagged' => in_array('\\flagged',$aMailbox['PERMANENTFLAGS'], true),
1048 'markRead' => in_array('\\seen',$aMailbox['PERMANENTFLAGS'], true),
1049 'markUnread' => in_array('\\seen',$aMailbox['PERMANENTFLAGS'], true),
1051 'delete' => $showDelete,
1052 'undeleteButton'=> $showUndelete,
1053 'bypass_trash' => $showByPassTrash,
1054 'expungeButton' => $showExpunge,
1055 'moveButton' => $showMove,
1058 /* Button strings */
1059 $aButtonStrings = array(
1060 'markUnflagged' => _("Unflag"),
1061 'markFlagged' => _("Flag"),
1062 'markRead' => _("Read"),
1063 'markUnread' => _("Unread"),
1064 'forward' => _("Forward"),
1065 'delete' => _("Delete"),
1066 'undeleteButton' => _("Undelete"),
1067 'bypass_trash' => _("Bypass Trash"),
1068 'expungeButton' => _("Expunge"),
1069 'moveButton' => _("Move"),
1070 'copyButton' => _("Copy")
1075 * Register buttons in order to an array
1076 * The key is the "name", the first element of the value array is the "value", second argument is the type.
1078 $aFormElements = array();
1079 foreach($aAdminControl as $k => $v) {
1080 if ($v & $aUserControl[$k] & $aImapControl[$k]) {
1082 case 'markUnflagged':
1087 case 'undeleteButton':
1088 case 'expungeButton':
1090 $aFormElements[$k] = array($aButtonStrings[$k],'submit');
1092 case 'bypass_trash':
1093 $aFormElements[$k] = array($aButtonStrings[$k],'checkbox');
1097 $aFormElements['targetMailbox'] =
1098 array(sqimap_mailbox_option_list($imapConnection, array(strtolower($lastTargetMailbox)), 0, $boxes),'select');
1099 $aFormElements['mailbox'] = array($aMailbox['NAME'],'hidden');
1100 $aFormElements['startMessage'] = array($aMailbox['PAGEOFFSET'],'hidden');
1101 $aFormElements[$k] = array($aButtonStrings[$k],'submit');
1105 $aFormElements['account'] = array($iAccount,'hidden');
1109 * This is the beginning of the message list table.
1110 * It wraps around all messages
1112 $safe_name = preg_replace("/[^0-9A-Za-z_]/", '_', $aMailbox['NAME']);
1113 $form_name = "FormMsgs" . $safe_name;
1115 //if (!sqgetGlobalVar('align',$align,SQ_SESSION)) {
1116 $align = array('left' => 'left', 'right' => 'right');
1118 //sm_print_r($align);
1120 /* finally set the template vars */
1122 // FIX ME, before we support multiple templates we must review the names of the vars
1125 $aTemplate['color'] = $color;
1126 $aTemplate['form_name'] = "FormMsgs" . $safe_name;
1127 $aTemplate['form_id'] = 'mbx_'.$iFormId;
1128 $aTemplate['page_selector'] = $page_selector;
1129 $aTemplate['page_selector_max'] = $page_selector_max;
1130 $aTemplate['messagesPerPage'] = $aMailbox['LIMIT'];
1131 $aTemplate['showall'] = $aMailbox['SHOWALL'][$iSetIndx];
1132 $aTemplate['end_msg'] = $iEnd;
1133 $aTemplate['align'] = $align;
1134 $aTemplate['iNumberOfMessages'] = $iNumberOfMessages;
1135 $aTemplate['aOrder'] = $aOrder;
1136 $aTemplate['aFormElements'] = $aFormElements;
1137 $aTemplate['sort'] = $sort;
1138 $aTemplate['pageOffset'] = $aMailbox['PAGEOFFSET'];
1139 $aTemplate['baseurl'] = $baseurl;
1140 $aTemplate['aMessages'] =& $aMessages;
1141 $aTemplate['trash_folder'] = $trash_folder;
1142 $aTemplate['sent_folder'] = $sent_folder;
1143 $aTemplate['draft_folder'] = $draft_folder;
1144 $aTemplate['thread_link_str'] = $thread_link_str;
1145 $aTemplate['php_self'] = str_replace('&','&',$php_self);
1146 $aTemplate['mailbox'] = $sMailbox;
1147 $aTemplate['javascript_on'] = (isset($aProps['config']['javascript_on'])) ?
$aProps['config']['javascript_on'] : false;
1148 $aTemplate['enablesort'] = (isset($aProps['config']['enablesort'])) ?
$aProps['config']['enablesort'] : false;
1149 $aTemplate['icon_theme'] = (isset($aProps['config']['icon_theme'])) ?
$aProps['config']['icon_theme'] : false;
1150 $aTemplate['use_icons'] = (isset($aProps['config']['use_icons'])) ?
$aProps['config']['use_icons'] : false;
1151 $aTemplate['alt_index_colors'] = (isset($aProps['config']['alt_index_colors'])) ?
$aProps['config']['alt_index_colors'] : false;
1152 $aTemplate['fancy_index_highlite'] = $fancy_index_highlite;
1160 * Truncates a string and take care of html encoded characters
1162 * @param string $s string to truncate
1163 * @param int $iTrimAt Trim at nn characters
1164 * @return string Trimmed string
1166 function truncateWithEntities($s, $iTrimAt) {
1167 global $languages, $squirrelmail_language;
1169 $ent_strlen = strlen($s);
1170 if (($iTrimAt <= 0) ||
($ent_strlen <= $iTrimAt))
1173 if (isset($languages[$squirrelmail_language]['XTRA_CODE']) &&
1174 function_exists($languages[$squirrelmail_language]['XTRA_CODE'] . '_strimwidth')) {
1175 return call_user_func($languages[$squirrelmail_language]['XTRA_CODE'] . '_strimwidth', $s, $iTrimAt);
1178 * see if this is entities-encoded string
1179 * If so, Iterate through the whole string, find out
1180 * the real number of characters, and if more
1181 * than $iTrimAt, substr with an updated trim value.
1183 $trim_val = $iTrimAt;
1186 while ( $ent_loc < $trim_val && (($ent_loc = strpos($s, '&', $ent_offset)) !== false) &&
1187 (($ent_loc_end = strpos($s, ';', $ent_loc+
3)) !== false) ) {
1188 $trim_val +
= ($ent_loc_end-$ent_loc);
1189 $ent_offset = $ent_loc_end+
1;
1192 if (($trim_val > $iTrimAt) && ($ent_strlen > $trim_val) && (strpos($s,';',$trim_val) < ($trim_val +
6))) {
1193 $i = strpos($s,';',$trim_val);
1195 $trim_val = strpos($s,';',$trim_val)+
1;
1198 // only print '...' when we're actually dropping part of the subject
1199 if ($ent_strlen <= $trim_val)
1202 return substr_replace($s, '...', $trim_val);
1207 * This should go in imap_mailbox.php
1208 * @param string $mailbox
1210 function handleAsSent($mailbox) {
1211 global $handleAsSent_result;
1213 /* First check if this is the sent or draft folder. */
1214 $handleAsSent_result = isSentMailbox($mailbox) ||
isDraftMailbox($mailbox);
1216 /* Then check the result of the handleAsSent hook. */
1217 do_hook('check_handleAsSent_result', $mailbox);
1219 /* And return the result. */
1220 return $handleAsSent_result;
1224 * Process messages list form and handle the cache gracefully. If $sButton and
1225 * $aUid are provided as argument then you can fake a message list submit and
1226 * use it i.e. in read_body.php for del move next and update the cache
1228 * @param resource $imapConnection imap connection
1229 * @param array $aMailbox (reference) cached mailbox
1230 * @param string $sButton fake a submit button
1231 * @param array $aUid fake the $msg array
1232 * @return string $sError error string in case of an error
1234 * @author Marc Groot Koerkamp
1236 function handleMessageListForm($imapConnection,&$aMailbox,$sButton='',$aUid = array()) {
1237 /* incoming formdata */
1238 $sButton = (sqgetGlobalVar('moveButton', $sTmp, SQ_POST
)) ?
'move' : $sButton;
1239 $sButton = (sqgetGlobalVar('copyButton', $sTmp, SQ_POST
)) ?
'copy' : $sButton;
1240 $sButton = (sqgetGlobalVar('expungeButton', $sTmp, SQ_POST
)) ?
'expunge' : $sButton;
1241 $sButton = (sqgetGlobalVar('forward', $sTmp, SQ_POST
)) ?
'forward' : $sButton;
1242 $sButton = (sqgetGlobalVar('delete', $sTmp, SQ_POST
)) ?
'setDeleted' : $sButton;
1243 $sButton = (sqgetGlobalVar('undeleteButton', $sTmp, SQ_POST
)) ?
'unsetDeleted' : $sButton;
1244 $sButton = (sqgetGlobalVar('markRead', $sTmp, SQ_POST
)) ?
'setSeen' : $sButton;
1245 $sButton = (sqgetGlobalVar('markUnread', $sTmp, SQ_POST
)) ?
'unsetSeen' : $sButton;
1246 $sButton = (sqgetGlobalVar('markFlagged', $sTmp, SQ_POST
)) ?
'setFlagged' : $sButton;
1247 $sButton = (sqgetGlobalVar('markUnflagged', $sTmp, SQ_POST
)) ?
'unsetFlagged' : $sButton;
1248 sqgetGlobalVar('targetMailbox', $targetMailbox, SQ_POST
);
1249 sqgetGlobalVar('bypass_trash', $bypass_trash, SQ_POST
);
1250 sqgetGlobalVar('msg', $msg, SQ_POST
);
1251 if (sqgetGlobalVar('account', $iAccount, SQ_POST
) === false) {
1255 $mailbox = $aMailbox['NAME'];
1257 /* retrieve the check boxes */
1258 $aUid = (isset($msg) && is_array($msg)) ?
array_values($msg) : $aUid;
1259 if (count($aUid) && $sButton != 'expunge') {
1260 $aUpdatedMsgs = false;
1264 // check if id exists in case we come from read_body
1265 if (count($aUid) == 1 && is_array($aMailbox['UIDSET'][$aMailbox['SETINDEX']]) &&
1266 !in_array($aUid[0],$aMailbox['UIDSET'][$aMailbox['SETINDEX']])) {
1269 $aUpdatedMsgs = sqimap_msgs_list_delete($imapConnection, $mailbox, $aUid,$bypass_trash);
1273 case 'unsetDeleted':
1277 case 'unsetFlagged':
1279 $sFlag = (substr($sButton,0,3) == 'set') ?
'\\'.substr($sButton,3) : '\\'.substr($sButton,5);
1280 $bSet = (substr($sButton,0,3) == 'set') ?
true : false;
1281 $aUpdatedMsgs = sqimap_toggle_flag($imapConnection, $aUid, $sFlag, $bSet, true);
1284 $aUpdatedMsgs = sqimap_msgs_list_move($imapConnection,$aUid,$targetMailbox,true,$mailbox);
1285 sqsession_register($targetMailbox,'lastTargetMailbox');
1289 // sqimap_msgs_list_copy returns true or false.
1290 // If error happens - fourth argument handles it inside function.
1291 sqimap_msgs_list_copy($imapConnection,$aUid,$targetMailbox,true);
1292 sqsession_register($targetMailbox,'lastTargetMailbox');
1295 $aMsgHeaders = array();
1296 foreach ($aUid as $iUid) {
1297 $aMsgHeaders[$iUid] = $aMailbox['MSG_HEADERS'][$iUid];
1299 if (count($aMsgHeaders)) {
1300 $composesession = attachSelectedMessages($imapConnection,$aMsgHeaders);
1301 // dirty hack, add info to $aMailbox
1302 $aMailbox['FORWARD_SESSION'] = $composesession;
1306 // Hook for plugin buttons
1307 do_hook_function('mailbox_display_button_action', $aUid);
1311 * Updates messages is an array containing the result of the untagged
1312 * fetch responses send by the imap server due to a flag change. That
1313 * response is parsed in a array with msg arrays by the parseFetch function
1315 if ($aUpdatedMsgs) {
1316 // Update the message headers cache
1317 $aDeleted = array();
1318 foreach ($aUpdatedMsgs as $iUid => $aMsg) {
1319 if (isset($aMsg['FLAGS'])) {
1321 * Only update the cached headers if the header is
1324 if (isset($aMailbox['MSG_HEADERS'][$iUid])) {
1325 $aMailbox['MSG_HEADERS'][$iUid]['FLAGS'] = $aMsg['FLAGS'];
1328 * Count the messages with the \Delete flag set so we can determine
1329 * if the number of expunged messages equals the number of flagged
1330 * messages for deletion.
1332 if (isset($aMsg['FLAGS']['\\deleted']) && $aMsg['FLAGS']['\\deleted']) {
1333 $aDeleted[] = $iUid;
1337 if ($bExpunge && $aMailbox['AUTO_EXPUNGE'] &&
1338 $iExpungedMessages = sqimap_mailbox_expunge($imapConnection, $aMailbox['NAME'], true))
1340 if (count($aDeleted) != $iExpungedMessages) {
1341 // there are more messages deleted permanently then we expected
1342 // invalidate the cache
1343 $aMailbox['UIDSET'][$aMailbox['SETINDEX']] = false;
1344 $aMailbox['MSG_HEADERS'] = false;
1346 // remove expunged messages from cache
1347 $aUidSet = $aMailbox['UIDSET'][$aMailbox['SETINDEX']];
1348 if (is_array($aUidSet)) {
1349 // create a UID => array index temp array
1350 $aUidSetDummy = array_flip($aUidSet);
1351 foreach ($aDeleted as $iUid) {
1352 // get the id as well in case of SQM_SORT_NONE
1353 if ($aMailbox['SORT'] == SQSORT_NONE
) {
1354 $aMailbox['ID'] = false;
1355 //$iId = $aMailbox['MSG_HEADERS'][$iUid]['ID'];
1356 //unset($aMailbox['ID'][$iId]);
1358 // unset the UID and message header
1359 unset($aUidSetDummy[$iUid]);
1360 unset($aMailbox['MSG_HEADERS'][$iUid]);
1362 $aMailbox['UIDSET'][$aMailbox['SETINDEX']] = array_keys($aUidSetDummy);
1365 // update EXISTS info
1366 if ($iExpungedMessages) {
1367 $aMailbox['EXISTS'] -= (int) $iExpungedMessages;
1368 $aMailbox['TOTAL'][$aMailbox['SETINDEX']] -= (int) $iExpungedMessages;
1370 if (($aMailbox['PAGEOFFSET']-1) >= $aMailbox['EXISTS']) {
1371 $aMailbox['PAGEOFFSET'] = ($aMailbox['PAGEOFFSET'] > $aMailbox['LIMIT']) ?
1372 $aMailbox['PAGEOFFSET'] - $aMailbox['LIMIT'] : 1;
1373 $aMailbox['OFFSET'] = $aMailbox['PAGEOFFSET'] - 1 ;
1378 if ($sButton == 'expunge') {
1380 * on expunge we do not know which messages will be deleted
1381 * so it's useless to try to sync the cache
1383 * Close the mailbox so we do not need to parse the untagged expunge
1384 * responses which do not contain uid info.
1385 * NB: Closing a mailbox is faster then expunge because the imap
1386 * server does not need to generate the untagged expunge responses
1388 sqimap_run_command($imapConnection,'CLOSE',false,$result,$message);
1389 $aMailbox = sqm_api_mailbox_select($imapConnection,$iAccount, $aMailbox['NAME'],array(),array());
1392 $sError = _("No messages were selected.");
1400 * Attach messages to a compose session
1402 * @param resource $imapConnection imap connection
1403 * @param array $aMsgHeaders
1404 * @return int $composesession unique compose_session_id where the attached messages belong to
1405 * @author Marc Groot Koerkamp
1407 function attachSelectedMessages($imapConnection,$aMsgHeaders) {
1408 global $username, $attachment_dir,
1412 sqgetGlobalVar('composesession', $composesession, SQ_SESSION
);
1413 sqgetGlobalVar('compose_messages', $compose_messages, SQ_SESSION
);
1414 if (!isset($compose_messages)||
is_null($compose_messages)) {
1415 $compose_messages = array();
1416 sqsession_register($compose_messages,'compose_messages');
1419 if (!$composesession) {
1420 $composesession = 1;
1421 sqsession_register($composesession,'composesession');
1424 sqsession_register($composesession,'composesession');
1427 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
1429 $composeMessage = new Message();
1430 $rfc822_header = new Rfc822Header();
1431 $composeMessage->rfc822_header
= $rfc822_header;
1432 $composeMessage->reply_rfc822_header
= '';
1434 foreach($aMsgHeaders as $iUid => $aMsgHeader) {
1436 * Retrieve the full message
1438 $body_a = sqimap_run_command($imapConnection, "FETCH $iUid RFC822", true, $response, $readmessage, TRUE);
1439 if ($response == 'OK') {
1441 $subject = (isset($aMsgHeader['subject'])) ?
$aMsgHeader['subject'] : $iUid;
1443 array_shift($body_a);
1445 $body = implode('', $body_a);
1448 $localfilename = GenerateRandomString(32, 'FILE', 7);
1449 $full_localfilename = "$hashed_attachment_dir/$localfilename";
1451 $fp = fopen( $full_localfilename, 'wb');
1452 fwrite ($fp, $body);
1454 $composeMessage->initAttachment('message/rfc822',$subject.'.msg',
1455 $full_localfilename);
1459 $compose_messages[$composesession] = $composeMessage;
1460 sqsession_register($compose_messages,'compose_messages');
1461 return $composesession;