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