Adding labels to other columns on message list for checkbox. STABLE admins, please...
[squirrelmail.git] / functions / mailbox_display.php
CommitLineData
59177427 1<?php
2ba13803 2
35586184 3/**
62f7daa5 4 * mailbox_display.php
5 *
6c84ba1e 6 * Copyright (c) 1999-2005 The SquirrelMail Project Team
62f7daa5 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');
19require_once(SM_PATH . 'class/html.class.php');
20require_once(SM_PATH . 'functions/imap_mailbox.php');
26454147 21require_once(SM_PATH . 'functions/imap_messages.php');
324ac3c5 22require_once(SM_PATH . 'functions/imap_asearch.php');
26454147 23require_once(SM_PATH . 'functions/mime.php');
b531f8ea 24require_once(SM_PATH . 'functions/forms.php');
43fdb2a4 25
d6c32258 26/**
62f7daa5 27 * default value for page_selector_max
28 */
6c930ade 29define('PG_SEL_MAX', 10);
30
8cc8ec79 31/**
62f7daa5 32 * The number of pages to cache msg headers
33 */
324ac3c5 34define('SQM_MAX_PAGES_IN_CACHE',5);
35
36/**
62f7daa5 37 * Sort constants used for sorting of messages
38 */
03975a39 39define('SQSORT_NONE',0);
40define('SQSORT_DATE_ASC',1);
41define('SQSORT_DATE_DEC',2);
42define('SQSORT_FROM_ASC',3);
43define('SQSORT_FROM_DEC',4);
44define('SQSORT_SUBJ_ASC',5);
45define('SQSORT_SUBJ_DEC',6);
46define('SQSORT_SIZE_ASC',7);
47define('SQSORT_SIZE_DEC',8);
48define('SQSORT_TO_ASC',9);
49define('SQSORT_TO_DEC',10);
50define('SQSORT_CC_ASC',11);
51define('SQSORT_CC_DEC',12);
52define('SQSORT_INT_DATE_ASC',13);
53define('SQSORT_INT_DATE_DEC',14);
324ac3c5 54
55define('SQSORT_THREAD',32);
56
57
58define('MBX_PREF_SORT',0);
59define('MBX_PREF_LIMIT',1);
60define('MBX_PREF_AUTO_EXPUNGE',2);
61define('MBX_PREF_INTERNALDATE',3);
62define('SQM_MAX_MBX_IN_CACHE',3);
63// define('MBX_PREF_FUTURE',unique integer key);
64
d6c32258 65/**
62f7daa5 66 * @param mixed $start UNDOCUMENTED
67 */
0fdc2fb6 68function elapsed($start) {
69
70 $end = microtime();
71 list($start2, $start1) = explode(" ", $start);
72 list($end2, $end1) = explode(" ", $end);
73 $diff1 = $end1 - $start1;
74 $diff2 = $end2 - $start2;
75 if( $diff2 < 0 ){
76 $diff1 -= 1;
77 $diff2 += 1.0;
78 }
79 return $diff2 + $diff1;
098ea084 80}
81
c7df3f1b 82/**
62f7daa5 83 * Displays message header row in messages list
84 *
85 * @param array $aMsg contains all message related parameters
86 * @return void
87 */
0fdc2fb6 88
8cc8ec79 89function printMessageInfo($aMsg) {
e0e30169 90 // FIX ME, remove these globals as well by adding an array as argument for the user settings
91 // specificly meant for header display
8008456a 92 global $checkall,
e0e30169 93 $color,
0fdc2fb6 94 $default_use_priority,
95 $message_highlight_list,
96 $index_order,
9294e269 97 $truncate_sender, /* number of characters for From/To field (<= 0 for unchanged) */
0fdc2fb6 98 $email_address,
9294e269 99 $show_recipient_instead, /* show recipient name instead of default identity */
100 $use_icons, /* indicates to use icons or text markers */
101 $icon_theme; /* icons theming */
459e3347 102
e9e5f0fa 103 $color_string = $color[4];
8008456a 104
03975a39 105 // initialisation:
8cc8ec79 106 $mailbox = $aMsg['MAILBOX'];
107 $msg = $aMsg['HEADER'];
108 $t = $aMsg['INDX'];
109 $start_msg = $aMsg['PAGEOFFSET'];
110 $last = $aMsg['LAST'];
111 if (isset($aMsg['SEARCH']) && count($aMsg['SEARCH']) >1 ) {
112 $where = $aMsg['SEARCH'][0];
113 $what = $aMsg['SEARCH'][1];
114 } else {
115 $where = false;
116 $what = false;
117 }
e0e30169 118 $iIndent = $aMsg['INDENT'];
8cc8ec79 119
120 $sSubject = (isset($msg['SUBJECT']) && $msg['SUBJECT'] != '') ? $msg['SUBJECT'] : _("(no subject)");
121 $sFrom = (isset($msg['FROM'])) ? $msg['FROM'] : _("Unknown sender");
122 $sTo = (isset($msg['TO'])) ? $msg['TO'] : _("Unknown recipient");
123 $sCc = (isset($msg['CC'])) ? $msg['CC'] : '';
124 $aFlags = (isset($msg['FLAGS'])) ? $msg['FLAGS'] : array();
125 $iPrio = (isset($msg['PRIORITY'])) ? $msg['PRIORITY'] : 3;
126 $iSize = (isset($msg['SIZE'])) ? $msg['SIZE'] : 0;
8d8da447 127
128 // These don't appear to be used... are they safe to remove
8cc8ec79 129 $sType0 = (isset($msg['TYPE0'])) ? $msg['TYPE0'] : 'text';
130 $sType1 = (isset($msg['TYPE1'])) ? $msg['TYPE1'] : 'plain';
e0e30169 131 if (isset($msg['INTERNALDATE'])) {
132 $sDate = getDateString(getTimeStamp(explode(' ',$msg['INTERNALDATE'])));
133 } else {
134 $sDate = (isset($msg['DATE'])) ? getDateString(getTimeStamp(explode(' ',$msg['DATE']))) : '';
135 }
324ac3c5 136 $iId = (isset($msg['UID'])) ? $msg['UID'] : false;
8cc8ec79 137
8cc8ec79 138 if (!$iId) {
139 return;
140 }
03975a39 141
8008456a 142 if ($GLOBALS['alt_index_colors']) {
e0e30169 143 if (!($t % 2)) {
8008456a 144 if (!isset($color[12])) {
145 $color[12] = '#EAEAEA';
146 }
147 $color_string = $color[12];
148 }
bdfb67f8 149 }
8008456a 150
8008456a 151 $urlMailbox = urlencode($mailbox);
e9e5f0fa 152
e0e30169 153 // FIXME, foldertype should be set in right_main.php
154 // in other words, handle as sent is obsoleted from now.
155 // We replace that by providing an array to aMailbox with the to shown headers
156 // that way we are free to show the user different layouts for different folders
53ad5f60 157 $bSentFolder = handleAsSent($mailbox);
1ae2832e 158 if ((!$bSentFolder) && ($show_recipient_instead)) {
159 // If the From address is the same as $email_address, then handle as Sent
03975a39 160 $from_array = parseAddress($sFrom, 1);
1ae2832e 161 if (!isset($email_address)) {
0bb37159 162 global $data_dir, $username;
163 $email_address = getPref($data_dir, $username, 'email_address');
1ae2832e 164 }
165 $bHandleAsSent = ((isset($from_array[0][0])) && ($from_array[0][0] == $email_address));
8cc8ec79 166 } else {
1ae2832e 167 $bHandleAsSent = $bSentFolder;
8cc8ec79 168 }
53ad5f60 169 // If this is a Sent message, display To address instead of From
03975a39 170 if ($bHandleAsSent) {
171 $sFrom = $sTo;
172 }
53ad5f60 173 // Passing 1 below results in only 1 address being parsed, thus defeating the following code
03975a39 174 $sFrom = parseAddress($sFrom/*,1*/);
9701e9c8 175
0fdc2fb6 176 /*
e9e5f0fa 177 * This is done in case you're looking into Sent folders,
178 * because you can have multiple receivers.
179 */
03975a39 180 $senderNames = $sFrom;
098ea084 181 $senderName = '';
26454147 182 $senderAddress = '';
098ea084 183 if (sizeof($senderNames)){
184 foreach ($senderNames as $senderNames_part) {
e9e5f0fa 185 if ($senderName != '') {
186 $senderName .= ', ';
26454147 187 $senderAddress .= ', ';
e9e5f0fa 188 }
26454147 189 $sender_address_part = htmlspecialchars($senderNames_part[0]);
53ad5f60 190 $sender_name_part = str_replace('&nbsp;',' ', decodeHeader($senderNames_part[1]));
26454147 191 if ($sender_name_part) {
192 $senderName .= $sender_name_part;
193 $senderAddress .= $sender_name_part . ' <' . $sender_address_part . '>';
098ea084 194 } else {
26454147 195 $senderName .= $sender_address_part;
196 $senderAddress .= $sender_address_part;
098ea084 197 }
198 }
937c81fa 199 }
53ad5f60 200 // If Sent, prefix with To: but only if not Sent folder
201 if ($bHandleAsSent ^ $bSentFolder) {
48027e89 202 $senderName = _("To") . ': ' . $senderName;
203 $senderAddress = _("To") . ': ' . $senderAddress;
53ad5f60 204 }
459e3347 205
e0e30169 206 // this is a column property which can apply to multiple columns. Do not use vars for one column
207 // only. instead we should use something like this:
208 // 1ed column $aMailbox['columns']['SUBJECT'] value: aray with properties ...
209 // 2ed column $aMailbox['columns']['FROM'] value: aray with properties ...
210 // NB in case of the sentfolder this could be the TO field
211 // properties array example:
212 // 'truncate' => length (0 is no truncate)
213 // 'prefix => if (x in b then do that )
8cc8ec79 214 if ($truncate_sender > 0) {
215 $senderName = truncateWithEntities($senderName, $truncate_sender);
bdfb67f8 216 }
8cc8ec79 217
8d8da447 218 $flag = $flag_end = $fontstr = $fontstr_end = $italic = $italic_end = '';
8cc8ec79 219 $bold = '<b>';
220 $bold_end = '</b>';
221
8cc8ec79 222 foreach ($aFlags as $sFlag => $value) {
223 switch ($sFlag) {
224 case '\\flagged':
225 if ($value) {
226 $flag = "<font color=\"$color[2]\">";
227 $flag_end = '</font>';
228 }
229 break;
230 case '\\seen':
231 if ($value) {
232 $bold = '';
233 $bold_end = '';
234 }
235 break;
236 case '\\deleted':
237 if ($value) {
238 $fontstr = "<font color=\"$color[9]\">";
239 $fontstr_end = '</font>';
240 }
241 break;
242 }
bdfb67f8 243 }
53ad5f60 244 if ($bHandleAsSent) {
8008456a 245 $italic = '<i>';
246 $italic_end = '</i>';
8008456a 247 }
248
d215ca7d 249 if ($where && $what) {
250 $searchstr = '&amp;where='.$where.'&amp;what='.$what;
251 } else {
252 $searchstr = '';
6a622b59 253 }
8cc8ec79 254 /*
255 * Message highlight code
256 */
3667f469 257 $matches = array('TO' => 'sTo', 'CC' => 'sCc', 'FROM' => 'sFrom', 'SUBJECT' => 'sSubject');
937c81fa 258 if (is_array($message_highlight_list) && count($message_highlight_list)) {
03975a39 259 $sTo = parseAddress($sTo);
260 $sCc = parseAddress($sCc);
8008456a 261 foreach ($message_highlight_list as $message_highlight_list_part) {
262 if (trim($message_highlight_list_part['value']) != '') {
c0d04f97 263 $high_val = strtolower($message_highlight_list_part['value']);
264 $match_type = strtoupper($message_highlight_list_part['match_type']);
8415b2d3 265 if($match_type == 'TO_CC') {
266 $match = array('TO', 'CC');
267 } else {
268 $match = array($match_type);
269 }
270 foreach($match as $match_type) {
271 switch($match_type) {
272 case('TO'):
273 case('CC'):
274 case('FROM'):
3667f469 275 foreach ($$matches[$match_type] as $address) {
9701e9c8 276 $address[0] = decodeHeader($address[0], true, false);
277 $address[1] = decodeHeader($address[1], true, false);
8415b2d3 278 if (strstr('^^' . strtolower($address[0]), $high_val) ||
279 strstr('^^' . strtolower($address[1]), $high_val)) {
280 $hlt_color = $message_highlight_list_part['color'];
281 break 4;
282 }
937c81fa 283 }
937c81fa 284 break;
8415b2d3 285 default:
3667f469 286 $headertest = strtolower(decodeHeader($$matches[$match_type], true, false));
9701e9c8 287 if (strstr('^^' . $headertest, $high_val)) {
937c81fa 288 $hlt_color = $message_highlight_list_part['color'];
4669e892 289 break 3;
937c81fa 290 }
8415b2d3 291 break;
292 }
8008456a 293 }
294 }
295 }
8cc8ec79 296 } /* end Message highlight code */
8008456a 297
298 if (!isset($hlt_color)) {
299 $hlt_color = $color_string;
300 }
5e0efebf 301 $col = 0;
03975a39 302 $sSubject = str_replace('&nbsp;', ' ', decodeHeader($sSubject));
e0e30169 303 $subject = processSubject($sSubject, $iIndent);
8cc8ec79 304
e1728a7a 305 echo html_tag( 'tr','','','','valign="top"') . "\n";
8cc8ec79 306
6a622b59 307 if (sizeof($index_order)) {
b4482518 308
309 // figure out which columns should serve as labels for checkbox:
310 // we try to grab the two columns before and after the checkbox,
311 // except the subject column, since it is the link that opens
312 // the message view
313 //
314 $get_next_two = 0;
315 $last_order_part = 0;
316 $last_last_order_part = 0;
317 $show_label_columns = array();
8008456a 318 foreach ($index_order as $index_order_part) {
b4482518 319 if ($index_order_part == 1) {
320 $get_next_two = 1;
321 if ($last_last_order_part != 4)
322 $show_label_columns[] = $last_last_order_part;
323 if ($last_order_part != 4)
324 $show_label_columns[] = $last_order_part;
325
326 } else if ($get_next_two > 0 && $get_next_two < 3 && $index_order_part != 4) {
327 $show_label_columns[] = $index_order_part;
328 $get_next_two++;
329 }
330 $last_last_order_part = $last_order_part;
331 $last_order_part = $index_order_part;
332 }
333
334
335 // build the actual columns for display
336 //
337 foreach ($index_order as $index_order_part) {
338 if (in_array($index_order_part, $show_label_columns)) {
339 $label_start = '<label for="msg[' . $t . ']">';
340 $label_end = '</label>';
341 } else {
342 $label_start = '';
343 $label_end = '';
344 }
8008456a 345 switch ($index_order_part) {
346 case 1: /* checkbox */
347 echo html_tag( 'td',
03975a39 348 addCheckBox("msg[$t]", $checkall, $iId),
0fdc2fb6 349 'center',
350 $hlt_color );
8008456a 351 break;
352 case 2: /* from */
26454147 353 if ($senderAddress != $senderName) {
354 $senderAddress = strtr($senderAddress, array_flip(get_html_translation_table(HTML_SPECIALCHARS)));
c435f076 355 $title = ' title="' . str_replace(array('"', '<', '>'), array("''", '&lt;', '&gt;'), $senderAddress) . '"';
8cc8ec79 356 } else {
26454147 357 $title = '';
8cc8ec79 358 }
8008456a 359 echo html_tag( 'td',
b4482518 360 $label_start . $italic . $bold . $flag . $fontstr . $senderName .
361 $fontstr_end . $flag_end . $bold_end . $italic_end . $label_end,
0fdc2fb6 362 'left',
363 $hlt_color, $title );
8008456a 364 break;
365 case 3: /* date */
03975a39 366 if ($sDate == '') {
367 $sDate = _("Unknown date");
53d9fbc5 368 }
8008456a 369 echo html_tag( 'td',
b4482518 370 $label_start . $bold . $flag . $fontstr . $sDate .
371 $fontstr_end . $flag_end . $bold_end . $label_end,
0fdc2fb6 372 'center',
373 $hlt_color,
c435f076 374 'style="white-space: nowrap;"' );
8008456a 375 break;
376 case 4: /* subject */
377 $td_str = $bold;
e0e30169 378 if ($iIndent) {
379 $td_str .= str_repeat("&nbsp;&nbsp;&nbsp;&nbsp;",$iIndent);
8008456a 380 }
e9e5f0fa 381 $td_str .= '<a href="read_body.php?mailbox='.$urlMailbox
324ac3c5 382 . '&amp;passed_id='. $iId
6a622b59 383 . '&amp;startMessage='.$start_msg.$searchstr.'"';
158fa340 384 $td_str .= ' ' .concat_hook_function('subject_link', array($start_msg, $searchstr));
03975a39 385 if ($subject != $sSubject) {
8008456a 386 $title = get_html_translation_table(HTML_SPECIALCHARS);
387 $title = array_flip($title);
03975a39 388 $title = strtr($sSubject, $title);
c435f076 389 $title = str_replace(array('"', '<', '>'), array("''", '&lt;', '&gt;'), $title);
8008456a 390 $td_str .= " title=\"$title\"";
391 }
392 $td_str .= ">$flag$subject$flag_end</a>$bold_end";
c1c17724 393 echo html_tag( 'td', $td_str, 'left', $hlt_color );
8008456a 394 break;
395 case 5: /* flags */
6a622b59 396
6395c46d 397 // icon message markers
398 //
399 if ($use_icons && $icon_theme != 'none') {
400 $td_str = "<b><small>";
03975a39 401 if (isset($aFlags['\\flagged']) && $aFlags['\\flagged'] == true) {
55103ede 402 $td_str .= '<img src="' . SM_PATH . 'images/themes/' . $icon_theme . '/flagged.png" border="0" height="10" width="10" /> ';
6395c46d 403 }
404 if ($default_use_priority) {
03975a39 405 if ( ($iPrio == 1) || ($iPrio == 2) ) {
55103ede 406 $td_str .= '<img src="' . SM_PATH . 'images/themes/' . $icon_theme . '/prio_high.png" border="0" height="10" width="5" /> ';
6395c46d 407 }
03975a39 408 else if ($iPrio == 5) {
55103ede 409 $td_str .= '<img src="' . SM_PATH . 'images/themes/' . $icon_theme . '/prio_low.png" border="0" height="10" width="5" /> ';
6395c46d 410 }
0fdc2fb6 411 else {
55103ede 412 $td_str .= '<img src="' . SM_PATH . 'images/themes/' . $icon_theme . '/transparent.png" border="0" width="5" /> ';
6395c46d 413 }
414 }
03975a39 415 if ($sType1 == 'mixed') {
55103ede 416 $td_str .= '<img src="' . SM_PATH . 'images/themes/' . $icon_theme . '/attach.png" border="0" height="10" width="6" />';
0fdc2fb6 417 } else {
55103ede 418 $td_str .= '<img src="' . SM_PATH . 'images/themes/' . $icon_theme . '/transparent.png" border="0" width="6" />';
6395c46d 419 }
420
421 $msg_icon = '';
03975a39 422 if (!isset($aFlags['\\seen']) || ($aFlags['\\seen']) == false) {
6395c46d 423 $msg_alt = '(' . _("New") . ')';
424 $msg_title = '(' . _("New") . ')';
425 $msg_icon .= SM_PATH . 'images/themes/' . $icon_theme . '/msg_new';
0fdc2fb6 426 } else {
6395c46d 427 $msg_alt = '(' . _("Read") . ')';
428 $msg_title = '(' . _("Read") . ')';
429 $msg_icon .= SM_PATH . 'images/themes/' . $icon_theme . '/msg_read';
430 }
03975a39 431 if (isset($aFlags['\\deleted']) && ($aFlags['\\deleted']) == true) {
6395c46d 432 $msg_icon .= '_deleted';
433 }
03975a39 434 if (isset($aFlags['\\answered']) && ($aFlags['\\answered']) == true) {
d3685401 435 $msg_alt = '(' . _("Answered") . ')';
436 $msg_title = '(' . _("Answered") . ')';
6395c46d 437 $msg_icon .= '_reply';
438 }
55103ede 439 $td_str .= '<img src="' . $msg_icon . '.png" border="0" alt="'. $msg_alt . '" title="' . $msg_title . '" height="12" width="18" />';
6395c46d 440 $td_str .= '</small></b>';
441 echo html_tag( 'td',
b4482518 442 $label_start . $td_str . $label_end,
0fdc2fb6 443 'right',
444 $hlt_color,
c435f076 445 'style="white-space: nowrap;"' );
8008456a 446 }
6395c46d 447
6395c46d 448 // plain text message markers
449 //
450 else {
451 $stuff = false;
452 $td_str = "<b><small>";
03975a39 453 if (isset($aFlags['\\answered']) && $aFlags['\\answered'] == true) {
6395c46d 454 $td_str .= _("A");
8008456a 455 $stuff = true;
456 }
03975a39 457 if ($sType1 == 'mixed') {
6395c46d 458 $td_str .= '+';
8008456a 459 $stuff = true;
460 }
6395c46d 461 if ($default_use_priority) {
03975a39 462 if ( ($iPrio == 1) || ($iPrio == 2) ) {
6395c46d 463 $td_str .= "<font color=\"$color[1]\">!</font>";
464 $stuff = true;
465 }
03975a39 466 if ($iPrio == 5) {
6395c46d 467 $td_str .= "<font color=\"$color[8]\">?</font>";
468 $stuff = true;
469 }
470 }
03975a39 471 if (isset($aFlags['\\deleted']) && $aFlags['\\deleted'] == true) {
6395c46d 472 $td_str .= "<font color=\"$color[1]\">D</font>";
473 $stuff = true;
474 }
475 if (!$stuff) {
476 $td_str .= '&nbsp;';
477 }
478 $td_str .= '</small></b>';
479 echo html_tag( 'td',
b4482518 480 $label_start . $td_str . $label_end,
0fdc2fb6 481 'center',
482 $hlt_color,
c435f076 483 'style="white-space: nowrap;"' );
8008456a 484 }
8008456a 485 break;
486 case 6: /* size */
8008456a 487 echo html_tag( 'td',
b4482518 488 $label_start . $bold . $fontstr . show_readable_size($iSize) .
489 $fontstr_end . $bold_end . $label_end,
0fdc2fb6 490 'right',
491 $hlt_color );
8008456a 492 break;
493 }
ed230bc3 494 ++$col;
8008456a 495 }
bdfb67f8 496 }
8cc8ec79 497 /* html for separationlines between rows */
1c198ef7 498 if ($last) {
499 echo '</tr>'."\n";
500 } else {
55103ede 501 echo '</tr>' . "\n" . '<tr><td colspan="' . $col . '" bgcolor="' .
0fdc2fb6 502 $color[0] . '" height="1"></td></tr>' . "\n";
6a622b59 503 }
e9e5f0fa 504}
505
324ac3c5 506
507function setUserPref($username, $pref, $value) {
508 global $data_dir;
509 setPref($data_dir,$username,$pref,$value);
510}
511
512/**
513 * Selects a mailbox for header retrieval.
514 * Cache control for message headers is embedded.
515 *
516 * @param resource $imapConnection imap socket handle
517 * @param string $mailbox mailbox to select and retrieve message headers from
518 * @param array $aConfig array with system config settings and incoming vars
519 * @param array $aProps mailbox specific properties
520 * @return array $aMailbox mailbox array with all relevant information
521 * @author Marc Groot Koerkamp
522 */
523function sqm_api_mailbox_select($imapConnection,$mailbox,$aConfig,$aProps) {
524 /**
525 * NB: retrieve this from the session before accessing this function
526 * and make sure you write it back at the end of the script after
527 * the aMailbox var is added so that the headers are added to the cache
528 */
529 global $mailbox_cache;
530 /**
531 * In case the properties arrays are empty set the defaults.
532 */
8d8da447 533 // Doesn't appear to be used... safe to remove?
324ac3c5 534 $aDefaultMbxPref = array ();
535// MBX_PREF_SORT => 0,
536// MBX_PREF_LIMIT => 15,
537// MBX_PREF_AUTO_EXPUNGE => 0,
538// MBX_PREF_INTERNALDATE => 0
539// );
540 /* array_merge doesn't work with integers as keys */
541// foreach ($aDefaultMbxPref as $key => $value) {
542// if (!isset($aProps[$key])) {
543// $aProps[$key] = $value;
544// }
545// }
546 $aDefaultConfigProps = array(
547// 'allow_thread_sort' => 0,
548 'allow_server_sort' => sqimap_capability($imapConnection,'SORT'),
549// 'charset' => 'US-ASCII',
550 'user' => false, /* no pref storage if false */
551 'setindex' => 0,
552// 'search' => 'ALL',
553 'max_cache_size' => SQM_MAX_MBX_IN_CACHE
554 );
555
556 $aConfig = array_merge($aDefaultConfigProps,$aConfig);
557 $iSetIndx = $aConfig['setindex'];
558
559 $aMbxResponse = sqimap_mailbox_select($imapConnection, $mailbox);
560
561 if ($mailbox_cache) {
562 if (isset($mailbox_cache[$mailbox])) {
563 $aCachedMailbox = $mailbox_cache[$mailbox];
564 } else {
565 $aCachedMailbox = false;
566 }
567 /* cleanup cache */
568 if (count($mailbox_cache) > $aConfig['max_cache_size'] -1) {
569 $aTime = array();
570 foreach($mailbox_cache as $cachedmailbox => $aVal) {
571 $aTime[$aVal['TIMESTAMP']] = $cachedmailbox;
572 }
573 if (ksort($aTime,SORT_NUMERIC)) {
574 for ($i=0,$iCnt=count($mailbox_cache);$i<($iCnt-$aConfig['max_cache_size']);++$i) {
575 $sOldestMbx = array_shift($aTime);
576 /**
577 * Remove only the UIDSET and MSG_HEADERS from cache because those can
578 * contain large amounts of data.
579 */
580 if (isset($mailbox_cache[$sOldestMbx]['UIDSET'])) {
581 $mailbox_cache[$sOldestMbx]['UIDSET']= false;
582 }
583 if (isset($mailbox_cache[$sOldestMbx]['MSG_HEADERS'])) {
584 $mailbox_cache[$sOldestMbx]['MSG_HEADERS'] = false;
585 }
586 }
587 }
588 }
589
590 } else {
591 $aCachedMailbox = false;
592 }
593
594 /**
595 * Deal with imap servers that do not return the required UIDNEXT or
596 * UIDVALIDITY response
597 * from a SELECT call (since rfc 3501 it's required).
598 */
599 if (!isset($aMbxResponse['UIDNEXT']) || !isset($aMbxResponse['UIDVALIDITY'])) {
600 $aStatus = sqimap_status_messages($imapConnection,$mailbox,
601 array('UIDNEXT','UIDVALIDITY'));
602 $aMbxResponse['UIDNEXT'] = $aStatus['UIDNEXT'];
603 $aMbxResponse['UIDVALIDTY'] = $aStatus['UIDVALIDITY'];
604 }
605
606 $aMailbox['UIDSET'][$iSetIndx] = false;
607 $aMailbox['ID'] = false;
608 $aMailbox['SETINDEX'] = $iSetIndx;
609
610 if ($aCachedMailbox) {
611 /**
612 * Validate integrity of cached data
613 */
614 if ($aCachedMailbox['EXISTS'] == $aMbxResponse['EXISTS'] &&
615 $aMbxResponse['EXISTS'] &&
616 $aCachedMailbox['UIDVALIDITY'] == $aMbxResponse['UIDVALIDITY'] &&
617 $aCachedMailbox['UIDNEXT'] == $aMbxResponse['UIDNEXT'] &&
618 isset($aCachedMailbox['SEARCH'][$iSetIndx]) &&
619 (!isset($aConfig['search']) || /* always set search from the searchpage */
620 $aCachedMailbox['SEARCH'][$iSetIndx] == $aConfig['search'])) {
621 if (isset($aCachedMailbox['MSG_HEADERS'])) {
622 $aMailbox['MSG_HEADERS'] = $aCachedMailbox['MSG_HEADERS'];
623 }
624 $aMailbox['ID'] = $aCachedMailbox['ID'];
625 if (isset($aCachedMailbox['UIDSET'][$iSetIndx]) && $aCachedMailbox['UIDSET'][$iSetIndx]) {
626 if (isset($aProps[MBX_PREF_SORT]) && $aProps[MBX_PREF_SORT] != $aCachedMailbox['SORT'] ) {
627 $newsort = $aProps[MBX_PREF_SORT];
628 $oldsort = $aCachedMailbox['SORT'];
629 /**
630 * If it concerns a reverse sort we do not need to invalidate
631 * the cached sorted UIDSET, a reverse is sufficient.
632 */
633 if ((($newsort % 2) && ($newsort + 1 == $oldsort)) ||
634 (!($newsort % 2) && ($newsort - 1 == $oldsort))) {
635 $aMailbox['UIDSET'][$iSetIndx] = array_reverse($aCachedMailbox['UIDSET'][$iSetIndx]);
636 } else {
324ac3c5 637 $aMailbox['MSG_HEADERS'] = false;
638 $aMailbox['ID'] = false;
639 }
640 // store the new sort value in the mailbox pref
641 if ($aConfig['user']) {
642 // FIXME, in ideal situation, we write back the
643 // prefs at the end of the script
644 setUserPref($aConfig['user'],"pref_$mailbox",serialize($aProps));
645 }
646 } else {
647 $aMailbox['UIDSET'][$iSetIndx] = $aCachedMailbox['UIDSET'][$iSetIndx];
648 }
649 }
650 }
651 }
652 /**
653 * Restore the offset in the paginator if no new offset is provided.
654 */
655 if (isset($aMailbox['UIDSET'][$iSetIndx]) && !isset($aConfig['offset']) && $aCachedMailbox['OFFSET']) {
656 $aMailbox['OFFSET'] = $aCachedMailbox['OFFSET'];
657 $aMailbox['PAGEOFFSET'] = $aCachedMailbox['PAGEOFFSET'];
658 } else {
659 $aMailbox['OFFSET'] = (isset($aConfig['offset']) && $aConfig['offset']) ? $aConfig['offset'] -1 : 0;
660 $aMailbox['PAGEOFFSET'] = (isset($aConfig['offset']) && $aConfig['offset']) ? $aConfig['offset'] : 1;
661 }
662
663 /**
664 * Restore the showall value no new showall value is provided.
665 */
666 if (isset($aMailbox['UIDSET'][$iSetIndx]) && !isset($aConfig['showall']) &&
667 isset($aCachedMailbox['SHOWALL'][$iSetIndx]) && $aCachedMailbox['SHOWALL'][$iSetIndx]) {
668 $aMailbox['SHOWALL'][$iSetIndx] = $aCachedMailbox['SHOWALL'][$iSetIndx];
669 } else {
670 $aMailbox['SHOWALL'][$iSetIndx] = (isset($aConfig['showall']) && $aConfig['showall']) ? 1 : 0;
671 }
672
673 if (!isset($aProps[MBX_PREF_SORT]) && isset($aCachedMailbox['SORT'])) {
674 $aMailbox['SORT'] = $aCachedMailbox['SORT'];
675 } else {
676 $aMailbox['SORT'] = (isset($aProps[MBX_PREF_SORT])) ? $aProps[MBX_PREF_SORT] : 0;
677 }
678
679 if (!isset($aProps[MBX_PREF_LIMIT]) && isset($aCachedMailbox['LIMIT'])) {
680 $aMailbox['LIMIT'] = $aCachedMailbox['LIMIT'];
681 } else {
682 $aMailbox['LIMIT'] = (isset($aProps[MBX_PREF_LIMIT])) ? $aProps[MBX_PREF_LIMIT] : 15;
683 }
684
685 if (!isset($aProps[MBX_PREF_INTERNALDATE]) && isset($aCachedMailbox['INTERNALDATE'])) {
686 $aMailbox['INTERNALDATE'] = $aCachedMailbox['INTERNALDATE'];
687 } else {
688 $aMailbox['INTERNALDATE'] = (isset($aProps[MBX_PREF_INTERNALDATE])) ? $aProps[MBX_PREF_INTERNALDATE] : false;
689 }
690
691 if (!isset($aProps[MBX_PREF_AUTO_EXPUNGE]) && isset($aCachedMailbox['AUTO_EXPUNGE'])) {
692 $aMailbox['AUTO_EXPUNGE'] = $aCachedMailbox['AUTO_EXPUNGE'];
693 } else {
694 $aMailbox['AUTO_EXPUNGE'] = (isset($aProps[MBX_PREF_AUTO_EXPUNGE])) ? $aProps[MBX_PREF_AUTO_EXPUNGE] : false;
695 }
696
697 if (!isset($aConfig['allow_thread_sort']) && isset($aCachedMailbox['ALLOW_THREAD'])) {
698 $aMailbox['ALLOW_THREAD'] = $aCachedMailbox['ALLOW_THREAD'];
699 } else {
700 $aMailbox['ALLOW_THREAD'] = (isset($aConfig['allow_thread_sort'])) ? $aConfig['allow_thread_sort'] : false;
701 }
702
703 if (!isset($aConfig['search']) && isset($aCachedMailbox['SEARCH'][$iSetIndx])) {
704 $aMailbox['SEARCH'][$iSetIndx] = $aCachedMailbox['SEARCH'][$iSetIndx];
705 } else {
706 $aMailbox['SEARCH'][$iSetIndx] = (isset($aConfig['search'])) ? $aConfig['search'] : 'ALL';
707 }
708
709 if (!isset($aConfig['charset']) && isset($aCachedMailbox['CHARSET'][$iSetIndx])) {
710 $aMailbox['CHARSET'][$iSetIndx] = $aCachedMailbox['CHARSET'][$iSetIndx];
711 } else {
712 $aMailbox['CHARSET'][$iSetIndx] = (isset($aConfig['charset'])) ? $aConfig['charset'] : 'US-ASCII';
713 }
714
715 $aMailbox['NAME'] = $mailbox;
716 $aMailbox['EXISTS'] = $aMbxResponse['EXISTS'];
717 $aMailbox['SEEN'] = (isset($aMbxResponse['SEEN'])) ? $aMbxResponse['SEEN'] : $aMbxResponse['EXISTS'];
718 $aMailbox['RECENT'] = (isset($aMbxResponse['RECENT'])) ? $aMbxResponse['RECENT'] : 0;
719 $aMailbox['UIDVALIDITY'] = $aMbxResponse['UIDVALIDITY'];
720 $aMailbox['UIDNEXT'] = $aMbxResponse['UIDNEXT'];
721 $aMailbox['PERMANENTFLAGS'] = $aMbxResponse['PERMANENTFLAGS'];
722 $aMailbox['RIGHTS'] = $aMbxResponse['RIGHTS'];
723
724
725
726 /* decide if we are thread sorting or not */
727 if (!$aMailbox['ALLOW_THREAD']) {
728 if ($aMailbox['SORT'] & SQSORT_THREAD) {
729 $aMailbox['SORT'] -= SQSORT_THREAD;
730 }
731 }
732 if ($aMailbox['SORT'] & SQSORT_THREAD) {
733 $aMailbox['SORT_METHOD'] = 'THREAD';
734 $aMailbox['THREAD_INDENT'] = $aCachedMailbox['THREAD_INDENT'];
735 } else if (isset($aConfig['allow_server_sort']) && $aConfig['allow_server_sort']) {
736 $aMailbox['SORT_METHOD'] = 'SERVER';
737 $aMailbox['THREAD_INDENT'] = false;
738 } else {
739 $aMailbox['SORT_METHOD'] = 'SQUIRREL';
740 $aMailbox['THREAD_INDENT'] = false;
741 }
742
743 /* set a timestamp for cachecontrol */
744 $aMailbox['TIMESTAMP'] = time();
745 return $aMailbox;
746}
747
748
749
c7df3f1b 750/**
62f7daa5 751 * Does the $srt $_GET var to field mapping
752 *
753 * @param int $srt Field to sort on
754 * @param bool $bServerSort Server sorting is true
755 * @return string $sSortField Field to sort on
756 */
03975a39 757function getSortField($sort,$bServerSort) {
758 switch($sort) {
759 case SQSORT_NONE:
760 $sSortField = 'UID';
761 break;
762 case SQSORT_DATE_ASC:
763 case SQSORT_DATE_DEC:
764 $sSortField = 'DATE';
765 break;
766 case SQSORT_FROM_ASC:
767 case SQSORT_FROM_DEC:
768 $sSortField = 'FROM';
769 break;
770 case SQSORT_SUBJ_ASC:
771 case SQSORT_SUBJ_DEC:
772 $sSortField = 'SUBJECT';
773 break;
774 case SQSORT_SIZE_ASC:
775 case SQSORT_SIZE_DEC:
776 $sSortField = ($bServerSort) ? 'SIZE' : 'RFC822.SIZE';
777 break;
778 case SQSORT_TO_ASC:
779 case SQSORT_TO_DEC:
780 $sSortField = 'TO';
781 break;
782 case SQSORT_CC_ASC:
783 case SQSORT_CC_DEC:
784 $sSortField = 'CC';
785 break;
786 case SQSORT_INT_DATE_ASC:
787 case SQSORT_INT_DATE_DEC:
788 $sSortField = ($bServerSort) ? 'ARRIVAL' : 'INTERNALDATE';
789 break;
e0e30169 790 case SQSORT_THREAD:
03975a39 791 break;
e0e30169 792 default: $sSortField = 'UID';
793 break;
794
fe6efa94 795 }
03975a39 796 return $sSortField;
e9e5f0fa 797}
798
324ac3c5 799function get_sorted_msgs_list($imapConnection,&$aMailbox,&$error) {
800 $iSetIndx = (isset($aMailbox['SETINDEX'])) ? $aMailbox['SETINDEX'] : 0;
e0e30169 801 $bDirection = ($aMailbox['SORT'] % 2);
03975a39 802 $error = false;
324ac3c5 803 if (!$aMailbox['SEARCH'][$iSetIndx]) {
804 $aMailbox['SEARCH'][$iSetIndx] = 'ALL';
805 }
e0e30169 806 switch ($aMailbox['SORT_METHOD']) {
8cc8ec79 807 case 'THREAD':
324ac3c5 808 $aRes = get_thread_sort($imapConnection,$aMailbox['SEARCH'][$iSetIndx]);
809 if ($aRes === false) {
03975a39 810 $error = '<b><small><center><font color=red>' .
ce45831c 811 _("Thread sorting is not supported by your IMAP server.") . '<br />' .
d3b3a524 812 _("Please contact your system administrator and report this error.") .
ce45831c 813 '</center></small></b>';
324ac3c5 814 $aMailbox['SORT'] -= SQSORT_THREAD;
815 } else {
816 $aMailbox['UIDSET'][$iSetIndx] = $aRes[0];
817 $aMailbox['THREAD_INDENT'][$iSetIndx] = $aRes[1];
03975a39 818 }
819 break;
8cc8ec79 820 case 'SERVER':
e0e30169 821 $sSortField = getSortField($aMailbox['SORT'],true);
324ac3c5 822 $id = sqimap_get_sort_order($imapConnection, $sSortField, $bDirection, $aMailbox['SEARCH'][$iSetIndx]);
03975a39 823 if ($id === false) {
824 $error = '<b><small><center><font color=red>' .
ce45831c 825 _("Server-side sorting is not supported by your IMAP server.") . '<br />' .
d3b3a524 826 _("Please contact your system administrator and report this error.") .
03975a39 827 '</center></small></b>';
324ac3c5 828 } else {
829 $aMailbox['UIDSET'][$iSetIndx] = $id;
83cdc70b 830 }
03975a39 831 break;
832 default:
324ac3c5 833 $id = NULL;
834 if ($aMailbox['SEARCH'][$iSetIndx] != 'ALL') {
835 $id = sqimap_run_search($imapConnection, $aMailbox['SEARCH'][$iSetIndx], $aMailbox['CHARSET'][$iSetIndx]);
836 }
e0e30169 837 $sSortField = getSortField($aMailbox['SORT'],false);
324ac3c5 838 $aMailbox['UIDSET'][$iSetIndx] = get_squirrel_sort($imapConnection, $sSortField, $bDirection, $id);
03975a39 839 break;
e9e5f0fa 840 }
324ac3c5 841 return $error;
e9e5f0fa 842}
843
8cc8ec79 844
8cc8ec79 845
324ac3c5 846
847function fetchMessageHeaders($imapConnection, &$aMailbox) {
848
849 /**
850 * Retrieve the UIDSET.
851 * Setindex is used to be able to store multiple uid sets. That will make it
852 * possible to display the mailbox multiple times in different sort order
853 * or to store serach results separate from normal mailbox view.
854 */
855 $iSetIndx = (isset($aMailbox['SETINDEX'])) ? $aMailbox['SETINDEX'] : 0;
856
857 $iLimit = ($aMailbox['SHOWALL'][$iSetIndx]) ? $aMailbox['EXISTS'] : $aMailbox['LIMIT'];
858 /**
859 * Adjust the start_msg
860 */
861 $start_msg = $aMailbox['PAGEOFFSET'];
862 if($aMailbox['PAGEOFFSET'] > $aMailbox['EXISTS']) {
863 $start_msg -= $aMailbox['LIMIT'];
864 if($start_msg < 1) {
865 $start_msg = 1;
866 }
867 }
868
869
870 if (is_array($aMailbox['UIDSET'])) {
871 $aUid =& $aMailbox['UIDSET'][$iSetIndx];
f6b262a3 872 } else {
324ac3c5 873 $aUid = false;
874 }
875
876 // initialize the fields we want to retrieve:
bddb3448 877 $aHeaderFields = array('Date', 'To', 'Cc', 'From', 'Subject', 'X-Priority', 'Importance', 'Priority', 'Content-Type');
324ac3c5 878 $aFetchItems = array('FLAGS', 'RFC822.SIZE');
879
880 // Are we sorting on internaldate then retrieve the internaldate value as well
881 if ($aMailbox['INTERNALDATE']) {
882 $aFetchItems[] = 'INTERNALDATE';
883 }
884
885
886 /**
887 * A uidset with sorted uid's is available. We can use the cache
888 */
889 if (($aMailbox['SORT'] != SQSORT_NONE || $aMailbox['SEARCH'][$iSetIndx] != 'ALL') &&
890 isset($aUid) && $aUid ) {
891
892 // limit the cache to SQM_MAX_PAGES_IN_CACHE
893 if (!$aMailbox['SHOWALL'][$iSetIndx]) {
894 $iMaxMsgs = $iLimit * SQM_MAX_PAGES_IN_CACHE;
895 $iCacheSize = count($aMailbox['MSG_HEADERS']);
896 if ($iCacheSize > $iMaxMsgs) {
897 $iReduce = $iCacheSize - $iMaxMsgs;
898 foreach ($aMailbox['MSG_HEADERS'] as $iUid => $value) {
899 if ($iReduce) {
900 unset($aMailbox['MSG_HEADERS'][$iUid]);
901 } else {
902 break;
903 }
904 --$iReduce;
905 }
6a622b59 906 }
e9e5f0fa 907 }
324ac3c5 908
909 $id_slice = array_slice($aUid,$start_msg-1,$iLimit);
910 /* do some funky cache checks */
911 $aUidCached = array_keys($aMailbox['MSG_HEADERS']);
912 $aUidNotCached = array_values(array_diff($id_slice,$aUidCached));
913 /**
914 * $aUidNotCached contains an array with UID's which need to be fetched to
915 * complete the needed message headers.
916 */
917 if (count($aUidNotCached)) {
918 $aMsgs = sqimap_get_small_header_list($imapConnection,$aUidNotCached,
919 $aHeaderFields,$aFetchItems);
920 // append the msgs to the existend headers
921 $aMailbox['MSG_HEADERS'] += $aMsgs;
8cc8ec79 922 }
6a622b59 923
324ac3c5 924 } else {
925 /**
926 * Initialize the sorted UID list and fetch the visible message headers
927 */
928 if ($aMailbox['SORT'] != SQSORT_NONE || $aMailbox['SEARCH'][$iSetIndx] != 'ALL') {// || $aMailbox['SORT_METHOD'] & SQSORT_THREAD 'THREAD') {
929
930 $error = false;
931 if ($aMailbox['SEARCH'][$iSetIndx] && $aMailbox['SORT'] == 0) {
932 $aUid = sqimap_run_search($imapConnection, $aMailbox['SEARCH'][$iSetIndx], $aMailbox['CHARSET'][$iSetIndx]);
933 } else {
934 $error = get_sorted_msgs_list($imapConnection,$aMailbox,$error);
935 $aUid = $aMailbox['UIDSET'][$iSetIndx];
03975a39 936 }
324ac3c5 937 if ($error === false) {
938 $id_slice = array_slice($aUid,$aMailbox['OFFSET'], $iLimit);
939 if (count($id_slice)) {
940 $aMailbox['MSG_HEADERS'] = sqimap_get_small_header_list($imapConnection,$id_slice,
941 $aHeaderFields,$aFetchItems);
942 } else {
943 return false;
944 }
e0e30169 945
03975a39 946 } else {
324ac3c5 947 // FIX ME, format message and fallback to squirrel sort
948 if ($error) {
949 echo $error;
950 }
03975a39 951 }
03975a39 952 } else {
324ac3c5 953 // limit the cache to SQM_MAX_PAGES_IN_CACHE
954 if (!$aMailbox['SHOWALL'][$iSetIndx] && isset($aMailbox['MSG_HEADERS']) && is_array($aMailbox['MSG_HEADERS'])) {
955 $iMaxMsgs = $iLimit * SQM_MAX_PAGES_IN_CACHE;
956 $iCacheSize = count($aMailbox['MSG_HEADERS']);
957 if ($iCacheSize > $iMaxMsgs) {
958 $iReduce = $iCacheSize - $iMaxMsgs;
959 foreach ($aMailbox['MSG_HEADERS'] as $iUid => $value) {
960 if ($iReduce) {
961 $iId = $aMailbox['MSG_HEADERS'][$iUid]['ID'];
962 unset($aMailbox['MSG_HEADERS'][$iUid]);
963 unset($aMailbox['ID'][$iId]);
964 } else {
965 break;
966 }
967 --$iReduce;
968 }
969 }
03975a39 970 }
e0e30169 971
324ac3c5 972 /**
62f7daa5 973 * retrieve messages by sequence id's and fetch the UID to retrieve
974 * the UID. for sorted lists this is not needed because a UID FETCH
975 * automaticly add the UID value in fetch results
976 **/
324ac3c5 977 $aFetchItems[] = 'UID';
978
979 //create id range
980 $iRangeStart = $aMailbox['EXISTS'] - $aMailbox['OFFSET'];
981 $iRangeEnd = ($iRangeStart > $iLimit) ?
982 ($iRangeStart - $iLimit+1):1;
983
984 $id_slice = range($iRangeStart, $iRangeEnd);
985 /**
986 * Non sorted mailbox with cached message headers
987 */
988 if (isset($aMailbox['ID']) && is_array($aMailbox['ID'])) {
989 // the fetched id => uid relation
990 $aId = $aMailbox['ID'];
991 $aIdCached = array();
992 foreach ($aId as $iId => $iUid) {
993 if (isset($aMailbox['MSG_HEADERS'][$iUid])) {
994 if ($iId <= $iRangeStart && $iId >= $iRangeEnd) {
995 $aIdCached[] = $iId;
996 }
f6b262a3 997 }
6a622b59 998 }
324ac3c5 999 $aIdNotCached = array_diff($id_slice,$aIdCached);
03975a39 1000 } else {
324ac3c5 1001 $aIdNotCached = $id_slice;
8cc8ec79 1002 }
324ac3c5 1003
1004 if (count($aIdNotCached)) {
1005 $aMsgs = sqimap_get_small_header_list($imapConnection,$aIdNotCached,
1006 $aHeaderFields,$aFetchItems);
1007 // append the msgs to the existend headers
1008 if (isset($aMailbox['MSG_HEADERS']) && is_array($aMailbox['MSG_HEADERS'])) {
1009 $aMailbox['MSG_HEADERS'] += $aMsgs;
1010 } else {
1011 $aMailbox['MSG_HEADERS'] = $aMsgs;
1012 }
1013 // update the ID array
1014 foreach ($aMsgs as $iUid => $aMsg) {
1015 if (isset($aMsg['ID'])) {
1016 $aMailbox['ID'][$aMsg['ID']] = $iUid;
1017 }
1018 }
03975a39 1019 }
7b73b809 1020
324ac3c5 1021 /**
1022 * In unsorted state we show newest messages first which means
1023 * that the UIDSET which represents the order of the messages
1024 * should contain a high to low ordered UID list
1025 */
1026 $aSortedUidList = array();
1027 foreach ($id_slice as $iId) {
1028 if (isset($aMailbox['ID'][$iId])) {
1029 $aSortedUidList[] = $aMailbox['ID'][$iId];
1030 }
1031 }
1032 $aMailbox['UIDSET'][$iSetIndx] = $aSortedUidList;
1033 $aMailbox['OFFSET'] = 0;
e0e30169 1034 }
324ac3c5 1035 }
1036 return true;
1037}
6a622b59 1038
324ac3c5 1039/**
62f7daa5 1040 * This function loops through a group of messages in the mailbox
1041 * and shows them to the user.
1042 *
1043 * @param mixed $imapConnection
1044 * @param array $aMailbox associative array with mailbox related vars
1045 */
324ac3c5 1046function showMessagesForMailbox($imapConnection, &$aMailbox) {
0bb37159 1047 global $color, $javascript_on, $compact_paginator;
324ac3c5 1048
1049 // to retrieve the internaldate pref: (I know this is not the right place to do that, move up in front
1050 // and use a properties array as function argument to provide user preferences
1051 global $data_dir, $username;
1052
1053 if (!fetchMessageHeaders($imapConnection, $aMailbox)) {
1054 return false;
1055 }
1056 $iSetIndx = $aMailbox['SETINDEX'];
1057 $iLimit = ($aMailbox['SHOWALL'][$iSetIndx]) ? $aMailbox['EXISTS'] : $aMailbox['LIMIT'];
1058 $iEnd = ($aMailbox['PAGEOFFSET'] + ($iLimit - 1) < $aMailbox['EXISTS']) ?
1059 $aMailbox['PAGEOFFSET'] + $iLimit - 1 : $aMailbox['EXISTS'];
6a622b59 1060
324ac3c5 1061 $paginator_str = get_paginator_str($aMailbox['NAME'], $aMailbox['PAGEOFFSET'],
0bb37159 1062 $aMailbox['EXISTS'], $aMailbox['LIMIT'], $aMailbox['SHOWALL'][$iSetIndx]);
6a622b59 1063
e0e30169 1064 $msg_cnt_str = get_msgcnt_str($aMailbox['PAGEOFFSET'], $iEnd,$aMailbox['EXISTS']);
6a622b59 1065
1066 do_hook('mailbox_index_before');
0bb37159 1067
1068 if ($javascript_on && $compact_paginator) {
1069 // Insert compact paginator javascript
1070 echo "\n<!-- start of compact paginator javascript -->\n"
1071 . "<script language=\"JavaScript\">\n"
1072 . "function SubmitOnSelect(select, URL)\n"
1073 . "{\n"
1074 . " URL += select.options[select.selectedIndex].value;\n"
1075 . " window.location.href = URL;\n"
1076 . "}\n"
1077 . "</script>\n"
1078 . "<!-- end of compact paginator javascript -->\n";
c435f076 1079 }
1080 mail_message_listing_beginning($imapConnection, $aMailbox, $msg_cnt_str, $paginator_str);
1081?>
0fdc2fb6 1082<tr><td height="5" bgcolor="<?php echo $color[4]; ?>"></td></tr>
1083<tr>
abafb676 1084 <td>
0fdc2fb6 1085 <table width="100%" cellpadding="1" cellspacing="0" align="center" border="0" bgcolor="<?php echo $color[9]; ?>">
abafb676 1086 <tr>
0fdc2fb6 1087 <td>
abafb676 1088 <table width="100%" cellpadding="1" cellspacing="0" align="center" border="0" bgcolor="<?php echo $color[5]; ?>">
0fdc2fb6 1089 <?php
8cc8ec79 1090 printHeader($aMailbox);
1091 displayMessageArray($imapConnection, $aMailbox);
0fdc2fb6 1092 ?>
abafb676 1093 </table>
0fdc2fb6 1094 </td>
abafb676 1095 </tr>
0fdc2fb6 1096 </table>
0bb37159 1097 </td>
1098</tr>
0bb37159 1099<?php
1100 mail_message_listing_end($aMailbox, $paginator_str, $msg_cnt_str);
1101?>
c435f076 1102</table></form>
abafb676 1103<?php
324ac3c5 1104
6c930ade 1105}
bdfb67f8 1106
c7df3f1b 1107/**
62f7daa5 1108 * Function to map an uid list with a msg header array by uid
1109 * The mapped headers are printed with printMessage
1110 * aMailbox parameters contains info about the page we are on, the
1111 * used search criteria, the number of messages to show
1112 *
1113 * @param resource $imapConnection socket handle to imap
1114 * @param array $aMailbox array with required elements MSG_HEADERS, UIDSET, OFFSET, LIMIT
1115 * @return void
1116 **/
8cc8ec79 1117function displayMessageArray($imapConnection, $aMailbox) {
324ac3c5 1118 $iSetIndx = $aMailbox['SETINDEX'];
1119 $aId = $aMailbox['UIDSET'][$iSetIndx];
8cc8ec79 1120 $aHeaders = $aMailbox['MSG_HEADERS'];
1121 $iOffset = $aMailbox['OFFSET'];
1122 $sort = $aMailbox['SORT'];
1123 $iPageOffset = $aMailbox['PAGEOFFSET'];
1124 $sMailbox = $aMailbox['NAME'];
324ac3c5 1125 $sSearch = (isset($aMailbox['SEARCH'][$aMailbox['SETINDEX']])) ? $aMailbox['SEARCH'][$aMailbox['SETINDEX']] : false;
1126 $aSearch = ($sSearch) ? array('search.php',$aMailbox['SETINDEX']) : null;
1127
1128 if ($aMailbox['SORT'] & SQSORT_THREAD) {
1129 $aIndentArray =& $aMailbox['THREAD_INDENT'][$aMailbox['SETINDEX']];
e0e30169 1130 $bThread = true;
1131 } else {
1132 $bThread = false;
1133 }
6a622b59 1134 /*
0fdc2fb6 1135 * Loop through and display the info for each message.
1136 * ($t is used for the checkbox number)
1137 */
324ac3c5 1138 $iEnd = ($aMailbox['SHOWALL'][$iSetIndx]) ? $aMailbox['EXISTS'] : $iOffset + $aMailbox['LIMIT'];
8cc8ec79 1139 for ($i=$iOffset,$t=0;$i<$iEnd;++$i) {
1140 if (isset($aId[$i])) {
1141 $bLast = ((isset($aId[$i+1]) && isset($aHeaders[$aId[$i+1]]))
1142 || ($i == $iEnd )) ? false : true;
e0e30169 1143 if ($bThread) {
1144 $indent = (isset($aIndentArray[$aId[$i]])) ? $aIndentArray[$aId[$i]] : 0;
1145 } else {
1146 $indent = 0;
1147 }
8cc8ec79 1148 $aMsg = array(
1149 'HEADER' => $aHeaders[$aId[$i]],
1150 'INDX' => $t,
1151 'OFFSET' => $iOffset,
1152 'PAGEOFFSET' => $iPageOffset,
1153 'SORT' => $sort,
1154 'SEARCH' => $aSearch,
1155 'MAILBOX' => $sMailbox,
e0e30169 1156 'INDENT' => $indent,
8cc8ec79 1157 'LAST' => $bLast
1158 );
324ac3c5 1159 printMessageInfo($aMsg);
0fdc2fb6 1160 ++$t;
6a622b59 1161 } else {
1c198ef7 1162 break;
6a622b59 1163 }
94ac35c6 1164 }
bdfb67f8 1165}
1166
c7df3f1b 1167/**
62f7daa5 1168 * Displays the standard message list header.
1169 *
1170 * To finish the table, you need to do a "</table></table>";
1171 *
1172 * @param resource $imapConnection
1173 * @param array $aMailbox associative array with mailbox related information
1174 * @param string $msg_cnt_str
1175 * @param string $paginator Paginator string
1176 */
6a622b59 1177function mail_message_listing_beginning ($imapConnection,
8cc8ec79 1178 $aMailbox,
1179 $msg_cnt_str = '',
1180 $paginator = '&nbsp;'
1181 ) {
324ac3c5 1182 global $color, $show_flag_buttons, $PHP_SELF;
1012f961 1183 global $lastTargetMailbox, $boxes;
6a622b59 1184
1185 $php_self = $PHP_SELF;
324ac3c5 1186
6a622b59 1187 if (preg_match('/^(.+)\?.+$/',$php_self,$regs)) {
1188 $source_url = $regs[1];
1189 } else {
1190 $source_url = $php_self;
1191 }
c435f076 1192 $php_self = str_replace('&', '&amp;', $php_self);
6a622b59 1193
1194 if (!isset($msg)) {
1195 $msg = '';
1196 }
abafb676 1197
b531f8ea 1198 $moveFields = addHidden('msg', $msg).
8cc8ec79 1199 addHidden('mailbox', $aMailbox['NAME']).
324ac3c5 1200 addHidden('startMessage', $aMailbox['PAGEOFFSET']);
8fe969f6 1201
5e0539cd 1202 /* build thread sorting links */
324ac3c5 1203 $sort = $aMailbox['SORT'];
8cc8ec79 1204 if ($aMailbox['ALLOW_THREAD']) {
324ac3c5 1205 if ($aMailbox['SORT'] & SQSORT_THREAD) {
1206 $sort -= SQSORT_THREAD;
8cc8ec79 1207 $thread_name = _("Unthread View");
1208 } else {
8cc8ec79 1209 $thread_name = _("Thread View");
324ac3c5 1210 $sort = $aMailbox['SORT'] + SQSORT_THREAD;
8cc8ec79 1211 }
324ac3c5 1212 $thread_link_str = '<small>[<a href="' . $source_url . '?srt='
c435f076 1213 . $sort . '&amp;start_messages=1'
1214 . '&amp;mailbox=' . urlencode($aMailbox['NAME']) . '">' . $thread_name
8cc8ec79 1215 . '</a>]</small>';
1216 } else {
1217 $thread_link_str ='';
1218 }
6a622b59 1219 /*
0fdc2fb6 1220 * This is the beginning of the message list table.
1221 * It wraps around all messages
1222 */
8cc8ec79 1223 $safe_name = preg_replace("/[^0-9A-Za-z_]/", '_', $aMailbox['NAME']);
3027826a 1224 $form_name = "FormMsgs" . $safe_name;
3da12e67 1225
324ac3c5 1226 echo '<form name="' . $form_name . '" method="post" action="'.$php_self.'">' ."\n"
0fdc2fb6 1227 . $moveFields;
c435f076 1228 echo '<table border="0" width="100%" cellpadding="0" cellspacing="0"><tr><td>';
324ac3c5 1229
9294e269 1230 $button_str = '';
1231 // display flag buttons only if supported
1232 if ($show_flag_buttons &&
1233 in_array('\\flagged',$aMailbox['PERMANENTFLAGS'], true) ) {
e1728a7a 1234 $button_str .= getButton('submit', 'markUnflagged', _("Unflag"));
1235 $button_str .= getButton('submit', 'markFlagged', _("Flag"));
9294e269 1236 $button_str .= "&nbsp;\n";
1237 }
1238 if (in_array('\\seen',$aMailbox['PERMANENTFLAGS'], true)) {
e1728a7a 1239 $button_str .= getButton('submit', 'markUnread', _("Unread"));
1240 $button_str .= getButton('submit', 'markRead', _("Read"));
9294e269 1241 $button_str .= "&nbsp;\n";
1242 }
e1728a7a 1243 $button_str .= getButton('submit', 'attache',_("Forward")) .
9294e269 1244 "&nbsp;\n";
1245 if (in_array('\\deleted',$aMailbox['PERMANENTFLAGS'], true)) {
e1728a7a 1246 $button_str .= getButton('submit', 'delete',_("Delete"));
9294e269 1247 $button_str .= '<input type="checkbox" name="bypass_trash" />' . _("Bypass Trash");
1248 $button_str .= "&nbsp;\n";
1249 }
1250 if (!$aMailbox['AUTO_EXPUNGE'] && $aMailbox['RIGHTS'] != 'READ-ONLY') {
e1728a7a 1251 $button_str .= getButton('submit', 'expungeButton',_("Expunge")) .'&nbsp;' . _("mailbox") . "\n";
9294e269 1252 $button_str .= '&nbsp;';
1253 }
abafb676 1254?>
0fdc2fb6 1255 <table width="100%" cellpadding="1" cellspacing="0" style="border: 1px solid <?php echo $color[0]; ?>">
abafb676 1256 <tr>
0fdc2fb6 1257 <td>
abafb676 1258 <table bgcolor="<?php echo $color[4]; ?>" border="0" width="100%" cellpadding="1" cellspacing="0">
0fdc2fb6 1259 <tr>
9294e269 1260 <?php echo html_tag('td', '<small>' . $paginator . $thread_link_str . '</small>', 'left') . "\n"; ?>
c435f076 1261 <?php echo html_tag('td', '&nbsp;', 'center') . "\n"; ?>
9294e269 1262 <?php echo html_tag('td', '<small>' . $msg_cnt_str . '</small>', 'right') . "\n"; ?>
0fdc2fb6 1263 </tr>
abafb676 1264 </table>
0fdc2fb6 1265 </td>
abafb676 1266 </tr>
c435f076 1267 <tr bgcolor="<?php echo $color[0]; ?>">
0fdc2fb6 1268 <td>
abafb676 1269 <table border="0" width="100%" cellpadding="1" cellspacing="0">
0fdc2fb6 1270 <tr>
9294e269 1271 <?php echo html_tag('td', '', 'left') . "\n"; ?>
1272 <small>
1273 <?php echo $button_str; ?>
1274 <?php do_hook('mailbox_display_buttons'); ?>
1275 </small>
abafb676 1276 </td>
4669e892 1277 <?php
c075fcfe 1278 if (in_array('\\deleted',$aMailbox['PERMANENTFLAGS'], true)) {
9294e269 1279 ?>
1280 <?php echo html_tag('td', '', 'right'); ?>
1281 <small>&nbsp;<tt>
1282 <select name="targetMailbox">
1283 <?php echo sqimap_mailbox_option_list($imapConnection, array(strtolower($lastTargetMailbox)), 0, $boxes); ?>
1284 </select></tt>&nbsp;
e1728a7a 1285 <?php echo getButton('submit', 'moveButton',_("Move")); ?>
9294e269 1286 </small>
1287 <?php
4669e892 1288 }
1289 ?>
abafb676 1290 </td>
0fdc2fb6 1291 </tr>
abafb676 1292 </table>
0fdc2fb6 1293 </td>
abafb676 1294 </tr>
0fdc2fb6 1295 </table>
c22c351b 1296</td></tr>
abafb676 1297<?php
1b91be0c 1298 do_hook('mailbox_form_before');
e9e5f0fa 1299}
a966982b 1300
c7df3f1b 1301/**
62f7daa5 1302 * Function to add the last row in a message list, it contains the paginator and info about
1303 * the number of messages.
1304 *
0bb37159 1305 * @param array $aMailbox associative array with mailbox related information
62f7daa5 1306 * @param string $paginator_str Paginator string [Prev | Next] [ 1 2 3 ... 91 92 94 ] [Show all]
1307 * @param string $msg_cnt_str Message count string Viewing Messages: 21 to 1861 (20 total)
1308 */
0bb37159 1309function mail_message_listing_end($aMailbox, $paginator_str, $msg_cnt_str) {
1310 global $color;
1311
1312 if ($aMailbox['EXISTS']) {
1313 /* space between list and footer */
1314
abafb676 1315?>
0fdc2fb6 1316<tr><td height="5" bgcolor="<?php echo $color[4]; ?>" colspan="1"></td></tr>
1317<tr>
abafb676 1318 <td>
0fdc2fb6 1319 <table width="100%" cellpadding="1" cellspacing="0" style="border: 1px solid <?php echo $color[0]; ?>">
abafb676 1320 <tr>
0fdc2fb6 1321 <td>
abafb676 1322 <table bgcolor="<?php echo $color[4]; ?>" border="0" width="100%" cellpadding="1" cellspacing="0">
0fdc2fb6 1323 <tr>
c435f076 1324 <?php echo html_tag('td', '<small>' . $paginator_str . '</small>', 'left'); ?>
1325 <?php echo html_tag('td', '<small>' . $msg_cnt_str . '</small>', 'right'); ?>
0fdc2fb6 1326 </tr>
abafb676 1327 </table>
0fdc2fb6 1328 </td>
abafb676 1329 </tr>
0fdc2fb6 1330 </table>
abafb676 1331 </td>
0fdc2fb6 1332</tr>
abafb676 1333<?php
0fdc2fb6 1334}
6a622b59 1335 /* End of message-list table */
a966982b 1336
6a622b59 1337 do_hook('mailbox_index_after');
a966982b 1338}
1339
c7df3f1b 1340/**
62f7daa5 1341 * Prints the table header for the messages list view
1342 *
1343 * @param array $aMailbox
1344 */
8cc8ec79 1345function printHeader($aMailbox) {
1346 global $index_order, $internal_date_sort, $color;
1347
1348 if ($aMailbox['SORT_METHOD'] != 'THREAD') {
1349 $showsort = true;
1350 } else {
1351 $showsort = false;
1352 }
1353
1b91be0c 1354 echo html_tag( 'tr' ,'' , 'center', $color[5] );
e4b5f9d1 1355
1356 /* calculate the width of the subject column based on the
62f7daa5 1357 * widths of the other columns */
e4b5f9d1 1358 $widths = array(1=>1,2=>25,3=>5,4=>0,5=>1,6=>5);
1359 $subjectwidth = 100;
1360 foreach($index_order as $item) {
4669e892 1361 $subjectwidth -= $widths[$item];
e4b5f9d1 1362 }
1363
1364 foreach ($index_order as $item) {
1365 switch ($item) {
8008456a 1366 case 1: /* checkbox */
8cc8ec79 1367 echo html_tag( 'td',get_selectall_link($aMailbox) , '', '', 'width="1%"' );
abafb676 1368 break;
8008456a 1369 case 5: /* flags */
c435f076 1370 echo html_tag( 'td','&nbsp;' , '', '', 'width="1%"' );
8008456a 1371 break;
1372 case 2: /* from */
8cc8ec79 1373 if (handleAsSent($aMailbox['NAME'])) {
1b91be0c 1374 echo html_tag( 'td' ,'' , 'left', '', 'width="25%"' )
0fdc2fb6 1375 . '<b>' . _("To") . '</b>';
03975a39 1376 if ($showsort) {
8cc8ec79 1377 ShowSortButton($aMailbox, SQSORT_TO_ASC, SQSORT_TO_DEC);
03975a39 1378 }
8008456a 1379 } else {
1b91be0c 1380 echo html_tag( 'td' ,'' , 'left', '', 'width="25%"' )
0fdc2fb6 1381 . '<b>' . _("From") . '</b>';
03975a39 1382 if ($showsort) {
8cc8ec79 1383 ShowSortButton($aMailbox, SQSORT_FROM_ASC, SQSORT_FROM_DEC);
03975a39 1384 }
8008456a 1385 }
1b91be0c 1386 echo "</td>\n";
8008456a 1387 break;
1388 case 3: /* date */
c435f076 1389 echo html_tag( 'td' ,'' , 'left', '', 'width="5%" style="white-space: nowrap;"' )
0fdc2fb6 1390 . '<b>' . _("Date") . '</b>';
a966982b 1391 if ($showsort) {
03975a39 1392 if ($internal_date_sort) {
8cc8ec79 1393 ShowSortButton($aMailbox, SQSORT_INT_DATE_ASC, SQSORT_INT_DATE_DEC);
03975a39 1394 } else {
8cc8ec79 1395 ShowSortButton($aMailbox, SQSORT_DATE_ASC, SQSORT_DATE_DEC);
03975a39 1396 }
8008456a 1397 }
1b91be0c 1398 echo "</td>\n";
8008456a 1399 break;
a966982b 1400 case 4: /* subject */
e4b5f9d1 1401 echo html_tag( 'td' ,'' , 'left', '', 'width="'.$subjectwidth.'%"' )
0fdc2fb6 1402 . '<b>' . _("Subject") . '</b>';
a966982b 1403 if ($showsort) {
8cc8ec79 1404 ShowSortButton($aMailbox, SQSORT_SUBJ_ASC, SQSORT_SUBJ_DEC);
8008456a 1405 }
1b91be0c 1406 echo "</td>\n";
8008456a 1407 break;
1408 case 6: /* size */
c435f076 1409 echo html_tag( 'td', '', 'center','','width="5%" style="white-space: nowrap;"')
03975a39 1410 . '<b>' . _("Size") . '</b>';
1411 if ($showsort) {
8cc8ec79 1412 ShowSortButton($aMailbox, SQSORT_SIZE_ASC, SQSORT_SIZE_DEC);
03975a39 1413 }
1414 echo "</td>\n";
8008456a 1415 break;
1416 }
bdfb67f8 1417 }
1b91be0c 1418 echo "</tr>\n";
bdfb67f8 1419}
1420
e9e5f0fa 1421
c7df3f1b 1422/**
62f7daa5 1423 * This function shows the sort button. Isn't this a good comment?
1424 *
1425 * @param array $aMailbox
1426 * @param integer $Down
1427 * @param integer $Up
1428 */
8cc8ec79 1429function ShowSortButton($aMailbox, $Down, $Up ) {
6a622b59 1430 global $PHP_SELF;
94a4ea53 1431
6a622b59 1432 /* Figure out which image we want to use. */
8cc8ec79 1433 if ($aMailbox['SORT'] != $Up && $aMailbox['SORT'] != $Down) {
6a622b59 1434 $img = 'sort_none.png';
1435 $which = $Up;
8cc8ec79 1436 } elseif ($aMailbox['SORT'] == $Up) {
6a622b59 1437 $img = 'up_pointer.png';
1438 $which = $Down;
6c930ade 1439 } else {
6a622b59 1440 $img = 'down_pointer.png';
03975a39 1441 $which = 0;
e35045b7 1442 }
6a622b59 1443
1444 if (preg_match('/^(.+)\?.+$/',$PHP_SELF,$regs)) {
1445 $source_url = $regs[1];
e35045b7 1446 } else {
6a622b59 1447 $source_url = $PHP_SELF;
6c930ade 1448 }
6a622b59 1449
1450 /* Now that we have everything figured out, show the actual button. */
324ac3c5 1451 echo ' <a href="' . $source_url .'?srt=' . $which
8cc8ec79 1452 . '&amp;startMessage=1&amp;mailbox=' . urlencode($aMailbox['NAME'])
0fdc2fb6 1453 . '"><img src="../images/' . $img
1454 . '" border="0" width="12" height="10" alt="sort" title="'
494607f5 1455 . _("Click here to change the sorting of the message list") .'" /></a>';
6a622b59 1456}
1457
c7df3f1b 1458/**
62f7daa5 1459 * FIXME: Undocumented function
1460 *
1461 * @param array $aMailbox
1462 */
8cc8ec79 1463function get_selectall_link($aMailbox) {
1464 global $checkall, $javascript_on;
324ac3c5 1465 global $PHP_SELF;
6a622b59 1466
1467 $result = '';
1468 if ($javascript_on) {
8cc8ec79 1469 $safe_name = preg_replace("/[^0-9A-Za-z_]/", '_', $aMailbox['NAME']);
3027826a 1470 $func_name = "CheckAll" . $safe_name;
1471 $form_name = "FormMsgs" . $safe_name;
6a622b59 1472 $result = '<script language="JavaScript" type="text/javascript">'
1473 . "\n<!-- \n"
3027826a 1474 . "function " . $func_name . "() {\n"
1475 . " for (var i = 0; i < document." . $form_name . ".elements.length; i++) {\n"
3962deff 1476 . " if(document." . $form_name . ".elements[i].type == 'checkbox' && "
e0b57c62 1477 . " document." . $form_name . ".elements[i].name.substring(0,3) == 'msg'){\n"
3027826a 1478 . " document." . $form_name . ".elements[i].checked = "
1479 . " !(document." . $form_name . ".elements[i].checked);\n"
6a622b59 1480 . " }\n"
1481 . " }\n"
1482 . "}\n"
1483 . "//-->\n"
abafb676 1484 . '</script>'
c7df3f1b 1485 . '<input type="checkbox" name="toggleAll" title="'._("Toggle All").'" onclick="'.$func_name.'();" />';
c435f076 1486// . <a href="javascript:void(0)" onclick="' . $func_name . '();">' . _("Toggle All")
abafb676 1487// . "</a>\n";
e35045b7 1488 } else {
a6d36aa5 1489 $result .= "<a href=\"$PHP_SELF";
1490 // FIXME: why strpos() is used to detect presense of the symbol in the string.
1491 // Function returns boolean value only when symbol is not found
6a622b59 1492 if (strpos($PHP_SELF, "?")) {
a6d36aa5 1493 $prefix = '&amp;';
6a622b59 1494 } else {
a6d36aa5 1495 $prefix = '?';
6a622b59 1496 }
a6d36aa5 1497
1498 // If variables are part of GET request, they are present in $PHP_SELF
1499 // maybe other functions can be used instead of sqgetGlobalVar (like preg_match)
1500 if (! sqgetGlobalVar('mailbox',$tmp,SQ_GET)) {
1501 $result .= $prefix . 'mailbox=' . urlencode($aMailbox['NAME']);
1502 $prefix = '&amp;';
1503 }
1504 if (! sqgetGlobalVar('startMessage',$tmp,SQ_GET)) {
1505 $result .= $prefix . 'startMessage=' . $aMailbox['PAGEOFFSET'];
1506 $prefix = '&amp;';
1507 }
1508 if (! sqgetGlobalVar('str',$tmp,SQ_GET)) {
1509 $result .= $prefix . 'str=' . $aMailbox['SORT'];
1510 $prefix = '&amp;';
1511 }
1512
6a622b59 1513 if (isset($checkall) && $checkall == '1') {
a6d36aa5 1514 $checkall_val = '0';
1515 } else {
1516 $checkall_val = '1';
1517 }
1518 if (! sqgetGlobalVar('checkall',$tmp,SQ_GET) ) {
1519 $result .= $prefix . 'checkall=' . $checkall_val;
1520 $prefix = '&amp;';
6a622b59 1521 } else {
a6d36aa5 1522 // checkall is already present in php_self. replace it
1523 $result = preg_replace("/checkall=(\d)/","checkall=$checkall_val",$result);
6a622b59 1524 }
1525
91e0dccc 1526 // FIXME: I suspect that search pages use different variables in 1.5.1cvs
a6d36aa5 1527 // and these variables are present in $PHP_SELF.
1528 if (isset($aMailbox['SEARCH']) && isset($aMailbox['SEARCH'][0]) && ! sqgetGlobalVar('where',$tmp,SQ_GET)) {
1529 $result .= '&amp;where=' . urlencode($aMailbox['SEARCH'][0]);
1530 if (isset($aMailbox['SEARCH'][1]) && ! sqgetGlobalVar('what',$tmp,SQ_GET)) {
1531 $result .= '&amp;what=' . urlencode($aMailbox['SEARCH'][1]);
0bb37159 1532 }
6a622b59 1533 }
1534 $result .= "\">";
98a9cc03 1535 $result .= _("All");
c7df3f1b 1536 $result .= "</a>\n";
e35045b7 1537 }
23d6bd09 1538
6a622b59 1539 /* Return our final result. */
1540 return ($result);
bdfb67f8 1541}
1a0e0983 1542
c7df3f1b 1543/**
62f7daa5 1544 * This function computes the "Viewing Messages..." string.
1545 *
1546 * @param integer $start_msg first message number
1547 * @param integer $end_msg last message number
1548 * @param integer $num_msgs total number of message in folder
1549 * @return string
1550 */
bdfb67f8 1551function get_msgcnt_str($start_msg, $end_msg, $num_msgs) {
6a622b59 1552 /* Compute the $msg_cnt_str. */
1553 $result = '';
1554 if ($start_msg < $end_msg) {
c7df3f1b 1555 $result = sprintf(_("Viewing Messages: %s to %s (%s total)"),
0fdc2fb6 1556 '<b>'.$start_msg.'</b>', '<b>'.$end_msg.'</b>', $num_msgs);
6a622b59 1557 } else if ($start_msg == $end_msg) {
043f9c14 1558 $result = sprintf(_("Viewing Message: %s (%s total)"), '<b>'.$start_msg.'</b>', $num_msgs);
6a622b59 1559 } else {
c7df3f1b 1560 $result = '<br />';
6a622b59 1561 }
1562 /* Return our result string. */
1563 return ($result);
bdfb67f8 1564}
1565
c7df3f1b 1566/**
62f7daa5 1567 * Generate a paginator link.
1568 *
1569 * @param mixed $box Mailbox name
1570 * @param mixed $start_msg Message Offset
1571 * @param mixed $use
1572 * @param string $text text used for paginator link
1573 * @return string
1574 */
324ac3c5 1575function get_paginator_link($box, $start_msg, $text) {
1576 sqgetGlobalVar('PHP_SELF',$php_self,SQ_SERVER);
1577 $result = "<a href=\"$php_self?startMessage=$start_msg&amp;mailbox=$box\" "
c7df3f1b 1578 . ">$text</a>";
6a622b59 1579
6a622b59 1580 return ($result);
7b294953 1581}
1582
c7df3f1b 1583/**
62f7daa5 1584 * This function computes the paginator string.
1585 *
1586 * @param string $box mailbox name
1587 * @param integer $iOffset offset in total number of messages
1588 * @param integer $iTotal total number of messages
1589 * @param integer $iLimit maximum number of messages to show on a page
1590 * @param bool $bShowAll show all messages at once (non paginate mode)
1591 * @return string $result paginate string with links to pages
1592 */
324ac3c5 1593function get_paginator_str($box, $iOffset, $iTotal, $iLimit, $bShowAll) {
0bb37159 1594 global $username, $data_dir, $javascript_on;
1595 // page selector globals
1596 global $page_selector, $page_selector_max, $compact_paginator;
324ac3c5 1597 sqgetGlobalVar('PHP_SELF',$php_self,SQ_SERVER);
6a622b59 1598
1599 /* Initialize paginator string chunks. */
1600 $prv_str = '';
1601 $nxt_str = '';
eac26493 1602 $pg_str = '';
6a622b59 1603 $all_str = '';
6a622b59 1604
1605 $box = urlencode($box);
324ac3c5 1606
6a622b59 1607 /* Create simple strings that will be creating the paginator. */
1608 $spc = '&nbsp;'; /* This will be used as a space. */
1609 $sep = '|'; /* This will be used as a seperator. */
1610
6a622b59 1611 /* Make sure that our start message number is not too big. */
324ac3c5 1612 $iOffset = min($iOffset, $iTotal);
6a622b59 1613
6a622b59 1614 /* Compute the starting message of the previous and next page group. */
324ac3c5 1615 $next_grp = $iOffset + $iLimit;
1616 $prev_grp = $iOffset - $iLimit;
1617
1618 if (!$bShowAll) {
1619 /* Compute the basic previous and next strings. */
0bb37159 1620 if ($compact_paginator) {
1621 if (($next_grp <= $iTotal) && ($prev_grp >= 0)) {
1622 $prv_str = get_paginator_link($box, $prev_grp, '<');
1623 $nxt_str = get_paginator_link($box, $next_grp, '>');
1624 } else if (($next_grp > $iTotal) && ($prev_grp >= 0)) {
1625 $prv_str = get_paginator_link($box, $prev_grp, '<');
1626 $nxt_str = '>';
1627 } else if (($next_grp <= $iTotal) && ($prev_grp < 0)) {
1628 $prv_str = '<';
1629 $nxt_str = get_paginator_link($box, $next_grp, '>');
1630 }
1631 } else {
1632 if (($next_grp <= $iTotal) && ($prev_grp >= 0)) {
1633 $prv_str = get_paginator_link($box, $prev_grp, _("Previous"));
1634 $nxt_str = get_paginator_link($box, $next_grp, _("Next"));
1635 } else if (($next_grp > $iTotal) && ($prev_grp >= 0)) {
1636 $prv_str = get_paginator_link($box, $prev_grp, _("Previous"));
1637 $nxt_str = _("Next");
1638 } else if (($next_grp <= $iTotal) && ($prev_grp < 0)) {
1639 $prv_str = _("Previous");
1640 $nxt_str = get_paginator_link($box, $next_grp, _("Next"));
1641 }
324ac3c5 1642 }
6a622b59 1643
324ac3c5 1644 /* Page selector block. Following code computes page links. */
0bb37159 1645 if ($iLimit != 0 && $page_selector && ($iTotal > $iLimit)) {
324ac3c5 1646 /* Most importantly, what is the current page!!! */
1647 $cur_pg = intval($iOffset / $iLimit) + 1;
6a622b59 1648
324ac3c5 1649 /* Compute total # of pages and # of paginator page links. */
1650 $tot_pgs = ceil($iTotal / $iLimit); /* Total number of Pages */
6a622b59 1651
0bb37159 1652 if (!$compact_paginator) {
1653 $vis_pgs = min($page_selector_max, $tot_pgs - 1); /* Visible Pages */
6a622b59 1654
0bb37159 1655 /* Compute the size of the four quarters of the page links. */
324ac3c5 1656
0bb37159 1657 /* If we can, just show all the pages. */
1658 if (($tot_pgs - 1) <= $page_selector_max) {
324ac3c5 1659 $q1_pgs = $cur_pg - 1;
0bb37159 1660 $q2_pgs = $q3_pgs = 0;
324ac3c5 1661 $q4_pgs = $tot_pgs - $cur_pg;
0bb37159 1662
1663 /* Otherwise, compute some magic to choose the four quarters. */
1664 } else {
1665 /*
1666 * Compute the magic base values. Added together,
1667 * these values will always equal to the $pag_pgs.
1668 * NOTE: These are DEFAULT values and do not take
1669 * the current page into account. That is below.
1670 */
1671 $q1_pgs = floor($vis_pgs/4);
1672 $q2_pgs = round($vis_pgs/4, 0);
1673 $q3_pgs = ceil($vis_pgs/4);
1674 $q4_pgs = round(($vis_pgs - $q2_pgs)/3, 0);
1675
1676 /* Adjust if the first quarter contains the current page. */
1677 if (($cur_pg - $q1_pgs) < 1) {
1678 $extra_pgs = ($q1_pgs - ($cur_pg - 1)) + $q2_pgs;
1679 $q1_pgs = $cur_pg - 1;
1680 $q2_pgs = 0;
1681 $q3_pgs += ceil($extra_pgs / 2);
1682 $q4_pgs += floor($extra_pgs / 2);
1683
1684 /* Adjust if the first and second quarters intersect. */
1685 } else if (($cur_pg - $q2_pgs - ceil($q2_pgs/3)) <= $q1_pgs) {
1686 $extra_pgs = $q2_pgs;
1687 $extra_pgs -= ceil(($cur_pg - $q1_pgs - 1) * 3/4);
1688 $q2_pgs = ceil(($cur_pg - $q1_pgs - 1) * 3/4);
1689 $q3_pgs += ceil($extra_pgs / 2);
1690 $q4_pgs += floor($extra_pgs / 2);
1691
1692 /* Adjust if the fourth quarter contains the current page. */
1693 } else if (($cur_pg + $q4_pgs) >= $tot_pgs) {
1694 $extra_pgs = ($q4_pgs - ($tot_pgs - $cur_pg)) + $q3_pgs;
1695 $q3_pgs = 0;
1696 $q4_pgs = $tot_pgs - $cur_pg;
1697 $q1_pgs += floor($extra_pgs / 2);
1698 $q2_pgs += ceil($extra_pgs / 2);
1699
1700 /* Adjust if the third and fourth quarter intersect. */
1701 } else if (($cur_pg + $q3_pgs + 1) >= ($tot_pgs - $q4_pgs + 1)) {
1702 $extra_pgs = $q3_pgs;
1703 $extra_pgs -= ceil(($tot_pgs - $cur_pg - $q4_pgs) * 3/4);
1704 $q3_pgs = ceil(($tot_pgs - $cur_pg - $q4_pgs) * 3/4);
1705 $q1_pgs += floor($extra_pgs / 2);
1706 $q2_pgs += ceil($extra_pgs / 2);
1707 }
324ac3c5 1708 }
e35045b7 1709
0bb37159 1710 /*
1711 * I am leaving this debug code here, commented out, because
1712 * it is a really nice way to see what the above code is doing.
1713 * echo "qts = $q1_pgs/$q2_pgs/$q3_pgs/$q4_pgs = "
1714 * . ($q1_pgs + $q2_pgs + $q3_pgs + $q4_pgs) . '<br />';
1715 */
6a622b59 1716
0bb37159 1717 /* Print out the page links from the compute page quarters. */
6a622b59 1718
0bb37159 1719 /* Start with the first quarter. */
1720 if (($q1_pgs == 0) && ($cur_pg > 1)) {
324ac3c5 1721 $pg_str .= "...$spc";
0bb37159 1722 } else {
1723 for ($pg = 1; $pg <= $q1_pgs; ++$pg) {
1724 $start = (($pg-1) * $iLimit) + 1;
1725 $pg_str .= get_paginator_link($box, $start, $pg) . $spc;
1726 }
1727 if ($cur_pg - $q2_pgs - $q1_pgs > 1) {
1728 $pg_str .= "...$spc";
1729 }
324ac3c5 1730 }
6a622b59 1731
0bb37159 1732 /* Continue with the second quarter. */
1733 for ($pg = $cur_pg - $q2_pgs; $pg < $cur_pg; ++$pg) {
1734 $start = (($pg-1) * $iLimit) + 1;
1735 $pg_str .= get_paginator_link($box, $start, $pg) . $spc;
1736 }
6a622b59 1737
0bb37159 1738 /* Now print the current page. */
1739 $pg_str .= $cur_pg . $spc;
6a622b59 1740
0bb37159 1741 /* Next comes the third quarter. */
1742 for ($pg = $cur_pg + 1; $pg <= $cur_pg + $q3_pgs; ++$pg) {
1743 $start = (($pg-1) * $iLimit) + 1;
1744 $pg_str .= get_paginator_link($box, $start, $pg) . $spc;
1745 }
6a622b59 1746
0bb37159 1747 /* And last, print the forth quarter page links. */
1748 if (($q4_pgs == 0) && ($cur_pg < $tot_pgs)) {
324ac3c5 1749 $pg_str .= "...$spc";
0bb37159 1750 } else {
1751 if (($tot_pgs - $q4_pgs) > ($cur_pg + $q3_pgs)) {
1752 $pg_str .= "...$spc";
1753 }
1754 for ($pg = $tot_pgs - $q4_pgs + 1; $pg <= $tot_pgs; ++$pg) {
1755 $start = (($pg-1) * $iLimit) + 1;
1756 $pg_str .= get_paginator_link($box, $start,$pg) . $spc;
1757 }
324ac3c5 1758 }
6a622b59 1759 }
0bb37159 1760 $last_grp = (($tot_pgs - 1) * $iLimit) + 1;
6a622b59 1761 }
324ac3c5 1762 } else {
1763 $pg_str = "<a href=\"$php_self?showall=0"
1764 . "&amp;startMessage=1&amp;mailbox=$box\" "
c7df3f1b 1765 . ">" ._("Paginate") . '</a>';
6a622b59 1766 }
1767
6a622b59 1768 /* Put all the pieces of the paginator string together. */
1769 /**
62f7daa5 1770 * Hairy code... But let's leave it like it is since I am not certain
1771 * a different approach would be any easier to read. ;)
1772 */
6a622b59 1773 $result = '';
324ac3c5 1774 if ( $prv_str || $nxt_str ) {
1775
1776 /* Compute the 'show all' string. */
1777 $all_str = "<a href=\"$php_self?showall=1"
1778 . "&amp;startMessage=1&amp;mailbox=$box\" "
0fdc2fb6 1779 . ">" . _("Show All") . '</a>';
abafb676 1780 }
eac26493 1781
0bb37159 1782 if ($compact_paginator) {
1783 if ( $prv_str || $nxt_str ) {
1784 $result .= '[' . get_paginator_link($box, 1, '<<') . ']';
1785 $result .= '[' . $prv_str . ']';
1786
1787 $pg_url = $php_self . '?mailbox=' . $box;
6a622b59 1788
0bb37159 1789 $result .= '[' . $nxt_str . ']';
1790 $result .= '[' . get_paginator_link($box, $last_grp, '>>') . ']';
1791
1792 if ($page_selector) {
1793 $result .= $spc . '<select name="startMessage"';
1794 if ($javascript_on) {
1795 $result .= ' onchange="JavaScript:SubmitOnSelect'
c435f076 1796 . '(this, \'' . $pg_url . '&amp;startMessage=\')"';
0bb37159 1797 }
1798 $result .='>';
1799
1800 for ($p = 0; $p < $tot_pgs; $p++) {
1801 $result .= '<option ';
1802 if (($p+1) == $cur_pg) $result .= 'selected ';
1803 $result .= 'value="' . (($p*$iLimit)+1) . '">'
1804 . ($p+1) . "/$tot_pgs" . '</option>';
1805 }
1806
1807 $result .= '</select>';
1808
1809 if ($javascript_on) {
1810 $result .= '<noscript language="JavaScript">'
1811 . addSubmit(_("Go"))
1812 . '</noscript>';
1813 } else {
1814 $result .= addSubmit(_("Go"));
1815 }
1816 }
1817 }
1818
1819 $result .= ($pg_str != '' ? '['.$pg_str.']' . $spc : '');
1820 $result .= ($all_str != '' ? $spc . '['.$all_str.']' . $spc . $spc : '');
1821 } else {
1822 if ( $prv_str || $nxt_str ) {
1823 $result .= '[';
1824 $result .= ($prv_str != '' ? $prv_str . $spc . $sep . $spc : '');
1825 $result .= ($nxt_str != '' ? $nxt_str : '');
1826 $result .= ']' . $spc ;
1827 }
1828
1829 $result .= ($pg_str != '' ? $spc . '['.$spc.$pg_str.']' . $spc : '');
1830 $result .= ($all_str != '' ? $spc . '['.$all_str.']' . $spc . $spc : '');
1831 }
6a622b59 1832 /* If the resulting string is blank, return a non-breaking space. */
1833 if ($result == '') {
1834 $result = '&nbsp;';
1835 }
6a622b59 1836 /* Return our final magical paginator string. */
1837 return ($result);
bdfb67f8 1838}
1839
c7df3f1b 1840/**
62f7daa5 1841 * FIXME: Undocumented function
1842 */
eaa4f45f 1843function truncateWithEntities($subject, $trim_at)
1844{
341aa42f 1845 $ent_strlen = strlen($subject);
1846 if (($trim_at <= 0) || ($ent_strlen <= $trim_at))
6a622b59 1847 return $subject;
eaa4f45f 1848
1849 global $languages, $squirrelmail_language;
6a622b59 1850
6a622b59 1851 /*
62f7daa5 1852 * see if this is entities-encoded string
1853 * If so, Iterate through the whole string, find out
1854 * the real number of characters, and if more
1855 * than $trim_at, substr with an updated trim value.
1856 */
341aa42f 1857 $trim_val = $trim_at;
1858 $ent_offset = 0;
1859 $ent_loc = 0;
a1440f89 1860 while ( $ent_loc < $trim_val && (($ent_loc = strpos($subject, '&', $ent_offset)) !== false) &&
1861 (($ent_loc_end = strpos($subject, ';', $ent_loc+3)) !== false) ) {
1862 $trim_val += ($ent_loc_end-$ent_loc);
6a622b59 1863 $ent_offset = $ent_loc_end+1;
1864 }
341aa42f 1865 if (($trim_val > $trim_at) && ($ent_strlen > $trim_val) && (strpos($subject,';',$trim_val) < ($trim_val + 6))) {
a1440f89 1866 $i = strpos($subject,';',$trim_val);
1867 if ($i) {
1868 $trim_val = strpos($subject,';',$trim_val);
1869 }
46a49f65 1870 }
341aa42f 1871 // only print '...' when we're actually dropping part of the subject
1872 if ($ent_strlen <= $trim_val)
6a622b59 1873 return $subject;
6a622b59 1874
1875 if (isset($languages[$squirrelmail_language]['XTRA_CODE']) &&
b3e4bf71 1876 function_exists($languages[$squirrelmail_language]['XTRA_CODE'] . '_strimwidth')) {
1877 return call_user_func($languages[$squirrelmail_language]['XTRA_CODE'] . '_strimwidth', $subject, $trim_val);
6a622b59 1878 }
ecaf6352 1879
c435f076 1880 return substr_replace($subject, '...', $trim_val + 1);
bdfb67f8 1881}
f93c93b9 1882
c7df3f1b 1883/**
62f7daa5 1884 * FIXME: Undocumented function
1885 */
eaa4f45f 1886function processSubject($subject, $threadlevel = 0) {
1887 /* Shouldn't ever happen -- caught too many times in the IMAP functions */
1888 if ($subject == '') {
1889 return _("(no subject)");
1890 }
1891
3fc1a95f 1892 global $truncate_subject; /* number of characters for Subject field (<= 0 for unchanged) */
1893 $trim_at = $truncate_subject;
eaa4f45f 1894
1895 /* if this is threaded, subtract two chars per indentlevel */
1896 if (($threadlevel > 0) && ($threadlevel <= 10))
1897 $trim_at -= (2*$threadlevel);
1898
1899 return truncateWithEntities($subject, $trim_at);
1900}
1901
e9e5f0fa 1902
c7df3f1b 1903/**
62f7daa5 1904 * Creates button
1905 *
1906 * @deprecated see form functions available in 1.5.1 and 1.4.3.
1907 * @param string $type
1908 * @param string $name
1909 * @param string $value
1910 * @param string $js
1911 * @param bool $enabled
1912 */
1a531551 1913function getButton($type, $name, $value, $js = '', $enabled = TRUE) {
98a9cc03 1914 $disabled = ( $enabled ? '' : 'disabled ' );
1a531551 1915 $js = ( $js ? $js.' ' : '' );
c7df3f1b 1916 return '<input '.$disabled.$js.
0fdc2fb6 1917 'type="'.$type.
1918 '" name="'.$name.
c7df3f1b 1919 '" value="'.$value .
1920 '" style="padding: 0px; margin: 0px" />';
e9e5f0fa 1921}
1922
c7df3f1b 1923/**
62f7daa5 1924 * Puts string into cell, aligns it and adds <small> tag
1925 *
1926 * @param string $string string
1927 * @param string $align alignment
1928 */
e9e5f0fa 1929function getSmallStringCell($string, $align) {
6a622b59 1930 return html_tag('td',
1931 '<small>' . $string . ':&nbsp; </small>',
1932 $align,
1933 '',
c435f076 1934 'style="white-space: nowrap;"' );
e9e5f0fa 1935}
1936
c7df3f1b 1937/**
62f7daa5 1938 * This should go in imap_mailbox.php
1939 * @param string $mailbox
1940 */
a3439b27 1941function handleAsSent($mailbox) {
6a8e7cae 1942 global $handleAsSent_result;
4669e892 1943
6a622b59 1944 /* First check if this is the sent or draft folder. */
6a8e7cae 1945 $handleAsSent_result = isSentMailbox($mailbox) || isDraftMailbox($mailbox);
a3439b27 1946
6a622b59 1947 /* Then check the result of the handleAsSent hook. */
1948 do_hook('check_handleAsSent_result', $mailbox);
a3439b27 1949
6a622b59 1950 /* And return the result. */
6a8e7cae 1951 return $handleAsSent_result;
6a622b59 1952}
e842b215 1953
324ac3c5 1954/**
1955 * Process messages list form and handle the cache gracefully. If $sButton and
1956 * $aUid are provided as argument then you can fake a message list submit and
1957 * use it i.e. in read_body.php for del move next and update the cache
1958 *
1959 * @param resource $imapConnection imap connection
1960 * @param array $aMailbox (reference) cached mailbox
1961 * @param string $sButton fake a submit button
1962 * @param array $aUid fake the $msg array
1963 * @return string $sError error string in case of an error
1964 * @author Marc Groot Koerkamp
1965 */
1966function handleMessageListForm($imapConnection,&$aMailbox,$sButton='',$aUid = array()) {
1967
1968 /* incoming formdata */
1969 $sButton = (sqgetGlobalVar('moveButton', $sTmp, SQ_POST)) ? 'move' : $sButton;
1970 $sButton = (sqgetGlobalVar('expungeButton', $sTmp, SQ_POST)) ? 'expunge' : $sButton;
1971 $sButton = (sqgetGlobalVar('attache', $sTmp, SQ_POST)) ? 'attache' : $sButton;
1972 $sButton = (sqgetGlobalVar('delete', $sTmp, SQ_POST)) ? 'setDeleted' : $sButton;
1973 $sButton = (sqgetGlobalVar('undeleteButton', $sTmp, SQ_POST)) ? 'setDeleted' : $sButton;
1974 $sButton = (sqgetGlobalVar('markRead', $sTmp, SQ_POST)) ? 'setSeen' : $sButton;
1975 $sButton = (sqgetGlobalVar('markUnread', $sTmp, SQ_POST)) ? 'unsetSeen' : $sButton;
1976 $sButton = (sqgetGlobalVar('markFlagged', $sTmp, SQ_POST)) ? 'setFlagged' : $sButton;
1977 $sButton = (sqgetGlobalVar('markUnflagged', $sTmp, SQ_POST)) ? 'unsetFlagged' : $sButton;
1978 sqgetGlobalVar('targetMailbox', $targetMailbox, SQ_POST);
1979 sqgetGlobalVar('bypass_trash', $bypass_trash, SQ_POST);
1980 sqgetGlobalVar('msg', $msg, SQ_POST);
1981
1982 $sError = '';
1983 $mailbox = $aMailbox['NAME'];
1984
1985 /* retrieve the check boxes */
1986 $aUid = (isset($msg) && is_array($msg)) ? array_values($msg) : $aUid;
1987
1988 if (count($aUid) && $sButton != 'expunge') {
1989 $aUpdatedMsgs = false;
1990 $bExpunge = false;
1991 switch ($sButton) {
1992 case 'setDeleted':
1993 // check if id exists in case we come from read_body
1994 if (count($aUid) == 1 && is_array($aMailbox['UIDSET'][$aMailbox['SETINDEX']]) &&
1995 !in_array($aUid[0],$aMailbox['UIDSET'][$aMailbox['SETINDEX']])) {
1996 break;
1997 }
7c788b1c 1998 $aUpdatedMsgs = sqimap_msgs_list_delete($imapConnection, $mailbox, $aUid,$bypass_trash);
1999 $bExpunge = true;
324ac3c5 2000 break;
2001 case 'unsetDeleted':
2002 case 'setSeen':
2003 case 'unsetSeen':
2004 case 'setFlagged':
2005 case 'unsetFlagged':
2006 // get flag
2007 $sFlag = (substr($sButton,0,3) == 'set') ? '\\'.substr($sButton,3) : '\\'.substr($sButton,5);
2008 $bSet = (substr($sButton,0,3) == 'set') ? true : false;
2009 $aUpdatedMsgs = sqimap_toggle_flag($imapConnection, $aUid, $sFlag, $bSet, true);
2010 break;
2011 case 'move':
2012 $aUpdatedMsgs = sqimap_msgs_list_move($imapConnection,$aUid,$targetMailbox);
2013 sqsession_register($targetMailbox,'lastTargetMailbox');
2014 $bExpunge = true;
2015 break;
2016 case 'attache':
2017 $aMsgHeaders = array();
2018 foreach ($aUid as $iUid) {
2019 $aMsgHeaders[$iUid] = $aMailbox['MSG_HEADERS'][$iUid];
2020 }
2021 if (count($aMsgHeaders)) {
2022 $composesession = attachSelectedMessages($imapConnection,$aMsgHeaders);
2023 // dirty hack, add info to $aMailbox
2024 $aMailbox['FORWARD_SESSION'] = $composesession;
2025 }
2026 break;
7c788b1c 2027 default:
2028 // Hook for plugin buttons
2029 do_hook_function('mailbox_display_button_action', $aUid);
2030 break;
324ac3c5 2031 }
2032 /**
2033 * Updates messages is an array containing the result of the untagged
2034 * fetch responses send by the imap server due to a flag change. That
2035 * response is parsed in a array with msg arrays by the parseFetch function
2036 */
2037 if ($aUpdatedMsgs) {
2038 // Update the message headers cache
2039 $aDeleted = array();
2040 foreach ($aUpdatedMsgs as $iUid => $aMsg) {
2041 if (isset($aMsg['FLAGS'])) {
fd28fa79 2042 /**
ddd209f2 2043 * Only update the cached headers if the header is
fd28fa79 2044 * cached.
2045 */
2046 if (isset($aMailbox['MSG_HEADERS'][$iUid])) {
2047 $aMailbox['MSG_HEADERS'][$iUid]['FLAGS'] = $aMsg['FLAGS'];
2048 }
324ac3c5 2049 /**
2050 * Count the messages with the \Delete flag set so we can determine
2051 * if the number of expunged messages equals the number of flagged
2052 * messages for deletion.
2053 */
2054 if (isset($aMsg['FLAGS']['\\deleted']) && $aMsg['FLAGS']['\\deleted']) {
2055 $aDeleted[] = $iUid;
2056 }
2057 }
2058 }
2059 if ($bExpunge && $aMailbox['AUTO_EXPUNGE'] &&
2060 $iExpungedMessages = sqimap_mailbox_expunge($imapConnection, $aMailbox['NAME'], true))
2061 {
2062 if (count($aDeleted) != $iExpungedMessages) {
2063 // there are more messages deleted permanently then we expected
2064 // invalidate the cache
2065 $aMailbox['UIDSET'][$aMailbox['SETINDEX']] = false;
2066 $aMailbox['MSG_HEADERS'] = false;
2067 } else {
2068 // remove expunged messages from cache
2069 $aUidSet = $aMailbox['UIDSET'][$aMailbox['SETINDEX']];
2070 if (is_array($aUidSet)) {
2071 // create a UID => array index temp array
2072 $aUidSetDummy = array_flip($aUidSet);
2073 foreach ($aDeleted as $iUid) {
2074 // get the id as well in case of SQM_SORT_NONE
2075 if ($aMailbox['SORT'] == SQSORT_NONE) {
2076 $aMailbox['ID'] = false;
2077 //$iId = $aMailbox['MSG_HEADERS'][$iUid]['ID'];
2078 //unset($aMailbox['ID'][$iId]);
2079 }
2080 // unset the UID and message header
2081 unset($aUidSetDummy[$iUid]);
2082 unset($aMailbox['MSG_HEADERS'][$iUid]);
2083 }
2084 $aMailbox['UIDSET'][$aMailbox['SETINDEX']] = array_keys($aUidSetDummy);
324ac3c5 2085 }
2086 }
ddd209f2 2087 // update EXISTS info
2088 if ($iExpungedMessages) {
2089 $aMailbox['EXISTS'] -= (int) $iExpungedMessages;
2090 }
324ac3c5 2091 // Change the startMessage number if the mailbox was changed
4c284a74 2092 if (($aMailbox['PAGEOFFSET']-1) >= $aMailbox['EXISTS']) {
324ac3c5 2093 $aMailbox['PAGEOFFSET'] = ($aMailbox['PAGEOFFSET'] > $aMailbox['LIMIT']) ?
2094 $aMailbox['PAGEOFFSET'] - $aMailbox['LIMIT'] : 1;
91e0dccc 2095 $aMailbox['OFFSET'] = $aMailbox['PAGEOFFSET'] - 1 ;
324ac3c5 2096 }
2097 }
2098 }
2099 } else {
2100 if ($sButton == 'expunge') {
2101 /**
2102 * on expunge we do not know which messages will be deleted
2103 * so it's useless to try to sync the cache
2104
2105 * Close the mailbox so we do not need to parse the untagged expunge
2106 * responses which do not contain uid info.
2107 * NB: Closing a mailbox is faster then expunge because the imap
2108 * server does not need to generate the untagged expunge responses
2109 */
2110 sqimap_run_command($imapConnection,'CLOSE',false,$result,$message);
2111 $aMbxResponse = sqimap_mailbox_select($imapConnection,$aMailbox['NAME']);
2112 // update the $aMailbox array
2113 $aMailbox['EXISTS'] = $aMbxResponse['EXISTS'];
2114 $aMailbox['UIDSET'] = false;
2115 } else {
2116 if ($sButton) {
2117 $sError = _("No messages were selected.");
2118 }
2119 }
2120 }
2121 return $sError;
2122}
2123
2124function attachSelectedMessages($imapConnection,$aMsgHeaders) {
2125 global $username, $attachment_dir,
2126 $data_dir, $composesession,
2127 $compose_messages;
2128
2129 if (!isset($compose_messages)) {
2130 $compose_messages = array();
2131 sqsession_register($compose_messages,'compose_messages');
2132 }
2133
2134 if (!$composesession) {
2135 $composesession = 1;
2136 sqsession_register($composesession,'composesession');
2137 } else {
2138 $composesession++;
2139 sqsession_register($composesession,'composesession');
2140 }
2141
2142 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
2143
2144 $composeMessage = new Message();
2145 $rfc822_header = new Rfc822Header();
2146 $composeMessage->rfc822_header = $rfc822_header;
2147 $composeMessage->reply_rfc822_header = '';
2148
2149 foreach($aMsgHeaders as $iUid => $aMsgHeader) {
2150 /**
2151 * Retrieve the full message
2152 */
2153 $body_a = sqimap_run_command($imapConnection, "FETCH $iUid RFC822", true, $response, $readmessage, TRUE);
2154
2155 if ($response == 'OK') {
2156 $subject = (isset($aMsgHeader['SUBJECT'])) ? $aMsgHeader['SUBJECT'] : $iUid;
2157
2158 array_shift($body_a);
2159 array_pop($body_a);
2160 $body = implode('', $body_a);
2161 $body .= "\r\n";
2162
2163 $localfilename = GenerateRandomString(32, 'FILE', 7);
2164 $full_localfilename = "$hashed_attachment_dir/$localfilename";
2165
2166 $fp = fopen( $full_localfilename, 'wb');
2167 fwrite ($fp, $body);
2168 fclose($fp);
2169 $composeMessage->initAttachment('message/rfc822',$subject.'.msg',
2170 $full_localfilename);
2171 }
2172 }
2173
2174 $compose_messages[$composesession] = $composeMessage;
2175 sqsession_register($compose_messages,'compose_messages');
2176 return $composesession;
2177}
2178
043f9c14 2179// vim: et ts=4
b4482518 2180?>