Today Cyrus 2.2.2-BETA with SASL Initial Client response was released so it
[squirrelmail.git] / functions / mailbox_display.php
CommitLineData
59177427 1<?php
2ba13803 2
35586184 3/**
4 * mailbox_display.php
5 *
76911253 6 * Copyright (c) 1999-2003 The SquirrelMail Project Team
35586184 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 */
a4c2cd49 14
b68edc75 15require_once(SM_PATH . 'functions/strings.php');
16require_once(SM_PATH . 'functions/html.php');
17require_once(SM_PATH . 'class/html.class.php');
18require_once(SM_PATH . 'functions/imap_mailbox.php');
26454147 19require_once(SM_PATH . 'functions/imap_messages.php');
20require_once(SM_PATH . 'functions/mime.php');
43fdb2a4 21
6dc5eca4 22/* Constants:
23 * PG_SEL_MAX: default value for page_selector_max
6dc5eca4 24 */
6c930ade 25define('PG_SEL_MAX', 10);
26
098ea084 27function elapsed($start)
28{
29 $end = microtime();
30 list($start2, $start1) = explode(" ", $start);
31 list($end2, $end1) = explode(" ", $end);
32 $diff1 = $end1 - $start1;
33 $diff2 = $end2 - $start2;
34 if( $diff2 < 0 ){
35 $diff1 -= 1;
36 $diff2 += 1.0;
37 }
38 return $diff2 + $diff1;
39}
40
7b887886 41function printMessageInfo($imapConnection, $t, $not_last=true, $key, $mailbox,
d215ca7d 42 $start_msg, $where, $what) {
8008456a 43 global $checkall,
5bba30b5 44 $color, $msgs, $msort, $td_str, $msg,
c1c17724 45 $default_use_priority,
46 $message_highlight_list,
47 $index_order,
459e3347 48 $indent_array, /* indent subject by */
49 $pos, /* Search postion (if any) */
c1c17724 50 $thread_sort_messages, /* thread sorting on/off */
459e3347 51 $server_sort_order, /* sort value when using server-sorting */
c1c17724 52 $row_count,
459e3347 53 $allow_server_sort, /* enable/disable server-side sorting */
53ad5f60 54 $truncate_sender, /* number of characters for From/To field (<= 0 for unchanged) */
1ae2832e 55 $email_address,
56 $show_recipient_instead; /* show recipient name instead of default identity */
459e3347 57
e9e5f0fa 58 $color_string = $color[4];
8008456a 59
60 if ($GLOBALS['alt_index_colors']) {
61 if (!isset($row_count)) {
62 $row_count = 0;
63 }
64 $row_count++;
65 if ($row_count % 2) {
66 if (!isset($color[12])) {
67 $color[12] = '#EAEAEA';
68 }
69 $color_string = $color[12];
70 }
bdfb67f8 71 }
8008456a 72 $msg = $msgs[$key];
73
6a622b59 74 if($mailbox == 'None') {
75 $boxes = sqimap_mailbox_list($imapConnection);
8008456a 76 $mailbox = $boxes[0]['unformatted'];
6a622b59 77 unset($boxes);
bdfb67f8 78 }
8008456a 79 $urlMailbox = urlencode($mailbox);
e9e5f0fa 80
53ad5f60 81 $bSentFolder = handleAsSent($mailbox);
1ae2832e 82 if ((!$bSentFolder) && ($show_recipient_instead)) {
83 // If the From address is the same as $email_address, then handle as Sent
84 $from_array = parseAddress($msg['FROM'], 1);
85 if (!isset($email_address)) {
86 global $datadir, $username;
87 $email_address = getPref($datadir, $username, 'email_address');
88 }
89 $bHandleAsSent = ((isset($from_array[0][0])) && ($from_array[0][0] == $email_address));
098ea084 90 }
1ae2832e 91 else
92 $bHandleAsSent = $bSentFolder;
53ad5f60 93 // If this is a Sent message, display To address instead of From
94 if ($bHandleAsSent)
95 $msg['FROM'] = $msg['TO'];
96 // Passing 1 below results in only 1 address being parsed, thus defeating the following code
97 $msg['FROM'] = parseAddress($msg['FROM']/*,1*/);
9701e9c8 98
e9e5f0fa 99 /*
100 * This is done in case you're looking into Sent folders,
101 * because you can have multiple receivers.
102 */
098ea084 103 $senderNames = $msg['FROM'];
104 $senderName = '';
26454147 105 $senderAddress = '';
098ea084 106 if (sizeof($senderNames)){
107 foreach ($senderNames as $senderNames_part) {
e9e5f0fa 108 if ($senderName != '') {
109 $senderName .= ', ';
26454147 110 $senderAddress .= ', ';
e9e5f0fa 111 }
26454147 112 $sender_address_part = htmlspecialchars($senderNames_part[0]);
53ad5f60 113 $sender_name_part = str_replace('&nbsp;',' ', decodeHeader($senderNames_part[1]));
26454147 114 if ($sender_name_part) {
115 $senderName .= $sender_name_part;
116 $senderAddress .= $sender_name_part . ' <' . $sender_address_part . '>';
098ea084 117 } else {
26454147 118 $senderName .= $sender_address_part;
119 $senderAddress .= $sender_address_part;
098ea084 120 }
121 }
937c81fa 122 }
53ad5f60 123 // If Sent, prefix with To: but only if not Sent folder
124 if ($bHandleAsSent ^ $bSentFolder) {
125 $senderName = _("To:") . ' ' . $senderName;
126 $senderAddress = _("To:") . ' ' . $senderAddress;
127 }
459e3347 128
eaa4f45f 129 if ($truncate_sender > 0)
130 $senderName = truncateWithEntities($senderName, $truncate_sender);
459e3347 131
5786ef5a 132 echo html_tag( 'tr','','','','VALIGN="top"') . "\n";
8008456a 133
134 if (isset($msg['FLAG_FLAGGED']) && ($msg['FLAG_FLAGGED'] == true)) {
135 $flag = "<font color=\"$color[2]\">";
136 $flag_end = '</font>';
137 } else {
138 $flag = '';
139 $flag_end = '';
bdfb67f8 140 }
8008456a 141 if (!isset($msg['FLAG_SEEN']) || ($msg['FLAG_SEEN'] == false)) {
142 $bold = '<b>';
143 $bold_end = '</b>';
144 } else {
145 $bold = '';
146 $bold_end = '';
bdfb67f8 147 }
53ad5f60 148 if ($bHandleAsSent) {
8008456a 149 $italic = '<i>';
150 $italic_end = '</i>';
151 } else {
152 $italic = '';
153 $italic_end = '';
154 }
155 if (isset($msg['FLAG_DELETED']) && $msg['FLAG_DELETED']) {
156 $fontstr = "<font color=\"$color[9]\">";
157 $fontstr_end = '</font>';
158 } else {
159 $fontstr = '';
160 $fontstr_end = '';
161 }
162
d215ca7d 163 if ($where && $what) {
164 $searchstr = '&amp;where='.$where.'&amp;what='.$what;
165 } else {
166 $searchstr = '';
6a622b59 167 }
9701e9c8 168
937c81fa 169 if (is_array($message_highlight_list) && count($message_highlight_list)) {
8415b2d3 170 $msg['TO'] = parseAddress($msg['TO']);
171 $msg['CC'] = parseAddress($msg['CC']);
8008456a 172 foreach ($message_highlight_list as $message_highlight_list_part) {
173 if (trim($message_highlight_list_part['value']) != '') {
c0d04f97 174 $high_val = strtolower($message_highlight_list_part['value']);
175 $match_type = strtoupper($message_highlight_list_part['match_type']);
8415b2d3 176 if($match_type == 'TO_CC') {
177 $match = array('TO', 'CC');
178 } else {
179 $match = array($match_type);
180 }
181 foreach($match as $match_type) {
182 switch($match_type) {
183 case('TO'):
184 case('CC'):
185 case('FROM'):
186 foreach ($msg[$match_type] as $address) {
9701e9c8 187 $address[0] = decodeHeader($address[0], true, false);
188 $address[1] = decodeHeader($address[1], true, false);
8415b2d3 189 if (strstr('^^' . strtolower($address[0]), $high_val) ||
190 strstr('^^' . strtolower($address[1]), $high_val)) {
191 $hlt_color = $message_highlight_list_part['color'];
192 break 4;
193 }
937c81fa 194 }
937c81fa 195 break;
8415b2d3 196 default:
9701e9c8 197 $headertest = strtolower(decodeHeader($msg[$match_type], true, false));
198 if (strstr('^^' . $headertest, $high_val)) {
937c81fa 199 $hlt_color = $message_highlight_list_part['color'];
8415b2d3 200 break 3;
937c81fa 201 }
8415b2d3 202 break;
203 }
8008456a 204 }
205 }
206 }
207 }
208
209 if (!isset($hlt_color)) {
210 $hlt_color = $color_string;
211 }
59352d08 212 $checked = ($checkall == 1) ? ' CHECKED' : '';
5e0efebf 213 $col = 0;
26454147 214 $msg['SUBJECT'] = str_replace('&nbsp;', ' ', decodeHeader($msg['SUBJECT']));
9701e9c8 215 $subject = processSubject($msg['SUBJECT'], $indent_array[$msg['ID']]);
6a622b59 216 if (sizeof($index_order)) {
8008456a 217 foreach ($index_order as $index_order_part) {
218 switch ($index_order_part) {
219 case 1: /* checkbox */
220 echo html_tag( 'td',
221 "<input type=checkbox name=\"msg[$t]\" value=\"".$msg['ID']."\"$checked>",
6a622b59 222 'center',
8008456a 223 $hlt_color );
224 break;
225 case 2: /* from */
26454147 226 if ($senderAddress != $senderName) {
227 $senderAddress = strtr($senderAddress, array_flip(get_html_translation_table(HTML_SPECIALCHARS)));
228 $title = ' title="' . str_replace('"', "''", $senderAddress) . '"';
229 }
230 else
231 $title = '';
8008456a 232 echo html_tag( 'td',
098ea084 233 $italic . $bold . $flag . $fontstr . $senderName .
8008456a 234 $fontstr_end . $flag_end . $bold_end . $italic_end,
c1c17724 235 'left',
26454147 236 $hlt_color, $title );
8008456a 237 break;
238 case 3: /* date */
53d9fbc5 239 $date_string = $msg['DATE_STRING'] . '';
240 if ($date_string == '') {
241 $date_string = _("Unknown date");
242 }
8008456a 243 echo html_tag( 'td',
53d9fbc5 244 $bold . $flag . $fontstr . $date_string .
8008456a 245 $fontstr_end . $flag_end . $bold_end,
246 'center',
247 $hlt_color,
248 'nowrap' );
249 break;
250 case 4: /* subject */
251 $td_str = $bold;
252 if ($thread_sort_messages == 1) {
6903c7bd 253 if (isset($indent_array[$msg['ID']])) {
8008456a 254 $td_str .= str_repeat("&nbsp;&nbsp;&nbsp;&nbsp;",$indent_array[$msg['ID']]);
255 }
256 }
e9e5f0fa 257 $td_str .= '<a href="read_body.php?mailbox='.$urlMailbox
6a622b59 258 . '&amp;passed_id='. $msg["ID"]
259 . '&amp;startMessage='.$start_msg.$searchstr.'"';
158fa340 260 $td_str .= ' ' .concat_hook_function('subject_link', array($start_msg, $searchstr));
306b6393 261 if ($subject != $msg['SUBJECT']) {
8008456a 262 $title = get_html_translation_table(HTML_SPECIALCHARS);
263 $title = array_flip($title);
306b6393 264 $title = strtr($msg['SUBJECT'], $title);
8008456a 265 $title = str_replace('"', "''", $title);
266 $td_str .= " title=\"$title\"";
267 }
268 $td_str .= ">$flag$subject$flag_end</a>$bold_end";
c1c17724 269 echo html_tag( 'td', $td_str, 'left', $hlt_color );
8008456a 270 break;
271 case 5: /* flags */
272 $stuff = false;
273 $td_str = "<b><small>";
6a622b59 274
8008456a 275 if (isset($msg['FLAG_ANSWERED']) && $msg['FLAG_ANSWERED'] == true) {
276 $td_str .= _("A");
277 $stuff = true;
278 }
279 if ($msg['TYPE0'] == 'multipart') {
280 $td_str .= '+';
281 $stuff = true;
282 }
283 if ($default_use_priority) {
284 if ( ($msg['PRIORITY'] == 1) || ($msg['PRIORITY'] == 2) ) {
285 $td_str .= "<font color=\"$color[1]\">!</font>";
286 $stuff = true;
287 }
288 if ($msg['PRIORITY'] == 5) {
289 $td_str .= "<font color=\"$color[8]\">?</font>";
290 $stuff = true;
291 }
292 }
293 if (isset($msg['FLAG_DELETED']) && $msg['FLAG_DELETED'] == true) {
294 $td_str .= "<font color=\"$color[1]\">D</font>";
295 $stuff = true;
296 }
297 if (!$stuff) {
298 $td_str .= '&nbsp;';
299 }
2bd00b7b 300 do_hook("msg_envelope");
8008456a 301 $td_str .= '</small></b>';
302 echo html_tag( 'td',
303 $td_str,
304 'center',
305 $hlt_color,
306 'nowrap' );
307 break;
308 case 6: /* size */
8008456a 309 echo html_tag( 'td',
6a622b59 310 $bold . $fontstr . show_readable_size($msg['SIZE']) .
8008456a 311 $fontstr_end . $bold_end,
312 'right',
313 $hlt_color );
314 break;
315 }
ed230bc3 316 ++$col;
8008456a 317 }
bdfb67f8 318 }
7b887886 319 if ($not_last) {
ed230bc3 320 echo '</tr>' . "\n" . '<tr><td COLSPAN="' . $col . '" BGCOLOR="' .
321 $color[0] . '" HEIGHT="1"></td></tr>' . "\n";
6a622b59 322 } else {
7b887886 323 echo '</tr>'."\n";
6a622b59 324 }
e9e5f0fa 325}
326
ed230bc3 327function getServerMessages($imapConnection, $start_msg, $show_num, $num_msgs, $id) {
328 if ($id != 'no') {
098ea084 329 $id = array_slice($id, ($start_msg-1), $show_num);
ed230bc3 330 $end = $start_msg + $show_num - 1;
331 if ($num_msgs < $show_num) {
332 $end_loop = $num_msgs;
333 } else if ($end > $num_msgs) {
334 $end_loop = $num_msgs - $start_msg + 1;
335 } else {
336 $end_loop = $show_num;
83cdc70b 337 }
338 return fillMessageArray($imapConnection,$id,$end_loop,$show_num);
ed230bc3 339 } else {
340 return false;
341 }
342}
343
344function getThreadMessages($imapConnection, $start_msg, $show_num, $num_msgs) {
345 $id = get_thread_sort($imapConnection);
346 return getServerMessages($imapConnection, $start_msg, $show_num, $num_msgs, $id);
347}
348
6a622b59 349function getServerSortMessages($imapConnection, $start_msg, $show_num,
ed230bc3 350 $num_msgs, $server_sort_order, $mbxresponse) {
351 $id = sqimap_get_sort_order($imapConnection, $server_sort_order,$mbxresponse);
352 return getServerMessages($imapConnection, $start_msg, $show_num, $num_msgs, $id);
e9e5f0fa 353}
354
6a622b59 355function getSelfSortMessages($imapConnection, $start_msg, $show_num,
e9e5f0fa 356 $num_msgs, $sort, $mbxresponse) {
ed230bc3 357 $msgs = array();
358 if ($num_msgs >= 1) {
359 $id = sqimap_get_php_sort_order ($imapConnection, $mbxresponse);
360 if ($sort < 6 ) {
361 $end = $num_msgs;
362 $end_loop = $end;
7b73b809 363 /* set shownum to 999999 to fool sqimap_get_small_header_list
364 and rebuild the msgs_str to 1:* */
365 $show_num = 999999;
ed230bc3 366 } else {
367 /* if it's not sorted */
368 if ($start_msg + ($show_num - 1) < $num_msgs) {
369 $end_msg = $start_msg + ($show_num - 1);
370 } else {
371 $end_msg = $num_msgs;
372 }
373 if ($end_msg < $start_msg) {
374 $start_msg = $start_msg - $show_num;
375 if ($start_msg < 1) {
376 $start_msg = 1;
377 }
378 }
098ea084 379 $id = array_slice(array_reverse($id), ($start_msg-1), $show_num);
ed230bc3 380 $end = $start_msg + $show_num - 1;
381 if ($num_msgs < $show_num) {
382 $end_loop = $num_msgs;
383 } else if ($end > $num_msgs) {
384 $end_loop = $num_msgs - $start_msg + 1;
385 } else {
386 $end_loop = $show_num;
387 }
83cdc70b 388 }
389 $msgs = fillMessageArray($imapConnection,$id,$end_loop, $show_num);
e9e5f0fa 390 }
ed230bc3 391 return $msgs;
e9e5f0fa 392}
393
394
395
6c930ade 396/*
e35045b7 397 * This function loops through a group of messages in the mailbox
398 * and shows them to the user.
399 */
6a622b59 400function showMessagesForMailbox($imapConnection, $mailbox, $num_msgs,
401 $start_msg, $sort, $color, $show_num,
402 $use_cache, $mode='') {
fcba142a 403 global $msgs, $msort, $auto_expunge, $thread_sort_messages,
6a622b59 404 $allow_server_sort, $server_sort_order;
405
167d7428 406 /*
407 * For some reason, on PHP 4.3+, this being unset, and set in the session causes havoc
408 * so setting it to an empty array beforehand seems to clean up the issue, and stopping the
409 * "Your script possibly relies on a session side-effect which existed until PHP 4.2.3" error
410 */
411
412 if (!isset($msort)) {
413 $msort = array();
414 }
415
49ae9d34 416 if (!isset($msgs)) {
417 $msgs = array();
418 }
419
8415b2d3 420 //$start = microtime();
6a622b59 421 /* If autoexpunge is turned on, then do it now. */
422 $mbxresponse = sqimap_mailbox_select($imapConnection, $mailbox);
423 $srt = $sort;
424 /* If autoexpunge is turned on, then do it now. */
425 if ($auto_expunge == true) {
426 $exp_cnt = sqimap_mailbox_expunge($imapConnection, $mailbox, false, '');
427 $mbxresponse['EXISTS'] = $mbxresponse['EXISTS'] - $exp_cnt;
428 $num_msgs = $mbxresponse['EXISTS'];
429 }
e35045b7 430
6a622b59 431 if ($mbxresponse['EXISTS'] > 0) {
432 /* if $start_msg is lower than $num_msgs, we probably deleted all messages
433 * in the last page. We need to re-adjust the start_msg
434 */
aa0da530 435
6a622b59 436 if($start_msg > $num_msgs) {
437 $start_msg -= $show_num;
438 if($start_msg < 1) {
439 $start_msg = 1;
440 }
e9e5f0fa 441 }
4e9f35e4 442
6a622b59 443 /* This code and the next if() block check for
444 * server-side sorting methods. The $id array is
445 * formatted and $sort is set to 6 to disable
446 * SM internal sorting
447 */
448
449 if ($thread_sort_messages == 1) {
450 $mode = 'thread';
451 } elseif ($allow_server_sort == 1) {
452 $mode = 'serversort';
453 } else {
454 $mode = '';
455 }
8008456a 456
7b73b809 457 if ($use_cache) {
458 sqgetGlobalVar('msgs', $msgs, SQ_SESSION);
459 sqgetGlobalVar('msort', $msort, SQ_SESSION);
460 } else {
461 sqsession_unregister('msort');
d3714d3a 462 sqsession_unregister('msgs'); }
6a622b59 463 switch ($mode) {
464 case 'thread':
ed230bc3 465 $id = get_thread_sort($imapConnection);
466 $msgs = getServerMessages($imapConnection, $start_msg, $show_num, $num_msgs, $id);
6a622b59 467 if ($msgs === false) {
468 echo '<b><small><center><font color=red>' .
469 _("Thread sorting is not supported by your IMAP server.<br>Please report this to the system administrator.").
470 '</center></small></b>';
471 $thread_sort_messages = 0;
472 $msort = $msgs = array();
6a622b59 473 } else {
474 $msort= $msgs;
475 $sort = 6;
6a622b59 476 }
477 break;
478 case 'serversort':
ed230bc3 479 $id = sqimap_get_sort_order($imapConnection, $sort, $mbxresponse);
480 $msgs = getServerMessages($imapConnection, $start_msg, $show_num, $num_msgs, $id);
6a622b59 481 if ($msgs === false) {
482 echo '<b><small><center><font color=red>' .
483 _( "Server-side sorting is not supported by your IMAP server.<br>Please report this to the system administrator.").
484 '</center></small></b>';
485 $sort = $server_sort_order;
486 $allow_server_sort = FALSE;
487 $msort = $msgs = array();
6a622b59 488 $id = array();
489 } else {
6a622b59 490 $msort = $msgs;
ed230bc3 491 $sort = 6;
6a622b59 492 }
493 break;
494 default:
495 if (!$use_cache) {
6a622b59 496 $msgs = getSelfSortMessages($imapConnection, $start_msg, $show_num,
497 $num_msgs, $sort, $mbxresponse);
498 $msort = calc_msort($msgs, $sort);
6a622b59 499 } /* !use cache */
500 break;
501 } // switch
ed230bc3 502 sqsession_register($msort, 'msort');
503 sqsession_register($msgs, 'msgs');
7b73b809 504
6a622b59 505 } /* if exists > 0 */
506
507 $res = getEndMessage($start_msg, $show_num, $num_msgs);
508 $start_msg = $res[0];
509 $end_msg = $res[1];
510
511 $paginator_str = get_paginator_str($mailbox, $start_msg, $end_msg,
512 $num_msgs, $show_num, $sort);
513
514 $msg_cnt_str = get_msgcnt_str($start_msg, $end_msg, $num_msgs);
515
516 do_hook('mailbox_index_before');
fcba142a 517 echo '<table border="0" width="100%" cellpadding="0" cellspacing="0">';
22642690 518 echo '<tr><td>';
519
520 mail_message_listing_beginning($imapConnection, $mailbox, $sort,
521 $msg_cnt_str, $paginator_str, $start_msg);
522 echo '</td></tr>';
523 /* line between the button area and the list */
524 echo '<tr><td HEIGHT="5" BGCOLOR="'.$color[4].'"></td></tr>';
525
526 echo '<tr><td>';
527 echo ' <table width="100%" cellpadding="1" cellspacing="0" align="center"'.' border="0" bgcolor="'.$color[9].'">';
528 echo ' <tr><td>';
529 echo ' <table width="100%" cellpadding="1" cellspacing="0" align="center" border="0" bgcolor="'.$color[5].'">';
530 echo '<tr><td>';
6a622b59 531 printHeader($mailbox, $srt, $color, !$thread_sort_messages);
532
22642690 533 displayMessageArray($imapConnection, $num_msgs, $start_msg,
534 $msort, $mailbox, $sort, $color, $show_num,0,0);
535 echo '</td></tr></table></td></tr></table>';
6a622b59 536
22642690 537 mail_message_listing_end($num_msgs, $paginator_str, $msg_cnt_str, $color);
6a622b59 538 echo '</td></tr></table>';
8415b2d3 539 //$t = elapsed($start);
46a49f65 540 //echo("elapsed time = $t seconds\n");
6c930ade 541}
bdfb67f8 542
e9e5f0fa 543function calc_msort($msgs, $sort) {
8008456a 544
e35045b7 545 /*
546 * 0 = Date (up)
547 * 1 = Date (dn)
548 * 2 = Name (up)
549 * 3 = Name (dn)
550 * 4 = Subject (up)
551 * 5 = Subject (dn)
552 */
625c8b78 553
6a622b59 554 if (($sort == 0) || ($sort == 1)) {
625c8b78 555 foreach ($msgs as $item) {
556 $msort[] = $item['TIME_STAMP'];
557 }
6a622b59 558 } elseif (($sort == 2) || ($sort == 3)) {
625c8b78 559 foreach ($msgs as $item) {
560 $msort[] = $item['FROM-SORT'];
561 }
6a622b59 562 } elseif (($sort == 4) || ($sort == 5)) {
625c8b78 563 foreach ($msgs as $item) {
564 $msort[] = $item['SUBJECT-SORT'];
565 }
6a622b59 566 } else {
567 $msort = $msgs;
568 }
569 if ($sort < 6) {
570 if ($sort % 2) {
571 asort($msort);
572 } else {
573 arsort($msort);
574 }
575 }
576 return $msort;
577}
578
fc60dc76 579function fillMessageArray($imapConnection, $id, $count, $show_num=false) {
a18594b2 580 return sqimap_get_small_header_list($imapConnection, $id, $show_num);
e35045b7 581}
8008456a 582
e9e5f0fa 583
e35045b7 584/* Generic function to convert the msgs array into an HTML table. */
6a622b59 585function displayMessageArray($imapConnection, $num_msgs, $start_msg,
586 $msort, $mailbox, $sort, $color,
587 $show_num, $where=0, $what=0) {
59352d08 588 global $imapServerAddress, $use_mailbox_cache, $index_order,
6a622b59 589 $indent_array, $thread_sort_messages, $allow_server_sort,
590 $server_sort_order, $PHP_SELF;
591
592 $res = getEndMessage($start_msg, $show_num, $num_msgs);
593 $start_msg = $res[0];
594 $end_msg = $res[1];
a966982b 595
6a622b59 596 $urlMailbox = urlencode($mailbox);
e9e5f0fa 597
6a622b59 598 /* get indent level for subject display */
540c4435 599 if ($thread_sort_messages == 1 && $num_msgs) {
6a622b59 600 $indent_array = get_parent_level($imapConnection);
94ac35c6 601 }
a966982b 602
6a622b59 603 $real_startMessage = $start_msg;
604 if ($sort == 6) {
605 if ($end_msg - $start_msg < $show_num - 1) {
606 $end_msg = $end_msg - $start_msg + 1;
607 $start_msg = 1;
608 } else if ($start_msg > $show_num) {
609 $end_msg = $show_num;
610 $start_msg = 1;
611 }
612 }
613 $endVar = $end_msg + 1;
614
615 /*
616 * Loop through and display the info for each message.
617 * ($t is used for the checkbox number)
618 */
619 $t = 0;
620
621 /* messages display */
622
540c4435 623 if (!$num_msgs) {
e35045b7 624 /* if there's no messages in this folder */
8008456a 625 echo html_tag( 'tr',
13cf639b 626 html_tag( 'td',
6b4bd11f 627 "<BR><b>" . _("THIS FOLDER IS EMPTY") . "</b><BR>&nbsp;",
13cf639b 628 'center',
629 $color[4],
630 'COLSPAN="' . count($index_order) . '"'
631 )
6a622b59 632 );
633 } elseif ($start_msg == $end_msg) {
e35045b7 634 /* if there's only one message in the box, handle it differently. */
6a622b59 635 if ($sort != 6) {
636 $i = $start_msg;
637 } else {
638 $i = 1;
639 }
640 reset($msort);
641 $k = 0;
642 do {
643 $key = key($msort);
644 next($msort);
645 $k++;
646 } while (isset ($key) && ($k < $i));
7b887886 647 printMessageInfo($imapConnection, $t, true, $key, $mailbox,
ed230bc3 648 $real_startMessage, $where, $what);
94ac35c6 649 } else {
6a622b59 650 $i = $start_msg;
651 reset($msort);
652 $k = 0;
653 do {
654 $key = key($msort);
655 next($msort);
656 $k++;
657 } while (isset ($key) && ($k < $i));
ed230bc3 658 $not_last = true;
6a622b59 659 do {
ed230bc3 660 if (!$i || $i == $endVar-1) $not_last = false;
661 printMessageInfo($imapConnection, $t, $not_last, $key, $mailbox,
662 $real_startMessage, $where, $what);
6a622b59 663 $key = key($msort);
664 $t++;
665 $i++;
666 next($msort);
667 } while ($i && $i < $endVar);
94ac35c6 668 }
bdfb67f8 669}
670
6c930ade 671/*
e35045b7 672 * Displays the standard message list header. To finish the table,
673 * you need to do a "</table></table>";
674 *
675 * $moveURL is the URL to submit the delete/move form to
676 * $mailbox is the current mailbox
677 * $sort is the current sorting method (-1 for no sorting available [searches])
678 * $Message is a message that is centered on top of the list
679 * $More is a second line that is left aligned
680 */
e35045b7 681
6a622b59 682function mail_message_listing_beginning ($imapConnection,
683 $mailbox = '', $sort = -1,
684 $msg_cnt_str = '',
685 $paginator = '&nbsp;',
686 $start_msg = 1) {
687 global $color, $auto_expunge, $base_uri, $thread_sort_messages,
688 $allow_thread_sort, $allow_server_sort, $server_sort_order,
689 $PHP_SELF;
690
691 $php_self = $PHP_SELF;
692 /* fix for incorrect $PHP_SELF */
693 if (strpos($php_self, 'move_messages.php')) {
694 $php_self = str_replace('move_messages.php', 'right_main.php', $php_self);
695 }
696 $urlMailbox = urlencode($mailbox);
e35045b7 697
6a622b59 698 if (preg_match('/^(.+)\?.+$/',$php_self,$regs)) {
699 $source_url = $regs[1];
700 } else {
701 $source_url = $php_self;
702 }
703
704 if (!isset($msg)) {
705 $msg = '';
706 }
8fe969f6 707 $moveFields = '<input type="hidden" name="msg" value="'.htmlspecialchars($msg).'">' .
708 '<input type="hidden" name="mailbox" value="'.htmlspecialchars($mailbox).'">' .
709 '<input type="hidden" name="startMessage" value="'.htmlspecialchars($start_msg).'">';
710
711// $moveURL = "move_messages.php?msg=$msg&amp;mailbox=$urlMailbox"
712// . "&amp;startMessage=$start_msg";
6a622b59 713 /*
714 * This is the beginning of the message list table.
715 * It wraps around all messages
716 */
3027826a 717 $safe_name = preg_replace("/[^0-9A-Za-z_]/", '_', $mailbox);
718 $form_name = "FormMsgs" . $safe_name;
719 echo '<form name="' . $form_name . '" method="post" action="move_messages.php">' ."\n"
8fe969f6 720 . $moveFields
22642690 721 . html_tag( 'table' ,
c1c17724 722 html_tag( 'tr',
723 html_tag( 'td' ,
724 html_tag( 'table' ,
725 html_tag( 'tr',
726 html_tag( 'td', $paginator, 'left' ) .
22642690 727 html_tag( 'td', $msg_cnt_str, 'right' )
c1c17724 728 )
22642690 729 , '', $color[4], 'border="0" width="100%" cellpadding="1" cellspacing="0"' )
c1c17724 730 , 'left', '', '' )
731 , '', $color[0] )
22642690 732 , '', '', 'border="0" width="100%" cellpadding="1" cellspacing="0"' );
4b920601 733 /* line between header and button area */
22642690 734 echo '<tr><td HEIGHT="5" BGCOLOR="'.$color[4].'"></td></tr>';
22642690 735
4b920601 736 echo '<tr><td>';
22642690 737 echo html_tag( 'tr' ) . "\n"
738 . html_tag( 'td' ,'' , 'left', '', '' )
739 . html_tag( 'table' ,'' , '', $color[9], 'border="0" width="100%" cellpadding="1" cellspacing="0"' )
740 . '<tr><td>'
741 . html_tag( 'table' ,'' , '', $color[0], 'border="0" width="100%" cellpadding="1" cellspacing="0"' )
c1c17724 742 . html_tag( 'tr',
22642690 743 getSmallStringCell(_("Move Selected To"), 'left', 'nowrap') .
744 getSmallStringCell(_("Transform Selected Messages"), 'right')
c1c17724 745 )
e9e5f0fa 746 . html_tag( 'tr' ) ."\n"
747 . html_tag( 'td', '', 'left', '', 'valign="middle" nowrap' );
22642690 748 getMbxList($imapConnection);
749 echo getButton('SUBMIT', 'moveButton',_("Move")) . "\n";
750 echo getButton('SUBMIT', 'attache',_("Forward")) . "\n";
6c930ade 751
22642690 752 echo " </TD>\n"
6a622b59 753 . html_tag( 'td', '', 'right', '', 'nowrap' );
e35045b7 754
22642690 755
756
6a622b59 757 if (!$auto_expunge) {
758 echo getButton('SUBMIT', 'expungeButton',_("Expunge"))
759 .'&nbsp;' . _("mailbox") . "\n";
760 }
d3714d3a 761 do_hook('mailbox_display_buttons');
6a622b59 762 echo getButton('SUBMIT', 'markRead',_("Read"));
763 echo getButton('SUBMIT', 'markUnread',_("Unread"));
8e724112 764 echo getButton('SUBMIT', 'delete',_("Delete")) ."&nbsp;\n";
6a622b59 765 if (!strpos($php_self,'mailbox')) {
766 $location = $php_self.'?mailbox=INBOX&amp;startMessage=1';
767 } else {
768 $location = $php_self;
8008456a 769 }
6a622b59 770 echo '<INPUT TYPE="HIDDEN" NAME="location" VALUE="'.$location.'">';
771 echo "</TD>\n"
772 . " </TR>\n";
773
774 /* draws thread sorting links */
775 if ($allow_thread_sort == TRUE) {
776 if ($thread_sort_messages == 1 ) {
777 $set_thread = 2;
778 $thread_name = _("Unthread View");
779 } elseif ($thread_sort_messages == 0) {
780 $set_thread = 1;
781 $thread_name = _("Thread View");
782 }
c1c17724 783 echo html_tag( 'tr' ,
784 html_tag( 'td' ,
e35045b7 785 '&nbsp;<a href=' . $source_url . '?sort='
c1c17724 786 . "$sort" . '&start_messages=1&set_thread=' . "$set_thread"
787 . '&mailbox=' . urlencode($mailbox) . '><small>' . $thread_name
788 . '</a></small>&nbsp;'
c359d4a2 789 , '', '', '' ) . html_tag( 'td', _("Bypass Trash") . '<input type="checkbox" name="bypass_trash">','right','','')
6c540963 790
c1c17724 791 , '', '', '' );
6a622b59 792 }
a966982b 793
22642690 794 echo "</TABLE></td></tr></table></td></tr>\n";
a966982b 795
1b91be0c 796 do_hook('mailbox_form_before');
6c766fd5 797
8008456a 798 /* if using server sort we highjack the
6a622b59 799 * the $sort var and use $server_sort_order
800 * instead. but here we reset sort for a bit
801 * since its easy
802 */
8008456a 803 if ($allow_server_sort == TRUE) {
804 $sort = $server_sort_order;
805 }
e9e5f0fa 806}
a966982b 807
e9e5f0fa 808function mail_message_listing_end($num_msgs, $paginator_str, $msg_cnt_str, $color) {
22642690 809 if ($num_msgs) {
4b920601 810 /* space between list and footer */
22642690 811 echo '<tr><td HEIGHT="5" BGCOLOR="'.$color[4].'" COLSPAN="1">';
4b920601 812
22642690 813 echo '</td></tr><tr><td>';
814 echo html_tag( 'table',
e9e5f0fa 815 html_tag( 'tr',
816 html_tag( 'td',
817 html_tag( 'table',
818 html_tag( 'tr',
819 html_tag( 'td', $paginator_str ) .
820 html_tag( 'td', $msg_cnt_str, 'right' )
821 )
22642690 822 , '', $color[4], 'width="100%" border="0" cellpadding="1" cellspacing="0"' )
e9e5f0fa 823 )
22642690 824 )
825 , '', $color[9], 'width="100%" border="0" cellpadding="1" cellspacing="0"' );
826 echo '</td></tr>';
827 }
6a622b59 828 /* End of message-list table */
a966982b 829
6a622b59 830 do_hook('mailbox_index_after');
831 echo "</FORM>\n";
a966982b 832}
833
e9e5f0fa 834function printHeader($mailbox, $sort, $color, $showsort=true) {
6a622b59 835 global $index_order;
1b91be0c 836 echo html_tag( 'tr' ,'' , 'center', $color[5] );
e4b5f9d1 837
838 /* calculate the width of the subject column based on the
839 * widths of the other columns */
840 $widths = array(1=>1,2=>25,3=>5,4=>0,5=>1,6=>5);
841 $subjectwidth = 100;
842 foreach($index_order as $item) {
843 $subjectwidth -= $widths[$item];
844 }
845
846 foreach ($index_order as $item) {
847 switch ($item) {
8008456a 848 case 1: /* checkbox */
849 case 5: /* flags */
1b91be0c 850 echo html_tag( 'td' ,'&nbsp;' , '', '', 'width="1%"' );
8008456a 851 break;
852 case 2: /* from */
853 if (handleAsSent($mailbox)) {
1b91be0c 854 echo html_tag( 'td' ,'' , 'left', '', 'width="25%"' )
6a622b59 855 . '<b>' . _("To") . '</b>';
8008456a 856 } else {
1b91be0c 857 echo html_tag( 'td' ,'' , 'left', '', 'width="25%"' )
6a622b59 858 . '<b>' . _("From") . '</b>';
8008456a 859 }
a966982b 860 if ($showsort) {
1b91be0c 861 ShowSortButton($sort, $mailbox, 2, 3);
8008456a 862 }
1b91be0c 863 echo "</td>\n";
8008456a 864 break;
865 case 3: /* date */
1b91be0c 866 echo html_tag( 'td' ,'' , 'left', '', 'width="5%" nowrap' )
6a622b59 867 . '<b>' . _("Date") . '</b>';
a966982b 868 if ($showsort) {
1b91be0c 869 ShowSortButton($sort, $mailbox, 0, 1);
8008456a 870 }
1b91be0c 871 echo "</td>\n";
8008456a 872 break;
a966982b 873 case 4: /* subject */
e4b5f9d1 874 echo html_tag( 'td' ,'' , 'left', '', 'width="'.$subjectwidth.'%"' )
6a622b59 875 . '<b>' . _("Subject") . '</b>';
a966982b 876 if ($showsort) {
1b91be0c 877 ShowSortButton($sort, $mailbox, 4, 5);
8008456a 878 }
1b91be0c 879 echo "</td>\n";
8008456a 880 break;
881 case 6: /* size */
e842b215 882 echo html_tag( 'td', '<b>' . _("Size") . '</b>', 'center', '', 'width="5%" nowrap' );
8008456a 883 break;
884 }
bdfb67f8 885 }
1b91be0c 886 echo "</tr>\n";
bdfb67f8 887}
888
e9e5f0fa 889
bdfb67f8 890/*
e35045b7 891 * This function shows the sort button. Isn't this a good comment?
892 */
893function ShowSortButton($sort, $mailbox, $Up, $Down ) {
6a622b59 894 global $PHP_SELF;
895 /* Figure out which image we want to use. */
896 if ($sort != $Up && $sort != $Down) {
897 $img = 'sort_none.png';
898 $which = $Up;
899 } elseif ($sort == $Up) {
900 $img = 'up_pointer.png';
901 $which = $Down;
6c930ade 902 } else {
6a622b59 903 $img = 'down_pointer.png';
904 $which = 6;
e35045b7 905 }
6a622b59 906
907 if (preg_match('/^(.+)\?.+$/',$PHP_SELF,$regs)) {
908 $source_url = $regs[1];
e35045b7 909 } else {
6a622b59 910 $source_url = $PHP_SELF;
6c930ade 911 }
6a622b59 912
913 /* Now that we have everything figured out, show the actual button. */
914 echo ' <a href="' . $source_url .'?newsort=' . $which
915 . '&amp;startMessage=1&amp;mailbox=' . urlencode($mailbox)
da9a8410 916 . '"><img src="../images/' . $img
917 . '" border="0" width="12" height="10" alt="sort" title="'
918 . _("Click here to change the sorting of the message list") .'"></a>';
6a622b59 919}
920
921function get_selectall_link($start_msg, $sort) {
922 global $checkall, $what, $where, $mailbox, $javascript_on;
923 global $PHP_SELF, $PG_SHOWNUM;
924
925 $result = '';
926 if ($javascript_on) {
3027826a 927 $safe_name = preg_replace("/[^0-9A-Za-z_]/", '_', $mailbox);
928 $func_name = "CheckAll" . $safe_name;
929 $form_name = "FormMsgs" . $safe_name;
6a622b59 930 $result = '<script language="JavaScript" type="text/javascript">'
931 . "\n<!-- \n"
3027826a 932 . "function " . $func_name . "() {\n"
933 . " for (var i = 0; i < document." . $form_name . ".elements.length; i++) {\n"
3962deff 934 . " if(document." . $form_name . ".elements[i].type == 'checkbox' && "
935 . "document." . $form_name . ".elements[i].name != 'bypass_trash'){\n"
3027826a 936 . " document." . $form_name . ".elements[i].checked = "
937 . " !(document." . $form_name . ".elements[i].checked);\n"
6a622b59 938 . " }\n"
939 . " }\n"
940 . "}\n"
941 . "//-->\n"
3027826a 942 . '</script><a href="javascript:void(0)" onClick="' . $func_name . '();">' . _("Toggle All")
943/* . '</script><a href="javascript:' . $func_name . '()">' . _("Toggle All")*/
6a622b59 944 . "</a>\n";
e35045b7 945 } else {
6a622b59 946 if (strpos($PHP_SELF, "?")) {
947 $result .= "<a href=\"$PHP_SELF&amp;mailbox=" . urlencode($mailbox)
948 . "&amp;startMessage=$start_msg&amp;sort=$sort&amp;checkall=";
949 } else {
950 $result .= "<a href=\"$PHP_SELF?mailbox=" . urlencode($mailbox)
951 . "&amp;startMessage=$start_msg&amp;sort=$sort&amp;checkall=";
952 }
953 if (isset($checkall) && $checkall == '1') {
954 $result .= '0';
955 } else {
956 $result .= '1';
957 }
958
959 if (isset($where) && isset($what)) {
960 $result .= '&amp;where=' . urlencode($where)
961 . '&amp;what=' . urlencode($what);
962 }
963 $result .= "\">";
964
965 if (isset($checkall) && ($checkall == '1')) {
966 $result .= _("Unselect All");
967 } else {
968 $result .= _("Select All");
969 }
970 $result .= "</A>\n";
e35045b7 971 }
23d6bd09 972
6a622b59 973 /* Return our final result. */
974 return ($result);
bdfb67f8 975}
1a0e0983 976
6c930ade 977/*
e35045b7 978 * This function computes the "Viewing Messages..." string.
979 */
bdfb67f8 980function get_msgcnt_str($start_msg, $end_msg, $num_msgs) {
6a622b59 981 /* Compute the $msg_cnt_str. */
982 $result = '';
983 if ($start_msg < $end_msg) {
984 $result = sprintf(_("Viewing Messages: <B>%s</B> to <B>%s</B> (%s total)"),
985 $start_msg, $end_msg, $num_msgs);
986 } else if ($start_msg == $end_msg) {
987 $result = sprintf(_("Viewing Message: <B>%s</B> (1 total)"), $start_msg);
988 } else {
989 $result = '<br>';
990 }
991 /* Return our result string. */
992 return ($result);
bdfb67f8 993}
994
6c930ade 995/*
e35045b7 996 * Generate a paginator link.
997 */
6c930ade 998function get_paginator_link($box, $start_msg, $use, $text) {
6a622b59 999 global $PHP_SELF;
e35045b7 1000
6a622b59 1001 $result = "<A HREF=\"right_main.php?use_mailbox_cache=$use"
1002 . "&amp;startMessage=$start_msg&amp;mailbox=$box\" "
1003 . "TARGET=\"right\">$text</A>";
1004 return ($result);
756d0680 1005/*
6a622b59 1006 if (preg_match('/^(.+)\?.+$/',$PHP_SELF,$regs)) {
1007 $source_url = $regs[1];
1008 } else {
1009 $source_url = $PHP_SELF;
1010 }
1011
1012 $result = '<A HREF="'. $source_url . "?use_mailbox_cache=$use"
1013 . "&amp;startMessage=$start_msg&amp;mailbox=$box\" "
1014 . "TARGET=\"right\">$text</A>";
1015 return ($result);
1016*/
7b294953 1017}
1018
6c930ade 1019/*
e35045b7 1020 * This function computes the paginator string.
1021 */
6a622b59 1022function get_paginator_str($box, $start_msg, $end_msg, $num_msgs,
1023 $show_num, $sort) {
1024 global $username, $data_dir, $use_mailbox_cache, $color, $PG_SHOWNUM;
1025
1026 /* Initialize paginator string chunks. */
1027 $prv_str = '';
1028 $nxt_str = '';
1029 $pg_str = '';
1030 $all_str = '';
1031 $tgl_str = '';
1032
1033 $box = urlencode($box);
1034
1035 /* Create simple strings that will be creating the paginator. */
1036 $spc = '&nbsp;'; /* This will be used as a space. */
1037 $sep = '|'; /* This will be used as a seperator. */
1038
1039 /* Get some paginator preference values. */
1040 $pg_sel = getPref($data_dir, $username, 'page_selector', SMPREF_ON);
1041 $pg_max = getPref($data_dir, $username, 'page_selector_max', PG_SEL_MAX);
1042
1043 /* Make sure that our start message number is not too big. */
1044 $start_msg = min($start_msg, $num_msgs);
1045
1046 /* Decide whether or not we will use the mailbox cache. */
1047 /* Not sure why $use_mailbox_cache is even passed in. */
1048 if ($sort == 6) {
1049 $use = 0;
1050 } else {
1051 $use = 1;
1052 }
1053
1054 /* Compute the starting message of the previous and next page group. */
1055 $next_grp = $start_msg + $show_num;
1056 $prev_grp = $start_msg - $show_num;
1057
1058 /* Compute the basic previous and next strings. */
1059 if (($next_grp <= $num_msgs) && ($prev_grp >= 0)) {
1060 $prv_str = get_paginator_link($box, $prev_grp, $use, _("Previous"));
1061 $nxt_str = get_paginator_link($box, $next_grp, $use, _("Next"));
1062 } else if (($next_grp > $num_msgs) && ($prev_grp >= 0)) {
1063 $prv_str = get_paginator_link($box, $prev_grp, $use, _("Previous"));
1064 $nxt_str = "<FONT COLOR=\"$color[9]\">"._("Next")."</FONT>\n";
1065 } else if (($next_grp <= $num_msgs) && ($prev_grp < 0)) {
1066 $prv_str = "<FONT COLOR=\"$color[9]\">"._("Previous") . '</FONT>';
1067 $nxt_str = get_paginator_link($box, $next_grp, $use, _("Next"));
1068 }
1069
1070 /* Page selector block. Following code computes page links. */
1071 if ($pg_sel && ($num_msgs > $show_num)) {
1072 /* Most importantly, what is the current page!!! */
1073 $cur_pg = intval($start_msg / $show_num) + 1;
1074
1075 /* Compute total # of pages and # of paginator page links. */
1076 $tot_pgs = ceil($num_msgs / $show_num); /* Total number of Pages */
1077 $vis_pgs = min($pg_max, $tot_pgs - 1); /* Visible Pages */
1078
1079 /* Compute the size of the four quarters of the page links. */
1080
1081 /* If we can, just show all the pages. */
1082 if (($tot_pgs - 1) <= $pg_max) {
1083 $q1_pgs = $cur_pg - 1;
1084 $q2_pgs = $q3_pgs = 0;
1085 $q4_pgs = $tot_pgs - $cur_pg;
1086
e35045b7 1087 /* Otherwise, compute some magic to choose the four quarters. */
6a622b59 1088 } else {
1089 /*
1090 * Compute the magic base values. Added together,
1091 * these values will always equal to the $pag_pgs.
1092 * NOTE: These are DEFAULT values and do not take
1093 * the current page into account. That is below.
1094 */
1095 $q1_pgs = floor($vis_pgs/4);
1096 $q2_pgs = round($vis_pgs/4, 0);
1097 $q3_pgs = ceil($vis_pgs/4);
1098 $q4_pgs = round(($vis_pgs - $q2_pgs)/3, 0);
1099
1100 /* Adjust if the first quarter contains the current page. */
1101 if (($cur_pg - $q1_pgs) < 1) {
1102 $extra_pgs = ($q1_pgs - ($cur_pg - 1)) + $q2_pgs;
1103 $q1_pgs = $cur_pg - 1;
1104 $q2_pgs = 0;
1105 $q3_pgs += ceil($extra_pgs / 2);
1106 $q4_pgs += floor($extra_pgs / 2);
1107
1108 /* Adjust if the first and second quarters intersect. */
1109 } else if (($cur_pg - $q2_pgs - ceil($q2_pgs/3)) <= $q1_pgs) {
1110 $extra_pgs = $q2_pgs;
176204a5 1111 $extra_pgs -= ceil(($cur_pg - $q1_pgs - 1) * 3/4);
1112 $q2_pgs = ceil(($cur_pg - $q1_pgs - 1) * 3/4);
6a622b59 1113 $q3_pgs += ceil($extra_pgs / 2);
1114 $q4_pgs += floor($extra_pgs / 2);
1115
1116 /* Adjust if the fourth quarter contains the current page. */
1117 } else if (($cur_pg + $q4_pgs) >= $tot_pgs) {
1118 $extra_pgs = ($q4_pgs - ($tot_pgs - $cur_pg)) + $q3_pgs;
1119 $q3_pgs = 0;
1120 $q4_pgs = $tot_pgs - $cur_pg;
1121 $q1_pgs += floor($extra_pgs / 2);
1122 $q2_pgs += ceil($extra_pgs / 2);
1123
1124 /* Adjust if the third and fourth quarter intersect. */
1125 } else if (($cur_pg + $q3_pgs + 1) >= ($tot_pgs - $q4_pgs + 1)) {
1126 $extra_pgs = $q3_pgs;
176204a5 1127 $extra_pgs -= ceil(($tot_pgs - $cur_pg - $q4_pgs) * 3/4);
1128 $q3_pgs = ceil(($tot_pgs - $cur_pg - $q4_pgs) * 3/4);
6a622b59 1129 $q1_pgs += floor($extra_pgs / 2);
1130 $q2_pgs += ceil($extra_pgs / 2);
1131 }
1132 }
e35045b7 1133
6a622b59 1134 /*
1135 * I am leaving this debug code here, commented out, because
1136 * it is a really nice way to see what the above code is doing.
1137 * echo "qts = $q1_pgs/$q2_pgs/$q3_pgs/$q4_pgs = "
1138 * . ($q1_pgs + $q2_pgs + $q3_pgs + $q4_pgs) . '<br>';
1139 */
1140
1141 /* Print out the page links from the compute page quarters. */
1142
1143 /* Start with the first quarter. */
1144 if (($q1_pgs == 0) && ($cur_pg > 1)) {
1145 $pg_str .= "...$spc";
1146 } else {
1147 for ($pg = 1; $pg <= $q1_pgs; ++$pg) {
1148 $start = (($pg-1) * $show_num) + 1;
1149 $pg_str .= get_paginator_link($box, $start, $use, $pg) . $spc;
1150 }
1151 if ($cur_pg - $q2_pgs - $q1_pgs > 1) {
1152 $pg_str .= "...$spc";
1153 }
1154 }
1155
1156 /* Continue with the second quarter. */
1157 for ($pg = $cur_pg - $q2_pgs; $pg < $cur_pg; ++$pg) {
1158 $start = (($pg-1) * $show_num) + 1;
1159 $pg_str .= get_paginator_link($box, $start, $use, $pg) . $spc;
1160 }
1161
1162 /* Now print the current page. */
1163 $pg_str .= $cur_pg . $spc;
1164
1165 /* Next comes the third quarter. */
1166 for ($pg = $cur_pg + 1; $pg <= $cur_pg + $q3_pgs; ++$pg) {
1167 $start = (($pg-1) * $show_num) + 1;
1168 $pg_str .= get_paginator_link($box, $start, $use, $pg) . $spc;
1169 }
1170
1171 /* And last, print the forth quarter page links. */
1172 if (($q4_pgs == 0) && ($cur_pg < $tot_pgs)) {
1173 $pg_str .= "...$spc";
1174 } else {
1175 if (($tot_pgs - $q4_pgs) > ($cur_pg + $q3_pgs)) {
1176 $pg_str .= "...$spc";
1177 }
1178 for ($pg = $tot_pgs - $q4_pgs + 1; $pg <= $tot_pgs; ++$pg) {
1179 $start = (($pg-1) * $show_num) + 1;
1180 $pg_str .= get_paginator_link($box, $start, $use, $pg) . $spc;
1181 }
1182 }
1183 } else if ($PG_SHOWNUM == 999999) {
1184 $pg_str = "<A HREF=\"right_main.php?PG_SHOWALL=0"
1185 . "&amp;use_mailbox_cache=$use&amp;startMessage=1&amp;mailbox=$box\" "
1186 . "TARGET=\"right\">" ._("Paginate") . '</A>' . $spc;
1187 }
1188
1189 /* If necessary, compute the 'show all' string. */
1190 if (($prv_str != '') || ($nxt_str != '')) {
1191 $all_str = "<A HREF=\"right_main.php?PG_SHOWALL=1"
1192 . "&amp;use_mailbox_cache=$use&amp;startMessage=1&amp;mailbox=$box\" "
1193 . "TARGET=\"right\">" . _("Show All") . '</A>';
1194 }
1195
1196 /* Last but not least, get the value for the toggle all link. */
1197 $tgl_str = get_selectall_link($start_msg, $sort);
1198
1199 /* Put all the pieces of the paginator string together. */
1200 /**
1201 * Hairy code... But let's leave it like it is since I am not certain
1202 * a different approach would be any easier to read. ;)
1203 */
1204 $result = '';
1205 $result .= ($prv_str != '' ? $prv_str . $spc . $sep . $spc : '');
1206 $result .= ($nxt_str != '' ? $nxt_str . $spc . $sep . $spc : '');
1207 $result .= ($pg_str != '' ? $pg_str : '');
1208 $result .= ($all_str != '' ? $sep . $spc . $all_str . $spc : '');
1209 $result .= ($result != '' ? $sep . $spc . $tgl_str: $tgl_str);
1210
1211 /* If the resulting string is blank, return a non-breaking space. */
1212 if ($result == '') {
1213 $result = '&nbsp;';
1214 }
e35045b7 1215
6a622b59 1216 /* Return our final magical paginator string. */
1217 return ($result);
bdfb67f8 1218}
1219
eaa4f45f 1220function truncateWithEntities($subject, $trim_at)
1221{
341aa42f 1222 $ent_strlen = strlen($subject);
1223 if (($trim_at <= 0) || ($ent_strlen <= $trim_at))
6a622b59 1224 return $subject;
eaa4f45f 1225
1226 global $languages, $squirrelmail_language;
6a622b59 1227
6a622b59 1228 /*
1229 * see if this is entities-encoded string
1230 * If so, Iterate through the whole string, find out
1231 * the real number of characters, and if more
eaa4f45f 1232 * than $trim_at, substr with an updated trim value.
6a622b59 1233 */
341aa42f 1234 $trim_val = $trim_at;
1235 $ent_offset = 0;
1236 $ent_loc = 0;
a1440f89 1237 while ( $ent_loc < $trim_val && (($ent_loc = strpos($subject, '&', $ent_offset)) !== false) &&
1238 (($ent_loc_end = strpos($subject, ';', $ent_loc+3)) !== false) ) {
1239 $trim_val += ($ent_loc_end-$ent_loc);
6a622b59 1240 $ent_offset = $ent_loc_end+1;
1241 }
341aa42f 1242 if (($trim_val > $trim_at) && ($ent_strlen > $trim_val) && (strpos($subject,';',$trim_val) < ($trim_val + 6))) {
a1440f89 1243 $i = strpos($subject,';',$trim_val);
1244 if ($i) {
1245 $trim_val = strpos($subject,';',$trim_val);
1246 }
46a49f65 1247 }
341aa42f 1248 // only print '...' when we're actually dropping part of the subject
1249 if ($ent_strlen <= $trim_val)
6a622b59 1250 return $subject;
6a622b59 1251
1252 if (isset($languages[$squirrelmail_language]['XTRA_CODE']) &&
1253 function_exists($languages[$squirrelmail_language]['XTRA_CODE'])) {
1254 return $languages[$squirrelmail_language]['XTRA_CODE']('strimwidth', $subject, $trim_val);
1255 }
ecaf6352 1256
341aa42f 1257 return substr_replace($subject, '...', $trim_val);
bdfb67f8 1258}
f93c93b9 1259
eaa4f45f 1260function processSubject($subject, $threadlevel = 0) {
1261 /* Shouldn't ever happen -- caught too many times in the IMAP functions */
1262 if ($subject == '') {
1263 return _("(no subject)");
1264 }
1265
3fc1a95f 1266 global $truncate_subject; /* number of characters for Subject field (<= 0 for unchanged) */
1267 $trim_at = $truncate_subject;
eaa4f45f 1268
1269 /* if this is threaded, subtract two chars per indentlevel */
1270 if (($threadlevel > 0) && ($threadlevel <= 10))
1271 $trim_at -= (2*$threadlevel);
1272
1273 return truncateWithEntities($subject, $trim_at);
1274}
1275
26454147 1276function getMbxList($imapConnection, $boxes = 0) {
6a622b59 1277 global $lastTargetMailbox;
1278 echo ' <small>&nbsp;<tt><select name="targetMailbox">';
26454147 1279 echo sqimap_mailbox_option_list($imapConnection, array(strtolower($lastTargetMailbox)), 0, $boxes);
6a622b59 1280 echo ' </SELECT></TT>&nbsp;';
e9e5f0fa 1281}
1282
1283function getButton($type, $name, $value) {
6a622b59 1284 return '<INPUT TYPE="'.$type.'" NAME="'.$name.'" VALUE="'.$value . '">';
e9e5f0fa 1285}
1286
1287function getSmallStringCell($string, $align) {
6a622b59 1288 return html_tag('td',
1289 '<small>' . $string . ':&nbsp; </small>',
1290 $align,
1291 '',
1292 'nowrap' );
e9e5f0fa 1293}
1294
1295function getEndMessage($start_msg, $show_num, $num_msgs) {
6a622b59 1296 if ($start_msg + ($show_num - 1) < $num_msgs){
1297 $end_msg = $start_msg + ($show_num - 1);
1298 } else {
1299 $end_msg = $num_msgs;
1300 }
e9e5f0fa 1301
6a622b59 1302 if ($end_msg < $start_msg) {
1303 $start_msg = $start_msg - $show_num;
1304 if ($start_msg < 1) {
1305 $start_msg = 1;
1306 }
e9e5f0fa 1307 }
6a622b59 1308 return (array($start_msg,$end_msg));
e9e5f0fa 1309}
1310
1ae2832e 1311// This should go in imap_mailbox.php
a3439b27 1312function handleAsSent($mailbox) {
6a8e7cae 1313 global $handleAsSent_result;
1314
6a622b59 1315 /* First check if this is the sent or draft folder. */
6a8e7cae 1316 $handleAsSent_result = isSentMailbox($mailbox) || isDraftMailbox($mailbox);
a3439b27 1317
6a622b59 1318 /* Then check the result of the handleAsSent hook. */
1319 do_hook('check_handleAsSent_result', $mailbox);
a3439b27 1320
6a622b59 1321 /* And return the result. */
6a8e7cae 1322 return $handleAsSent_result;
6a622b59 1323}
e842b215 1324
937c81fa 1325?>