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