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