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