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