Flush instead of echo
[squirrelmail.git] / functions / mailbox_display.php
CommitLineData
59177427 1<?php
2ba13803 2
35586184 3/**
4 * mailbox_display.php
5 *
15e6162e 6 * Copyright (c) 1999-2002 The SquirrelMail Project Team
35586184 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 * $Id$
13 */
a4c2cd49 14
46d38f78 15require_once('../functions/strings.php');
869ece5d 16require_once('../functions/imap_utf7_decode_local.php');
43fdb2a4 17
6c930ade 18/* Default value for page_selector_max. */
19define('PG_SEL_MAX', 10);
20
21function printMessageInfo($imapConnection, $t, $i, $key, $mailbox, $sort,
22 $start_msg, $where, $what){
23 global $checkall,
24 $color, $msgs, $msort,
25 $sent_folder, $draft_folder,
26 $default_use_priority,
27 $message_highlight_list,
28 $index_order,
29 $indent_array, /* indent subject by */
30 $pos, /* Search postion (if any) */
31 $thread_sort_messages, /* thread sorting on/off */
aa0da530 32 $server_sort_order, /* sort value when using server-sorting */
33 $row_count,
34 $allow_server_sort; /* enable/disable server-side sorting */
94ac35c6 35 $color_string = $color[4];
6c930ade 36
37 if ($GLOBALS['alt_index_colors']) {
38 if (!isset($row_count)) {
39 $row_count = 0;
bdfb67f8 40 }
6c930ade 41 $row_count++;
42 if ($row_count % 2) {
43 if (!isset($color[12])) {
44 $color[12] = '#EAEAEA';
45 }
46 $color_string = $color[12];
bdfb67f8 47 }
6c930ade 48 }
49 $msg = $msgs[$key];
50
51 /*
52 * This is done in case you're looking into Sent folders,
53 * because you can have multiple receivers.
54 */
55 $senderNames = explode(',', $msg['FROM']);
56 $senderName = '';
57 if (sizeof($senderNames)){
58 foreach ($senderNames as $senderNames_part) {
59 if ($senderName != '') {
60 $senderName .= ', ';
61 }
62 $senderName .= sqimap_find_displayable_name($senderNames_part);
bdfb67f8 63 }
6c930ade 64 }
65
66 if( $mailbox == 'None' ) {
67 $boxes = sqimap_mailbox_list($imapConnection);
68 $mailbox = $boxes[0]['unformatted'];
69 unset( $boxes );
70 }
71 $urlMailbox = urlencode($mailbox);
72 $subject = processSubject($msg['SUBJECT']);
73 echo "<TR>\n";
74
75 if (isset($msg['FLAG_FLAGGED']) && ($msg['FLAG_FLAGGED'] == true)) {
76 $flag = "<font color=\"$color[2]\">";
77 $flag_end = '</font>';
78 } else {
79 $flag = '';
80 $flag_end = '';
81 }
82 if (!isset($msg['FLAG_SEEN']) || ($msg['FLAG_SEEN'] == false)) {
83 $bold = '<b>';
84 $bold_end = '</b>';
85 } else {
86 $bold = '';
87 $bold_end = '';
88 }
89 if (handleAsSent($mailbox)) {
90 $italic = '<i>';
91 $italic_end = '</i>';
92 } else {
93 $italic = '';
94 $italic_end = '';
95 }
96 if (isset($msg['FLAG_DELETED']) && $msg['FLAG_DELETED']) {
97 $fontstr = "<font color=\"$color[9]\">";
98 $fontstr_end = '</font>';
99 } else {
100 $fontstr = '';
101 $fontstr_end = '';
102 }
103
104 /**
105 * AAAAH! Make my eyes stop bleeding!
106 * Who wrote this?!
107 */
108 if (sizeof($message_highlight_list)){
109 foreach ($message_highlight_list as $message_highlight_list_part) {
110 if (trim($message_highlight_list_part['value']) != '') {
111 if ($message_highlight_list_part['match_type'] == 'to_cc') {
112 if (strstr('^^' . strtolower($msg['TO']),
113 strtolower($message_highlight_list_part['value']))
114 || strstr('^^'.strtolower($msg['CC']),
115 strtolower($message_highlight_list_part['value']))) {
116 $hlt_color = $message_highlight_list_part['color'];
117 continue;
118 }
119 } else
120 if (strstr('^^' . strtolower($msg[strtoupper($message_highlight_list_part['match_type'])]),
121 strtolower($message_highlight_list_part['value']))) {
122 $hlt_color = $message_highlight_list_part['color'];
123 continue;
124 }
125 }
bdfb67f8 126 }
6c930ade 127 }
128
129 if (!isset($hlt_color)) {
130 $hlt_color = $color_string;
131 }
132
133 if ($where && $what) {
134 if(!isset($pos) || $pos == '') {
135 $pos = '0';
bdfb67f8 136 }
6c930ade 137 $search_stuff = "&amp;pos=" . urlencode($pos)
138 . "&amp;where=" . urlencode($where) . '&amp;what=' . urlencode($what);
139 } else {
140 $search_stuff = '';
141 }
142
143 $checked = ($checkall == 1) ? ' checked' : '';
144
145 if (sizeof($index_order)){
146 foreach ($index_order as $index_order_part) {
147 switch ($index_order_part) {
148 case 1: /* checkbox */
149 echo " <td bgcolor=\"$hlt_color\" align=center>"
150 . "<input type=checkbox name=\"msg[$t]\" value="
151 . $msg["ID"]."$checked></TD>\n";
152 break;
153 case 2: /* from */
154 echo " <td bgcolor=\"$hlt_color\">$italic$bold$flag$fontstr"
155 . "$senderName$fontstr_end$flag_end$bold_end$italic_end</td>\n";
156 break;
157 case 3: /* date */
158 echo " <td nowrap bgcolor=\"$hlt_color\"><center>$bold$flag$fontstr"
159 . $msg["DATE_STRING"]
160 . "$fontstr_end$flag_end$bold_end</center></td>\n";
161 break;
162 case 4: /* subject */
163 echo " <td bgcolor=\"$hlt_color\">$bold";
164 if ($thread_sort_messages == 1) {
2728fa19 165 if (isset($indent_array[$msg["ID"]])) {
166 echo str_repeat("&nbsp;&nbsp;&nbsp;&nbsp;",$indent_array[$msg["ID"]]);
167 }
168 }
6c930ade 169 echo "<a href=\"read_body.php?mailbox=$urlMailbox&amp;passed_id="
170 . $msg["ID"]
171 . "&amp;startMessage=$start_msg&amp;show_more=0$search_stuff\"";
172 do_hook("subject_link");
173 if ($subject != $msg['SUBJECT']) {
174 $title = get_html_translation_table(HTML_SPECIALCHARS);
175 $title = array_flip($title);
176 $title = strtr($msg['SUBJECT'], $title);
177 $title = str_replace('"', "''", $title);
178 echo " title=\"$title\"";
179 }
180 echo ">$flag$subject$flag_end</a>$bold_end</td>\n";
181 break;
182 case 5: /* flags */
183 $stuff = false;
184 echo " <td bgcolor=\"$hlt_color\" align=center nowrap><b><small>\n";
185 if (isset($msg['FLAG_ANSWERED']) && $msg['FLAG_ANSWERED'] == true) {
186 echo _("A") . "\n";
187 $stuff = true;
188 }
189 if ($msg['TYPE0'] == 'multipart') {
190 echo "+\n";
191 $stuff = true;
192 }
193 if ($default_use_priority) {
e3324f30 194 if ( ($msg['PRIORITY'] == 1) || ($msg['PRIORITY'] == 2) ) {
6c930ade 195 echo "<font color=\"$color[1]\">!</font>\n";
196 $stuff = true;
197 }
e3324f30 198 if ($msg['PRIORITY'] == 5) {
6c930ade 199 echo "<font color=\"$color[8]\">?</font>\n";
200 $stuff = true;
201 }
202 }
203 if (isset($msg['FLAG_DELETED']) && $msg['FLAG_DELETED'] == true) {
204 echo "<font color=\"$color[1]\">D</font>\n";
205 $stuff = true;
206 }
207 if (!$stuff) {
208 echo "&nbsp;\n";
209 }
210 echo "</small></b></td>\n";
211 break;
212 case 6: /* size */
213 echo " <td bgcolor=\"$hlt_color\">$bold$fontstr"
214 . show_readable_size($msg['SIZE']) . "$fontstr_end$bold_end</td>\n";
215 break;
216 }
bdfb67f8 217 }
6c930ade 218 }
219 echo "</tr>\n";
bdfb67f8 220}
221
6c930ade 222/*
bdfb67f8 223 * This function loops through a group of messages in the mailbox
224 * and shows them to the user.
225 */
6c930ade 226function showMessagesForMailbox($imapConnection, $mailbox, $num_msgs,
227 $start_msg, $sort, $color, $show_num,
228 $use_cache) {
229 global $msgs, $msort,
230 $sent_folder, $draft_folder,
aa0da530 231 $message_highlight_list,
232 $auto_expunge, $thread_sort_messages, $allow_server_sort,
233 $data_dir, $username, $server_sort_order;
234
4e9f35e4 235 /* if $start_msg is lower than $num_msgs, we probably deleted all messages
236 * in the last page. We need to re-adjust the start_msg
237 */
238
239 if($start_msg > $num_msgs) {
240 $start_msg -= $show_num;
241 if($start_msg < 1) {
242 $start_msg = 1;
243 }
244 }
245
aa0da530 246 /* This code and the next if() block check for
247 * server-side sorting methods. The $id array is
248 * formatted and $sort is set to 6 to disable
249 * SM internal sorting
250 */
6c930ade 251 if ($thread_sort_messages == 1) {
252 $id = get_thread_sort($imapConnection);
26eca02e 253 if ($id == 'no') {
254 echo '<b><small><center><font color=red>Thread sorting is not'.
255 ' supported by your IMAP server.<br>Please report this'.
f24c898b 256 ' to the system administrator.</center></small></b>';
0c46707d 257 $thread_sort_messages = 0;
26eca02e 258 $id = array();
259 }
260 else {
261 $sort = 6;
262 if ($start_msg + ($show_num - 1) < $num_msgs) {
263 $end_msg = $start_msg + ($show_num-1);
264 }
265 else {
266 $end_msg = $num_msgs;
267 }
268 $id = array_slice($id, ($start_msg-1), ($end_msg));
269 }
aa0da530 270 }
271
794d59c0 272 if ($allow_server_sort == TRUE && $thread_sort_messages != 1) {
aa0da530 273 $server_sort_order = $sort;
274 $id = sqimap_get_sort_order($imapConnection, $server_sort_order);
26eca02e 275 if ($id == 'no') {
f24c898b 276 echo '<b><small><center><font color=red>Server-side sorting'.
277 ' is not supported by your IMAP server.<br>Please report this'.
26eca02e 278 ' to the system administrator.</center></small></b>';
279 $sort = $server_sort_order;
794d59c0 280 $allow_server_sort = FALSE;
26eca02e 281 $id = array();
282 }
aa0da530 283 else {
26eca02e 284 $sort = 6;
285 if ($start_msg + ($show_num - 1) < $num_msgs) {
286 $end_msg = $start_msg + ($show_num-1);
287 }
288 else {
289 $end_msg = $num_msgs;
290 }
291 $id = array_slice($id, ($start_msg-1), ($end_msg));
292 }
6c930ade 293 }
aa0da530 294
295 /* If autoexpunge is turned on, then do it now. */
6c930ade 296 if ($auto_expunge == true) {
297 sqimap_mailbox_expunge($imapConnection, $mailbox, false);
298 }
299 sqimap_mailbox_select($imapConnection, $mailbox);
300 $issent = handleAsSent($mailbox);
301 if (!$use_cache) {
302 /* If it is sorted... */
303 if ($num_msgs >= 1) {
304 if ($sort < 6 ) {
305 $id = range(1, $num_msgs);
306 }
794d59c0 307 elseif ($thread_sort_messages != 1 && $allow_server_sort != TRUE && $sort == 6) {
6c930ade 308 /* if it's not sorted */
309 if ($start_msg + ($show_num - 1) < $num_msgs){
310 $end_msg = $start_msg + ($show_num - 1);
311 } else {
312 $end_msg = $num_msgs;
313 }
314 if ($end_msg < $start_msg) {
315 $start_msg = $start_msg - $show_num;
316 if ($start_msg < 1) {
317 $start_msg = 1;
318 }
319 }
320
321 $real_startMessage = $num_msgs - $start_msg + 1;
322 $real_endMessage = $num_msgs - $start_msg - $show_num + 2;
323 if ($real_endMessage <= 0) {
324 $real_endMessage = 1;
325 }
326 $id = array_reverse(range($real_endMessage, $real_startMessage));
327 }
328 $msgs_list = sqimap_get_small_header_list($imapConnection, $id, $issent);
0811196d 329// $flags = sqimap_get_flags_list($imapConnection, $id, $issent);
6c930ade 330 if (sizeof($msgs_list)){
331 foreach ($msgs_list as $hdr) {
332 $from[] = $hdr->from;
333 $date[] = $hdr->date;
334 $subject[] = $hdr->subject;
335 $to[] = $hdr->to;
336 $priority[] = $hdr->priority;
337 $cc[] = $hdr->cc;
338 $size[] = $hdr->size;
339 $type[] = $hdr->type0;
0811196d 340 $flag_deleted[] = $hdr->flag_deleted;
341 $flag_answered[] = $hdr->flag_answered;
342 $flag_seen[] = $hdr->flag_seen;
343 $flag_flagged[] = $hdr->flag_flagged;
6c930ade 344 }
345 }
bdfb67f8 346 }
6c930ade 347 $j = 0;
348 if ($sort == 6) {
349 $end = $start_msg + $show_num - 1;
350 if ($num_msgs < $show_num) {
351 $end_loop = $num_msgs;
352 } else if ($end > $num_msgs) {
353 $end_loop = $num_msgs - $start_msg + 1;
354 } else {
355 $end_loop = $show_num;
356 }
357 } else {
358 $end = $num_msgs;
359 $end_loop = $end;
7c612fdd 360 }
6c930ade 361 while ($j < $end_loop) {
362 if (isset($date[$j])) {
363 $date[$j] = str_replace(' ', ' ', $date[$j]);
364 $tmpdate = explode(' ', trim($date[$j]));
365 } else {
366 $tmpdate = $date = array('', '', '', '', '', '');
367 }
368
369 $messages[$j]['TIME_STAMP'] = getTimeStamp($tmpdate);
370 $messages[$j]['DATE_STRING'] =
371 getDateString($messages[$j]['TIME_STAMP']);
372 $messages[$j]['ID'] = $id[$j];
373 $messages[$j]['FROM'] = decodeHeader($from[$j]);
374 $messages[$j]['FROM-SORT'] =
375 strtolower(sqimap_find_displayable_name(decodeHeader($from[$j])));
376 $messages[$j]['SUBJECT'] = decodeHeader($subject[$j]);
377 $messages[$j]['SUBJECT-SORT'] = strtolower(decodeHeader($subject[$j]));
378 $messages[$j]['TO'] = decodeHeader($to[$j]);
379 $messages[$j]['PRIORITY'] = $priority[$j];
380 $messages[$j]['CC'] = $cc[$j];
381 $messages[$j]['SIZE'] = $size[$j];
382 $messages[$j]['TYPE0'] = $type[$j];
0811196d 383 $messages[$j]['FLAG_DELETED'] = $flag_deleted[$j];
384 $messages[$j]['FLAG_ANSWERED'] = $flag_answered[$j];
385 $messages[$j]['FLAG_SEEN'] = $flag_seen[$j];
386 $messages[$j]['FLAG_FLAGGED'] = $flag_flagged[$j];
387
6c930ade 388
389 /*
390 * fix SUBJECT-SORT to remove Re:
391 * vedr|sv (Danish)
392 * re|aw (English)
393 *
394 * TODO: i18n should be incorporated here. E.g. we catch the ones
395 * we know about, but also define in i18n what the localized
396 * "Re: " is for this or that locale.
397 */
398 if (preg_match("/^(vedr|sv|re|aw):\s*(.*)$/si",
399 $messages[$j]['SUBJECT-SORT'], $matches)){
400 $messages[$j]['SUBJECT-SORT'] = $matches[2];
401 }
0811196d 402/*
6c930ade 403 $num = 0;
404 while ($num < count($flags[$j])) {
405 if ($flags[$j][$num] == 'Deleted') {
406 $messages[$j]['FLAG_DELETED'] = true;
407 } else if ($flags[$j][$num] == 'Answered') {
408 $messages[$j]['FLAG_ANSWERED'] = true;
409 } else if ($flags[$j][$num] == 'Seen') {
410 $messages[$j]['FLAG_SEEN'] = true;
411 } else if ($flags[$j][$num] == 'Flagged') {
412 $messages[$j]['FLAG_FLAGGED'] = true;
413 }
414 $num++;
415 }
8d53b72a 416*/
e3324f30 417 $j++;
418
bdfb67f8 419 }
bdfb67f8 420
6c930ade 421 /*
422 * Only ignore messages flagged as deleted if we are
423 * using a trash folder or auto_expunge
424 */
425 if (((isset($move_to_trash) && $move_to_trash)
426 || (isset($auto_expunge) && $auto_expunge))
427 && $sort != 6) {
428
429 /* Find and remove the ones that are deleted */
430 $i = 0;
431 $j = 0;
432
433 while ($j < $num_msgs) {
434 if (isset($messages[$j]['FLAG_DELETED'])
435 && $messages[$j]['FLAG_DELETED'] == true) {
436 $j++;
437 continue;
438 }
439 $msgs[$i] = $messages[$j];
440
441 $i++;
442 $j++;
443 }
444 $num_msgs = $i;
bdfb67f8 445 } else {
6c930ade 446 if (!isset($messages)) {
447 $messages = array();
448 }
449 $msgs = $messages;
bdfb67f8 450 }
6c930ade 451 }
bdfb67f8 452
6c930ade 453 /* There's gotta be messages in the array for it to sort them. */
454 if (($num_msgs > 0) && (!$use_cache)) {
455 /*
456 * 0 = Date (up)
457 * 1 = Date (dn)
458 * 2 = Name (up)
459 * 3 = Name (dn)
460 * 4 = Subject (up)
461 * 5 = Subject (dn)
462 */
463 session_unregister('msgs');
464 if (($sort == 0) || ($sort == 1)) {
465 $msort = array_cleave ($msgs, 'TIME_STAMP');
466 } elseif (($sort == 2) || ($sort == 3)) {
467 $msort = array_cleave ($msgs, 'FROM-SORT');
468 } elseif (($sort == 4) || ($sort == 5)) {
469 $msort = array_cleave ($msgs, 'SUBJECT-SORT');
470 } else {
471 $msort = $msgs;
bdfb67f8 472 }
6c930ade 473
474 if ($sort < 6) {
475 if ($sort % 2) {
476 asort($msort);
477 } else {
478 arsort($msort);
479 }
480 }
481 session_register('msort');
794d59c0 482 } elseif ($thread_sort_messages == 1 || $allow_server_sort == TRUE) {
6c930ade 483 $msort = $msgs;
484 session_unregister('msgs');
485 session_register('msort');
486 }
487 displayMessageArray($imapConnection, $num_msgs, $start_msg, $msgs,
488 $msort, $mailbox, $sort, $color,$show_num);
489 /**
490 * TODO: Switch to using $_SESSION[] whenever we ditch the 4.0.x series.
491 */
492 session_register('msgs');
493}
bdfb67f8 494
6c930ade 495/* Generic function to convert the msgs array into an HTML table. */
496function displayMessageArray($imapConnection, $num_msgs, $start_msg,
497 &$msgs, $msort, $mailbox, $sort, $color,
498 $show_num) {
94ac35c6 499
500 global $folder_prefix, $sent_folder,
501 $imapServerAddress, $data_dir, $username, $use_mailbox_cache,
502 $index_order, $real_endMessage, $real_startMessage, $checkall,
503 $indent_array, $thread_sort_messages, $allow_server_sort,
504 $server_sort_order, $html;
505
506 /* If cache isn't already set, do it now. */
507 if (!session_is_registered('msgs')) {
508 session_register('msgs');
bdfb67f8 509 }
94ac35c6 510 if (!session_is_registered('msort')) {
511 session_register('msort');
7c612fdd 512 }
94ac35c6 513
514 if ($start_msg + ($show_num - 1) < $num_msgs){
515 $end_msg = $start_msg + ($show_num - 1);
bdfb67f8 516 } else {
94ac35c6 517 $end_msg = $num_msgs;
bdfb67f8 518 }
94ac35c6 519
520 if ($end_msg < $start_msg) {
521 $start_msg = $start_msg - $show_num;
522 if ($start_msg < 1) {
523 $start_msg = 1;
524 }
525 }
526
527 $urlMailbox = urlencode($mailbox);
528
529 do_hook('mailbox_index_before');
530
531 $msg_cnt_str = get_msgcnt_str($start_msg, $end_msg, $num_msgs);
532 $paginator_str = get_paginator_str($urlMailbox, $start_msg, $end_msg,
533 $num_msgs, $show_num, $sort);
534
535 if (!isset($msg)) {
536 $msg = '';
537 }
538
539 /* get indent level for subject display */
540 if ($thread_sort_messages == 1 ) {
541 $indent_array = get_parent_level($imapConnection);
542 }
543 $fstring = "move_messages.php?msg=$msg&amp;mailbox=$urlMailbox"
544 . "&amp;startMessage=$start_msg";
545 mail_message_listing_beginning($imapConnection, $fstring,
546 $mailbox, $sort, $msg_cnt_str,
547 $paginator_str, $start_msg);
548
549 $groupNum = $start_msg % ($show_num - 1);
550 $real_startMessage = $start_msg;
551 if ($sort == 6) {
552 if ($end_msg - $start_msg < $show_num - 1) {
553 $end_msg = $end_msg - $start_msg + 1;
554 $start_msg = 1;
555 } else if ($start_msg > $show_num) {
556 $end_msg = $show_num;
557 $start_msg = 1;
558 }
559 }
560 $endVar = $end_msg + 1;
561
562 /*
563 * Loop through and display the info for each message.
564 * ($t is used for the checkbox number)
565 */
566 $t = 0;
567 if ($num_msgs == 0) {
568 /* if there's no messages in this folder */
569 echo "<TR><TD BGCOLOR=\"$color[4]\" COLSPAN="
570 . count($index_order) . ">\n"
571 . " <CENTER><BR><B>". _("THIS FOLDER IS EMPTY")
572 . "</B><BR>&nbsp;</CENTER>\n"
573 . "</TD></TR>";
574 } elseif ($start_msg == $end_msg) {
575 /* if there's only one message in the box, handle it differently. */
576 if ($sort != 6){
577 $i = $start_msg;
578 } else {
579 $i = 1;
580 }
581 reset($msort);
582 $k = 0;
583 do {
584 $key = key($msort);
585 next($msort);
586 $k++;
587 } while (isset ($key) && ($k < $i));
588 printMessageInfo($imapConnection, $t, $i, $key, $mailbox, $sort,
589 $real_startMessage, 0, 0);
590 } else {
591 $i = $start_msg;
592 reset($msort);
593 $k = 0;
594 do {
595 $key = key($msort);
596 next($msort);
597 $k++;
598 } while (isset ($key) && ($k < $i));
599 do {
600 printMessageInfo($imapConnection, $t, $i, $key, $mailbox,
601 $sort, $real_startMessage, 0, 0);
602 $key = key($msort);
603 $t++;
604 $i++;
605 next($msort);
606 } while ($i && $i < $endVar);
607 }
608
8e3dd603 609 echo '</table>';
610 $html->flush(
94ac35c6 611 $html->tag( 'table',
612 $html->tag( 'tr',
613 $html->tag( 'td',
614 $html->tag( 'table',
615 $html->tag( 'tr',
616 $html->tag( 'td', $paginator_str ) .
617 $html->tag( 'td', $msg_cnt_str, array( 'align' => 'right' ) )
618 )
619 , array( 'bgcolor'=> $color[4],
620 'width' => '100%',
621 'cellpadding' => 1,
622 'cellspacing' => 1 ) )
623 )
624 , array( 'bgcolor' => $color[4] ) )
625 , array( 'bgcolor' => $color[9],
626 'width' => '100%',
627 'cellpadding' => 1,
8e3dd603 628 'cellspacing' => 1 ) )
629 );
94ac35c6 630
631 /* End of message-list table */
632
633 do_hook('mailbox_index_after');
634 echo "</TABLE></FORM>\n";
bdfb67f8 635}
636
6c930ade 637/*
bdfb67f8 638 * Displays the standard message list header. To finish the table,
639 * you need to do a "</table></table>";
640 *
641 * $moveURL is the URL to submit the delete/move form to
642 * $mailbox is the current mailbox
643 * $sort is the current sorting method (-1 for no sorting available [searches])
644 * $Message is a message that is centered on top of the list
645 * $More is a second line that is left aligned
646 */
cd885078 647
6c930ade 648function mail_message_listing_beginning ($imapConnection, $moveURL,
649 $mailbox = '', $sort = -1,
650 $msg_cnt_str = '',
651 $paginator = '&nbsp;',
652 $start_msg = 1) {
653 global $color, $index_order, $auto_expunge, $move_to_trash, $base_uri,
654 $checkall, $sent_folder, $draft_folder, $thread_sort_messages,
cd885078 655 $allow_thread_sort, $allow_server_sort, $server_sort_order,
656 $lastTargetMailbox;
657
6c930ade 658 $urlMailbox = urlencode($mailbox);
659
660 /*
661 * This is the beginning of the message list table.
662 * It wraps around all messages
663 */
c9062c05 664 echo "<FORM name=\"messageList\" method=post action=\"$moveURL\">\n"
6c930ade 665 . "<TABLE WIDTH=\"100%\" BORDER=\"0\" CELLPADDING=\"1\" "
666 . "CELLSPACING=\"0\">\n<TR BGCOLOR=\"$color[0]\"><TD>"
667 . " <TABLE BGCOLOR=\"$color[4]\" width=\"100%\" CELLPADDING=\"2\" "
668 . "CELLSPACING=\"0\" BORDER=\"0\"><TR>\n"
669 . " <TD ALIGN=LEFT>$paginator\n";
670
6c930ade 671 echo " <TD ALIGN=RIGHT>$msg_cnt_str</TD>\n"
672 . " </TR></TABLE>\n"
673 . '</TD></TR>'
674 . "<TR><TD BGCOLOR=\"$color[0]\">\n"
c9062c05 675 . "<TABLE BGCOLOR=\"$color[0]\" BORDER=0 cellpadding=0 "
6c930ade 676 . "cellspacing=0 width=\"100%\">\n"
677 . " <TR>\n"
678 . " <TD ALIGN=LEFT VALIGN=MIDDLE NOWRAP>\n"
679 . ' <SMALL>&nbsp;' . _("Move Selected To:") . "</SMALL>\n"
680 . " </TD>\n"
681 . " <TD ALIGN=RIGHT NOWRAP>\n"
682 . ' <SMALL>&nbsp;' . _("Transform Selected Messages")
683 . ": &nbsp; </SMALL><BR>\n"
684 . " </TD>\n"
685 . " </TR>\n"
686 . " <TR>\n"
687 . " <TD ALIGN=\"LEFT\" VALIGN=\"MIDDLE\" NOWRAP>\n"
688 . ' <SMALL>&nbsp;<TT><SELECT NAME="targetMailbox">';
689
690 $boxes = sqimap_mailbox_list($imapConnection);
691 foreach ($boxes as $boxes_part) {
692 if (!in_array('noselect', $boxes_part['flags'])) {
693 $box = $boxes_part['unformatted'];
48cb84d6 694 $box2 = str_replace(' ', '&nbsp;', imap_utf7_decode_local($boxes_part['unformatted-disp']));
6c930ade 695 if( $box2 == 'INBOX' ) {
696 $box2 = _("INBOX");
697 }
cd885078 698 if ($lastTargetMailbox == $box) {
699 echo " <OPTION VALUE=\"$box\" SELECTED>$box2</OPTION>\n";
700 }
701 else {
702 echo " <OPTION VALUE=\"$box\">$box2</OPTION>\n";
703 }
bdfb67f8 704 }
6c930ade 705 }
706 echo ' </SELECT></TT>&nbsp;'
707 . '<INPUT TYPE="SUBMIT" NAME="moveButton" VALUE="' . _("Move") . '">&nbsp;'
708 . '<INPUT TYPE="SUBMIT" NAME="attache" VALUE="' . _("Forward")
709 . "\">&nbsp;\n" . "</SMALL>\n";
710
711 echo " </TD>\n"
712 . " <TD ALIGN=\"RIGHT\" NOWRAP>";
713 if (!$auto_expunge) {
714 echo '<INPUT TYPE=SUBMIT NAME="expungeButton" VALUE="' . _("Expunge")
715 . '">&nbsp;' . _("mailbox") . '&nbsp;';
716 }
717 echo '<INPUT TYPE="SUBMIT" NAME="markRead" VALUE="' . _("Read") . '">'
718 . '<INPUT TYPE="SUBMIT" NAME="markUnread" VALUE="' . _("Unread") . '">'
719 . '<INPUT TYPE="SUBMIT" VALUE="' . _("Delete") . '">&nbsp;'
720 . "</TD>\n"
aa0da530 721 . " </TR>\n";
722
3baefe86 723 /* draws thread sorting links */
794d59c0 724 if ($allow_thread_sort == TRUE) {
aa0da530 725 if ($thread_sort_messages == 1 ) {
726 $set_thread = 2;
cd17dc27 727 $thread_name = _("Unthread View");
aa0da530 728 }
729 elseif ($thread_sort_messages == 0) {
730 $set_thread = 1;
cd17dc27 731 $thread_name = _("Thread View");
aa0da530 732 }
733 echo '<tr><td>&nbsp;<a href=' . "$base_uri" . 'src/right_main.php?sort='
734 . "$sort" . '&start_messages=1&set_thread=' . "$set_thread"
cd17dc27 735 . '&mailbox=' . urlencode($mailbox) . '><small>' . $thread_name
aa0da530 736 . '</a></small>&nbsp;</td></tr>';
737 }
738
739 echo "</TABLE>\n";
6c930ade 740 do_hook('mailbox_form_before');
741 echo '</TD></TR>'
742 . "<TR><TD BGCOLOR=\"$color[0]\">"
743 . '<TABLE WIDTH="100%" BORDER=0 CELLPADDING=2 CELLSPACING=';
744 if ($GLOBALS['alt_index_colors']){
745 echo '0';
746 } else {
747 echo '1';
748 }
749 echo " BGCOLOR=\"$color[0]\">"
750 . "<TR BGCOLOR=\"$color[5]\" ALIGN=\"center\">";
aa0da530 751 /* if using server sort we highjack the
752 * the $sort var and use $server_sort_order
753 * instead. but here we reset sort for a bit
754 * since its easy
755 */
794d59c0 756 if ($allow_server_sort == TRUE) {
aa0da530 757 $sort = $server_sort_order;
758 }
6c930ade 759 /* Print the headers. */
760 for ($i=1; $i <= count($index_order); $i++) {
761 switch ($index_order[$i]) {
762 case 1: /* checkbox */
763 case 5: /* flags */
764 echo ' <TD WIDTH="1%"><B>&nbsp;</B></TD>';
765 break;
766 case 2: /* from */
767 if (handleAsSent($mailbox)) {
768 echo ' <TD WIDTH="25%"><B>' . _("To") . '</B>';
769 } else {
770 echo ' <TD WIDTH="25%"><B>' . _("From") . '</B>';
771 }
794d59c0 772 if ($allow_thread_sort != TRUE || $thread_sort_messages != 1) {
6c930ade 773 ShowSortButton($sort, $mailbox, 2, 3);
774 }
775 echo "</TD>\n";
776 break;
777 case 3: /* date */
778 echo ' <TD NOWRAP WIDTH="5%"><B>' . _("Date") . '</B>';
794d59c0 779 if ($allow_thread_sort != TRUE || $thread_sort_messages != 1) {
6c930ade 780 ShowSortButton($sort, $mailbox, 0, 1);
781 }
782 echo "</TD>\n";
783 break;
784 case 4: /* subject */
785 echo ' <TD><B>' . _("Subject") . '</B> ';
794d59c0 786 if ($allow_thread_sort != TRUE || $thread_sort_messages != 1) {
6c930ade 787 ShowSortButton($sort, $mailbox, 4, 5);
788 }
789 echo "</TD>\n";
790 break;
791 case 6: /* size */
792 echo ' <TD WIDTH="5%"><b>' . _("Size") . "</b></TD>\n";
793 break;
bdfb67f8 794 }
6c930ade 795 }
aa0da530 796 /* if using server-sorting,
797 * send sort back to 6
798 */
794d59c0 799 if ($allow_server_sort == TRUE) {
aa0da530 800 $sort = 6;
801 }
6c930ade 802 echo "</TR>\n";
bdfb67f8 803}
804
805/*
806 * This function shows the sort button. Isn't this a good comment?
807 */
808function ShowSortButton($sort, $mailbox, $Up, $Down) {
6c930ade 809 /* Figure out which image we want to use. */
810 if ($sort != $Up && $sort != $Down) {
811 $img = 'sort_none.png';
812 $which = $Up;
813 } elseif ($sort == $Up) {
814 $img = 'up_pointer.png';
815 $which = $Down;
816 } else {
817 $img = 'down_pointer.png';
818 $which = 6;
819 }
820
821 /* Now that we have everything figured out, show the actual button. */
822 echo ' <a href="right_main.php?newsort=' . $which
823 . '&amp;startMessage=1&amp;mailbox=' . urlencode($mailbox)
824 . '"><IMG SRC="../images/' . $img
c9062c05 825 . '" BORDER=0 WIDTH=12 HEIGHT=10 ALT="sort"></a>';
bdfb67f8 826}
827
828function get_selectall_link($start_msg, $sort) {
6c930ade 829 global $checkall, $what, $where, $mailbox, $javascript_on;
830 global $PHP_SELF, $PG_SHOWNUM;
831
832 $result = '';
833 if ($javascript_on) {
834 $result =
835 '<script language="JavaScript" type="text/javascript">'
836 . "\n<!-- \n"
837 . "function CheckAll() {\n"
838 . " for (var i = 0; i < document.messageList.elements.length; i++) {\n"
839 . " if(document.messageList.elements[i].type == 'checkbox'){\n"
840 . " document.messageList.elements[i].checked = "
841 . " !(document.messageList.elements[i].checked);\n"
842 . " }\n"
843 . " }\n"
844 . "}\n"
845 . "//-->\n"
846 . '</script><a href="#" onClick="CheckAll();">' . _("Toggle All")
847 . "</a>\n";
848 } else {
849 if (strpos($PHP_SELF, "?")) {
850 $result .= "<a href=\"$PHP_SELF&amp;mailbox=" . urlencode($mailbox)
851 . "&amp;startMessage=$start_msg&amp;sort=$sort&amp;checkall=";
852 } else {
853 $result .= "<a href=\"$PHP_SELF?mailbox=" . urlencode($mailbox)
854 . "&amp;startMessage=$start_msg&amp;sort=$sort&amp;checkall=";
855 }
856 if (isset($checkall) && $checkall == '1') {
857 $result .= '0';
bdfb67f8 858 } else {
6c930ade 859 $result .= '1';
23d6bd09 860 }
6c930ade 861
862 if (isset($where) && isset($what)) {
863 $result .= '&amp;where=' . urlencode($where)
864 . '&amp;what=' . urlencode($what);
865 }
866
6c930ade 867 $result .= "\">";
868
869 if (isset($checkall) && ($checkall == '1')) {
870 $result .= _("Unselect All");
871 } else {
872 $result .= _("Select All");
873 }
874
875 $result .= "</A>\n";
876 }
23d6bd09 877
6c930ade 878 /* Return our final result. */
879 return ($result);
bdfb67f8 880}
1a0e0983 881
6c930ade 882/*
883 * This function computes the "Viewing Messages..." string.
884 */
bdfb67f8 885function get_msgcnt_str($start_msg, $end_msg, $num_msgs) {
6c930ade 886 /* Compute the $msg_cnt_str. */
887 $result = '';
888 if ($start_msg < $end_msg) {
889 $result = sprintf(_("Viewing Messages: <B>%s</B> to <B>%s</B> (%s total)"),
890 $start_msg, $end_msg, $num_msgs);
891 } else if ($start_msg == $end_msg) {
892 $result = sprintf(_("Viewing Message: <B>%s</B> (1 total)"), $start_msg);
893 } else {
894 $result = '<br>';
895 }
896
897 /* Return our result string. */
898 return ($result);
bdfb67f8 899}
900
6c930ade 901/*
7b294953 902 * Generate a paginator link.
903 */
6c930ade 904function get_paginator_link($box, $start_msg, $use, $text) {
905 $result = "<A HREF=\"right_main.php?use_mailbox_cache=$use"
906 . "&amp;startMessage=$start_msg&amp;mailbox=$box\" "
907 . "TARGET=\"right\">$text</A>";
908 return ($result);
7b294953 909}
910
6c930ade 911/*
7b294953 912 * This function computes the paginator string.
913 */
6c930ade 914function get_paginator_str($box, $start_msg, $end_msg, $num_msgs,
915 $show_num, $sort) {
916 global $username, $data_dir, $use_mailbox_cache, $color, $PG_SHOWNUM;
917
918 /* Initialize paginator string chunks. */
919 $prv_str = '';
920 $nxt_str = '';
921 $pg_str = '';
922 $all_str = '';
923 $tgl_str = '';
924
925 /* Create simple strings that will be creating the paginator. */
926 $spc = '&nbsp;'; /* This will be used as a space. */
927 $sep = '|'; /* This will be used as a seperator. */
928
929 /* Get some paginator preference values. */
930 $pg_sel = getPref($data_dir, $username, 'page_selector', SMPREF_ON);
931 $pg_max = getPref($data_dir, $username, 'page_selector_max', PG_SEL_MAX);
932
933 /* Make sure that our start message number is not too big. */
934 $start_msg = min($start_msg, $num_msgs);
935
936 /* Decide whether or not we will use the mailbox cache. */
937 /* Not sure why $use_mailbox_cache is even passed in. */
938 if ($sort == 6) {
939 $use = 0;
940 } else {
941 $use = 1;
942 }
943
944 /* Compute the starting message of the previous and next page group. */
945 $next_grp = $start_msg + $show_num;
946 $prev_grp = $start_msg - $show_num;
947
948 /* Compute the basic previous and next strings. */
949 if (($next_grp <= $num_msgs) && ($prev_grp >= 0)) {
950 $prv_str = get_paginator_link($box, $prev_grp, $use, _("Previous"));
951 $nxt_str = get_paginator_link($box, $next_grp, $use, _("Next"));
952 } else if (($next_grp > $num_msgs) && ($prev_grp >= 0)) {
953 $prv_str = get_paginator_link($box, $prev_grp, $use, _("Previous"));
954 $nxt_str = "<FONT COLOR=\"$color[9]\">"._("Next")."</FONT>\n";
955 } else if (($next_grp <= $num_msgs) && ($prev_grp < 0)) {
956 $prv_str = "<FONT COLOR=\"$color[9]\">"._("Previous") . '</FONT>';
957 $nxt_str = get_paginator_link($box, $next_grp, $use, _("Next"));
958 }
959
960 /* Page selector block. Following code computes page links. */
961 if ($pg_sel && ($num_msgs > $show_num)) {
962 /* Most importantly, what is the current page!!! */
963 $cur_pg = intval($start_msg / $show_num) + 1;
964
965 /* Compute total # of pages and # of paginator page links. */
966 $tot_pgs = ceil($num_msgs / $show_num); /* Total number of Pages */
967 $vis_pgs = min($pg_max, $tot_pgs - 1); /* Visible Pages */
968
969 /* Compute the size of the four quarters of the page links. */
970
971 /* If we can, just show all the pages. */
972 if (($tot_pgs - 1) <= $pg_max) {
973 $q1_pgs = $cur_pg - 1;
974 $q2_pgs = $q3_pgs = 0;
975 $q4_pgs = $tot_pgs - $cur_pg;
976
7b294953 977 /* Otherwise, compute some magic to choose the four quarters. */
6c930ade 978 } else {
979 /*
980 * Compute the magic base values. Added together,
981 * these values will always equal to the $pag_pgs.
982 * NOTE: These are DEFAULT values and do not take
983 * the current page into account. That is below.
984 */
985 $q1_pgs = floor($vis_pgs/4);
986 $q2_pgs = round($vis_pgs/4, 0);
987 $q3_pgs = ceil($vis_pgs/4);
988 $q4_pgs = round(($vis_pgs - $q2_pgs)/3, 0);
7b294953 989
6c930ade 990 /* Adjust if the first quarter contains the current page. */
991 if (($cur_pg - $q1_pgs) < 1) {
992 $extra_pgs = ($q1_pgs - ($cur_pg - 1)) + $q2_pgs;
993 $q1_pgs = $cur_pg - 1;
994 $q2_pgs = 0;
995 $q3_pgs += ceil($extra_pgs / 2);
996 $q4_pgs += floor($extra_pgs / 2);
997
998 /* Adjust if the first and second quarters intersect. */
999 } else if (($cur_pg - $q2_pgs - ceil($q2_pgs/3)) <= $q1_pgs) {
1000 $extra_pgs = $q2_pgs;
1001 $extra_pgs -= ceil(($cur_pg - $q1_pgs - 1) * 0.75);
1002 $q2_pgs = ceil(($cur_pg - $q1_pgs - 1) * 0.75);
1003 $q3_pgs += ceil($extra_pgs / 2);
1004 $q4_pgs += floor($extra_pgs / 2);
1005
1006 /* Adjust if the fourth quarter contains the current page. */
1007 } else if (($cur_pg + $q4_pgs) >= $tot_pgs) {
1008 $extra_pgs = ($q4_pgs - ($tot_pgs - $cur_pg)) + $q3_pgs;
1009 $q3_pgs = 0;
1010 $q4_pgs = $tot_pgs - $cur_pg;
1011 $q1_pgs += floor($extra_pgs / 2);
1012 $q2_pgs += ceil($extra_pgs / 2);
1013
1014 /* Adjust if the third and fourth quarter intersect. */
1015 } else if (($cur_pg + $q3_pgs + 1) >= ($tot_pgs - $q4_pgs + 1)) {
1016 $extra_pgs = $q3_pgs;
1017 $extra_pgs -= ceil(($tot_pgs - $cur_pg - $q4_pgs) * 0.75);
1018 $q3_pgs = ceil(($tot_pgs - $cur_pg - $q4_pgs) * 0.75);
1019 $q1_pgs += floor($extra_pgs / 2);
1020 $q2_pgs += ceil($extra_pgs / 2);
1021 }
1022 }
1023
1024 /*
1025 * I am leaving this debug code here, commented out, because
1026 * it is a really nice way to see what the above code is doing.
1027 * echo "qts = $q1_pgs/$q2_pgs/$q3_pgs/$q4_pgs = "
1028 * . ($q1_pgs + $q2_pgs + $q3_pgs + $q4_pgs) . '<br>';
1029 */
1030
1031 /* Print out the page links from the compute page quarters. */
1032
1033 /* Start with the first quarter. */
1034 if (($q1_pgs == 0) && ($cur_pg > 1)) {
1035 $pg_str .= "...$spc";
1036 } else {
1037 for ($pg = 1; $pg <= $q1_pgs; ++$pg) {
1038 $start = (($pg-1) * $show_num) + 1;
1039 $pg_str .= get_paginator_link($box, $start, $use, $pg) . $spc;
1040 }
1041 if ($cur_pg - $q2_pgs - $q1_pgs > 1) {
1042 $pg_str .= "...$spc";
1043 }
1044 }
1045
1046 /* Continue with the second quarter. */
1047 for ($pg = $cur_pg - $q2_pgs; $pg < $cur_pg; ++$pg) {
1048 $start = (($pg-1) * $show_num) + 1;
1049 $pg_str .= get_paginator_link($box, $start, $use, $pg) . $spc;
1050 }
1051
1052 /* Now print the current page. */
1053 $pg_str .= $cur_pg . $spc;
1054
1055 /* Next comes the third quarter. */
1056 for ($pg = $cur_pg + 1; $pg <= $cur_pg + $q3_pgs; ++$pg) {
1057 $start = (($pg-1) * $show_num) + 1;
1058 $pg_str .= get_paginator_link($box, $start, $use, $pg) . $spc;
1059 }
1060
1061 /* And last, print the forth quarter page links. */
1062 if (($q4_pgs == 0) && ($cur_pg < $tot_pgs)) {
1063 $pg_str .= "...$spc";
1064 } else {
1065 if (($tot_pgs - $q4_pgs) > ($cur_pg + $q3_pgs)) {
1066 $pg_str .= "...$spc";
1067 }
1068 for ($pg = $tot_pgs - $q4_pgs + 1; $pg <= $tot_pgs; ++$pg) {
1069 $start = (($pg-1) * $show_num) + 1;
1070 $pg_str .= get_paginator_link($box, $start, $use, $pg) . $spc;
1071 }
1072 }
1073 } else if ($PG_SHOWNUM == 999999) {
cf710efe 1074 $pg_str = "<A HREF=\"right_main.php?PG_SHOWALL=0"
1075 . "&amp;use_mailbox_cache=$use&amp;startMessage=1&amp;mailbox=$box\" "
1076 . "TARGET=\"right\">" ._("Paginate") . '</A>' . $spc;
6c930ade 1077 }
1078
1079 /* If necessary, compute the 'show all' string. */
1080 if (($prv_str != '') || ($nxt_str != '')) {
cf710efe 1081 $all_str = "<A HREF=\"right_main.php?PG_SHOWALL=1"
6c930ade 1082 . "&amp;use_mailbox_cache=$use&amp;startMessage=1&amp;mailbox=$box\" "
1083 . "TARGET=\"right\">" . _("Show All") . '</A>';
1084 }
1085
1086 /* Last but not least, get the value for the toggle all link. */
1087 $tgl_str = get_selectall_link($start_msg, $sort);
1088
1089 /* Put all the pieces of the paginator string together. */
1090 /**
1091 * Hairy code... But let's leave it like it is since I am not certain
1092 * a different approach would be any easier to read. ;)
1093 */
1094 $result = '';
1095 $result .= ($prv_str != '' ? $prv_str . $spc . $sep . $spc : '');
1096 $result .= ($nxt_str != '' ? $nxt_str . $spc . $sep . $spc : '');
1097 $result .= ($pg_str != '' ? $pg_str : '');
1098 $result .= ($all_str != '' ? $sep . $spc . $all_str . $spc : '');
1099 $result .= ($result != '' ? $sep . $spc . $tgl_str: $tgl_str);
1100
1101 /* If the resulting string is blank, return a non-breaking space. */
1102 if ($result == '') {
1103 $result = '&nbsp;';
1104 }
1105
1106 /* Return our final magical paginator string. */
1107 return ($result);
bdfb67f8 1108}
1109
1110function processSubject($subject) {
6c930ade 1111 /* Shouldn't ever happen -- caught too many times in the IMAP functions */
1112 if ($subject == '')
1113 return _("(no subject)");
1114
1115 if (strlen($subject) <= 55)
1116 return $subject;
1117
1118 $ent_strlen=strlen($subject);
1119 $trim_val=50;
1120 $ent_offset=0;
1121 /*
1122 * see if this is entities-encoded string
1123 * If so, Iterate through the whole string, find out
1124 * the real number of characters, and if more
1125 * than 55, substr with an updated trim value.
1126 */
1127 while ( (($ent_loc = strpos($subject, '&', $ent_offset)) !== false) &&
1128 (($ent_loc_end = strpos($subject, ';', $ent_loc)) !== false) ) {
bdfb67f8 1129 $trim_val += ($ent_loc_end-$ent_loc)+1;
1130 $ent_strlen -= $ent_loc_end-$ent_loc;
1131 $ent_offset = $ent_loc_end+1;
6c930ade 1132 }
bdfb67f8 1133
6c930ade 1134 if ($ent_strlen <= 55){
1135 return $subject;
1136 }
1137
1138 return substr($subject, 0, $trim_val) . '...';
bdfb67f8 1139}
f93c93b9 1140
a3439b27 1141function handleAsSent($mailbox) {
6c930ade 1142 global $sent_folder, $draft_folder, $handleAsSent_result;
a3439b27 1143
6c930ade 1144 /* First check if this is the sent or draft folder. */
1145 $handleAsSent_result = (($mailbox == $sent_folder)
1146 || ($mailbox == $draft_folder));
1147
1148 /* Then check the result of the handleAsSent hook. */
1149 do_hook('check_handleAsSent_result', $mailbox);
a3439b27 1150
6c930ade 1151 /* And return the result. */
1152 return ($handleAsSent_result);
a3439b27 1153}
1154
7d89b19d 1155?>