fixed some problems with reply all
[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 $url_replytoallcc = $message->header->from . ", ";
162 $url_replytoallcc .= getLineOfAddrs($message->header->to);
163 $url_replytoallcc_cc = getLineOfAddrs($message->header->cc);
164 if ($url_replytoallcc) {
165 if ($url_replytoallcc_cc) {
166 $url_replytoallcc .= ", " . $url_replytoallcc_cc;
167 }
168 } else {
169 if ($url_replytoallcc_cc) {
170 $url_replytoallcc = $url_replytoallcc_cc;
171 } else {
172 $url_replytoallcc = "";
173 }
174 }
175 $url_replytoallcc = urlencode($url_replytoallcc);
176
177 $dateString = getLongDateString($message->header->date);
178 $ent_num = findDisplayEntity($message);
179
180 /** TEXT STRINGS DEFINITIONS **/
181 $echo_more = _("more");
182 $echo_less = _("less");
183
184 /** FORMAT THE TO STRING **/
185 $i = 0;
186 $to_string = "";
187 $to_ary = $message->header->to;
188 while ($i < count($to_ary)) {
189 $to_ary[$i] = htmlspecialchars(decodeHeader($to_ary[$i]));
190
191 if ($to_string)
192 $to_string = "$to_string<BR>$to_ary[$i]";
193 else
194 $to_string = "$to_ary[$i]";
195
196 $i++;
197 if (count($to_ary) > 1) {
198 if ($show_more == false) {
199 if ($i == 1) {
200 if ($where && $what) {
201 // from a search
202 $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>)";
203 } else {
204 $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>)";
205 }
206 $i = count($to_ary);
207 }
208 } else if ($i == 1) {
209 if ($where && $what) {
210 // from a search
211 $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>)";
212 } else {
213 $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>)";
214 }
215 }
216 }
217 }
218
219 /** FORMAT THE CC STRING **/
220 $i = 0;
221 $cc_string = "";
222 $cc_ary = $message->header->cc;
223 while ($i < count(decodeHeader($cc_ary))) {
224 $cc_ary[$i] = htmlspecialchars($cc_ary[$i]);
225 if ($cc_string)
226 $cc_string = "$cc_string<BR>$cc_ary[$i]";
227 else
228 $cc_string = "$cc_ary[$i]";
229
230 $i++;
231 if (count($cc_ary) > 1) {
232 if ($show_more_cc == false) {
233 if ($i == 1) {
234 if ($where && $what) {
235 // from a search
236 $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>)";
237 } else {
238 $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>)";
239 }
240 $i = count($cc_ary);
241 }
242 } else if ($i == 1) {
243 if ($where && $what) {
244 // from a search
245 $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>)";
246 } else {
247 $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>)";
248 }
249 }
250 }
251 }
252
253 /** make sure everything will display in HTML format **/
254 $from_name = decodeHeader(htmlspecialchars($message->header->from));
255 $subject = decodeHeader(htmlspecialchars($message->header->subject));
256
257 do_hook("read_body_top");
258 echo "<BR>";
259
260 echo "<TABLE COLS=1 CELLSPACING=0 WIDTH=100% BORDER=0 ALIGN=CENTER CELLPADDING=0>\n";
261 echo " <TR><TD BGCOLOR=\"$color[9]\" WIDTH=100%>";
262 echo " <TABLE WIDTH=100% CELLSPACING=0 BORDER=0 COLS=2 CELLPADDING=3>";
263 echo " <TR>";
264 echo " <TD ALIGN=LEFT WIDTH=33%>";
265 echo " <SMALL>";
266 if ($where && $what) {
267 echo " <A HREF=\"search.php?where=".urlencode($where)."&what=".urlencode($what)."&mailbox=$urlMailbox\">";
268 } else {
269 echo " <A HREF=\"right_main.php?use_mailbox_cache=1&sort=$sort&startMessage=$startMessage&mailbox=$urlMailbox\">";
270 }
271 echo _("Message List");
272 echo "</A>&nbsp;|&nbsp;";
273 if ($where && $what) {
274 echo " <A HREF=\"delete_message.php?mailbox=$urlMailbox&message=$passed_id&where=".urlencode($where)."&what=".urlencode($what)."\">";
275 } else {
276 echo " <A HREF=\"delete_message.php?mailbox=$urlMailbox&message=$passed_id&sort=$sort&startMessage=$startMessage\">";
277 }
278 echo _("Delete");
279 echo "</A>&nbsp;&nbsp;";
280 echo " </SMALL>";
281 echo " </TD><TD WIDTH=33% ALIGN=CENTER>";
282 echo " <SMALL>\n";
283 if ($where && $what) {
284 } else {
285 if ($currentArrayIndex == -1) {
286 echo "Previous&nbsp;|&nbsp;Next";
287 } else {
288 $prev = findPreviousMessage();
289 $next = findNextMessage();
290 if ($prev != -1)
291 echo "<a href=\"read_body.php?passed_id=$prev&mailbox=$urlMailbox&sort=$sort&startMessage=$startMessage&show_more=0\">" . _("Previous") . "</A>&nbsp;|&nbsp;";
292 else
293 echo _("Previous") . "&nbsp;|&nbsp;";
294 if ($next != -1)
295 echo "<a href=\"read_body.php?passed_id=$next&mailbox=$urlMailbox&sort=$sort&startMessage=$startMessage&show_more=0\">" . _("Next") . "</A>";
296 else
297 echo _("Next");
298 }
299 }
300 echo " </SMALL>\n";
301 echo " </TD><TD WIDTH=33% ALIGN=RIGHT>";
302 echo " <SMALL>";
303 echo " <A HREF=\"compose.php?forward_id=$passed_id&forward_subj=$url_subj&mailbox=$urlMailbox&ent_num=$ent_num\">";
304 echo _("Forward");
305 echo "</A>&nbsp;|&nbsp;";
306 echo " <A HREF=\"compose.php?send_to=$url_replyto&reply_subj=$url_subj&reply_id=$passed_id&mailbox=$urlMailbox&ent_num=$ent_num\">";
307 echo _("Reply");
308 echo "</A>&nbsp;|&nbsp;";
309 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\">";
310 echo _("Reply All");
311 echo "</A>&nbsp;&nbsp;";
312 echo " </SMALL>";
313 echo " </TD>";
314 echo " </TR>";
315 echo " </TABLE>";
316 echo " </TD></TR>";
317 echo " <TR><TD CELLSPACING=0 WIDTH=100%>";
318 echo " <TABLE COLS=2 WIDTH=100% BORDER=0 CELLSPACING=0 CELLPADDING=3>\n";
319 echo " <TR>\n";
320 /** subject **/
321 echo " <TD BGCOLOR=\"$color[0]\" WIDTH=15% ALIGN=RIGHT>\n";
322 echo _("Subject:");
323 echo " </TD><TD BGCOLOR=\"$color[0]\" WIDTH=84%>\n";
324 echo " <B>$subject</B>&nbsp;\n";
325 echo " </TD>\n";
326 if ($where && $what) {
327 // Got here from a search
328 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>";
329 } else {
330 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>";
331 }
332 echo " </TR>\n";
333 /** from **/
334 echo " <TR>\n";
335 echo " <TD BGCOLOR=\"$color[0]\" WIDTH=15% ALIGN=RIGHT>\n";
336 echo _("From:");
337 echo " </TD><TD BGCOLOR=\"$color[0]\" WIDTH=85% colspan=2>\n";
338 echo " <B>$from_name</B>&nbsp;\n";
339 echo " </TD>\n";
340 echo " </TR>\n";
341 /** date **/
342 echo " <TR>\n";
343 echo " <TD BGCOLOR=\"$color[0]\" WIDTH=15% ALIGN=RIGHT>\n";
344 echo _("Date:");
345 echo " </TD><TD BGCOLOR=\"$color[0]\" WIDTH=85% colspan=2>\n";
346 echo " <B>$dateString</B>&nbsp;\n";
347 echo " </TD>\n";
348 echo " </TR>\n";
349 /** to **/
350 echo " <TR>\n";
351 echo " <TD BGCOLOR=\"$color[0]\" WIDTH=15% ALIGN=RIGHT VALIGN=TOP>\n";
352 echo _("To:");
353 echo " </TD><TD BGCOLOR=\"$color[0]\" WIDTH=85% VALIGN=TOP colspan=2>\n";
354 echo " <B>$to_string</B>&nbsp;\n";
355 echo " </TD>\n";
356 echo " </TR>\n";
357 /** cc **/
358 if ($message->header->cc) {
359 echo " <TR>\n";
360 echo " <TD BGCOLOR=\"$color[0]\" WIDTH=15% ALIGN=RIGHT VALIGN=TOP>\n";
361 echo " Cc:\n";
362 echo " </TD><TD BGCOLOR=\"$color[0]\" WIDTH=85% VALIGN=TOP colspan=2>\n";
363 echo " <B>$cc_string</B>&nbsp;\n";
364 echo " </TD>\n";
365 echo " </TR>\n";
366 }
367 echo "</TABLE>";
368 echo " </TD></TR>";
369 echo "</table>";
370 echo "<TABLE COLS=1 CELLSPACING=0 WIDTH=97% BORDER=0 ALIGN=CENTER CELLPADDING=0>\n";
371
372 echo " <TR><TD BGCOLOR=\"$color[4]\" WIDTH=100%>\n";
373 echo "<BR>";
374
375 $body = formatBody($imapConnection, $message, $color, $wrap_at);
376
377 echo $body;
378
379 echo "<TABLE COLS=1 CELLSPACING=0 WIDTH=100% BORDER=0 ALIGN=CENTER CELLPADDING=0>\n";
380 echo " <TR><TD BGCOLOR=\"$color[9]\">&nbsp;</TD></TR>";
381 echo "</TABLE>\n";
382
383 do_hook("read_body_bottom");
384 do_hook("html_bottom");
385 sqimap_logout($imapConnection);
386 ?>