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 /*****************************************************************/
16 /*** THIS FILE NEEDS TO HAVE ITS FORMATTING FIXED!!! ***/
17 /*** PLEASE DO SO AND REMOVE THIS COMMENT SECTION. ***/
18 /*** + Base level indent should begin at left margin, as ***/
19 /*** the require_once below looks. ***/
20 /*** + All identation should consist of four space blocks ***/
21 /*** + Tab characters are evil. ***/
22 /*** + all comments should use "slash-star ... star-slash" ***/
23 /*** style -- no pound characters, no slash-slash style ***/
24 /*** + FLOW CONTROL STATEMENTS (if, while, etc) SHOULD ***/
25 /*** ALWAYS USE { AND } CHARACTERS!!! ***/
26 /*** + Please use ' instead of ", when possible. Note " ***/
27 /*** should always be used in _( ) function calls. ***/
28 /*** Thank you for your help making the SM code more readable. ***/
29 /*****************************************************************/
31 require_once('../src/validate.php');
32 require_once('../functions/imap.php');
33 require_once('../functions/mime.php');
34 require_once('../functions/date.php');
35 require_once('../functions/url_parser.php');
38 * Given an IMAP message id number, this will look it up in the cached
39 * and sorted msgs array and return the index. Used for finding the next
40 * and previous messages.
42 * returns the index of the next valid message from the array
44 function findNextMessage() {
45 global $msort, $currentArrayIndex, $msgs, $sort;
49 if ($currentArrayIndex != 1) {
50 $result = $currentArrayIndex - 1;
53 for (reset($msort); ($key = key($msort)), (isset($key)); next($msort)) {
54 if ($currentArrayIndex == $msgs[$key]['ID']) {
58 $result = $msgs[$key]['ID'];
66 /** Removes just one address from the list of addresses. */
67 function RemoveAddress(&$addr_list, $addr) {
69 foreach (array_keys($addr_list, $addr) as $key_to_delete) {
70 unset($addr_list[$key_to_delete]);
75 /** returns the index of the previous message from the array. */
76 function findPreviousMessage() {
77 global $msort, $currentArrayIndex, $sort, $msgs, $imapConnection;
78 global $mailbox, $data_dir, $username;
82 $numMessages = sqimap_get_num_messages($imapConnection, $mailbox);
83 if ($currentArrayIndex != $numMessages) {
84 $result = $currentArrayIndex +
1;
87 for (reset($msort); ($key = key($msort)), (isset($key)); next($msort)) {
88 if ($currentArrayIndex == $msgs[$key]['ID']) {
92 $result = $msgs[$key]['ID'];
102 * Displays a link to a page where the message is displayed more
103 * "printer friendly".
105 function printer_friendly_link() {
106 global $passed_id, $mailbox, $ent_num, $color;
107 global $pf_subtle_link;
108 global $javascript_on;
110 if (strlen(trim($mailbox)) < 1) {
114 $params = '?passed_ent_id=' . $ent_num;
115 $params .= '&mailbox=' . urlencode($mailbox);
116 $params .= '&passed_id=' . $passed_id;
118 $print_text = _("View Printable Version");
120 if (!$pf_subtle_link) {
121 /* The link is large, on the bottom of the header panel. */
122 $result = ' <tr bgcolor="' . $color[0] . '">' . "\n" .
123 ' <td class="medText" align="right" valign="top">' . "\n" .
125 ' </td><td class="medText" valign="top" colspan="2">'."\n";
127 /* The link is subtle, below "view full header". */
131 /* Output the link. */
132 if ($javascript_on) {
133 $result .= '<script language="javascript">' . "\n" .
135 " function printFormat() {\n" .
136 ' window.open("../src/printer_friendly_main.php' .
137 $params . '","Print","width=800,height=600");' . "\n".
141 "<A HREF=\"javascript:printFormat();\">$print_text</A>\n";
143 $result .= '<A TARGET="_blank" HREF="../src/printer_friendly_bottom.php' .
144 "$params\">$print_text</A>\n";
147 if (!$pf_subtle_link) {
148 /* The link is large, on the bottom of the header panel. */
149 $result .= ' </td>' . "\n" .
156 /*****************************/
157 /*** Main of read_boby.php ***/
158 /*****************************/
160 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
161 sqimap_mailbox_select($imapConnection, $mailbox);
163 displayPageHeader($color, $mailbox);
165 if (isset($view_hdr)) {
166 fputs ($imapConnection, sqimap_session_id() . " FETCH $passed_id BODY[HEADER]\r\n");
167 $read = sqimap_read_data ($imapConnection, sqimap_session_id(), true, $a, $b);
170 '<TABLE WIDTH="100%" CELLPADDING="2" CELLSPACING="0" BORDER="0" ALIGN="CENTER">' . "\n" .
171 " <TR><TD BGCOLOR=\"$color[9]\" WIDTH=\"100%\"><CENTER><B>" . _("Viewing Full Header") . '</B> - ';
172 if (isset($where) && isset($what)) {
173 // Got here from a search
174 echo "<a href=\"read_body.php?mailbox=".urlencode($mailbox)."&passed_id=$passed_id&where=".urlencode($where)."&what=".urlencode($what).'">';
176 echo "<a href=\"read_body.php?mailbox=".urlencode($mailbox)."&passed_id=$passed_id&startMessage=$startMessage&show_more=$show_more\">";
178 echo ''._("View message") . "</a></b></center></td></tr></table>\n" .
179 "<table width=\"99%\" cellpadding=2 cellspacing=0 border=0 align=center>\n" .
183 for ($i=1; $i < count($read); $i++
) {
184 $line = htmlspecialchars($read[$i]);
185 if (eregi("^>", $line)) {
187 $first[$i] = ' ';
189 } else if (eregi("^[ |\t]", $line)) {
192 } else if (eregi("^([^:]+):(.+)", $line, $regs)) {
193 $first[$i] = $regs[1] . ':';
194 $second[$i] = $regs[2];
197 $second[$i] = trim($line);
201 for ($i=0; $i < count($second); $i = $j) {
202 if (isset($first[$i])) {
205 if (isset($second[$i])) {
206 $s = nl2br($second[$i]);
209 while (($first[$j] == '') && ($j < count($first))) {
210 $s .= ' ' . nl2br($second[$j]);
214 if (isset($f)) echo "<nobr><tt><b>$f</b>$s</tt></nobr>";
216 echo "</td></tr></table>\n";
217 echo '</body></html>';
218 sqimap_logout($imapConnection);
223 $currentArrayIndex = $passed_id;
225 $currentArrayIndex = -1;
228 for ($i = 0; $i < count($msgs); $i++
) {
229 if ($msgs[$i]['ID'] == $passed_id) {
230 $msgs[$i]['FLAG_SEEN'] = true;
234 // $message contains all information about the message
235 // including header and body
236 $message = sqimap_get_message($imapConnection, $passed_id, $mailbox);
238 /** translate the subject and mailbox into url-able text **/
239 $url_subj = urlencode(trim($message->header
->subject
));
240 $urlMailbox = urlencode($mailbox);
242 if (isset($message->header
->replyto
)) {
243 $url_replyto = urlencode($message->header
->replyto
);
246 $url_replytoall = $url_replyto;
248 // If we are replying to all, then find all other addresses and
249 // add them to the list. Remove duplicates.
250 // This is somewhat messy, so I'll explain:
251 // 1) Take all addresses (from, to, cc) (avoid nasty join errors here)
252 $url_replytoall_extra_addrs = array_merge(
253 array($message->header
->from
),
254 $message->header
->to
,
258 // 2) Make one big string out of them
259 $url_replytoall_extra_addrs = join(';', $url_replytoall_extra_addrs);
261 // 3) Parse that into an array of addresses
262 $url_replytoall_extra_addrs = parseAddrs($url_replytoall_extra_addrs);
264 // 4) Make them unique -- weed out duplicates
265 // (Coded for PHP 4.0.0)
266 $url_replytoall_extra_addrs =
267 array_keys(array_flip($url_replytoall_extra_addrs));
269 // 5) Remove the addresses we'll be sending the message 'to'
270 $url_replytoall_avoid_addrs = '';
271 if (isset($message->header
->replyto
)) {
272 $url_replytoall_avoid_addrs = $message->header
->replyto
;
275 $url_replytoall_avoid_addrs = parseAddrs($url_replytoall_avoid_addrs);
276 foreach ($url_replytoall_avoid_addrs as $addr) {
277 RemoveAddress($url_replytoall_extra_addrs, $addr);
280 // 6) Remove our identities from the CC list (they still can be in the
281 // TO list) only if $include_self_reply_all is turned off
282 if (!$include_self_reply_all) {
283 RemoveAddress($url_replytoall_extra_addrs,
284 getPref($data_dir, $username, 'email_address'));
285 $idents = getPref($data_dir, $username, 'identities');
286 if ($idents != '' && $idents > 1) {
287 for ($i = 1; $i < $idents; $i ++
) {
288 $cur_email_address = getPref($data_dir, $username, 'email_address' . $i);
289 RemoveAddress($url_replytoall_extra_addrs, $cur_email_address);
294 // 7) Smoosh back into one nice line
295 $url_replytoallcc = getLineOfAddrs($url_replytoall_extra_addrs);
298 $url_replytoallcc = urlencode($url_replytoallcc);
300 $dateString = getLongDateString($message->header
->date
);
302 // What do we reply to -- text only, if possible
303 $ent_num = findDisplayEntity($message);
305 /** TEXT STRINGS DEFINITIONS **/
306 $echo_more = _("more");
307 $echo_less = _("less");
309 if (!isset($show_more_cc)) $show_more_cc = false;
311 /** FORMAT THE TO STRING **/
314 $to_ary = $message->header
->to
;
315 while ($i < count($to_ary)) {
316 $to_ary[$i] = htmlspecialchars(decodeHeader($to_ary[$i]));
319 $to_string = "$to_string<BR>$to_ary[$i]";
321 $to_string = "$to_ary[$i]";
325 if (count($to_ary) > 1) {
326 if ($show_more == false) {
328 /* From a search... */
329 if (isset($where) && isset($what)) {
330 $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>)";
332 $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>)";
336 } else if ($i == 1) {
337 /* From a search... */
338 if (isset($where) && isset($what)) {
339 $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>)";
341 $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>)";
347 /** FORMAT THE CC STRING **/
349 if (isset ($message->header
->cc
[0]) && trim($message->header
->cc
[0])) {
351 $cc_ary = $message->header
->cc
;
352 while ($i < count(decodeHeader($cc_ary))) {
353 $cc_ary[$i] = htmlspecialchars($cc_ary[$i]);
355 $cc_string = "$cc_string<BR>$cc_ary[$i]";
357 $cc_string = "$cc_ary[$i]";
361 if (count($cc_ary) > 1) {
362 if ($show_more_cc == false) {
364 /* From a search... */
365 if (isset($where) && isset($what)) {
366 $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>)";
368 $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>)";
372 } else if ($i == 1) {
373 /* From a search... */
374 if (isset($where) && isset($what)) {
375 $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>)";
377 $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>)";
384 /** FORMAT THE BCC STRING **/
386 if (isset ($message->header
->bcc
[0]) && trim($message->header
->bcc
[0])){
388 $bcc_ary = $message->header
->bcc
;
389 while ($i < count(decodeHeader($bcc_ary))) {
390 $bcc_ary[$i] = htmlspecialchars($bcc_ary[$i]);
392 $bcc_string = "$bcc_string<BR>$bcc_ary[$i]";
394 $bcc_string = "$bcc_ary[$i]";
398 if (count($bcc_ary) > 1) {
399 if ($show_more_cc == false) {
401 /* From a search... */
402 if (isset($where) && isset($what)) {
403 $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>)";
405 $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>)";
407 $i = count($bcc_ary);
409 } else if ($i == 1) {
410 /* From a search... */
411 if (isset($where) && isset($what)) {
412 $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>)";
414 $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>)";
421 if ($default_use_priority) {
422 switch(substr($message->header
->priority
,0,1)) {
423 /* First, check for a higher then normal priority. */
425 case "2": $priority_string = _("High"); break;
427 /* Second, check for a normal priority. */
428 case "3": $priority_string = _("Normal"); break;
430 /* Last, check for a lower then normal priority. */
432 case "5": $priority_string = _("Low"); break;
436 /** make sure everything will display in HTML format **/
437 $from_name = decodeHeader(htmlspecialchars($message->header
->from
));
438 $subject = decodeHeader(htmlspecialchars($message->header
->subject
));
440 do_hook('read_body_top');
442 '<TABLE CELLSPACING="0" WIDTH="100%" BORDER="0" ALIGN="CENTER" CELLPADDING="0">' . "\n" .
443 ' <TR><TD BGCOLOR="' . $color[9] . '" WIDTH="100%">' . "\n" .
444 ' <TABLE WIDTH="100%" CELLSPACING="0" BORDER="0" CELLPADDING="3">' . "\n" .
446 ' <TD ALIGN="LEFT" WIDTH="33%">' . "\n" .
448 if ($where && $what) {
449 echo " <A HREF=\"search.php?where=".urlencode($where)."&what=".urlencode($what)."&mailbox=$urlMailbox\">";
451 echo " <A HREF=\"right_main.php?use_mailbox_cache=1&sort=$sort&startMessage=$startMessage&mailbox=$urlMailbox\">";
453 echo _("Message List");
454 echo '</A> | ';
455 if ($where && $what) {
456 echo " <A HREF=\"delete_message.php?mailbox=$urlMailbox&message=$passed_id&where=".urlencode($where)."&what=".urlencode($what).'">';
458 echo " <A HREF=\"delete_message.php?mailbox=$urlMailbox&message=$passed_id&sort=$sort&startMessage=$startMessage\">";
460 echo _("Delete") . '</A> ';
461 if (($mailbox == $draft_folder) && ($save_as_draft)) {
463 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\">";
464 echo _("Resume Draft") . '</a>';
467 echo ' ' .
470 ' <TD WIDTH="33%" ALIGN="CENTER">' . "\n" .
473 if ($where && $what) {
475 if ($currentArrayIndex == -1) {
476 echo 'Previous | Next';
478 $prev = findPreviousMessage();
479 $next = findNextMessage();
482 echo "<a href=\"read_body.php?passed_id=$prev&mailbox=$urlMailbox&sort=$sort&startMessage=$startMessage&show_more=0\">" . _("Previous") . "</A> | ";
484 echo _("Previous") . ' | ';
488 echo "<a href=\"read_body.php?passed_id=$next&mailbox=$urlMailbox&sort=$sort&startMessage=$startMessage&show_more=0\">" . _("Next") . "</A>";
495 echo ' </SMALL>' . "\n" .
496 ' </TD><TD WIDTH="33%" ALIGN="RIGHT">' .
498 " <A HREF=\"compose.php?forward_id=$passed_id&forward_subj=$url_subj&mailbox=$urlMailbox&ent_num=$ent_num\">" .
500 '</A> | ' .
501 " <A HREF=\"compose.php?send_to=$url_replyto&reply_subj=$url_subj&reply_id=$passed_id&mailbox=$urlMailbox&ent_num=$ent_num\">" .
503 '</A> | ' .
504 " <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\">" .
512 ' <TR><TD CELLSPACING="0" WIDTH="100%">' .
513 ' <TABLE WIDTH="100%" BORDER="0" CELLSPACING="0" CELLPADDING="3">' . "\n" .
517 echo " <TD BGCOLOR=\"$color[0]\" WIDTH=\"10%\" ALIGN=\"right\" VALIGN=\"top\">\n" .
519 " </TD><TD BGCOLOR=\"$color[0]\" WIDTH=\"80%\" VALIGN=\"top\">\n" .
520 " <B>$subject</B> \n" .
522 ' <TD ROWSPAN="4" width="10%" BGCOLOR="'.$color[0].'" ALIGN=right VALIGN=top NOWRAP><small>' . "\n";
524 /* From a search... */
525 if ($where && $what) {
526 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";
528 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";
531 /* Output the printer friendly link if we are in subtle mode. */
532 if ($pf_subtle_link) {
533 echo printer_friendly_link(true);
536 do_hook("read_body_header_right");
537 echo '</small></TD>' . "\n" .
541 echo ' <TR>' . "\n" .
542 ' <TD BGCOLOR="' . $color[0] . '" ALIGN="RIGHT">' . "\n" .
544 ' </TD><TD BGCOLOR="' . $color[0] . '">' . "\n" .
545 " <B>$from_name</B> \n" .
549 echo ' <TR>' . "\n" .
550 ' <TD BGCOLOR="' . $color[0] . '" ALIGN="RIGHT">' . "\n" .
552 " </TD><TD BGCOLOR=\"$color[0]\">\n" .
553 " <B>$dateString</B> \n" .
559 " <TD BGCOLOR=\"$color[0]\" ALIGN=RIGHT VALIGN=TOP>\n" .
561 ' </TD><TD BGCOLOR="' . $color[0] . '" VALIGN="TOP">' . "\n" .
562 " <B>$to_string</B> \n" .
566 if (isset($cc_string)) {
568 " <TD BGCOLOR=\"$color[0]\" ALIGN=RIGHT VALIGN=TOP>\n" .
570 " </TD><TD BGCOLOR=\"$color[0]\" VALIGN=TOP colspan=2>\n" .
571 " <B>$cc_string</B> \n" .
577 if (isset($bcc_string)) {
579 " <TD BGCOLOR=\"$color[0]\" ALIGN=RIGHT VALIGN=TOP>\n" .
581 " </TD><TD BGCOLOR=\"$color[0]\" VALIGN=TOP colspan=2>\n" .
582 " <B>$bcc_string</B> \n" .
586 if ($default_use_priority) {
587 if (isset($priority_string)) {
589 " <TD BGCOLOR=\"$color[0]\" ALIGN=RIGHT VALIGN=TOP>\n" .
590 " "._("Priority").": \n".
591 " </TD><TD BGCOLOR=\"$color[0]\" VALIGN=TOP colspan=2>\n" .
592 " <B>$priority_string</B> \n" .
598 if ($show_xmailer_default) {
599 fputs ($imapConnection, sqimap_session_id() .
600 " FETCH $passed_id BODY.PEEK[HEADER.FIELDS (X-Mailer User-Agent)]\r\n");
601 $read = sqimap_read_data ($imapConnection, sqimap_session_id(), true,
602 $response, $readmessage);
603 $mailer = substr($read[1], strpos($read[1], " "));
606 " <TD BGCOLOR=\"$color[0]\" ALIGN=RIGHT VALIGN=TOP>\n" .
607 " "._("Mailer").": \n".
608 " </TD><TD BGCOLOR=\"$color[0]\" VALIGN=TOP colspan=2>\n" .
609 " <B>$mailer</B> \n" .
615 /* Output the printer friendly link if we are not in subtle mode. */
616 if (!$pf_subtle_link) {
617 echo printer_friendly_link(true);
620 do_hook("read_body_header");
625 echo "<TABLE CELLSPACING=0 WIDTH=\"97%\" BORDER=0 ALIGN=CENTER CELLPADDING=0>\n" .
626 " <TR><TD BGCOLOR=\"$color[4]\" WIDTH=\"100%\">\n" .
629 $body = formatBody($imapConnection, $message, $color, $wrap_at);
633 '<TABLE CELLSPACING="0" WIDTH="100%" BORDER="0" ALIGN="CENTER" CELLPADDING="0">' . "\n" .
634 " <TR><TD BGCOLOR=\"$color[9]\"> </TD></TR>" .
637 /* show attached images inline -- if pref'fed so */
638 if (($attachment_common_show_images) and
639 is_array($attachment_common_show_images_list)) {
640 foreach ($attachment_common_show_images_list as $img) {
641 echo "<TABLE BORDER=0 CELLSPACING=0 CELLPADDING=2 ALIGN=CENTER>\n" .
644 ' <img src="../src/download.php' .
645 '?passed_id=' . urlencode($img['passed_id']) .
646 '&mailbox=' . urlencode($img['mailbox']) .
647 '&passed_ent_id=' . urlencode($img['ent_id']) .
648 '&absolute_dl=true">' . "\n" .
655 do_hook('read_body_bottom');
656 do_hook('html_bottom');
657 sqimap_logout($imapConnection);