* Moved load_prefs.php and display_page.php (or whatever it is called) into
[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 // returns the index of the previous message from the array
105 function findPreviousMessage() {
106 global $msort, $currentArrayIndex, $sort, $msgs, $imapConnection, $mailbox;
107 if ($sort == 6) {
108 $numMessages = sqimap_get_num_messages($imapConnection, $mailbox);
109 if ($currentArrayIndex != $numMessages) {
110 return $currentArrayIndex + 1;
111 }
112 } else {
113 for (reset($msort); ($key = key($msort)), (isset($key)); next($msort)) {
114 if ($currentArrayIndex == $msgs[$key]['ID']) {
115 prev($msort);
116 $key = key($msort);
117 if (isset($key))
118 return $msgs[$key]['ID'];
119 }
120 }
121 }
122 return -1;
123 }
124
125 if (isset($msgs)) {
126 $currentArrayIndex = $passed_id;
127 /*
128 for ($i=0; $i < count($msgs); $i++) {
129 if ($msgs[$i]["ID"] == $passed_id) {
130 $currentArrayIndex = $i;
131 break;
132 }
133 }
134 */
135 } else {
136 $currentArrayIndex = -1;
137 }
138
139 for ($i = 0; $i < count($msgs); $i++) {
140 if ($msgs[$i]['ID'] == $passed_id)
141 $msgs[$i]['FLAG_SEEN'] = true;
142 }
143
144 // $message contains all information about the message
145 // including header and body
146 $message = sqimap_get_message($imapConnection, $passed_id, $mailbox);
147
148 /** translate the subject and mailbox into url-able text **/
149 $url_subj = urlencode(trim($message->header->subject));
150 $urlMailbox = urlencode($mailbox);
151 $url_replyto = urlencode($message->header->replyto);
152
153 $url_replytoall = urlencode($message->header->replyto);
154
155 // If we are replying to all, then find all other addresses and
156 // add them to the list. Remove duplicates.
157 // This is somewhat messy, so I'll explain:
158 // 1) Take all addresses (from, to, cc) (avoid nasty join errors here)
159 $url_replytoall_extra_addrs = array_merge(array($message->header->from),
160 $message->header->to, $message->header->cc);
161
162 // 2) Make one big string out of them
163 $url_replytoall_extra_addrs = join(';', $url_replytoall_extra_addrs);
164
165 // 3) Parse that into an array of addresses
166 $url_replytoall_extra_addrs = parseAddrs($url_replytoall_extra_addrs);
167
168 // 4) Make them unique -- weed out duplicates
169 // (Coded for PHP 4.0.0)
170 $url_replytoall_extra_addrs =
171 array_keys(array_flip($url_replytoall_extra_addrs));
172
173 // 5) Remove the addresses we'll be sending the message 'to'
174 $url_replytoall_avoid_addrs = parseAddrs($message->header->replyto);
175 foreach ($url_replytoall_avoid_addrs as $addr)
176 {
177 foreach (array_keys($url_replytoall_extra_addrs, $addr) as $key_to_delete)
178 {
179 unset($url_replytoall_extra_addrs[$key_to_delete]);
180 }
181 }
182
183 // 6) Smoosh back into one nice line
184 $url_replytoallcc = getLineOfAddrs($url_replytoall_extra_addrs);
185
186 // 7) urlencode() it
187 $url_replytoallcc = urlencode($url_replytoallcc);
188
189 $dateString = getLongDateString($message->header->date);
190
191 // What do we reply to -- text only, if possible
192 $ent_num = findDisplayEntity($message);
193
194 /** TEXT STRINGS DEFINITIONS **/
195 $echo_more = _("more");
196 $echo_less = _("less");
197
198 if (!isset($show_more_cc)) $show_more_cc = false;
199
200 /** FORMAT THE TO STRING **/
201 $i = 0;
202 $to_string = '';
203 $to_ary = $message->header->to;
204 while ($i < count($to_ary)) {
205 $to_ary[$i] = htmlspecialchars(decodeHeader($to_ary[$i]));
206
207 if ($to_string)
208 $to_string = "$to_string<BR>$to_ary[$i]";
209 else
210 $to_string = "$to_ary[$i]";
211
212 $i++;
213 if (count($to_ary) > 1) {
214 if ($show_more == false) {
215 if ($i == 1) {
216 if (isset($where) && isset($what)) {
217 // from a search
218 $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>)";
219 } else {
220 $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>)";
221 }
222 $i = count($to_ary);
223 }
224 } else if ($i == 1) {
225 if (isset($where) && isset($what)) {
226 // from a search
227 $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>)";
228 } else {
229 $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>)";
230 }
231 }
232 }
233 }
234
235 /** FORMAT THE CC STRING **/
236 $i = 0;
237 if (isset ($message->header->cc[0]) && trim($message->header->cc[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 (isset($where) && isset($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 (isset($where) && isset($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 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" 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 WIDTH="100%" BORDER="0" CELLSPACING="0" CELLPADDING="3">' . "\n";
336 echo ' <TR>' . "\n";
337 /** subject **/
338 echo " <TD BGCOLOR=\"$color[0]\" WIDTH=\"10%\" ALIGN=\"right\" VALIGN=\"top\">\n";
339 echo _("Subject:");
340 echo " </TD><TD BGCOLOR=\"$color[0]\" WIDTH=\"80%\" VALIGN=\"top\">\n";
341 echo " <B>$subject</B>&nbsp;\n";
342 echo " </TD>\n";
343 echo ' <TD ROWSPAN="4" width=10% BGCOLOR="'.$color[0].'" ALIGN=right VALIGN=top NOWRAP><small>' . "\n";
344 if ($where && $what) {
345 // Got here from a search
346 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";
347 } else {
348 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";
349 }
350 do_hook("read_body_header_right");
351 echo '</small></TD>' . "\n";
352 echo ' </TR>' ."\n";
353 /** from **/
354 echo ' <TR>' . "\n";
355 echo ' <TD BGCOLOR="' . $color[0] . '" ALIGN="RIGHT">' . "\n";
356 echo _("From:");
357 echo ' </TD><TD BGCOLOR="' . $color[0] . '">' . "\n";
358 echo " <B>$from_name</B>&nbsp;\n";
359 echo ' </TD>' . "\n";
360 echo ' </TR>' . "\n";
361 /** date **/
362 echo ' <TR>' . "\n";
363 echo ' <TD BGCOLOR="' . $color[0] . '" ALIGN="RIGHT">' . "\n";
364 echo _("Date:");
365 echo " </TD><TD BGCOLOR=\"$color[0]\">\n";
366 echo " <B>$dateString</B>&nbsp;\n";
367 echo ' </TD>' . "\n";
368 echo ' </TR>' . "\n";
369 /** to **/
370 echo " <TR>\n";
371 echo " <TD BGCOLOR=\"$color[0]\" ALIGN=RIGHT VALIGN=TOP>\n";
372 echo _("To:");
373 echo ' </TD><TD BGCOLOR="' . $color[0] . '" VALIGN="TOP">' . "\n";
374 echo " <B>$to_string</B>&nbsp;\n";
375 echo ' </TD>' . "\n";
376 echo ' </TR>' . "\n";
377 /** cc **/
378 if (isset($cc_string)) {
379 echo " <TR>\n";
380 echo " <TD BGCOLOR=\"$color[0]\" ALIGN=RIGHT VALIGN=TOP>\n";
381 echo ' Cc:' . "\n";
382 echo " </TD><TD BGCOLOR=\"$color[0]\" VALIGN=TOP colspan=2>\n";
383 echo " <B>$cc_string</B>&nbsp;\n";
384 echo ' </TD>' . "\n";
385 echo ' </TR>' . "\n";
386 }
387 do_hook("read_body_header");
388 echo '</TABLE>';
389 echo ' </TD></TR>';
390 echo '</table>';
391 echo "<TABLE CELLSPACING=0 WIDTH=97% BORDER=0 ALIGN=CENTER CELLPADDING=0>\n";
392
393 echo " <TR><TD BGCOLOR=\"$color[4]\" WIDTH=100%>\n";
394 echo '<BR>';
395
396 $body = formatBody($imapConnection, $message, $color, $wrap_at);
397
398 echo $body;
399
400 echo '</TABLE>';
401 echo '<TABLE CELLSPACING="0" WIDTH="100%" BORDER="0" ALIGN="CENTER" CELLPADDING="0">' . "\n";
402 echo " <TR><TD BGCOLOR=\"$color[9]\">&nbsp;</TD></TR>";
403 echo '</TABLE>' . "\n";
404
405 do_hook('read_body_bottom');
406 do_hook('html_bottom');
407 sqimap_logout($imapConnection);
408 ?>