Fix for #500910 Bug (display wrong address in INBOX.SENT).
[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 define('PG_SEL_MAX', 10); /* Default value for page_selector_max. */
16
17 function printMessageInfo($imapConnection, $t, $i, $key, $mailbox, $sort, $start_msg, $where, $what) {
18 global $checkall;
19 global $color, $msgs, $msort;
20 global $sent_folder, $draft_folder;
21 global $default_use_priority;
22 global $message_highlight_list;
23 global $index_order;
24 global $pos; /* Search postion (if any) */
25
26 $color_string = $color[4];
27 if ($GLOBALS['alt_index_colors']) {
28 if (!isset($GLOBALS["row_count"])) {
29 $GLOBALS["row_count"] = 0;
30 }
31 $GLOBALS["row_count"]++;
32 if ($GLOBALS["row_count"] % 2) {
33 if (!isset($color[12])) $color[12] = '#EAEAEA';
34 $color_string = $color[12];
35 }
36 }
37 $msg = $msgs[$key];
38
39 /**
40 * This is done in case you're looking into Sent folders,
41 * because you can have multi receiver.
42 */
43 $sendersName = split(',', $msg['FROM']);
44 for( $index = 0 ; $index < count($sendersName) ; $index++ ) {
45 if( strlen($senderName) > 0 )
46 $senderName .= ', ';
47 $senderName .= sqimap_find_displayable_name($sendersName[$index]);
48 }
49
50 if( $mailbox == 'None' ) {
51 // $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
52 $boxes = sqimap_mailbox_list($imapConnection);
53 // sqimap_logout($imapConnection);
54 $mailbox = $boxes[0]['unformatted'];
55 unset( $boxes );
56 }
57 $urlMailbox = urlencode($mailbox);
58 $subject = processSubject($msg['SUBJECT']);
59 echo "<TR>\n";
60
61 if (isset($msg['FLAG_FLAGGED']) && ($msg['FLAG_FLAGGED'] == true)) {
62 $flag = "<font color=\"$color[2]\">";
63 $flag_end = '</font>';
64 } else {
65 $flag = '';
66 $flag_end = '';
67 }
68 if (!isset($msg['FLAG_SEEN']) || ($msg['FLAG_SEEN'] == false)) {
69 $bold = '<b>';
70 $bold_end = '</b>';
71 } else {
72 $bold = '';
73 $bold_end = '';
74 }
75
76 if (handleAsSent($mailbox)) {
77 $italic = '<i>';
78 $italic_end = '</i>';
79 } else {
80 $italic = '';
81 $italic_end = '';
82 }
83
84 if (isset($msg['FLAG_DELETED']) && $msg['FLAG_DELETED']) {
85 $fontstr = "<font color=\"$color[9]\">";
86 $fontstr_end = '</font>';
87 } else {
88 $fontstr = '';
89 $fontstr_end = '';
90 }
91
92 for ($i=0; $i < count($message_highlight_list); $i++) {
93 if (trim($message_highlight_list[$i]['value']) != '') {
94 if ($message_highlight_list[$i]['match_type'] == 'to_cc') {
95 if (strpos('^^'.strtolower($msg['TO']), strtolower($message_highlight_list[$i]['value'])) || strpos('^^'.strtolower($msg['CC']), strtolower($message_highlight_list[$i]['value']))) {
96 $hlt_color = $message_highlight_list[$i]['color'];
97 continue;
98 }
99 } else if (strpos('^^'.strtolower($msg[strtoupper($message_highlight_list[$i]['match_type'])]),strtolower($message_highlight_list[$i]['value']))) {
100 $hlt_color = $message_highlight_list[$i]['color'];
101 continue;
102 }
103 }
104 }
105
106 if (!isset($hlt_color)) {
107 $hlt_color = $color_string;
108 }
109
110 if ($where && $what) {
111 if( !isset( $pos ) || $pos == '' ) {
112 $pos = '0';
113 }
114 $search_stuff = "&pos=" . urlencode( $pos ) . "&where=".urlencode($where).'&what='.urlencode($what);
115 }
116
117 $checked = ($checkall == 1 ?' checked' : '');
118
119 for ($i=1; $i <= count($index_order); $i++) {
120 switch ($index_order[$i]) {
121 case 1: /* checkbox */
122 echo " <td bgcolor=\"$hlt_color\" align=center><input type=checkbox name=\"msg[$t]\" value=".$msg["ID"]."$checked></TD>\n";
123 break;
124 case 2: /* from */
125 echo " <td bgcolor=\"$hlt_color\">$italic$bold$flag$fontstr$senderName$fontstr_end$flag_end$bold_end$italic_end</td>\n";
126 break;
127 case 3: /* date */
128 echo " <td nowrap bgcolor=\"$hlt_color\"><center>$bold$flag$fontstr".$msg["DATE_STRING"]."$fontstr_end$flag_end$bold_end</center></td>\n";
129 break;
130 case 4: /* subject */
131 echo " <td bgcolor=\"$hlt_color\">$bold";
132 if (! isset($search_stuff)) { $search_stuff = ''; }
133 echo "<a href=\"read_body.php?mailbox=$urlMailbox&passed_id=".$msg["ID"]."&startMessage=$start_msg&show_more=0$search_stuff\"";
134 do_hook("subject_link");
135
136 if ($subject != $msg['SUBJECT']) {
137 $title = get_html_translation_table(HTML_SPECIALCHARS);
138 $title = array_flip($title);
139 $title = strtr($msg['SUBJECT'], $title);
140 $title = str_replace('"', "''", $title);
141 echo " title=\"$title\"";
142 }
143 echo ">$flag$subject$flag_end</a>$bold_end</td>\n";
144 break;
145 case 5: /* flags */
146 $stuff = false;
147 echo " <td bgcolor=\"$hlt_color\" align=center nowrap><b><small>\n";
148 if (isset($msg['FLAG_ANSWERED']) &&
149 $msg['FLAG_ANSWERED'] == true) {
150 echo "A\n";
151 $stuff = true;
152 }
153 if ($msg['TYPE0'] == 'multipart') {
154 echo "+\n";
155 $stuff = true;
156 }
157 if ($default_use_priority) {
158 if (ereg('(1|2)',substr($msg['PRIORITY'],0,1))) {
159 echo "<font color=\"$color[1]\">!</font>\n";
160 $stuff = true;
161 }
162 if (ereg('(5)',substr($msg['PRIORITY'],0,1))) {
163 echo "<font color=\"$color[8]\">?</font>\n";
164 $stuff = true;
165 }
166 }
167 if (isset($msg['FLAG_DELETED']) && $msg['FLAG_DELETED']) {
168 echo "<font color=\"$color[1]\">D</font>\n";
169 $stuff = true;
170 }
171
172 if (!$stuff) {
173 echo "&nbsp;\n";
174 }
175 echo "</small></b></td>\n";
176 break;
177 case 6: /* size */
178 echo " <td bgcolor=\"$hlt_color\">$bold$fontstr" .
179 show_readable_size($msg['SIZE']) .
180 "$fontstr_end$bold_end</td>\n";
181 break;
182 }
183 }
184 echo "</tr>\n";
185 }
186
187 /**
188 * This function loops through a group of messages in the mailbox
189 * and shows them to the user.
190 */
191 function showMessagesForMailbox
192 ($imapConnection, $mailbox, $num_msgs, $start_msg, $sort,
193 $color, $show_num, $use_cache) {
194 global $msgs, $msort;
195 global $sent_folder, $draft_folder;
196 global $message_highlight_list;
197 global $auto_expunge;
198
199 /* If autoexpunge is turned on, then do it now. */
200 if ($auto_expunge == true) {
201 sqimap_mailbox_expunge($imapConnection, $mailbox, false);
202 }
203 sqimap_mailbox_select($imapConnection, $mailbox);
204
205 $issent = handleAsSent($mailbox);
206 if (!$use_cache) {
207 /* If it is sorted... */
208 if ($num_msgs >= 1) {
209 if ($sort < 6) {
210 $id = range(1, $num_msgs);
211 } else {
212 // if it's not sorted
213 if ($start_msg + ($show_num - 1) < $num_msgs) {
214 $end_msg = $start_msg + ($show_num-1);
215 } else {
216 $end_msg = $num_msgs;
217 }
218
219 if ($end_msg < $start_msg) {
220 $start_msg = $start_msg - $show_num;
221 if ($start_msg < 1) {
222 $start_msg = 1;
223 }
224 }
225
226 $real_startMessage = $num_msgs - $start_msg + 1;
227 $real_endMessage = $num_msgs - $start_msg - $show_num + 2;
228 if ($real_endMessage <= 0) {
229 $real_endMessage = 1;
230 }
231 $id = array_reverse(range($real_endMessage, $real_startMessage));
232 }
233
234 $msgs_list = sqimap_get_small_header_list($imapConnection, $id, $issent);
235 $flags = sqimap_get_flags_list($imapConnection, $id, $issent);
236 foreach ($msgs_list as $hdr) {
237 $from[] = $hdr->from;
238 $date[] = $hdr->date;
239 $subject[] = $hdr->subject;
240 $to[] = $hdr->to;
241 $priority[] = $hdr->priority;
242 $cc[] = $hdr->cc;
243 $size[] = $hdr->size;
244 $type[] = $hdr->type0;
245 }
246 }
247
248 $j = 0;
249 if ($sort == 6) {
250 $end = $start_msg + $show_num - 1;
251 if ($num_msgs < $show_num) {
252 $end_loop = $num_msgs;
253 } else if ($end > $num_msgs) {
254 $end_loop = $num_msgs - $start_msg + 1;
255 } else {
256 $end_loop = $show_num;
257 }
258 } else {
259 $end = $num_msgs;
260 $end_loop = $end;
261 }
262
263 while ($j < $end_loop) {
264 if (isset($date[$j])) {
265 $date[$j] = ereg_replace(' ', ' ', $date[$j]);
266 $tmpdate = explode(' ', trim($date[$j]));
267 } else {
268 $tmpdate = $date = array('', '', '', '', '', '');
269 }
270
271 $messages[$j]['TIME_STAMP'] = getTimeStamp($tmpdate);
272 $messages[$j]['DATE_STRING'] = getDateString($messages[$j]['TIME_STAMP']);
273 $messages[$j]['ID'] = $id[$j];
274 $messages[$j]['FROM'] = decodeHeader($from[$j]);
275 $messages[$j]['FROM-SORT'] = strtolower(sqimap_find_displayable_name(decodeHeader($from[$j])));
276 $messages[$j]['SUBJECT'] = decodeHeader($subject[$j]);
277 $messages[$j]['SUBJECT-SORT'] = strtolower(decodeHeader($subject[$j]));
278 $messages[$j]['TO'] = decodeHeader($to[$j]);
279 $messages[$j]['PRIORITY'] = $priority[$j];
280 $messages[$j]['CC'] = $cc[$j];
281 $messages[$j]['SIZE'] = $size[$j];
282 $messages[$j]['TYPE0'] = $type[$j];
283
284 # fix SUBJECT-SORT to remove Re:
285 $re_abbr = /* Add more here! */
286 'vedr|sv|' . /* Danish */
287 're|aw'; /* English */
288
289 if (eregi( "^($re_abbr):[ ]*(.*)$", $messages[$j]['SUBJECT-SORT'], $regs)) {
290 $messages[$j]['SUBJECT-SORT'] = $regs[2];
291 }
292
293 $num = 0;
294 while ($num < count($flags[$j])) {
295 if ($flags[$j][$num] == 'Deleted') {
296 $messages[$j]['FLAG_DELETED'] = true;
297 } else if ($flags[$j][$num] == 'Answered') {
298 $messages[$j]['FLAG_ANSWERED'] = true;
299 } else if ($flags[$j][$num] == 'Seen') {
300 $messages[$j]['FLAG_SEEN'] = true;
301 } else if ($flags[$j][$num] == 'Flagged') {
302 $messages[$j]['FLAG_FLAGGED'] = true;
303 }
304 $num++;
305 }
306 $j++;
307 }
308
309 /* Only ignore messages flagged as deleted if we are using a
310 * trash folder or auto_expunge */
311 if (((isset($move_to_trash) && $move_to_trash)
312 || (isset($auto_expunge) && $auto_expunge)) && $sort != 6) {
313
314 /** Find and remove the ones that are deleted */
315 $i = 0;
316 $j = 0;
317
318 while ($j < $num_msgs) {
319 if (isset($messages[$j]['FLAG_DELETED']) && $messages[$j]['FLAG_DELETED'] == true) {
320 $j++;
321 continue;
322 }
323 $msgs[$i] = $messages[$j];
324
325 $i++;
326 $j++;
327 }
328 $num_msgs = $i;
329 } else {
330 if (!isset($messages)) {
331 $messages = array();
332 }
333 $msgs = $messages;
334 }
335 }
336
337 // There's gotta be messages in the array for it to sort them.
338 if ($num_msgs > 0 && ! $use_cache) {
339 /** 0 = Date (up) 4 = Subject (up)
340 ** 1 = Date (dn) 5 = Subject (dn)
341 ** 2 = Name (up)
342 ** 3 = Name (dn)
343 **/
344 session_unregister("msgs");
345 if (($sort == 0) || ($sort == 1))
346 $msort = array_cleave ($msgs, 'TIME_STAMP');
347 elseif (($sort == 2) || ($sort == 3))
348 $msort = array_cleave ($msgs, 'FROM-SORT');
349 elseif (($sort == 4) || ($sort == 5))
350 $msort = array_cleave ($msgs, 'SUBJECT-SORT');
351 else // ($sort == 6)
352 $msort = $msgs;
353
354 if ($sort < 6) {
355 if ($sort % 2) {
356 asort($msort);
357 } else {
358 arsort($msort);
359 }
360 }
361 session_register('msort');
362 }
363 displayMessageArray($imapConnection, $num_msgs, $start_msg, $msgs, $msort, $mailbox, $sort, $color,$show_num);
364 session_register('msgs');
365 }
366
367 /**
368 * Generic function to convert the msgs array into an HTML table.
369 */
370 function displayMessageArray($imapConnection, $num_msgs, $start_msg, &$msgs, $msort, $mailbox, $sort, $color, $show_num) {
371 global $folder_prefix, $sent_folder;
372 global $imapServerAddress, $data_dir, $username, $use_mailbox_cache;
373 global $index_order, $real_endMessage, $real_startMessage, $checkall;
374
375 /* If cache isn't already set, do it now. */
376 if (!session_is_registered('msgs')) { session_register('msgs'); }
377 if (!session_is_registered('msort')) { session_register('msort'); }
378
379 if ($start_msg + ($show_num - 1) < $num_msgs) {
380 $end_msg = $start_msg + ($show_num-1);
381 } else {
382 $end_msg = $num_msgs;
383 }
384
385 if ($end_msg < $start_msg) {
386 $start_msg = $start_msg - $show_num;
387 if ($start_msg < 1) { $start_msg = 1; }
388 }
389
390 $urlMailbox = urlencode($mailbox);
391
392 do_hook('mailbox_index_before');
393
394 $msg_cnt_str = get_msgcnt_str($start_msg, $end_msg, $num_msgs);
395 $paginator_str = get_paginator_str
396 ($urlMailbox, $start_msg, $end_msg, $num_msgs, $show_num, $sort);
397
398 if (! isset($msg)) {
399 $msg = '';
400 }
401
402 mail_message_listing_beginning( $imapConnection,
403 "move_messages.php?msg=$msg&mailbox=$urlMailbox&startMessage=$start_msg",
404 $mailbox, $sort, $msg_cnt_str, $paginator_str, $start_msg);
405
406 $groupNum = $start_msg % ($show_num - 1);
407 $real_startMessage = $start_msg;
408 if ($sort == 6) {
409 if ($end_msg - $start_msg < $show_num - 1) {
410 $end_msg = $end_msg - $start_msg + 1;
411 $start_msg = 1;
412 } else if ($start_msg > $show_num) {
413 $end_msg = $show_num;
414 $start_msg = 1;
415 }
416 }
417 $endVar = $end_msg + 1;
418
419 /* Loop through and display the info for each message. */
420 $t = 0; // $t is used for the checkbox number
421 if ($num_msgs == 0) { // if there's no messages in this folder
422 echo "<TR><TD BGCOLOR=\"$color[4]\" COLSPAN=" . count($index_order) . ">\n".
423 " <CENTER><BR><B>". _("THIS FOLDER IS EMPTY") ."</B><BR>&nbsp;</CENTER>\n".
424 "</TD></TR>";
425 } elseif ($start_msg == $end_msg) {
426 /* If there's only one message in the box, handle it differently. */
427 if ($sort != 6) {
428 $i = $start_msg;
429 } else {
430 $i = 1;
431 }
432
433 reset($msort);
434 $k = 0;
435 do {
436 $key = key($msort);
437 next($msort);
438 $k++;
439 } while (isset ($key) && ($k < $i));
440 printMessageInfo($imapConnection, $t, $i, $key, $mailbox, $sort, $real_startMessage, 0, 0);
441 } else {
442 $i = $start_msg;
443
444 reset($msort);
445 $k = 0;
446 do {
447 $key = key($msort);
448 next($msort);
449 $k++;
450 } while (isset ($key) && ($k < $i));
451
452 do {
453 printMessageInfo($imapConnection, $t, $i, $key, $mailbox, $sort, $real_startMessage, 0, 0);
454 $key = key($msort);
455 $t++;
456 $i++;
457 next($msort);
458 } while ($i && $i < $endVar);
459 }
460
461 echo '</table>'.
462 "<table bgcolor=\"$color[9]\" width=\"100%\" border=0 cellpadding=1 cellspacing=1>" .
463 "<tr BGCOLOR=\"$color[4]\"><td>" .
464 "<table width=\"100%\" BGCOLOR=\"$color[4]\" border=0 cellpadding=1 cellspacing=0><tr><td>$paginator_str</td>".
465 "<td align=right>$msg_cnt_str</td></tr></table>".
466 "</td></tr>".
467 "</table>";
468 /** End of message-list table */
469
470 do_hook('mailbox_index_after');
471 echo "</TABLE></FORM>\n";
472 }
473
474 /**
475 * Displays the standard message list header. To finish the table,
476 * you need to do a "</table></table>";
477 *
478 * $moveURL is the URL to submit the delete/move form to
479 * $mailbox is the current mailbox
480 * $sort is the current sorting method (-1 for no sorting available [searches])
481 * $Message is a message that is centered on top of the list
482 * $More is a second line that is left aligned
483 */
484 function mail_message_listing_beginning
485 ($imapConnection, $moveURL, $mailbox = '', $sort = -1,
486 $msg_cnt_str = '', $paginator = '&nbsp;', $start_msg = 1) {
487 global $color, $index_order, $auto_expunge, $move_to_trash;
488 global $checkall, $sent_folder, $draft_folder;
489 $urlMailbox = urlencode($mailbox);
490
491 /*
492 * This is the beginning of the message list table.
493 * It wraps around all messages
494 */
495 echo "<FORM name=messageList method=post action=\"$moveURL\">\n"
496 . "<TABLE WIDTH=\"100%\" BORDER=\"0\" CELLPADDING=\"1\" CELLSPACING=\"0\">\n"
497 . "<TR BGCOLOR=\"$color[0]\"><TD>"
498 . " <TABLE BGCOLOR=\"$color[4]\" width=\"100%\" CELLPADDING=\"2\" CELLSPACING=\"0\" BORDER=\"0\"><TR>\n"
499 . " <TD ALIGN=LEFT>$paginator</TD>\n"
500 . " <TD ALIGN=RIGHT>$msg_cnt_str</TD>\n"
501 . " </TR></TABLE>\n"
502 . '</TD></TR>'
503 . "<TR><TD BGCOLOR=\"$color[0]\">\n"
504 . "<TABLE BGCOLOR=\"$color[0]\" COLS=2 BORDER=0 cellpadding=0 cellspacing=0 width=\"100%\">\n"
505 . " <TR>\n"
506 . " <TD ALIGN=LEFT VALIGN=MIDDLE NOWRAP>\n"
507 . ' <SMALL>&nbsp;' . _("Move Selected To:") . "</SMALL>\n"
508 . " </TD>\n"
509 . " <TD ALIGN=RIGHT NOWRAP>\n"
510 . ' <SMALL>&nbsp;' . _("Transform Selected Messages") . ": &nbsp; </SMALL><BR>\n"
511 . " </TD>\n"
512 . " </TR>\n"
513 . " <TR>\n"
514 . " <TD ALIGN=LEFT VALIGN=MIDDLE NOWRAP>\n"
515 . ' <SMALL>&nbsp;<TT><SELECT NAME="targetMailbox">';
516
517 $boxes = sqimap_mailbox_list($imapConnection);
518 for ($i = 0; $i < count($boxes); $i++) {
519 if (!in_array("noselect", $boxes[$i]['flags'])) {
520 $box = $boxes[$i]['unformatted'];
521 $box2 = str_replace(' ', '&nbsp;', $boxes[$i]['unformatted-disp']);
522 echo " <OPTION VALUE=\"$box\">$box2</option>\n";
523 }
524 }
525 echo ' </SELECT></TT>&nbsp;'.
526 "<INPUT TYPE=SUBMIT NAME=\"moveButton\" VALUE=\"" . _("Move") . "\"></SMALL>\n".
527 " </TD>\n".
528 " <TD ALIGN=RIGHT NOWRAP>&nbsp;&nbsp;&nbsp;\n";
529 if (!$auto_expunge) {
530 echo ' <INPUT TYPE=SUBMIT NAME="expungeButton" VALUE="'. _("Expunge") .'">&nbsp;'. _("mailbox") ."&nbsp;\n";
531 }
532 echo " <INPUT TYPE=SUBMIT NAME=\"markRead\" VALUE=\"". _("Read")."\">\n".
533 " <INPUT TYPE=SUBMIT NAME=\"markUnread\" VALUE=\"". _("Unread")."\">\n".
534 " <INPUT TYPE=SUBMIT VALUE=\"". _("Delete") . "\">&nbsp;\n".
535 " </TD>\n".
536 " </TR>\n".
537 "</TABLE>\n";
538 do_hook('mailbox_form_before');
539 echo '</TD></TR>'.
540
541 "<TR><TD BGCOLOR=\"$color[0]\">".
542 "<TABLE WIDTH=\"100%\" BORDER=0 CELLPADDING=2 CELLSPACING=";
543 if ($GLOBALS['alt_index_colors']) {
544 echo "0";
545 } else {
546 echo "1";
547 }
548 echo " BGCOLOR=\"$color[0]\">".
549 "<TR BGCOLOR=\"$color[5]\" ALIGN=\"center\">";
550
551 /* Print the headers. */
552 for ($i=1; $i <= count($index_order); $i++) {
553 switch ($index_order[$i]) {
554 case 1: /* checkbox */
555 case 5: /* flags */
556 echo ' <TD WIDTH="1%"><B>&nbsp;</B></TD>';
557 break;
558
559 case 2: /* from */
560 if (handleAsSent($mailbox)) {
561 echo ' <TD WIDTH="25%"><B>'. _("To") .'</B>';
562 } else {
563 echo ' <TD WIDTH="25%"><B>'. _("From") .'</B>';
564 }
565
566 ShowSortButton($sort, $mailbox, 2, 3);
567 echo "</TD>\n";
568 break;
569
570 case 3: /* date */
571 echo ' <TD NOWRAP WIDTH="5%"><B>'. _("Date") .'</B>';
572 ShowSortButton($sort, $mailbox, 0, 1);
573 echo "</TD>\n";
574 break;
575
576 case 4: /* subject */
577 echo ' <TD><B>'. _("Subject") .'</B> ';
578 ShowSortButton($sort, $mailbox, 4, 5);
579 echo "</TD>\n";
580 break;
581
582 case 6: /* size */
583 echo ' <TD WIDTH="5%"><b>' . _("Size")."</b></TD>\n";
584 break;
585 }
586 }
587 echo "</TR>\n";
588 }
589
590 /*
591 * This function shows the sort button. Isn't this a good comment?
592 */
593 function ShowSortButton($sort, $mailbox, $Up, $Down) {
594 /* Figure out which image we want to use. */
595 if ($sort != $Up && $sort != $Down) {
596 $img = 'sort_none.png';
597 $which = $Up;
598 } elseif ($sort == $Up) {
599 $img = 'up_pointer.png';
600 $which = $Down;
601 } else {
602 $img = 'down_pointer.png';
603 $which = 6;
604 }
605
606 /* Now that we have everything figured out, show the actual button. */
607 echo ' <a href="right_main.php?newsort=' . $which .
608 '&startMessage=1&mailbox=' . urlencode($mailbox) .
609 '"><IMG SRC="../images/' . $img .
610 '" BORDER=0 WIDTH=12 HEIGHT=10></a>';
611 }
612
613 function get_selectall_link($start_msg, $sort) {
614 global $checkall, $what, $where, $mailbox, $javascript_on;
615 global $PHP_SELF, $PG_SHOWNUM;
616
617 if ($javascript_on) {
618 $result =
619 '<script language="JavaScript">' .
620 "\n<!-- \n" .
621 "function CheckAll() {\n" .
622 " for (var i = 0; i < document.messageList.elements.length; i++) {\n" .
623 " if( document.messageList.elements[i].type == 'checkbox' ) {\n" .
624 " document.messageList.elements[i].checked = !(document.messageList.elements[i].checked);\n".
625 " }\n" .
626 " }\n" .
627 "}\n" .
628 "//-->\n" .
629 '</script><a href="#" onClick="CheckAll();">' . _("Toggle All") . "</a>\n";
630 } else {
631 $result .= "<a href=\"$PHP_SELF?mailbox=" . urlencode($mailbox)
632 . "&startMessage=$start_msg&sort=$sort&checkall=";
633 if (isset($checkall) && $checkall == '1') {
634 $result .= '0';
635 } else {
636 $result .= '1';
637 }
638
639 if (isset($where) && isset($what)) {
640 $result .= '&where=' . urlencode($where)
641 . '&what=' . urlencode($what);
642 }
643
644 if ($PG_SHOWNUM == 999999) {
645 $result .= '&PG_SHOWNUM=999999';
646 }
647
648 $result .= "\">";
649
650 if (isset($checkall) && ($checkall == '1')) {
651 $result .= _("Unselect All");
652 } else {
653 $result .= _("Select All");
654 }
655
656 $result .= "</A>\n";
657 }
658
659 /* Return our final result. */
660 return ($result);
661 }
662
663 /**
664 * This function computes the "Viewing Messages..." string.
665 */
666 function get_msgcnt_str($start_msg, $end_msg, $num_msgs) {
667 /* Compute the $msg_cnt_str. */
668 $result = '';
669 if ($start_msg < $end_msg) {
670 $result = sprintf(_("Viewing Messages: <B>%s</B> to <B>%s</B> (%s total)"), $start_msg, $end_msg, $num_msgs);
671 } else if ($start_msg == $end_msg) {
672 $result = sprintf(_("Viewing Message: <B>%s</B> (1 total)"), $start_msg);
673 } else {
674 $result = '<br>';
675 }
676
677 /* Return our result string. */
678 return ($result);
679 }
680
681 /**
682 * Generate a paginator link.
683 */
684 function get_paginator_link
685 ($box, $start_msg, $use, $text) {
686 $result = "<A HREF=\"right_main.php?use_mailbox_cache=$use"
687 . "&startMessage=$start_msg&mailbox=$box\" "
688 . "TARGET=\"right\">$text</A>";
689 return ($result);
690 }
691
692 /**
693 * This function computes the paginator string.
694 */
695 function get_paginator_str
696 ($box, $start_msg, $end_msg, $num_msgs, $show_num, $sort) {
697 global $username, $data_dir, $use_mailbox_cache, $color, $PG_SHOWNUM;
698
699 /* Initialize paginator string chunks. */
700 $prv_str = '';
701 $nxt_str = '';
702 $pg_str = '';
703 $all_str = '';
704 $tgl_str = '';
705
706 /* Create simple strings that will be creating the paginator. */
707 $spc = '&nbsp;'; /* This will be used as a space. */
708 $sep = '|'; /* This will be used as a seperator. */
709
710 /* Get some paginator preference values. */
711 $pg_sel = getPref($data_dir, $username, 'page_selector', SMPREF_ON);
712 $pg_max = getPref($data_dir, $username, 'page_selector_max', PG_SEL_MAX);
713
714 /* Make sure that our start message number is not too big. */
715 $start_msg = min($start_msg, $num_msgs);
716
717 /* Decide whether or not we will use the mailbox cache. */
718 /* Not sure why $use_mailbox_cache is even passed in. */
719 if ($sort == 6) {
720 $use = 0;
721 } else {
722 $use = 1;
723 }
724
725 /* Compute the starting message of the previous and next page group. */
726 $next_grp = $start_msg + $show_num;
727 $prev_grp = $start_msg - $show_num;
728
729 /* Compute the basic previous and next strings. */
730 if (($next_grp <= $num_msgs) && ($prev_grp >= 0)) {
731 $prv_str = get_paginator_link($box, $prev_grp, $use, _("Previous"));
732 $nxt_str = get_paginator_link($box, $next_grp, $use, _("Next"));
733 } else if (($next_grp > $num_msgs) && ($prev_grp >= 0)) {
734 $prv_str = get_paginator_link($box, $prev_grp, $use, _("Previous"));
735 $nxt_str = "<FONT COLOR=\"$color[9]\">"._("Next")."</FONT>\n";
736 } else if (($next_grp <= $num_msgs) && ($prev_grp < 0)) {
737 $prv_str = "<FONT COLOR=\"$color[9]\">"._("Previous") . '</FONT>';
738 $nxt_str = get_paginator_link($box, $next_grp, $use, _("Next"));
739 }
740
741 /* Page selector block. Following code computes page links. */
742 if ($pg_sel && ($num_msgs > $show_num)) {
743 /* Most importantly, what is the current page!!! */
744 $cur_pg = intval($start_msg / $show_num) + 1;
745
746 /* Compute total # of pages and # of paginator page links. */
747 $tot_pgs = ceil($num_msgs / $show_num); /* Total # of Pages */
748 $vis_pgs = min($pg_max, $tot_pgs - 1); /* Visible Pages */
749
750 /************************************************************/
751 /* Compute the size of the four quarters of the page links. */
752 /************************************************************/
753
754 /* If we can, just show all the pages. */
755 if (($tot_pgs - 1) <= $pg_max) {
756 $q1_pgs = $cur_pg - 1;
757 $q2_pgs = $q3_pgs = 0;
758 $q4_pgs = $tot_pgs - $cur_pg;
759
760 /* Otherwise, compute some magic to choose the four quarters. */
761 } else {
762 /* Compute the magic base values. Added together, */
763 /* these values will always equal to the $pag_pgs. */
764 /* NOTE: These are DEFAULT values and do not take */
765 /* the current page into account. That is below. */
766 $q1_pgs = floor($vis_pgs/4);
767 $q2_pgs = round($vis_pgs/4, 0);
768 $q3_pgs = ceil($vis_pgs/4);
769 $q4_pgs = round(($vis_pgs - $q2_pgs)/3, 0);
770
771 /* Adjust if the first quarter contains the current page. */
772 if (($cur_pg - $q1_pgs) < 1) {
773 $extra_pgs = ($q1_pgs - ($cur_pg - 1)) + $q2_pgs;
774 $q1_pgs = $cur_pg - 1;
775 $q2_pgs = 0;
776 $q3_pgs += ceil($extra_pgs / 2);
777 $q4_pgs += floor($extra_pgs / 2);
778
779 /* Adjust if the first and second quarters intersect. */
780 } else if (($cur_pg - $q2_pgs - ceil($q2_pgs/3)) <= $q1_pgs) {
781 $extra_pgs = $q2_pgs;
782 $extra_pgs -= ceil(($cur_pg - $q1_pgs - 1) * 0.75);
783 $q2_pgs = ceil(($cur_pg - $q1_pgs - 1) * 0.75);
784 $q3_pgs += ceil($extra_pgs / 2);
785 $q4_pgs += floor($extra_pgs / 2);
786
787 /* Adjust if the fourth quarter contains the current page. */
788 } else if (($cur_pg + $q4_pgs) >= $tot_pgs) {
789 $extra_pgs = ($q4_pgs - ($tot_pgs - $cur_pg)) + $q3_pgs;
790 $q3_pgs = 0;
791 $q4_pgs = $tot_pgs - $cur_pg;
792 $q1_pgs += floor($extra_pgs / 2);
793 $q2_pgs += ceil($extra_pgs / 2);
794
795 /* Adjust if the third and fourth quarter intersect. */
796 } else if (($cur_pg + $q3_pgs + 1) >= ($tot_pgs - $q4_pgs + 1)) {
797 $extra_pgs = $q3_pgs;
798 $extra_pgs -= ceil(($tot_pgs - $cur_pg - $q4_pgs) * 0.75);
799 $q3_pgs = ceil(($tot_pgs - $cur_pg - $q4_pgs) * 0.75);
800 $q1_pgs += floor($extra_pgs / 2);
801 $q2_pgs += ceil($extra_pgs / 2);
802 }
803 }
804
805 /* I am leaving this debug code here, commented out, because */
806 /* it is a really nice way to see what the above code is doing. */
807 /* echo "qts = $q1_pgs/$q2_pgs/$q3_pgs/$q4_pgs = " */
808 /* . ($q1_pgs + $q2_pgs + $q3_pgs + $q4_pgs) . '<br>'; */
809
810 /************************************************************/
811 /* Print out the page links from the compute page quarters. */
812 /************************************************************/
813
814 /* Start with the first quarter. */
815 if (($q1_pgs == 0) && ($cur_pg > 1)) {
816 $pg_str .= "...$spc";
817 } else {
818 for ($pg = 1; $pg <= $q1_pgs; ++$pg) {
819 $start = (($pg-1) * $show_num) + 1;
820 $pg_str .= get_paginator_link($box, $start, $use, $pg) . $spc;
821 }
822 if ($cur_pg - $q2_pgs - $q1_pgs > 1) {
823 $pg_str .= "...$spc";
824 }
825 }
826
827 /* Continue with the second quarter. */
828 for ($pg = $cur_pg - $q2_pgs; $pg < $cur_pg; ++$pg) {
829 $start = (($pg-1) * $show_num) + 1;
830 $pg_str .= get_paginator_link($box, $start, $use, $pg) . $spc;
831 }
832
833 /* Now print the current page. */
834 $pg_str .= $cur_pg . $spc;
835
836 /* Next comes the third quarter. */
837 for ($pg = $cur_pg + 1; $pg <= $cur_pg + $q3_pgs; ++$pg) {
838 $start = (($pg-1) * $show_num) + 1;
839 $pg_str .= get_paginator_link($box, $start, $use, $pg) . $spc;
840 }
841
842 /* And last, print the forth quarter page links. */
843 if (($q4_pgs == 0) && ($cur_pg < $tot_pgs)) {
844 $pg_str .= "...$spc";
845 } else {
846 if (($tot_pgs - $q4_pgs) > ($cur_pg + $q3_pgs)) {
847 $pg_str .= "...$spc";
848 }
849 for ($pg = $tot_pgs - $q4_pgs + 1; $pg <= $tot_pgs; ++$pg) {
850 $start = (($pg-1) * $show_num) + 1;
851 $pg_str .= get_paginator_link($box, $start, $use, $pg) . $spc;
852 }
853 }
854 } else if ($PG_SHOWNUM == 999999) {
855 $pg_str = "<A HREF=\"right_main.php?use_mailbox_cache=$use"
856 . "&startMessage=1&mailbox=$box\" TARGET=\"right\">"
857 . _("Paginate") . '</A>' . $spc;
858 }
859
860 /* If necessary, compute the 'show all' string. */
861 if (($prv_str != '') || ($nxt_str != '')) {
862 $all_str = "<A HREF=\"right_main.php?PG_SHOWNUM=999999"
863 . "&use_mailbox_cache=$use&startMessage=1&mailbox=$box\" "
864 . "TARGET=\"right\">" . _("Show All") . '</A>';
865 }
866
867 /* Last but not least, get the value for the toggle all link. */
868 $tgl_str = get_selectall_link($start_msg, $sort);
869
870 /* Put all the pieces of the paginator string together. */
871 $result = '';
872 $result .= ($prv_str != '' ? $prv_str . $spc . $sep . $spc : '');
873 $result .= ($nxt_str != '' ? $nxt_str . $spc . $sep . $spc : '');
874 $result .= ($pg_str != '' ? $pg_str : '');
875 $result .= ($all_str != '' ? $sep . $spc . $all_str . $spc : '');
876 $result .= ($result != '' ? $sep . $spc . $tgl_str: $tgl_str);
877
878 /* If the resulting string is blank, return a non-breaking space. */
879 if ($result == '') {
880 $result = '&nbsp;';
881 }
882
883 /* Return our final magical paginator string. */
884 return ($result);
885 }
886
887 function processSubject($subject) {
888 // Shouldn't ever happen -- caught too many times in the IMAP functions
889 if ($subject == '')
890 return _("(no subject)");
891
892 if (strlen($subject) <= 55)
893 return $subject;
894
895 $ent_strlen=strlen($subject);
896 $trim_val=50;
897 $ent_offset=0;
898 // see if this is entities-encoded string
899 // If so, Iterate through the whole string, find out
900 // the real number of characters, and if more
901 // than 55, substr with an updated trim value.
902 while (($ent_loc = strpos($subject, '&', $ent_offset)) !== false &&
903 ($ent_loc_end = strpos($subject, ';', $ent_loc)) !== false)
904 {
905 $trim_val += ($ent_loc_end-$ent_loc)+1;
906 $ent_strlen -= $ent_loc_end-$ent_loc;
907 $ent_offset = $ent_loc_end+1;
908 }
909
910 if ($ent_strlen <= 55)
911 return $subject;
912
913 return substr($subject, 0, $trim_val) . '...';
914 }
915
916 function handleAsSent($mailbox) {
917 global $sent_folder, $draft_folder;
918 global $handleAsSent_result;
919
920 /* First check if this is the sent or draft folder. */
921 $handleAsSent_result = (($mailbox == $sent_folder)
922 || ($mailbox == $draft_folder));
923
924 /* Then check the result of the handleAsSent hook. */
925 do_hook('check_handleAsSent_result', $mailbox);
926
927 /* And return the result. */
928 return ($handleAsSent_result);
929 }
930
931 ?>