Allow options to use HTML in trailing text
[squirrelmail.git] / functions / mailbox_display.php
CommitLineData
59177427 1<?php
2ba13803 2
35586184 3/**
4b4abf93 4 * mailbox_display.php
5 *
6 * This contains functions that display mailbox information, such as the
7 * table row that has sender, date, subject, etc...
8 *
22387c8d 9 * @copyright 1999-2017 The SquirrelMail Project Team
4b4abf93 10 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
11 * @version $Id$
12 * @package squirrelmail
13 */
a4c2cd49 14
324ac3c5 15
16/**
17 * Selects a mailbox for header retrieval.
18 * Cache control for message headers is embedded.
19 *
20 * @param resource $imapConnection imap socket handle
21 * @param string $mailbox mailbox to select and retrieve message headers from
22 * @param array $aConfig array with system config settings and incoming vars
23 * @param array $aProps mailbox specific properties
d849b570 24 *
324ac3c5 25 * @return array $aMailbox mailbox array with all relevant information
d849b570 26 *
4955562f 27 * @since 1.5.1
324ac3c5 28 * @author Marc Groot Koerkamp
29 */
91c27aee 30function sqm_api_mailbox_select($imapConnection,$account,$mailbox,$aConfig,$aProps) {
31
324ac3c5 32 /**
33 * NB: retrieve this from the session before accessing this function
34 * and make sure you write it back at the end of the script after
35 * the aMailbox var is added so that the headers are added to the cache
36 */
37 global $mailbox_cache;
91c27aee 38
324ac3c5 39 $aDefaultConfigProps = array(
324ac3c5 40// 'charset' => 'US-ASCII',
41 'user' => false, /* no pref storage if false */
42 'setindex' => 0,
43// 'search' => 'ALL',
44 'max_cache_size' => SQM_MAX_MBX_IN_CACHE
45 );
46
47 $aConfig = array_merge($aDefaultConfigProps,$aConfig);
91c27aee 48
324ac3c5 49 $iSetIndx = $aConfig['setindex'];
50
51 $aMbxResponse = sqimap_mailbox_select($imapConnection, $mailbox);
52
53 if ($mailbox_cache) {
91c27aee 54 if (isset($mailbox_cache[$account.'_'.$mailbox])) {
55 $aCachedMailbox = $mailbox_cache[$account.'_'.$mailbox];
324ac3c5 56 } else {
57 $aCachedMailbox = false;
58 }
59 /* cleanup cache */
60 if (count($mailbox_cache) > $aConfig['max_cache_size'] -1) {
61 $aTime = array();
62 foreach($mailbox_cache as $cachedmailbox => $aVal) {
63 $aTime[$aVal['TIMESTAMP']] = $cachedmailbox;
64 }
65 if (ksort($aTime,SORT_NUMERIC)) {
66 for ($i=0,$iCnt=count($mailbox_cache);$i<($iCnt-$aConfig['max_cache_size']);++$i) {
67 $sOldestMbx = array_shift($aTime);
68 /**
69 * Remove only the UIDSET and MSG_HEADERS from cache because those can
70 * contain large amounts of data.
71 */
72 if (isset($mailbox_cache[$sOldestMbx]['UIDSET'])) {
73 $mailbox_cache[$sOldestMbx]['UIDSET']= false;
74 }
75 if (isset($mailbox_cache[$sOldestMbx]['MSG_HEADERS'])) {
76 $mailbox_cache[$sOldestMbx]['MSG_HEADERS'] = false;
77 }
78 }
79 }
80 }
81
82 } else {
83 $aCachedMailbox = false;
84 }
85
86 /**
87 * Deal with imap servers that do not return the required UIDNEXT or
88 * UIDVALIDITY response
89 * from a SELECT call (since rfc 3501 it's required).
90 */
91 if (!isset($aMbxResponse['UIDNEXT']) || !isset($aMbxResponse['UIDVALIDITY'])) {
92 $aStatus = sqimap_status_messages($imapConnection,$mailbox,
93 array('UIDNEXT','UIDVALIDITY'));
94 $aMbxResponse['UIDNEXT'] = $aStatus['UIDNEXT'];
95 $aMbxResponse['UIDVALIDTY'] = $aStatus['UIDVALIDITY'];
96 }
97
91c27aee 98 $aMailbox['ACCOUNT'] = $account;
324ac3c5 99 $aMailbox['UIDSET'][$iSetIndx] = false;
100 $aMailbox['ID'] = false;
101 $aMailbox['SETINDEX'] = $iSetIndx;
c3731db5 102 $aMailbox['MSG_HEADERS'] = false;
324ac3c5 103
104 if ($aCachedMailbox) {
105 /**
106 * Validate integrity of cached data
107 */
108 if ($aCachedMailbox['EXISTS'] == $aMbxResponse['EXISTS'] &&
109 $aMbxResponse['EXISTS'] &&
110 $aCachedMailbox['UIDVALIDITY'] == $aMbxResponse['UIDVALIDITY'] &&
111 $aCachedMailbox['UIDNEXT'] == $aMbxResponse['UIDNEXT'] &&
112 isset($aCachedMailbox['SEARCH'][$iSetIndx]) &&
113 (!isset($aConfig['search']) || /* always set search from the searchpage */
114 $aCachedMailbox['SEARCH'][$iSetIndx] == $aConfig['search'])) {
115 if (isset($aCachedMailbox['MSG_HEADERS'])) {
116 $aMailbox['MSG_HEADERS'] = $aCachedMailbox['MSG_HEADERS'];
117 }
118 $aMailbox['ID'] = $aCachedMailbox['ID'];
119 if (isset($aCachedMailbox['UIDSET'][$iSetIndx]) && $aCachedMailbox['UIDSET'][$iSetIndx]) {
120 if (isset($aProps[MBX_PREF_SORT]) && $aProps[MBX_PREF_SORT] != $aCachedMailbox['SORT'] ) {
121 $newsort = $aProps[MBX_PREF_SORT];
122 $oldsort = $aCachedMailbox['SORT'];
123 /**
124 * If it concerns a reverse sort we do not need to invalidate
125 * the cached sorted UIDSET, a reverse is sufficient.
126 */
127 if ((($newsort % 2) && ($newsort + 1 == $oldsort)) ||
128 (!($newsort % 2) && ($newsort - 1 == $oldsort))) {
129 $aMailbox['UIDSET'][$iSetIndx] = array_reverse($aCachedMailbox['UIDSET'][$iSetIndx]);
130 } else {
91c27aee 131 $server_sort_array = false;
324ac3c5 132 $aMailbox['MSG_HEADERS'] = false;
133 $aMailbox['ID'] = false;
134 }
135 // store the new sort value in the mailbox pref
136 if ($aConfig['user']) {
137 // FIXME, in ideal situation, we write back the
138 // prefs at the end of the script
91c27aee 139 setUserPref($aConfig['user'],'pref_'.$account.'_'.$mailbox,serialize($aProps));
324ac3c5 140 }
141 } else {
142 $aMailbox['UIDSET'][$iSetIndx] = $aCachedMailbox['UIDSET'][$iSetIndx];
143 }
144 }
145 }
146 }
147 /**
148 * Restore the offset in the paginator if no new offset is provided.
149 */
150 if (isset($aMailbox['UIDSET'][$iSetIndx]) && !isset($aConfig['offset']) && $aCachedMailbox['OFFSET']) {
151 $aMailbox['OFFSET'] = $aCachedMailbox['OFFSET'];
152 $aMailbox['PAGEOFFSET'] = $aCachedMailbox['PAGEOFFSET'];
153 } else {
154 $aMailbox['OFFSET'] = (isset($aConfig['offset']) && $aConfig['offset']) ? $aConfig['offset'] -1 : 0;
155 $aMailbox['PAGEOFFSET'] = (isset($aConfig['offset']) && $aConfig['offset']) ? $aConfig['offset'] : 1;
156 }
91c27aee 157 /**
158 * Restore the number of messages in the result set
159 */
160 if (isset($aCachedMailbox['TOTAL'][$iSetIndx]) && $aCachedMailbox['TOTAL'][$iSetIndx]) {
161 $aMailbox['TOTAL'][$iSetIndx] = $aCachedMailbox['TOTAL'][$iSetIndx];
162 }
324ac3c5 163
164 /**
165 * Restore the showall value no new showall value is provided.
166 */
167 if (isset($aMailbox['UIDSET'][$iSetIndx]) && !isset($aConfig['showall']) &&
168 isset($aCachedMailbox['SHOWALL'][$iSetIndx]) && $aCachedMailbox['SHOWALL'][$iSetIndx]) {
169 $aMailbox['SHOWALL'][$iSetIndx] = $aCachedMailbox['SHOWALL'][$iSetIndx];
170 } else {
171 $aMailbox['SHOWALL'][$iSetIndx] = (isset($aConfig['showall']) && $aConfig['showall']) ? 1 : 0;
172 }
173
91c27aee 174 /**
175 * Restore the sort order if no new sort order is provided.
176 */
324ac3c5 177 if (!isset($aProps[MBX_PREF_SORT]) && isset($aCachedMailbox['SORT'])) {
178 $aMailbox['SORT'] = $aCachedMailbox['SORT'];
179 } else {
180 $aMailbox['SORT'] = (isset($aProps[MBX_PREF_SORT])) ? $aProps[MBX_PREF_SORT] : 0;
181 }
182
91c27aee 183 /**
184 * Restore the number of message to show per page when no new limit is provided
185 */
324ac3c5 186 if (!isset($aProps[MBX_PREF_LIMIT]) && isset($aCachedMailbox['LIMIT'])) {
187 $aMailbox['LIMIT'] = $aCachedMailbox['LIMIT'];
188 } else {
189 $aMailbox['LIMIT'] = (isset($aProps[MBX_PREF_LIMIT])) ? $aProps[MBX_PREF_LIMIT] : 15;
190 }
191
91c27aee 192 /**
193 * Restore the ordered columns to show when no new ordered columns are provided
194 */
195 if (!isset($aProps[MBX_PREF_COLUMNS]) && isset($aCachedMailbox['COLUMNS'])) {
196 $aMailbox['COLUMNS'] = $aCachedMailbox['COLUMNS'];
324ac3c5 197 } else {
91c27aee 198 $aMailbox['COLUMNS'] = (isset($aProps[MBX_PREF_COLUMNS])) ? $aProps[MBX_PREF_COLUMNS] :
199 array(SQM_COL_FLAGS,SQM_COL_FROM, SQM_COL_SUBJ, SQM_COL_FLAGS);
324ac3c5 200 }
201
d0b119a5 202 /**
203 * Restore the headers we fetch the last time. Saves intitialisation stuff in read_body.
204 */
205 $aMailbox['FETCHHEADERS'] = (isset($aCachedMailbox['FETCHHEADERS'])) ? $aCachedMailbox['FETCHHEADERS'] : null;
206
324ac3c5 207 if (!isset($aProps[MBX_PREF_AUTO_EXPUNGE]) && isset($aCachedMailbox['AUTO_EXPUNGE'])) {
208 $aMailbox['AUTO_EXPUNGE'] = $aCachedMailbox['AUTO_EXPUNGE'];
209 } else {
210 $aMailbox['AUTO_EXPUNGE'] = (isset($aProps[MBX_PREF_AUTO_EXPUNGE])) ? $aProps[MBX_PREF_AUTO_EXPUNGE] : false;
211 }
324ac3c5 212 if (!isset($aConfig['search']) && isset($aCachedMailbox['SEARCH'][$iSetIndx])) {
213 $aMailbox['SEARCH'][$iSetIndx] = $aCachedMailbox['SEARCH'][$iSetIndx];
7762b03c 214 } else if (isset($aConfig['search']) && isset($aCachedMailbox['SEARCH'][$iSetIndx]) &&
215 $aConfig['search'] != $aCachedMailbox['SEARCH'][$iSetIndx]) {
216 // reset the pageindex
217 $aMailbox['SEARCH'][$iSetIndx] = $aConfig['search'];
218 $aMailbox['OFFSET'] = 0;
219 $aMailbox['PAGEOFFSET'] = 1;
324ac3c5 220 } else {
221 $aMailbox['SEARCH'][$iSetIndx] = (isset($aConfig['search'])) ? $aConfig['search'] : 'ALL';
222 }
324ac3c5 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
324ac3c5 238 /* decide if we are thread sorting or not */
324ac3c5 239 if ($aMailbox['SORT'] & SQSORT_THREAD) {
91c27aee 240 if (!sqimap_capability($imapConnection,'THREAD')) {
241 $aMailbox['SORT'] ^= SQSORT_THREAD;
242 } else {
243 $aMailbox['THREAD_INDENT'] = $aCachedMailbox['THREAD_INDENT'];
244 }
324ac3c5 245 } else {
324ac3c5 246 $aMailbox['THREAD_INDENT'] = false;
247 }
248
249 /* set a timestamp for cachecontrol */
250 $aMailbox['TIMESTAMP'] = time();
251 return $aMailbox;
252}
253
c7df3f1b 254/**
91c27aee 255 * Fetch the message headers for a mailbox. Settings are part of the aMailbox
4955562f 256 * array. Dependent of the mailbox settings it deals with sort, thread and search
257 * If server sort is supported then SORT is also used for retrieving sorted search results
62f7daa5 258 *
91c27aee 259 * @param resource $imapConnection imap socket handle
260 * @param array $aMailbox (reference) mailbox retrieved from sqm_api_mailbox_select
261 * @return error $error error number
4955562f 262 * @since 1.5.1
91c27aee 263 * @author Marc Groot Koerkamp
62f7daa5 264 */
d0b119a5 265function fetchMessageHeaders($imapConnection, &$aMailbox) {
e0e30169 266
91c27aee 267 /* FIX ME, this function is kind of big, maybe we can split it up in
268 a couple of functions. Make sure the functions are private and starts with _
269 Also make sure that the error codes are propagated */
324ac3c5 270
271 /**
272 * Retrieve the UIDSET.
273 * Setindex is used to be able to store multiple uid sets. That will make it
274 * possible to display the mailbox multiple times in different sort order
275 * or to store serach results separate from normal mailbox view.
276 */
277 $iSetIndx = (isset($aMailbox['SETINDEX'])) ? $aMailbox['SETINDEX'] : 0;
278
279 $iLimit = ($aMailbox['SHOWALL'][$iSetIndx]) ? $aMailbox['EXISTS'] : $aMailbox['LIMIT'];
280 /**
281 * Adjust the start_msg
282 */
283 $start_msg = $aMailbox['PAGEOFFSET'];
284 if($aMailbox['PAGEOFFSET'] > $aMailbox['EXISTS']) {
285 $start_msg -= $aMailbox['LIMIT'];
286 if($start_msg < 1) {
287 $start_msg = 1;
288 }
289 }
d0b119a5 290
324ac3c5 291 if (is_array($aMailbox['UIDSET'])) {
292 $aUid =& $aMailbox['UIDSET'][$iSetIndx];
f6b262a3 293 } else {
324ac3c5 294 $aUid = false;
295 }
d0b119a5 296 $aFetchHeaders = $aMailbox['FETCHHEADERS'];
324ac3c5 297
91c27aee 298 $iError = 0;
299 $aFetchItems = $aHeaderItems = array();
324ac3c5 300 // initialize the fields we want to retrieve:
b4246036 301 $aHeaderFields = array();
91c27aee 302 foreach ($aFetchHeaders as $v) {
303 switch ($v) {
304 case SQM_COL_DATE: $aHeaderFields[] = 'Date'; break;
305 case SQM_COL_TO: $aHeaderFields[] = 'To'; break;
306 case SQM_COL_CC: $aHeaderFields[] = 'Cc'; break;
307 case SQM_COL_FROM: $aHeaderFields[] = 'From'; break;
308 case SQM_COL_SUBJ: $aHeaderFields[] = 'Subject'; break;
309 case SQM_COL_PRIO: $aHeaderFields[] = 'X-Priority'; break;
310 case SQM_COL_ATTACHMENT: $aHeaderFields[] = 'Content-Type'; break;
311 case SQM_COL_INT_DATE: $aFetchItems[] = 'INTERNALDATE'; break;
312 case SQM_COL_FLAGS: $aFetchItems[] = 'FLAGS'; break;
313 case SQM_COL_SIZE: $aFetchItems[] = 'RFC822.SIZE'; break;
314 default: break;
315 }
324ac3c5 316 }
317
324ac3c5 318 /**
319 * A uidset with sorted uid's is available. We can use the cache
320 */
91c27aee 321 if (isset($aUid) && $aUid ) {
324ac3c5 322 // limit the cache to SQM_MAX_PAGES_IN_CACHE
91c27aee 323 if (!$aMailbox['SHOWALL'][$iSetIndx] && isset($aMailbox['MSG_HEADERS'])) {
324ac3c5 324 $iMaxMsgs = $iLimit * SQM_MAX_PAGES_IN_CACHE;
325 $iCacheSize = count($aMailbox['MSG_HEADERS']);
326 if ($iCacheSize > $iMaxMsgs) {
327 $iReduce = $iCacheSize - $iMaxMsgs;
328 foreach ($aMailbox['MSG_HEADERS'] as $iUid => $value) {
329 if ($iReduce) {
330 unset($aMailbox['MSG_HEADERS'][$iUid]);
331 } else {
332 break;
333 }
334 --$iReduce;
335 }
6a622b59 336 }
e9e5f0fa 337 }
324ac3c5 338
339 $id_slice = array_slice($aUid,$start_msg-1,$iLimit);
340 /* do some funky cache checks */
6ab20f61 341 if (isset($aMailbox['MSG_HEADERS']) && is_array($aMailbox['MSG_HEADERS'])) {
fe1521ef 342 // temp code, read_body del / next links fo not update fields.
343 foreach ($aMailbox['MSG_HEADERS'] as $iUid => $aValue) {
344 if (!isset($aValue['UID'])) {
345 unset($aMailbox['MSG_HEADERS'][$iUid]);
346 }
347 }
91c27aee 348 $aUidCached = array_keys($aMailbox['MSG_HEADERS']);
349 } else {
350 $aMailbox['MSG_HEADERS'] = array();
351 $aUidCached = array();
352 }
324ac3c5 353 $aUidNotCached = array_values(array_diff($id_slice,$aUidCached));
91c27aee 354
324ac3c5 355 /**
356 * $aUidNotCached contains an array with UID's which need to be fetched to
357 * complete the needed message headers.
358 */
359 if (count($aUidNotCached)) {
360 $aMsgs = sqimap_get_small_header_list($imapConnection,$aUidNotCached,
361 $aHeaderFields,$aFetchItems);
362 // append the msgs to the existend headers
363 $aMailbox['MSG_HEADERS'] += $aMsgs;
8cc8ec79 364 }
324ac3c5 365 } else {
366 /**
91c27aee 367 * Initialize the sorted UID list or initiate a UID list with search
368 * results and fetch the visible message headers
324ac3c5 369 */
324ac3c5 370
91c27aee 371 if ($aMailbox['SEARCH'][$iSetIndx] != 'ALL') { // in case of a search request
372
324ac3c5 373 if ($aMailbox['SEARCH'][$iSetIndx] && $aMailbox['SORT'] == 0) {
374 $aUid = sqimap_run_search($imapConnection, $aMailbox['SEARCH'][$iSetIndx], $aMailbox['CHARSET'][$iSetIndx]);
375 } else {
91c27aee 376
377 $iError = 0;
378 $iError = _get_sorted_msgs_list($imapConnection,$aMailbox,$iError);
324ac3c5 379 $aUid = $aMailbox['UIDSET'][$iSetIndx];
03975a39 380 }
91c27aee 381 if (!$iError) {
382 /**
383 * Number of messages is the resultset
384 */
385 $aMailbox['TOTAL'][$iSetIndx] = count($aUid);
324ac3c5 386 $id_slice = array_slice($aUid,$aMailbox['OFFSET'], $iLimit);
387 if (count($id_slice)) {
388 $aMailbox['MSG_HEADERS'] = sqimap_get_small_header_list($imapConnection,$id_slice,
389 $aHeaderFields,$aFetchItems);
390 } else {
91c27aee 391 $iError = 1; // FIX ME, define an error code
324ac3c5 392 }
03975a39 393 }
91c27aee 394 } else { //
395 $iError = 0;
396 $iError = _get_sorted_msgs_list($imapConnection,$aMailbox,$iError);
397 $aUid = $aMailbox['UIDSET'][$iSetIndx];
398
399 if (!$iError) {
400 /**
401 * Number of messages is the resultset
402 */
403 $aMailbox['TOTAL'][$iSetIndx] = count($aUid);
404 $id_slice = array_slice($aUid,$aMailbox['OFFSET'], $iLimit);
405 if (count($id_slice)) {
406 $aMailbox['MSG_HEADERS'] = sqimap_get_small_header_list($imapConnection,$id_slice,
407 $aHeaderFields,$aFetchItems);
324ac3c5 408 } else {
91c27aee 409 $iError = 1; // FIX ME, define an error code
324ac3c5 410 }
411 }
8cc8ec79 412 }
8cc8ec79 413 }
91c27aee 414 return $iError;
0fdc2fb6 415}
a966982b 416
4955562f 417/**
418 * Prepares the message headers for display inside a template. The links are calculated,
419 * color for row highlighting is calculated and optionally the strings are truncated.
420 *
421 * @param array $aMailbox (reference) mailbox retrieved from sqm_api_mailbox_select
422 * @param array $aProps properties
423 * @return array $aFormattedMessages array with message headers and format info
424 * @since 1.5.1
425 * @author Marc Groot Koerkamp
426 */
91c27aee 427function prepareMessageList(&$aMailbox, $aProps) {
84bb1d31 428
429 /* Globalize link attributes so plugins can share in modifying them */
02def6a1 430 global $link, $title, $target, $onclick, $link_extra, $preselected;
84bb1d31 431
91c27aee 432 /* retrieve the properties */
433 $my_email_address = (isset($aProps['email'])) ? $aProps['email'] : false;
434 $highlight_list = (isset($aProps['config']['highlight_list'])) ? $aProps['config']['highlight_list'] : false;
435 $aColumnDesc = (isset($aProps['columns'])) ? $aProps['columns'] : false;
436 $aExtraColumns = (isset($aProps['extra_columns'])) ? $aProps['extra_columns'] : array();
437 $iAccount = (isset($aProps['account'])) ? (int) $aProps['account'] : 0;
438 $sMailbox = (isset($aProps['mailbox'])) ? $aProps['mailbox'] : false;
439 $sTargetModule = (isset($aProps['module'])) ? $aProps['module'] : 'read_body';
a966982b 440
91c27aee 441 /*
442 * TODO 1, retrieve array with identity email addresses in order to match against to,cc and set a flag
443 * $aFormattedMessages[$iUid]['match_identity'] = true
444 * The template can show some image if there is a match.
445 * TODO 2, makes sure the matching is done fast by doing a strpos call on the returned $value
446 */
8cc8ec79 447
91c27aee 448 /**
449 * Only retrieve values for displayable columns
450 */
451 foreach ($aColumnDesc as $k => $v) {
452 switch ($k) {
453 case SQM_COL_FROM: $aCol[SQM_COL_FROM] = 'from'; break;
454 case SQM_COL_DATE: $aCol[SQM_COL_DATE] = 'date'; break;
455 case SQM_COL_SUBJ: $aCol[SQM_COL_SUBJ] = 'subject'; break;
456 case SQM_COL_FLAGS: $aCol[SQM_COL_FLAGS] = 'FLAGS'; break;
457 case SQM_COL_SIZE: $aCol[SQM_COL_SIZE] = 'SIZE'; break;
458 case SQM_COL_PRIO: $aCol[SQM_COL_PRIO] = 'x-priority'; break;
459 case SQM_COL_ATTACHMENT: $aCol[SQM_COL_ATTACHMENT] = 'content-type'; break;
460 case SQM_COL_INT_DATE: $aCol[SQM_COL_INT_DATE] = 'INTERNALDATE'; break;
461 case SQM_COL_TO: $aCol[SQM_COL_TO] = 'to'; break;
462 case SQM_COL_CC: $aCol[SQM_COL_CC] = 'cc'; break;
463 case SQM_COL_BCC: $aCol[SQM_COL_BCC] = 'bcc'; break;
464 default: break;
465 }
466 }
83eb12fc 467 $aExtraHighLightColumns = array();
468 foreach ($aExtraColumns as $v) {
469 switch ($v) {
470 case SQM_COL_FROM: $aExtraHighLightColumns[] = 'from'; break;
471 case SQM_COL_SUBJ: $aExtraHighLightColumns[] = 'subject'; break;
472 case SQM_COL_TO: $aExtraHighLightColumns[] = 'to'; break;
473 case SQM_COL_CC: $aExtraHighLightColumns[] = 'cc'; break;
474 case SQM_COL_BCC: $aExtraHighLightColumns[] = 'bcc'; break;
23541351 475 default: break;
476 }
477 }
91c27aee 478 $aFormattedMessages = array();
479
480
481 $iSetIndx = $aMailbox['SETINDEX'];
482 $aId = $aMailbox['UIDSET'][$iSetIndx];
483 $aHeaders =& $aMailbox['MSG_HEADERS']; /* use a reference to avoid a copy.
484 MSG_HEADERS can contain large amounts of data */
485 $iOffset = $aMailbox['OFFSET'];
486 $sort = $aMailbox['SORT'];
487 $iPageOffset = $aMailbox['PAGEOFFSET'];
488 $sMailbox = $aMailbox['NAME'];
489 $sSearch = (isset($aMailbox['SEARCH'][$aMailbox['SETINDEX']]) &&
490 $aMailbox['SEARCH'][$aMailbox['SETINDEX']] != 'ALL') ? $aMailbox['SEARCH'][$aMailbox['SETINDEX']] : false;
491 $aSearch = ($sSearch) ? array('search.php',$aMailbox['SETINDEX']) : null;
492 /* avoid improper usage */
493 if ($sMailbox && isset($iAccount) && $sTargetModule) {
494 $aInitQuery = array("account=$iAccount",'mailbox='.urlencode($sMailbox));
8cc8ec79 495 } else {
91c27aee 496 $aInitQuery = false;
8cc8ec79 497 }
498
91c27aee 499 if ($aMailbox['SORT'] & SQSORT_THREAD) {
500 $aIndentArray =& $aMailbox['THREAD_INDENT'][$aMailbox['SETINDEX']];
501 $bThread = true;
502 } else {
503 $bThread = false;
504 }
505 /*
506 * Retrieve value for checkbox column
507 */
508 if (!sqgetGlobalVar('checkall',$checkall,SQ_GET)) {
509 $checkall = false;
e4b5f9d1 510 }
511
91c27aee 512 /*
513 * Loop through and display the info for each message.
514 */
515 $iEnd = ($aMailbox['SHOWALL'][$iSetIndx]) ? $aMailbox['EXISTS'] : $iOffset + $aMailbox['LIMIT'];
516 for ($i=$iOffset,$t=0;$i<$iEnd;++$i) {
517 if (isset($aId[$i])) {
518
519 $bHighLight = false;
02e830bd 520 $value = $title = $link = $target = $onclick = $link_extra = '';
91c27aee 521 $aQuery = ($aInitQuery !== false) ? $aInitQuery : false;
522 $aMsg = $aHeaders[$aId[$i]];
523 if (isset($aSearch) && count($aSearch) > 1 && $aQuery) {
524 $aQuery[] = "where=". $aSearch[0];
525 $aQuery[] = "what=" . $aSearch[1];
8008456a 526 }
91c27aee 527 $iUid = (isset($aMsg['UID'])) ? $aMsg['UID'] : $aId[$i];
528 if ($aQuery) {
529 $aQuery[] = "passed_id=$aId[$i]";
530 $aQuery[] = "startMessage=$iPageOffset";
8008456a 531 }
91c27aee 532
533 foreach ($aCol as $k => $v) {
02e830bd 534 $title = $link = $target = $onclick = $link_extra = '';
91c27aee 535 $aColumns[$k] = array();
536 $value = (isset($aMsg[$v])) ? $aMsg[$v] : '';
537 $sUnknown = _("Unknown recipient");
538 switch ($k) {
539 case SQM_COL_FROM:
540 $sUnknown = _("Unknown sender");
541 case SQM_COL_TO:
542 case SQM_COL_CC:
543 case SQM_COL_BCC:
544 $sTmp = false;
545 if ($value) {
546 if ($highlight_list && !$bHighLight) {
547 $bHighLight = highlightMessage($aCol[$k], $value, $highlight_list,$aFormattedMessages[$iUid]);
548 }
582bbe27 549 $aAddressList = parseRFC822Address($value);
550 $sTmp = getAddressString($aAddressList,array('best' => true));
551 $title = $title_maybe = '';
552 foreach ($aAddressList as $aAddr) {
553 $sPersonal = (isset($aAddr[SQM_ADDR_PERSONAL])) ? $aAddr[SQM_ADDR_PERSONAL] : '';
554 $sMailbox = (isset($aAddr[SQM_ADDR_MAILBOX])) ? $aAddr[SQM_ADDR_MAILBOX] : '';
555 $sHost = (isset($aAddr[SQM_ADDR_HOST])) ? $aAddr[SQM_ADDR_HOST] : '';
556 if ($sPersonal) {
3047e291 557 $title .= sm_encode_html_special_chars($sMailbox.'@'.$sHost).', ';
582bbe27 558 } else {
559 // if $value gets truncated we need to add the addresses with no
560 // personal name as well
3047e291 561 $title_maybe .= sm_encode_html_special_chars($sMailbox.'@'.$sHost).', ';
582bbe27 562 }
563 }
564 if ($title) {
565 $title = substr($title,0,-2); // strip ', ';
566 }
86ef259b 567 $sTmp = decodeHeader($sTmp);
91c27aee 568 if (isset($aColumnDesc[$k]['truncate']) && $aColumnDesc[$k]['truncate']) {
c19e5483 569 $sTrunc = sm_truncate_string($sTmp, $aColumnDesc[$k]['truncate'], '...', TRUE);
582bbe27 570 if ($sTrunc != $sTmp) {
571 if (!$title) {
2b883a9a 572 $title = $sTmp;
582bbe27 573 } else if ($title_maybe) {
574 $title = $title .', '.$title_maybe;
575 $title = substr($title,0,-2); // strip ', ';
576 }
577 }
91c27aee 578 $sTmp = $sTrunc;
579 }
580 }
c04f123a 581 $value = ($sTmp) ? (substr($sTmp, 0, 6) == '&quot;' && substr($sTmp, -6) == '&quot;' ? substr(substr($sTmp, 0, -6), 6) : $sTmp) : $sUnknown;
91c27aee 582 break;
583 case SQM_COL_SUBJ:
d0b119a5 584 // subject is mime encoded, decode it.
129c22ed 585 // value is sanitized in decoding function.
86ef259b 586 // TODO, verify if it should be done before or after the highlighting
129c22ed 587 $value=decodeHeader($value);
91c27aee 588 if ($highlight_list && !$bHighLight) {
589 $bHighLight = highlightMessage('SUBJECT', $value, $highlight_list, $aFormattedMessages[$iUid]);
590 }
591 $iIndent = (isset($aIndentArray[$aId[$i]])) ? $aIndentArray[$aId[$i]] : 0;
129c22ed 592 // FIXME: don't break 8bit symbols and html entities during truncation
91c27aee 593 if (isset($aColumnDesc[$k]['truncate']) && $aColumnDesc[$k]['truncate']) {
c19e5483 594 $sTmp = sm_truncate_string($value, $aColumnDesc[$k]['truncate']-$iIndent, '...', TRUE);
887f7f28 595 // drop any double spaces since these will be displayed in the title
585eaee4 596 // Nah, it's nice to always have a roll-over
597 //$title = ($sTmp != $value) ? preg_replace('/\s{2,}/', ' ', $value) : '';
598 $title = preg_replace('/\s{2,}/', ' ', $value);
91c27aee 599 $value = $sTmp;
600 }
601 /* generate the link to the message */
602 if ($aQuery) {
603 // TODO, $sTargetModule should be a query parameter so that we can use a single entrypoint
604 $link = $sTargetModule.'.php?' . implode('&amp;',$aQuery);
02e830bd 605
84bb1d31 606 // see top of this function for which attributes are available
202bcbcc 607 // in the global scope for plugin use (like $link, $target,
84bb1d31 608 // $onclick, $link_extra, $title, and so forth)
202bcbcc 609 // plugins are responsible for sharing nicely (such as for
84bb1d31 610 // setting the target, etc)
0e525861 611 $temp = array(&$iPageOffset, &$sSearch, &$aSearch, $aMsg);
612 do_hook('subject_link', $temp);
91c27aee 613 }
91c27aee 614 $value = (trim($value)) ? $value : _("(no subject)");
615 /* add thread indentation */
616 $aColumns[$k]['indent'] = $iIndent;
91c27aee 617 break;
618 case SQM_COL_SIZE:
619 $value = show_readable_size($value);
620 break;
621 case SQM_COL_DATE:
622 case SQM_COL_INT_DATE:
d72549cb 623 $value = getTimeStamp(explode(' ',trim($value)));
624 $title = getDateString($value, TRUE);
625 $value = getDateString($value);
91c27aee 626 break;
627 case SQM_COL_FLAGS:
628 $aFlagColumn = array('seen' => false,
629 'deleted'=>false,
630 'answered'=>false,
1a753239 631 'forwarded'=>false,
91c27aee 632 'flagged' => false,
633 'draft' => false);
c3731db5 634
635 if(!is_array($value)) $value = array();
e961d845 636 foreach ($value as $sFlag => $v) {
91c27aee 637 switch ($sFlag) {
1a753239 638 case '\\seen' : $aFlagColumn['seen'] = true; break;
639 case '\\deleted' : $aFlagColumn['deleted'] = true; break;
640 case '\\answered': $aFlagColumn['answered'] = true; break;
641 case '$forwarded': $aFlagColumn['forwarded'] = true; break;
642 case '\\flagged' : $aFlagColumn['flagged'] = true; break;
643 case '\\draft' : $aFlagColumn['draft'] = true; break;
91c27aee 644 default: break;
645 }
646 }
647 $value = $aFlagColumn;
648 break;
649 case SQM_COL_PRIO:
650 $value = ($value) ? (int) $value : 3;
651 break;
652 case SQM_COL_ATTACHMENT:
653 $value = (is_array($value) && $value[0] == 'multipart' && $value[1] == 'mixed') ? true : false;
654 break;
655 case SQM_COL_CHECK:
02def6a1 656 $value = ($checkall || in_array($iUid, $preselected));
91c27aee 657 break;
658 default : break;
659 }
02e830bd 660 if ($title) { $aColumns[$k]['title'] = $title; }
661 if ($link) { $aColumns[$k]['link'] = $link; }
662 if ($link_extra) { $aColumns[$k]['link_extra'] = $link_extra; }
663 if ($onclick) { $aColumns[$k]['onclick'] = $onclick; }
664 if ($target) { $aColumns[$k]['target'] = $target; }
91c27aee 665 $aColumns[$k]['value'] = $value;
8008456a 666 }
91c27aee 667 /* columns which will not be displayed but should be inspected
668 because the highlight list contains rules with those columns */
83eb12fc 669 foreach ($aExtraHighLightColumns as $v) {
670 if ($highlight_list && !$bHighLight && isset($aMsg[$v])) {
671 $bHighLight = highlightMessage($v, $aMsg[$v], $highlight_list,$aFormattedMessages[$iUid]);
91c27aee 672 }
03975a39 673 }
91c27aee 674 $aFormattedMessages[$iUid]['columns'] = $aColumns;
675
676 } else {
8008456a 677 break;
678 }
bdfb67f8 679 }
91c27aee 680 return $aFormattedMessages;
bdfb67f8 681}
682
e9e5f0fa 683
4955562f 684/**
685 * Sets the row color if the provided column value pair matches a hightlight rule
686 *
687 * @param string $sCol column name
688 * @param string $sVal column value
689 * @param array $highlight_list highlight rules
690 * @param array $aFormat (reference) array where row color info is stored
691 * @return bool match found
692 * @since 1.5.1
693 * @author Marc Groot Koerkamp
694 */
91c27aee 695function highlightMessage($sCol, $sVal, $highlight_list, &$aFormat) {
91c27aee 696 if (!is_array($highlight_list) && count($highlight_list) == 0) {
697 return false;
698 }
699 $hlt_color = false;
700 $sCol = strtoupper($sCol);
83eb12fc 701
91c27aee 702 foreach ($highlight_list as $highlight_list_part) {
703 if (trim($highlight_list_part['value'])) {
704 $high_val = strtolower($highlight_list_part['value']);
705 $match_type = strtoupper($highlight_list_part['match_type']);
706 if($match_type == 'TO_CC') {
707 if ($sCol == 'TO' || $sCol == 'CC') {
708 $match_type = $sCol;
709 } else {
710 continue;
711 }
712 } else {
713 if ($match_type != $sCol) {
714 continue;
715 }
716 }
717 if (strpos(strtolower($sVal),$high_val) !== false) {
718 $hlt_color = $highlight_list_part['color'];
719 break;
720 }
721 }
722 }
723 if ($hlt_color) {
d0b119a5 724 // Bug in highlight color???
f3a1e5fa 725 if ($hlt_color{0} != '#') {
726 $hlt_color = '#'. $hlt_color;
727 }
91c27aee 728 $aFormat['row']['color'] = $hlt_color;
729 return true;
e35045b7 730 } else {
91c27aee 731 return false;
6c930ade 732 }
91c27aee 733}
6a622b59 734
91c27aee 735function setUserPref($username, $pref, $value) {
736 global $data_dir;
737 setPref($data_dir,$username,$pref,$value);
6a622b59 738}
739
c7df3f1b 740/**
4b4abf93 741 * Execute the sorting for a mailbox
742 *
743 * @param resource $imapConnection Imap connection
744 * @param array $aMailbox (reference) Mailbox retrieved with sqm_api_mailbox_select
745 * @return int $error (reference) Error number
746 * @private
4955562f 747 * @since 1.5.1
4b4abf93 748 * @author Marc Groot Koerkamp
749 */
91c27aee 750function _get_sorted_msgs_list($imapConnection,&$aMailbox) {
751 $iSetIndx = (isset($aMailbox['SETINDEX'])) ? $aMailbox['SETINDEX'] : 0;
49719da8 752 $bDirection = !($aMailbox['SORT'] % 2);
91c27aee 753 $error = 0;
754 if (!$aMailbox['SEARCH'][$iSetIndx]) {
755 $aMailbox['SEARCH'][$iSetIndx] = 'ALL';
756 }
757 if (($aMailbox['SORT'] & SQSORT_THREAD) && sqimap_capability($imapConnection,'THREAD')) {
758 $aRes = get_thread_sort($imapConnection,$aMailbox['SEARCH'][$iSetIndx]);
759 if ($aRes === false) {
760 $aMailbox['SORT'] -= SQSORT_THREAD;
761 $error = 1; // fix me, define an error code;
6a622b59 762 } else {
91c27aee 763 $aMailbox['UIDSET'][$iSetIndx] = $aRes[0];
764 $aMailbox['THREAD_INDENT'][$iSetIndx] = $aRes[1];
a6d36aa5 765 }
91c27aee 766 } else if ($aMailbox['SORT'] === SQSORT_NONE) {
767 $id = sqimap_run_search($imapConnection, 'ALL' , '');
768 if ($id === false) {
769 $error = 1; // fix me, define an error code
a6d36aa5 770 } else {
91c27aee 771 $aMailbox['UIDSET'][$iSetIndx] = array_reverse($id);
772 $aMailbox['TOTAL'][$iSetIndx] = $aMailbox['EXISTS'];
a6d36aa5 773 }
91c27aee 774 } else {
775 if (sqimap_capability($imapConnection,'SORT')) {
776 $sSortField = _getSortField($aMailbox['SORT'],true);
777 $id = sqimap_get_sort_order($imapConnection, $sSortField, $bDirection, $aMailbox['SEARCH'][$iSetIndx]);
778 if ($id === false) {
779 $error = 1; // fix me, define an error code
780 } else {
781 $aMailbox['UIDSET'][$iSetIndx] = $id;
782 }
6a622b59 783 } else {
91c27aee 784 $id = NULL;
785 if ($aMailbox['SEARCH'][$iSetIndx] != 'ALL') {
786 $id = sqimap_run_search($imapConnection, $aMailbox['SEARCH'][$iSetIndx], $aMailbox['CHARSET'][$iSetIndx]);
787 }
788 $sSortField = _getSortField($aMailbox['SORT'],false);
789 $aMailbox['UIDSET'][$iSetIndx] = get_squirrel_sort($imapConnection, $sSortField, $bDirection, $id);
6a622b59 790 }
e35045b7 791 }
91c27aee 792 return $error;
bdfb67f8 793}
1a0e0983 794
c7df3f1b 795/**
4b4abf93 796 * Does the $srt $_GET var to field mapping
797 *
798 * @param int $srt Field to sort on
799 * @param bool $bServerSort Server sorting is true
800 * @return string $sSortField Field to sort on
4955562f 801 * @since 1.5.1
4b4abf93 802 * @private
803 */
91c27aee 804function _getSortField($sort,$bServerSort) {
805 switch($sort) {
806 case SQSORT_NONE:
807 $sSortField = 'UID';
808 break;
809 case SQSORT_DATE_ASC:
810 case SQSORT_DATE_DESC:
811 $sSortField = 'DATE';
812 break;
813 case SQSORT_FROM_ASC:
814 case SQSORT_FROM_DESC:
815 $sSortField = 'FROM';
816 break;
817 case SQSORT_SUBJ_ASC:
818 case SQSORT_SUBJ_DESC:
819 $sSortField = 'SUBJECT';
820 break;
821 case SQSORT_SIZE_ASC:
822 case SQSORT_SIZE_DESC:
823 $sSortField = ($bServerSort) ? 'SIZE' : 'RFC822.SIZE';
824 break;
825 case SQSORT_TO_ASC:
826 case SQSORT_TO_DESC:
827 $sSortField = 'TO';
828 break;
829 case SQSORT_CC_ASC:
830 case SQSORT_CC_DESC:
831 $sSortField = 'CC';
832 break;
833 case SQSORT_INT_DATE_ASC:
834 case SQSORT_INT_DATE_DESC:
835 $sSortField = ($bServerSort) ? 'ARRIVAL' : 'INTERNALDATE';
836 break;
837 case SQSORT_THREAD:
838 break;
839 default: $sSortField = 'UID';
840 break;
841
6a622b59 842 }
91c27aee 843 return $sSortField;
bdfb67f8 844}
845
4955562f 846/**
847 * This function is a utility function for setting which headers should be
848 * fetched. It takes into account the highlight list which requires extra
849 * headers to be fetch in order to make those rules work. It's called before
850 * the headers are fetched which happens in showMessagesForMailbox and when
851 * the next and prev links in read_body.php are used.
852 *
853 * @param array $aMailbox associative array with mailbox related vars
854 * @param array $aProps
855 * @return void
856 * @since 1.5.1
857 */
858
c3731db5 859function calcFetchColumns(&$aMailbox, &$aProps) {
860
861 $highlight_list = (isset($aProps['config']['highlight_list'])) ? $aProps['config']['highlight_list'] : false;
862 $aColumnsDesc = (isset($aProps['columns'])) ? $aProps['columns'] : false;
863
864 $aFetchColumns = $aColumnsDesc;
865 if (isset($aFetchColumns[SQM_COL_CHECK])) {
866 unset($aFetchColumns[SQM_COL_CHECK]);
867 }
868
869 /*
870 * Before we fetch the message headers, check if we need to fetch extra columns
871 * to make the message highlighting work
872 */
873 if (is_array($highlight_list) && count($highlight_list)) {
874 $aHighlightColumns = array();
875 foreach ($highlight_list as $highlight_list_part) {
876 if (trim($highlight_list_part['value'])) {
877 $match_type = strtoupper($highlight_list_part['match_type']);
878 switch ($match_type) {
879 case 'TO_CC':
880 $aHighlightColumns[SQM_COL_TO] = true;
881 $aHighlightColumns[SQM_COL_CC] = true;
882 break;
883 case 'TO': $aHighlightColumns[SQM_COL_TO] = true; break;
884 case 'CC': $aHighlightColumns[SQM_COL_CC] = true; break;
885 case 'FROM': $aHighlightColumns[SQM_COL_FROM] = true; break;
886 case 'SUBJECT':$aHighlightColumns[SQM_COL_SUBJ] = true; break;
887 }
888 }
889 }
890 $aExtraColumns = array();
891 foreach ($aHighlightColumns as $k => $v) {
892 if (!isset($aFetchColumns[$k])) {
893 $aExtraColumns[] = $k;
894 $aFetchColumns[$k] = true;
895 }
896 }
897 if (count($aExtraColumns)) {
898 $aProps['extra_columns'] = $aExtraColumns;
899 }
900 }
901 $aMailbox['FETCHHEADERS'] = array_keys($aFetchColumns);
c3731db5 902}
6a622b59 903
6a622b59 904
91c27aee 905/**
4b4abf93 906 * This function loops through a group of messages in the mailbox
907 * and shows them to the user.
908 *
909 * @param resource $imapConnection
910 * @param array $aMailbox associative array with mailbox related vars
911 * @param array $aProps
912 * @param int $iError error code, 0 is no error
913 */
91c27aee 914function showMessagesForMailbox($imapConnection, &$aMailbox,$aProps, &$iError) {
915 global $PHP_SELF;
24bb7e49 916 global $boxes, $show_copy_buttons;
91c27aee 917
918 $highlight_list = (isset($aProps['config']['highlight_list'])) ? $aProps['config']['highlight_list'] : false;
919 $fancy_index_highlite = (isset($aProps['config']['fancy_index_highlite'])) ? $aProps['config']['fancy_index_highlite'] : true;
920 $aColumnsDesc = (isset($aProps['columns'])) ? $aProps['columns'] : false;
921 $iAccount = (isset($aProps['account'])) ? (int) $aProps['account'] : 0;
922 $sMailbox = (isset($aProps['mailbox'])) ? $aProps['mailbox'] : false;
923 $sTargetModule = (isset($aProps['module'])) ? $aProps['module'] : 'read_body';
924 $show_flag_buttons = (isset($aProps['config']['show_flag_buttons'])) ? $aProps['config']['show_flag_buttons'] : true;
24bb7e49 925
926 /* allows to control copy button in function call. If array key is not set, code follows user preferences */
927 if (isset($aProps['config']['show_copy_buttons']))
928 $show_copy_buttons = $aProps['config']['show_copy_buttons'];
929
91c27aee 930 $lastTargetMailbox = (isset($aProps['config']['lastTargetMailbox'])) ? $aProps['config']['lastTargetMailbox'] : '';
931 $aOrder = array_keys($aProps['columns']);
932 $trash_folder = (isset($aProps['config']['trash_folder']) && $aProps['config']['trash_folder'])
933 ? $aProps['config']['trash_folder'] : false;
934 $sent_folder = (isset($aProps['config']['sent_folder']) && $aProps['config']['sent_folder'])
935 ? $aProps['config']['sent_folder'] : false;
936 $draft_folder = (isset($aProps['config']['draft_folder']) && $aProps['config']['draft_folder'])
937 ? $aProps['config']['draft_folder'] : false;
938 $page_selector = (isset($aProps['config']['page_selector'])) ? $aProps['config']['page_selector'] : false;
939 $page_selector_max = (isset($aProps['config']['page_selector_max'])) ? $aProps['config']['page_selector_max'] : 10;
940 $color = $aProps['config']['color'];
6a622b59 941
6a622b59 942
91c27aee 943 /*
944 * Form ID
945 */
946 static $iFormId;
324ac3c5 947
91c27aee 948 if (!isset($iFormId)) {
949 $iFormId=1;
950 } else {
951 ++$iFormId;
952 }
d0b119a5 953 // store the columns to fetch so we can pick them up in read_body
91c27aee 954 // where we validate the cache.
c3731db5 955 calcFetchColumns($aMailbox ,$aProps);
e35045b7 956
d0b119a5 957 $iError = fetchMessageHeaders($imapConnection, $aMailbox);
91c27aee 958 if ($iError) {
959 return array();
960 } else {
961 $aMessages = prepareMessageList($aMailbox, $aProps);
962 }
6a622b59 963
91c27aee 964 $iSetIndx = $aMailbox['SETINDEX'];
965 $iLimit = ($aMailbox['SHOWALL'][$iSetIndx]) ? $aMailbox['EXISTS'] : $aMailbox['LIMIT'];
966 $iEnd = ($aMailbox['PAGEOFFSET'] + ($iLimit - 1) < $aMailbox['EXISTS']) ?
967 $aMailbox['PAGEOFFSET'] + $iLimit - 1 : $aMailbox['EXISTS'];
6a622b59 968
91c27aee 969 $iNumberOfMessages = $aMailbox['TOTAL'][$iSetIndx];
aa2e9274 970 $iEnd = min ( $iEnd, $iNumberOfMessages );
6a622b59 971
91c27aee 972 $php_self = $PHP_SELF;
6a622b59 973
91c27aee 974 $urlMailbox = urlencode($aMailbox['NAME']);
6a622b59 975
91c27aee 976 if (preg_match('/^(.+)\?.+$/',$php_self,$regs)) {
977 $source_url = $regs[1];
978 } else {
979 $source_url = $php_self;
980 }
6a622b59 981
f830c8ed 982 $baseurl = $source_url.'?mailbox=' . urlencode($aMailbox['NAME']) .'&amp;account='.$aMailbox['ACCOUNT'] . (strpos($source_url, 'src/search.php') ? '&amp;smtoken=' . sm_generate_security_token() : '');
91c27aee 983 $where = urlencode($aMailbox['SEARCH'][$iSetIndx][0]);
984 $what = urlencode($aMailbox['SEARCH'][$iSetIndx][1]);
985 $baseurl .= '&amp;where=' . $where . '&amp;what=' . $what;
986
987 /* build thread sorting links */
988 $newsort = $aMailbox['SORT'];
989 if (sqimap_capability($imapConnection,'THREAD')) {
990 if ($aMailbox['SORT'] & SQSORT_THREAD) {
991 $newsort -= SQSORT_THREAD;
992 $thread_name = _("Unthread View");
993 } else {
994 $thread_name = _("Thread View");
995 $newsort = $aMailbox['SORT'] + SQSORT_THREAD;
6a622b59 996 }
cd693cf4 997 $thread_link_uri = $baseurl . '&amp;srt=' . $newsort
998 . '&amp;startMessage=1';
324ac3c5 999 } else {
cd693cf4 1000 $thread_link_uri ='';
1001 $thread_name = '';
6a622b59 1002 }
91c27aee 1003 $sort = $aMailbox['SORT'];
6a622b59 1004
91c27aee 1005 /* FIX ME ADD CHECKBOX CONTROL. No checkbox => no buttons */
1006
1007
1008
1009 /* future admin control over displayable buttons */
91c27aee 1010 $aAdminControl = array(
91c27aee 1011 'markFlagged' => 1,
47485591 1012 'markUnflagged' => 1,
91c27aee 1013 'markRead' => 1,
1014 'markUnread' => 1,
a5d40e74 1015 'forward' => 1,
91c27aee 1016 'delete' => 1,
1017 'undeleteButton'=> 1,
1018 'bypass_trash' => 1,
1019 'expungeButton' => 1,
24bb7e49 1020 'moveButton' => 1,
1021 'copyButton' => 1
91c27aee 1022 );
24bb7e49 1023
91c27aee 1024 /* user prefs control */
1025 $aUserControl = array (
a5d40e74 1026
91c27aee 1027 'markFlagged' => $show_flag_buttons,
47485591 1028 'markUnflagged' => $show_flag_buttons,
91c27aee 1029 'markRead' => 1,
1030 'markUnread' => 1,
a5d40e74 1031 'forward' => 1,
91c27aee 1032 'delete' => 1,
1033 'undeleteButton'=> 1,
1034 'bypass_trash' => 1,
1035 'expungeButton' => 1,
24bb7e49 1036 'moveButton' => 1,
1037 'copyButton' => $show_copy_buttons
a5d40e74 1038
91c27aee 1039 );
1040
1041 $showDelete = ($aMailbox['RIGHTS'] != 'READ-ONLY' &&
1042 in_array('\\deleted',$aMailbox['PERMANENTFLAGS'], true)) ? true : false;
a5d40e74 1043 $showByPassTrash = (($aMailbox['AUTO_EXPUNGE'] && $aMailbox['RIGHTS'] != 'READ-ONLY' &&
91c27aee 1044 in_array('\\deleted',$aMailbox['PERMANENTFLAGS'], true)) &&
a5d40e74 1045 $trash_folder) ? true : false; //
1046
91c27aee 1047 $showUndelete = (!$aMailbox['AUTO_EXPUNGE'] && $aMailbox['RIGHTS'] != 'READ-ONLY' &&
b86c4939 1048 in_array('\\deleted',$aMailbox['PERMANENTFLAGS'], true) /* trash folder unrelated methinks: && !$trash_folder*/) ? true : false;
91c27aee 1049 $showMove = ($aMailbox['RIGHTS'] != 'READ-ONLY') ? true : false;
1050 $showExpunge = (!$aMailbox['AUTO_EXPUNGE'] && $aMailbox['RIGHTS'] != 'READ-ONLY' &&
1051 in_array('\\deleted',$aMailbox['PERMANENTFLAGS'], true)) ? true : false;
24bb7e49 1052
1053 /* Button options that depend on IMAP server and selected folder */
91c27aee 1054 $aImapControl = array (
1055 'markUnflagged' => in_array('\\flagged',$aMailbox['PERMANENTFLAGS'], true),
1056 'markFlagged' => in_array('\\flagged',$aMailbox['PERMANENTFLAGS'], true),
1057 'markRead' => in_array('\\seen',$aMailbox['PERMANENTFLAGS'], true),
1058 'markUnread' => in_array('\\seen',$aMailbox['PERMANENTFLAGS'], true),
a5d40e74 1059 'forward' => 1,
91c27aee 1060 'delete' => $showDelete,
1061 'undeleteButton'=> $showUndelete,
1062 'bypass_trash' => $showByPassTrash,
1063 'expungeButton' => $showExpunge,
24bb7e49 1064 'moveButton' => $showMove,
1065 'copyButton' => 1
91c27aee 1066 );
24bb7e49 1067 /* Button strings */
91c27aee 1068 $aButtonStrings = array(
47485591 1069 'markFlagged' => _("Flag"),
1070 'markUnflagged' => _("Unflag"),
1071 'markRead' => _("Read"),
1072 'markUnread' => _("Unread"),
1073 'forward' => _("Forward"),
1074 'delete' => _("Delete"),
1075 'undeleteButton' => _("Undelete"),
1076 'bypass_trash' => _("Bypass Trash"),
1077 'expungeButton' => _("Expunge"),
1078 'moveButton' => _("Move"),
1079 'copyButton' => _("Copy")
1080 );
1081 /* Button access keys */
1082 global $accesskey_mailbox_flag, $accesskey_mailbox_unflag,
1083 $accesskey_mailbox_read, $accesskey_mailbox_unread,
1084 $accesskey_mailbox_forward, $accesskey_mailbox_delete,
1085 $accesskey_mailbox_undelete, $accesskey_mailbox_bypass_trash,
1086 $accesskey_mailbox_expunge, $accesskey_mailbox_move,
1087 $accesskey_mailbox_copy, $accesskey_mailbox_move_to;
1088 $aButtonAccessKeys = array(
1089 'markFlagged' => $accesskey_mailbox_flag,
1090 'markUnflagged' => $accesskey_mailbox_unflag,
1091 'markRead' => $accesskey_mailbox_read,
1092 'markUnread' => $accesskey_mailbox_unread,
1093 'forward' => $accesskey_mailbox_forward,
1094 'delete' => $accesskey_mailbox_delete,
1095 'undeleteButton' => $accesskey_mailbox_undelete,
1096 'bypass_trash' => $accesskey_mailbox_bypass_trash,
1097 'expungeButton' => $accesskey_mailbox_expunge,
1098 'moveButton' => $accesskey_mailbox_move,
1099 'copyButton' => $accesskey_mailbox_copy,
91c27aee 1100 );
a5d40e74 1101
1102
6a622b59 1103 /**
91c27aee 1104 * Register buttons in order to an array
1105 * The key is the "name", the first element of the value array is the "value", second argument is the type.
62f7daa5 1106 */
91c27aee 1107 $aFormElements = array();
1108 foreach($aAdminControl as $k => $v) {
1109 if ($v & $aUserControl[$k] & $aImapControl[$k]) {
1110 switch ($k) {
91c27aee 1111 case 'markFlagged':
47485591 1112 case 'markUnflagged':
91c27aee 1113 case 'markRead':
1114 case 'markUnread':
1115 case 'delete':
1116 case 'undeleteButton':
1117 case 'expungeButton':
1118 case 'forward':
4127171c 1119 $aFormElements[$k]
47485591 1120 = array('value' => $aButtonStrings[$k], 'type' => 'submit', 'accesskey' => (isset($aButtonAccessKeys[$k]) ? $aButtonAccessKeys[$k] : 'NONE'));
91c27aee 1121 break;
1122 case 'bypass_trash':
4127171c 1123 $aFormElements[$k]
47485591 1124 = array('value' => $aButtonStrings[$k], 'type' => 'checkbox', 'accesskey' => (isset($aButtonAccessKeys[$k]) ? $aButtonAccessKeys[$k] : 'NONE'));
91c27aee 1125 break;
1126 case 'moveButton':
24bb7e49 1127 case 'copyButton':
4127171c 1128 $aFormElements['targetMailbox']
1129 = array('options_list' => sqimap_mailbox_option_list($imapConnection, array(strtolower($lastTargetMailbox)), 0, $boxes),
47485591 1130 'type' => 'select',
1131 'accesskey' => $accesskey_mailbox_move_to);
4127171c 1132 $aFormElements['mailbox']
1133 = array('value' => $aMailbox['NAME'], 'type' => 'hidden');
1134 $aFormElements['startMessage']
1135 = array('value' => $aMailbox['PAGEOFFSET'], 'type' => 'hidden');
1136 $aFormElements[$k]
47485591 1137 = array('value' => $aButtonStrings[$k], 'type' => 'submit', 'accesskey' => (isset($aButtonAccessKeys[$k]) ? $aButtonAccessKeys[$k] : 'NONE'));
91c27aee 1138 break;
0bb37159 1139 }
1140 }
4127171c 1141 $aFormElements['account'] = array('value' => $iAccount,'type' => 'hidden');
91c27aee 1142 }
4127171c 1143 do_hook('message_list_controls', $aFormElements);
0bb37159 1144
91c27aee 1145 /*
4b4abf93 1146 * This is the beginning of the message list table.
1147 * It wraps around all messages
1148 */
91c27aee 1149 $safe_name = preg_replace("/[^0-9A-Za-z_]/", '_', $aMailbox['NAME']);
1150 $form_name = "FormMsgs" . $safe_name;
0bb37159 1151
91c27aee 1152 //if (!sqgetGlobalVar('align',$align,SQ_SESSION)) {
1153 $align = array('left' => 'left', 'right' => 'right');
1154 //}
1155 //sm_print_r($align);
1156
1157 /* finally set the template vars */
1158
4127171c 1159// FIXME, before we support multiple templates we must review the names of the vars
1160// BUMP!
91c27aee 1161
1162
91c27aee 1163 $aTemplate['color'] = $color;
1164 $aTemplate['form_name'] = "FormMsgs" . $safe_name;
1165 $aTemplate['form_id'] = 'mbx_'.$iFormId;
1166 $aTemplate['page_selector'] = $page_selector;
1167 $aTemplate['page_selector_max'] = $page_selector_max;
1168 $aTemplate['messagesPerPage'] = $aMailbox['LIMIT'];
1169 $aTemplate['showall'] = $aMailbox['SHOWALL'][$iSetIndx];
1170 $aTemplate['end_msg'] = $iEnd;
1171 $aTemplate['align'] = $align;
1172 $aTemplate['iNumberOfMessages'] = $iNumberOfMessages;
1173 $aTemplate['aOrder'] = $aOrder;
1174 $aTemplate['aFormElements'] = $aFormElements;
1175 $aTemplate['sort'] = $sort;
1176 $aTemplate['pageOffset'] = $aMailbox['PAGEOFFSET'];
1177 $aTemplate['baseurl'] = $baseurl;
1178 $aTemplate['aMessages'] =& $aMessages;
1179 $aTemplate['trash_folder'] = $trash_folder;
1180 $aTemplate['sent_folder'] = $sent_folder;
1181 $aTemplate['draft_folder'] = $draft_folder;
cd693cf4 1182 $aTemplate['thread_link_uri'] = $thread_link_uri;
1183 $aTemplate['thread_name'] = $thread_name;
91c27aee 1184 $aTemplate['php_self'] = str_replace('&','&amp;',$php_self);
1185 $aTemplate['mailbox'] = $sMailbox;
83aff890 1186//FIXME: javascript_on is always assigned to the template object in places like init.php; is there some reason to reassign it here? is there some chance that it was changed? if not, please remove this line!
91c27aee 1187 $aTemplate['javascript_on'] = (isset($aProps['config']['javascript_on'])) ? $aProps['config']['javascript_on'] : false;
1188 $aTemplate['enablesort'] = (isset($aProps['config']['enablesort'])) ? $aProps['config']['enablesort'] : false;
1189 $aTemplate['icon_theme'] = (isset($aProps['config']['icon_theme'])) ? $aProps['config']['icon_theme'] : false;
1190 $aTemplate['use_icons'] = (isset($aProps['config']['use_icons'])) ? $aProps['config']['use_icons'] : false;
1191 $aTemplate['alt_index_colors'] = (isset($aProps['config']['alt_index_colors'])) ? $aProps['config']['alt_index_colors'] : false;
1192 $aTemplate['fancy_index_highlite'] = $fancy_index_highlite;
1193
49719da8 1194
4127171c 1195 /**
1196 * Set up sort possibilities; one could argue that this is best
1197 * placed in the template, but most template authors won't understand
1198 * or need to understand it, so some advanced templates can override
1199 * it if they do something different.
1200 */
1201 if (!($aTemplate['sort'] & SQSORT_THREAD) && $aTemplate['enablesort']) {
1202 $aTemplate['aSortSupported']
1203 = array(SQM_COL_SUBJ => array(SQSORT_SUBJ_ASC , SQSORT_SUBJ_DESC),
1204 SQM_COL_DATE => array(SQSORT_DATE_DESC , SQSORT_DATE_ASC),
1205 SQM_COL_INT_DATE => array(SQSORT_INT_DATE_DESC, SQSORT_INT_DATE_ASC),
1206 SQM_COL_FROM => array(SQSORT_FROM_ASC , SQSORT_FROM_DESC),
1207 SQM_COL_TO => array(SQSORT_TO_ASC , SQSORT_TO_DESC),
1208 SQM_COL_CC => array(SQSORT_CC_ASC , SQSORT_CC_DESC),
1209 SQM_COL_SIZE => array(SQSORT_SIZE_ASC , SQSORT_SIZE_DESC));
1210 } else {
1211 $aTemplate['aSortSupported'] = array();
1212 }
1213
1214
1215 /**
1216 * Figure out which columns should serve as labels for checkbox:
1217 * we try to grab the two columns before and after the checkbox,
1218 * except the subject column, since it is the link that opens
1219 * the message view
1220 *
1221 * if $javascript_on is set, then the highlighting code takes
1222 * care of this; just skip it
1223 *
1224 * This code also might be more appropriate in a template file, but
1225 * we are moving this complex stuff out of the way of template
1226 * authors; advanced template sets are always free to override
1227 * the resultant values.
1228 *
1229 */
1230 $show_label_columns = array();
1231 $index_order_part = array();
1232 if (!($aTemplate['javascript_on'] && $aTemplate['fancy_index_highlite'])) {
1233 $get_next_two = 0;
1234 $last_order_part = 0;
1235 $last_last_order_part = 0;
1236 foreach ($aTemplate['aOrder'] as $index_order_part) {
1237 if ($index_order_part == SQM_COL_CHECK) {
1238 $get_next_two = 1;
1239 if ($last_last_order_part != SQM_COL_SUBJ)
1240 $show_label_columns[] = $last_last_order_part;
1241 if ($last_order_part != SQM_COL_SUBJ)
1242 $show_label_columns[] = $last_order_part;
1243
1244 } else if ($get_next_two > 0 && $get_next_two < 3 && $index_order_part != SQM_COL_SUBJ) {
1245 $show_label_columns[] = $index_order_part;
1246 $get_next_two++;
1247 }
1248 $last_last_order_part = $last_order_part;
1249 $last_order_part = $index_order_part;
1250 }
1251 }
1252 $aTemplate['show_label_columns'] = $show_label_columns;
1253
1254
91c27aee 1255 return $aTemplate;
4127171c 1256
bdfb67f8 1257}
1258
91c27aee 1259
324ac3c5 1260/**
1261 * Process messages list form and handle the cache gracefully. If $sButton and
1262 * $aUid are provided as argument then you can fake a message list submit and
1263 * use it i.e. in read_body.php for del move next and update the cache
1264 *
1265 * @param resource $imapConnection imap connection
ee19932b 1266 * @param array $aMailbox (reference) cached mailbox
1267 * @param string $sButton fake a submit button
1268 * @param array $aUid fake the $msg array
1269 * @param string $targetMailbox fake the target mailbox for move operations
1270 * @param boolean $bypass_trash fake the bypass trash checkbox for delete operations
324ac3c5 1271 * @return string $sError error string in case of an error
4955562f 1272 * @since 1.5.1
324ac3c5 1273 * @author Marc Groot Koerkamp
1274 */
ee19932b 1275function handleMessageListForm($imapConnection, &$aMailbox, $sButton='',
1276 $aUid = array(), $targetMailbox='', $bypass_trash=NULL) {
324ac3c5 1277 /* incoming formdata */
c9b94b04 1278 $sButton = (sqgetGlobalVar('moveButton', $sTmp, SQ_FORM)) ? 'move' : $sButton;
1279 $sButton = (sqgetGlobalVar('copyButton', $sTmp, SQ_FORM)) ? 'copy' : $sButton;
1280 $sButton = (sqgetGlobalVar('expungeButton', $sTmp, SQ_FORM)) ? 'expunge' : $sButton;
1281 $sButton = (sqgetGlobalVar('forward', $sTmp, SQ_FORM)) ? 'forward' : $sButton;
1282 $sButton = (sqgetGlobalVar('delete', $sTmp, SQ_FORM)) ? 'setDeleted' : $sButton;
1283 $sButton = (sqgetGlobalVar('undeleteButton', $sTmp, SQ_FORM)) ? 'unsetDeleted' : $sButton;
1284 $sButton = (sqgetGlobalVar('markRead', $sTmp, SQ_FORM)) ? 'setSeen' : $sButton;
1285 $sButton = (sqgetGlobalVar('markUnread', $sTmp, SQ_FORM)) ? 'unsetSeen' : $sButton;
1286 $sButton = (sqgetGlobalVar('markFlagged', $sTmp, SQ_FORM)) ? 'setFlagged' : $sButton;
1287 $sButton = (sqgetGlobalVar('markUnflagged', $sTmp, SQ_FORM)) ? 'unsetFlagged' : $sButton;
ee19932b 1288 if (empty($targetMailbox)) sqgetGlobalVar('targetMailbox', $targetMailbox, SQ_FORM);
1289 if (is_null($bypass_trash)) sqgetGlobalVar('bypass_trash', $bypass_trash, SQ_FORM);
c9b94b04 1290 sqgetGlobalVar('msg', $msg, SQ_FORM);
1291 if (sqgetGlobalVar('account', $iAccount, SQ_FORM) === false) {
91c27aee 1292 $iAccount = 0;
1293 }
324ac3c5 1294 $sError = '';
1295 $mailbox = $aMailbox['NAME'];
1296
1297 /* retrieve the check boxes */
1298 $aUid = (isset($msg) && is_array($msg)) ? array_values($msg) : $aUid;
324ac3c5 1299 if (count($aUid) && $sButton != 'expunge') {
51bbe8fa 1300
199a9ab8 1301 // don't do anything to any messages until we have done security check
1302 // FIXME: not sure this code really belongs here, but there's nowhere else to put it with this architecture
7e6bff9b 1303 sqgetGlobalVar('smtoken', $submitted_token, SQ_FORM, '');
2cefa62a 1304 sm_validate_security_token($submitted_token, -1, TRUE);
199a9ab8 1305
51bbe8fa 1306 // make sure message UIDs are sanitized (BIGINT)
1307 foreach ($aUid as $i => $uid)
1308 $aUid[$i] = (preg_match('/^[0-9]+$/', $uid) ? $uid : '0');
1309
324ac3c5 1310 $aUpdatedMsgs = false;
1311 $bExpunge = false;
1312 switch ($sButton) {
1313 case 'setDeleted':
1314 // check if id exists in case we come from read_body
1315 if (count($aUid) == 1 && is_array($aMailbox['UIDSET'][$aMailbox['SETINDEX']]) &&
1316 !in_array($aUid[0],$aMailbox['UIDSET'][$aMailbox['SETINDEX']])) {
1317 break;
1318 }
7c788b1c 1319 $aUpdatedMsgs = sqimap_msgs_list_delete($imapConnection, $mailbox, $aUid,$bypass_trash);
1320 $bExpunge = true;
91c27aee 1321 //}
324ac3c5 1322 break;
1323 case 'unsetDeleted':
1324 case 'setSeen':
1325 case 'unsetSeen':
1326 case 'setFlagged':
1327 case 'unsetFlagged':
1328 // get flag
1329 $sFlag = (substr($sButton,0,3) == 'set') ? '\\'.substr($sButton,3) : '\\'.substr($sButton,5);
1330 $bSet = (substr($sButton,0,3) == 'set') ? true : false;
1331 $aUpdatedMsgs = sqimap_toggle_flag($imapConnection, $aUid, $sFlag, $bSet, true);
1332 break;
1333 case 'move':
821651ff 1334 $aUpdatedMsgs = sqimap_msgs_list_move($imapConnection,$aUid,$targetMailbox,true,$mailbox);
324ac3c5 1335 sqsession_register($targetMailbox,'lastTargetMailbox');
1336 $bExpunge = true;
1337 break;
24bb7e49 1338 case 'copy':
b400dda3 1339 // sqimap_msgs_list_copy returns true or false.
1340 // If error happens - fourth argument handles it inside function.
1341 sqimap_msgs_list_copy($imapConnection,$aUid,$targetMailbox,true);
24bb7e49 1342 sqsession_register($targetMailbox,'lastTargetMailbox');
1343 break;
91c27aee 1344 case 'forward':
324ac3c5 1345 $aMsgHeaders = array();
1346 foreach ($aUid as $iUid) {
1347 $aMsgHeaders[$iUid] = $aMailbox['MSG_HEADERS'][$iUid];
1348 }
1349 if (count($aMsgHeaders)) {
1350 $composesession = attachSelectedMessages($imapConnection,$aMsgHeaders);
1351 // dirty hack, add info to $aMailbox
e506b6e5 1352 $aMailbox['FORWARD_SESSION']['SESSION_NUMBER'] = $composesession;
1353 $aMailbox['FORWARD_SESSION']['UIDS'] = $aUid;
324ac3c5 1354 }
1355 break;
7c788b1c 1356 default:
91c27aee 1357 // Hook for plugin buttons
c1c0ce3e 1358 $temp = array(&$sButton, &$aMailbox, $iAccount, $aMailbox['NAME'], &$aUid);
b77cbccc 1359 do_hook('mailbox_display_button_action', $temp);
91c27aee 1360 break;
324ac3c5 1361 }
1362 /**
bdfa0f9a 1363 * $aUpdatedMsgs is an array containing the result of the untagged
324ac3c5 1364 * fetch responses send by the imap server due to a flag change. That
bdfa0f9a 1365 * response is parsed in an array with msg arrays by the parseFetch function
324ac3c5 1366 */
1367 if ($aUpdatedMsgs) {
1368 // Update the message headers cache
1369 $aDeleted = array();
1370 foreach ($aUpdatedMsgs as $iUid => $aMsg) {
1371 if (isset($aMsg['FLAGS'])) {
fd28fa79 1372 /**
ddd209f2 1373 * Only update the cached headers if the header is
fd28fa79 1374 * cached.
1375 */
1376 if (isset($aMailbox['MSG_HEADERS'][$iUid])) {
1377 $aMailbox['MSG_HEADERS'][$iUid]['FLAGS'] = $aMsg['FLAGS'];
1378 }
bdfa0f9a 1379 /**
1380 * Also update flags in message object
1381 */
1382//FIXME: WHY are we keeping flags in TWO places?!? This is error-prone and some core code uses the is_xxxx message object values while other code uses the flags array above. That's a mess.
1383 if (isset($aMailbox['MSG_HEADERS'][$iUid]['MESSAGE_OBJECT'])) {
1384 $message = $aMailbox['MSG_HEADERS'][$iUid]['MESSAGE_OBJECT'];
1385 $message->is_seen = false;
1386 $message->is_answered = false;
1a753239 1387 $message->is_forwarded = false;
bdfa0f9a 1388 $message->is_deleted = false;
1389 $message->is_flagged = false;
1390 $message->is_mdnsent = false;
1391 foreach ($aMsg['FLAGS'] as $flag => $value) {
1392 if (strtolower($flag) == '\\seen' && $value)
1393 $message->is_seen = true;
1394 else if (strtolower($flag) == '\\answered' && $value)
1395 $message->is_answered = true;
1a753239 1396 else if (strtolower($flag) == '$forwarded' && $value)
1397 $message->is_forwarded = true;
bdfa0f9a 1398 else if (strtolower($flag) == '\\deleted' && $value)
1399 $message->is_deleted = true;
1400 else if (strtolower($flag) == '\\flagged' && $value)
1401 $message->is_flagged = true;
1402 else if (strtolower($flag) == '$mdnsent' && $value)
1403 $message->is_mdnsent = true;
1404 }
1405 $aMailbox['MSG_HEADERS'][$iUid]['MESSAGE_OBJECT'] = $message;
1406 }
324ac3c5 1407 /**
1408 * Count the messages with the \Delete flag set so we can determine
1409 * if the number of expunged messages equals the number of flagged
1410 * messages for deletion.
1411 */
1412 if (isset($aMsg['FLAGS']['\\deleted']) && $aMsg['FLAGS']['\\deleted']) {
1413 $aDeleted[] = $iUid;
1414 }
1415 }
1416 }
1417 if ($bExpunge && $aMailbox['AUTO_EXPUNGE'] &&
1418 $iExpungedMessages = sqimap_mailbox_expunge($imapConnection, $aMailbox['NAME'], true))
1419 {
1420 if (count($aDeleted) != $iExpungedMessages) {
1421 // there are more messages deleted permanently then we expected
1422 // invalidate the cache
1423 $aMailbox['UIDSET'][$aMailbox['SETINDEX']] = false;
1424 $aMailbox['MSG_HEADERS'] = false;
1425 } else {
1426 // remove expunged messages from cache
1427 $aUidSet = $aMailbox['UIDSET'][$aMailbox['SETINDEX']];
1428 if (is_array($aUidSet)) {
1429 // create a UID => array index temp array
1430 $aUidSetDummy = array_flip($aUidSet);
1431 foreach ($aDeleted as $iUid) {
1432 // get the id as well in case of SQM_SORT_NONE
1433 if ($aMailbox['SORT'] == SQSORT_NONE) {
1434 $aMailbox['ID'] = false;
1435 //$iId = $aMailbox['MSG_HEADERS'][$iUid]['ID'];
1436 //unset($aMailbox['ID'][$iId]);
1437 }
1438 // unset the UID and message header
1439 unset($aUidSetDummy[$iUid]);
1440 unset($aMailbox['MSG_HEADERS'][$iUid]);
1441 }
1442 $aMailbox['UIDSET'][$aMailbox['SETINDEX']] = array_keys($aUidSetDummy);
324ac3c5 1443 }
1444 }
ddd209f2 1445 // update EXISTS info
1446 if ($iExpungedMessages) {
1447 $aMailbox['EXISTS'] -= (int) $iExpungedMessages;
91c27aee 1448 $aMailbox['TOTAL'][$aMailbox['SETINDEX']] -= (int) $iExpungedMessages;
ddd209f2 1449 }
4c284a74 1450 if (($aMailbox['PAGEOFFSET']-1) >= $aMailbox['EXISTS']) {
324ac3c5 1451 $aMailbox['PAGEOFFSET'] = ($aMailbox['PAGEOFFSET'] > $aMailbox['LIMIT']) ?
1452 $aMailbox['PAGEOFFSET'] - $aMailbox['LIMIT'] : 1;
91c27aee 1453 $aMailbox['OFFSET'] = $aMailbox['PAGEOFFSET'] - 1 ;
324ac3c5 1454 }
1455 }
1456 }
1457 } else {
1458 if ($sButton == 'expunge') {
1459 /**
1460 * on expunge we do not know which messages will be deleted
1461 * so it's useless to try to sync the cache
4b4abf93 1462 *
324ac3c5 1463 * Close the mailbox so we do not need to parse the untagged expunge
1464 * responses which do not contain uid info.
1465 * NB: Closing a mailbox is faster then expunge because the imap
1466 * server does not need to generate the untagged expunge responses
1467 */
1468 sqimap_run_command($imapConnection,'CLOSE',false,$result,$message);
91c27aee 1469 $aMailbox = sqm_api_mailbox_select($imapConnection,$iAccount, $aMailbox['NAME'],array(),array());
324ac3c5 1470 } else {
b262c520 1471 // this is the same hook as above, but here it is called in the
1472 // context of not having had any messages selected and if any
1473 // plugin handles the situation, it should return TRUE so we
1474 // know this was not an erroneous user action
1475 //
1476 global $null;
c1c0ce3e 1477 $temp = array(&$sButton, &$aMailbox, $iAccount, $aMailbox['NAME'], $null);
b77cbccc 1478 if (!boolean_hook_function('mailbox_display_button_action', $temp, 1)
b262c520 1479 && $sButton) {
324ac3c5 1480 $sError = _("No messages were selected.");
1481 }
1482 }
1483 }
1484 return $sError;
1485}
1486
4955562f 1487/**
1488 * Attach messages to a compose session
1489 *
1490 * @param resource $imapConnection imap connection
1491 * @param array $aMsgHeaders
1492 * @return int $composesession unique compose_session_id where the attached messages belong to
1493 * @author Marc Groot Koerkamp
1494 */
324ac3c5 1495function attachSelectedMessages($imapConnection,$aMsgHeaders) {
324ac3c5 1496
91c27aee 1497 sqgetGlobalVar('composesession', $composesession, SQ_SESSION);
1498 sqgetGlobalVar('compose_messages', $compose_messages, SQ_SESSION);
1499 if (!isset($compose_messages)|| is_null($compose_messages)) {
324ac3c5 1500 $compose_messages = array();
1501 sqsession_register($compose_messages,'compose_messages');
1502 }
1503
1504 if (!$composesession) {
1505 $composesession = 1;
1506 sqsession_register($composesession,'composesession');
1507 } else {
1508 $composesession++;
1509 sqsession_register($composesession,'composesession');
1510 }
1511
324ac3c5 1512 $composeMessage = new Message();
1513 $rfc822_header = new Rfc822Header();
1514 $composeMessage->rfc822_header = $rfc822_header;
1515 $composeMessage->reply_rfc822_header = '';
1516
1517 foreach($aMsgHeaders as $iUid => $aMsgHeader) {
1518 /**
1519 * Retrieve the full message
1520 */
1521 $body_a = sqimap_run_command($imapConnection, "FETCH $iUid RFC822", true, $response, $readmessage, TRUE);
324ac3c5 1522 if ($response == 'OK') {
91c27aee 1523
1524 $subject = (isset($aMsgHeader['subject'])) ? $aMsgHeader['subject'] : $iUid;
324ac3c5 1525
1526 array_shift($body_a);
1527 array_pop($body_a);
1528 $body = implode('', $body_a);
1529 $body .= "\r\n";
1530
1f270d3c 1531 global $username, $attachment_dir;
628bce99 1532 $filename = sq_get_attach_tempfile();
1f270d3c 1533 $fullpath = getHashedDir($username, $attachment_dir) . '/' . $filename;
1534 $fp = fopen($fullpath, 'wb');
324ac3c5 1535 fwrite ($fp, $body);
1536 fclose($fp);
628bce99 1537
54156b1c 1538 $composeMessage->initAttachment('message/rfc822', $subject . '.eml', $filename);
f2ad47f1 1539
1540 // create subject for new message
1541 //
1542 $subject = decodeHeader($subject,false,false,true);
1543 $subject = str_replace('"', "'", $subject);
1544 $subject = trim($subject);
1545 if (substr(strtolower($subject), 0, 4) != 'fwd:') {
1546 $subject = 'Fwd: ' . $subject;
1547 }
1548 $composeMessage->rfc822_header->subject = $subject;
1549
324ac3c5 1550 }
1551 }
1552
1553 $compose_messages[$composesession] = $composeMessage;
1554 sqsession_register($compose_messages,'compose_messages');
1555 return $composesession;
1556}
628bce99 1557