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