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