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