6 * Copyright (c) 1999-2002 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
9 * This file is used for reading the msgs array and displaying
10 * the resulting emails in the right frame.
15 require_once('../src/validate.php');
16 require_once('../functions/imap.php');
17 require_once('../functions/mime.php');
18 require_once('../functions/date.php');
19 require_once('../functions/url_parser.php');
22 * Given an IMAP message id number, this will look it up in the cached
23 * and sorted msgs array and return the index. Used for finding the next
24 * and previous messages.
26 * returns the index of the next valid message from the array
28 function findNextMessage() {
29 global $msort, $currentArrayIndex, $msgs, $sort;
33 if ($currentArrayIndex != 1) {
34 $result = $currentArrayIndex - 1;
37 for (reset($msort); ($key = key($msort)), (isset($key)); next($msort)) {
38 if ($currentArrayIndex == $msgs[$key]['ID']) {
42 $result = $msgs[$key]['ID'];
50 /** Removes just one address from the list of addresses. */
51 function RemoveAddress(&$addr_list, $addr) {
53 foreach (array_keys($addr_list, $addr) as $key_to_delete) {
54 unset($addr_list[$key_to_delete]);
59 /** returns the index of the previous message from the array. */
60 function findPreviousMessage() {
61 global $msort, $currentArrayIndex, $sort, $msgs, $imapConnection;
62 global $mailbox, $data_dir, $username;
66 $numMessages = sqimap_get_num_messages($imapConnection, $mailbox);
67 if ($currentArrayIndex != $numMessages) {
68 $result = $currentArrayIndex +
1;
71 for (reset($msort); ($key = key($msort)), (isset($key)); next($msort)) {
72 if ($currentArrayIndex == $msgs[$key]['ID']) {
76 $result = $msgs[$key]['ID'];
86 * Displays a link to a page where the message is displayed more
89 function printer_friendly_link() {
90 global $passed_id, $mailbox, $ent_num, $color;
91 global $pf_subtle_link;
92 global $javascript_on;
94 if (strlen(trim($mailbox)) < 1) {
98 $params = '?passed_ent_id=' . $ent_num;
99 $params .= '&mailbox=' . urlencode($mailbox);
100 $params .= '&passed_id=' . $passed_id;
102 $print_text = _("View Printable Version");
104 if (!$pf_subtle_link) {
105 /* The link is large, on the bottom of the header panel. */
106 $result = ' <tr bgcolor="' . $color[0] . '">' . "\n" .
107 ' <td class="medText" align="right" valign="top">' . "\n" .
109 ' </td><td class="medText" valign="top" colspan="2">'."\n";
111 /* The link is subtle, below "view full header". */
115 /* Output the link. */
116 if ($javascript_on) {
117 $result .= '<script language="javascript">' . "\n" .
119 " function printFormat() {\n" .
120 ' window.open("../src/printer_friendly_main.php' .
121 $params . '","Print","width=800,height=600");' . "\n".
125 "<A HREF=\"javascript:printFormat();\">$print_text</A>\n";
127 $result .= '<A TARGET="_blank" HREF="../src/printer_friendly_bottom.php' .
128 "$params\">$print_text</A>\n";
131 if (!$pf_subtle_link) {
132 /* The link is large, on the bottom of the header panel. */
133 $result .= ' </td>' . "\n" .
140 /*****************************/
141 /*** Main of read_boby.php ***/
142 /*****************************/
144 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
145 sqimap_mailbox_select($imapConnection, $mailbox);
147 displayPageHeader($color, $mailbox);
149 if (isset($view_hdr)) {
150 $read = sqimap_run_command ($imapConnection, "FETCH $passed_id BODY[HEADER]", true, $a, $b);
153 '<TABLE WIDTH="100%" CELLPADDING="2" CELLSPACING="0" BORDER="0" ALIGN="CENTER">' . "\n" .
154 " <TR><TD BGCOLOR=\"$color[9]\" WIDTH=\"100%\"><CENTER><B>" . _("Viewing Full Header") . '</B> - '.
155 '<a href="' . $base_uri . "src/read_body.php?mailbox=".urlencode($mailbox);
156 if (isset($where) && isset($what)) {
157 // Got here from a search
158 echo "&passed_id=$passed_id&where=".urlencode($where)."&what=".urlencode($what).'">';
160 echo "&passed_id=$passed_id&startMessage=$startMessage&show_more=$show_more\">";
162 echo ''._("View message") . "</a></b></center></td></tr></table>\n" .
163 "<table width=\"99%\" cellpadding=2 cellspacing=0 border=0 align=center>\n" .
167 for ($i=1; $i < count($read); $i++
) {
168 $line = htmlspecialchars($read[$i]);
169 if (eregi("^>", $line)) {
171 $first[$i] = ' ';
173 } else if (eregi("^[ |\t]", $line)) {
176 } else if (eregi("^([^:]+):(.+)", $line, $regs)) {
177 $first[$i] = $regs[1] . ':';
178 $second[$i] = $regs[2];
181 $second[$i] = trim($line);
185 for ($i=0; $i < count($second); $i = $j) {
186 if (isset($first[$i])) {
189 if (isset($second[$i])) {
190 $s = nl2br($second[$i]);
193 while (($first[$j] == '') && ($j < count($first))) {
194 $s .= ' ' . nl2br($second[$j]);
198 if (isset($f)) echo "<nobr><tt><b>$f</b>$s</tt></nobr>";
200 echo "</td></tr></table>\n";
201 echo '</body></html>';
202 sqimap_logout($imapConnection);
207 $currentArrayIndex = $passed_id;
209 $currentArrayIndex = -1;
212 for ($i = 0; $i < count($msgs); $i++
) {
213 if ($msgs[$i]['ID'] == $passed_id) {
214 $msgs[$i]['FLAG_SEEN'] = true;
218 // $message contains all information about the message
219 // including header and body
220 $message = sqimap_get_message($imapConnection, $passed_id, $mailbox);
222 /** translate the subject and mailbox into url-able text **/
223 $url_subj = urlencode(trim($message->header
->subject
));
224 $urlMailbox = urlencode($mailbox);
226 if (isset($message->header
->replyto
)) {
227 $url_replyto = urlencode($message->header
->replyto
);
230 $url_replytoall = $url_replyto;
232 // If we are replying to all, then find all other addresses and
233 // add them to the list. Remove duplicates.
234 // This is somewhat messy, so I'll explain:
235 // 1) Take all addresses (from, to, cc) (avoid nasty join errors here)
236 $url_replytoall_extra_addrs = array_merge(
237 array($message->header
->from
),
238 $message->header
->to
,
242 // 2) Make one big string out of them
243 $url_replytoall_extra_addrs = join(';', $url_replytoall_extra_addrs);
245 // 3) Parse that into an array of addresses
246 $url_replytoall_extra_addrs = parseAddrs($url_replytoall_extra_addrs);
248 // 4) Make them unique -- weed out duplicates
249 // (Coded for PHP 4.0.0)
250 $url_replytoall_extra_addrs =
251 array_keys(array_flip($url_replytoall_extra_addrs));
253 // 5) Remove the addresses we'll be sending the message 'to'
254 $url_replytoall_avoid_addrs = '';
255 if (isset($message->header
->replyto
)) {
256 $url_replytoall_avoid_addrs = $message->header
->replyto
;
259 $url_replytoall_avoid_addrs = parseAddrs($url_replytoall_avoid_addrs);
260 foreach ($url_replytoall_avoid_addrs as $addr) {
261 RemoveAddress($url_replytoall_extra_addrs, $addr);
264 // 6) Remove our identities from the CC list (they still can be in the
265 // TO list) only if $include_self_reply_all is turned off
266 if (!$include_self_reply_all) {
267 RemoveAddress($url_replytoall_extra_addrs,
268 getPref($data_dir, $username, 'email_address'));
269 $idents = getPref($data_dir, $username, 'identities');
270 if ($idents != '' && $idents > 1) {
271 for ($i = 1; $i < $idents; $i ++
) {
272 $cur_email_address = getPref($data_dir, $username, 'email_address' . $i);
273 RemoveAddress($url_replytoall_extra_addrs, $cur_email_address);
278 // 7) Smoosh back into one nice line
279 $url_replytoallcc = getLineOfAddrs($url_replytoall_extra_addrs);
282 $url_replytoallcc = urlencode($url_replytoallcc);
284 $dateString = getLongDateString($message->header
->date
);
286 // What do we reply to -- text only, if possible
287 $ent_num = findDisplayEntity($message);
289 /** TEXT STRINGS DEFINITIONS **/
290 $echo_more = _("more");
291 $echo_less = _("less");
293 if (!isset($show_more_cc)) $show_more_cc = false;
295 /** FORMAT THE TO STRING **/
298 $to_ary = $message->header
->to
;
299 while ($i < count($to_ary)) {
300 $to_ary[$i] = htmlspecialchars(decodeHeader($to_ary[$i]));
303 $to_string = "$to_string<BR>$to_ary[$i]";
305 $to_string = "$to_ary[$i]";
309 if (count($to_ary) > 1) {
310 if ($show_more == false) {
312 /* From a search... */
313 $to_string .= ' (<A HREF="' . $base_uri .
314 "src/read_body.php?mailbox=$urlMailbox&passed_id=$passed_id&";
315 if (isset($where) && isset($what)) {
316 $to_string .= 'where='.urlencode($where)."&what=".urlencode($what)."&show_more=1&show_more_cc=$show_more_cc\">$echo_more</A>)";
318 $to_string .= "sort=$sort&startMessage=$startMessage&show_more=1&show_more_cc=$show_more_cc\">$echo_more</A>)";
322 } else if ($i == 1) {
323 /* From a search... */
324 $to_string .= ' (<A HREF="' . $base_uri .
325 "src/read_body.php?mailbox=$urlMailbox&passed_id=$passed_id&";
326 if (isset($where) && isset($what)) {
327 $to_string .= 'where='.urlencode($where)."&what=".urlencode($what)."&show_more=0&show_more_cc=$show_more_cc\">$echo_less</A>)";
329 $to_string .= "sort=$sort&startMessage=$startMessage&show_more=0&show_more_cc=$show_more_cc\">$echo_less</A>)";
335 /** FORMAT THE CC STRING **/
337 if (isset ($message->header
->cc
[0]) && trim($message->header
->cc
[0])) {
339 $cc_ary = $message->header
->cc
;
340 while ($i < count(decodeHeader($cc_ary))) {
341 $cc_ary[$i] = htmlspecialchars($cc_ary[$i]);
343 $cc_string = "$cc_string<BR>$cc_ary[$i]";
345 $cc_string = "$cc_ary[$i]";
349 if (count($cc_ary) > 1) {
350 if ($show_more_cc == false) {
352 /* From a search... */
353 $cc_string .= ' (<A HREF="' . $base_uri .
354 "src/read_body.php?mailbox=$urlMailbox&passed_id=$passed_id";
355 if (isset($where) && isset($what)) {
356 $cc_string .= '&what='.urlencode($what)."&where=".urlencode($where)."&show_more_cc=1&show_more=$show_more\">$echo_more</A>)";
358 $cc_string .= "&sort=$sort&startMessage=$startMessage&show_more_cc=1&show_more=$show_more\">$echo_more</A>)";
362 } else if ($i == 1) {
363 /* From a search... */
364 $cc_string .= ' (<A HREF="' . $base_uri .
365 "src/read_body.php?mailbox=$urlMailbox&passed_id=$passed_id&";
366 if (isset($where) && isset($what)) {
367 $cc_string .= 'what=' . urlencode($what)."&where=".urlencode($where)."&show_more_cc=0&show_more=$show_more\">$echo_less</A>)";
369 $cc_string .= "sort=$sort&startMessage=$startMessage&show_more_cc=0&show_more=$show_more\">$echo_less</A>)";
376 /** FORMAT THE BCC STRING **/
378 if (isset ($message->header
->bcc
[0]) && trim($message->header
->bcc
[0])){
380 $bcc_ary = $message->header
->bcc
;
381 while ($i < count(decodeHeader($bcc_ary))) {
382 $bcc_ary[$i] = htmlspecialchars($bcc_ary[$i]);
384 $bcc_string = "$bcc_string<BR>$bcc_ary[$i]";
386 $bcc_string = "$bcc_ary[$i]";
390 if (count($bcc_ary) > 1) {
391 if ($show_more_cc == false) {
393 /* From a search... */
394 $bcc_string .= ' (<A HREF="' . $base_uri .
395 "src/read_body.php?mailbox=$urlMailbox&passed_id=$passed_id&";
396 if (isset($where) && isset($what)) {
397 $bcc_string .= 'what=' . urlencode($what)."&where=".urlencode($where)."&show_more_cc=1&show_more=$show_more\">$echo_more</A>)";
399 $bcc_string .= "sort=$sort&startMessage=$startMessage&show_more_cc=1&show_more=$show_more\">$echo_more</A>)";
401 $i = count($bcc_ary);
403 } else if ($i == 1) {
404 /* From a search... */
405 $bcc_string .= ' (<A HREF="' . $base_uri .
406 "src/read_body.php?mailbox=$urlMailbox&passed_id=$passed_id&";
407 if (isset($where) && isset($what)) {
408 $bcc_string .= 'what=' . urlencode($what)."&where=".urlencode($where)."&show_more_cc=0&show_more=$show_more\">$echo_less</A>)";
410 $bcc_string .= "sort=$sort&startMessage=$startMessage&show_more_cc=0&show_more=$show_more\">$echo_less</A>)";
417 if ($default_use_priority) {
418 $priority_level = substr($message->header
->priority
,0,1);
420 switch($priority_level) {
421 /* check for a higher then normal priority. */
424 $priority_string = _("High");
427 /* check for a lower then normal priority. */
430 $priority_string = _("Low");
433 /* check for a normal priority. */
436 $priority_level = '3';
437 $priority_string = _("Normal");
443 /** make sure everything will display in HTML format **/
444 $from_name = decodeHeader(htmlspecialchars($message->header
->from
));
445 $subject = decodeHeader(htmlspecialchars($message->header
->subject
));
447 do_hook('read_body_top');
449 '<TABLE CELLSPACING="0" WIDTH="100%" BORDER="0" ALIGN="CENTER" CELLPADDING="0">' . "\n" .
450 ' <TR><TD BGCOLOR="' . $color[9] . '" WIDTH="100%">' . "\n" .
451 ' <TABLE WIDTH="100%" CELLSPACING="0" BORDER="0" CELLPADDING="3">' . "\n" .
453 ' <TD ALIGN="LEFT" WIDTH="33%">' . "\n" .
455 '<A HREF="' . $base_uri . 'src/';
457 if ($where && $what) {
461 echo "search.php?where$pos=".urlencode($where)."&pos=$pos&what$pos=".urlencode($what)."&mailbox=$urlMailbox\">";
463 echo "right_main.php?sort=$sort&startMessage=$startMessage&mailbox=$urlMailbox\">";
465 echo _("Message List") .
466 '</A> | ' .
467 '<A HREF="' . $base_uri . "src/delete_message.php?mailbox=$urlMailbox&message=$passed_id&";
468 if ($where && $what) {
469 echo 'where=' . urlencode($where) . '&what=' . urlencode($what) . '">';
471 echo "sort=$sort&startMessage=$startMessage\">";
473 echo _("Delete") . '</A> ';
474 if (($mailbox == $draft_folder) && ($save_as_draft)) {
475 echo '| <A HREF="' . $base_uri .
476 "src/compose.php?mailbox=$mailbox&send_to=$to_string&send_to_cc=$cc_string&send_to_bcc=$bcc_string&subject=$url_subj&draft_id=$passed_id&ent_num=$ent_num\">".
477 _("Resume Draft") . '</a>';
480 echo ' ' .
483 ' <TD WIDTH="33%" ALIGN="CENTER">' . "\n" .
486 if ( !($where && $what) ) {
488 if ($currentArrayIndex == -1) {
489 echo 'Previous | Next';
491 $prev = findPreviousMessage();
492 $next = findNextMessage();
495 echo '<a href="' . $base_uri . "src/read_body.php?passed_id=$prev&mailbox=$urlMailbox&sort=$sort&startMessage=$startMessage&show_more=0\">" . _("Previous") . "</A> | ";
497 echo _("Previous") . ' | ';
501 echo '<a href="' . $base_uri . "src/read_body.php?passed_id=$next&mailbox=$urlMailbox&sort=$sort&startMessage=$startMessage&show_more=0\">" . _("Next") . "</A>";
508 echo ' </SMALL>' . "\n" .
509 ' </TD><TD WIDTH="33%" ALIGN="RIGHT">' .
511 ' <A HREF="' . $base_uri . "src/compose.php?forward_id=$passed_id&forward_subj=$url_subj&".
512 ($default_use_priority?
"mailprio=$priority_level&":"")
513 ."mailbox=$urlMailbox&ent_num=$ent_num\">" .
515 '</A> | ' .
516 ' <A HREF="' . $base_uri . "src/compose.php?send_to=$url_replyto&reply_subj=$url_subj&".
517 ($default_use_priority?
"mailprio=$priority_level&":"").
518 "reply_id=$passed_id&mailbox=$urlMailbox&ent_num=$ent_num\">" .
520 '</A> | ' .
521 ' <A HREF="' . $base_uri . "src/compose.php?send_to=$url_replytoall&send_to_cc=$url_replytoallcc&reply_subj=$url_subj&".
522 ($default_use_priority?
"mailprio=$priority_level&":"").
523 "reply_id=$passed_id&mailbox=$urlMailbox&ent_num=$ent_num\">" .
531 ' <TR><TD CELLSPACING="0" WIDTH="100%">' .
532 ' <TABLE WIDTH="100%" BORDER="0" CELLSPACING="0" CELLPADDING="3">' . "\n" .
536 echo " <TD BGCOLOR=\"$color[0]\" WIDTH=\"10%\" ALIGN=\"right\" VALIGN=\"top\">\n" .
538 " </TD><TD BGCOLOR=\"$color[0]\" WIDTH=\"80%\" VALIGN=\"top\">\n" .
539 " <B>$subject</B> \n" .
541 ' <TD ROWSPAN="4" width="10%" BGCOLOR="' . $color[0] .
542 '" ALIGN=right VALIGN=top NOWRAP><small>'.
543 '<A HREF="' . $base_uri . "src/read_body.php?mailbox=$urlMailbox&passed_id=$passed_id&";
545 /* From a search... */
546 if ($where && $what) {
547 echo 'where=' . urlencode($where) . '&what=' . urlencode($what) .
548 "&view_hdr=1\">" . _("View Full Header") . "</A>\n";
550 echo "startMessage=$startMessage&show_more=$show_more&view_hdr=1\">" .
551 _("View Full Header") . "</A>\n";
554 /* Output the printer friendly link if we are in subtle mode. */
555 if ($pf_subtle_link) {
556 echo printer_friendly_link(true);
559 do_hook("read_body_header_right");
560 echo '</small></TD>' . "\n" .
564 echo ' <TR>' . "\n" .
565 ' <TD BGCOLOR="' . $color[0] . '" ALIGN="RIGHT">' . "\n" .
567 ' </TD><TD BGCOLOR="' . $color[0] . '">' . "\n" .
568 " <B>$from_name</B> \n" .
572 echo ' <TR>' . "\n" .
573 ' <TD BGCOLOR="' . $color[0] . '" ALIGN="RIGHT">' . "\n" .
575 " </TD><TD BGCOLOR=\"$color[0]\">\n" .
576 " <B>$dateString</B> \n" .
582 " <TD BGCOLOR=\"$color[0]\" ALIGN=RIGHT VALIGN=TOP>\n" .
584 ' </TD><TD BGCOLOR="' . $color[0] . '" VALIGN="TOP">' . "\n" .
585 " <B>$to_string</B> \n" .
589 if (isset($cc_string)) {
591 " <TD BGCOLOR=\"$color[0]\" ALIGN=RIGHT VALIGN=TOP>\n" .
593 " </TD><TD BGCOLOR=\"$color[0]\" VALIGN=TOP colspan=2>\n" .
594 " <B>$cc_string</B> \n" .
600 if (isset($bcc_string)) {
602 " <TD BGCOLOR=\"$color[0]\" ALIGN=RIGHT VALIGN=TOP>\n" .
604 " </TD><TD BGCOLOR=\"$color[0]\" VALIGN=TOP colspan=2>\n" .
605 " <B>$bcc_string</B> \n" .
609 if ($default_use_priority) {
610 if (isset($priority_string)) {
612 " <TD BGCOLOR=\"$color[0]\" ALIGN=RIGHT VALIGN=TOP>\n" .
613 " "._("Priority").": \n".
614 " </TD><TD BGCOLOR=\"$color[0]\" VALIGN=TOP colspan=2>\n" .
615 " <B>$priority_string</B> \n" .
621 if ($show_xmailer_default) {
622 $read = sqimap_run_command ($imapConnection, "FETCH $passed_id BODY.PEEK[HEADER.FIELDS (X-Mailer User-Agent)]", true,
623 $response, $readmessage);
624 $mailer = substr($read[1], strpos($read[1], " "));
627 " <TD BGCOLOR=\"$color[0]\" ALIGN=RIGHT VALIGN=TOP>\n" .
628 " "._("Mailer").": \n".
629 " </TD><TD BGCOLOR=\"$color[0]\" VALIGN=TOP colspan=2>\n" .
630 " <B>$mailer</B> \n" .
636 /* Output the printer friendly link if we are not in subtle mode. */
637 if (!$pf_subtle_link) {
638 echo printer_friendly_link(true);
641 do_hook("read_body_header");
646 echo "<TABLE CELLSPACING=0 WIDTH=\"97%\" BORDER=0 ALIGN=CENTER CELLPADDING=0>\n" .
647 " <TR><TD BGCOLOR=\"$color[4]\" WIDTH=\"100%\">\n" .
649 formatBody($imapConnection, $message, $color, $wrap_at).
651 '<TABLE CELLSPACING="0" WIDTH="100%" BORDER="0" ALIGN="CENTER" CELLPADDING="0">' . "\n" .
652 " <TR><TD BGCOLOR=\"$color[9]\"> </TD></TR>" .
655 /* show attached images inline -- if pref'fed so */
656 if (($attachment_common_show_images) and
657 is_array($attachment_common_show_images_list)) {
658 foreach ($attachment_common_show_images_list as $img) {
659 echo "<TABLE BORDER=0 CELLSPACING=0 CELLPADDING=2 ALIGN=CENTER>\n" .
662 ' <img src="../src/download.php' .
663 '?passed_id=' . urlencode($img['passed_id']) .
664 '&mailbox=' . urlencode($img['mailbox']) .
665 '&passed_ent_id=' . urlencode($img['ent_id']) .
666 '&absolute_dl=true">' . "\n" .
673 do_hook('read_body_bottom');
674 do_hook('html_bottom');
675 sqimap_logout($imapConnection);