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