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