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