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 /*****************************************************************/
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');
37 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
38 sqimap_mailbox_select($imapConnection, $mailbox);
40 displayPageHeader($color, $mailbox);
42 if (isset($view_hdr)) {
43 fputs ($imapConnection, sqimap_session_id() . " FETCH $passed_id BODY[HEADER]\r\n");
44 $read = sqimap_read_data ($imapConnection, sqimap_session_id(), true, $a, $b);
47 '<TABLE WIDTH="100%" CELLPADDING="2" CELLSPACING="0" BORDER="0" ALIGN="CENTER">' . "\n" .
48 " <TR><TD BGCOLOR=\"$color[9]\" WIDTH=\"100%\"><CENTER><B>" . _("Viewing Full Header") . '</B> - ';
49 if (isset($where) && isset($what)) {
50 // Got here from a search
51 echo "<a href=\"read_body.php?mailbox=".urlencode($mailbox)."&passed_id=$passed_id&where=".urlencode($where)."&what=".urlencode($what).'">';
53 echo "<a href=\"read_body.php?mailbox=".urlencode($mailbox)."&passed_id=$passed_id&startMessage=$startMessage&show_more=$show_more\">";
55 echo ''._("View message") . "</a></b></center></td></tr></table>\n" .
56 "<table width=99% cellpadding=2 cellspacing=0 border=0 align=center>\n" .
60 for ($i=1; $i < count($read); $i++
) {
61 $line = htmlspecialchars($read[$i]);
62 if (eregi("^>", $line)) {
64 $first[$i] = ' ';
66 } else if (eregi("^[ |\t]", $line)) {
69 } else if (eregi("^([^:]+):(.+)", $line, $regs)) {
70 $first[$i] = $regs[1] . ':';
71 $second[$i] = $regs[2];
74 $second[$i] = trim($line);
78 for ($i=0; $i < count($second); $i = $j) {
79 if (isset($first[$i])) {
82 if (isset($second[$i])) {
83 $s = nl2br($second[$i]);
86 while (($first[$j] == '') && ($j < count($first))) {
87 $s .= ' ' . nl2br($second[$j]);
91 if (isset($f)) echo "<nobr><tt><b>$f</b>$s</tt></nobr>";
93 echo "</td></tr></table>\n";
94 echo '</body></html>';
95 sqimap_logout($imapConnection);
100 * Given an IMAP message id number, this will look it up in the cached
101 * and sorted msgs array and return the index. Used for finding the next
102 * and previous messages.
104 * returns the index of the next valid message from the array
106 function findNextMessage() {
107 global $msort, $currentArrayIndex, $msgs, $sort;
111 if ($currentArrayIndex != 1) {
112 $result = $currentArrayIndex - 1;
115 for (reset($msort); ($key = key($msort)), (isset($key)); next($msort)) {
116 if ($currentArrayIndex == $msgs[$key]['ID']) {
120 $result = $msgs[$key]['ID'];
128 /** Removes just one address from the list of addresses. */
129 function RemoveAddress(&$addr_list, $addr) {
131 foreach (array_keys($addr_list, $addr) as $key_to_delete) {
132 unset($addr_list[$key_to_delete]);
137 /** returns the index of the previous message from the array. */
138 function findPreviousMessage() {
139 global $msort, $currentArrayIndex, $sort, $msgs, $imapConnection;
140 global $mailbox, $data_dir, $username;
144 $numMessages = sqimap_get_num_messages($imapConnection, $mailbox);
145 if ($currentArrayIndex != $numMessages) {
146 $result = $currentArrayIndex +
1;
149 for (reset($msort); ($key = key($msort)), (isset($key)); next($msort)) {
150 if ($currentArrayIndex == $msgs[$key]['ID']) {
154 $result = $msgs[$key]['ID'];
164 * Displays a link to a page where the message is displayed more
165 * "printer friendly".
167 function printer_friendly_link() {
168 global $passed_id, $mailbox, $ent_num, $color;
169 global $pf_subtle_link;
170 global $javascript_on;
172 if (strlen(trim($mailbox)) < 1) {
176 $params = '?passed_ent_id=' . $ent_num;
177 $params .= '&mailbox=' . urlencode($mailbox);
178 $params .= '&passed_id=' . $passed_id;
180 $print_text = _("View Printable Version");
182 if (!$pf_subtle_link) {
183 /* The link is large, on the bottom of the header panel. */
184 $result = ' <tr bgcolor="' . $color[0] . '">' . "\n" .
185 ' <td class="medText" align="right" valign="top">' . "\n" .
187 ' </td><td class="medText" valign="top" colspan="2">'."\n";
189 /* The link is subtle, below "view full header". */
193 /* Output the link. */
194 if ($javascript_on) {
195 $result .= '<script language="javascript">' . "\n" .
197 " function printFormat() {\n" .
198 ' window.open("../src/printer_friendly_main.php' .
199 $params . '","Print","width=800,height=600");' . "\n".
203 "<A HREF=\"javascript:printFormat();\">$print_text</A>\n";
205 $result .= '<A TARGET="_blank" HREF="../src/printer_friendly_bottom.php' .
206 "$params\">$print_text</A>\n";
209 if (!$pf_subtle_link) {
210 /* The link is large, on the bottom of the header panel. */
211 $result .= ' </td>' . "\n" .
218 /*****************************/
219 /*** Main of read_boby.php ***/
220 /*****************************/
223 $currentArrayIndex = $passed_id;
224 /*** START OF COMMENTED OUT CODE - PHILIPPE, CAN THIS BE REMOVED?
225 for ($i=0; $i < count($msgs); $i++) {
226 if ($msgs[$i]["ID"] == $passed_id) {
227 $currentArrayIndex = $i;
231 *** END OF COMMENTED OUT CODE */
233 $currentArrayIndex = -1;
236 for ($i = 0; $i < count($msgs); $i++
) {
237 if ($msgs[$i]['ID'] == $passed_id) {
238 $msgs[$i]['FLAG_SEEN'] = true;
242 // $message contains all information about the message
243 // including header and body
244 $message = sqimap_get_message($imapConnection, $passed_id, $mailbox);
246 /** translate the subject and mailbox into url-able text **/
247 $url_subj = urlencode(trim($message->header
->subject
));
248 $urlMailbox = urlencode($mailbox);
250 if (isset($message->header
->replyto
)) {
251 $url_replyto = urlencode($message->header
->replyto
);
254 $url_replytoall = $url_replyto;
256 // If we are replying to all, then find all other addresses and
257 // add them to the list. Remove duplicates.
258 // This is somewhat messy, so I'll explain:
259 // 1) Take all addresses (from, to, cc) (avoid nasty join errors here)
260 $url_replytoall_extra_addrs = array_merge(
261 array($message->header
->from
),
262 $message->header
->to
,
266 // 2) Make one big string out of them
267 $url_replytoall_extra_addrs = join(';', $url_replytoall_extra_addrs);
269 // 3) Parse that into an array of addresses
270 $url_replytoall_extra_addrs = parseAddrs($url_replytoall_extra_addrs);
272 // 4) Make them unique -- weed out duplicates
273 // (Coded for PHP 4.0.0)
274 $url_replytoall_extra_addrs =
275 array_keys(array_flip($url_replytoall_extra_addrs));
277 // 5) Remove the addresses we'll be sending the message 'to'
278 $url_replytoall_avoid_addrs = '';
279 if (isset($message->header
->replyto
)) {
280 $url_replytoall_avoid_addrs = $message->header
->replyto
;
283 $url_replytoall_avoid_addrs = parseAddrs($url_replytoall_avoid_addrs);
284 foreach ($url_replytoall_avoid_addrs as $addr) {
285 RemoveAddress($url_replytoall_extra_addrs, $addr);
288 // 6) Remove our identities from the CC list (they still can be in the
289 // TO list) only if $include_self_reply_all is turned off
290 if (!$include_self_reply_all) {
291 RemoveAddress($url_replytoall_extra_addrs,
292 getPref($data_dir, $username, 'email_address'));
293 $idents = getPref($data_dir, $username, 'identities');
294 if ($idents != '' && $idents > 1) {
295 for ($i = 1; $i < $idents; $i ++
) {
296 $cur_email_address = getPref($data_dir, $username, 'email_address' . $i);
297 RemoveAddress($url_replytoall_extra_addrs, $cur_email_address);
302 // 7) Smoosh back into one nice line
303 $url_replytoallcc = getLineOfAddrs($url_replytoall_extra_addrs);
306 $url_replytoallcc = urlencode($url_replytoallcc);
308 $dateString = getLongDateString($message->header
->date
);
310 // What do we reply to -- text only, if possible
311 $ent_num = findDisplayEntity($message);
313 /** TEXT STRINGS DEFINITIONS **/
314 $echo_more = _("more");
315 $echo_less = _("less");
317 if (!isset($show_more_cc)) $show_more_cc = false;
319 /** FORMAT THE TO STRING **/
322 $to_ary = $message->header
->to
;
323 while ($i < count($to_ary)) {
324 $to_ary[$i] = htmlspecialchars(decodeHeader($to_ary[$i]));
327 $to_string = "$to_string<BR>$to_ary[$i]";
329 $to_string = "$to_ary[$i]";
333 if (count($to_ary) > 1) {
334 if ($show_more == false) {
336 /* From a search... */
337 if (isset($where) && isset($what)) {
338 $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>)";
340 $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>)";
344 } else if ($i == 1) {
345 /* From a search... */
346 if (isset($where) && isset($what)) {
347 $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>)";
349 $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>)";
355 /** FORMAT THE CC STRING **/
357 if (isset ($message->header
->cc
[0]) && trim($message->header
->cc
[0])) {
359 $cc_ary = $message->header
->cc
;
360 while ($i < count(decodeHeader($cc_ary))) {
361 $cc_ary[$i] = htmlspecialchars($cc_ary[$i]);
363 $cc_string = "$cc_string<BR>$cc_ary[$i]";
365 $cc_string = "$cc_ary[$i]";
369 if (count($cc_ary) > 1) {
370 if ($show_more_cc == false) {
372 /* From a search... */
373 if (isset($where) && isset($what)) {
374 $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>)";
376 $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>)";
380 } else if ($i == 1) {
381 /* From a search... */
382 if (isset($where) && isset($what)) {
383 $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>)";
385 $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>)";
392 /** FORMAT THE BCC STRING **/
394 if (isset ($message->header
->bcc
[0]) && trim($message->header
->bcc
[0])){
396 $bcc_ary = $message->header
->bcc
;
397 while ($i < count(decodeHeader($bcc_ary))) {
398 $bcc_ary[$i] = htmlspecialchars($bcc_ary[$i]);
400 $bcc_string = "$bcc_string<BR>$bcc_ary[$i]";
402 $bcc_string = "$bcc_ary[$i]";
406 if (count($bcc_ary) > 1) {
407 if ($show_more_cc == false) {
409 /* From a search... */
410 if (isset($where) && isset($what)) {
411 $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>)";
413 $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>)";
415 $i = count($bcc_ary);
417 } else if ($i == 1) {
418 /* From a search... */
419 if (isset($where) && isset($what)) {
420 $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>)";
422 $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>)";
429 if ($default_use_priority) {
430 switch(substr($message->header
->priority
,0,1)) {
431 /* First, check for a higher then normal priority. */
433 case "2": $priority_string = _("High"); break;
435 /* Second, check for a normal priority. */
436 case "3": $priority_string = _("Normal"); break;
438 /* Last, check for a lower then normal priority. */
440 case "5": $priority_string = _("Low"); break;
444 /** make sure everything will display in HTML format **/
445 $from_name = decodeHeader(htmlspecialchars($message->header
->from
));
446 $subject = decodeHeader(htmlspecialchars($message->header
->subject
));
448 do_hook('read_body_top');
450 '<TABLE CELLSPACING="0" WIDTH="100%" BORDER="0" ALIGN="CENTER" CELLPADDING="0">' . "\n" .
451 ' <TR><TD BGCOLOR="' . $color[9] . '" WIDTH="100%">' . "\n" .
452 ' <TABLE WIDTH="100%" CELLSPACING="0" BORDER="0" CELLPADDING="3">' . "\n" .
454 ' <TD ALIGN="LEFT" WIDTH="33%">' . "\n" .
456 if ($where && $what) {
457 echo " <A HREF=\"search.php?where=".urlencode($where)."&what=".urlencode($what)."&mailbox=$urlMailbox\">";
459 echo " <A HREF=\"right_main.php?use_mailbox_cache=1&sort=$sort&startMessage=$startMessage&mailbox=$urlMailbox\">";
461 echo _("Message List");
462 echo '</A> | ';
463 if ($where && $what) {
464 echo " <A HREF=\"delete_message.php?mailbox=$urlMailbox&message=$passed_id&where=".urlencode($where)."&what=".urlencode($what).'">';
466 echo " <A HREF=\"delete_message.php?mailbox=$urlMailbox&message=$passed_id&sort=$sort&startMessage=$startMessage\">";
468 echo _("Delete") . '</A> ';
469 if (($mailbox == $draft_folder) && ($save_as_draft)) {
471 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\">";
472 echo _("Resume Draft") . '</a>';
475 echo ' ' .
478 ' <TD WIDTH="33%" ALIGN="CENTER">' . "\n" .
481 if ($where && $what) {
483 if ($currentArrayIndex == -1) {
484 echo 'Previous | Next';
486 $prev = findPreviousMessage();
487 $next = findNextMessage();
490 echo "<a href=\"read_body.php?passed_id=$prev&mailbox=$urlMailbox&sort=$sort&startMessage=$startMessage&show_more=0\">" . _("Previous") . "</A> | ";
492 echo _("Previous") . ' | ';
496 echo "<a href=\"read_body.php?passed_id=$next&mailbox=$urlMailbox&sort=$sort&startMessage=$startMessage&show_more=0\">" . _("Next") . "</A>";
503 echo ' </SMALL>' . "\n" .
504 ' </TD><TD WIDTH="33%" ALIGN="RIGHT">' .
506 " <A HREF=\"compose.php?forward_id=$passed_id&forward_subj=$url_subj&mailbox=$urlMailbox&ent_num=$ent_num\">" .
508 '</A> | ' .
509 " <A HREF=\"compose.php?send_to=$url_replyto&reply_subj=$url_subj&reply_id=$passed_id&mailbox=$urlMailbox&ent_num=$ent_num\">" .
511 '</A> | ' .
512 " <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\">" .
520 ' <TR><TD CELLSPACING="0" WIDTH="100%">' .
521 ' <TABLE WIDTH="100%" BORDER="0" CELLSPACING="0" CELLPADDING="3">' . "\n" .
525 echo " <TD BGCOLOR=\"$color[0]\" WIDTH=\"10%\" ALIGN=\"right\" VALIGN=\"top\">\n" .
527 " </TD><TD BGCOLOR=\"$color[0]\" WIDTH=\"80%\" VALIGN=\"top\">\n" .
528 " <B>$subject</B> \n" .
530 ' <TD ROWSPAN="4" width=10% BGCOLOR="'.$color[0].'" ALIGN=right VALIGN=top NOWRAP><small>' . "\n";
532 /* From a search... */
533 if ($where && $what) {
534 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";
536 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";
539 /* Output the printer friendly link if we are in subtle mode. */
540 if ($pf_subtle_link) {
541 echo printer_friendly_link(true);
544 do_hook("read_body_header_right");
545 echo '</small></TD>' . "\n" .
549 echo ' <TR>' . "\n" .
550 ' <TD BGCOLOR="' . $color[0] . '" ALIGN="RIGHT">' . "\n" .
552 ' </TD><TD BGCOLOR="' . $color[0] . '">' . "\n" .
553 " <B>$from_name</B> \n" .
557 echo ' <TR>' . "\n" .
558 ' <TD BGCOLOR="' . $color[0] . '" ALIGN="RIGHT">' . "\n" .
560 " </TD><TD BGCOLOR=\"$color[0]\">\n" .
561 " <B>$dateString</B> \n" .
567 " <TD BGCOLOR=\"$color[0]\" ALIGN=RIGHT VALIGN=TOP>\n" .
569 ' </TD><TD BGCOLOR="' . $color[0] . '" VALIGN="TOP">' . "\n" .
570 " <B>$to_string</B> \n" .
574 if (isset($cc_string)) {
576 " <TD BGCOLOR=\"$color[0]\" ALIGN=RIGHT VALIGN=TOP>\n" .
578 " </TD><TD BGCOLOR=\"$color[0]\" VALIGN=TOP colspan=2>\n" .
579 " <B>$cc_string</B> \n" .
585 if (isset($bcc_string)) {
587 " <TD BGCOLOR=\"$color[0]\" ALIGN=RIGHT VALIGN=TOP>\n" .
589 " </TD><TD BGCOLOR=\"$color[0]\" VALIGN=TOP colspan=2>\n" .
590 " <B>$bcc_string</B> \n" .
594 if ($default_use_priority) {
595 if (isset($priority_string)) {
597 " <TD BGCOLOR=\"$color[0]\" ALIGN=RIGHT VALIGN=TOP>\n" .
598 " "._("Priority").": \n".
599 " </TD><TD BGCOLOR=\"$color[0]\" VALIGN=TOP colspan=2>\n" .
600 " <B>$priority_string</B> \n" .
606 if ($show_xmailer_default) {
607 fputs ($imapConnection, sqimap_session_id() .
608 " FETCH $passed_id BODY.PEEK[HEADER.FIELDS (X-Mailer User-Agent)]\r\n");
609 $read = sqimap_read_data ($imapConnection, sqimap_session_id(), true,
610 $response, $readmessage);
611 $mailer = substr($read[1], strpos($read[1], " "));
614 " <TD BGCOLOR=\"$color[0]\" ALIGN=RIGHT VALIGN=TOP>\n" .
615 " "._("Mailer").": \n".
616 " </TD><TD BGCOLOR=\"$color[0]\" VALIGN=TOP colspan=2>\n" .
617 " <B>$mailer</B> \n" .
623 /* Output the printer friendly link if we are not in subtle mode. */
624 if (!$pf_subtle_link) {
625 echo printer_friendly_link(true);
628 do_hook("read_body_header");
633 echo "<TABLE CELLSPACING=0 WIDTH=97% BORDER=0 ALIGN=CENTER CELLPADDING=0>\n" .
634 " <TR><TD BGCOLOR=\"$color[4]\" WIDTH=\"100%\">\n" .
637 $body = formatBody($imapConnection, $message, $color, $wrap_at);
641 '<TABLE CELLSPACING="0" WIDTH="100%" BORDER="0" ALIGN="CENTER" CELLPADDING="0">' . "\n" .
642 " <TR><TD BGCOLOR=\"$color[9]\"> </TD></TR>" .
645 /* show attached images inline -- if pref'fed so */
646 if (($attachment_common_show_images) and
647 is_array($attachment_common_show_images_list)) {
648 foreach ($attachment_common_show_images_list as $img) {
649 echo "<TABLE BORDER=0 CELLSPACING=0 CELLPADDING=2 ALIGN=CENTER>\n" .
652 ' <img src="../src/download.php' .
653 '?passed_id=' . urlencode($img['passed_id']) .
654 '&mailbox=' . urlencode($img['mailbox']) .
655 '&passed_ent_id=' . urlencode($img['ent_id']) .
656 '&absolute_dl=true">' . "\n" .
663 do_hook('read_body_bottom');
664 do_hook('html_bottom');
665 sqimap_logout($imapConnection);