Show bypass trash checkbox again and move the delete button to the right.
[squirrelmail.git] / functions / mailbox_display.php
1 <?php
2
3 /**
4 * mailbox_display.php
5 *
6 * Copyright (c) 1999-2005 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * This contains functions that display mailbox information, such as the
10 * table row that has sender, date, subject, etc...
11 *
12 * @version $Id$
13 * @package squirrelmail
14 */
15
16 /** The standard includes.. */
17 require_once(SM_PATH . 'functions/strings.php');
18 require_once(SM_PATH . 'functions/html.php');
19 require_once(SM_PATH . 'functions/imap_mailbox.php');
20 require_once(SM_PATH . 'functions/imap_messages.php');
21 require_once(SM_PATH . 'functions/imap_asearch.php');
22 require_once(SM_PATH . 'functions/mime.php');
23 require_once(SM_PATH . 'functions/forms.php');
24
25
26 /**
27 * Selects a mailbox for header retrieval.
28 * Cache control for message headers is embedded.
29 *
30 * @param resource $imapConnection imap socket handle
31 * @param string $mailbox mailbox to select and retrieve message headers from
32 * @param array $aConfig array with system config settings and incoming vars
33 * @param array $aProps mailbox specific properties
34 * @return array $aMailbox mailbox array with all relevant information
35 * @author Marc Groot Koerkamp
36 */
37 function sqm_api_mailbox_select($imapConnection,$account,$mailbox,$aConfig,$aProps) {
38
39 /**
40 * NB: retrieve this from the session before accessing this function
41 * and make sure you write it back at the end of the script after
42 * the aMailbox var is added so that the headers are added to the cache
43 */
44 global $mailbox_cache;
45
46 $aDefaultConfigProps = array(
47 // 'charset' => 'US-ASCII',
48 'user' => false, /* no pref storage if false */
49 'setindex' => 0,
50 // 'search' => 'ALL',
51 'max_cache_size' => SQM_MAX_MBX_IN_CACHE
52 );
53
54 $aConfig = array_merge($aDefaultConfigProps,$aConfig);
55
56 $iSetIndx = $aConfig['setindex'];
57
58 $aMbxResponse = sqimap_mailbox_select($imapConnection, $mailbox);
59
60 if ($mailbox_cache) {
61 if (isset($mailbox_cache[$account.'_'.$mailbox])) {
62 $aCachedMailbox = $mailbox_cache[$account.'_'.$mailbox];
63 } else {
64 $aCachedMailbox = false;
65 }
66 /* cleanup cache */
67 if (count($mailbox_cache) > $aConfig['max_cache_size'] -1) {
68 $aTime = array();
69 foreach($mailbox_cache as $cachedmailbox => $aVal) {
70 $aTime[$aVal['TIMESTAMP']] = $cachedmailbox;
71 }
72 if (ksort($aTime,SORT_NUMERIC)) {
73 for ($i=0,$iCnt=count($mailbox_cache);$i<($iCnt-$aConfig['max_cache_size']);++$i) {
74 $sOldestMbx = array_shift($aTime);
75 /**
76 * Remove only the UIDSET and MSG_HEADERS from cache because those can
77 * contain large amounts of data.
78 */
79 if (isset($mailbox_cache[$sOldestMbx]['UIDSET'])) {
80 $mailbox_cache[$sOldestMbx]['UIDSET']= false;
81 }
82 if (isset($mailbox_cache[$sOldestMbx]['MSG_HEADERS'])) {
83 $mailbox_cache[$sOldestMbx]['MSG_HEADERS'] = false;
84 }
85 }
86 }
87 }
88
89 } else {
90 $aCachedMailbox = false;
91 }
92
93 /**
94 * Deal with imap servers that do not return the required UIDNEXT or
95 * UIDVALIDITY response
96 * from a SELECT call (since rfc 3501 it's required).
97 */
98 if (!isset($aMbxResponse['UIDNEXT']) || !isset($aMbxResponse['UIDVALIDITY'])) {
99 $aStatus = sqimap_status_messages($imapConnection,$mailbox,
100 array('UIDNEXT','UIDVALIDITY'));
101 $aMbxResponse['UIDNEXT'] = $aStatus['UIDNEXT'];
102 $aMbxResponse['UIDVALIDTY'] = $aStatus['UIDVALIDITY'];
103 }
104
105 $aMailbox['ACCOUNT'] = $account;
106 $aMailbox['UIDSET'][$iSetIndx] = false;
107 $aMailbox['ID'] = false;
108 $aMailbox['SETINDEX'] = $iSetIndx;
109
110 if ($aCachedMailbox) {
111 /**
112 * Validate integrity of cached data
113 */
114 if ($aCachedMailbox['EXISTS'] == $aMbxResponse['EXISTS'] &&
115 $aMbxResponse['EXISTS'] &&
116 $aCachedMailbox['UIDVALIDITY'] == $aMbxResponse['UIDVALIDITY'] &&
117 $aCachedMailbox['UIDNEXT'] == $aMbxResponse['UIDNEXT'] &&
118 isset($aCachedMailbox['SEARCH'][$iSetIndx]) &&
119 (!isset($aConfig['search']) || /* always set search from the searchpage */
120 $aCachedMailbox['SEARCH'][$iSetIndx] == $aConfig['search'])) {
121 if (isset($aCachedMailbox['MSG_HEADERS'])) {
122 $aMailbox['MSG_HEADERS'] = $aCachedMailbox['MSG_HEADERS'];
123 }
124 $aMailbox['ID'] = $aCachedMailbox['ID'];
125 if (isset($aCachedMailbox['UIDSET'][$iSetIndx]) && $aCachedMailbox['UIDSET'][$iSetIndx]) {
126 if (isset($aProps[MBX_PREF_SORT]) && $aProps[MBX_PREF_SORT] != $aCachedMailbox['SORT'] ) {
127 $newsort = $aProps[MBX_PREF_SORT];
128 $oldsort = $aCachedMailbox['SORT'];
129 /**
130 * If it concerns a reverse sort we do not need to invalidate
131 * the cached sorted UIDSET, a reverse is sufficient.
132 */
133 if ((($newsort % 2) && ($newsort + 1 == $oldsort)) ||
134 (!($newsort % 2) && ($newsort - 1 == $oldsort))) {
135 $aMailbox['UIDSET'][$iSetIndx] = array_reverse($aCachedMailbox['UIDSET'][$iSetIndx]);
136 } else {
137 $server_sort_array = false;
138 $aMailbox['MSG_HEADERS'] = false;
139 $aMailbox['ID'] = false;
140 }
141 // store the new sort value in the mailbox pref
142 if ($aConfig['user']) {
143 // FIXME, in ideal situation, we write back the
144 // prefs at the end of the script
145 setUserPref($aConfig['user'],'pref_'.$account.'_'.$mailbox,serialize($aProps));
146 }
147 } else {
148 $aMailbox['UIDSET'][$iSetIndx] = $aCachedMailbox['UIDSET'][$iSetIndx];
149 }
150 }
151 }
152 }
153 /**
154 * Restore the offset in the paginator if no new offset is provided.
155 */
156 if (isset($aMailbox['UIDSET'][$iSetIndx]) && !isset($aConfig['offset']) && $aCachedMailbox['OFFSET']) {
157 $aMailbox['OFFSET'] = $aCachedMailbox['OFFSET'];
158 $aMailbox['PAGEOFFSET'] = $aCachedMailbox['PAGEOFFSET'];
159 } else {
160 $aMailbox['OFFSET'] = (isset($aConfig['offset']) && $aConfig['offset']) ? $aConfig['offset'] -1 : 0;
161 $aMailbox['PAGEOFFSET'] = (isset($aConfig['offset']) && $aConfig['offset']) ? $aConfig['offset'] : 1;
162 }
163 /**
164 * Restore the number of messages in the result set
165 */
166 if (isset($aCachedMailbox['TOTAL'][$iSetIndx]) && $aCachedMailbox['TOTAL'][$iSetIndx]) {
167 $aMailbox['TOTAL'][$iSetIndx] = $aCachedMailbox['TOTAL'][$iSetIndx];
168 }
169
170 /**
171 * Restore the showall value no new showall value is provided.
172 */
173 if (isset($aMailbox['UIDSET'][$iSetIndx]) && !isset($aConfig['showall']) &&
174 isset($aCachedMailbox['SHOWALL'][$iSetIndx]) && $aCachedMailbox['SHOWALL'][$iSetIndx]) {
175 $aMailbox['SHOWALL'][$iSetIndx] = $aCachedMailbox['SHOWALL'][$iSetIndx];
176 } else {
177 $aMailbox['SHOWALL'][$iSetIndx] = (isset($aConfig['showall']) && $aConfig['showall']) ? 1 : 0;
178 }
179
180 /**
181 * Restore the sort order if no new sort order is provided.
182 */
183 if (!isset($aProps[MBX_PREF_SORT]) && isset($aCachedMailbox['SORT'])) {
184 $aMailbox['SORT'] = $aCachedMailbox['SORT'];
185 } else {
186 $aMailbox['SORT'] = (isset($aProps[MBX_PREF_SORT])) ? $aProps[MBX_PREF_SORT] : 0;
187 }
188
189 /**
190 * Restore the number of message to show per page when no new limit is provided
191 */
192 if (!isset($aProps[MBX_PREF_LIMIT]) && isset($aCachedMailbox['LIMIT'])) {
193 $aMailbox['LIMIT'] = $aCachedMailbox['LIMIT'];
194 } else {
195 $aMailbox['LIMIT'] = (isset($aProps[MBX_PREF_LIMIT])) ? $aProps[MBX_PREF_LIMIT] : 15;
196 }
197
198 /**
199 * Restore the ordered columns to show when no new ordered columns are provided
200 */
201 if (!isset($aProps[MBX_PREF_COLUMNS]) && isset($aCachedMailbox['COLUMNS'])) {
202 $aMailbox['COLUMNS'] = $aCachedMailbox['COLUMNS'];
203 } else {
204 $aMailbox['COLUMNS'] = (isset($aProps[MBX_PREF_COLUMNS])) ? $aProps[MBX_PREF_COLUMNS] :
205 array(SQM_COL_FLAGS,SQM_COL_FROM, SQM_COL_SUBJ, SQM_COL_FLAGS);
206 }
207
208 /**
209 * Restore the headers we fetch the last time. Saves intitialisation stuff in read_body.
210 */
211 $aMailbox['FETCHHEADERS'] = (isset($aCachedMailbox['FETCHHEADERS'])) ? $aCachedMailbox['FETCHHEADERS'] : null;
212
213 if (!isset($aProps[MBX_PREF_AUTO_EXPUNGE]) && isset($aCachedMailbox['AUTO_EXPUNGE'])) {
214 $aMailbox['AUTO_EXPUNGE'] = $aCachedMailbox['AUTO_EXPUNGE'];
215 } else {
216 $aMailbox['AUTO_EXPUNGE'] = (isset($aProps[MBX_PREF_AUTO_EXPUNGE])) ? $aProps[MBX_PREF_AUTO_EXPUNGE] : false;
217 }
218 if (!isset($aConfig['search']) && isset($aCachedMailbox['SEARCH'][$iSetIndx])) {
219 $aMailbox['SEARCH'][$iSetIndx] = $aCachedMailbox['SEARCH'][$iSetIndx];
220 } else {
221 $aMailbox['SEARCH'][$iSetIndx] = (isset($aConfig['search'])) ? $aConfig['search'] : 'ALL';
222 }
223 if (!isset($aConfig['charset']) && isset($aCachedMailbox['CHARSET'][$iSetIndx])) {
224 $aMailbox['CHARSET'][$iSetIndx] = $aCachedMailbox['CHARSET'][$iSetIndx];
225 } else {
226 $aMailbox['CHARSET'][$iSetIndx] = (isset($aConfig['charset'])) ? $aConfig['charset'] : 'US-ASCII';
227 }
228
229 $aMailbox['NAME'] = $mailbox;
230 $aMailbox['EXISTS'] = $aMbxResponse['EXISTS'];
231 $aMailbox['SEEN'] = (isset($aMbxResponse['SEEN'])) ? $aMbxResponse['SEEN'] : $aMbxResponse['EXISTS'];
232 $aMailbox['RECENT'] = (isset($aMbxResponse['RECENT'])) ? $aMbxResponse['RECENT'] : 0;
233 $aMailbox['UIDVALIDITY'] = $aMbxResponse['UIDVALIDITY'];
234 $aMailbox['UIDNEXT'] = $aMbxResponse['UIDNEXT'];
235 $aMailbox['PERMANENTFLAGS'] = $aMbxResponse['PERMANENTFLAGS'];
236 $aMailbox['RIGHTS'] = $aMbxResponse['RIGHTS'];
237
238 /* decide if we are thread sorting or not */
239 if ($aMailbox['SORT'] & SQSORT_THREAD) {
240 if (!sqimap_capability($imapConnection,'THREAD')) {
241 $aMailbox['SORT'] ^= SQSORT_THREAD;
242 } else {
243 $aMailbox['THREAD_INDENT'] = $aCachedMailbox['THREAD_INDENT'];
244 }
245 } else {
246 $aMailbox['THREAD_INDENT'] = false;
247 }
248
249 /* set a timestamp for cachecontrol */
250 $aMailbox['TIMESTAMP'] = time();
251 return $aMailbox;
252 }
253
254 /**
255 * Fetch the message headers for a mailbox. Settings are part of the aMailbox
256 * array.
257 *
258 * @param resource $imapConnection imap socket handle
259 * @param array $aMailbox (reference) mailbox retrieved from sqm_api_mailbox_select
260 * @return error $error error number
261 * @author Marc Groot Koerkamp
262 */
263 function fetchMessageHeaders($imapConnection, &$aMailbox) {
264
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 */
268
269 /**
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.
274 */
275 $iSetIndx = (isset($aMailbox['SETINDEX'])) ? $aMailbox['SETINDEX'] : 0;
276
277 $iLimit = ($aMailbox['SHOWALL'][$iSetIndx]) ? $aMailbox['EXISTS'] : $aMailbox['LIMIT'];
278 /**
279 * Adjust the start_msg
280 */
281 $start_msg = $aMailbox['PAGEOFFSET'];
282 if($aMailbox['PAGEOFFSET'] > $aMailbox['EXISTS']) {
283 $start_msg -= $aMailbox['LIMIT'];
284 if($start_msg < 1) {
285 $start_msg = 1;
286 }
287 }
288
289 if (is_array($aMailbox['UIDSET'])) {
290 $aUid =& $aMailbox['UIDSET'][$iSetIndx];
291 } else {
292 $aUid = false;
293 }
294 $aFetchHeaders = $aMailbox['FETCHHEADERS'];
295
296 $iError = 0;
297 $aFetchItems = $aHeaderItems = array();
298 // initialize the fields we want to retrieve:
299 foreach ($aFetchHeaders as $v) {
300 switch ($v) {
301 case SQM_COL_DATE: $aHeaderFields[] = 'Date'; break;
302 case SQM_COL_TO: $aHeaderFields[] = 'To'; break;
303 case SQM_COL_CC: $aHeaderFields[] = 'Cc'; break;
304 case SQM_COL_FROM: $aHeaderFields[] = 'From'; break;
305 case SQM_COL_SUBJ: $aHeaderFields[] = 'Subject'; break;
306 case SQM_COL_PRIO: $aHeaderFields[] = 'X-Priority'; break;
307 case SQM_COL_ATTACHMENT: $aHeaderFields[] = 'Content-Type'; break;
308 case SQM_COL_INT_DATE: $aFetchItems[] = 'INTERNALDATE'; break;
309 case SQM_COL_FLAGS: $aFetchItems[] = 'FLAGS'; break;
310 case SQM_COL_SIZE: $aFetchItems[] = 'RFC822.SIZE'; break;
311 default: break;
312 }
313 }
314
315 /**
316 * A uidset with sorted uid's is available. We can use the cache
317 */
318 if (isset($aUid) && $aUid ) {
319 // limit the cache to SQM_MAX_PAGES_IN_CACHE
320 if (!$aMailbox['SHOWALL'][$iSetIndx] && isset($aMailbox['MSG_HEADERS'])) {
321 $iMaxMsgs = $iLimit * SQM_MAX_PAGES_IN_CACHE;
322 $iCacheSize = count($aMailbox['MSG_HEADERS']);
323 if ($iCacheSize > $iMaxMsgs) {
324 $iReduce = $iCacheSize - $iMaxMsgs;
325 foreach ($aMailbox['MSG_HEADERS'] as $iUid => $value) {
326 if ($iReduce) {
327 unset($aMailbox['MSG_HEADERS'][$iUid]);
328 } else {
329 break;
330 }
331 --$iReduce;
332 }
333 }
334 }
335
336 $id_slice = array_slice($aUid,$start_msg-1,$iLimit);
337 /* do some funky cache checks */
338 if (isset($aMailbox['MSG_HEADERS'])) {
339 $aUidCached = array_keys($aMailbox['MSG_HEADERS']);
340 } else {
341 $aMailbox['MSG_HEADERS'] = array();
342 $aUidCached = array();
343 }
344 $aUidNotCached = array_values(array_diff($id_slice,$aUidCached));
345
346 /**
347 * $aUidNotCached contains an array with UID's which need to be fetched to
348 * complete the needed message headers.
349 */
350 if (count($aUidNotCached)) {
351 $aMsgs = sqimap_get_small_header_list($imapConnection,$aUidNotCached,
352 $aHeaderFields,$aFetchItems);
353 // append the msgs to the existend headers
354 $aMailbox['MSG_HEADERS'] += $aMsgs;
355 }
356 } else {
357 /**
358 * Initialize the sorted UID list or initiate a UID list with search
359 * results and fetch the visible message headers
360 */
361
362 if ($aMailbox['SEARCH'][$iSetIndx] != 'ALL') { // in case of a search request
363
364 if ($aMailbox['SEARCH'][$iSetIndx] && $aMailbox['SORT'] == 0) {
365 $aUid = sqimap_run_search($imapConnection, $aMailbox['SEARCH'][$iSetIndx], $aMailbox['CHARSET'][$iSetIndx]);
366 } else {
367
368 $iError = 0;
369 $iError = _get_sorted_msgs_list($imapConnection,$aMailbox,$iError);
370 $aUid = $aMailbox['UIDSET'][$iSetIndx];
371 }
372 if (!$iError) {
373 /**
374 * Number of messages is the resultset
375 */
376 $aMailbox['TOTAL'][$iSetIndx] = count($aUid);
377 $id_slice = array_slice($aUid,$aMailbox['OFFSET'], $iLimit);
378 if (count($id_slice)) {
379 $aMailbox['MSG_HEADERS'] = sqimap_get_small_header_list($imapConnection,$id_slice,
380 $aHeaderFields,$aFetchItems);
381 } else {
382 $iError = 1; // FIX ME, define an error code
383 }
384 }
385 } else { //
386 $iError = 0;
387 $iError = _get_sorted_msgs_list($imapConnection,$aMailbox,$iError);
388 $aUid = $aMailbox['UIDSET'][$iSetIndx];
389
390 if (!$iError) {
391 /**
392 * Number of messages is the resultset
393 */
394 $aMailbox['TOTAL'][$iSetIndx] = count($aUid);
395 $id_slice = array_slice($aUid,$aMailbox['OFFSET'], $iLimit);
396 if (count($id_slice)) {
397 $aMailbox['MSG_HEADERS'] = sqimap_get_small_header_list($imapConnection,$id_slice,
398 $aHeaderFields,$aFetchItems);
399 } else {
400 $iError = 1; // FIX ME, define an error code
401 }
402 }
403 }
404 }
405 return $iError;
406 }
407
408 function prepareMessageList(&$aMailbox, $aProps) {
409 /* retrieve the properties */
410 $my_email_address = (isset($aProps['email'])) ? $aProps['email'] : false;
411 $highlight_list = (isset($aProps['config']['highlight_list'])) ? $aProps['config']['highlight_list'] : false;
412 $aColumnDesc = (isset($aProps['columns'])) ? $aProps['columns'] : false;
413 $aExtraColumns = (isset($aProps['extra_columns'])) ? $aProps['extra_columns'] : array();
414 $iAccount = (isset($aProps['account'])) ? (int) $aProps['account'] : 0;
415 $sMailbox = (isset($aProps['mailbox'])) ? $aProps['mailbox'] : false;
416 $sTargetModule = (isset($aProps['module'])) ? $aProps['module'] : 'read_body';
417
418 /*
419 * TODO 1, retrieve array with identity email addresses in order to match against to,cc and set a flag
420 * $aFormattedMessages[$iUid]['match_identity'] = true
421 * The template can show some image if there is a match.
422 * TODO 2, makes sure the matching is done fast by doing a strpos call on the returned $value
423 */
424
425 /**
426 * Only retrieve values for displayable columns
427 */
428 foreach ($aColumnDesc as $k => $v) {
429 switch ($k) {
430 case SQM_COL_FROM: $aCol[SQM_COL_FROM] = 'from'; break;
431 case SQM_COL_DATE: $aCol[SQM_COL_DATE] = 'date'; break;
432 case SQM_COL_SUBJ: $aCol[SQM_COL_SUBJ] = 'subject'; break;
433 case SQM_COL_FLAGS: $aCol[SQM_COL_FLAGS] = 'FLAGS'; break;
434 case SQM_COL_SIZE: $aCol[SQM_COL_SIZE] = 'SIZE'; break;
435 case SQM_COL_PRIO: $aCol[SQM_COL_PRIO] = 'x-priority'; break;
436 case SQM_COL_ATTACHMENT: $aCol[SQM_COL_ATTACHMENT] = 'content-type'; break;
437 case SQM_COL_INT_DATE: $aCol[SQM_COL_INT_DATE] = 'INTERNALDATE'; break;
438 case SQM_COL_TO: $aCol[SQM_COL_TO] = 'to'; break;
439 case SQM_COL_CC: $aCol[SQM_COL_CC] = 'cc'; break;
440 case SQM_COL_BCC: $aCol[SQM_COL_BCC] = 'bcc'; break;
441 default: break;
442 }
443 }
444 $aExtraHighLightColumns = array();
445 foreach ($aExtraColumns as $v) {
446 switch ($v) {
447 case SQM_COL_FROM: $aExtraHighLightColumns[] = 'from'; break;
448 case SQM_COL_SUBJ: $aExtraHighLightColumns[] = 'subject'; break;
449 case SQM_COL_TO: $aExtraHighLightColumns[] = 'to'; break;
450 case SQM_COL_CC: $aExtraHighLightColumns[] = 'cc'; break;
451 case SQM_COL_BCC: $aExtraHighLightColumns[] = 'bcc'; break;
452 default: break;
453 }
454 }
455 $aFormattedMessages = array();
456
457
458 $iSetIndx = $aMailbox['SETINDEX'];
459 $aId = $aMailbox['UIDSET'][$iSetIndx];
460 $aHeaders =& $aMailbox['MSG_HEADERS']; /* use a reference to avoid a copy.
461 MSG_HEADERS can contain large amounts of data */
462 $iOffset = $aMailbox['OFFSET'];
463 $sort = $aMailbox['SORT'];
464 $iPageOffset = $aMailbox['PAGEOFFSET'];
465 $sMailbox = $aMailbox['NAME'];
466 $sSearch = (isset($aMailbox['SEARCH'][$aMailbox['SETINDEX']]) &&
467 $aMailbox['SEARCH'][$aMailbox['SETINDEX']] != 'ALL') ? $aMailbox['SEARCH'][$aMailbox['SETINDEX']] : false;
468 $aSearch = ($sSearch) ? array('search.php',$aMailbox['SETINDEX']) : null;
469 /* avoid improper usage */
470 if ($sMailbox && isset($iAccount) && $sTargetModule) {
471 $aInitQuery = array("account=$iAccount",'mailbox='.urlencode($sMailbox));
472 } else {
473 $aInitQuery = false;
474 }
475
476 if ($aMailbox['SORT'] & SQSORT_THREAD) {
477 $aIndentArray =& $aMailbox['THREAD_INDENT'][$aMailbox['SETINDEX']];
478 $bThread = true;
479 } else {
480 $bThread = false;
481 }
482 /*
483 * Retrieve value for checkbox column
484 */
485 if (!sqgetGlobalVar('checkall',$checkall,SQ_GET)) {
486 $checkall = false;
487 }
488
489 /*
490 * Loop through and display the info for each message.
491 */
492 $iEnd = ($aMailbox['SHOWALL'][$iSetIndx]) ? $aMailbox['EXISTS'] : $iOffset + $aMailbox['LIMIT'];
493 for ($i=$iOffset,$t=0;$i<$iEnd;++$i) {
494 if (isset($aId[$i])) {
495
496 $bHighLight = false;
497 $value = $title = $link = $target = '';
498 $aQuery = ($aInitQuery !== false) ? $aInitQuery : false;
499 $aMsg = $aHeaders[$aId[$i]];
500 if (isset($aSearch) && count($aSearch) > 1 && $aQuery) {
501 $aQuery[] = "where=". $aSearch[0];
502 $aQuery[] = "what=" . $aSearch[1];
503 }
504 $iUid = (isset($aMsg['UID'])) ? $aMsg['UID'] : $aId[$i];
505 if ($aQuery) {
506 $aQuery[] = "passed_id=$aId[$i]";
507 $aQuery[] = "startMessage=$iPageOffset";
508 }
509
510 foreach ($aCol as $k => $v) {
511 $link = $target = $title = '';
512 $aColumns[$k] = array();
513 $value = (isset($aMsg[$v])) ? $aMsg[$v] : '';
514 $sUnknown = _("Unknown recipient");
515 switch ($k) {
516 case SQM_COL_FROM:
517 $sUnknown = _("Unknown sender");
518 case SQM_COL_TO:
519 case SQM_COL_CC:
520 case SQM_COL_BCC:
521 $sTmp = false;
522 if ($value) {
523 if ($highlight_list && !$bHighLight) {
524 $bHighLight = highlightMessage($aCol[$k], $value, $highlight_list,$aFormattedMessages[$iUid]);
525 }
526 $sTmp = getAddressString(parseRFC822Address($value),array('best' => true));
527 $sTmp = decodeHeader($sTmp);
528 if (isset($aColumnDesc[$k]['truncate']) && $aColumnDesc[$k]['truncate']) {
529 $sTrunc = truncateWithEntities($sTmp, $aColumnDesc[$k]['truncate']);
530 $title = ($sTrunc != $sTmp) ? htmlspecialchars($sTmp) : '';
531 $sTmp = $sTrunc;
532 }
533 }
534 $value = ($sTmp) ? $sTmp : $sUnknown;
535 break;
536 case SQM_COL_SUBJ:
537 // subject is mime encoded, decode it.
538 // value is sanitized in decoding function.
539 // TODO, verify if it should be done before or after the highlighting
540 $value=decodeHeader($value);
541 if ($highlight_list && !$bHighLight) {
542 $bHighLight = highlightMessage('SUBJECT', $value, $highlight_list, $aFormattedMessages[$iUid]);
543 }
544 $iIndent = (isset($aIndentArray[$aId[$i]])) ? $aIndentArray[$aId[$i]] : 0;
545 // FIXME: don't break 8bit symbols and html entities during truncation
546 if (isset($aColumnDesc[$k]['truncate']) && $aColumnDesc[$k]['truncate']) {
547 $sTmp = truncateWithEntities($value, $aColumnDesc[$k]['truncate']-$iIndent);
548 $title = ($sTmp != $value) ? $value : '';
549 $value = $sTmp;
550 }
551 /* generate the link to the message */
552 if ($aQuery) {
553 // TODO, $sTargetModule should be a query parameter so that we can use a single entrypoint
554 $link = $sTargetModule.'.php?' . implode('&amp;',$aQuery);
555 }
556 $value = (trim($value)) ? $value : _("(no subject)");
557 /* add thread indentation */
558 $aColumns[$k]['indent'] = $iIndent;
559 break;
560 case SQM_COL_SIZE:
561 $value = show_readable_size($value);
562 break;
563 case SQM_COL_DATE:
564 case SQM_COL_INT_DATE:
565 $value = getDateString(getTimeStamp(explode(' ',trim($value))));
566 break;
567 case SQM_COL_FLAGS:
568 $aFlagColumn = array('seen' => false,
569 'deleted'=>false,
570 'answered'=>false,
571 'flagged' => false,
572 'draft' => false);
573 foreach ($value as $sFlag => $value) {
574 switch ($sFlag) {
575 case '\\seen' : $aFlagColumn['seen'] = true; break;
576 case '\\deleted' : $aFlagColumn['deleted'] = true; break;
577 case '\\answered': $aFlagColumn['answered'] = true; break;
578 case '\\flagged' : $aFlagColumn['flagged'] = true; break;
579 case '\\draft' : $aFlagColumn['draft'] = true; break;
580 default: break;
581 }
582 }
583 $value = $aFlagColumn;
584 break;
585 case SQM_COL_PRIO:
586 $value = ($value) ? (int) $value : 3;
587 break;
588 case SQM_COL_ATTACHMENT:
589 $value = (is_array($value) && $value[0] == 'multipart' && $value[1] == 'mixed') ? true : false;
590 break;
591 case SQM_COL_CHECK:
592 $value = $checkall;
593 break;
594 default : break;
595 }
596 if ($title) { $aColumns[$k]['title'] = $title; }
597 if ($link) { $aColumns[$k]['link'] = $link; }
598 if ($target) { $aColumns[$k]['target'] = $target; }
599 $aColumns[$k]['value'] = $value;
600 }
601 /* columns which will not be displayed but should be inspected
602 because the highlight list contains rules with those columns */
603 foreach ($aExtraHighLightColumns as $v) {
604 if ($highlight_list && !$bHighLight && isset($aMsg[$v])) {
605 $bHighLight = highlightMessage($v, $aMsg[$v], $highlight_list,$aFormattedMessages[$iUid]);
606 }
607 }
608 $aFormattedMessages[$iUid]['columns'] = $aColumns;
609
610 } else {
611 break;
612 }
613 }
614 return $aFormattedMessages;
615 }
616
617
618
619 function highlightMessage($sCol, $sVal, $highlight_list, &$aFormat) {
620 if (!is_array($highlight_list) && count($highlight_list) == 0) {
621 return false;
622 }
623 $hlt_color = false;
624 $sCol = strtoupper($sCol);
625
626 foreach ($highlight_list as $highlight_list_part) {
627 if (trim($highlight_list_part['value'])) {
628 $high_val = strtolower($highlight_list_part['value']);
629 $match_type = strtoupper($highlight_list_part['match_type']);
630 if($match_type == 'TO_CC') {
631 if ($sCol == 'TO' || $sCol == 'CC') {
632 $match_type = $sCol;
633 } else {
634 continue;
635 }
636 } else {
637 if ($match_type != $sCol) {
638 continue;
639 }
640 }
641 if (strpos(strtolower($sVal),$high_val) !== false) {
642 $hlt_color = $highlight_list_part['color'];
643 break;
644 }
645 }
646 }
647 if ($hlt_color) {
648 // Bug in highlight color???
649 if ($hlt_color{0} != '#') {
650 $hlt_color = '#'. $hlt_color;
651 }
652 $aFormat['row']['color'] = $hlt_color;
653 return true;
654 } else {
655 return false;
656 }
657 }
658
659 function setUserPref($username, $pref, $value) {
660 global $data_dir;
661 setPref($data_dir,$username,$pref,$value);
662 }
663
664 /**
665 * Execute the sorting for a mailbox
666 *
667 * @param resource $imapConnection Imap connection
668 * @param array $aMailbox (reference) Mailbox retrieved with sqm_api_mailbox_select
669 * @return int $error (reference) Error number
670 * @private
671 * @author Marc Groot Koerkamp
672 */
673 function _get_sorted_msgs_list($imapConnection,&$aMailbox) {
674 $iSetIndx = (isset($aMailbox['SETINDEX'])) ? $aMailbox['SETINDEX'] : 0;
675 $bDirection = ($aMailbox['SORT'] % 2);
676 $error = 0;
677 if (!$aMailbox['SEARCH'][$iSetIndx]) {
678 $aMailbox['SEARCH'][$iSetIndx] = 'ALL';
679 }
680 if (($aMailbox['SORT'] & SQSORT_THREAD) && sqimap_capability($imapConnection,'THREAD')) {
681 $aRes = get_thread_sort($imapConnection,$aMailbox['SEARCH'][$iSetIndx]);
682 if ($aRes === false) {
683 $aMailbox['SORT'] -= SQSORT_THREAD;
684 $error = 1; // fix me, define an error code;
685 } else {
686 $aMailbox['UIDSET'][$iSetIndx] = $aRes[0];
687 $aMailbox['THREAD_INDENT'][$iSetIndx] = $aRes[1];
688 }
689 } else if ($aMailbox['SORT'] === SQSORT_NONE) {
690 $id = sqimap_run_search($imapConnection, 'ALL' , '');
691 if ($id === false) {
692 $error = 1; // fix me, define an error code
693 } else {
694 $aMailbox['UIDSET'][$iSetIndx] = array_reverse($id);
695 $aMailbox['TOTAL'][$iSetIndx] = $aMailbox['EXISTS'];
696 }
697 } else {
698 if (sqimap_capability($imapConnection,'SORT')) {
699 $sSortField = _getSortField($aMailbox['SORT'],true);
700 $id = sqimap_get_sort_order($imapConnection, $sSortField, $bDirection, $aMailbox['SEARCH'][$iSetIndx]);
701 if ($id === false) {
702 $error = 1; // fix me, define an error code
703 } else {
704 $aMailbox['UIDSET'][$iSetIndx] = $id;
705 }
706 } else {
707 $id = NULL;
708 if ($aMailbox['SEARCH'][$iSetIndx] != 'ALL') {
709 $id = sqimap_run_search($imapConnection, $aMailbox['SEARCH'][$iSetIndx], $aMailbox['CHARSET'][$iSetIndx]);
710 }
711 $sSortField = _getSortField($aMailbox['SORT'],false);
712 $aMailbox['UIDSET'][$iSetIndx] = get_squirrel_sort($imapConnection, $sSortField, $bDirection, $id);
713 }
714 }
715 return $error;
716 }
717
718 /**
719 * Does the $srt $_GET var to field mapping
720 *
721 * @param int $srt Field to sort on
722 * @param bool $bServerSort Server sorting is true
723 * @return string $sSortField Field to sort on
724 * @private
725 */
726 function _getSortField($sort,$bServerSort) {
727 switch($sort) {
728 case SQSORT_NONE:
729 $sSortField = 'UID';
730 break;
731 case SQSORT_DATE_ASC:
732 case SQSORT_DATE_DESC:
733 $sSortField = 'DATE';
734 break;
735 case SQSORT_FROM_ASC:
736 case SQSORT_FROM_DESC:
737 $sSortField = 'FROM';
738 break;
739 case SQSORT_SUBJ_ASC:
740 case SQSORT_SUBJ_DESC:
741 $sSortField = 'SUBJECT';
742 break;
743 case SQSORT_SIZE_ASC:
744 case SQSORT_SIZE_DESC:
745 $sSortField = ($bServerSort) ? 'SIZE' : 'RFC822.SIZE';
746 break;
747 case SQSORT_TO_ASC:
748 case SQSORT_TO_DESC:
749 $sSortField = 'TO';
750 break;
751 case SQSORT_CC_ASC:
752 case SQSORT_CC_DESC:
753 $sSortField = 'CC';
754 break;
755 case SQSORT_INT_DATE_ASC:
756 case SQSORT_INT_DATE_DESC:
757 $sSortField = ($bServerSort) ? 'ARRIVAL' : 'INTERNALDATE';
758 break;
759 case SQSORT_THREAD:
760 break;
761 default: $sSortField = 'UID';
762 break;
763
764 }
765 return $sSortField;
766 }
767
768
769
770
771 /**
772 * This function loops through a group of messages in the mailbox
773 * and shows them to the user.
774 *
775 * @param resource $imapConnection
776 * @param array $aMailbox associative array with mailbox related vars
777 * @param array $aProps
778 * @param int $iError error code, 0 is no error
779 */
780 function showMessagesForMailbox($imapConnection, &$aMailbox,$aProps, &$iError) {
781 global $PHP_SELF;
782 global $boxes;
783
784 $highlight_list = (isset($aProps['config']['highlight_list'])) ? $aProps['config']['highlight_list'] : false;
785 $fancy_index_highlite = (isset($aProps['config']['fancy_index_highlite'])) ? $aProps['config']['fancy_index_highlite'] : true;
786 $aColumnsDesc = (isset($aProps['columns'])) ? $aProps['columns'] : false;
787 $iAccount = (isset($aProps['account'])) ? (int) $aProps['account'] : 0;
788 $sMailbox = (isset($aProps['mailbox'])) ? $aProps['mailbox'] : false;
789 $sTargetModule = (isset($aProps['module'])) ? $aProps['module'] : 'read_body';
790 $show_flag_buttons = (isset($aProps['config']['show_flag_buttons'])) ? $aProps['config']['show_flag_buttons'] : true;
791 $lastTargetMailbox = (isset($aProps['config']['lastTargetMailbox'])) ? $aProps['config']['lastTargetMailbox'] : '';
792 $aOrder = array_keys($aProps['columns']);
793 $trash_folder = (isset($aProps['config']['trash_folder']) && $aProps['config']['trash_folder'])
794 ? $aProps['config']['trash_folder'] : false;
795 $sent_folder = (isset($aProps['config']['sent_folder']) && $aProps['config']['sent_folder'])
796 ? $aProps['config']['sent_folder'] : false;
797 $draft_folder = (isset($aProps['config']['draft_folder']) && $aProps['config']['draft_folder'])
798 ? $aProps['config']['draft_folder'] : false;
799 $page_selector = (isset($aProps['config']['page_selector'])) ? $aProps['config']['page_selector'] : false;
800 $page_selector_max = (isset($aProps['config']['page_selector_max'])) ? $aProps['config']['page_selector_max'] : 10;
801 $color = $aProps['config']['color'];
802
803
804 /*
805 * Form ID
806 */
807 static $iFormId;
808
809 if (!isset($iFormId)) {
810 $iFormId=1;
811 } else {
812 ++$iFormId;
813 }
814 /*
815 * Remove the checkbox column because we cannot fetch it from the imap server
816 */
817 $aFetchColumns = $aColumnsDesc;
818 if (isset($aFetchColumns[SQM_COL_CHECK])) {
819 unset($aFetchColumns[SQM_COL_CHECK]);
820 }
821
822 /*
823 * Before we fetch the message headers, check if we need to fetch extra columns
824 * to make the message highlighting work
825 */
826 if (is_array($highlight_list) && count($highlight_list)) {
827 $aHighlightColumns = array();
828 foreach ($highlight_list as $highlight_list_part) {
829 if (trim($highlight_list_part['value'])) {
830 $match_type = strtoupper($highlight_list_part['match_type']);
831 switch ($match_type) {
832 case 'TO_CC':
833 $aHighlightColumns[SQM_COL_TO] = true;
834 $aHighlightColumns[SQM_COL_CC] = true;
835 break;
836 case 'TO': $aHighlightColumns[SQM_COL_TO] = true; break;
837 case 'CC': $aHighlightColumns[SQM_COL_CC] = true; break;
838 case 'FROM': $aHighlightColumns[SQM_COL_FROM] = true; break;
839 case 'SUBJECT':$aHighlightColumns[SQM_COL_SUBJ] = true; break;
840 }
841 }
842 }
843 $aExtraColumns = array();
844 foreach ($aHighlightColumns as $k => $v) {
845 if (!isset($aFetchColumns[$k])) {
846 $aExtraColumns[] = $k;
847 $aFetchColumns[$k] = true;
848 }
849 }
850 if (count($aExtraColumns)) {
851 $aProps['extra_columns'] = $aExtraColumns;
852 }
853 }
854 $aFetchColumns = array_keys($aFetchColumns);
855 // store the columns to fetch so we can pick them up in read_body
856 // where we validate the cache.
857 $aMailbox['FETCHHEADERS'] = $aFetchColumns;
858
859 $iError = fetchMessageHeaders($imapConnection, $aMailbox);
860 if ($iError) {
861 return array();
862 } else {
863 $aMessages = prepareMessageList($aMailbox, $aProps);
864 }
865
866 $iSetIndx = $aMailbox['SETINDEX'];
867 $iLimit = ($aMailbox['SHOWALL'][$iSetIndx]) ? $aMailbox['EXISTS'] : $aMailbox['LIMIT'];
868 $iEnd = ($aMailbox['PAGEOFFSET'] + ($iLimit - 1) < $aMailbox['EXISTS']) ?
869 $aMailbox['PAGEOFFSET'] + $iLimit - 1 : $aMailbox['EXISTS'];
870
871 $iNumberOfMessages = $aMailbox['TOTAL'][$iSetIndx];
872
873 $php_self = $PHP_SELF;
874
875 $urlMailbox = urlencode($aMailbox['NAME']);
876
877 if (preg_match('/^(.+)\?.+$/',$php_self,$regs)) {
878 $source_url = $regs[1];
879 } else {
880 $source_url = $php_self;
881 }
882
883 $baseurl = $source_url.'?mailbox=' . urlencode($aMailbox['NAME']) .'&amp;account='.$aMailbox['ACCOUNT'];
884 $where = urlencode($aMailbox['SEARCH'][$iSetIndx][0]);
885 $what = urlencode($aMailbox['SEARCH'][$iSetIndx][1]);
886 $baseurl .= '&amp;where=' . $where . '&amp;what=' . $what;
887
888 /* build thread sorting links */
889 $newsort = $aMailbox['SORT'];
890 if (sqimap_capability($imapConnection,'THREAD')) {
891 if ($aMailbox['SORT'] & SQSORT_THREAD) {
892 $newsort -= SQSORT_THREAD;
893 $thread_name = _("Unthread View");
894 } else {
895 $thread_name = _("Thread View");
896 $newsort = $aMailbox['SORT'] + SQSORT_THREAD;
897 }
898 $thread_link_str = '<small>[<a href="' . $baseurl . '&amp;srt='
899 . $newsort . '&amp;startMessage=1">' . $thread_name
900 . '</a>]</small>';
901 } else {
902 $thread_link_str ='';
903 }
904 $sort = $aMailbox['SORT'];
905
906 /* FIX ME ADD CHECKBOX CONTROL. No checkbox => no buttons */
907
908
909
910 /* future admin control over displayable buttons */
911
912 $aAdminControl = array(
913 'markUnflagged' => 1,
914 'markFlagged' => 1,
915 'markRead' => 1,
916 'markUnread' => 1,
917 'forward' => 1,
918 'delete' => 1,
919 'undeleteButton'=> 1,
920 'bypass_trash' => 1,
921 'expungeButton' => 1,
922 'moveButton' => 1
923 );
924 /* user prefs control */
925 $aUserControl = array (
926
927 'markUnflagged' => $show_flag_buttons,
928 'markFlagged' => $show_flag_buttons,
929 'markRead' => 1,
930 'markUnread' => 1,
931 'forward' => 1,
932 'delete' => 1,
933 'undeleteButton'=> 1,
934 'bypass_trash' => 1,
935 'expungeButton' => 1,
936 'moveButton' => 1
937
938 );
939
940 $showDelete = ($aMailbox['RIGHTS'] != 'READ-ONLY' &&
941 in_array('\\deleted',$aMailbox['PERMANENTFLAGS'], true)) ? true : false;
942 $showByPassTrash = (($aMailbox['AUTO_EXPUNGE'] && $aMailbox['RIGHTS'] != 'READ-ONLY' &&
943 in_array('\\deleted',$aMailbox['PERMANENTFLAGS'], true)) &&
944 $trash_folder) ? true : false; //
945
946 $showUndelete = (!$aMailbox['AUTO_EXPUNGE'] && $aMailbox['RIGHTS'] != 'READ-ONLY' &&
947 in_array('\\deleted',$aMailbox['PERMANENTFLAGS'], true) && !$trash_folder) ? true : false;
948 $showMove = ($aMailbox['RIGHTS'] != 'READ-ONLY') ? true : false;
949 $showExpunge = (!$aMailbox['AUTO_EXPUNGE'] && $aMailbox['RIGHTS'] != 'READ-ONLY' &&
950 in_array('\\deleted',$aMailbox['PERMANENTFLAGS'], true)) ? true : false;
951 $aImapControl = array (
952 'markUnflagged' => in_array('\\flagged',$aMailbox['PERMANENTFLAGS'], true),
953 'markFlagged' => in_array('\\flagged',$aMailbox['PERMANENTFLAGS'], true),
954 'markRead' => in_array('\\seen',$aMailbox['PERMANENTFLAGS'], true),
955 'markUnread' => in_array('\\seen',$aMailbox['PERMANENTFLAGS'], true),
956 'forward' => 1,
957 'delete' => $showDelete,
958 'undeleteButton'=> $showUndelete,
959 'bypass_trash' => $showByPassTrash,
960 'expungeButton' => $showExpunge,
961 'moveButton' => $showMove
962 );
963 $aButtonStrings = array(
964 'markUnflagged' => _("Unflag"),
965 'markFlagged' => _("Flag"),
966 'markRead' => _("Read"),
967 'markUnread' => _("Unread"),
968 'forward' => _("Forward"),
969 'delete' => _("Delete"),
970 'undeleteButton' => _("Undelete"),
971 'bypass_trash' => _("Bypass Trash"),
972 'expungeButton' => _("Expunge"),
973 'moveButton' => _("Move")
974 );
975
976
977 /**
978 * Register buttons in order to an array
979 * The key is the "name", the first element of the value array is the "value", second argument is the type.
980 */
981 $aFormElements = array();
982 foreach($aAdminControl as $k => $v) {
983 if ($v & $aUserControl[$k] & $aImapControl[$k]) {
984 switch ($k) {
985 case 'markUnflagged':
986 case 'markFlagged':
987 case 'markRead':
988 case 'markUnread':
989 case 'delete':
990 case 'undeleteButton':
991 case 'expungeButton':
992 case 'forward':
993 $aFormElements[$k] = array($aButtonStrings[$k],'submit');
994 break;
995 case 'bypass_trash':
996 $aFormElements[$k] = array($aButtonStrings[$k],'checkbox');
997 break;
998 case 'moveButton':
999 $aFormElements['targetMailbox'] =
1000 array(sqimap_mailbox_option_list($imapConnection, array(strtolower($lastTargetMailbox)), 0, $boxes),'select');
1001 $aFormElements['mailbox'] = array($aMailbox['NAME'],'hidden');
1002 $aFormElements['startMessage'] = array($aMailbox['PAGEOFFSET'],'hidden');
1003 $aFormElements[$k] = array($aButtonStrings[$k],'submit');
1004 break;
1005 }
1006 }
1007 $aFormElements['account'] = array($iAccount,'hidden');
1008 }
1009
1010 /*
1011 * This is the beginning of the message list table.
1012 * It wraps around all messages
1013 */
1014 $safe_name = preg_replace("/[^0-9A-Za-z_]/", '_', $aMailbox['NAME']);
1015 $form_name = "FormMsgs" . $safe_name;
1016
1017 //if (!sqgetGlobalVar('align',$align,SQ_SESSION)) {
1018 $align = array('left' => 'left', 'right' => 'right');
1019 //}
1020 //sm_print_r($align);
1021
1022 /* finally set the template vars */
1023
1024 // FIX ME, before we support multiple templates we must review the names of the vars
1025
1026
1027
1028 $aTemplate['color'] = $color;
1029 $aTemplate['form_name'] = "FormMsgs" . $safe_name;
1030 $aTemplate['form_id'] = 'mbx_'.$iFormId;
1031 $aTemplate['page_selector'] = $page_selector;
1032 $aTemplate['page_selector_max'] = $page_selector_max;
1033 $aTemplate['messagesPerPage'] = $aMailbox['LIMIT'];
1034 $aTemplate['showall'] = $aMailbox['SHOWALL'][$iSetIndx];
1035 $aTemplate['end_msg'] = $iEnd;
1036 $aTemplate['align'] = $align;
1037 $aTemplate['iNumberOfMessages'] = $iNumberOfMessages;
1038 $aTemplate['aOrder'] = $aOrder;
1039 $aTemplate['aFormElements'] = $aFormElements;
1040 $aTemplate['sort'] = $sort;
1041 $aTemplate['pageOffset'] = $aMailbox['PAGEOFFSET'];
1042 $aTemplate['baseurl'] = $baseurl;
1043 $aTemplate['aMessages'] =& $aMessages;
1044 $aTemplate['trash_folder'] = $trash_folder;
1045 $aTemplate['sent_folder'] = $sent_folder;
1046 $aTemplate['draft_folder'] = $draft_folder;
1047 $aTemplate['thread_link_str'] = $thread_link_str;
1048 $aTemplate['php_self'] = str_replace('&','&amp;',$php_self);
1049 $aTemplate['mailbox'] = $sMailbox;
1050 $aTemplate['javascript_on'] = (isset($aProps['config']['javascript_on'])) ? $aProps['config']['javascript_on'] : false;
1051 $aTemplate['enablesort'] = (isset($aProps['config']['enablesort'])) ? $aProps['config']['enablesort'] : false;
1052 $aTemplate['icon_theme'] = (isset($aProps['config']['icon_theme'])) ? $aProps['config']['icon_theme'] : false;
1053 $aTemplate['use_icons'] = (isset($aProps['config']['use_icons'])) ? $aProps['config']['use_icons'] : false;
1054 $aTemplate['alt_index_colors'] = (isset($aProps['config']['alt_index_colors'])) ? $aProps['config']['alt_index_colors'] : false;
1055 $aTemplate['fancy_index_highlite'] = $fancy_index_highlite;
1056
1057 return $aTemplate;
1058 }
1059
1060
1061 /**
1062 * FIXME: Undocumented function
1063 */
1064 function truncateWithEntities($subject, $trim_at)
1065 {
1066 $ent_strlen = strlen($subject);
1067 if (($trim_at <= 0) || ($ent_strlen <= $trim_at))
1068 return $subject;
1069
1070 global $languages, $squirrelmail_language;
1071
1072 /*
1073 * see if this is entities-encoded string
1074 * If so, Iterate through the whole string, find out
1075 * the real number of characters, and if more
1076 * than $trim_at, substr with an updated trim value.
1077 */
1078 $trim_val = $trim_at;
1079 $ent_offset = 0;
1080 $ent_loc = 0;
1081 while ( $ent_loc < $trim_val && (($ent_loc = strpos($subject, '&', $ent_offset)) !== false) &&
1082 (($ent_loc_end = strpos($subject, ';', $ent_loc+3)) !== false) ) {
1083 $trim_val += ($ent_loc_end-$ent_loc);
1084 $ent_offset = $ent_loc_end+1;
1085 }
1086 if (($trim_val > $trim_at) && ($ent_strlen > $trim_val) && (strpos($subject,';',$trim_val) < ($trim_val + 6))) {
1087 $i = strpos($subject,';',$trim_val);
1088 if ($i) {
1089 $trim_val = strpos($subject,';',$trim_val);
1090 }
1091 }
1092 // only print '...' when we're actually dropping part of the subject
1093 if ($ent_strlen <= $trim_val)
1094 return $subject;
1095
1096 if (isset($languages[$squirrelmail_language]['XTRA_CODE']) &&
1097 function_exists($languages[$squirrelmail_language]['XTRA_CODE'] . '_strimwidth')) {
1098 return call_user_func($languages[$squirrelmail_language]['XTRA_CODE'] . '_strimwidth', $subject, $trim_val);
1099 }
1100
1101 return substr_replace($subject, '...', $trim_val);
1102 }
1103
1104 /**
1105 * This should go in imap_mailbox.php
1106 * @param string $mailbox
1107 */
1108 function handleAsSent($mailbox) {
1109 global $handleAsSent_result;
1110
1111 /* First check if this is the sent or draft folder. */
1112 $handleAsSent_result = isSentMailbox($mailbox) || isDraftMailbox($mailbox);
1113
1114 /* Then check the result of the handleAsSent hook. */
1115 do_hook('check_handleAsSent_result', $mailbox);
1116
1117 /* And return the result. */
1118 return $handleAsSent_result;
1119 }
1120
1121 /**
1122 * Process messages list form and handle the cache gracefully. If $sButton and
1123 * $aUid are provided as argument then you can fake a message list submit and
1124 * use it i.e. in read_body.php for del move next and update the cache
1125 *
1126 * @param resource $imapConnection imap connection
1127 * @param array $aMailbox (reference) cached mailbox
1128 * @param string $sButton fake a submit button
1129 * @param array $aUid fake the $msg array
1130 * @return string $sError error string in case of an error
1131 * @author Marc Groot Koerkamp
1132 */
1133 function handleMessageListForm($imapConnection,&$aMailbox,$sButton='',$aUid = array()) {
1134 /* incoming formdata */
1135 $sButton = (sqgetGlobalVar('moveButton', $sTmp, SQ_POST)) ? 'move' : $sButton;
1136 $sButton = (sqgetGlobalVar('expungeButton', $sTmp, SQ_POST)) ? 'expunge' : $sButton;
1137 $sButton = (sqgetGlobalVar('forward', $sTmp, SQ_POST)) ? 'forward' : $sButton;
1138 $sButton = (sqgetGlobalVar('delete', $sTmp, SQ_POST)) ? 'setDeleted' : $sButton;
1139 $sButton = (sqgetGlobalVar('undeleteButton', $sTmp, SQ_POST)) ? 'unsetDeleted' : $sButton;
1140 $sButton = (sqgetGlobalVar('markRead', $sTmp, SQ_POST)) ? 'setSeen' : $sButton;
1141 $sButton = (sqgetGlobalVar('markUnread', $sTmp, SQ_POST)) ? 'unsetSeen' : $sButton;
1142 $sButton = (sqgetGlobalVar('markFlagged', $sTmp, SQ_POST)) ? 'setFlagged' : $sButton;
1143 $sButton = (sqgetGlobalVar('markUnflagged', $sTmp, SQ_POST)) ? 'unsetFlagged' : $sButton;
1144 sqgetGlobalVar('targetMailbox', $targetMailbox, SQ_POST);
1145 sqgetGlobalVar('bypass_trash', $bypass_trash, SQ_POST);
1146 sqgetGlobalVar('msg', $msg, SQ_POST);
1147 if (sqgetGlobalVar('account', $iAccount, SQ_POST) === false) {
1148 $iAccount = 0;
1149 }
1150 $sError = '';
1151 $mailbox = $aMailbox['NAME'];
1152
1153 /* retrieve the check boxes */
1154 $aUid = (isset($msg) && is_array($msg)) ? array_values($msg) : $aUid;
1155 if (count($aUid) && $sButton != 'expunge') {
1156 $aUpdatedMsgs = false;
1157 $bExpunge = false;
1158 switch ($sButton) {
1159 case 'setDeleted':
1160 // check if id exists in case we come from read_body
1161 if (count($aUid) == 1 && is_array($aMailbox['UIDSET'][$aMailbox['SETINDEX']]) &&
1162 !in_array($aUid[0],$aMailbox['UIDSET'][$aMailbox['SETINDEX']])) {
1163 break;
1164 }
1165 $aUpdatedMsgs = sqimap_msgs_list_delete($imapConnection, $mailbox, $aUid,$bypass_trash);
1166 $bExpunge = true;
1167 //}
1168 break;
1169 case 'unsetDeleted':
1170 case 'setSeen':
1171 case 'unsetSeen':
1172 case 'setFlagged':
1173 case 'unsetFlagged':
1174 // get flag
1175 $sFlag = (substr($sButton,0,3) == 'set') ? '\\'.substr($sButton,3) : '\\'.substr($sButton,5);
1176 $bSet = (substr($sButton,0,3) == 'set') ? true : false;
1177 $aUpdatedMsgs = sqimap_toggle_flag($imapConnection, $aUid, $sFlag, $bSet, true);
1178 break;
1179 case 'move':
1180 $aUpdatedMsgs = sqimap_msgs_list_move($imapConnection,$aUid,$targetMailbox);
1181 sqsession_register($targetMailbox,'lastTargetMailbox');
1182 $bExpunge = true;
1183 break;
1184 case 'forward':
1185 $aMsgHeaders = array();
1186 foreach ($aUid as $iUid) {
1187 $aMsgHeaders[$iUid] = $aMailbox['MSG_HEADERS'][$iUid];
1188 }
1189 if (count($aMsgHeaders)) {
1190 $composesession = attachSelectedMessages($imapConnection,$aMsgHeaders);
1191 // dirty hack, add info to $aMailbox
1192 $aMailbox['FORWARD_SESSION'] = $composesession;
1193 }
1194 break;
1195 default:
1196 // Hook for plugin buttons
1197 do_hook_function('mailbox_display_button_action', $aUid);
1198 break;
1199 }
1200 /**
1201 * Updates messages is an array containing the result of the untagged
1202 * fetch responses send by the imap server due to a flag change. That
1203 * response is parsed in a array with msg arrays by the parseFetch function
1204 */
1205 if ($aUpdatedMsgs) {
1206 // Update the message headers cache
1207 $aDeleted = array();
1208 foreach ($aUpdatedMsgs as $iUid => $aMsg) {
1209 if (isset($aMsg['FLAGS'])) {
1210 /**
1211 * Only update the cached headers if the header is
1212 * cached.
1213 */
1214 if (isset($aMailbox['MSG_HEADERS'][$iUid])) {
1215 $aMailbox['MSG_HEADERS'][$iUid]['FLAGS'] = $aMsg['FLAGS'];
1216 }
1217 /**
1218 * Count the messages with the \Delete flag set so we can determine
1219 * if the number of expunged messages equals the number of flagged
1220 * messages for deletion.
1221 */
1222 if (isset($aMsg['FLAGS']['\\deleted']) && $aMsg['FLAGS']['\\deleted']) {
1223 $aDeleted[] = $iUid;
1224 }
1225 }
1226 }
1227 if ($bExpunge && $aMailbox['AUTO_EXPUNGE'] &&
1228 $iExpungedMessages = sqimap_mailbox_expunge($imapConnection, $aMailbox['NAME'], true))
1229 {
1230 if (count($aDeleted) != $iExpungedMessages) {
1231 // there are more messages deleted permanently then we expected
1232 // invalidate the cache
1233 $aMailbox['UIDSET'][$aMailbox['SETINDEX']] = false;
1234 $aMailbox['MSG_HEADERS'] = false;
1235 } else {
1236 // remove expunged messages from cache
1237 $aUidSet = $aMailbox['UIDSET'][$aMailbox['SETINDEX']];
1238 if (is_array($aUidSet)) {
1239 // create a UID => array index temp array
1240 $aUidSetDummy = array_flip($aUidSet);
1241 foreach ($aDeleted as $iUid) {
1242 // get the id as well in case of SQM_SORT_NONE
1243 if ($aMailbox['SORT'] == SQSORT_NONE) {
1244 $aMailbox['ID'] = false;
1245 //$iId = $aMailbox['MSG_HEADERS'][$iUid]['ID'];
1246 //unset($aMailbox['ID'][$iId]);
1247 }
1248 // unset the UID and message header
1249 unset($aUidSetDummy[$iUid]);
1250 unset($aMailbox['MSG_HEADERS'][$iUid]);
1251 }
1252 $aMailbox['UIDSET'][$aMailbox['SETINDEX']] = array_keys($aUidSetDummy);
1253 }
1254 }
1255 // update EXISTS info
1256 if ($iExpungedMessages) {
1257 $aMailbox['EXISTS'] -= (int) $iExpungedMessages;
1258 $aMailbox['TOTAL'][$aMailbox['SETINDEX']] -= (int) $iExpungedMessages;
1259 }
1260 if (($aMailbox['PAGEOFFSET']-1) >= $aMailbox['EXISTS']) {
1261 $aMailbox['PAGEOFFSET'] = ($aMailbox['PAGEOFFSET'] > $aMailbox['LIMIT']) ?
1262 $aMailbox['PAGEOFFSET'] - $aMailbox['LIMIT'] : 1;
1263 $aMailbox['OFFSET'] = $aMailbox['PAGEOFFSET'] - 1 ;
1264 }
1265 }
1266 }
1267 } else {
1268 if ($sButton == 'expunge') {
1269 /**
1270 * on expunge we do not know which messages will be deleted
1271 * so it's useless to try to sync the cache
1272
1273 * Close the mailbox so we do not need to parse the untagged expunge
1274 * responses which do not contain uid info.
1275 * NB: Closing a mailbox is faster then expunge because the imap
1276 * server does not need to generate the untagged expunge responses
1277 */
1278 sqimap_run_command($imapConnection,'CLOSE',false,$result,$message);
1279 $aMailbox = sqm_api_mailbox_select($imapConnection,$iAccount, $aMailbox['NAME'],array(),array());
1280 } else {
1281 if ($sButton) {
1282 $sError = _("No messages were selected.");
1283 }
1284 }
1285 }
1286 return $sError;
1287 }
1288
1289 function attachSelectedMessages($imapConnection,$aMsgHeaders) {
1290 global $username, $attachment_dir,
1291 $data_dir;
1292
1293
1294 sqgetGlobalVar('composesession', $composesession, SQ_SESSION);
1295 sqgetGlobalVar('compose_messages', $compose_messages, SQ_SESSION);
1296 if (!isset($compose_messages)|| is_null($compose_messages)) {
1297 $compose_messages = array();
1298 sqsession_register($compose_messages,'compose_messages');
1299 }
1300
1301 if (!$composesession) {
1302 $composesession = 1;
1303 sqsession_register($composesession,'composesession');
1304 } else {
1305 $composesession++;
1306 sqsession_register($composesession,'composesession');
1307 }
1308
1309 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
1310
1311 $composeMessage = new Message();
1312 $rfc822_header = new Rfc822Header();
1313 $composeMessage->rfc822_header = $rfc822_header;
1314 $composeMessage->reply_rfc822_header = '';
1315
1316 foreach($aMsgHeaders as $iUid => $aMsgHeader) {
1317 /**
1318 * Retrieve the full message
1319 */
1320 $body_a = sqimap_run_command($imapConnection, "FETCH $iUid RFC822", true, $response, $readmessage, TRUE);
1321 if ($response == 'OK') {
1322
1323 $subject = (isset($aMsgHeader['subject'])) ? $aMsgHeader['subject'] : $iUid;
1324
1325 array_shift($body_a);
1326 array_pop($body_a);
1327 $body = implode('', $body_a);
1328 $body .= "\r\n";
1329
1330 $localfilename = GenerateRandomString(32, 'FILE', 7);
1331 $full_localfilename = "$hashed_attachment_dir/$localfilename";
1332
1333 $fp = fopen( $full_localfilename, 'wb');
1334 fwrite ($fp, $body);
1335 fclose($fp);
1336 $composeMessage->initAttachment('message/rfc822',$subject.'.msg',
1337 $full_localfilename);
1338 }
1339 }
1340
1341 $compose_messages[$composesession] = $composeMessage;
1342 sqsession_register($compose_messages,'compose_messages');
1343 return $composesession;
1344 }
1345
1346 ?>