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