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