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