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