Added hook to read_body.php, and rearranged the layout of table cells in the header...
[squirrelmail.git] / src / read_body.php
CommitLineData
59177427 1<?php
ef870322 2 /**
3 ** read_body.php
4 **
5 ** Copyright (c) 1999-2000 The SquirrelMail development team
6 ** Licensed under the GNU GPL. For full terms see the file COPYING.
7 **
8 ** This file is used for reading the msgs array and displaying
9 ** the resulting emails in the right frame.
245a6892 10 **
11 ** $Id$
ef870322 12 **/
13
f740c049 14 include("../src/validate.php");
f740c049 15 include('../functions/page_header.php');
16 include('../functions/imap.php');
17 include('../functions/mime.php');
18 include('../functions/date.php');
19 include('../functions/url_parser.php');
e5a06719 20 include('../src/load_prefs.php');
f740c049 21
c36ed9cf 22 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
23 sqimap_mailbox_select($imapConnection, $mailbox);
e5a06719 24 do_hook('html_top');
c36ed9cf 25 displayPageHeader($color, $mailbox);
26
245a6892 27 if (isset($view_hdr)) {
c36ed9cf 28 fputs ($imapConnection, "a003 FETCH $passed_id BODY[HEADER]\r\n");
29 $read = sqimap_read_data ($imapConnection, "a003", true, $a, $b);
30
e5a06719 31 echo '<br>';
32 echo '<table width="100%" cellpadding="2" cellspacing="0" border="0" align="center">' . "\n";
33 echo " <TR><TD BGCOLOR=\"$color[9]\" WIDTH=100%><center><b>" . _("Viewing full header") . '</b> - ';
cd928157 34 if (isset($where) && isset($what)) {
f4991a86 35 // Got here from a search
e5a06719 36 echo "<a href=\"read_body.php?mailbox=".urlencode($mailbox)."&passed_id=$passed_id&where=".urlencode($where)."&what=".urlencode($what).'">';
8ae331c2 37 } else {
f4991a86 38 echo "<a href=\"read_body.php?mailbox=".urlencode($mailbox)."&passed_id=$passed_id&startMessage=$startMessage&show_more=$show_more\">";
39 }
e5a06719 40 echo ''._("View message") . "</a></b></center></td></tr></table>\n";
e9f8ea4e 41 echo "<table width=99% cellpadding=2 cellspacing=0 border=0 align=center>\n";
e5a06719 42 echo '<tr><td>';
5c54e435 43
cd928157 44 $cnum = 0;
c36ed9cf 45 for ($i=1; $i < count($read)-1; $i++) {
5c54e435 46 $line = htmlspecialchars($read[$i]);
172fd718 47 if (eregi("^&gt;", $line)) {
48 $second[$i] = $line;
e5a06719 49 $first[$i] = '&nbsp;';
172fd718 50 $cnum++;
51 } else if (eregi("^[ |\t]", $line)) {
52 $second[$i] = $line;
e5a06719 53 $first[$i] = '';
172fd718 54 } else if (eregi("^([^:]+):(.+)", $line, $regs)) {
e5a06719 55 $first[$i] = $regs[1] . ':';
172fd718 56 $second[$i] = $regs[2];
57 $cnum++;
5c54e435 58 } else {
172fd718 59 $second[$i] = trim($line);
e5a06719 60 $first[$i] = '';
c36ed9cf 61 }
172fd718 62 }
63 for ($i=0; $i < count($second); $i = $j) {
cd928157 64 if (isset($first[$i]))
65 $f = $first[$i];
66 if (isset($second[$i]))
67 $s = nl2br($second[$i]);
172fd718 68 $j = $i + 1;
e5a06719 69 while ($first[$j] == '' && $j < count($first)) {
70 $s .= '&nbsp;&nbsp;&nbsp;&nbsp;' . nl2br($second[$j]);
172fd718 71 $j++;
72 }
73 parseEmail($s);
cd928157 74 if (isset($f)) echo "<nobr><tt><b>$f</b>$s</tt></nobr>";
c36ed9cf 75 }
5c54e435 76 echo "</td></tr></table>\n";
e5a06719 77 echo '</body></html>';
eabc2883 78 sqimap_logout($imapConnection);
c36ed9cf 79 exit;
80 }
81
90033b64 82 // given an IMAP message id number, this will look it up in the cached and sorted msgs array and
83 // return the index. used for finding the next and previous messages
84
85 // returns the index of the next valid message from the array
86 function findNextMessage() {
5c54e435 87 global $msort, $currentArrayIndex, $msgs, $sort;
88
89 if ($sort == 6) {
90 if ($currentArrayIndex != 1) {
91 return $currentArrayIndex - 1;
92 }
93 } else {
94 for (reset($msort); ($key = key($msort)), (isset($key)); next($msort)) {
26f1e4f1 95 if ($currentArrayIndex == $msgs[$key]['ID']) {
5c54e435 96 next($msort);
97 $key = key($msort);
98 if (isset($key))
e5a06719 99 return $msgs[$key]['ID'];
5c54e435 100 }
101 }
102 }
90033b64 103 return -1;
104 }
105
106 // returns the index of the previous message from the array
107 function findPreviousMessage() {
5c54e435 108 global $msort, $currentArrayIndex, $sort, $msgs, $imapConnection, $mailbox;
109 if ($sort == 6) {
110 $numMessages = sqimap_get_num_messages($imapConnection, $mailbox);
111 if ($currentArrayIndex != $numMessages) {
112 return $currentArrayIndex + 1;
113 }
114 } else {
115 for (reset($msort); ($key = key($msort)), (isset($key)); next($msort)) {
e5a06719 116 if ($currentArrayIndex == $msgs[$key]['ID']) {
5c54e435 117 prev($msort);
118 $key = key($msort);
119 if (isset($key))
e5a06719 120 return $msgs[$key]['ID'];
5c54e435 121 }
122 }
123 }
90033b64 124 return -1;
125 }
126
127 if (isset($msgs)) {
8ae331c2 128 $currentArrayIndex = $passed_id;
53524fa0 129 /*
90033b64 130 for ($i=0; $i < count($msgs); $i++) {
131 if ($msgs[$i]["ID"] == $passed_id) {
132 $currentArrayIndex = $i;
133 break;
134 }
135 }
53524fa0 136 */
90033b64 137 } else {
138 $currentArrayIndex = -1;
139 }
140
1108e8bb 141 for ($i = 0; $i < count($msgs); $i++) {
e5a06719 142 if ($msgs[$i]['ID'] == $passed_id)
143 $msgs[$i]['FLAG_SEEN'] = true;
1108e8bb 144 }
145
f7fb20fe 146 // $message contains all information about the message
147 // including header and body
813eba2f 148 $message = sqimap_get_message($imapConnection, $passed_id, $mailbox);
d51894be 149
f7fb20fe 150 /** translate the subject and mailbox into url-able text **/
d51894be 151 $url_subj = urlencode(trim($message->header->subject));
8467bf00 152 $urlMailbox = urlencode($mailbox);
8beafbbc 153 $url_replyto = urlencode($message->header->replyto);
be69e508 154
8beafbbc 155 $url_replytoall = urlencode($message->header->replyto);
5bc39e3f 156
157 // If we are replying to all, then find all other addresses and
158 // add them to the list. Remove duplicates.
159 // This is somewhat messy, so I'll explain:
160 // 1) Take all addresses (from, to, cc) (avoid nasty join errors here)
161 $url_replytoall_extra_addrs = array_merge(array($message->header->from),
162 $message->header->to, $message->header->cc);
163
164 // 2) Make one big string out of them
165 $url_replytoall_extra_addrs = join(';', $url_replytoall_extra_addrs);
166
167 // 3) Parse that into an array of addresses
168 $url_replytoall_extra_addrs = parseAddrs($url_replytoall_extra_addrs);
169
170 // 4) Make them unique -- weed out duplicates
40023540 171 // (Coded for PHP 4.0.0)
172 $url_replytoall_extra_addrs =
f4405199 173 array_keys(array_flip($url_replytoall_extra_addrs));
5bc39e3f 174
175 // 5) Remove the addresses we'll be sending the message 'to'
176 $url_replytoall_avoid_addrs = parseAddrs($message->header->replyto);
177 foreach ($url_replytoall_avoid_addrs as $addr)
178 {
179 foreach (array_keys($url_replytoall_extra_addrs, $addr) as $key_to_delete)
180 {
181 unset($url_replytoall_extra_addrs[$key_to_delete]);
182 }
183 }
184
185 // 6) Smoosh back into one nice line
186 $url_replytoallcc = getLineOfAddrs($url_replytoall_extra_addrs);
187
188 // 7) urlencode() it
b676ba7e 189 $url_replytoallcc = urlencode($url_replytoallcc);
4bfed9f3 190
8beafbbc 191 $dateString = getLongDateString($message->header->date);
3ae6e629 192
193 // What do we reply to -- text only, if possible
429f8906 194 $ent_num = findDisplayEntity($message);
31f3d7c0 195
b581fa60 196 /** TEXT STRINGS DEFINITIONS **/
197 $echo_more = _("more");
198 $echo_less = _("less");
199
e788294e 200 if (!isset($show_more_cc)) $show_more_cc = false;
201
078a40a4 202 /** FORMAT THE TO STRING **/
2844086d 203 $i = 0;
e5a06719 204 $to_string = '';
8beafbbc 205 $to_ary = $message->header->to;
2844086d 206 while ($i < count($to_ary)) {
99fa2b21 207 $to_ary[$i] = htmlspecialchars(decodeHeader($to_ary[$i]));
be8e07f8 208
2844086d 209 if ($to_string)
210 $to_string = "$to_string<BR>$to_ary[$i]";
211 else
212 $to_string = "$to_ary[$i]";
213
214 $i++;
215 if (count($to_ary) > 1) {
216 if ($show_more == false) {
217 if ($i == 1) {
30833080 218 if (isset($where) && isset($what)) {
f4991a86 219 // from a search
220 $to_string = "$to_string&nbsp;(<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>)";
221 } else {
222 $to_string = "$to_string&nbsp;(<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>)";
223 }
2844086d 224 $i = count($to_ary);
225 }
226 } else if ($i == 1) {
30833080 227 if (isset($where) && isset($what)) {
f4991a86 228 // from a search
229 $to_string = "$to_string&nbsp;(<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>)";
230 } else {
231 $to_string = "$to_string&nbsp;(<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>)";
232 }
2844086d 233 }
234 }
235 }
236
078a40a4 237 /** FORMAT THE CC STRING **/
238 $i = 0;
e7681707 239 if (isset ($message->header->cc[0]) && trim($message->header->cc[0])){
240 $cc_string = "";
241 $cc_ary = $message->header->cc;
242 while ($i < count(decodeHeader($cc_ary))) {
243 $cc_ary[$i] = htmlspecialchars($cc_ary[$i]);
244 if ($cc_string)
245 $cc_string = "$cc_string<BR>$cc_ary[$i]";
246 else
247 $cc_string = "$cc_ary[$i]";
248
249 $i++;
250 if (count($cc_ary) > 1) {
251 if ($show_more_cc == false) {
252 if ($i == 1) {
30833080 253 if (isset($where) && isset($what)) {
e7681707 254 // from a search
255 $cc_string = "$cc_string&nbsp;(<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>)";
256 } else {
257 $cc_string = "$cc_string&nbsp;(<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>)";
258 }
259 $i = count($cc_ary);
260 }
261 } else if ($i == 1) {
30833080 262 if (isset($where) && isset($what)) {
f4991a86 263 // from a search
e7681707 264 $cc_string = "$cc_string&nbsp;(<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>)";
f4991a86 265 } else {
e7681707 266 $cc_string = "$cc_string&nbsp;(<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>)";
f4991a86 267 }
078a40a4 268 }
078a40a4 269 }
270 }
271 }
f7fb20fe 272 /** make sure everything will display in HTML format **/
8beafbbc 273 $from_name = decodeHeader(htmlspecialchars($message->header->from));
7aaa81fc 274 $subject = decodeHeader(htmlspecialchars($message->header->subject));
078a40a4 275
e5a06719 276 do_hook('read_body_top');
277 echo '<BR>';
8ae331c2 278
e5a06719 279 echo '<TABLE COLS="1" CELLSPACING="0" WIDTH="100%" BORDER="0" ALIGN="CENTER" CELLPADDING="0">' . "\n";
280 echo ' <TR><TD BGCOLOR="' . $color[9] . '" WIDTH="100%">';
281 echo ' <TABLE WIDTH="100%" CELLSPACING="0" BORDER="0" COLS="2" CELLPADDING="3">';
282 echo ' <TR>';
283 echo ' <TD ALIGN="LEFT" WIDTH="33%">';
284 echo ' <SMALL>';
1809bad8 285 if ($where && $what) {
286 echo " <A HREF=\"search.php?where=".urlencode($where)."&what=".urlencode($what)."&mailbox=$urlMailbox\">";
287 } else {
288 echo " <A HREF=\"right_main.php?use_mailbox_cache=1&sort=$sort&startMessage=$startMessage&mailbox=$urlMailbox\">";
289 }
b581fa60 290 echo _("Message List");
e5a06719 291 echo '</A>&nbsp;|&nbsp;';
1809bad8 292 if ($where && $what) {
e5a06719 293 echo " <A HREF=\"delete_message.php?mailbox=$urlMailbox&message=$passed_id&where=".urlencode($where)."&what=".urlencode($what).'">';
1809bad8 294 } else {
d36f3e63 295 echo " <A HREF=\"delete_message.php?mailbox=$urlMailbox&message=$passed_id&sort=$sort&startMessage=$startMessage\">";
1809bad8 296 }
b581fa60 297 echo _("Delete");
e5a06719 298 echo '</A>&nbsp;&nbsp;';
299 echo ' </SMALL>';
300 echo ' </TD><TD WIDTH="33%" ALIGN="CENTER">';
301 echo ' <SMALL>' . "\n";
1809bad8 302 if ($where && $what) {
90033b64 303 } else {
1809bad8 304 if ($currentArrayIndex == -1) {
e5a06719 305 echo 'Previous&nbsp;|&nbsp;Next';
1809bad8 306 } else {
307 $prev = findPreviousMessage();
308 $next = findNextMessage();
309 if ($prev != -1)
310 echo "<a href=\"read_body.php?passed_id=$prev&mailbox=$urlMailbox&sort=$sort&startMessage=$startMessage&show_more=0\">" . _("Previous") . "</A>&nbsp;|&nbsp;";
311 else
e5a06719 312 echo _("Previous") . '&nbsp;|&nbsp;';
1809bad8 313 if ($next != -1)
314 echo "<a href=\"read_body.php?passed_id=$next&mailbox=$urlMailbox&sort=$sort&startMessage=$startMessage&show_more=0\">" . _("Next") . "</A>";
315 else
316 echo _("Next");
317 }
318 }
e5a06719 319 echo ' </SMALL>' . "\n";
320 echo ' </TD><TD WIDTH="33%" ALIGN="RIGHT">';
321 echo ' <SMALL>';
429f8906 322 echo " <A HREF=\"compose.php?forward_id=$passed_id&forward_subj=$url_subj&mailbox=$urlMailbox&ent_num=$ent_num\">";
b581fa60 323 echo _("Forward");
e5a06719 324 echo '</A>&nbsp;|&nbsp;';
429f8906 325 echo " <A HREF=\"compose.php?send_to=$url_replyto&reply_subj=$url_subj&reply_id=$passed_id&mailbox=$urlMailbox&ent_num=$ent_num\">";
b581fa60 326 echo _("Reply");
e5a06719 327 echo '</A>&nbsp;|&nbsp;';
429f8906 328 echo " <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\">";
b581fa60 329 echo _("Reply All");
e5a06719 330 echo '</A>&nbsp;&nbsp;';
331 echo ' </SMALL>';
332 echo ' </TD>';
333 echo ' </TR>';
334 echo ' </TABLE>';
335 echo ' </TD></TR>';
336 echo ' <TR><TD CELLSPACING="0" WIDTH="100%">';
9736fafe 337 echo ' <TABLE COLS="3" WIDTH="100%" BORDER="0" CELLSPACING="0" CELLPADDING="3">' . "\n";
e5a06719 338 echo ' <TR>' . "\n";
be69e508 339 /** subject **/
c36ed9cf 340 echo " <TD BGCOLOR=\"$color[0]\" WIDTH=15% ALIGN=RIGHT>\n";
b581fa60 341 echo _("Subject:");
c36ed9cf 342 echo " </TD><TD BGCOLOR=\"$color[0]\" WIDTH=84%>\n";
c221adce 343 echo " <B>$subject</B>&nbsp;\n";
be69e508 344 echo " </TD>\n";
9736fafe 345 echo ' <TD WIDTH=1% ROWSPAN=4 BGCOLOR="'.$color[0].'" ALIGN=right VALIGN=top NOWRAP><small>' . "\n";
f4991a86 346 if ($where && $what) {
347 // Got here from a search
9736fafe 348 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><br>\n";
349 } else {
350 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><br>\n";
f4991a86 351 }
9736fafe 352 do_hook("read_body_header_right");
353 echo ' </TR>' ."\n";
be69e508 354 /** from **/
e5a06719 355 echo ' <TR>' . "\n";
356 echo ' <TD BGCOLOR="' . $color[0] . '" WIDTH="15%" ALIGN="RIGHT">' . "\n";
b581fa60 357 echo _("From:");
9736fafe 358 echo ' </TD><TD BGCOLOR="' . $color[0] . '" WIDTH="85%">' . "\n";
c221adce 359 echo " <B>$from_name</B>&nbsp;\n";
e5a06719 360 echo ' </TD>' . "\n";
361 echo ' </TR>' . "\n";
be69e508 362 /** date **/
e5a06719 363 echo ' <TR>' . "\n";
364 echo ' <TD BGCOLOR="' . $color[0] . '" WIDTH="15%" ALIGN="RIGHT">' . "\n";
32c7898c 365 echo _("Date:");
9736fafe 366 echo " </TD><TD BGCOLOR=\"$color[0]\" WIDTH=85%>\n";
c221adce 367 echo " <B>$dateString</B>&nbsp;\n";
e5a06719 368 echo ' </TD>' . "\n";
369 echo ' </TR>' . "\n";
2844086d 370 /** to **/
371 echo " <TR>\n";
c36ed9cf 372 echo " <TD BGCOLOR=\"$color[0]\" WIDTH=15% ALIGN=RIGHT VALIGN=TOP>\n";
b581fa60 373 echo _("To:");
9736fafe 374 echo ' </TD><TD BGCOLOR="' . $color[0] . '" WIDTH="85%" VALIGN="TOP">' . "\n";
c221adce 375 echo " <B>$to_string</B>&nbsp;\n";
e5a06719 376 echo ' </TD>' . "\n";
377 echo ' </TR>' . "\n";
078a40a4 378 /** cc **/
e7681707 379 if (isset($cc_string)) {
078a40a4 380 echo " <TR>\n";
c36ed9cf 381 echo " <TD BGCOLOR=\"$color[0]\" WIDTH=15% ALIGN=RIGHT VALIGN=TOP>\n";
e5a06719 382 echo ' Cc:' . "\n";
c36ed9cf 383 echo " </TD><TD BGCOLOR=\"$color[0]\" WIDTH=85% VALIGN=TOP colspan=2>\n";
c221adce 384 echo " <B>$cc_string</B>&nbsp;\n";
e5a06719 385 echo ' </TD>' . "\n";
386 echo ' </TR>' . "\n";
078a40a4 387 }
cc24badd 388 do_hook("read_body_header");
e5a06719 389 echo '</TABLE>';
390 echo ' </TD></TR>';
391 echo '</table>';
a48fbf9b 392 echo "<TABLE COLS=1 CELLSPACING=0 WIDTH=97% BORDER=0 ALIGN=CENTER CELLPADDING=0>\n";
be69e508 393
f8f9bed9 394 echo " <TR><TD BGCOLOR=\"$color[4]\" WIDTH=100%>\n";
e5a06719 395 echo '<BR>';
9297917e 396
397 $body = formatBody($imapConnection, $message, $color, $wrap_at);
5c55c295 398
441f2d33 399 echo $body;
400
9c6d8388 401 echo '</TABLE>';
e5a06719 402 echo '<TABLE COLS="1" CELLSPACING="0" WIDTH="100%" BORDER="0" ALIGN="CENTER" CELLPADDING="0">' . "\n";
7831268e 403 echo " <TR><TD BGCOLOR=\"$color[9]\">&nbsp;</TD></TR>";
e5a06719 404 echo '</TABLE>' . "\n";
be69e508 405
e5a06719 406 do_hook('read_body_bottom');
407 do_hook('html_bottom');
1195c340 408 sqimap_logout($imapConnection);
b581fa60 409?>