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