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