Continue development on 1.4.1!
[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, $td_str, $msg,
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 /*
356 * For some reason, on PHP 4.3+, this being unset, and set in the session causes havoc
357 * so setting it to an empty array beforehand seems to clean up the issue, and stopping the
358 * "Your script possibly relies on a session side-effect which existed until PHP 4.2.3" error
359 */
360
361 if (!isset($msort)) {
362 $msort = array();
363 }
364
365 if (!isset($msgs)) {
366 $msgs = array();
367 }
368
369 $start = microtime();
370 /* If autoexpunge is turned on, then do it now. */
371 $mbxresponse = sqimap_mailbox_select($imapConnection, $mailbox);
372 $srt = $sort;
373 /* If autoexpunge is turned on, then do it now. */
374 if ($auto_expunge == true) {
375 $exp_cnt = sqimap_mailbox_expunge($imapConnection, $mailbox, false, '');
376 $mbxresponse['EXISTS'] = $mbxresponse['EXISTS'] - $exp_cnt;
377 $num_msgs = $mbxresponse['EXISTS'];
378 }
379
380 if ($mbxresponse['EXISTS'] > 0) {
381 /* if $start_msg is lower than $num_msgs, we probably deleted all messages
382 * in the last page. We need to re-adjust the start_msg
383 */
384
385 if($start_msg > $num_msgs) {
386 $start_msg -= $show_num;
387 if($start_msg < 1) {
388 $start_msg = 1;
389 }
390 }
391
392 /* This code and the next if() block check for
393 * server-side sorting methods. The $id array is
394 * formatted and $sort is set to 6 to disable
395 * SM internal sorting
396 */
397
398 if ($thread_sort_messages == 1) {
399 $mode = 'thread';
400 } elseif ($allow_server_sort == 1) {
401 $mode = 'serversort';
402 } else {
403 $mode = '';
404 }
405
406 sqsession_unregister('msort');
407 sqsession_unregister('msgs');
408 switch ($mode) {
409 case 'thread':
410 $id = get_thread_sort($imapConnection);
411 $msgs = getServerMessages($imapConnection, $start_msg, $show_num, $num_msgs, $id);
412 if ($msgs === false) {
413 echo '<b><small><center><font color=red>' .
414 _("Thread sorting is not supported by your IMAP server.<br>Please report this to the system administrator.").
415 '</center></small></b>';
416 $thread_sort_messages = 0;
417 $msort = $msgs = array();
418 } else {
419 $msort= $msgs;
420 $sort = 6;
421 }
422 break;
423 case 'serversort':
424 $id = sqimap_get_sort_order($imapConnection, $sort, $mbxresponse);
425 $msgs = getServerMessages($imapConnection, $start_msg, $show_num, $num_msgs, $id);
426 if ($msgs === false) {
427 echo '<b><small><center><font color=red>' .
428 _( "Server-side sorting is not supported by your IMAP server.<br>Please report this to the system administrator.").
429 '</center></small></b>';
430 $sort = $server_sort_order;
431 $allow_server_sort = FALSE;
432 $msort = $msgs = array();
433 $id = array();
434 } else {
435 $msort = $msgs;
436 $sort = 6;
437 }
438 break;
439 default:
440 if (!$use_cache) {
441 $msgs = getSelfSortMessages($imapConnection, $start_msg, $show_num,
442 $num_msgs, $sort, $mbxresponse);
443 $msort = calc_msort($msgs, $sort);
444 } /* !use cache */
445 break;
446 } // switch
447 sqsession_register($msort, 'msort');
448 sqsession_register($msgs, 'msgs');
449 } /* if exists > 0 */
450
451 $res = getEndMessage($start_msg, $show_num, $num_msgs);
452 $start_msg = $res[0];
453 $end_msg = $res[1];
454
455 $paginator_str = get_paginator_str($mailbox, $start_msg, $end_msg,
456 $num_msgs, $show_num, $sort);
457
458 $msg_cnt_str = get_msgcnt_str($start_msg, $end_msg, $num_msgs);
459
460 do_hook('mailbox_index_before');
461 echo '<table border="0" width="100%" cellpadding="0" cellspacing="0">';
462 echo '<tr><td>';
463
464 mail_message_listing_beginning($imapConnection, $mailbox, $sort,
465 $msg_cnt_str, $paginator_str, $start_msg);
466 echo '</td></tr>';
467 /* line between the button area and the list */
468 echo '<tr><td HEIGHT="5" BGCOLOR="'.$color[4].'"></td></tr>';
469
470 echo '<tr><td>';
471 echo ' <table width="100%" cellpadding="1" cellspacing="0" align="center"'.' border="0" bgcolor="'.$color[9].'">';
472 echo ' <tr><td>';
473 echo ' <table width="100%" cellpadding="1" cellspacing="0" align="center" border="0" bgcolor="'.$color[5].'">';
474 echo '<tr><td>';
475 printHeader($mailbox, $srt, $color, !$thread_sort_messages);
476
477 displayMessageArray($imapConnection, $num_msgs, $start_msg,
478 $msort, $mailbox, $sort, $color, $show_num,0,0);
479 echo '</td></tr></table></td></tr></table>';
480
481 mail_message_listing_end($num_msgs, $paginator_str, $msg_cnt_str, $color);
482 echo '</td></tr></table>';
483 $t = elapsed($start);
484 //echo("elapsed time = $t seconds\n");
485 }
486
487 function calc_msort($msgs, $sort) {
488
489 /*
490 * 0 = Date (up)
491 * 1 = Date (dn)
492 * 2 = Name (up)
493 * 3 = Name (dn)
494 * 4 = Subject (up)
495 * 5 = Subject (dn)
496 */
497
498 if (($sort == 0) || ($sort == 1)) {
499 foreach ($msgs as $item) {
500 $msort[] = $item['TIME_STAMP'];
501 }
502 } elseif (($sort == 2) || ($sort == 3)) {
503 foreach ($msgs as $item) {
504 $msort[] = $item['FROM-SORT'];
505 }
506 } elseif (($sort == 4) || ($sort == 5)) {
507 foreach ($msgs as $item) {
508 $msort[] = $item['SUBJECT-SORT'];
509 }
510 } else {
511 $msort = $msgs;
512 }
513 if ($sort < 6) {
514 if ($sort % 2) {
515 asort($msort);
516 } else {
517 arsort($msort);
518 }
519 }
520 return $msort;
521 }
522
523 function fillMessageArray($imapConnection, $id, $count) {
524 return sqimap_get_small_header_list($imapConnection, $id);
525 }
526
527
528 /* Generic function to convert the msgs array into an HTML table. */
529 function displayMessageArray($imapConnection, $num_msgs, $start_msg,
530 $msort, $mailbox, $sort, $color,
531 $show_num, $where=0, $what=0) {
532 global $imapServerAddress, $use_mailbox_cache, $index_order,
533 $indent_array, $thread_sort_messages, $allow_server_sort,
534 $server_sort_order, $PHP_SELF;
535
536 $res = getEndMessage($start_msg, $show_num, $num_msgs);
537 $start_msg = $res[0];
538 $end_msg = $res[1];
539
540 $urlMailbox = urlencode($mailbox);
541
542 /* get indent level for subject display */
543 if ($thread_sort_messages == 1 && $num_msgs) {
544 $indent_array = get_parent_level($imapConnection);
545 }
546
547 $real_startMessage = $start_msg;
548 if ($sort == 6) {
549 if ($end_msg - $start_msg < $show_num - 1) {
550 $end_msg = $end_msg - $start_msg + 1;
551 $start_msg = 1;
552 } else if ($start_msg > $show_num) {
553 $end_msg = $show_num;
554 $start_msg = 1;
555 }
556 }
557 $endVar = $end_msg + 1;
558
559 /*
560 * Loop through and display the info for each message.
561 * ($t is used for the checkbox number)
562 */
563 $t = 0;
564
565 /* messages display */
566
567 if (!$num_msgs) {
568 /* if there's no messages in this folder */
569 echo html_tag( 'tr',
570 html_tag( 'td',
571 "<BR><b>" . _("THIS FOLDER IS EMPTY") . "</b><BR>&nbsp;",
572 'center',
573 $color[4],
574 'COLSPAN="' . count($index_order) . '"'
575 )
576 );
577 } elseif ($start_msg == $end_msg) {
578 /* if there's only one message in the box, handle it differently. */
579 if ($sort != 6) {
580 $i = $start_msg;
581 } else {
582 $i = 1;
583 }
584 reset($msort);
585 $k = 0;
586 do {
587 $key = key($msort);
588 next($msort);
589 $k++;
590 } while (isset ($key) && ($k < $i));
591 printMessageInfo($imapConnection, $t, true, $key, $mailbox,
592 $real_startMessage, $where, $what);
593 } else {
594 $i = $start_msg;
595 reset($msort);
596 $k = 0;
597 do {
598 $key = key($msort);
599 next($msort);
600 $k++;
601 } while (isset ($key) && ($k < $i));
602 $not_last = true;
603 do {
604 if (!$i || $i == $endVar-1) $not_last = false;
605 printMessageInfo($imapConnection, $t, $not_last, $key, $mailbox,
606 $real_startMessage, $where, $what);
607 $key = key($msort);
608 $t++;
609 $i++;
610 next($msort);
611 } while ($i && $i < $endVar);
612 }
613 }
614
615 /*
616 * Displays the standard message list header. To finish the table,
617 * you need to do a "</table></table>";
618 *
619 * $moveURL is the URL to submit the delete/move form to
620 * $mailbox is the current mailbox
621 * $sort is the current sorting method (-1 for no sorting available [searches])
622 * $Message is a message that is centered on top of the list
623 * $More is a second line that is left aligned
624 */
625
626 function mail_message_listing_beginning ($imapConnection,
627 $mailbox = '', $sort = -1,
628 $msg_cnt_str = '',
629 $paginator = '&nbsp;',
630 $start_msg = 1) {
631 global $color, $auto_expunge, $base_uri, $thread_sort_messages,
632 $allow_thread_sort, $allow_server_sort, $server_sort_order,
633 $PHP_SELF;
634
635 $php_self = $PHP_SELF;
636 /* fix for incorrect $PHP_SELF */
637 if (strpos($php_self, 'move_messages.php')) {
638 $php_self = str_replace('move_messages.php', 'right_main.php', $php_self);
639 }
640 $urlMailbox = urlencode($mailbox);
641
642 if (preg_match('/^(.+)\?.+$/',$php_self,$regs)) {
643 $source_url = $regs[1];
644 } else {
645 $source_url = $php_self;
646 }
647
648 if (!isset($msg)) {
649 $msg = '';
650 }
651 $moveFields = '<input type="hidden" name="msg" value="'.htmlspecialchars($msg).'">' .
652 '<input type="hidden" name="mailbox" value="'.htmlspecialchars($mailbox).'">' .
653 '<input type="hidden" name="startMessage" value="'.htmlspecialchars($start_msg).'">';
654
655 // $moveURL = "move_messages.php?msg=$msg&amp;mailbox=$urlMailbox"
656 // . "&amp;startMessage=$start_msg";
657 /*
658 * This is the beginning of the message list table.
659 * It wraps around all messages
660 */
661 echo '<form name="messageList" method="post" action="move_messages.php">' ."\n"
662 . $moveFields
663 . html_tag( 'table' ,
664 html_tag( 'tr',
665 html_tag( 'td' ,
666 html_tag( 'table' ,
667 html_tag( 'tr',
668 html_tag( 'td', $paginator, 'left' ) .
669 html_tag( 'td', $msg_cnt_str, 'right' )
670 )
671 , '', $color[4], 'border="0" width="100%" cellpadding="1" cellspacing="0"' )
672 , 'left', '', '' )
673 , '', $color[0] )
674 , '', '', 'border="0" width="100%" cellpadding="1" cellspacing="0"' );
675 /* line between header and button area */
676 echo '<tr><td HEIGHT="5" BGCOLOR="'.$color[4].'"></td></tr>';
677
678 echo '<tr><td>';
679 echo html_tag( 'tr' ) . "\n"
680 . html_tag( 'td' ,'' , 'left', '', '' )
681 . html_tag( 'table' ,'' , '', $color[9], 'border="0" width="100%" cellpadding="1" cellspacing="0"' )
682 . '<tr><td>'
683 . html_tag( 'table' ,'' , '', $color[0], 'border="0" width="100%" cellpadding="1" cellspacing="0"' )
684 . html_tag( 'tr',
685 getSmallStringCell(_("Move Selected To"), 'left', 'nowrap') .
686 getSmallStringCell(_("Transform Selected Messages"), 'right')
687 )
688 . html_tag( 'tr' ) ."\n"
689 . html_tag( 'td', '', 'left', '', 'valign="middle" nowrap' );
690 getMbxList($imapConnection);
691 echo getButton('SUBMIT', 'moveButton',_("Move")) . "\n";
692 echo getButton('SUBMIT', 'attache',_("Forward")) . "\n";
693
694 echo " </TD>\n"
695 . html_tag( 'td', '', 'right', '', 'nowrap' );
696
697
698
699 if (!$auto_expunge) {
700 echo getButton('SUBMIT', 'expungeButton',_("Expunge"))
701 .'&nbsp;' . _("mailbox") . "\n";
702 }
703
704 echo getButton('SUBMIT', 'markRead',_("Read"));
705 echo getButton('SUBMIT', 'markUnread',_("Unread"));
706 echo getButton('SUBMIT', 'delete',_("Delete")) ."&nbsp;\n";
707 if (!strpos($php_self,'mailbox')) {
708 $location = $php_self.'?mailbox=INBOX&amp;startMessage=1';
709 } else {
710 $location = $php_self;
711 }
712 echo '<INPUT TYPE="HIDDEN" NAME="location" VALUE="'.$location.'">';
713 echo "</TD>\n"
714 . " </TR>\n";
715
716 /* draws thread sorting links */
717 if ($allow_thread_sort == TRUE) {
718 if ($thread_sort_messages == 1 ) {
719 $set_thread = 2;
720 $thread_name = _("Unthread View");
721 } elseif ($thread_sort_messages == 0) {
722 $set_thread = 1;
723 $thread_name = _("Thread View");
724 }
725 echo html_tag( 'tr' ,
726 html_tag( 'td' ,
727 '&nbsp;<a href=' . $source_url . '?sort='
728 . "$sort" . '&start_messages=1&set_thread=' . "$set_thread"
729 . '&mailbox=' . urlencode($mailbox) . '><small>' . $thread_name
730 . '</a></small>&nbsp;'
731 , '', '', '' )
732 , '', '', '' );
733 }
734
735 echo "</TABLE></td></tr></table></td></tr>\n";
736
737 do_hook('mailbox_form_before');
738
739 /* if using server sort we highjack the
740 * the $sort var and use $server_sort_order
741 * instead. but here we reset sort for a bit
742 * since its easy
743 */
744 if ($allow_server_sort == TRUE) {
745 $sort = $server_sort_order;
746 }
747 }
748
749 function mail_message_listing_end($num_msgs, $paginator_str, $msg_cnt_str, $color) {
750 if ($num_msgs) {
751 /* space between list and footer */
752 echo '<tr><td HEIGHT="5" BGCOLOR="'.$color[4].'" COLSPAN="1">';
753
754 echo '</td></tr><tr><td>';
755 echo html_tag( 'table',
756 html_tag( 'tr',
757 html_tag( 'td',
758 html_tag( 'table',
759 html_tag( 'tr',
760 html_tag( 'td', $paginator_str ) .
761 html_tag( 'td', $msg_cnt_str, 'right' )
762 )
763 , '', $color[4], 'width="100%" border="0" cellpadding="1" cellspacing="0"' )
764 )
765 )
766 , '', $color[9], 'width="100%" border="0" cellpadding="1" cellspacing="0"' );
767 echo '</td></tr>';
768 }
769 /* End of message-list table */
770
771 do_hook('mailbox_index_after');
772 echo "</FORM>\n";
773 }
774
775 function printHeader($mailbox, $sort, $color, $showsort=true) {
776 global $index_order;
777 echo html_tag( 'tr' ,'' , 'center', $color[5] );
778
779 /* calculate the width of the subject column based on the
780 * widths of the other columns */
781 $widths = array(1=>1,2=>25,3=>5,4=>0,5=>1,6=>5);
782 $subjectwidth = 100;
783 foreach($index_order as $item) {
784 $subjectwidth -= $widths[$item];
785 }
786
787 foreach ($index_order as $item) {
788 switch ($item) {
789 case 1: /* checkbox */
790 case 5: /* flags */
791 echo html_tag( 'td' ,'&nbsp;' , '', '', 'width="1%"' );
792 break;
793 case 2: /* from */
794 if (handleAsSent($mailbox)) {
795 echo html_tag( 'td' ,'' , 'left', '', 'width="25%"' )
796 . '<b>' . _("To") . '</b>';
797 } else {
798 echo html_tag( 'td' ,'' , 'left', '', 'width="25%"' )
799 . '<b>' . _("From") . '</b>';
800 }
801 if ($showsort) {
802 ShowSortButton($sort, $mailbox, 2, 3);
803 }
804 echo "</td>\n";
805 break;
806 case 3: /* date */
807 echo html_tag( 'td' ,'' , 'left', '', 'width="5%" nowrap' )
808 . '<b>' . _("Date") . '</b>';
809 if ($showsort) {
810 ShowSortButton($sort, $mailbox, 0, 1);
811 }
812 echo "</td>\n";
813 break;
814 case 4: /* subject */
815 echo html_tag( 'td' ,'' , 'left', '', 'width="'.$subjectwidth.'%"' )
816 . '<b>' . _("Subject") . '</b>';
817 if ($showsort) {
818 ShowSortButton($sort, $mailbox, 4, 5);
819 }
820 echo "</td>\n";
821 break;
822 case 6: /* size */
823 echo html_tag( 'td', '<b>' . _("Size") . '</b>', 'center', '', 'width="5%" nowrap' );
824 break;
825 }
826 }
827 echo "</tr>\n";
828 }
829
830
831 /*
832 * This function shows the sort button. Isn't this a good comment?
833 */
834 function ShowSortButton($sort, $mailbox, $Up, $Down ) {
835 global $PHP_SELF;
836 /* Figure out which image we want to use. */
837 if ($sort != $Up && $sort != $Down) {
838 $img = 'sort_none.png';
839 $which = $Up;
840 } elseif ($sort == $Up) {
841 $img = 'up_pointer.png';
842 $which = $Down;
843 } else {
844 $img = 'down_pointer.png';
845 $which = 6;
846 }
847
848 if (preg_match('/^(.+)\?.+$/',$PHP_SELF,$regs)) {
849 $source_url = $regs[1];
850 } else {
851 $source_url = $PHP_SELF;
852 }
853
854 /* Now that we have everything figured out, show the actual button. */
855 echo ' <a href="' . $source_url .'?newsort=' . $which
856 . '&amp;startMessage=1&amp;mailbox=' . urlencode($mailbox)
857 . '"><IMG SRC="../images/' . $img
858 . '" BORDER=0 WIDTH=12 HEIGHT=10 ALT="sort"></a>';
859 }
860
861 function get_selectall_link($start_msg, $sort) {
862 global $checkall, $what, $where, $mailbox, $javascript_on;
863 global $PHP_SELF, $PG_SHOWNUM;
864
865 $result = '';
866 if ($javascript_on) {
867 $result = '<script language="JavaScript" type="text/javascript">'
868 . "\n<!-- \n"
869 . "function CheckAll() {\n"
870 . " for (var i = 0; i < document.messageList.elements.length; i++) {\n"
871 . " if(document.messageList.elements[i].type == 'checkbox'){\n"
872 . " document.messageList.elements[i].checked = "
873 . " !(document.messageList.elements[i].checked);\n"
874 . " }\n"
875 . " }\n"
876 . "}\n"
877 . "//-->\n"
878 . '</script><a href="#" onClick="CheckAll();">' . _("Toggle All")
879 . "</a>\n";
880 } else {
881 if (strpos($PHP_SELF, "?")) {
882 $result .= "<a href=\"$PHP_SELF&amp;mailbox=" . urlencode($mailbox)
883 . "&amp;startMessage=$start_msg&amp;sort=$sort&amp;checkall=";
884 } else {
885 $result .= "<a href=\"$PHP_SELF?mailbox=" . urlencode($mailbox)
886 . "&amp;startMessage=$start_msg&amp;sort=$sort&amp;checkall=";
887 }
888 if (isset($checkall) && $checkall == '1') {
889 $result .= '0';
890 } else {
891 $result .= '1';
892 }
893
894 if (isset($where) && isset($what)) {
895 $result .= '&amp;where=' . urlencode($where)
896 . '&amp;what=' . urlencode($what);
897 }
898 $result .= "\">";
899
900 if (isset($checkall) && ($checkall == '1')) {
901 $result .= _("Unselect All");
902 } else {
903 $result .= _("Select All");
904 }
905 $result .= "</A>\n";
906 }
907
908 /* Return our final result. */
909 return ($result);
910 }
911
912 /*
913 * This function computes the "Viewing Messages..." string.
914 */
915 function get_msgcnt_str($start_msg, $end_msg, $num_msgs) {
916 /* Compute the $msg_cnt_str. */
917 $result = '';
918 if ($start_msg < $end_msg) {
919 $result = sprintf(_("Viewing Messages: <B>%s</B> to <B>%s</B> (%s total)"),
920 $start_msg, $end_msg, $num_msgs);
921 } else if ($start_msg == $end_msg) {
922 $result = sprintf(_("Viewing Message: <B>%s</B> (1 total)"), $start_msg);
923 } else {
924 $result = '<br>';
925 }
926 /* Return our result string. */
927 return ($result);
928 }
929
930 /*
931 * Generate a paginator link.
932 */
933 function get_paginator_link($box, $start_msg, $use, $text) {
934 global $PHP_SELF;
935
936 $result = "<A HREF=\"right_main.php?use_mailbox_cache=$use"
937 . "&amp;startMessage=$start_msg&amp;mailbox=$box\" "
938 . "TARGET=\"right\">$text</A>";
939 return ($result);
940 /*
941 if (preg_match('/^(.+)\?.+$/',$PHP_SELF,$regs)) {
942 $source_url = $regs[1];
943 } else {
944 $source_url = $PHP_SELF;
945 }
946
947 $result = '<A HREF="'. $source_url . "?use_mailbox_cache=$use"
948 . "&amp;startMessage=$start_msg&amp;mailbox=$box\" "
949 . "TARGET=\"right\">$text</A>";
950 return ($result);
951 */
952 }
953
954 /*
955 * This function computes the paginator string.
956 */
957 function get_paginator_str($box, $start_msg, $end_msg, $num_msgs,
958 $show_num, $sort) {
959 global $username, $data_dir, $use_mailbox_cache, $color, $PG_SHOWNUM;
960
961 /* Initialize paginator string chunks. */
962 $prv_str = '';
963 $nxt_str = '';
964 $pg_str = '';
965 $all_str = '';
966 $tgl_str = '';
967
968 $box = urlencode($box);
969
970 /* Create simple strings that will be creating the paginator. */
971 $spc = '&nbsp;'; /* This will be used as a space. */
972 $sep = '|'; /* This will be used as a seperator. */
973
974 /* Get some paginator preference values. */
975 $pg_sel = getPref($data_dir, $username, 'page_selector', SMPREF_ON);
976 $pg_max = getPref($data_dir, $username, 'page_selector_max', PG_SEL_MAX);
977
978 /* Make sure that our start message number is not too big. */
979 $start_msg = min($start_msg, $num_msgs);
980
981 /* Decide whether or not we will use the mailbox cache. */
982 /* Not sure why $use_mailbox_cache is even passed in. */
983 if ($sort == 6) {
984 $use = 0;
985 } else {
986 $use = 1;
987 }
988
989 /* Compute the starting message of the previous and next page group. */
990 $next_grp = $start_msg + $show_num;
991 $prev_grp = $start_msg - $show_num;
992
993 /* Compute the basic previous and next strings. */
994 if (($next_grp <= $num_msgs) && ($prev_grp >= 0)) {
995 $prv_str = get_paginator_link($box, $prev_grp, $use, _("Previous"));
996 $nxt_str = get_paginator_link($box, $next_grp, $use, _("Next"));
997 } else if (($next_grp > $num_msgs) && ($prev_grp >= 0)) {
998 $prv_str = get_paginator_link($box, $prev_grp, $use, _("Previous"));
999 $nxt_str = "<FONT COLOR=\"$color[9]\">"._("Next")."</FONT>\n";
1000 } else if (($next_grp <= $num_msgs) && ($prev_grp < 0)) {
1001 $prv_str = "<FONT COLOR=\"$color[9]\">"._("Previous") . '</FONT>';
1002 $nxt_str = get_paginator_link($box, $next_grp, $use, _("Next"));
1003 }
1004
1005 /* Page selector block. Following code computes page links. */
1006 if ($pg_sel && ($num_msgs > $show_num)) {
1007 /* Most importantly, what is the current page!!! */
1008 $cur_pg = intval($start_msg / $show_num) + 1;
1009
1010 /* Compute total # of pages and # of paginator page links. */
1011 $tot_pgs = ceil($num_msgs / $show_num); /* Total number of Pages */
1012 $vis_pgs = min($pg_max, $tot_pgs - 1); /* Visible Pages */
1013
1014 /* Compute the size of the four quarters of the page links. */
1015
1016 /* If we can, just show all the pages. */
1017 if (($tot_pgs - 1) <= $pg_max) {
1018 $q1_pgs = $cur_pg - 1;
1019 $q2_pgs = $q3_pgs = 0;
1020 $q4_pgs = $tot_pgs - $cur_pg;
1021
1022 /* Otherwise, compute some magic to choose the four quarters. */
1023 } else {
1024 /*
1025 * Compute the magic base values. Added together,
1026 * these values will always equal to the $pag_pgs.
1027 * NOTE: These are DEFAULT values and do not take
1028 * the current page into account. That is below.
1029 */
1030 $q1_pgs = floor($vis_pgs/4);
1031 $q2_pgs = round($vis_pgs/4, 0);
1032 $q3_pgs = ceil($vis_pgs/4);
1033 $q4_pgs = round(($vis_pgs - $q2_pgs)/3, 0);
1034
1035 /* Adjust if the first quarter contains the current page. */
1036 if (($cur_pg - $q1_pgs) < 1) {
1037 $extra_pgs = ($q1_pgs - ($cur_pg - 1)) + $q2_pgs;
1038 $q1_pgs = $cur_pg - 1;
1039 $q2_pgs = 0;
1040 $q3_pgs += ceil($extra_pgs / 2);
1041 $q4_pgs += floor($extra_pgs / 2);
1042
1043 /* Adjust if the first and second quarters intersect. */
1044 } else if (($cur_pg - $q2_pgs - ceil($q2_pgs/3)) <= $q1_pgs) {
1045 $extra_pgs = $q2_pgs;
1046 $extra_pgs -= ceil(($cur_pg - $q1_pgs - 1) * 0.75);
1047 $q2_pgs = ceil(($cur_pg - $q1_pgs - 1) * 0.75);
1048 $q3_pgs += ceil($extra_pgs / 2);
1049 $q4_pgs += floor($extra_pgs / 2);
1050
1051 /* Adjust if the fourth quarter contains the current page. */
1052 } else if (($cur_pg + $q4_pgs) >= $tot_pgs) {
1053 $extra_pgs = ($q4_pgs - ($tot_pgs - $cur_pg)) + $q3_pgs;
1054 $q3_pgs = 0;
1055 $q4_pgs = $tot_pgs - $cur_pg;
1056 $q1_pgs += floor($extra_pgs / 2);
1057 $q2_pgs += ceil($extra_pgs / 2);
1058
1059 /* Adjust if the third and fourth quarter intersect. */
1060 } else if (($cur_pg + $q3_pgs + 1) >= ($tot_pgs - $q4_pgs + 1)) {
1061 $extra_pgs = $q3_pgs;
1062 $extra_pgs -= ceil(($tot_pgs - $cur_pg - $q4_pgs) * 0.75);
1063 $q3_pgs = ceil(($tot_pgs - $cur_pg - $q4_pgs) * 0.75);
1064 $q1_pgs += floor($extra_pgs / 2);
1065 $q2_pgs += ceil($extra_pgs / 2);
1066 }
1067 }
1068
1069 /*
1070 * I am leaving this debug code here, commented out, because
1071 * it is a really nice way to see what the above code is doing.
1072 * echo "qts = $q1_pgs/$q2_pgs/$q3_pgs/$q4_pgs = "
1073 * . ($q1_pgs + $q2_pgs + $q3_pgs + $q4_pgs) . '<br>';
1074 */
1075
1076 /* Print out the page links from the compute page quarters. */
1077
1078 /* Start with the first quarter. */
1079 if (($q1_pgs == 0) && ($cur_pg > 1)) {
1080 $pg_str .= "...$spc";
1081 } else {
1082 for ($pg = 1; $pg <= $q1_pgs; ++$pg) {
1083 $start = (($pg-1) * $show_num) + 1;
1084 $pg_str .= get_paginator_link($box, $start, $use, $pg) . $spc;
1085 }
1086 if ($cur_pg - $q2_pgs - $q1_pgs > 1) {
1087 $pg_str .= "...$spc";
1088 }
1089 }
1090
1091 /* Continue with the second quarter. */
1092 for ($pg = $cur_pg - $q2_pgs; $pg < $cur_pg; ++$pg) {
1093 $start = (($pg-1) * $show_num) + 1;
1094 $pg_str .= get_paginator_link($box, $start, $use, $pg) . $spc;
1095 }
1096
1097 /* Now print the current page. */
1098 $pg_str .= $cur_pg . $spc;
1099
1100 /* Next comes the third quarter. */
1101 for ($pg = $cur_pg + 1; $pg <= $cur_pg + $q3_pgs; ++$pg) {
1102 $start = (($pg-1) * $show_num) + 1;
1103 $pg_str .= get_paginator_link($box, $start, $use, $pg) . $spc;
1104 }
1105
1106 /* And last, print the forth quarter page links. */
1107 if (($q4_pgs == 0) && ($cur_pg < $tot_pgs)) {
1108 $pg_str .= "...$spc";
1109 } else {
1110 if (($tot_pgs - $q4_pgs) > ($cur_pg + $q3_pgs)) {
1111 $pg_str .= "...$spc";
1112 }
1113 for ($pg = $tot_pgs - $q4_pgs + 1; $pg <= $tot_pgs; ++$pg) {
1114 $start = (($pg-1) * $show_num) + 1;
1115 $pg_str .= get_paginator_link($box, $start, $use, $pg) . $spc;
1116 }
1117 }
1118 } else if ($PG_SHOWNUM == 999999) {
1119 $pg_str = "<A HREF=\"right_main.php?PG_SHOWALL=0"
1120 . "&amp;use_mailbox_cache=$use&amp;startMessage=1&amp;mailbox=$box\" "
1121 . "TARGET=\"right\">" ._("Paginate") . '</A>' . $spc;
1122 }
1123
1124 /* If necessary, compute the 'show all' string. */
1125 if (($prv_str != '') || ($nxt_str != '')) {
1126 $all_str = "<A HREF=\"right_main.php?PG_SHOWALL=1"
1127 . "&amp;use_mailbox_cache=$use&amp;startMessage=1&amp;mailbox=$box\" "
1128 . "TARGET=\"right\">" . _("Show All") . '</A>';
1129 }
1130
1131 /* Last but not least, get the value for the toggle all link. */
1132 $tgl_str = get_selectall_link($start_msg, $sort);
1133
1134 /* Put all the pieces of the paginator string together. */
1135 /**
1136 * Hairy code... But let's leave it like it is since I am not certain
1137 * a different approach would be any easier to read. ;)
1138 */
1139 $result = '';
1140 $result .= ($prv_str != '' ? $prv_str . $spc . $sep . $spc : '');
1141 $result .= ($nxt_str != '' ? $nxt_str . $spc . $sep . $spc : '');
1142 $result .= ($pg_str != '' ? $pg_str : '');
1143 $result .= ($all_str != '' ? $sep . $spc . $all_str . $spc : '');
1144 $result .= ($result != '' ? $sep . $spc . $tgl_str: $tgl_str);
1145
1146 /* If the resulting string is blank, return a non-breaking space. */
1147 if ($result == '') {
1148 $result = '&nbsp;';
1149 }
1150
1151 /* Return our final magical paginator string. */
1152 return ($result);
1153 }
1154
1155 function processSubject($subject, $threadlevel = 0) {
1156 global $languages, $squirrelmail_language;
1157 /* Shouldn't ever happen -- caught too many times in the IMAP functions */
1158 if ($subject == '')
1159 return _("(no subject)");
1160
1161 $trim_at = 55;
1162
1163 /* if this is threaded, subtract two chars per indentlevel */
1164 if($threadlevel > 0 && $threadlevel <= 10)
1165 $trim_at -= (2*$threadlevel);
1166
1167 if (strlen($subject) <= $trim_at)
1168 return $subject;
1169
1170 $ent_strlen = $orig_len = strlen($subject);
1171 $trim_val = $trim_at - 5;
1172 $ent_offset = 0;
1173 /*
1174 * see if this is entities-encoded string
1175 * If so, Iterate through the whole string, find out
1176 * the real number of characters, and if more
1177 * than 55, substr with an updated trim value.
1178 */
1179 $step = $ent_loc = 0;
1180 while ( $ent_loc < $trim_val && (($ent_loc = strpos($subject, '&', $ent_offset)) !== false) &&
1181 (($ent_loc_end = strpos($subject, ';', $ent_loc+3)) !== false) ) {
1182 $trim_val += ($ent_loc_end-$ent_loc);
1183 $ent_offset = $ent_loc_end+1;
1184 ++$step;
1185 }
1186
1187 if (($trim_val > 50) && (strlen($subject) > ($trim_val))&& (strpos($subject,';',$trim_val) < ($trim_val +6))) {
1188 $i = strpos($subject,';',$trim_val);
1189 if ($i) {
1190 $trim_val = strpos($subject,';',$trim_val);
1191 }
1192 }
1193 if ($ent_strlen <= $trim_at){
1194 return $subject;
1195 }
1196
1197 if (isset($languages[$squirrelmail_language]['XTRA_CODE']) &&
1198 function_exists($languages[$squirrelmail_language]['XTRA_CODE'])) {
1199 return $languages[$squirrelmail_language]['XTRA_CODE']('strimwidth', $subject, $trim_val);
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 echo sqimap_mailbox_option_list($imapConnection, array(strtolower($lastTargetMailbox)) );
1208 echo ' </SELECT></TT>&nbsp;';
1209 }
1210
1211 function getButton($type, $name, $value) {
1212 return '<INPUT TYPE="'.$type.'" NAME="'.$name.'" VALUE="'.$value . '">';
1213 }
1214
1215 function getSmallStringCell($string, $align) {
1216 return html_tag('td',
1217 '<small>' . $string . ':&nbsp; </small>',
1218 $align,
1219 '',
1220 'nowrap' );
1221 }
1222
1223 function getEndMessage($start_msg, $show_num, $num_msgs) {
1224 if ($start_msg + ($show_num - 1) < $num_msgs){
1225 $end_msg = $start_msg + ($show_num - 1);
1226 } else {
1227 $end_msg = $num_msgs;
1228 }
1229
1230 if ($end_msg < $start_msg) {
1231 $start_msg = $start_msg - $show_num;
1232 if ($start_msg < 1) {
1233 $start_msg = 1;
1234 }
1235 }
1236 return (array($start_msg,$end_msg));
1237 }
1238
1239 function handleAsSent($mailbox) {
1240 global $handleAsSent_result;
1241
1242 /* First check if this is the sent or draft folder. */
1243 $handleAsSent_result = isSentMailbox($mailbox) || isDraftMailbox($mailbox);
1244
1245 /* Then check the result of the handleAsSent hook. */
1246 do_hook('check_handleAsSent_result', $mailbox);
1247
1248 /* And return the result. */
1249 return $handleAsSent_result;
1250 }
1251
1252 ?>