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