9b55139ce092b9d664fa99e189ee308ba5b7d76a
[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(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 printMessageInfo($imapConnection, $t, $not_last=true, $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','','','','VALIGN="top"') . "\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 $high_val = strtolower($message_highlight_list_part['value']);
125 $match_type = strtoupper($message_highlight_list_part['match_type']);
126 if ($match_type == 'TO_CC') {
127 if (strstr('^^' . strtolower($msg['TO']), $high_val) ||
128 strstr('^^' . strtolower($msg['CC']), $high_val)) {
129 $hlt_color = $message_highlight_list_part['color'];
130 continue;
131 }
132 } else {
133 if (strstr('^^' . strtolower($msg[$match_type]), $high_val)) {
134 $hlt_color = $message_highlight_list_part['color'];
135 continue;
136 }
137 }
138 }
139 }
140 }
141
142 if (!isset($hlt_color)) {
143 $hlt_color = $color_string;
144 }
145 $checked = ($checkall == 1) ? ' CHECKED' : '';
146 $col = 0;
147 if (sizeof($index_order)) {
148 foreach ($index_order as $index_order_part) {
149 switch ($index_order_part) {
150 case 1: /* checkbox */
151 echo html_tag( 'td',
152 "<input type=checkbox name=\"msg[$t]\" value=\"".$msg['ID']."\"$checked>",
153 'center',
154 $hlt_color );
155 break;
156 case 2: /* from */
157 echo html_tag( 'td',
158 $italic . $bold . $flag . $fontstr . $senderName .
159 $fontstr_end . $flag_end . $bold_end . $italic_end,
160 'left',
161 $hlt_color );
162 break;
163 case 3: /* date */
164 $date_string = $msg['DATE_STRING'] . '';
165 if ($date_string == '') {
166 $date_string = _("Unknown date");
167 }
168 echo html_tag( 'td',
169 $bold . $flag . $fontstr . $date_string .
170 $fontstr_end . $flag_end . $bold_end,
171 'center',
172 $hlt_color,
173 'nowrap' );
174 break;
175 case 4: /* subject */
176 $td_str = $bold;
177 if ($thread_sort_messages == 1) {
178 if (isset($indent_array[$msg["ID"]])) {
179 $td_str .= str_repeat("&nbsp;&nbsp;&nbsp;&nbsp;",$indent_array[$msg['ID']]);
180 }
181 }
182 $td_str .= '<a href="read_body.php?mailbox='.$urlMailbox
183 . '&amp;passed_id='. $msg["ID"]
184 . '&amp;startMessage='.$start_msg.$searchstr.'"';
185 do_hook("subject_link");
186 if ($subject != $msg['SUBJECT']) {
187 $title = get_html_translation_table(HTML_SPECIALCHARS);
188 $title = array_flip($title);
189 $title = strtr($msg['SUBJECT'], $title);
190 $title = str_replace('"', "''", $title);
191 $td_str .= " title=\"$title\"";
192 }
193 $td_str .= ">$flag$subject$flag_end</a>$bold_end";
194 echo html_tag( 'td', $td_str, 'left', $hlt_color );
195 break;
196 case 5: /* flags */
197 $stuff = false;
198 $td_str = "<b><small>";
199
200 if (isset($msg['FLAG_ANSWERED']) && $msg['FLAG_ANSWERED'] == true) {
201 $td_str .= _("A");
202 $stuff = true;
203 }
204 if ($msg['TYPE0'] == 'multipart') {
205 $td_str .= '+';
206 $stuff = true;
207 }
208 if ($default_use_priority) {
209 if ( ($msg['PRIORITY'] == 1) || ($msg['PRIORITY'] == 2) ) {
210 $td_str .= "<font color=\"$color[1]\">!</font>";
211 $stuff = true;
212 }
213 if ($msg['PRIORITY'] == 5) {
214 $td_str .= "<font color=\"$color[8]\">?</font>";
215 $stuff = true;
216 }
217 }
218 if (isset($msg['FLAG_DELETED']) && $msg['FLAG_DELETED'] == true) {
219 $td_str .= "<font color=\"$color[1]\">D</font>";
220 $stuff = true;
221 }
222 if (!$stuff) {
223 $td_str .= '&nbsp;';
224 }
225 $td_str .= '</small></b>';
226 echo html_tag( 'td',
227 $td_str,
228 'center',
229 $hlt_color,
230 'nowrap' );
231 break;
232 case 6: /* size */
233 echo html_tag( 'td',
234 $bold . $fontstr . show_readable_size($msg['SIZE']) .
235 $fontstr_end . $bold_end,
236 'right',
237 $hlt_color );
238 break;
239 }
240 ++$col;
241 }
242 }
243 if ($not_last) {
244 echo '</tr>' . "\n" . '<tr><td COLSPAN="' . $col . '" BGCOLOR="' .
245 $color[0] . '" HEIGHT="1"></td></tr>' . "\n";
246 } else {
247 echo '</tr>'."\n";
248 }
249 }
250
251 function getServerMessages($imapConnection, $start_msg, $show_num, $num_msgs, $id) {
252 if ($id != 'no') {
253 if ($start_msg + ($show_num - 1) < $num_msgs) {
254 $end_msg = $start_msg + ($show_num-1);
255 } else {
256 $end_msg = $num_msgs;
257 }
258 $id = array_slice($id, ($start_msg-1), ($end_msg));
259
260 $end = $start_msg + $show_num - 1;
261 if ($num_msgs < $show_num) {
262 $end_loop = $num_msgs;
263 } else if ($end > $num_msgs) {
264 $end_loop = $num_msgs - $start_msg + 1;
265 } else {
266 $end_loop = $show_num;
267 }
268 return fillMessageArray($imapConnection,$id,$end_loop);
269 } else {
270 return false;
271 }
272 }
273
274 function getThreadMessages($imapConnection, $start_msg, $show_num, $num_msgs) {
275 $id = get_thread_sort($imapConnection);
276 return getServerMessages($imapConnection, $start_msg, $show_num, $num_msgs, $id);
277 }
278
279 function getServerSortMessages($imapConnection, $start_msg, $show_num,
280 $num_msgs, $server_sort_order, $mbxresponse) {
281 $id = sqimap_get_sort_order($imapConnection, $server_sort_order,$mbxresponse);
282 return getServerMessages($imapConnection, $start_msg, $show_num, $num_msgs, $id);
283 }
284
285 function getSelfSortMessages($imapConnection, $start_msg, $show_num,
286 $num_msgs, $sort, $mbxresponse) {
287 $msgs = array();
288 if ($num_msgs >= 1) {
289 $id = sqimap_get_php_sort_order ($imapConnection, $mbxresponse);
290 if ($sort < 6 ) {
291 $end = $num_msgs;
292 $end_loop = $end;
293 } else {
294 /* if it's not sorted */
295 if ($start_msg + ($show_num - 1) < $num_msgs) {
296 $end_msg = $start_msg + ($show_num - 1);
297 } else {
298 $end_msg = $num_msgs;
299 }
300 if ($end_msg < $start_msg) {
301 $start_msg = $start_msg - $show_num;
302 if ($start_msg < 1) {
303 $start_msg = 1;
304 }
305 }
306 $id = array_slice(array_reverse($id), ($start_msg-1), ($end_msg));
307 $end = $start_msg + $show_num - 1;
308 if ($num_msgs < $show_num) {
309 $end_loop = $num_msgs;
310 } else if ($end > $num_msgs) {
311 $end_loop = $num_msgs - $start_msg + 1;
312 } else {
313 $end_loop = $show_num;
314 }
315 }
316 $msgs = fillMessageArray($imapConnection,$id,$end_loop);
317 }
318 return $msgs;
319 }
320
321
322
323 /*
324 * This function loops through a group of messages in the mailbox
325 * and shows them to the user.
326 */
327 function showMessagesForMailbox($imapConnection, $mailbox, $num_msgs,
328 $start_msg, $sort, $color, $show_num,
329 $use_cache, $mode='') {
330 global $msgs, $msort, $auto_expunge, $thread_sort_messages,
331 $allow_server_sort, $server_sort_order;
332
333 /* If autoexpunge is turned on, then do it now. */
334 $mbxresponse = sqimap_mailbox_select($imapConnection, $mailbox);
335 $srt = $sort;
336 /* If autoexpunge is turned on, then do it now. */
337 if ($auto_expunge == true) {
338 $exp_cnt = sqimap_mailbox_expunge($imapConnection, $mailbox, false, '');
339 $mbxresponse['EXISTS'] = $mbxresponse['EXISTS'] - $exp_cnt;
340 $num_msgs = $mbxresponse['EXISTS'];
341 }
342
343 if ($mbxresponse['EXISTS'] > 0) {
344 /* if $start_msg is lower than $num_msgs, we probably deleted all messages
345 * in the last page. We need to re-adjust the start_msg
346 */
347
348 if($start_msg > $num_msgs) {
349 $start_msg -= $show_num;
350 if($start_msg < 1) {
351 $start_msg = 1;
352 }
353 }
354
355 /* This code and the next if() block check for
356 * server-side sorting methods. The $id array is
357 * formatted and $sort is set to 6 to disable
358 * SM internal sorting
359 */
360
361 if ($thread_sort_messages == 1) {
362 $mode = 'thread';
363 } elseif ($allow_server_sort == 1) {
364 $mode = 'serversort';
365 } else {
366 $mode = '';
367 }
368
369 sqsession_unregister('msort');
370 sqsession_unregister('msgs');
371 switch ($mode) {
372 case 'thread':
373 $id = get_thread_sort($imapConnection);
374 $msgs = getServerMessages($imapConnection, $start_msg, $show_num, $num_msgs, $id);
375 if ($msgs === false) {
376 echo '<b><small><center><font color=red>' .
377 _("Thread sorting is not supported by your IMAP server.<br>Please report this to the system administrator.").
378 '</center></small></b>';
379 $thread_sort_messages = 0;
380 $msort = $msgs = array();
381 } else {
382 $msort= $msgs;
383 $sort = 6;
384 }
385 break;
386 case 'serversort':
387 $id = sqimap_get_sort_order($imapConnection, $sort, $mbxresponse);
388 $msgs = getServerMessages($imapConnection, $start_msg, $show_num, $num_msgs, $id);
389 if ($msgs === false) {
390 echo '<b><small><center><font color=red>' .
391 _( "Server-side sorting is not supported by your IMAP server.<br>Please report this to the system administrator.").
392 '</center></small></b>';
393 $sort = $server_sort_order;
394 $allow_server_sort = FALSE;
395 $msort = $msgs = array();
396 $id = array();
397 } else {
398 $msort = $msgs;
399 $sort = 6;
400 }
401 break;
402 default:
403 if (!$use_cache) {
404 $msgs = getSelfSortMessages($imapConnection, $start_msg, $show_num,
405 $num_msgs, $sort, $mbxresponse);
406 $msort = calc_msort($msgs, $sort);
407 } /* !use cache */
408 break;
409 } // switch
410 sqsession_register($msort, 'msort');
411 sqsession_register($msgs, 'msgs');
412 } /* if exists > 0 */
413
414 $res = getEndMessage($start_msg, $show_num, $num_msgs);
415 $start_msg = $res[0];
416 $end_msg = $res[1];
417
418 $paginator_str = get_paginator_str($mailbox, $start_msg, $end_msg,
419 $num_msgs, $show_num, $sort);
420
421 $msg_cnt_str = get_msgcnt_str($start_msg, $end_msg, $num_msgs);
422
423 do_hook('mailbox_index_before');
424 echo '<table border="0" width="100%" cellpadding="0" cellspacing="0">';
425 echo '<tr><td>';
426
427 mail_message_listing_beginning($imapConnection, $mailbox, $sort,
428 $msg_cnt_str, $paginator_str, $start_msg);
429 echo '</td></tr>';
430 /* line between the button area and the list */
431 echo '<tr><td HEIGHT="5" BGCOLOR="'.$color[4].'"></td></tr>';
432
433 echo '<tr><td>';
434 echo ' <table width="100%" cellpadding="1" cellspacing="0" align="center"'.' border="0" bgcolor="'.$color[9].'">';
435 echo ' <tr><td>';
436 echo ' <table width="100%" cellpadding="1" cellspacing="0" align="center" border="0" bgcolor="'.$color[5].'">';
437 echo '<tr><td>';
438 printHeader($mailbox, $srt, $color, !$thread_sort_messages);
439
440 displayMessageArray($imapConnection, $num_msgs, $start_msg,
441 $msort, $mailbox, $sort, $color, $show_num,0,0);
442 echo '</td></tr></table></td></tr></table>';
443
444 mail_message_listing_end($num_msgs, $paginator_str, $msg_cnt_str, $color);
445 echo '</td></tr></table>';
446 }
447
448 function calc_msort($msgs, $sort) {
449
450 /*
451 * 0 = Date (up)
452 * 1 = Date (dn)
453 * 2 = Name (up)
454 * 3 = Name (dn)
455 * 4 = Subject (up)
456 * 5 = Subject (dn)
457 */
458 if (($sort == 0) || ($sort == 1)) {
459 $msort = array_cleave ($msgs, 'TIME_STAMP');
460 } elseif (($sort == 2) || ($sort == 3)) {
461 $msort = array_cleave ($msgs, 'FROM-SORT');
462 } elseif (($sort == 4) || ($sort == 5)) {
463 $msort = array_cleave ($msgs, 'SUBJECT-SORT');
464 } else {
465 $msort = $msgs;
466 }
467 if ($sort < 6) {
468 if ($sort % 2) {
469 asort($msort);
470 } else {
471 arsort($msort);
472 }
473 }
474 return $msort;
475 }
476
477 function fillMessageArray($imapConnection, $id, $count) {
478 $msgs_list = sqimap_get_small_header_list($imapConnection, $id);
479 $messages = array();
480 if (sizeof($msgs_list)) {
481 foreach ($msgs_list as $hdr) {
482 $unique_id[] = $hdr->uid;
483 $from[] = $hdr->from;
484 $date[] = $hdr->date;
485 $subject[] = $hdr->subject;
486 $to[] = $hdr->to;
487 $priority[] = $hdr->priority;
488 $cc[] = $hdr->cc;
489 $size[] = $hdr->size;
490 $type[] = $hdr->type0;
491 $flag_deleted[] = $hdr->flag_deleted;
492 $flag_answered[] = $hdr->flag_answered;
493 $flag_seen[] = $hdr->flag_seen;
494 $flag_flagged[] = $hdr->flag_flagged;
495 }
496 }
497
498 for($j = 0; $j < $count; ++$j) {
499 if (isset($date[$j])) {
500 $date[$j] = str_replace(' ', ' ', $date[$j]);
501 $tmpdate = explode(' ', trim($date[$j]));
502 } else {
503 $tmpdate = $date = array('', '', '', '', '', '');
504 }
505 $messages[$j]['TIME_STAMP'] = getTimeStamp($tmpdate);
506 $messages[$j]['DATE_STRING'] =
507 getDateString($messages[$j]['TIME_STAMP']);
508 $messages[$j]['ID'] = $unique_id[$j];
509 $messages[$j]['FROM'] = decodeHeader($from[$j]);
510 $messages[$j]['FROM-SORT'] =
511 strtolower(sqimap_find_displayable_name(decodeHeader($from[$j])));
512 $messages[$j]['SUBJECT'] = decodeHeader($subject[$j]);
513 $messages[$j]['SUBJECT-SORT'] = strtolower(decodeHeader($subject[$j]));
514 $messages[$j]['TO'] = decodeHeader($to[$j]);
515 $messages[$j]['PRIORITY'] = $priority[$j];
516 $messages[$j]['CC'] = $cc[$j];
517 $messages[$j]['SIZE'] = $size[$j];
518 $messages[$j]['TYPE0'] = $type[$j];
519 $messages[$j]['FLAG_DELETED'] = $flag_deleted[$j];
520 $messages[$j]['FLAG_ANSWERED'] = $flag_answered[$j];
521 $messages[$j]['FLAG_SEEN'] = $flag_seen[$j];
522 $messages[$j]['FLAG_FLAGGED'] = $flag_flagged[$j];
523
524 /*
525 * fix SUBJECT-SORT to remove Re:
526 * vedr|sv (Danish)
527 * re|aw (English)
528 *
529 * TODO: i18n should be incorporated here. E.g. we catch the ones
530 * we know about, but also define in i18n what the localized
531 * "Re: " is for this or that locale.
532 */
533 if (preg_match("/^(vedr|sv|re|aw):\s*(.*)$/si",
534 $messages[$j]['SUBJECT-SORT'], $matches)){
535 $messages[$j]['SUBJECT-SORT'] = $matches[2];
536 }
537 }
538 return $messages;
539 }
540
541
542 /* Generic function to convert the msgs array into an HTML table. */
543 function displayMessageArray($imapConnection, $num_msgs, $start_msg,
544 $msort, $mailbox, $sort, $color,
545 $show_num, $where=0, $what=0) {
546 global $imapServerAddress, $use_mailbox_cache, $index_order,
547 $indent_array, $thread_sort_messages, $allow_server_sort,
548 $server_sort_order, $PHP_SELF;
549
550 $res = getEndMessage($start_msg, $show_num, $num_msgs);
551 $start_msg = $res[0];
552 $end_msg = $res[1];
553
554 $urlMailbox = urlencode($mailbox);
555
556 /* get indent level for subject display */
557 if ($thread_sort_messages == 1 ) {
558 $indent_array = get_parent_level($imapConnection);
559 }
560
561 $real_startMessage = $start_msg;
562 if ($sort == 6) {
563 if ($end_msg - $start_msg < $show_num - 1) {
564 $end_msg = $end_msg - $start_msg + 1;
565 $start_msg = 1;
566 } else if ($start_msg > $show_num) {
567 $end_msg = $show_num;
568 $start_msg = 1;
569 }
570 }
571 $endVar = $end_msg + 1;
572
573 /*
574 * Loop through and display the info for each message.
575 * ($t is used for the checkbox number)
576 */
577 $t = 0;
578
579 /* messages display */
580
581 if ($num_msgs == 0) {
582 /* if there's no messages in this folder */
583 echo html_tag( 'tr',
584 html_tag( 'td',
585 "<BR><b>" . _("THIS FOLDER IS EMPTY") . "</b><BR>&nbsp;",
586 'center',
587 $color[4],
588 'COLSPAN="' . count($index_order) . '"'
589 )
590 );
591 } elseif ($start_msg == $end_msg) {
592 /* if there's only one message in the box, handle it differently. */
593 if ($sort != 6) {
594 $i = $start_msg;
595 } else {
596 $i = 1;
597 }
598 reset($msort);
599 $k = 0;
600 do {
601 $key = key($msort);
602 next($msort);
603 $k++;
604 } while (isset ($key) && ($k < $i));
605 printMessageInfo($imapConnection, $t, true, $key, $mailbox,
606 $real_startMessage, $where, $what);
607 } else {
608 $i = $start_msg;
609 reset($msort);
610 $k = 0;
611 do {
612 $key = key($msort);
613 next($msort);
614 $k++;
615 } while (isset ($key) && ($k < $i));
616 $not_last = true;
617 do {
618 if (!$i || $i == $endVar-1) $not_last = false;
619 printMessageInfo($imapConnection, $t, $not_last, $key, $mailbox,
620 $real_startMessage, $where, $what);
621 $key = key($msort);
622 $t++;
623 $i++;
624 next($msort);
625 } while ($i && $i < $endVar);
626 }
627 }
628
629 /*
630 * Displays the standard message list header. To finish the table,
631 * you need to do a "</table></table>";
632 *
633 * $moveURL is the URL to submit the delete/move form to
634 * $mailbox is the current mailbox
635 * $sort is the current sorting method (-1 for no sorting available [searches])
636 * $Message is a message that is centered on top of the list
637 * $More is a second line that is left aligned
638 */
639
640 function mail_message_listing_beginning ($imapConnection,
641 $mailbox = '', $sort = -1,
642 $msg_cnt_str = '',
643 $paginator = '&nbsp;',
644 $start_msg = 1) {
645 global $color, $auto_expunge, $base_uri, $thread_sort_messages,
646 $allow_thread_sort, $allow_server_sort, $server_sort_order,
647 $PHP_SELF;
648
649 $php_self = $PHP_SELF;
650 /* fix for incorrect $PHP_SELF */
651 if (strpos($php_self, 'move_messages.php')) {
652 $php_self = str_replace('move_messages.php', 'right_main.php', $php_self);
653 }
654 $urlMailbox = urlencode($mailbox);
655
656 if (preg_match('/^(.+)\?.+$/',$php_self,$regs)) {
657 $source_url = $regs[1];
658 } else {
659 $source_url = $php_self;
660 }
661
662 if (!isset($msg)) {
663 $msg = '';
664 }
665 $moveURL = "move_messages.php?msg=$msg&amp;mailbox=$urlMailbox"
666 . "&amp;startMessage=$start_msg";
667 /*
668 * This is the beginning of the message list table.
669 * It wraps around all messages
670 */
671 echo "<FORM name=\"messageList\" method=post action=\"$moveURL\">\n"
672 . html_tag( 'table' ,
673 html_tag( 'tr',
674 html_tag( 'td' ,
675 html_tag( 'table' ,
676 html_tag( 'tr',
677 html_tag( 'td', $paginator, 'left' ) .
678 html_tag( 'td', $msg_cnt_str, 'right' )
679 )
680 , '', $color[4], 'border="0" width="100%" cellpadding="1" cellspacing="0"' )
681 , 'left', '', '' )
682 , '', $color[0] )
683 , '', '', 'border="0" width="100%" cellpadding="1" cellspacing="0"' );
684 /* line between header and button area */
685 echo '<tr><td HEIGHT="5" BGCOLOR="'.$color[4].'"></td></tr>';
686
687 echo '<tr><td>';
688 echo html_tag( 'tr' ) . "\n"
689 . html_tag( 'td' ,'' , 'left', '', '' )
690 . html_tag( 'table' ,'' , '', $color[9], 'border="0" width="100%" cellpadding="1" cellspacing="0"' )
691 . '<tr><td>'
692 . html_tag( 'table' ,'' , '', $color[0], 'border="0" width="100%" cellpadding="1" cellspacing="0"' )
693 . html_tag( 'tr',
694 getSmallStringCell(_("Move Selected To"), 'left', 'nowrap') .
695 getSmallStringCell(_("Transform Selected Messages"), 'right')
696 )
697 . html_tag( 'tr' ) ."\n"
698 . html_tag( 'td', '', 'left', '', 'valign="middle" nowrap' );
699 getMbxList($imapConnection);
700 echo getButton('SUBMIT', 'moveButton',_("Move")) . "\n";
701 echo getButton('SUBMIT', 'attache',_("Forward")) . "\n";
702
703 echo " </TD>\n"
704 . html_tag( 'td', '', 'right', '', 'nowrap' );
705
706
707
708 if (!$auto_expunge) {
709 echo getButton('SUBMIT', 'expungeButton',_("Expunge"))
710 .'&nbsp;' . _("mailbox") . "\n";
711 }
712
713 echo getButton('SUBMIT', 'markRead',_("Read"));
714 echo getButton('SUBMIT', 'markUnread',_("Unread"));
715 echo getButton('SUBMIT', 'delete',_("Delete")) ."&nbsp\n";
716 if (!strpos($php_self,'mailbox')) {
717 $location = $php_self.'?mailbox=INBOX&amp;startMessage=1';
718 } else {
719 $location = $php_self;
720 }
721 echo '<INPUT TYPE="HIDDEN" NAME="location" VALUE="'.$location.'">';
722 echo "</TD>\n"
723 . " </TR>\n";
724
725 /* draws thread sorting links */
726 if ($allow_thread_sort == TRUE) {
727 if ($thread_sort_messages == 1 ) {
728 $set_thread = 2;
729 $thread_name = _("Unthread View");
730 } elseif ($thread_sort_messages == 0) {
731 $set_thread = 1;
732 $thread_name = _("Thread View");
733 }
734 echo html_tag( 'tr' ,
735 html_tag( 'td' ,
736 '&nbsp;<a href=' . $source_url . '?sort='
737 . "$sort" . '&start_messages=1&set_thread=' . "$set_thread"
738 . '&mailbox=' . urlencode($mailbox) . '><small>' . $thread_name
739 . '</a></small>&nbsp;'
740 , '', '', '' )
741 , '', '', '' );
742 }
743
744 echo "</TABLE></td></tr></table></td></tr>\n";
745
746 do_hook('mailbox_form_before');
747
748 /* if using server sort we highjack the
749 * the $sort var and use $server_sort_order
750 * instead. but here we reset sort for a bit
751 * since its easy
752 */
753 if ($allow_server_sort == TRUE) {
754 $sort = $server_sort_order;
755 }
756 }
757
758 function mail_message_listing_end($num_msgs, $paginator_str, $msg_cnt_str, $color) {
759 if ($num_msgs) {
760 /* space between list and footer */
761 echo '<tr><td HEIGHT="5" BGCOLOR="'.$color[4].'" COLSPAN="1">';
762
763 echo '</td></tr><tr><td>';
764 echo html_tag( 'table',
765 html_tag( 'tr',
766 html_tag( 'td',
767 html_tag( 'table',
768 html_tag( 'tr',
769 html_tag( 'td', $paginator_str ) .
770 html_tag( 'td', $msg_cnt_str, 'right' )
771 )
772 , '', $color[4], 'width="100%" border="0" cellpadding="1" cellspacing="0"' )
773 )
774 )
775 , '', $color[9], 'width="100%" border="0" cellpadding="1" cellspacing="0"' );
776 echo '</td></tr>';
777 }
778 /* End of message-list table */
779
780 do_hook('mailbox_index_after');
781 echo "</FORM>\n";
782 }
783
784 function printHeader($mailbox, $sort, $color, $showsort=true) {
785 global $index_order;
786 echo html_tag( 'tr' ,'' , 'center', $color[5] );
787 for ($i = 1; $i <= count($index_order); $i++) {
788 switch ($index_order[$i]) {
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', '', '' )
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%"' );
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) {
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 if (strlen($subject) <= 55)
1162 return $subject;
1163
1164 $ent_strlen = strlen($subject);
1165 $trim_val=50;
1166 $ent_offset=0;
1167 /*
1168 * see if this is entities-encoded string
1169 * If so, Iterate through the whole string, find out
1170 * the real number of characters, and if more
1171 * than 55, substr with an updated trim value.
1172 */
1173 while ( (($ent_loc = strpos($subject, '&', $ent_offset)) !== false) &&
1174 (($ent_loc_end = strpos($subject, ';', $ent_loc)) !== false) ) {
1175 $trim_val += ($ent_loc_end-$ent_loc)+1;
1176 $ent_strlen -= $ent_loc_end-$ent_loc;
1177 $ent_offset = $ent_loc_end+1;
1178 }
1179
1180 if ($ent_strlen <= 55){
1181 return $subject;
1182 }
1183
1184 if (isset($languages[$squirrelmail_language]['XTRA_CODE']) &&
1185 function_exists($languages[$squirrelmail_language]['XTRA_CODE'])) {
1186 return $languages[$squirrelmail_language]['XTRA_CODE']('strimwidth', $subject, $trim_val);
1187 }
1188
1189 return substr($subject, 0, $trim_val) . '...';
1190 }
1191
1192 function getMbxList($imapConnection) {
1193 global $lastTargetMailbox;
1194 echo ' <small>&nbsp;<tt><select name="targetMailbox">';
1195 echo sqimap_mailbox_option_list($imapConnection, array(strtolower($lastTargetMailbox)) );
1196 echo ' </SELECT></TT>&nbsp;';
1197 }
1198
1199 function getButton($type, $name, $value) {
1200 return '<INPUT TYPE="'.$type.'" NAME="'.$name.'" VALUE="'.$value . '">';
1201 }
1202
1203 function getSmallStringCell($string, $align) {
1204 return html_tag('td',
1205 '<small>' . $string . ':&nbsp; </small>',
1206 $align,
1207 '',
1208 'nowrap' );
1209 }
1210
1211 function getEndMessage($start_msg, $show_num, $num_msgs) {
1212 if ($start_msg + ($show_num - 1) < $num_msgs){
1213 $end_msg = $start_msg + ($show_num - 1);
1214 } else {
1215 $end_msg = $num_msgs;
1216 }
1217
1218 if ($end_msg < $start_msg) {
1219 $start_msg = $start_msg - $show_num;
1220 if ($start_msg < 1) {
1221 $start_msg = 1;
1222 }
1223 }
1224 return (array($start_msg,$end_msg));
1225 }
1226
1227 function handleAsSent($mailbox) {
1228 global $sent_folder, $draft_folder, $handleAsSent_result;
1229
1230 /* First check if this is the sent or draft folder. */
1231 $handleAsSent_result = (($mailbox == $sent_folder)
1232 || ($mailbox == $draft_folder));
1233
1234 /* Then check the result of the handleAsSent hook. */
1235 do_hook('check_handleAsSent_result', $mailbox);
1236
1237 /* And return the result. */
1238 return ($handleAsSent_result);
1239 }
1240 ?>