updated changelog
[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
2a32fc83 14 session_start();
15
d068c0ec 16 if (!isset($strings_php))
e5a06719 17 include('../functions/strings.php');
245a6892 18 if (!isset($config_php))
e5a06719 19 include('../config/config.php');
d068c0ec 20 if (!isset($page_header_php))
e5a06719 21 include('../functions/page_header.php');
d068c0ec 22 if (!isset($imap_php))
e5a06719 23 include('../functions/imap.php');
d068c0ec 24 if (!isset($mime_php))
e5a06719 25 include('../functions/mime.php');
d068c0ec 26 if (!isset($date_php))
e5a06719 27 include('../functions/date.php');
172fd718 28 if (!isset($url_parser_php))
e5a06719 29 include('../functions/url_parser.php');
be69e508 30
e5a06719 31 include('../src/load_prefs.php');
c36ed9cf 32 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
33 sqimap_mailbox_select($imapConnection, $mailbox);
e5a06719 34 do_hook('html_top');
c36ed9cf 35 displayPageHeader($color, $mailbox);
36
245a6892 37 if (isset($view_hdr)) {
c36ed9cf 38 fputs ($imapConnection, "a003 FETCH $passed_id BODY[HEADER]\r\n");
39 $read = sqimap_read_data ($imapConnection, "a003", true, $a, $b);
40
e5a06719 41 echo '<br>';
42 echo '<table width="100%" cellpadding="2" cellspacing="0" border="0" align="center">' . "\n";
43 echo " <TR><TD BGCOLOR=\"$color[9]\" WIDTH=100%><center><b>" . _("Viewing full header") . '</b> - ';
cd928157 44 if (isset($where) && isset($what)) {
f4991a86 45 // Got here from a search
e5a06719 46 echo "<a href=\"read_body.php?mailbox=".urlencode($mailbox)."&passed_id=$passed_id&where=".urlencode($where)."&what=".urlencode($what).'">';
8ae331c2 47 } else {
f4991a86 48 echo "<a href=\"read_body.php?mailbox=".urlencode($mailbox)."&passed_id=$passed_id&startMessage=$startMessage&show_more=$show_more\">";
49 }
e5a06719 50 echo ''._("View message") . "</a></b></center></td></tr></table>\n";
e9f8ea4e 51 echo "<table width=99% cellpadding=2 cellspacing=0 border=0 align=center>\n";
e5a06719 52 echo '<tr><td>';
5c54e435 53
cd928157 54 $cnum = 0;
c36ed9cf 55 for ($i=1; $i < count($read)-1; $i++) {
5c54e435 56 $line = htmlspecialchars($read[$i]);
172fd718 57 if (eregi("^&gt;", $line)) {
58 $second[$i] = $line;
e5a06719 59 $first[$i] = '&nbsp;';
172fd718 60 $cnum++;
61 } else if (eregi("^[ |\t]", $line)) {
62 $second[$i] = $line;
e5a06719 63 $first[$i] = '';
172fd718 64 } else if (eregi("^([^:]+):(.+)", $line, $regs)) {
e5a06719 65 $first[$i] = $regs[1] . ':';
172fd718 66 $second[$i] = $regs[2];
67 $cnum++;
5c54e435 68 } else {
172fd718 69 $second[$i] = trim($line);
e5a06719 70 $first[$i] = '';
c36ed9cf 71 }
172fd718 72 }
73 for ($i=0; $i < count($second); $i = $j) {
cd928157 74 if (isset($first[$i]))
75 $f = $first[$i];
76 if (isset($second[$i]))
77 $s = nl2br($second[$i]);
172fd718 78 $j = $i + 1;
e5a06719 79 while ($first[$j] == '' && $j < count($first)) {
80 $s .= '&nbsp;&nbsp;&nbsp;&nbsp;' . nl2br($second[$j]);
172fd718 81 $j++;
82 }
83 parseEmail($s);
cd928157 84 if (isset($f)) echo "<nobr><tt><b>$f</b>$s</tt></nobr>";
c36ed9cf 85 }
5c54e435 86 echo "</td></tr></table>\n";
e5a06719 87 echo '</body></html>';
eabc2883 88 sqimap_logout($imapConnection);
c36ed9cf 89 exit;
90 }
91
90033b64 92 // given an IMAP message id number, this will look it up in the cached and sorted msgs array and
93 // return the index. used for finding the next and previous messages
94
95 // returns the index of the next valid message from the array
96 function findNextMessage() {
5c54e435 97 global $msort, $currentArrayIndex, $msgs, $sort;
98
99 if ($sort == 6) {
100 if ($currentArrayIndex != 1) {
101 return $currentArrayIndex - 1;
102 }
103 } else {
104 for (reset($msort); ($key = key($msort)), (isset($key)); next($msort)) {
26f1e4f1 105 if ($currentArrayIndex == $msgs[$key]['ID']) {
5c54e435 106 next($msort);
107 $key = key($msort);
108 if (isset($key))
e5a06719 109 return $msgs[$key]['ID'];
5c54e435 110 }
111 }
112 }
90033b64 113 return -1;
114 }
115
116 // returns the index of the previous message from the array
117 function findPreviousMessage() {
5c54e435 118 global $msort, $currentArrayIndex, $sort, $msgs, $imapConnection, $mailbox;
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 {
189 foreach (array_keys($url_replytoall_extra_addrs, $addr) as $key_to_delete)
190 {
191 unset($url_replytoall_extra_addrs[$key_to_delete]);
192 }
193 }
194
195 // 6) Smoosh back into one nice line
196 $url_replytoallcc = getLineOfAddrs($url_replytoall_extra_addrs);
197
198 // 7) urlencode() it
b676ba7e 199 $url_replytoallcc = urlencode($url_replytoallcc);
4bfed9f3 200
8beafbbc 201 $dateString = getLongDateString($message->header->date);
3ae6e629 202
203 // What do we reply to -- text only, if possible
429f8906 204 $ent_num = findDisplayEntity($message);
31f3d7c0 205
b581fa60 206 /** TEXT STRINGS DEFINITIONS **/
207 $echo_more = _("more");
208 $echo_less = _("less");
209
e788294e 210 if (!isset($show_more_cc)) $show_more_cc = false;
211
078a40a4 212 /** FORMAT THE TO STRING **/
2844086d 213 $i = 0;
e5a06719 214 $to_string = '';
8beafbbc 215 $to_ary = $message->header->to;
2844086d 216 while ($i < count($to_ary)) {
99fa2b21 217 $to_ary[$i] = htmlspecialchars(decodeHeader($to_ary[$i]));
be8e07f8 218
2844086d 219 if ($to_string)
220 $to_string = "$to_string<BR>$to_ary[$i]";
221 else
222 $to_string = "$to_ary[$i]";
223
224 $i++;
225 if (count($to_ary) > 1) {
226 if ($show_more == false) {
227 if ($i == 1) {
30833080 228 if (isset($where) && isset($what)) {
f4991a86 229 // from a search
230 $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>)";
231 } else {
232 $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>)";
233 }
2844086d 234 $i = count($to_ary);
235 }
236 } else if ($i == 1) {
30833080 237 if (isset($where) && isset($what)) {
f4991a86 238 // from a search
239 $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>)";
240 } else {
241 $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>)";
242 }
2844086d 243 }
244 }
245 }
246
078a40a4 247 /** FORMAT THE CC STRING **/
248 $i = 0;
e7681707 249 if (isset ($message->header->cc[0]) && trim($message->header->cc[0])){
250 $cc_string = "";
251 $cc_ary = $message->header->cc;
252 while ($i < count(decodeHeader($cc_ary))) {
253 $cc_ary[$i] = htmlspecialchars($cc_ary[$i]);
254 if ($cc_string)
255 $cc_string = "$cc_string<BR>$cc_ary[$i]";
256 else
257 $cc_string = "$cc_ary[$i]";
258
259 $i++;
260 if (count($cc_ary) > 1) {
261 if ($show_more_cc == false) {
262 if ($i == 1) {
30833080 263 if (isset($where) && isset($what)) {
e7681707 264 // from a search
265 $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>)";
266 } else {
267 $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>)";
268 }
269 $i = count($cc_ary);
270 }
271 } else if ($i == 1) {
30833080 272 if (isset($where) && isset($what)) {
f4991a86 273 // from a search
e7681707 274 $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 275 } else {
e7681707 276 $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 277 }
078a40a4 278 }
078a40a4 279 }
280 }
281 }
f7fb20fe 282 /** make sure everything will display in HTML format **/
8beafbbc 283 $from_name = decodeHeader(htmlspecialchars($message->header->from));
7aaa81fc 284 $subject = decodeHeader(htmlspecialchars($message->header->subject));
078a40a4 285
e5a06719 286 do_hook('read_body_top');
287 echo '<BR>';
8ae331c2 288
e5a06719 289 echo '<TABLE COLS="1" CELLSPACING="0" WIDTH="100%" BORDER="0" ALIGN="CENTER" CELLPADDING="0">' . "\n";
290 echo ' <TR><TD BGCOLOR="' . $color[9] . '" WIDTH="100%">';
291 echo ' <TABLE WIDTH="100%" CELLSPACING="0" BORDER="0" COLS="2" CELLPADDING="3">';
292 echo ' <TR>';
293 echo ' <TD ALIGN="LEFT" WIDTH="33%">';
294 echo ' <SMALL>';
1809bad8 295 if ($where && $what) {
296 echo " <A HREF=\"search.php?where=".urlencode($where)."&what=".urlencode($what)."&mailbox=$urlMailbox\">";
297 } else {
298 echo " <A HREF=\"right_main.php?use_mailbox_cache=1&sort=$sort&startMessage=$startMessage&mailbox=$urlMailbox\">";
299 }
b581fa60 300 echo _("Message List");
e5a06719 301 echo '</A>&nbsp;|&nbsp;';
1809bad8 302 if ($where && $what) {
e5a06719 303 echo " <A HREF=\"delete_message.php?mailbox=$urlMailbox&message=$passed_id&where=".urlencode($where)."&what=".urlencode($what).'">';
1809bad8 304 } else {
d36f3e63 305 echo " <A HREF=\"delete_message.php?mailbox=$urlMailbox&message=$passed_id&sort=$sort&startMessage=$startMessage\">";
1809bad8 306 }
b581fa60 307 echo _("Delete");
e5a06719 308 echo '</A>&nbsp;&nbsp;';
309 echo ' </SMALL>';
310 echo ' </TD><TD WIDTH="33%" ALIGN="CENTER">';
311 echo ' <SMALL>' . "\n";
1809bad8 312 if ($where && $what) {
90033b64 313 } else {
1809bad8 314 if ($currentArrayIndex == -1) {
e5a06719 315 echo 'Previous&nbsp;|&nbsp;Next';
1809bad8 316 } else {
317 $prev = findPreviousMessage();
318 $next = findNextMessage();
319 if ($prev != -1)
320 echo "<a href=\"read_body.php?passed_id=$prev&mailbox=$urlMailbox&sort=$sort&startMessage=$startMessage&show_more=0\">" . _("Previous") . "</A>&nbsp;|&nbsp;";
321 else
e5a06719 322 echo _("Previous") . '&nbsp;|&nbsp;';
1809bad8 323 if ($next != -1)
324 echo "<a href=\"read_body.php?passed_id=$next&mailbox=$urlMailbox&sort=$sort&startMessage=$startMessage&show_more=0\">" . _("Next") . "</A>";
325 else
326 echo _("Next");
327 }
328 }
e5a06719 329 echo ' </SMALL>' . "\n";
330 echo ' </TD><TD WIDTH="33%" ALIGN="RIGHT">';
331 echo ' <SMALL>';
429f8906 332 echo " <A HREF=\"compose.php?forward_id=$passed_id&forward_subj=$url_subj&mailbox=$urlMailbox&ent_num=$ent_num\">";
b581fa60 333 echo _("Forward");
e5a06719 334 echo '</A>&nbsp;|&nbsp;';
429f8906 335 echo " <A HREF=\"compose.php?send_to=$url_replyto&reply_subj=$url_subj&reply_id=$passed_id&mailbox=$urlMailbox&ent_num=$ent_num\">";
b581fa60 336 echo _("Reply");
e5a06719 337 echo '</A>&nbsp;|&nbsp;';
429f8906 338 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 339 echo _("Reply All");
e5a06719 340 echo '</A>&nbsp;&nbsp;';
341 echo ' </SMALL>';
342 echo ' </TD>';
343 echo ' </TR>';
344 echo ' </TABLE>';
345 echo ' </TD></TR>';
346 echo ' <TR><TD CELLSPACING="0" WIDTH="100%">';
347 echo ' <TABLE COLS="2" WIDTH="100%" BORDER="0" CELLSPACING="0" CELLPADDING="3">' . "\n";
348 echo ' <TR>' . "\n";
be69e508 349 /** subject **/
c36ed9cf 350 echo " <TD BGCOLOR=\"$color[0]\" WIDTH=15% ALIGN=RIGHT>\n";
b581fa60 351 echo _("Subject:");
c36ed9cf 352 echo " </TD><TD BGCOLOR=\"$color[0]\" WIDTH=84%>\n";
c221adce 353 echo " <B>$subject</B>&nbsp;\n";
be69e508 354 echo " </TD>\n";
f4991a86 355 if ($where && $what) {
356 // Got here from a search
357 echo " <TD WIDTH=1% bgcolor=\"$color[0]\" nowrap align=right><small><a href=\"read_body.php?mailbox=$urlMailbox&passed_id=$passed_id&where=".urlencode($where)."&what=".urlencode($what)."&view_hdr=1\">" . _("View full header") . "</a></small>&nbsp;&nbsp;</td>";
358 } else {
359 echo " <TD WIDTH=1% bgcolor=\"$color[0]\" nowrap align=right><small><a href=\"read_body.php?mailbox=$urlMailbox&passed_id=$passed_id&startMessage=$startMessage&show_more=$show_more&view_hdr=1\">" . _("View full header") . "</a></small>&nbsp;&nbsp;</td>";
360 }
e5a06719 361 echo ' </TR>' ."\n";
be69e508 362 /** from **/
e5a06719 363 echo ' <TR>' . "\n";
364 echo ' <TD BGCOLOR="' . $color[0] . '" WIDTH="15%" ALIGN="RIGHT">' . "\n";
b581fa60 365 echo _("From:");
e5a06719 366 echo ' </TD><TD BGCOLOR="' . $color[0] . '" WIDTH="85%" colspan="2">' . "\n";
c221adce 367 echo " <B>$from_name</B>&nbsp;\n";
e5a06719 368 echo ' </TD>' . "\n";
369 echo ' </TR>' . "\n";
be69e508 370 /** date **/
e5a06719 371 echo ' <TR>' . "\n";
372 echo ' <TD BGCOLOR="' . $color[0] . '" WIDTH="15%" ALIGN="RIGHT">' . "\n";
32c7898c 373 echo _("Date:");
c36ed9cf 374 echo " </TD><TD BGCOLOR=\"$color[0]\" WIDTH=85% colspan=2>\n";
c221adce 375 echo " <B>$dateString</B>&nbsp;\n";
e5a06719 376 echo ' </TD>' . "\n";
377 echo ' </TR>' . "\n";
2844086d 378 /** to **/
379 echo " <TR>\n";
c36ed9cf 380 echo " <TD BGCOLOR=\"$color[0]\" WIDTH=15% ALIGN=RIGHT VALIGN=TOP>\n";
b581fa60 381 echo _("To:");
e5a06719 382 echo ' </TD><TD BGCOLOR="' . $color[0] . '" WIDTH="85%" VALIGN="TOP" colspan="2">' . "\n";
c221adce 383 echo " <B>$to_string</B>&nbsp;\n";
e5a06719 384 echo ' </TD>' . "\n";
385 echo ' </TR>' . "\n";
078a40a4 386 /** cc **/
e7681707 387 if (isset($cc_string)) {
078a40a4 388 echo " <TR>\n";
c36ed9cf 389 echo " <TD BGCOLOR=\"$color[0]\" WIDTH=15% ALIGN=RIGHT VALIGN=TOP>\n";
e5a06719 390 echo ' Cc:' . "\n";
c36ed9cf 391 echo " </TD><TD BGCOLOR=\"$color[0]\" WIDTH=85% VALIGN=TOP colspan=2>\n";
c221adce 392 echo " <B>$cc_string</B>&nbsp;\n";
e5a06719 393 echo ' </TD>' . "\n";
394 echo ' </TR>' . "\n";
078a40a4 395 }
cc24badd 396 do_hook("read_body_header");
e5a06719 397 echo '</TABLE>';
398 echo ' </TD></TR>';
399 echo '</table>';
a48fbf9b 400 echo "<TABLE COLS=1 CELLSPACING=0 WIDTH=97% BORDER=0 ALIGN=CENTER CELLPADDING=0>\n";
be69e508 401
f8f9bed9 402 echo " <TR><TD BGCOLOR=\"$color[4]\" WIDTH=100%>\n";
e5a06719 403 echo '<BR>';
9297917e 404
405 $body = formatBody($imapConnection, $message, $color, $wrap_at);
5c55c295 406
441f2d33 407 echo $body;
408
e5a06719 409 echo '<TABLE COLS="1" CELLSPACING="0" WIDTH="100%" BORDER="0" ALIGN="CENTER" CELLPADDING="0">' . "\n";
7831268e 410 echo " <TR><TD BGCOLOR=\"$color[9]\">&nbsp;</TD></TR>";
e5a06719 411 echo '</TABLE>' . "\n";
be69e508 412
e5a06719 413 do_hook('read_body_bottom');
414 do_hook('html_bottom');
1195c340 415 sqimap_logout($imapConnection);
b581fa60 416?>