6 ** Copyright (c) 1999-2001 The SquirrelMail Development 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');
21 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
22 sqimap_mailbox_select($imapConnection, $mailbox);
24 displayPageHeader($color, $mailbox);
26 if (isset($view_hdr)) {
27 fputs ($imapConnection, sqimap_session_id() . " FETCH $passed_id BODY[HEADER]\r\n");
28 $read = sqimap_read_data ($imapConnection, sqimap_session_id(), true, $a, $b);
31 '<TABLE WIDTH="100%" CELLPADDING="2" CELLSPACING="0" BORDER="0" ALIGN="CENTER">' . "\n" .
32 " <TR><TD BGCOLOR=\"$color[9]\" WIDTH=\"100%\"><CENTER><B>" . _("Viewing Full Header") . '</B> - ';
33 if (isset($where) && isset($what)) {
34 // Got here from a search
35 echo "<a href=\"read_body.php?mailbox=".urlencode($mailbox)."&passed_id=$passed_id&where=".urlencode($where)."&what=".urlencode($what).'">';
37 echo "<a href=\"read_body.php?mailbox=".urlencode($mailbox)."&passed_id=$passed_id&startMessage=$startMessage&show_more=$show_more\">";
39 echo ''._("View message") . "</a></b></center></td></tr></table>\n" .
40 "<table width=99% cellpadding=2 cellspacing=0 border=0 align=center>\n" .
44 for ($i=1; $i < count($read); $i++
) {
45 $line = htmlspecialchars($read[$i]);
46 if (eregi("^>", $line)) {
48 $first[$i] = ' ';
50 } else if (eregi("^[ |\t]", $line)) {
53 } else if (eregi("^([^:]+):(.+)", $line, $regs)) {
54 $first[$i] = $regs[1] . ':';
55 $second[$i] = $regs[2];
58 $second[$i] = trim($line);
62 for ($i=0; $i < count($second); $i = $j) {
63 if (isset($first[$i])) {
66 if (isset($second[$i])) {
67 $s = nl2br($second[$i]);
70 while (($first[$j] == '') && ($j < count($first))) {
71 $s .= ' ' . nl2br($second[$j]);
75 if (isset($f)) echo "<nobr><tt><b>$f</b>$s</tt></nobr>";
77 echo "</td></tr></table>\n";
78 echo '</body></html>';
79 sqimap_logout($imapConnection);
84 * Given an IMAP message id number, this will look it up in the cached
85 * and sorted msgs array and return the index. Used for finding the next
86 * and previous messages.
88 * returns the index of the next valid message from the array
90 function findNextMessage() {
91 global $msort, $currentArrayIndex, $msgs, $sort;
95 if ($currentArrayIndex != 1) {
96 $result = $currentArrayIndex - 1;
99 for (reset($msort); ($key = key($msort)), (isset($key)); next($msort)) {
100 if ($currentArrayIndex == $msgs[$key]['ID']) {
104 $result = $msgs[$key]['ID'];
112 /** Removes just one address from the list of addresses. */
113 function RemoveAddress(&$addr_list, $addr) {
115 foreach (array_keys($addr_list, $addr) as $key_to_delete) {
116 unset($addr_list[$key_to_delete]);
121 /** returns the index of the previous message from the array. */
122 function findPreviousMessage() {
123 global $msort, $currentArrayIndex, $sort, $msgs, $imapConnection;
124 global $mailbox, $data_dir, $username;
128 $numMessages = sqimap_get_num_messages($imapConnection, $mailbox);
129 if ($currentArrayIndex != $numMessages) {
130 $result = $currentArrayIndex +
1;
133 for (reset($msort); ($key = key($msort)), (isset($key)); next($msort)) {
134 if ($currentArrayIndex == $msgs[$key]['ID']) {
138 $result = $msgs[$key]['ID'];
148 * Displays a link to a page where the message is displayed more
149 * "printer friendly".
151 function printer_friendly_link() {
152 global $passed_id, $mailbox, $ent_num, $color;
153 global $pf_subtle_link;
154 global $javascript_on;
156 if (strlen(trim($mailbox)) < 1) {
160 $params = '?passed_ent_id=' . $ent_num;
161 $params .= '&mailbox=' . urlencode($mailbox);
162 $params .= '&passed_id=' . $passed_id;
164 $print_text = _("View Printable Version");
166 if (!$pf_subtle_link) {
167 /* The link is large, on the bottom of the header panel. */
168 $result = ' <tr bgcolor="' . $color[0] . '">' . "\n" .
169 ' <td class="medText" align="right" valign="top">' . "\n" .
171 ' </td><td class="medText" valign="top" colspan="2">'."\n";
173 /* The link is subtle, below "view full header". */
177 /* Output the link. */
178 if ($javascript_on) {
179 $result .= '<script language="javascript">' . "\n" .
181 " function printFormat() {\n" .
182 ' window.open("../src/printer_friendly_main.php' .
183 $params . '","Print","width=800,height=600");' . "\n".
187 "<A HREF=\"javascript:printFormat();\">$print_text</A>\n";
189 $result .= '<A TARGET="_blank" HREF="../src/printer_friendly_bottom.php' .
190 "$params\">$print_text</A>\n";
193 if (!$pf_subtle_link) {
194 /* The link is large, on the bottom of the header panel. */
195 $result .= ' </td>' . "\n" .
202 /*****************************/
203 /*** Main of read_boby.php ***/
204 /*****************************/
207 $currentArrayIndex = $passed_id;
208 /*** START OF COMMENTED OUT CODE - PHILIPPE, CAN THIS BE REMOVED?
209 for ($i=0; $i < count($msgs); $i++) {
210 if ($msgs[$i]["ID"] == $passed_id) {
211 $currentArrayIndex = $i;
215 *** END OF COMMENTED OUT CODE */
217 $currentArrayIndex = -1;
220 for ($i = 0; $i < count($msgs); $i++
) {
221 if ($msgs[$i]['ID'] == $passed_id) {
222 $msgs[$i]['FLAG_SEEN'] = true;
226 // $message contains all information about the message
227 // including header and body
228 $message = sqimap_get_message($imapConnection, $passed_id, $mailbox);
230 /** translate the subject and mailbox into url-able text **/
231 $url_subj = urlencode(trim($message->header
->subject
));
232 $urlMailbox = urlencode($mailbox);
234 if (isset($message->header
->replyto
)) {
235 $url_replyto = urlencode($message->header
->replyto
);
238 $url_replytoall = $url_replyto;
240 // If we are replying to all, then find all other addresses and
241 // add them to the list. Remove duplicates.
242 // This is somewhat messy, so I'll explain:
243 // 1) Take all addresses (from, to, cc) (avoid nasty join errors here)
244 $url_replytoall_extra_addrs = array_merge(
245 array($message->header
->from
),
246 $message->header
->to
,
250 // 2) Make one big string out of them
251 $url_replytoall_extra_addrs = join(';', $url_replytoall_extra_addrs);
253 // 3) Parse that into an array of addresses
254 $url_replytoall_extra_addrs = parseAddrs($url_replytoall_extra_addrs);
256 // 4) Make them unique -- weed out duplicates
257 // (Coded for PHP 4.0.0)
258 $url_replytoall_extra_addrs =
259 array_keys(array_flip($url_replytoall_extra_addrs));
261 // 5) Remove the addresses we'll be sending the message 'to'
262 $url_replytoall_avoid_addrs = '';
263 if (isset($message->header
->replyto
)) {
264 $url_replytoall_avoid_addrs = $message->header
->replyto
;
267 $url_replytoall_avoid_addrs = parseAddrs($url_replytoall_avoid_addrs);
268 foreach ($url_replytoall_avoid_addrs as $addr) {
269 RemoveAddress($url_replytoall_extra_addrs, $addr);
272 // 6) Remove our identities from the CC list (they still can be in the
273 // TO list) only if $include_self_reply_all is turned off
274 if (!$include_self_reply_all) {
275 RemoveAddress($url_replytoall_extra_addrs,
276 getPref($data_dir, $username, 'email_address'));
277 $idents = getPref($data_dir, $username, 'identities');
278 if ($idents != '' && $idents > 1) {
279 for ($i = 1; $i < $idents; $i ++
) {
280 $cur_email_address = getPref($data_dir, $username, 'email_address' . $i);
281 RemoveAddress($url_replytoall_extra_addrs, $cur_email_address);
286 // 7) Smoosh back into one nice line
287 $url_replytoallcc = getLineOfAddrs($url_replytoall_extra_addrs);
290 $url_replytoallcc = urlencode($url_replytoallcc);
292 $dateString = getLongDateString($message->header
->date
);
294 // What do we reply to -- text only, if possible
295 $ent_num = findDisplayEntity($message);
297 /** TEXT STRINGS DEFINITIONS **/
298 $echo_more = _("more");
299 $echo_less = _("less");
301 if (!isset($show_more_cc)) $show_more_cc = false;
303 /** FORMAT THE TO STRING **/
306 $to_ary = $message->header
->to
;
307 while ($i < count($to_ary)) {
308 $to_ary[$i] = htmlspecialchars(decodeHeader($to_ary[$i]));
311 $to_string = "$to_string<BR>$to_ary[$i]";
313 $to_string = "$to_ary[$i]";
317 if (count($to_ary) > 1) {
318 if ($show_more == false) {
320 /* From a search... */
321 if (isset($where) && isset($what)) {
322 $to_string = "$to_string (<A HREF=\"read_body.php?mailbox=$urlMailbox&passed_id=$passed_id&where=".urlencode($where)."&what=".urlencode($what)."&show_more=1&show_more_cc=$show_more_cc\">$echo_more</A>)";
324 $to_string = "$to_string (<A HREF=\"read_body.php?mailbox=$urlMailbox&passed_id=$passed_id&sort=$sort&startMessage=$startMessage&show_more=1&show_more_cc=$show_more_cc\">$echo_more</A>)";
328 } else if ($i == 1) {
329 /* From a search... */
330 if (isset($where) && isset($what)) {
331 $to_string = "$to_string (<A HREF=\"read_body.php?mailbox=$urlMailbox&passed_id=$passed_id&where=".urlencode($where)."&what=".urlencode($what)."&show_more=0&show_more_cc=$show_more_cc\">$echo_less</A>)";
333 $to_string = "$to_string (<A HREF=\"read_body.php?mailbox=$urlMailbox&passed_id=$passed_id&sort=$sort&startMessage=$startMessage&show_more=0&show_more_cc=$show_more_cc\">$echo_less</A>)";
339 /** FORMAT THE CC STRING **/
341 if (isset ($message->header
->cc
[0]) && trim($message->header
->cc
[0])) {
343 $cc_ary = $message->header
->cc
;
344 while ($i < count(decodeHeader($cc_ary))) {
345 $cc_ary[$i] = htmlspecialchars($cc_ary[$i]);
347 $cc_string = "$cc_string<BR>$cc_ary[$i]";
349 $cc_string = "$cc_ary[$i]";
353 if (count($cc_ary) > 1) {
354 if ($show_more_cc == false) {
356 /* From a search... */
357 if (isset($where) && isset($what)) {
358 $cc_string = "$cc_string (<A HREF=\"read_body.php?mailbox=$urlMailbox&passed_id=$passed_id&what=".urlencode($what)."&where=".urlencode($where)."&show_more_cc=1&show_more=$show_more\">$echo_more</A>)";
360 $cc_string = "$cc_string (<A HREF=\"read_body.php?mailbox=$urlMailbox&passed_id=$passed_id&sort=$sort&startMessage=$startMessage&show_more_cc=1&show_more=$show_more\">$echo_more</A>)";
364 } else if ($i == 1) {
365 /* From a search... */
366 if (isset($where) && isset($what)) {
367 $cc_string = "$cc_string (<A HREF=\"read_body.php?mailbox=$urlMailbox&passed_id=$passed_id&what=".urlencode($what)."&where=".urlencode($where)."&show_more_cc=0&show_more=$show_more\">$echo_less</A>)";
369 $cc_string = "$cc_string (<A HREF=\"read_body.php?mailbox=$urlMailbox&passed_id=$passed_id&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 if (isset($where) && isset($what)) {
395 $bcc_string = "$bcc_string (<A HREF=\"read_body.php?mailbox=$urlMailbox&passed_id=$passed_id&what=".urlencode($what)."&where=".urlencode($where)."&show_more_cc=1&show_more=$show_more\">$echo_more</A>)";
397 $bcc_string = "$bcc_string (<A HREF=\"read_body.php?mailbox=$urlMailbox&passed_id=$passed_id&sort=$sort&startMessage=$startMessage&show_more_cc=1&show_more=$show_more\">$echo_more</A>)";
399 $i = count($bcc_ary);
401 } else if ($i == 1) {
402 /* From a search... */
403 if (isset($where) && isset($what)) {
404 $bcc_string = "$bcc_string (<A HREF=\"read_body.php?mailbox=$urlMailbox&passed_id=$passed_id&what=".urlencode($what)."&where=".urlencode($where)."&show_more_cc=0&show_more=$show_more\">$echo_less</A>)";
406 $bcc_string = "$bcc_string (<A HREF=\"read_body.php?mailbox=$urlMailbox&passed_id=$passed_id&sort=$sort&startMessage=$startMessage&show_more_cc=0&show_more=$show_more\">$echo_less</A>)";
413 if ($default_use_priority) {
414 switch(substr($message->header
->priority
,0,1)) {
415 /* First, check for a higher then normal priority. */
417 case "2": $priority_string = _("High"); break;
419 /* Second, check for a normal priority. */
420 case "3": $priority_string = _("Normal"); break;
422 /* Last, check for a lower then normal priority. */
424 case "5": $priority_string = _("Low"); break;
428 /** make sure everything will display in HTML format **/
429 $from_name = decodeHeader(htmlspecialchars($message->header
->from
));
430 $subject = decodeHeader(htmlspecialchars($message->header
->subject
));
432 do_hook('read_body_top');
434 '<TABLE CELLSPACING="0" WIDTH="100%" BORDER="0" ALIGN="CENTER" CELLPADDING="0">' . "\n" .
435 ' <TR><TD BGCOLOR="' . $color[9] . '" WIDTH="100%">' . "\n" .
436 ' <TABLE WIDTH="100%" CELLSPACING="0" BORDER="0" CELLPADDING="3">' . "\n" .
438 ' <TD ALIGN="LEFT" WIDTH="33%">' . "\n" .
440 if ($where && $what) {
441 echo " <A HREF=\"search.php?where=".urlencode($where)."&what=".urlencode($what)."&mailbox=$urlMailbox\">";
443 echo " <A HREF=\"right_main.php?use_mailbox_cache=1&sort=$sort&startMessage=$startMessage&mailbox=$urlMailbox\">";
445 echo _("Message List");
446 echo '</A> | ';
447 if ($where && $what) {
448 echo " <A HREF=\"delete_message.php?mailbox=$urlMailbox&message=$passed_id&where=".urlencode($where)."&what=".urlencode($what).'">';
450 echo " <A HREF=\"delete_message.php?mailbox=$urlMailbox&message=$passed_id&sort=$sort&startMessage=$startMessage\">";
452 echo _("Delete") . '</A> ';
453 if (($mailbox == $draft_folder) && ($save_as_draft)) {
455 echo " <A HREF=\"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\">";
456 echo _("Resume Draft") . '</a>';
459 echo ' ' .
462 ' <TD WIDTH="33%" ALIGN="CENTER">' . "\n" .
465 if ($where && $what) {
467 if ($currentArrayIndex == -1) {
468 echo 'Previous | Next';
470 $prev = findPreviousMessage();
471 $next = findNextMessage();
474 echo "<a href=\"read_body.php?passed_id=$prev&mailbox=$urlMailbox&sort=$sort&startMessage=$startMessage&show_more=0\">" . _("Previous") . "</A> | ";
476 echo _("Previous") . ' | ';
480 echo "<a href=\"read_body.php?passed_id=$next&mailbox=$urlMailbox&sort=$sort&startMessage=$startMessage&show_more=0\">" . _("Next") . "</A>";
487 echo ' </SMALL>' . "\n" .
488 ' </TD><TD WIDTH="33%" ALIGN="RIGHT">' .
490 " <A HREF=\"compose.php?forward_id=$passed_id&forward_subj=$url_subj&mailbox=$urlMailbox&ent_num=$ent_num\">" .
492 '</A> | ' .
493 " <A HREF=\"compose.php?send_to=$url_replyto&reply_subj=$url_subj&reply_id=$passed_id&mailbox=$urlMailbox&ent_num=$ent_num\">" .
495 '</A> | ' .
496 " <A HREF=\"compose.php?send_to=$url_replytoall&send_to_cc=$url_replytoallcc&reply_subj=$url_subj&reply_id=$passed_id&mailbox=$urlMailbox&ent_num=$ent_num\">" .
504 ' <TR><TD CELLSPACING="0" WIDTH="100%">' .
505 ' <TABLE WIDTH="100%" BORDER="0" CELLSPACING="0" CELLPADDING="3">' . "\n" .
509 echo " <TD BGCOLOR=\"$color[0]\" WIDTH=\"10%\" ALIGN=\"right\" VALIGN=\"top\">\n" .
511 " </TD><TD BGCOLOR=\"$color[0]\" WIDTH=\"80%\" VALIGN=\"top\">\n" .
512 " <B>$subject</B> \n" .
514 ' <TD ROWSPAN="4" width=10% BGCOLOR="'.$color[0].'" ALIGN=right VALIGN=top NOWRAP><small>' . "\n";
516 /* From a search... */
517 if ($where && $what) {
518 echo "<A HREF=\"read_body.php?mailbox=$urlMailbox&passed_id=$passed_id&where=".urlencode($where)."&what=".urlencode($what)."&view_hdr=1\">" . _("View Full Header") . "</A>\n";
520 echo "<A HREF=\"read_body.php?mailbox=$urlMailbox&passed_id=$passed_id&startMessage=$startMessage&show_more=$show_more&view_hdr=1\">" . _("View Full Header") . "</A>\n";
523 /* Output the printer friendly link if we are in subtle mode. */
524 if ($pf_subtle_link) {
525 echo printer_friendly_link(true);
528 do_hook("read_body_header_right");
529 echo '</small></TD>' . "\n" .
533 echo ' <TR>' . "\n" .
534 ' <TD BGCOLOR="' . $color[0] . '" ALIGN="RIGHT">' . "\n" .
536 ' </TD><TD BGCOLOR="' . $color[0] . '">' . "\n" .
537 " <B>$from_name</B> \n" .
541 echo ' <TR>' . "\n" .
542 ' <TD BGCOLOR="' . $color[0] . '" ALIGN="RIGHT">' . "\n" .
544 " </TD><TD BGCOLOR=\"$color[0]\">\n" .
545 " <B>$dateString</B> \n" .
551 " <TD BGCOLOR=\"$color[0]\" ALIGN=RIGHT VALIGN=TOP>\n" .
553 ' </TD><TD BGCOLOR="' . $color[0] . '" VALIGN="TOP">' . "\n" .
554 " <B>$to_string</B> \n" .
558 if (isset($cc_string)) {
560 " <TD BGCOLOR=\"$color[0]\" ALIGN=RIGHT VALIGN=TOP>\n" .
562 " </TD><TD BGCOLOR=\"$color[0]\" VALIGN=TOP colspan=2>\n" .
563 " <B>$cc_string</B> \n" .
569 if (isset($bcc_string)) {
571 " <TD BGCOLOR=\"$color[0]\" ALIGN=RIGHT VALIGN=TOP>\n" .
573 " </TD><TD BGCOLOR=\"$color[0]\" VALIGN=TOP colspan=2>\n" .
574 " <B>$bcc_string</B> \n" .
578 if ($default_use_priority) {
579 if (isset($priority_string)) {
581 " <TD BGCOLOR=\"$color[0]\" ALIGN=RIGHT VALIGN=TOP>\n" .
582 " "._("Priority").": \n".
583 " </TD><TD BGCOLOR=\"$color[0]\" VALIGN=TOP colspan=2>\n" .
584 " <B>$priority_string</B> \n" .
590 if ($show_xmailer_default) {
591 fputs ($imapConnection, sqimap_session_id() .
592 " FETCH $passed_id BODY.PEEK[HEADER.FIELDS (X-Mailer User-Agent)]\r\n");
593 $read = sqimap_read_data ($imapConnection, sqimap_session_id(), true,
594 $response, $readmessage);
595 $mailer = substr($read[1], strpos($read[1], " "));
598 " <TD BGCOLOR=\"$color[0]\" ALIGN=RIGHT VALIGN=TOP>\n" .
599 " "._("Mailer").": \n".
600 " </TD><TD BGCOLOR=\"$color[0]\" VALIGN=TOP colspan=2>\n" .
601 " <B>$mailer</B> \n" .
607 /* Output the printer friendly link if we are not in subtle mode. */
608 if (!$pf_subtle_link) {
609 echo printer_friendly_link(true);
612 do_hook("read_body_header");
617 echo "<TABLE CELLSPACING=0 WIDTH=97% BORDER=0 ALIGN=CENTER CELLPADDING=0>\n" .
618 " <TR><TD BGCOLOR=\"$color[4]\" WIDTH=\"100%\">\n" .
621 $body = formatBody($imapConnection, $message, $color, $wrap_at);
625 '<TABLE CELLSPACING="0" WIDTH="100%" BORDER="0" ALIGN="CENTER" CELLPADDING="0">' . "\n" .
626 " <TR><TD BGCOLOR=\"$color[9]\"> </TD></TR>" .
629 /* show attached images inline -- if pref'fed so */
630 if (($attachment_common_show_images) and
631 is_array($attachment_common_show_images_list)) {
632 foreach ($attachment_common_show_images_list as $img) {
633 echo "<TABLE BORDER=0 CELLSPACING=0 CELLPADDING=2 ALIGN=CENTER>\n" .
636 ' <img src="../src/download.php' .
637 '?passed_id=' . urlencode($img['passed_id']) .
638 '&mailbox=' . urlencode($img['mailbox']) .
639 '&passed_ent_id=' . urlencode($img['ent_id']) .
640 '&absolute_dl=true">' . "\n" .
647 do_hook('read_body_bottom');
648 do_hook('html_bottom');
649 sqimap_logout($imapConnection);