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