Fixed major problems with IMAP session handling in the MIME code. This
[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
27 include("../src/load_prefs.php");
28 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
29 sqimap_mailbox_select($imapConnection, $mailbox);
30 displayPageHeader($color, $mailbox);
31
32 if ($view_hdr) {
33 fputs ($imapConnection, "a003 FETCH $passed_id BODY[HEADER]\r\n");
34 $read = sqimap_read_data ($imapConnection, "a003", true, $a, $b);
35
36 echo "<br>";
37 echo "<table width=95% cellpadding=2 cellspacing=0 border=0 align=center>\n";
38 echo " <TR><TD BGCOLOR=\"$color[9]\" WIDTH=100%><center><b>" . _("Viewing full header") . "</b> - ";
39 echo "<a href=\"read_body.php?mailbox=".urlencode($mailbox)."&passed_id=$passed_id&startMessage=$startMessage&show_more=$show_more\">";
40 echo ""._("View message") . "</a></b></center></td></tr>\n";
41 echo "<tr><td><pre>";
42 for ($i=1; $i < count($read)-1; $i++) {
43 $read[$i] = htmlspecialchars($read[$i]);
44 if (substr($read[$i], 0, 1) != "\t" &&
45 substr($read[$i], 0, 1) != " " &&
46 substr($read[$i], 0, 1) != "&" &&
47 trim($read[$i])) {
48 $pre = substr($read[$i], 0, strpos($read[$i], ":"));
49 $read[$i] = str_replace("$pre", "<b>$pre</b>", $read[$i]);
50 }
51 echo "$read[$i]";
52 }
53 echo "</pre></td></tr></table>\n";
54 echo "</body></html>";
55 exit;
56 }
57
58 // given an IMAP message id number, this will look it up in the cached and sorted msgs array and
59 // return the index. used for finding the next and previous messages
60
61 // returns the index of the next valid message from the array
62 function findNextMessage() {
63 global $msort, $currentArrayIndex, $msgs;
64 for (reset($msort); ($key = key($msort)), (isset($key)); next($msort)) {
65 if ($currentArrayIndex == $msgs[$key]["ID"]) {
66 next($msort);
67 $key = key($msort);
68 if (isset($key))
69 return $msgs[$key]["ID"];
70 }
71 }
72 return -1;
73 }
74
75 // returns the index of the previous message from the array
76 function findPreviousMessage() {
77 global $msort, $currentArrayIndex, $msgs;
78 for (reset($msort); ($key = key($msort)), (isset($key)); next($msort)) {
79 if ($currentArrayIndex == $msgs[$key]["ID"]) {
80 prev($msort);
81 $key = key($msort);
82 if (isset($key))
83 return $msgs[$key]["ID"];
84 }
85 }
86 return -1;
87 }
88
89 if (isset($msgs)) {
90 $currentArrayIndex = $passed_id;
91 /*
92 for ($i=0; $i < count($msgs); $i++) {
93 if ($msgs[$i]["ID"] == $passed_id) {
94 $currentArrayIndex = $i;
95 break;
96 }
97 }
98 */
99 } else {
100 $currentArrayIndex = -1;
101 }
102
103 for ($i = 0; $i < count($msgs); $i++) {
104 if ($msgs[$i]["ID"] == $passed_id)
105 $msgs[$i]["FLAG_SEEN"] = true;
106 }
107
108 // $message contains all information about the message
109 // including header and body
110 $message = sqimap_get_message($imapConnection, $passed_id, $mailbox);
111
112 /** translate the subject and mailbox into url-able text **/
113 $url_subj = urlencode(trim(stripslashes($message->header->subject)));
114 $urlMailbox = urlencode($mailbox);
115 $url_replyto = urlencode($message->header->replyto);
116
117 $url_replytoall = urlencode($message->header->replyto);
118 $url_replytoallcc = urlencode(getLineOfAddrs($message->header->to) . ", " . getLineOfAddrs($message->header->cc));
119
120 $dateString = getLongDateString($message->header->date);
121 $ent_num = findDisplayEntity($message);
122
123 /** TEXT STRINGS DEFINITIONS **/
124 $echo_more = _("more");
125 $echo_less = _("less");
126
127 /** FORMAT THE TO STRING **/
128 $i = 0;
129 $to_string = "";
130 $to_ary = $message->header->to;
131 while ($i < count($to_ary)) {
132 $to_ary[$i] = htmlspecialchars($to_ary[$i]);
133
134 if ($to_string)
135 $to_string = "$to_string<BR>$to_ary[$i]";
136 else
137 $to_string = "$to_ary[$i]";
138
139 $i++;
140 if (count($to_ary) > 1) {
141 if ($show_more == false) {
142 if ($i == 1) {
143 $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>)";
144 $i = count($to_ary);
145 }
146 } else if ($i == 1) {
147 $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>)";
148 }
149 }
150 }
151
152 /** FORMAT THE CC STRING **/
153 $i = 0;
154 $cc_string = "";
155 $cc_ary = $message->header->cc;
156 while ($i < count($cc_ary)) {
157 $cc_ary[$i] = htmlspecialchars($cc_ary[$i]);
158 if ($cc_string)
159 $cc_string = "$cc_string<BR>$cc_ary[$i]";
160 else
161 $cc_string = "$cc_ary[$i]";
162
163 $i++;
164 if (count($cc_ary) > 1) {
165 if ($show_more_cc == false) {
166 if ($i == 1) {
167 $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>)";
168 $i = count($cc_ary);
169 }
170 } else if ($i == 1) {
171 $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>)";
172 }
173 }
174 }
175
176 /** make sure everything will display in HTML format **/
177 $from_name = decodeHeader(htmlspecialchars($message->header->from));
178 $subject = decodeHeader(htmlspecialchars(stripslashes($message->header->subject)));
179
180 echo "<BR>";
181 echo "<TABLE COLS=1 CELLSPACING=0 WIDTH=98% BORDER=0 ALIGN=CENTER CELLPADDING=0>\n";
182 echo " <TR><TD BGCOLOR=\"$color[9]\" WIDTH=100%>";
183 echo " <TABLE WIDTH=100% CELLSPACING=0 BORDER=0 COLS=2 CELLPADDING=3>";
184 echo " <TR>";
185 echo " <TD ALIGN=LEFT WIDTH=33%>";
186 echo " <SMALL>";
187 echo " <A HREF=\"right_main.php?use_mailbox_cache=1&sort=$sort&startMessage=$startMessage&mailbox=$urlMailbox\">";
188 echo _("Message List");
189 echo "</A>&nbsp;|&nbsp;";
190 echo " <A HREF=\"delete_message.php?mailbox=$urlMailbox&message=$passed_id&sort=$sort&startMessage=1\">";
191 echo _("Delete");
192 echo "</A>&nbsp;&nbsp;";
193 echo " </SMALL>";
194 echo " </TD><TD WIDTH=33% ALIGN=CENTER>";
195 echo " <SMALL>\n";
196 if ($currentArrayIndex == -1) {
197 echo "Previous&nbsp;|&nbsp;Next";
198 } else {
199 $prev = findPreviousMessage();
200 $next = findNextMessage();
201 if ($prev != -1)
202 echo "<a href=\"read_body.php?passed_id=$prev&mailbox=$mailbox&sort=$sort&startMessage=$startMessage&show_more=0\">" . _("Previous") . "</A>&nbsp;|&nbsp;";
203 else
204 echo _("Previous") . "&nbsp;|&nbsp;";
205 if ($next != -1)
206 echo "<a href=\"read_body.php?passed_id=$next&mailbox=$mailbox&sort=$sort&startMessage=$startMessage&show_more=0\">" . _("Next") . "</A>";
207 else
208 echo _("Next");
209 }
210 echo " </SMALL>\n";
211 echo " </TD><TD WIDTH=33% ALIGN=RIGHT>";
212 echo " <SMALL>";
213 echo " <A HREF=\"compose.php?forward_id=$passed_id&forward_subj=$url_subj&mailbox=$urlMailbox&ent_num=$ent_num\">";
214 echo _("Forward");
215 echo "</A>&nbsp;|&nbsp;";
216 echo " <A HREF=\"compose.php?send_to=$url_replyto&reply_subj=$url_subj&reply_id=$passed_id&mailbox=$urlMailbox&ent_num=$ent_num\">";
217 echo _("Reply");
218 echo "</A>&nbsp;|&nbsp;";
219 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\">";
220 echo _("Reply All");
221 echo "</A>&nbsp;&nbsp;";
222 echo " </SMALL>";
223 echo " </TD>";
224 echo " </TR>";
225 echo " </TABLE>";
226 echo " </TD></TR>";
227 echo " <TR><TD CELLSPACING=0 WIDTH=100%>";
228 echo " <TABLE COLS=2 WIDTH=100% BORDER=0 CELLSPACING=0 CELLPADDING=3>\n";
229 echo " <TR>\n";
230 /** subject **/
231 echo " <TD BGCOLOR=\"$color[0]\" WIDTH=15% ALIGN=RIGHT>\n";
232 echo _("Subject:");
233 echo " </TD><TD BGCOLOR=\"$color[0]\" WIDTH=84%>\n";
234 echo " <B>$subject</B>\n";
235 echo " </TD>\n";
236 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>";
237 echo " </TR>\n";
238 /** from **/
239 echo " <TR>\n";
240 echo " <TD BGCOLOR=\"$color[0]\" WIDTH=15% ALIGN=RIGHT>\n";
241 echo _("From:");
242 echo " </TD><TD BGCOLOR=\"$color[0]\" WIDTH=85% colspan=2>\n";
243 echo " <B>$from_name</B>\n";
244 echo " </TD>\n";
245 echo " </TR>\n";
246 /** date **/
247 echo " <TR>\n";
248 echo " <TD BGCOLOR=\"$color[0]\" WIDTH=15% ALIGN=RIGHT>\n";
249 echo _("Date:");
250 echo " </TD><TD BGCOLOR=\"$color[0]\" WIDTH=85% colspan=2>\n";
251 echo " <B>$dateString</B>\n";
252 echo " </TD>\n";
253 echo " </TR>\n";
254 /** to **/
255 echo " <TR>\n";
256 echo " <TD BGCOLOR=\"$color[0]\" WIDTH=15% ALIGN=RIGHT VALIGN=TOP>\n";
257 echo _("To:");
258 echo " </TD><TD BGCOLOR=\"$color[0]\" WIDTH=85% VALIGN=TOP colspan=2>\n";
259 echo " <B>$to_string</B>\n";
260 echo " </TD>\n";
261 echo " </TR>\n";
262 /** cc **/
263 if ($message->header->cc) {
264 echo " <TR>\n";
265 echo " <TD BGCOLOR=\"$color[0]\" WIDTH=15% ALIGN=RIGHT VALIGN=TOP>\n";
266 echo " Cc:\n";
267 echo " </TD><TD BGCOLOR=\"$color[0]\" WIDTH=85% VALIGN=TOP colspan=2>\n";
268 echo " <B>$cc_string</B>\n";
269 echo " </TD>\n";
270 echo " </TR>\n";
271 }
272 echo "</TABLE>";
273 echo " </TD></TR>";
274
275 echo " <TR><TD BGCOLOR=\"$color[4]\" WIDTH=100%>\n";
276 $body = formatBody($imapConnection, $message, $color, $wrap_at);
277 echo "<BR>";
278
279 echo "$body";
280
281 echo " </TD></TR>\n";
282 echo " <TR><TD BGCOLOR=\"$color[9]\">&nbsp;</TD></TR>";
283 echo "</TABLE>\n";
284
285 sqimap_logout($imapConnection);
286 ?>