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