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