78cf9ba58ce65a53eb853e86948867b40e6231f3
[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 include("../src/validate.php");
15 include('../functions/imap.php');
16 include('../functions/mime.php');
17 include('../functions/date.php');
18 include('../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, "a003 FETCH $passed_id BODY[HEADER]\r\n");
27 $read = sqimap_read_data ($imapConnection, "a003", true, $a, $b);
28
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> - ';
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 echo "<table width=99% cellpadding=2 cellspacing=0 border=0 align=center>\n";
40 echo '<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, $mailbox;
118 if ($sort == 6) {
119 $numMessages = sqimap_get_num_messages($imapConnection, $mailbox);
120 if ($currentArrayIndex != $numMessages) {
121 return $currentArrayIndex + 1;
122 }
123 } else {
124 for (reset($msort); ($key = key($msort)), (isset($key)); next($msort)) {
125 if ($currentArrayIndex == $msgs[$key]['ID']) {
126 prev($msort);
127 $key = key($msort);
128 if (isset($key))
129 return $msgs[$key]['ID'];
130 }
131 }
132 }
133 return -1;
134 }
135
136 if (isset($msgs)) {
137 $currentArrayIndex = $passed_id;
138 /*
139 for ($i=0; $i < count($msgs); $i++) {
140 if ($msgs[$i]["ID"] == $passed_id) {
141 $currentArrayIndex = $i;
142 break;
143 }
144 }
145 */
146 } else {
147 $currentArrayIndex = -1;
148 }
149
150 for ($i = 0; $i < count($msgs); $i++) {
151 if ($msgs[$i]['ID'] == $passed_id)
152 $msgs[$i]['FLAG_SEEN'] = true;
153 }
154
155 // $message contains all information about the message
156 // including header and body
157 $message = sqimap_get_message($imapConnection, $passed_id, $mailbox);
158
159 /** translate the subject and mailbox into url-able text **/
160 $url_subj = urlencode(trim($message->header->subject));
161 $urlMailbox = urlencode($mailbox);
162 $url_replyto = urlencode($message->header->replyto);
163
164 $url_replytoall = urlencode($message->header->replyto);
165
166 // If we are replying to all, then find all other addresses and
167 // add them to the list. Remove duplicates.
168 // This is somewhat messy, so I'll explain:
169 // 1) Take all addresses (from, to, cc) (avoid nasty join errors here)
170 $url_replytoall_extra_addrs = array_merge(array($message->header->from),
171 $message->header->to, $message->header->cc);
172
173 // 2) Make one big string out of them
174 $url_replytoall_extra_addrs = join(';', $url_replytoall_extra_addrs);
175
176 // 3) Parse that into an array of addresses
177 $url_replytoall_extra_addrs = parseAddrs($url_replytoall_extra_addrs);
178
179 // 4) Make them unique -- weed out duplicates
180 // (Coded for PHP 4.0.0)
181 $url_replytoall_extra_addrs =
182 array_keys(array_flip($url_replytoall_extra_addrs));
183
184 // 5) Remove the addresses we'll be sending the message 'to'
185 $url_replytoall_avoid_addrs = parseAddrs($message->header->replyto);
186 foreach ($url_replytoall_avoid_addrs as $addr)
187 {
188 RemoveAddress($url_replytoall_extra_addrs, $addr);
189 }
190
191 // 6) Remove our identities from the CC list (they still can be in the
192 // TO list)
193 RemoveAddress($url_replytoall_extra_addrs, getPref($data_dir, $username,
194 'email_address'));
195 $idents = getPref($data_dir, $username, 'identities');
196 if ($idents != '' && $idents > 1)
197 {
198 for ($i = 1; $i < $idents; $i ++)
199 {
200 RemoveAddress($url_replytoall_extra_addrs,
201 getPref($data_dir, $username, 'email_address' . $i));
202 }
203 }
204
205 // 7) Smoosh back into one nice line
206 $url_replytoallcc = getLineOfAddrs($url_replytoall_extra_addrs);
207
208 // 8) urlencode() it
209 $url_replytoallcc = urlencode($url_replytoallcc);
210
211 $dateString = getLongDateString($message->header->date);
212
213 // What do we reply to -- text only, if possible
214 $ent_num = findDisplayEntity($message);
215
216 /** TEXT STRINGS DEFINITIONS **/
217 $echo_more = _("more");
218 $echo_less = _("less");
219
220 if (!isset($show_more_cc)) $show_more_cc = false;
221
222 /** FORMAT THE TO STRING **/
223 $i = 0;
224 $to_string = '';
225 $to_ary = $message->header->to;
226 while ($i < count($to_ary)) {
227 $to_ary[$i] = htmlspecialchars(decodeHeader($to_ary[$i]));
228
229 if ($to_string)
230 $to_string = "$to_string<BR>$to_ary[$i]";
231 else
232 $to_string = "$to_ary[$i]";
233
234 $i++;
235 if (count($to_ary) > 1) {
236 if ($show_more == false) {
237 if ($i == 1) {
238 if (isset($where) && isset($what)) {
239 // from a search
240 $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>)";
241 } else {
242 $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>)";
243 }
244 $i = count($to_ary);
245 }
246 } else if ($i == 1) {
247 if (isset($where) && isset($what)) {
248 // from a search
249 $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>)";
250 } else {
251 $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>)";
252 }
253 }
254 }
255 }
256
257 /** FORMAT THE CC STRING **/
258 $i = 0;
259 if (isset ($message->header->cc[0]) && trim($message->header->cc[0])){
260 $cc_string = "";
261 $cc_ary = $message->header->cc;
262 while ($i < count(decodeHeader($cc_ary))) {
263 $cc_ary[$i] = htmlspecialchars($cc_ary[$i]);
264 if ($cc_string)
265 $cc_string = "$cc_string<BR>$cc_ary[$i]";
266 else
267 $cc_string = "$cc_ary[$i]";
268
269 $i++;
270 if (count($cc_ary) > 1) {
271 if ($show_more_cc == false) {
272 if ($i == 1) {
273 if (isset($where) && isset($what)) {
274 // from a search
275 $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>)";
276 } else {
277 $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>)";
278 }
279 $i = count($cc_ary);
280 }
281 } else if ($i == 1) {
282 if (isset($where) && isset($what)) {
283 // from a search
284 $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>)";
285 } else {
286 $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>)";
287 }
288 }
289 }
290 }
291 }
292 /** make sure everything will display in HTML format **/
293 $from_name = decodeHeader(htmlspecialchars($message->header->from));
294 $subject = decodeHeader(htmlspecialchars($message->header->subject));
295
296 do_hook('read_body_top');
297 echo '<BR>';
298
299 echo '<TABLE CELLSPACING="0" WIDTH="100%" BORDER="0" ALIGN="CENTER" CELLPADDING="0">' . "\n";
300 echo ' <TR><TD BGCOLOR="' . $color[9] . '" WIDTH="100%">';
301 echo ' <TABLE WIDTH="100%" CELLSPACING="0" BORDER="0" CELLPADDING="3">';
302 echo ' <TR>';
303 echo ' <TD ALIGN="LEFT" WIDTH="33%">';
304 echo ' <SMALL>';
305 if ($where && $what) {
306 echo " <A HREF=\"search.php?where=".urlencode($where)."&what=".urlencode($what)."&mailbox=$urlMailbox\">";
307 } else {
308 echo " <A HREF=\"right_main.php?use_mailbox_cache=1&sort=$sort&startMessage=$startMessage&mailbox=$urlMailbox\">";
309 }
310 echo _("Message List");
311 echo '</A>&nbsp;|&nbsp;';
312 if ($where && $what) {
313 echo " <A HREF=\"delete_message.php?mailbox=$urlMailbox&message=$passed_id&where=".urlencode($where)."&what=".urlencode($what).'">';
314 } else {
315 echo " <A HREF=\"delete_message.php?mailbox=$urlMailbox&message=$passed_id&sort=$sort&startMessage=$startMessage\">";
316 }
317 echo _("Delete");
318 echo '</A>&nbsp;&nbsp;';
319 echo ' </SMALL>';
320 echo ' </TD><TD WIDTH="33%" ALIGN="CENTER">';
321 echo ' <SMALL>' . "\n";
322 if ($where && $what) {
323 } else {
324 if ($currentArrayIndex == -1) {
325 echo 'Previous&nbsp;|&nbsp;Next';
326 } else {
327 $prev = findPreviousMessage();
328 $next = findNextMessage();
329 if ($prev != -1)
330 echo "<a href=\"read_body.php?passed_id=$prev&mailbox=$urlMailbox&sort=$sort&startMessage=$startMessage&show_more=0\">" . _("Previous") . "</A>&nbsp;|&nbsp;";
331 else
332 echo _("Previous") . '&nbsp;|&nbsp;';
333 if ($next != -1)
334 echo "<a href=\"read_body.php?passed_id=$next&mailbox=$urlMailbox&sort=$sort&startMessage=$startMessage&show_more=0\">" . _("Next") . "</A>";
335 else
336 echo _("Next");
337 }
338 }
339 echo ' </SMALL>' . "\n";
340 echo ' </TD><TD WIDTH="33%" ALIGN="RIGHT">';
341 echo ' <SMALL>';
342 echo " <A HREF=\"compose.php?forward_id=$passed_id&forward_subj=$url_subj&mailbox=$urlMailbox&ent_num=$ent_num\">";
343 echo _("Forward");
344 echo '</A>&nbsp;|&nbsp;';
345 echo " <A HREF=\"compose.php?send_to=$url_replyto&reply_subj=$url_subj&reply_id=$passed_id&mailbox=$urlMailbox&ent_num=$ent_num\">";
346 echo _("Reply");
347 echo '</A>&nbsp;|&nbsp;';
348 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\">";
349 echo _("Reply All");
350 echo '</A>&nbsp;&nbsp;';
351 echo ' </SMALL>';
352 echo ' </TD>';
353 echo ' </TR>';
354 echo ' </TABLE>';
355 echo ' </TD></TR>';
356 echo ' <TR><TD CELLSPACING="0" WIDTH="100%">';
357 echo ' <TABLE WIDTH="100%" BORDER="0" CELLSPACING="0" CELLPADDING="3">' . "\n";
358 echo ' <TR>' . "\n";
359 /** subject **/
360 echo " <TD BGCOLOR=\"$color[0]\" WIDTH=\"10%\" ALIGN=\"right\" VALIGN=\"top\">\n";
361 echo _("Subject:");
362 echo " </TD><TD BGCOLOR=\"$color[0]\" WIDTH=\"80%\" VALIGN=\"top\">\n";
363 echo " <B>$subject</B>&nbsp;\n";
364 echo " </TD>\n";
365 echo ' <TD ROWSPAN="4" width=10% BGCOLOR="'.$color[0].'" ALIGN=right VALIGN=top NOWRAP><small>' . "\n";
366 if ($where && $what) {
367 // Got here from a search
368 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";
369 } else {
370 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";
371 }
372 do_hook("read_body_header_right");
373 echo '</small></TD>' . "\n";
374 echo ' </TR>' ."\n";
375 /** from **/
376 echo ' <TR>' . "\n";
377 echo ' <TD BGCOLOR="' . $color[0] . '" ALIGN="RIGHT">' . "\n";
378 echo _("From:");
379 echo ' </TD><TD BGCOLOR="' . $color[0] . '">' . "\n";
380 echo " <B>$from_name</B>&nbsp;\n";
381 echo ' </TD>' . "\n";
382 echo ' </TR>' . "\n";
383 /** date **/
384 echo ' <TR>' . "\n";
385 echo ' <TD BGCOLOR="' . $color[0] . '" ALIGN="RIGHT">' . "\n";
386 echo _("Date:");
387 echo " </TD><TD BGCOLOR=\"$color[0]\">\n";
388 echo " <B>$dateString</B>&nbsp;\n";
389 echo ' </TD>' . "\n";
390 echo ' </TR>' . "\n";
391 /** to **/
392 echo " <TR>\n";
393 echo " <TD BGCOLOR=\"$color[0]\" ALIGN=RIGHT VALIGN=TOP>\n";
394 echo _("To:");
395 echo ' </TD><TD BGCOLOR="' . $color[0] . '" VALIGN="TOP">' . "\n";
396 echo " <B>$to_string</B>&nbsp;\n";
397 echo ' </TD>' . "\n";
398 echo ' </TR>' . "\n";
399 /** cc **/
400 if (isset($cc_string)) {
401 echo " <TR>\n";
402 echo " <TD BGCOLOR=\"$color[0]\" ALIGN=RIGHT VALIGN=TOP>\n";
403 echo ' Cc:' . "\n";
404 echo " </TD><TD BGCOLOR=\"$color[0]\" VALIGN=TOP colspan=2>\n";
405 echo " <B>$cc_string</B>&nbsp;\n";
406 echo ' </TD>' . "\n";
407 echo ' </TR>' . "\n";
408 }
409 do_hook("read_body_header");
410 echo '</TABLE>';
411 echo ' </TD></TR>';
412 echo '</table>';
413 echo "<TABLE CELLSPACING=0 WIDTH=97% BORDER=0 ALIGN=CENTER CELLPADDING=0>\n";
414
415 echo " <TR><TD BGCOLOR=\"$color[4]\" WIDTH=100%>\n";
416 echo '<BR>';
417
418 $body = formatBody($imapConnection, $message, $color, $wrap_at);
419
420 echo $body;
421
422 echo '</TABLE>';
423 echo '<TABLE CELLSPACING="0" WIDTH="100%" BORDER="0" ALIGN="CENTER" CELLPADDING="0">' . "\n";
424 echo " <TR><TD BGCOLOR=\"$color[9]\">&nbsp;</TD></TR>";
425 echo '</TABLE>' . "\n";
426
427 do_hook('read_body_bottom');
428 do_hook('html_bottom');
429 sqimap_logout($imapConnection);
430 ?>