added bugfix from 1.0.3cvs
[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 session_start();
15
16 if (!isset($strings_php))
17 include('../functions/strings.php');
18 if (!isset($config_php))
19 include('../config/config.php');
20 if (!isset($page_header_php))
21 include('../functions/page_header.php');
22 if (!isset($imap_php))
23 include('../functions/imap.php');
24 if (!isset($mime_php))
25 include('../functions/mime.php');
26 if (!isset($date_php))
27 include('../functions/date.php');
28 if (!isset($url_parser_php))
29 include('../functions/url_parser.php');
30
31 include('../src/load_prefs.php');
32 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
33 sqimap_mailbox_select($imapConnection, $mailbox);
34 do_hook('html_top');
35 displayPageHeader($color, $mailbox);
36
37 if (isset($view_hdr)) {
38 fputs ($imapConnection, "a003 FETCH $passed_id BODY[HEADER]\r\n");
39 $read = sqimap_read_data ($imapConnection, "a003", true, $a, $b);
40
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> - ';
44 if (isset($where) && isset($what)) {
45 // Got here from a search
46 echo "<a href=\"read_body.php?mailbox=".urlencode($mailbox)."&passed_id=$passed_id&where=".urlencode($where)."&what=".urlencode($what).'">';
47 } else {
48 echo "<a href=\"read_body.php?mailbox=".urlencode($mailbox)."&passed_id=$passed_id&startMessage=$startMessage&show_more=$show_more\">";
49 }
50 echo ''._("View message") . "</a></b></center></td></tr></table>\n";
51 echo "<table width=99% cellpadding=2 cellspacing=0 border=0 align=center>\n";
52 echo '<tr><td>';
53
54 $cnum = 0;
55 for ($i=1; $i < count($read)-1; $i++) {
56 $line = htmlspecialchars($read[$i]);
57 if (eregi("^&gt;", $line)) {
58 $second[$i] = $line;
59 $first[$i] = '&nbsp;';
60 $cnum++;
61 } else if (eregi("^[ |\t]", $line)) {
62 $second[$i] = $line;
63 $first[$i] = '';
64 } else if (eregi("^([^:]+):(.+)", $line, $regs)) {
65 $first[$i] = $regs[1] . ':';
66 $second[$i] = $regs[2];
67 $cnum++;
68 } else {
69 $second[$i] = trim($line);
70 $first[$i] = '';
71 }
72 }
73 for ($i=0; $i < count($second); $i = $j) {
74 if (isset($first[$i]))
75 $f = $first[$i];
76 if (isset($second[$i]))
77 $s = nl2br($second[$i]);
78 $j = $i + 1;
79 while ($first[$j] == '' && $j < count($first)) {
80 $s .= '&nbsp;&nbsp;&nbsp;&nbsp;' . nl2br($second[$j]);
81 $j++;
82 }
83 parseEmail($s);
84 if (isset($f)) echo "<nobr><tt><b>$f</b>$s</tt></nobr>";
85 }
86 echo "</td></tr></table>\n";
87 echo '</body></html>';
88 sqimap_logout($imapConnection);
89 exit;
90 }
91
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() {
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)) {
105 if ($currentArrayIndex == $msgs[$key]['ID']) {
106 next($msort);
107 $key = key($msort);
108 if (isset($key))
109 return $msgs[$key]['ID'];
110 }
111 }
112 }
113 return -1;
114 }
115
116 // returns the index of the previous message from the array
117 function findPreviousMessage() {
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)) {
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 = urlencode($message->header->replyto);
164
165 $url_replytoall = urlencode($message->header->replyto);
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
181 // (Coded for PHP 4.0.0)
182 $url_replytoall_extra_addrs =
183 array_keys(array_flip($url_replytoall_extra_addrs));
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
199 $url_replytoallcc = urlencode($url_replytoallcc);
200
201 $dateString = getLongDateString($message->header->date);
202 $ent_num = findDisplayEntity($message);
203
204 /** TEXT STRINGS DEFINITIONS **/
205 $echo_more = _("more");
206 $echo_less = _("less");
207
208 if (!isset($show_more_cc)) $show_more_cc = false;
209
210 /** FORMAT THE TO STRING **/
211 $i = 0;
212 $to_string = '';
213 $to_ary = $message->header->to;
214 while ($i < count($to_ary)) {
215 $to_ary[$i] = htmlspecialchars(decodeHeader($to_ary[$i]));
216
217 if ($to_string)
218 $to_string = "$to_string<BR>$to_ary[$i]";
219 else
220 $to_string = "$to_ary[$i]";
221
222 $i++;
223 if (count($to_ary) > 1) {
224 if ($show_more == false) {
225 if ($i == 1) {
226 if (isset($where) && isset($what)) {
227 // from a search
228 $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>)";
229 } else {
230 $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>)";
231 }
232 $i = count($to_ary);
233 }
234 } else if ($i == 1) {
235 if (isset($where) && isset($what)) {
236 // from a search
237 $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>)";
238 } else {
239 $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>)";
240 }
241 }
242 }
243 }
244
245 /** FORMAT THE CC STRING **/
246 $i = 0;
247 if (isset ($message->header->cc[0]) && trim($message->header->cc[0])){
248 $cc_string = "";
249 $cc_ary = $message->header->cc;
250 while ($i < count(decodeHeader($cc_ary))) {
251 $cc_ary[$i] = htmlspecialchars($cc_ary[$i]);
252 if ($cc_string)
253 $cc_string = "$cc_string<BR>$cc_ary[$i]";
254 else
255 $cc_string = "$cc_ary[$i]";
256
257 $i++;
258 if (count($cc_ary) > 1) {
259 if ($show_more_cc == false) {
260 if ($i == 1) {
261 if (isset($where) && isset($what)) {
262 // from a search
263 $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>)";
264 } else {
265 $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>)";
266 }
267 $i = count($cc_ary);
268 }
269 } else if ($i == 1) {
270 if (isset($where) && isset($what)) {
271 // from a search
272 $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>)";
273 } else {
274 $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>)";
275 }
276 }
277 }
278 }
279 }
280 /** make sure everything will display in HTML format **/
281 $from_name = decodeHeader(htmlspecialchars($message->header->from));
282 $subject = decodeHeader(htmlspecialchars($message->header->subject));
283
284 do_hook('read_body_top');
285 echo '<BR>';
286
287 echo '<TABLE COLS="1" CELLSPACING="0" WIDTH="100%" BORDER="0" ALIGN="CENTER" CELLPADDING="0">' . "\n";
288 echo ' <TR><TD BGCOLOR="' . $color[9] . '" WIDTH="100%">';
289 echo ' <TABLE WIDTH="100%" CELLSPACING="0" BORDER="0" COLS="2" CELLPADDING="3">';
290 echo ' <TR>';
291 echo ' <TD ALIGN="LEFT" WIDTH="33%">';
292 echo ' <SMALL>';
293 if ($where && $what) {
294 echo " <A HREF=\"search.php?where=".urlencode($where)."&what=".urlencode($what)."&mailbox=$urlMailbox\">";
295 } else {
296 echo " <A HREF=\"right_main.php?use_mailbox_cache=1&sort=$sort&startMessage=$startMessage&mailbox=$urlMailbox\">";
297 }
298 echo _("Message List");
299 echo '</A>&nbsp;|&nbsp;';
300 if ($where && $what) {
301 echo " <A HREF=\"delete_message.php?mailbox=$urlMailbox&message=$passed_id&where=".urlencode($where)."&what=".urlencode($what).'">';
302 } else {
303 echo " <A HREF=\"delete_message.php?mailbox=$urlMailbox&message=$passed_id&sort=$sort&startMessage=$startMessage\">";
304 }
305 echo _("Delete");
306 echo '</A>&nbsp;&nbsp;';
307 echo ' </SMALL>';
308 echo ' </TD><TD WIDTH="33%" ALIGN="CENTER">';
309 echo ' <SMALL>' . "\n";
310 if ($where && $what) {
311 } else {
312 if ($currentArrayIndex == -1) {
313 echo 'Previous&nbsp;|&nbsp;Next';
314 } else {
315 $prev = findPreviousMessage();
316 $next = findNextMessage();
317 if ($prev != -1)
318 echo "<a href=\"read_body.php?passed_id=$prev&mailbox=$urlMailbox&sort=$sort&startMessage=$startMessage&show_more=0\">" . _("Previous") . "</A>&nbsp;|&nbsp;";
319 else
320 echo _("Previous") . '&nbsp;|&nbsp;';
321 if ($next != -1)
322 echo "<a href=\"read_body.php?passed_id=$next&mailbox=$urlMailbox&sort=$sort&startMessage=$startMessage&show_more=0\">" . _("Next") . "</A>";
323 else
324 echo _("Next");
325 }
326 }
327 echo ' </SMALL>' . "\n";
328 echo ' </TD><TD WIDTH="33%" ALIGN="RIGHT">';
329 echo ' <SMALL>';
330 echo " <A HREF=\"compose.php?forward_id=$passed_id&forward_subj=$url_subj&mailbox=$urlMailbox&ent_num=$ent_num\">";
331 echo _("Forward");
332 echo '</A>&nbsp;|&nbsp;';
333 echo " <A HREF=\"compose.php?send_to=$url_replyto&reply_subj=$url_subj&reply_id=$passed_id&mailbox=$urlMailbox&ent_num=$ent_num\">";
334 echo _("Reply");
335 echo '</A>&nbsp;|&nbsp;';
336 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\">";
337 echo _("Reply All");
338 echo '</A>&nbsp;&nbsp;';
339 echo ' </SMALL>';
340 echo ' </TD>';
341 echo ' </TR>';
342 echo ' </TABLE>';
343 echo ' </TD></TR>';
344 echo ' <TR><TD CELLSPACING="0" WIDTH="100%">';
345 echo ' <TABLE COLS="2" WIDTH="100%" BORDER="0" CELLSPACING="0" CELLPADDING="3">' . "\n";
346 echo ' <TR>' . "\n";
347 /** subject **/
348 echo " <TD BGCOLOR=\"$color[0]\" WIDTH=15% ALIGN=RIGHT>\n";
349 echo _("Subject:");
350 echo " </TD><TD BGCOLOR=\"$color[0]\" WIDTH=84%>\n";
351 echo " <B>$subject</B>&nbsp;\n";
352 echo " </TD>\n";
353 if ($where && $what) {
354 // Got here from a search
355 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>";
356 } else {
357 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>";
358 }
359 echo ' </TR>' ."\n";
360 /** from **/
361 echo ' <TR>' . "\n";
362 echo ' <TD BGCOLOR="' . $color[0] . '" WIDTH="15%" ALIGN="RIGHT">' . "\n";
363 echo _("From:");
364 echo ' </TD><TD BGCOLOR="' . $color[0] . '" WIDTH="85%" colspan="2">' . "\n";
365 echo " <B>$from_name</B>&nbsp;\n";
366 echo ' </TD>' . "\n";
367 echo ' </TR>' . "\n";
368 /** date **/
369 echo ' <TR>' . "\n";
370 echo ' <TD BGCOLOR="' . $color[0] . '" WIDTH="15%" ALIGN="RIGHT">' . "\n";
371 echo _("Date:");
372 echo " </TD><TD BGCOLOR=\"$color[0]\" WIDTH=85% colspan=2>\n";
373 echo " <B>$dateString</B>&nbsp;\n";
374 echo ' </TD>' . "\n";
375 echo ' </TR>' . "\n";
376 /** to **/
377 echo " <TR>\n";
378 echo " <TD BGCOLOR=\"$color[0]\" WIDTH=15% ALIGN=RIGHT VALIGN=TOP>\n";
379 echo _("To:");
380 echo ' </TD><TD BGCOLOR="' . $color[0] . '" WIDTH="85%" VALIGN="TOP" colspan="2">' . "\n";
381 echo " <B>$to_string</B>&nbsp;\n";
382 echo ' </TD>' . "\n";
383 echo ' </TR>' . "\n";
384 /** cc **/
385 if (isset($cc_string)) {
386 echo " <TR>\n";
387 echo " <TD BGCOLOR=\"$color[0]\" WIDTH=15% ALIGN=RIGHT VALIGN=TOP>\n";
388 echo ' Cc:' . "\n";
389 echo " </TD><TD BGCOLOR=\"$color[0]\" WIDTH=85% VALIGN=TOP colspan=2>\n";
390 echo " <B>$cc_string</B>&nbsp;\n";
391 echo ' </TD>' . "\n";
392 echo ' </TR>' . "\n";
393 }
394 do_hook("read_body_header");
395 echo '</TABLE>';
396 echo ' </TD></TR>';
397 echo '</table>';
398 echo "<TABLE COLS=1 CELLSPACING=0 WIDTH=97% BORDER=0 ALIGN=CENTER CELLPADDING=0>\n";
399
400 echo " <TR><TD BGCOLOR=\"$color[4]\" WIDTH=100%>\n";
401 echo '<BR>';
402
403 $body = formatBody($imapConnection, $message, $color, $wrap_at);
404
405 echo $body;
406
407 echo '<TABLE COLS="1" CELLSPACING="0" WIDTH="100%" BORDER="0" ALIGN="CENTER" CELLPADDING="0">' . "\n";
408 echo " <TR><TD BGCOLOR=\"$color[9]\">&nbsp;</TD></TR>";
409 echo '</TABLE>' . "\n";
410
411 do_hook('read_body_bottom');
412 do_hook('html_bottom');
413 sqimap_logout($imapConnection);
414 ?>