6 * This contains functions that display mailbox information, such as the
7 * table row that has sender, date, subject, etc...
9 * @copyright © 1999-2007 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
25 * @return array $aMailbox mailbox array with all relevant information
28 * @author Marc Groot Koerkamp
30 function sqm_api_mailbox_select($imapConnection,$account,$mailbox,$aConfig,$aProps) {
33 * NB: retrieve this from the session before accessing this function
34 * and make sure you write it back at the end of the script after
35 * the aMailbox var is added so that the headers are added to the cache
37 global $mailbox_cache;
39 $aDefaultConfigProps = array(
40 // 'charset' => 'US-ASCII',
41 'user' => false, /* no pref storage if false */
44 'max_cache_size' => SQM_MAX_MBX_IN_CACHE
47 $aConfig = array_merge($aDefaultConfigProps,$aConfig);
49 $iSetIndx = $aConfig['setindex'];
51 $aMbxResponse = sqimap_mailbox_select($imapConnection, $mailbox);
54 if (isset($mailbox_cache[$account.'_'.$mailbox])) {
55 $aCachedMailbox = $mailbox_cache[$account.'_'.$mailbox];
57 $aCachedMailbox = false;
60 if (count($mailbox_cache) > $aConfig['max_cache_size'] -1) {
62 foreach($mailbox_cache as $cachedmailbox => $aVal) {
63 $aTime[$aVal['TIMESTAMP']] = $cachedmailbox;
65 if (ksort($aTime,SORT_NUMERIC
)) {
66 for ($i=0,$iCnt=count($mailbox_cache);$i<($iCnt-$aConfig['max_cache_size']);++
$i) {
67 $sOldestMbx = array_shift($aTime);
69 * Remove only the UIDSET and MSG_HEADERS from cache because those can
70 * contain large amounts of data.
72 if (isset($mailbox_cache[$sOldestMbx]['UIDSET'])) {
73 $mailbox_cache[$sOldestMbx]['UIDSET']= false;
75 if (isset($mailbox_cache[$sOldestMbx]['MSG_HEADERS'])) {
76 $mailbox_cache[$sOldestMbx]['MSG_HEADERS'] = false;
83 $aCachedMailbox = false;
87 * Deal with imap servers that do not return the required UIDNEXT or
88 * UIDVALIDITY response
89 * from a SELECT call (since rfc 3501 it's required).
91 if (!isset($aMbxResponse['UIDNEXT']) ||
!isset($aMbxResponse['UIDVALIDITY'])) {
92 $aStatus = sqimap_status_messages($imapConnection,$mailbox,
93 array('UIDNEXT','UIDVALIDITY'));
94 $aMbxResponse['UIDNEXT'] = $aStatus['UIDNEXT'];
95 $aMbxResponse['UIDVALIDTY'] = $aStatus['UIDVALIDITY'];
98 $aMailbox['ACCOUNT'] = $account;
99 $aMailbox['UIDSET'][$iSetIndx] = false;
100 $aMailbox['ID'] = false;
101 $aMailbox['SETINDEX'] = $iSetIndx;
102 $aMailbox['MSG_HEADERS'] = false;
104 if ($aCachedMailbox) {
106 * Validate integrity of cached data
108 if ($aCachedMailbox['EXISTS'] == $aMbxResponse['EXISTS'] &&
109 $aMbxResponse['EXISTS'] &&
110 $aCachedMailbox['UIDVALIDITY'] == $aMbxResponse['UIDVALIDITY'] &&
111 $aCachedMailbox['UIDNEXT'] == $aMbxResponse['UIDNEXT'] &&
112 isset($aCachedMailbox['SEARCH'][$iSetIndx]) &&
113 (!isset($aConfig['search']) ||
/* always set search from the searchpage */
114 $aCachedMailbox['SEARCH'][$iSetIndx] == $aConfig['search'])) {
115 if (isset($aCachedMailbox['MSG_HEADERS'])) {
116 $aMailbox['MSG_HEADERS'] = $aCachedMailbox['MSG_HEADERS'];
118 $aMailbox['ID'] = $aCachedMailbox['ID'];
119 if (isset($aCachedMailbox['UIDSET'][$iSetIndx]) && $aCachedMailbox['UIDSET'][$iSetIndx]) {
120 if (isset($aProps[MBX_PREF_SORT
]) && $aProps[MBX_PREF_SORT
] != $aCachedMailbox['SORT'] ) {
121 $newsort = $aProps[MBX_PREF_SORT
];
122 $oldsort = $aCachedMailbox['SORT'];
124 * If it concerns a reverse sort we do not need to invalidate
125 * the cached sorted UIDSET, a reverse is sufficient.
127 if ((($newsort %
2) && ($newsort +
1 == $oldsort)) ||
128 (!($newsort %
2) && ($newsort - 1 == $oldsort))) {
129 $aMailbox['UIDSET'][$iSetIndx] = array_reverse($aCachedMailbox['UIDSET'][$iSetIndx]);
131 $server_sort_array = false;
132 $aMailbox['MSG_HEADERS'] = false;
133 $aMailbox['ID'] = false;
135 // store the new sort value in the mailbox pref
136 if ($aConfig['user']) {
137 // FIXME, in ideal situation, we write back the
138 // prefs at the end of the script
139 setUserPref($aConfig['user'],'pref_'.$account.'_'.$mailbox,serialize($aProps));
142 $aMailbox['UIDSET'][$iSetIndx] = $aCachedMailbox['UIDSET'][$iSetIndx];
148 * Restore the offset in the paginator if no new offset is provided.
150 if (isset($aMailbox['UIDSET'][$iSetIndx]) && !isset($aConfig['offset']) && $aCachedMailbox['OFFSET']) {
151 $aMailbox['OFFSET'] = $aCachedMailbox['OFFSET'];
152 $aMailbox['PAGEOFFSET'] = $aCachedMailbox['PAGEOFFSET'];
154 $aMailbox['OFFSET'] = (isset($aConfig['offset']) && $aConfig['offset']) ?
$aConfig['offset'] -1 : 0;
155 $aMailbox['PAGEOFFSET'] = (isset($aConfig['offset']) && $aConfig['offset']) ?
$aConfig['offset'] : 1;
158 * Restore the number of messages in the result set
160 if (isset($aCachedMailbox['TOTAL'][$iSetIndx]) && $aCachedMailbox['TOTAL'][$iSetIndx]) {
161 $aMailbox['TOTAL'][$iSetIndx] = $aCachedMailbox['TOTAL'][$iSetIndx];
165 * Restore the showall value no new showall value is provided.
167 if (isset($aMailbox['UIDSET'][$iSetIndx]) && !isset($aConfig['showall']) &&
168 isset($aCachedMailbox['SHOWALL'][$iSetIndx]) && $aCachedMailbox['SHOWALL'][$iSetIndx]) {
169 $aMailbox['SHOWALL'][$iSetIndx] = $aCachedMailbox['SHOWALL'][$iSetIndx];
171 $aMailbox['SHOWALL'][$iSetIndx] = (isset($aConfig['showall']) && $aConfig['showall']) ?
1 : 0;
175 * Restore the sort order if no new sort order is provided.
177 if (!isset($aProps[MBX_PREF_SORT
]) && isset($aCachedMailbox['SORT'])) {
178 $aMailbox['SORT'] = $aCachedMailbox['SORT'];
180 $aMailbox['SORT'] = (isset($aProps[MBX_PREF_SORT
])) ?
$aProps[MBX_PREF_SORT
] : 0;
184 * Restore the number of message to show per page when no new limit is provided
186 if (!isset($aProps[MBX_PREF_LIMIT
]) && isset($aCachedMailbox['LIMIT'])) {
187 $aMailbox['LIMIT'] = $aCachedMailbox['LIMIT'];
189 $aMailbox['LIMIT'] = (isset($aProps[MBX_PREF_LIMIT
])) ?
$aProps[MBX_PREF_LIMIT
] : 15;
193 * Restore the ordered columns to show when no new ordered columns are provided
195 if (!isset($aProps[MBX_PREF_COLUMNS
]) && isset($aCachedMailbox['COLUMNS'])) {
196 $aMailbox['COLUMNS'] = $aCachedMailbox['COLUMNS'];
198 $aMailbox['COLUMNS'] = (isset($aProps[MBX_PREF_COLUMNS
])) ?
$aProps[MBX_PREF_COLUMNS
] :
199 array(SQM_COL_FLAGS
,SQM_COL_FROM
, SQM_COL_SUBJ
, SQM_COL_FLAGS
);
203 * Restore the headers we fetch the last time. Saves intitialisation stuff in read_body.
205 $aMailbox['FETCHHEADERS'] = (isset($aCachedMailbox['FETCHHEADERS'])) ?
$aCachedMailbox['FETCHHEADERS'] : null;
207 if (!isset($aProps[MBX_PREF_AUTO_EXPUNGE
]) && isset($aCachedMailbox['AUTO_EXPUNGE'])) {
208 $aMailbox['AUTO_EXPUNGE'] = $aCachedMailbox['AUTO_EXPUNGE'];
210 $aMailbox['AUTO_EXPUNGE'] = (isset($aProps[MBX_PREF_AUTO_EXPUNGE
])) ?
$aProps[MBX_PREF_AUTO_EXPUNGE
] : false;
212 if (!isset($aConfig['search']) && isset($aCachedMailbox['SEARCH'][$iSetIndx])) {
213 $aMailbox['SEARCH'][$iSetIndx] = $aCachedMailbox['SEARCH'][$iSetIndx];
214 } else if (isset($aConfig['search']) && isset($aCachedMailbox['SEARCH'][$iSetIndx]) &&
215 $aConfig['search'] != $aCachedMailbox['SEARCH'][$iSetIndx]) {
216 // reset the pageindex
217 $aMailbox['SEARCH'][$iSetIndx] = $aConfig['search'];
218 $aMailbox['OFFSET'] = 0;
219 $aMailbox['PAGEOFFSET'] = 1;
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
256 * array. Dependent of the mailbox settings it deals with sort, thread and search
257 * If server sort is supported then SORT is also used for retrieving sorted search results
259 * @param resource $imapConnection imap socket handle
260 * @param array $aMailbox (reference) mailbox retrieved from sqm_api_mailbox_select
261 * @return error $error error number
263 * @author Marc Groot Koerkamp
265 function fetchMessageHeaders($imapConnection, &$aMailbox) {
267 /* FIX ME, this function is kind of big, maybe we can split it up in
268 a couple of functions. Make sure the functions are private and starts with _
269 Also make sure that the error codes are propagated */
272 * Retrieve the UIDSET.
273 * Setindex is used to be able to store multiple uid sets. That will make it
274 * possible to display the mailbox multiple times in different sort order
275 * or to store serach results separate from normal mailbox view.
277 $iSetIndx = (isset($aMailbox['SETINDEX'])) ?
$aMailbox['SETINDEX'] : 0;
279 $iLimit = ($aMailbox['SHOWALL'][$iSetIndx]) ?
$aMailbox['EXISTS'] : $aMailbox['LIMIT'];
281 * Adjust the start_msg
283 $start_msg = $aMailbox['PAGEOFFSET'];
284 if($aMailbox['PAGEOFFSET'] > $aMailbox['EXISTS']) {
285 $start_msg -= $aMailbox['LIMIT'];
291 if (is_array($aMailbox['UIDSET'])) {
292 $aUid =& $aMailbox['UIDSET'][$iSetIndx];
296 $aFetchHeaders = $aMailbox['FETCHHEADERS'];
299 $aFetchItems = $aHeaderItems = array();
300 // initialize the fields we want to retrieve:
301 $aHeaderFields = array();
302 foreach ($aFetchHeaders as $v) {
304 case SQM_COL_DATE
: $aHeaderFields[] = 'Date'; break;
305 case SQM_COL_TO
: $aHeaderFields[] = 'To'; break;
306 case SQM_COL_CC
: $aHeaderFields[] = 'Cc'; break;
307 case SQM_COL_FROM
: $aHeaderFields[] = 'From'; break;
308 case SQM_COL_SUBJ
: $aHeaderFields[] = 'Subject'; break;
309 case SQM_COL_PRIO
: $aHeaderFields[] = 'X-Priority'; break;
310 case SQM_COL_ATTACHMENT
: $aHeaderFields[] = 'Content-Type'; break;
311 case SQM_COL_INT_DATE
: $aFetchItems[] = 'INTERNALDATE'; break;
312 case SQM_COL_FLAGS
: $aFetchItems[] = 'FLAGS'; break;
313 case SQM_COL_SIZE
: $aFetchItems[] = 'RFC822.SIZE'; break;
319 * A uidset with sorted uid's is available. We can use the cache
321 if (isset($aUid) && $aUid ) {
322 // limit the cache to SQM_MAX_PAGES_IN_CACHE
323 if (!$aMailbox['SHOWALL'][$iSetIndx] && isset($aMailbox['MSG_HEADERS'])) {
324 $iMaxMsgs = $iLimit * SQM_MAX_PAGES_IN_CACHE
;
325 $iCacheSize = count($aMailbox['MSG_HEADERS']);
326 if ($iCacheSize > $iMaxMsgs) {
327 $iReduce = $iCacheSize - $iMaxMsgs;
328 foreach ($aMailbox['MSG_HEADERS'] as $iUid => $value) {
330 unset($aMailbox['MSG_HEADERS'][$iUid]);
339 $id_slice = array_slice($aUid,$start_msg-1,$iLimit);
340 /* do some funky cache checks */
341 if (isset($aMailbox['MSG_HEADERS']) && is_array($aMailbox['MSG_HEADERS'])) {
342 // temp code, read_body del / next links fo not update fields.
343 foreach ($aMailbox['MSG_HEADERS'] as $iUid => $aValue) {
344 if (!isset($aValue['UID'])) {
345 unset($aMailbox['MSG_HEADERS'][$iUid]);
348 $aUidCached = array_keys($aMailbox['MSG_HEADERS']);
350 $aMailbox['MSG_HEADERS'] = array();
351 $aUidCached = array();
353 $aUidNotCached = array_values(array_diff($id_slice,$aUidCached));
356 * $aUidNotCached contains an array with UID's which need to be fetched to
357 * complete the needed message headers.
359 if (count($aUidNotCached)) {
360 $aMsgs = sqimap_get_small_header_list($imapConnection,$aUidNotCached,
361 $aHeaderFields,$aFetchItems);
362 // append the msgs to the existend headers
363 $aMailbox['MSG_HEADERS'] +
= $aMsgs;
367 * Initialize the sorted UID list or initiate a UID list with search
368 * results and fetch the visible message headers
371 if ($aMailbox['SEARCH'][$iSetIndx] != 'ALL') { // in case of a search request
373 if ($aMailbox['SEARCH'][$iSetIndx] && $aMailbox['SORT'] == 0) {
374 $aUid = sqimap_run_search($imapConnection, $aMailbox['SEARCH'][$iSetIndx], $aMailbox['CHARSET'][$iSetIndx]);
378 $iError = _get_sorted_msgs_list($imapConnection,$aMailbox,$iError);
379 $aUid = $aMailbox['UIDSET'][$iSetIndx];
383 * Number of messages is the resultset
385 $aMailbox['TOTAL'][$iSetIndx] = count($aUid);
386 $id_slice = array_slice($aUid,$aMailbox['OFFSET'], $iLimit);
387 if (count($id_slice)) {
388 $aMailbox['MSG_HEADERS'] = sqimap_get_small_header_list($imapConnection,$id_slice,
389 $aHeaderFields,$aFetchItems);
391 $iError = 1; // FIX ME, define an error code
396 $iError = _get_sorted_msgs_list($imapConnection,$aMailbox,$iError);
397 $aUid = $aMailbox['UIDSET'][$iSetIndx];
401 * Number of messages is the resultset
403 $aMailbox['TOTAL'][$iSetIndx] = count($aUid);
404 $id_slice = array_slice($aUid,$aMailbox['OFFSET'], $iLimit);
405 if (count($id_slice)) {
406 $aMailbox['MSG_HEADERS'] = sqimap_get_small_header_list($imapConnection,$id_slice,
407 $aHeaderFields,$aFetchItems);
409 $iError = 1; // FIX ME, define an error code
418 * Prepares the message headers for display inside a template. The links are calculated,
419 * color for row highlighting is calculated and optionally the strings are truncated.
421 * @param array $aMailbox (reference) mailbox retrieved from sqm_api_mailbox_select
422 * @param array $aProps properties
423 * @return array $aFormattedMessages array with message headers and format info
425 * @author Marc Groot Koerkamp
427 function prepareMessageList(&$aMailbox, $aProps) {
429 /* Globalize link attributes so plugins can share in modifying them */
430 global $link, $title, $target, $onclick, $link_extra;
432 /* retrieve the properties */
433 $my_email_address = (isset($aProps['email'])) ?
$aProps['email'] : false;
434 $highlight_list = (isset($aProps['config']['highlight_list'])) ?
$aProps['config']['highlight_list'] : false;
435 $aColumnDesc = (isset($aProps['columns'])) ?
$aProps['columns'] : false;
436 $aExtraColumns = (isset($aProps['extra_columns'])) ?
$aProps['extra_columns'] : array();
437 $iAccount = (isset($aProps['account'])) ?
(int) $aProps['account'] : 0;
438 $sMailbox = (isset($aProps['mailbox'])) ?
$aProps['mailbox'] : false;
439 $sTargetModule = (isset($aProps['module'])) ?
$aProps['module'] : 'read_body';
442 * TODO 1, retrieve array with identity email addresses in order to match against to,cc and set a flag
443 * $aFormattedMessages[$iUid]['match_identity'] = true
444 * The template can show some image if there is a match.
445 * TODO 2, makes sure the matching is done fast by doing a strpos call on the returned $value
449 * Only retrieve values for displayable columns
451 foreach ($aColumnDesc as $k => $v) {
453 case SQM_COL_FROM
: $aCol[SQM_COL_FROM
] = 'from'; break;
454 case SQM_COL_DATE
: $aCol[SQM_COL_DATE
] = 'date'; break;
455 case SQM_COL_SUBJ
: $aCol[SQM_COL_SUBJ
] = 'subject'; break;
456 case SQM_COL_FLAGS
: $aCol[SQM_COL_FLAGS
] = 'FLAGS'; break;
457 case SQM_COL_SIZE
: $aCol[SQM_COL_SIZE
] = 'SIZE'; break;
458 case SQM_COL_PRIO
: $aCol[SQM_COL_PRIO
] = 'x-priority'; break;
459 case SQM_COL_ATTACHMENT
: $aCol[SQM_COL_ATTACHMENT
] = 'content-type'; break;
460 case SQM_COL_INT_DATE
: $aCol[SQM_COL_INT_DATE
] = 'INTERNALDATE'; break;
461 case SQM_COL_TO
: $aCol[SQM_COL_TO
] = 'to'; break;
462 case SQM_COL_CC
: $aCol[SQM_COL_CC
] = 'cc'; break;
463 case SQM_COL_BCC
: $aCol[SQM_COL_BCC
] = 'bcc'; break;
467 $aExtraHighLightColumns = array();
468 foreach ($aExtraColumns as $v) {
470 case SQM_COL_FROM
: $aExtraHighLightColumns[] = 'from'; break;
471 case SQM_COL_SUBJ
: $aExtraHighLightColumns[] = 'subject'; break;
472 case SQM_COL_TO
: $aExtraHighLightColumns[] = 'to'; break;
473 case SQM_COL_CC
: $aExtraHighLightColumns[] = 'cc'; break;
474 case SQM_COL_BCC
: $aExtraHighLightColumns[] = 'bcc'; break;
478 $aFormattedMessages = array();
481 $iSetIndx = $aMailbox['SETINDEX'];
482 $aId = $aMailbox['UIDSET'][$iSetIndx];
483 $aHeaders =& $aMailbox['MSG_HEADERS']; /* use a reference to avoid a copy.
484 MSG_HEADERS can contain large amounts of data */
485 $iOffset = $aMailbox['OFFSET'];
486 $sort = $aMailbox['SORT'];
487 $iPageOffset = $aMailbox['PAGEOFFSET'];
488 $sMailbox = $aMailbox['NAME'];
489 $sSearch = (isset($aMailbox['SEARCH'][$aMailbox['SETINDEX']]) &&
490 $aMailbox['SEARCH'][$aMailbox['SETINDEX']] != 'ALL') ?
$aMailbox['SEARCH'][$aMailbox['SETINDEX']] : false;
491 $aSearch = ($sSearch) ?
array('search.php',$aMailbox['SETINDEX']) : null;
492 /* avoid improper usage */
493 if ($sMailbox && isset($iAccount) && $sTargetModule) {
494 $aInitQuery = array("account=$iAccount",'mailbox='.urlencode($sMailbox));
499 if ($aMailbox['SORT'] & SQSORT_THREAD
) {
500 $aIndentArray =& $aMailbox['THREAD_INDENT'][$aMailbox['SETINDEX']];
506 * Retrieve value for checkbox column
508 if (!sqgetGlobalVar('checkall',$checkall,SQ_GET
)) {
513 * Loop through and display the info for each message.
515 $iEnd = ($aMailbox['SHOWALL'][$iSetIndx]) ?
$aMailbox['EXISTS'] : $iOffset +
$aMailbox['LIMIT'];
516 for ($i=$iOffset,$t=0;$i<$iEnd;++
$i) {
517 if (isset($aId[$i])) {
520 $value = $title = $link = $target = $onclick = $link_extra = '';
521 $aQuery = ($aInitQuery !== false) ?
$aInitQuery : false;
522 $aMsg = $aHeaders[$aId[$i]];
523 if (isset($aSearch) && count($aSearch) > 1 && $aQuery) {
524 $aQuery[] = "where=". $aSearch[0];
525 $aQuery[] = "what=" . $aSearch[1];
527 $iUid = (isset($aMsg['UID'])) ?
$aMsg['UID'] : $aId[$i];
529 $aQuery[] = "passed_id=$aId[$i]";
530 $aQuery[] = "startMessage=$iPageOffset";
533 foreach ($aCol as $k => $v) {
534 $title = $link = $target = $onclick = $link_extra = '';
535 $aColumns[$k] = array();
536 $value = (isset($aMsg[$v])) ?
$aMsg[$v] : '';
537 $sUnknown = _("Unknown recipient");
540 $sUnknown = _("Unknown sender");
546 if ($highlight_list && !$bHighLight) {
547 $bHighLight = highlightMessage($aCol[$k], $value, $highlight_list,$aFormattedMessages[$iUid]);
549 $aAddressList = parseRFC822Address($value);
550 $sTmp = getAddressString($aAddressList,array('best' => true));
551 $title = $title_maybe = '';
552 foreach ($aAddressList as $aAddr) {
553 $sPersonal = (isset($aAddr[SQM_ADDR_PERSONAL
])) ?
$aAddr[SQM_ADDR_PERSONAL
] : '';
554 $sMailbox = (isset($aAddr[SQM_ADDR_MAILBOX
])) ?
$aAddr[SQM_ADDR_MAILBOX
] : '';
555 $sHost = (isset($aAddr[SQM_ADDR_HOST
])) ?
$aAddr[SQM_ADDR_HOST
] : '';
557 $title .= htmlspecialchars($sMailbox.'@'.$sHost).', ';
559 // if $value gets truncated we need to add the addresses with no
560 // personal name as well
561 $title_maybe .= htmlspecialchars($sMailbox.'@'.$sHost).', ';
565 $title = substr($title,0,-2); // strip ', ';
567 $sTmp = decodeHeader($sTmp);
568 if (isset($aColumnDesc[$k]['truncate']) && $aColumnDesc[$k]['truncate']) {
569 $sTrunc = truncateWithEntities($sTmp, $aColumnDesc[$k]['truncate']);
570 if ($sTrunc != $sTmp) {
572 $title = htmlspecialchars($sTmp);
573 } else if ($title_maybe) {
574 $title = $title .', '.$title_maybe;
575 $title = substr($title,0,-2); // strip ', ';
581 $value = ($sTmp) ?
$sTmp : $sUnknown;
584 // subject is mime encoded, decode it.
585 // value is sanitized in decoding function.
586 // TODO, verify if it should be done before or after the highlighting
587 $value=decodeHeader($value);
588 if ($highlight_list && !$bHighLight) {
589 $bHighLight = highlightMessage('SUBJECT', $value, $highlight_list, $aFormattedMessages[$iUid]);
591 $iIndent = (isset($aIndentArray[$aId[$i]])) ?
$aIndentArray[$aId[$i]] : 0;
592 // FIXME: don't break 8bit symbols and html entities during truncation
593 if (isset($aColumnDesc[$k]['truncate']) && $aColumnDesc[$k]['truncate']) {
594 $sTmp = truncateWithEntities($value, $aColumnDesc[$k]['truncate']-$iIndent);
595 // drop any double spaces since these will be displayed in the title
596 $title = ($sTmp != $value) ?
preg_replace('/\s{2,}/', ' ', $value) : '';
599 /* generate the link to the message */
601 // TODO, $sTargetModule should be a query parameter so that we can use a single entrypoint
602 $link = $sTargetModule.'.php?' . implode('&',$aQuery);
604 // see top of this function for which attributes are available
605 // in the global scope for plugin use (like $link, $target,
606 // $onclick, $link_extra, $title, and so forth)
607 // plugins are responsible for sharing nicely (such as for
608 // setting the target, etc)
609 do_hook('subject_link', $temp=array(&$iPageOffset, &$sSearch, &$aSearch));
611 $value = (trim($value)) ?
$value : _("(no subject)");
612 /* add thread indentation */
613 $aColumns[$k]['indent'] = $iIndent;
616 $value = show_readable_size($value);
619 case SQM_COL_INT_DATE
:
620 $value = getDateString(getTimeStamp(explode(' ',trim($value))));
623 $aFlagColumn = array('seen' => false,
629 if(!is_array($value)) $value = array();
630 foreach ($value as $sFlag => $value) {
632 case '\\seen' : $aFlagColumn['seen'] = true; break;
633 case '\\deleted' : $aFlagColumn['deleted'] = true; break;
634 case '\\answered': $aFlagColumn['answered'] = true; break;
635 case '\\flagged' : $aFlagColumn['flagged'] = true; break;
636 case '\\draft' : $aFlagColumn['draft'] = true; break;
640 $value = $aFlagColumn;
643 $value = ($value) ?
(int) $value : 3;
645 case SQM_COL_ATTACHMENT
:
646 $value = (is_array($value) && $value[0] == 'multipart' && $value[1] == 'mixed') ?
true : false;
653 if ($title) { $aColumns[$k]['title'] = $title; }
654 if ($link) { $aColumns[$k]['link'] = $link; }
655 if ($link_extra) { $aColumns[$k]['link_extra'] = $link_extra; }
656 if ($onclick) { $aColumns[$k]['onclick'] = $onclick; }
657 if ($target) { $aColumns[$k]['target'] = $target; }
658 $aColumns[$k]['value'] = $value;
660 /* columns which will not be displayed but should be inspected
661 because the highlight list contains rules with those columns */
662 foreach ($aExtraHighLightColumns as $v) {
663 if ($highlight_list && !$bHighLight && isset($aMsg[$v])) {
664 $bHighLight = highlightMessage($v, $aMsg[$v], $highlight_list,$aFormattedMessages[$iUid]);
667 $aFormattedMessages[$iUid]['columns'] = $aColumns;
673 return $aFormattedMessages;
678 * Sets the row color if the provided column value pair matches a hightlight rule
680 * @param string $sCol column name
681 * @param string $sVal column value
682 * @param array $highlight_list highlight rules
683 * @param array $aFormat (reference) array where row color info is stored
684 * @return bool match found
686 * @author Marc Groot Koerkamp
688 function highlightMessage($sCol, $sVal, $highlight_list, &$aFormat) {
689 if (!is_array($highlight_list) && count($highlight_list) == 0) {
693 $sCol = strtoupper($sCol);
695 foreach ($highlight_list as $highlight_list_part) {
696 if (trim($highlight_list_part['value'])) {
697 $high_val = strtolower($highlight_list_part['value']);
698 $match_type = strtoupper($highlight_list_part['match_type']);
699 if($match_type == 'TO_CC') {
700 if ($sCol == 'TO' ||
$sCol == 'CC') {
706 if ($match_type != $sCol) {
710 if (strpos(strtolower($sVal),$high_val) !== false) {
711 $hlt_color = $highlight_list_part['color'];
717 // Bug in highlight color???
718 if ($hlt_color{0} != '#') {
719 $hlt_color = '#'. $hlt_color;
721 $aFormat['row']['color'] = $hlt_color;
728 function setUserPref($username, $pref, $value) {
730 setPref($data_dir,$username,$pref,$value);
734 * Execute the sorting for a mailbox
736 * @param resource $imapConnection Imap connection
737 * @param array $aMailbox (reference) Mailbox retrieved with sqm_api_mailbox_select
738 * @return int $error (reference) Error number
741 * @author Marc Groot Koerkamp
743 function _get_sorted_msgs_list($imapConnection,&$aMailbox) {
744 $iSetIndx = (isset($aMailbox['SETINDEX'])) ?
$aMailbox['SETINDEX'] : 0;
745 $bDirection = !($aMailbox['SORT'] %
2);
747 if (!$aMailbox['SEARCH'][$iSetIndx]) {
748 $aMailbox['SEARCH'][$iSetIndx] = 'ALL';
750 if (($aMailbox['SORT'] & SQSORT_THREAD
) && sqimap_capability($imapConnection,'THREAD')) {
751 $aRes = get_thread_sort($imapConnection,$aMailbox['SEARCH'][$iSetIndx]);
752 if ($aRes === false) {
753 $aMailbox['SORT'] -= SQSORT_THREAD
;
754 $error = 1; // fix me, define an error code;
756 $aMailbox['UIDSET'][$iSetIndx] = $aRes[0];
757 $aMailbox['THREAD_INDENT'][$iSetIndx] = $aRes[1];
759 } else if ($aMailbox['SORT'] === SQSORT_NONE
) {
760 $id = sqimap_run_search($imapConnection, 'ALL' , '');
762 $error = 1; // fix me, define an error code
764 $aMailbox['UIDSET'][$iSetIndx] = array_reverse($id);
765 $aMailbox['TOTAL'][$iSetIndx] = $aMailbox['EXISTS'];
768 if (sqimap_capability($imapConnection,'SORT')) {
769 $sSortField = _getSortField($aMailbox['SORT'],true);
770 $id = sqimap_get_sort_order($imapConnection, $sSortField, $bDirection, $aMailbox['SEARCH'][$iSetIndx]);
772 $error = 1; // fix me, define an error code
774 $aMailbox['UIDSET'][$iSetIndx] = $id;
778 if ($aMailbox['SEARCH'][$iSetIndx] != 'ALL') {
779 $id = sqimap_run_search($imapConnection, $aMailbox['SEARCH'][$iSetIndx], $aMailbox['CHARSET'][$iSetIndx]);
781 $sSortField = _getSortField($aMailbox['SORT'],false);
782 $aMailbox['UIDSET'][$iSetIndx] = get_squirrel_sort($imapConnection, $sSortField, $bDirection, $id);
789 * Does the $srt $_GET var to field mapping
791 * @param int $srt Field to sort on
792 * @param bool $bServerSort Server sorting is true
793 * @return string $sSortField Field to sort on
797 function _getSortField($sort,$bServerSort) {
802 case SQSORT_DATE_ASC
:
803 case SQSORT_DATE_DESC
:
804 $sSortField = 'DATE';
806 case SQSORT_FROM_ASC
:
807 case SQSORT_FROM_DESC
:
808 $sSortField = 'FROM';
810 case SQSORT_SUBJ_ASC
:
811 case SQSORT_SUBJ_DESC
:
812 $sSortField = 'SUBJECT';
814 case SQSORT_SIZE_ASC
:
815 case SQSORT_SIZE_DESC
:
816 $sSortField = ($bServerSort) ?
'SIZE' : 'RFC822.SIZE';
826 case SQSORT_INT_DATE_ASC
:
827 case SQSORT_INT_DATE_DESC
:
828 $sSortField = ($bServerSort) ?
'ARRIVAL' : 'INTERNALDATE';
832 default: $sSortField = 'UID';
840 * This function is a utility function for setting which headers should be
841 * fetched. It takes into account the highlight list which requires extra
842 * headers to be fetch in order to make those rules work. It's called before
843 * the headers are fetched which happens in showMessagesForMailbox and when
844 * the next and prev links in read_body.php are used.
846 * @param array $aMailbox associative array with mailbox related vars
847 * @param array $aProps
852 function calcFetchColumns(&$aMailbox, &$aProps) {
854 $highlight_list = (isset($aProps['config']['highlight_list'])) ?
$aProps['config']['highlight_list'] : false;
855 $aColumnsDesc = (isset($aProps['columns'])) ?
$aProps['columns'] : false;
857 $aFetchColumns = $aColumnsDesc;
858 if (isset($aFetchColumns[SQM_COL_CHECK
])) {
859 unset($aFetchColumns[SQM_COL_CHECK
]);
863 * Before we fetch the message headers, check if we need to fetch extra columns
864 * to make the message highlighting work
866 if (is_array($highlight_list) && count($highlight_list)) {
867 $aHighlightColumns = array();
868 foreach ($highlight_list as $highlight_list_part) {
869 if (trim($highlight_list_part['value'])) {
870 $match_type = strtoupper($highlight_list_part['match_type']);
871 switch ($match_type) {
873 $aHighlightColumns[SQM_COL_TO
] = true;
874 $aHighlightColumns[SQM_COL_CC
] = true;
876 case 'TO': $aHighlightColumns[SQM_COL_TO
] = true; break;
877 case 'CC': $aHighlightColumns[SQM_COL_CC
] = true; break;
878 case 'FROM': $aHighlightColumns[SQM_COL_FROM
] = true; break;
879 case 'SUBJECT':$aHighlightColumns[SQM_COL_SUBJ
] = true; break;
883 $aExtraColumns = array();
884 foreach ($aHighlightColumns as $k => $v) {
885 if (!isset($aFetchColumns[$k])) {
886 $aExtraColumns[] = $k;
887 $aFetchColumns[$k] = true;
890 if (count($aExtraColumns)) {
891 $aProps['extra_columns'] = $aExtraColumns;
894 $aMailbox['FETCHHEADERS'] = array_keys($aFetchColumns);
899 * This function loops through a group of messages in the mailbox
900 * and shows them to the user.
902 * @param resource $imapConnection
903 * @param array $aMailbox associative array with mailbox related vars
904 * @param array $aProps
905 * @param int $iError error code, 0 is no error
907 function showMessagesForMailbox($imapConnection, &$aMailbox,$aProps, &$iError) {
909 global $boxes, $show_copy_buttons;
911 $highlight_list = (isset($aProps['config']['highlight_list'])) ?
$aProps['config']['highlight_list'] : false;
912 $fancy_index_highlite = (isset($aProps['config']['fancy_index_highlite'])) ?
$aProps['config']['fancy_index_highlite'] : true;
913 $aColumnsDesc = (isset($aProps['columns'])) ?
$aProps['columns'] : false;
914 $iAccount = (isset($aProps['account'])) ?
(int) $aProps['account'] : 0;
915 $sMailbox = (isset($aProps['mailbox'])) ?
$aProps['mailbox'] : false;
916 $sTargetModule = (isset($aProps['module'])) ?
$aProps['module'] : 'read_body';
917 $show_flag_buttons = (isset($aProps['config']['show_flag_buttons'])) ?
$aProps['config']['show_flag_buttons'] : true;
919 /* allows to control copy button in function call. If array key is not set, code follows user preferences */
920 if (isset($aProps['config']['show_copy_buttons']))
921 $show_copy_buttons = $aProps['config']['show_copy_buttons'];
923 $lastTargetMailbox = (isset($aProps['config']['lastTargetMailbox'])) ?
$aProps['config']['lastTargetMailbox'] : '';
924 $aOrder = array_keys($aProps['columns']);
925 $trash_folder = (isset($aProps['config']['trash_folder']) && $aProps['config']['trash_folder'])
926 ?
$aProps['config']['trash_folder'] : false;
927 $sent_folder = (isset($aProps['config']['sent_folder']) && $aProps['config']['sent_folder'])
928 ?
$aProps['config']['sent_folder'] : false;
929 $draft_folder = (isset($aProps['config']['draft_folder']) && $aProps['config']['draft_folder'])
930 ?
$aProps['config']['draft_folder'] : false;
931 $page_selector = (isset($aProps['config']['page_selector'])) ?
$aProps['config']['page_selector'] : false;
932 $page_selector_max = (isset($aProps['config']['page_selector_max'])) ?
$aProps['config']['page_selector_max'] : 10;
933 $color = $aProps['config']['color'];
941 if (!isset($iFormId)) {
946 // store the columns to fetch so we can pick them up in read_body
947 // where we validate the cache.
948 calcFetchColumns($aMailbox ,$aProps);
950 $iError = fetchMessageHeaders($imapConnection, $aMailbox);
954 $aMessages = prepareMessageList($aMailbox, $aProps);
957 $iSetIndx = $aMailbox['SETINDEX'];
958 $iLimit = ($aMailbox['SHOWALL'][$iSetIndx]) ?
$aMailbox['EXISTS'] : $aMailbox['LIMIT'];
959 $iEnd = ($aMailbox['PAGEOFFSET'] +
($iLimit - 1) < $aMailbox['EXISTS']) ?
960 $aMailbox['PAGEOFFSET'] +
$iLimit - 1 : $aMailbox['EXISTS'];
962 $iNumberOfMessages = $aMailbox['TOTAL'][$iSetIndx];
963 $iEnd = min ( $iEnd, $iNumberOfMessages );
965 $php_self = $PHP_SELF;
967 $urlMailbox = urlencode($aMailbox['NAME']);
969 if (preg_match('/^(.+)\?.+$/',$php_self,$regs)) {
970 $source_url = $regs[1];
972 $source_url = $php_self;
975 $baseurl = $source_url.'?mailbox=' . urlencode($aMailbox['NAME']) .'&account='.$aMailbox['ACCOUNT'];
976 $where = urlencode($aMailbox['SEARCH'][$iSetIndx][0]);
977 $what = urlencode($aMailbox['SEARCH'][$iSetIndx][1]);
978 $baseurl .= '&where=' . $where . '&what=' . $what;
980 /* build thread sorting links */
981 $newsort = $aMailbox['SORT'];
982 if (sqimap_capability($imapConnection,'THREAD')) {
983 if ($aMailbox['SORT'] & SQSORT_THREAD
) {
984 $newsort -= SQSORT_THREAD
;
985 $thread_name = _("Unthread View");
987 $thread_name = _("Thread View");
988 $newsort = $aMailbox['SORT'] + SQSORT_THREAD
;
990 $thread_link_uri = $baseurl . '&srt=' . $newsort
991 . '&startMessage=1';
993 $thread_link_uri ='';
996 $sort = $aMailbox['SORT'];
998 /* FIX ME ADD CHECKBOX CONTROL. No checkbox => no buttons */
1002 /* future admin control over displayable buttons */
1003 $aAdminControl = array(
1004 'markUnflagged' => 1,
1010 'undeleteButton'=> 1,
1011 'bypass_trash' => 1,
1012 'expungeButton' => 1,
1017 /* user prefs control */
1018 $aUserControl = array (
1020 'markUnflagged' => $show_flag_buttons,
1021 'markFlagged' => $show_flag_buttons,
1026 'undeleteButton'=> 1,
1027 'bypass_trash' => 1,
1028 'expungeButton' => 1,
1030 'copyButton' => $show_copy_buttons
1034 $showDelete = ($aMailbox['RIGHTS'] != 'READ-ONLY' &&
1035 in_array('\\deleted',$aMailbox['PERMANENTFLAGS'], true)) ?
true : false;
1036 $showByPassTrash = (($aMailbox['AUTO_EXPUNGE'] && $aMailbox['RIGHTS'] != 'READ-ONLY' &&
1037 in_array('\\deleted',$aMailbox['PERMANENTFLAGS'], true)) &&
1038 $trash_folder) ?
true : false; //
1040 $showUndelete = (!$aMailbox['AUTO_EXPUNGE'] && $aMailbox['RIGHTS'] != 'READ-ONLY' &&
1041 in_array('\\deleted',$aMailbox['PERMANENTFLAGS'], true) && !$trash_folder) ?
true : false;
1042 $showMove = ($aMailbox['RIGHTS'] != 'READ-ONLY') ?
true : false;
1043 $showExpunge = (!$aMailbox['AUTO_EXPUNGE'] && $aMailbox['RIGHTS'] != 'READ-ONLY' &&
1044 in_array('\\deleted',$aMailbox['PERMANENTFLAGS'], true)) ?
true : false;
1046 /* Button options that depend on IMAP server and selected folder */
1047 $aImapControl = array (
1048 'markUnflagged' => in_array('\\flagged',$aMailbox['PERMANENTFLAGS'], true),
1049 'markFlagged' => in_array('\\flagged',$aMailbox['PERMANENTFLAGS'], true),
1050 'markRead' => in_array('\\seen',$aMailbox['PERMANENTFLAGS'], true),
1051 'markUnread' => in_array('\\seen',$aMailbox['PERMANENTFLAGS'], true),
1053 'delete' => $showDelete,
1054 'undeleteButton'=> $showUndelete,
1055 'bypass_trash' => $showByPassTrash,
1056 'expungeButton' => $showExpunge,
1057 'moveButton' => $showMove,
1060 /* Button strings */
1061 $aButtonStrings = array(
1062 'markUnflagged' => _("Unflag"),
1063 'markFlagged' => _("Flag"),
1064 'markRead' => _("Read"),
1065 'markUnread' => _("Unread"),
1066 'forward' => _("Forward"),
1067 'delete' => _("Delete"),
1068 'undeleteButton' => _("Undelete"),
1069 'bypass_trash' => _("Bypass Trash"),
1070 'expungeButton' => _("Expunge"),
1071 'moveButton' => _("Move"),
1072 'copyButton' => _("Copy")
1077 * Register buttons in order to an array
1078 * The key is the "name", the first element of the value array is the "value", second argument is the type.
1080 $aFormElements = array();
1081 foreach($aAdminControl as $k => $v) {
1082 if ($v & $aUserControl[$k] & $aImapControl[$k]) {
1084 case 'markUnflagged':
1089 case 'undeleteButton':
1090 case 'expungeButton':
1093 = array('value' => $aButtonStrings[$k], 'type' => 'submit');
1095 case 'bypass_trash':
1097 = array('value' => $aButtonStrings[$k], 'type' => 'checkbox');
1101 $aFormElements['targetMailbox']
1102 = array('options_list' => sqimap_mailbox_option_list($imapConnection, array(strtolower($lastTargetMailbox)), 0, $boxes),
1103 'type' => 'select');
1104 $aFormElements['mailbox']
1105 = array('value' => $aMailbox['NAME'], 'type' => 'hidden');
1106 $aFormElements['startMessage']
1107 = array('value' => $aMailbox['PAGEOFFSET'], 'type' => 'hidden');
1109 = array('value' => $aButtonStrings[$k], 'type' => 'submit');
1113 $aFormElements['account'] = array('value' => $iAccount,'type' => 'hidden');
1115 do_hook('message_list_controls', $aFormElements);
1118 * This is the beginning of the message list table.
1119 * It wraps around all messages
1121 $safe_name = preg_replace("/[^0-9A-Za-z_]/", '_', $aMailbox['NAME']);
1122 $form_name = "FormMsgs" . $safe_name;
1124 //if (!sqgetGlobalVar('align',$align,SQ_SESSION)) {
1125 $align = array('left' => 'left', 'right' => 'right');
1127 //sm_print_r($align);
1129 /* finally set the template vars */
1131 // FIXME, before we support multiple templates we must review the names of the vars
1135 $aTemplate['color'] = $color;
1136 $aTemplate['form_name'] = "FormMsgs" . $safe_name;
1137 $aTemplate['form_id'] = 'mbx_'.$iFormId;
1138 $aTemplate['page_selector'] = $page_selector;
1139 $aTemplate['page_selector_max'] = $page_selector_max;
1140 $aTemplate['messagesPerPage'] = $aMailbox['LIMIT'];
1141 $aTemplate['showall'] = $aMailbox['SHOWALL'][$iSetIndx];
1142 $aTemplate['end_msg'] = $iEnd;
1143 $aTemplate['align'] = $align;
1144 $aTemplate['iNumberOfMessages'] = $iNumberOfMessages;
1145 $aTemplate['aOrder'] = $aOrder;
1146 $aTemplate['aFormElements'] = $aFormElements;
1147 $aTemplate['sort'] = $sort;
1148 $aTemplate['pageOffset'] = $aMailbox['PAGEOFFSET'];
1149 $aTemplate['baseurl'] = $baseurl;
1150 $aTemplate['aMessages'] =& $aMessages;
1151 $aTemplate['trash_folder'] = $trash_folder;
1152 $aTemplate['sent_folder'] = $sent_folder;
1153 $aTemplate['draft_folder'] = $draft_folder;
1154 $aTemplate['thread_link_uri'] = $thread_link_uri;
1155 $aTemplate['thread_name'] = $thread_name;
1156 $aTemplate['php_self'] = str_replace('&','&',$php_self);
1157 $aTemplate['mailbox'] = $sMailbox;
1158 //FIXME: javascript_on is always assigned to the template object in places like init.php; is there some reason to reassign it here? is there some chance that it was changed? if not, please remove this line!
1159 $aTemplate['javascript_on'] = (isset($aProps['config']['javascript_on'])) ?
$aProps['config']['javascript_on'] : false;
1160 $aTemplate['enablesort'] = (isset($aProps['config']['enablesort'])) ?
$aProps['config']['enablesort'] : false;
1161 $aTemplate['icon_theme'] = (isset($aProps['config']['icon_theme'])) ?
$aProps['config']['icon_theme'] : false;
1162 $aTemplate['use_icons'] = (isset($aProps['config']['use_icons'])) ?
$aProps['config']['use_icons'] : false;
1163 $aTemplate['alt_index_colors'] = (isset($aProps['config']['alt_index_colors'])) ?
$aProps['config']['alt_index_colors'] : false;
1164 $aTemplate['fancy_index_highlite'] = $fancy_index_highlite;
1168 * Set up sort possibilities; one could argue that this is best
1169 * placed in the template, but most template authors won't understand
1170 * or need to understand it, so some advanced templates can override
1171 * it if they do something different.
1173 if (!($aTemplate['sort'] & SQSORT_THREAD
) && $aTemplate['enablesort']) {
1174 $aTemplate['aSortSupported']
1175 = array(SQM_COL_SUBJ
=> array(SQSORT_SUBJ_ASC
, SQSORT_SUBJ_DESC
),
1176 SQM_COL_DATE
=> array(SQSORT_DATE_DESC
, SQSORT_DATE_ASC
),
1177 SQM_COL_INT_DATE
=> array(SQSORT_INT_DATE_DESC
, SQSORT_INT_DATE_ASC
),
1178 SQM_COL_FROM
=> array(SQSORT_FROM_ASC
, SQSORT_FROM_DESC
),
1179 SQM_COL_TO
=> array(SQSORT_TO_ASC
, SQSORT_TO_DESC
),
1180 SQM_COL_CC
=> array(SQSORT_CC_ASC
, SQSORT_CC_DESC
),
1181 SQM_COL_SIZE
=> array(SQSORT_SIZE_ASC
, SQSORT_SIZE_DESC
));
1183 $aTemplate['aSortSupported'] = array();
1188 * Figure out which columns should serve as labels for checkbox:
1189 * we try to grab the two columns before and after the checkbox,
1190 * except the subject column, since it is the link that opens
1193 * if $javascript_on is set, then the highlighting code takes
1194 * care of this; just skip it
1196 * This code also might be more appropriate in a template file, but
1197 * we are moving this complex stuff out of the way of template
1198 * authors; advanced template sets are always free to override
1199 * the resultant values.
1202 $show_label_columns = array();
1203 $index_order_part = array();
1204 if (!($aTemplate['javascript_on'] && $aTemplate['fancy_index_highlite'])) {
1206 $last_order_part = 0;
1207 $last_last_order_part = 0;
1208 foreach ($aTemplate['aOrder'] as $index_order_part) {
1209 if ($index_order_part == SQM_COL_CHECK
) {
1211 if ($last_last_order_part != SQM_COL_SUBJ
)
1212 $show_label_columns[] = $last_last_order_part;
1213 if ($last_order_part != SQM_COL_SUBJ
)
1214 $show_label_columns[] = $last_order_part;
1216 } else if ($get_next_two > 0 && $get_next_two < 3 && $index_order_part != SQM_COL_SUBJ
) {
1217 $show_label_columns[] = $index_order_part;
1220 $last_last_order_part = $last_order_part;
1221 $last_order_part = $index_order_part;
1224 $aTemplate['show_label_columns'] = $show_label_columns;
1233 * Truncates a string and take care of html encoded characters
1235 * @param string $s string to truncate
1236 * @param int $iTrimAt Trim at nn characters
1237 * @return string Trimmed string
1239 function truncateWithEntities($s, $iTrimAt) {
1240 global $languages, $squirrelmail_language;
1242 $ent_strlen = strlen($s);
1243 if (($iTrimAt <= 0) ||
($ent_strlen <= $iTrimAt))
1246 if (isset($languages[$squirrelmail_language]['XTRA_CODE']) &&
1247 function_exists($languages[$squirrelmail_language]['XTRA_CODE'] . '_strimwidth')) {
1248 return call_user_func($languages[$squirrelmail_language]['XTRA_CODE'] . '_strimwidth', $s, $iTrimAt);
1251 * see if this is entities-encoded string
1252 * If so, Iterate through the whole string, find out
1253 * the real number of characters, and if more
1254 * than $iTrimAt, substr with an updated trim value.
1256 $trim_val = $iTrimAt;
1259 while ( $ent_loc < $trim_val && (($ent_loc = strpos($s, '&', $ent_offset)) !== false) &&
1260 (($ent_loc_end = strpos($s, ';', $ent_loc+
3)) !== false) ) {
1261 $trim_val +
= ($ent_loc_end-$ent_loc);
1262 $ent_offset = $ent_loc_end+
1;
1265 if (($trim_val > $iTrimAt) && ($ent_strlen > $trim_val) && (strpos($s,';',$trim_val) < ($trim_val +
6))) {
1266 $i = strpos($s,';',$trim_val);
1268 $trim_val = strpos($s,';',$trim_val)+
1;
1271 // only print '...' when we're actually dropping part of the subject
1272 if ($ent_strlen <= $trim_val)
1275 return substr_replace($s, '...', $trim_val);
1280 * This should go in imap_mailbox.php
1281 * @param string $mailbox
1283 function handleAsSent($mailbox) {
1284 global $handleAsSent_result;
1286 /* First check if this is the sent or draft folder. */
1287 $handleAsSent_result = isSentMailbox($mailbox) ||
isDraftMailbox($mailbox);
1289 /* Then check the result of the handleAsSent hook. */
1290 do_hook('check_handleAsSent_result', $mailbox);
1292 /* And return the result. */
1293 return $handleAsSent_result;
1297 * Process messages list form and handle the cache gracefully. If $sButton and
1298 * $aUid are provided as argument then you can fake a message list submit and
1299 * use it i.e. in read_body.php for del move next and update the cache
1301 * @param resource $imapConnection imap connection
1302 * @param array $aMailbox (reference) cached mailbox
1303 * @param string $sButton fake a submit button
1304 * @param array $aUid fake the $msg array
1305 * @return string $sError error string in case of an error
1307 * @author Marc Groot Koerkamp
1309 function handleMessageListForm($imapConnection,&$aMailbox,$sButton='',$aUid = array()) {
1310 /* incoming formdata */
1311 $sButton = (sqgetGlobalVar('moveButton', $sTmp, SQ_FORM
)) ?
'move' : $sButton;
1312 $sButton = (sqgetGlobalVar('copyButton', $sTmp, SQ_FORM
)) ?
'copy' : $sButton;
1313 $sButton = (sqgetGlobalVar('expungeButton', $sTmp, SQ_FORM
)) ?
'expunge' : $sButton;
1314 $sButton = (sqgetGlobalVar('forward', $sTmp, SQ_FORM
)) ?
'forward' : $sButton;
1315 $sButton = (sqgetGlobalVar('delete', $sTmp, SQ_FORM
)) ?
'setDeleted' : $sButton;
1316 $sButton = (sqgetGlobalVar('undeleteButton', $sTmp, SQ_FORM
)) ?
'unsetDeleted' : $sButton;
1317 $sButton = (sqgetGlobalVar('markRead', $sTmp, SQ_FORM
)) ?
'setSeen' : $sButton;
1318 $sButton = (sqgetGlobalVar('markUnread', $sTmp, SQ_FORM
)) ?
'unsetSeen' : $sButton;
1319 $sButton = (sqgetGlobalVar('markFlagged', $sTmp, SQ_FORM
)) ?
'setFlagged' : $sButton;
1320 $sButton = (sqgetGlobalVar('markUnflagged', $sTmp, SQ_FORM
)) ?
'unsetFlagged' : $sButton;
1321 sqgetGlobalVar('targetMailbox', $targetMailbox, SQ_FORM
);
1322 sqgetGlobalVar('bypass_trash', $bypass_trash, SQ_FORM
);
1323 sqgetGlobalVar('msg', $msg, SQ_FORM
);
1324 if (sqgetGlobalVar('account', $iAccount, SQ_FORM
) === false) {
1328 $mailbox = $aMailbox['NAME'];
1330 /* retrieve the check boxes */
1331 $aUid = (isset($msg) && is_array($msg)) ?
array_values($msg) : $aUid;
1332 if (count($aUid) && $sButton != 'expunge') {
1333 $aUpdatedMsgs = false;
1337 // check if id exists in case we come from read_body
1338 if (count($aUid) == 1 && is_array($aMailbox['UIDSET'][$aMailbox['SETINDEX']]) &&
1339 !in_array($aUid[0],$aMailbox['UIDSET'][$aMailbox['SETINDEX']])) {
1342 $aUpdatedMsgs = sqimap_msgs_list_delete($imapConnection, $mailbox, $aUid,$bypass_trash);
1346 case 'unsetDeleted':
1350 case 'unsetFlagged':
1352 $sFlag = (substr($sButton,0,3) == 'set') ?
'\\'.substr($sButton,3) : '\\'.substr($sButton,5);
1353 $bSet = (substr($sButton,0,3) == 'set') ?
true : false;
1354 $aUpdatedMsgs = sqimap_toggle_flag($imapConnection, $aUid, $sFlag, $bSet, true);
1357 $aUpdatedMsgs = sqimap_msgs_list_move($imapConnection,$aUid,$targetMailbox,true,$mailbox);
1358 sqsession_register($targetMailbox,'lastTargetMailbox');
1362 // sqimap_msgs_list_copy returns true or false.
1363 // If error happens - fourth argument handles it inside function.
1364 sqimap_msgs_list_copy($imapConnection,$aUid,$targetMailbox,true);
1365 sqsession_register($targetMailbox,'lastTargetMailbox');
1368 $aMsgHeaders = array();
1369 foreach ($aUid as $iUid) {
1370 $aMsgHeaders[$iUid] = $aMailbox['MSG_HEADERS'][$iUid];
1372 if (count($aMsgHeaders)) {
1373 $composesession = attachSelectedMessages($imapConnection,$aMsgHeaders);
1374 // dirty hack, add info to $aMailbox
1375 $aMailbox['FORWARD_SESSION'] = $composesession;
1379 // Hook for plugin buttons
1380 do_hook('mailbox_display_button_action', $aUid);
1384 * Updates messages is an array containing the result of the untagged
1385 * fetch responses send by the imap server due to a flag change. That
1386 * response is parsed in a array with msg arrays by the parseFetch function
1388 if ($aUpdatedMsgs) {
1389 // Update the message headers cache
1390 $aDeleted = array();
1391 foreach ($aUpdatedMsgs as $iUid => $aMsg) {
1392 if (isset($aMsg['FLAGS'])) {
1394 * Only update the cached headers if the header is
1397 if (isset($aMailbox['MSG_HEADERS'][$iUid])) {
1398 $aMailbox['MSG_HEADERS'][$iUid]['FLAGS'] = $aMsg['FLAGS'];
1401 * Count the messages with the \Delete flag set so we can determine
1402 * if the number of expunged messages equals the number of flagged
1403 * messages for deletion.
1405 if (isset($aMsg['FLAGS']['\\deleted']) && $aMsg['FLAGS']['\\deleted']) {
1406 $aDeleted[] = $iUid;
1410 if ($bExpunge && $aMailbox['AUTO_EXPUNGE'] &&
1411 $iExpungedMessages = sqimap_mailbox_expunge($imapConnection, $aMailbox['NAME'], true))
1413 if (count($aDeleted) != $iExpungedMessages) {
1414 // there are more messages deleted permanently then we expected
1415 // invalidate the cache
1416 $aMailbox['UIDSET'][$aMailbox['SETINDEX']] = false;
1417 $aMailbox['MSG_HEADERS'] = false;
1419 // remove expunged messages from cache
1420 $aUidSet = $aMailbox['UIDSET'][$aMailbox['SETINDEX']];
1421 if (is_array($aUidSet)) {
1422 // create a UID => array index temp array
1423 $aUidSetDummy = array_flip($aUidSet);
1424 foreach ($aDeleted as $iUid) {
1425 // get the id as well in case of SQM_SORT_NONE
1426 if ($aMailbox['SORT'] == SQSORT_NONE
) {
1427 $aMailbox['ID'] = false;
1428 //$iId = $aMailbox['MSG_HEADERS'][$iUid]['ID'];
1429 //unset($aMailbox['ID'][$iId]);
1431 // unset the UID and message header
1432 unset($aUidSetDummy[$iUid]);
1433 unset($aMailbox['MSG_HEADERS'][$iUid]);
1435 $aMailbox['UIDSET'][$aMailbox['SETINDEX']] = array_keys($aUidSetDummy);
1438 // update EXISTS info
1439 if ($iExpungedMessages) {
1440 $aMailbox['EXISTS'] -= (int) $iExpungedMessages;
1441 $aMailbox['TOTAL'][$aMailbox['SETINDEX']] -= (int) $iExpungedMessages;
1443 if (($aMailbox['PAGEOFFSET']-1) >= $aMailbox['EXISTS']) {
1444 $aMailbox['PAGEOFFSET'] = ($aMailbox['PAGEOFFSET'] > $aMailbox['LIMIT']) ?
1445 $aMailbox['PAGEOFFSET'] - $aMailbox['LIMIT'] : 1;
1446 $aMailbox['OFFSET'] = $aMailbox['PAGEOFFSET'] - 1 ;
1451 if ($sButton == 'expunge') {
1453 * on expunge we do not know which messages will be deleted
1454 * so it's useless to try to sync the cache
1456 * Close the mailbox so we do not need to parse the untagged expunge
1457 * responses which do not contain uid info.
1458 * NB: Closing a mailbox is faster then expunge because the imap
1459 * server does not need to generate the untagged expunge responses
1461 sqimap_run_command($imapConnection,'CLOSE',false,$result,$message);
1462 $aMailbox = sqm_api_mailbox_select($imapConnection,$iAccount, $aMailbox['NAME'],array(),array());
1465 $sError = _("No messages were selected.");
1473 * Attach messages to a compose session
1475 * @param resource $imapConnection imap connection
1476 * @param array $aMsgHeaders
1477 * @return int $composesession unique compose_session_id where the attached messages belong to
1478 * @author Marc Groot Koerkamp
1480 function attachSelectedMessages($imapConnection,$aMsgHeaders) {
1482 sqgetGlobalVar('composesession', $composesession, SQ_SESSION
);
1483 sqgetGlobalVar('compose_messages', $compose_messages, SQ_SESSION
);
1484 if (!isset($compose_messages)||
is_null($compose_messages)) {
1485 $compose_messages = array();
1486 sqsession_register($compose_messages,'compose_messages');
1489 if (!$composesession) {
1490 $composesession = 1;
1491 sqsession_register($composesession,'composesession');
1494 sqsession_register($composesession,'composesession');
1497 $composeMessage = new Message();
1498 $rfc822_header = new Rfc822Header();
1499 $composeMessage->rfc822_header
= $rfc822_header;
1500 $composeMessage->reply_rfc822_header
= '';
1502 foreach($aMsgHeaders as $iUid => $aMsgHeader) {
1504 * Retrieve the full message
1506 $body_a = sqimap_run_command($imapConnection, "FETCH $iUid RFC822", true, $response, $readmessage, TRUE);
1507 if ($response == 'OK') {
1509 $subject = (isset($aMsgHeader['subject'])) ?
$aMsgHeader['subject'] : $iUid;
1511 array_shift($body_a);
1513 $body = implode('', $body_a);
1516 $filename = sq_get_attach_tempfile();
1517 $fp = fopen($filename, 'wb');
1518 fwrite ($fp, $body);
1521 $composeMessage->initAttachment('message/rfc822',$subject.'.msg',
1526 $compose_messages[$composesession] = $composeMessage;
1527 sqsession_register($compose_messages,'compose_messages');
1528 return $composesession;