if any of the standard headers are blank, read_body.php doesn't show
[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=100% 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 if ($where && $what) {
40 // Got here from a search
41 echo "<a href=\"read_body.php?mailbox=".urlencode($mailbox)."&passed_id=$passed_id&where=".urlencode($where)."&what=".urlencode($what)."\">";
42 } else {
43 echo "<a href=\"read_body.php?mailbox=".urlencode($mailbox)."&passed_id=$passed_id&startMessage=$startMessage&show_more=$show_more\">";
44 }
45 echo ""._("View message") . "</a></b></center></td></tr></table>\n";
46 echo "<table width=99% cellpadding=2 cellspacing=0 border=0 align=center>\n";
47 echo "<tr><td><pre>";
48 for ($i=1; $i < count($read)-1; $i++) {
49 $read[$i] = htmlspecialchars($read[$i]);
50 if (substr($read[$i], 0, 1) != "\t" &&
51 substr($read[$i], 0, 1) != " " &&
52 substr($read[$i], 0, 1) != "&" &&
53 trim($read[$i])) {
54 $pre = substr($read[$i], 0, strpos($read[$i], ":"));
55 $read[$i] = str_replace("$pre", "<b>$pre</b>", decodeHeader($read[$i]));
56 }
57 echo "$read[$i]";
58 }
59 echo "</pre></td></tr></table>\n";
60 echo "</body></html>";
61 sqimap_mailbox_close($imapConnection);
62 sqimap_logout($imapConnection);
63 exit;
64 }
65
66 // given an IMAP message id number, this will look it up in the cached and sorted msgs array and
67 // return the index. used for finding the next and previous messages
68
69 // returns the index of the next valid message from the array
70 function findNextMessage() {
71 global $msort, $currentArrayIndex, $msgs;
72 for (reset($msort); ($key = key($msort)), (isset($key)); next($msort)) {
73 if ($currentArrayIndex == $msgs[$key]["ID"]) {
74 next($msort);
75 $key = key($msort);
76 if (isset($key))
77 return $msgs[$key]["ID"];
78 }
79 }
80 return -1;
81 }
82
83 // returns the index of the previous message from the array
84 function findPreviousMessage() {
85 global $msort, $currentArrayIndex, $msgs;
86 for (reset($msort); ($key = key($msort)), (isset($key)); next($msort)) {
87 if ($currentArrayIndex == $msgs[$key]["ID"]) {
88 prev($msort);
89 $key = key($msort);
90 if (isset($key))
91 return $msgs[$key]["ID"];
92 }
93 }
94 return -1;
95 }
96
97 if (isset($msgs)) {
98 $currentArrayIndex = $passed_id;
99 /*
100 for ($i=0; $i < count($msgs); $i++) {
101 if ($msgs[$i]["ID"] == $passed_id) {
102 $currentArrayIndex = $i;
103 break;
104 }
105 }
106 */
107 } else {
108 $currentArrayIndex = -1;
109 }
110
111 for ($i = 0; $i < count($msgs); $i++) {
112 if ($msgs[$i]["ID"] == $passed_id)
113 $msgs[$i]["FLAG_SEEN"] = true;
114 }
115
116 // $message contains all information about the message
117 // including header and body
118 $message = sqimap_get_message($imapConnection, $passed_id, $mailbox);
119
120 /** translate the subject and mailbox into url-able text **/
121 $url_subj = urlencode(trim(sqStripSlashes($message->header->subject)));
122 $urlMailbox = urlencode($mailbox);
123 $url_replyto = urlencode($message->header->replyto);
124
125 $url_replytoall = urlencode($message->header->replyto);
126 $url_replytoallcc = getLineOfAddrs($message->header->to);
127 $url_replytoallcc_cc = getLineOfAddrs($message->header->cc);
128 if ($url_replytoallcc) {
129 if ($url_replytoallcc_cc) {
130 $url_replytoallcc .= ", " . $url_replytoallcc_cc;
131 }
132 } else {
133 if ($url_replytoallcc_cc) {
134 $url_replytoallcc = $url_replytoallcc_cc;
135 } else {
136 $url_replytoallcc = "";
137 }
138 }
139 $url_replytoallcc = urlencode($url_replytoallcc);
140
141 $dateString = getLongDateString($message->header->date);
142 $ent_num = findDisplayEntity($message);
143
144 /** TEXT STRINGS DEFINITIONS **/
145 $echo_more = _("more");
146 $echo_less = _("less");
147
148 /** FORMAT THE TO STRING **/
149 $i = 0;
150 $to_string = "";
151 $to_ary = $message->header->to;
152 while ($i < count($to_ary)) {
153 $to_ary[$i] = htmlspecialchars(decodeHeader($to_ary[$i]));
154
155 if ($to_string)
156 $to_string = "$to_string<BR>$to_ary[$i]";
157 else
158 $to_string = "$to_ary[$i]";
159
160 $i++;
161 if (count($to_ary) > 1) {
162 if ($show_more == false) {
163 if ($i == 1) {
164 if ($where && $what) {
165 // from a search
166 $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>)";
167 } else {
168 $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>)";
169 }
170 $i = count($to_ary);
171 }
172 } else if ($i == 1) {
173 if ($where && $what) {
174 // from a search
175 $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>)";
176 } else {
177 $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>)";
178 }
179 }
180 }
181 }
182
183 /** FORMAT THE CC STRING **/
184 $i = 0;
185 $cc_string = "";
186 $cc_ary = $message->header->cc;
187 while ($i < count(decodeHeader($cc_ary))) {
188 $cc_ary[$i] = htmlspecialchars($cc_ary[$i]);
189 if ($cc_string)
190 $cc_string = "$cc_string<BR>$cc_ary[$i]";
191 else
192 $cc_string = "$cc_ary[$i]";
193
194 $i++;
195 if (count($cc_ary) > 1) {
196 if ($show_more_cc == false) {
197 if ($i == 1) {
198 if ($where && $what) {
199 // from a search
200 $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>)";
201 } else {
202 $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>)";
203 }
204 $i = count($cc_ary);
205 }
206 } else if ($i == 1) {
207 if ($where && $what) {
208 // from a search
209 $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>)";
210 } else {
211 $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>)";
212 }
213 }
214 }
215 }
216
217 /** make sure everything will display in HTML format **/
218 $from_name = decodeHeader(htmlspecialchars($message->header->from));
219 $subject = decodeHeader(htmlspecialchars($message->header->subject));
220
221 do_hook("read_body_top");
222 echo "<BR>";
223 echo "<TABLE COLS=1 CELLSPACING=0 WIDTH=100% BORDER=0 ALIGN=CENTER CELLPADDING=0>\n";
224 echo " <TR><TD BGCOLOR=\"$color[9]\" WIDTH=100%>";
225 echo " <TABLE WIDTH=100% CELLSPACING=0 BORDER=0 COLS=2 CELLPADDING=3>";
226 echo " <TR>";
227 echo " <TD ALIGN=LEFT WIDTH=33%>";
228 echo " <SMALL>";
229 if ($where && $what) {
230 echo " <A HREF=\"search.php?where=".urlencode($where)."&what=".urlencode($what)."&mailbox=$urlMailbox\">";
231 } else {
232 echo " <A HREF=\"right_main.php?use_mailbox_cache=1&sort=$sort&startMessage=$startMessage&mailbox=$urlMailbox\">";
233 }
234 echo _("Message List");
235 echo "</A>&nbsp;|&nbsp;";
236 if ($where && $what) {
237 echo " <A HREF=\"delete_message.php?mailbox=$urlMailbox&message=$passed_id&where=".urlencode($where)."&what=".urlencode($what)."\">";
238 } else {
239 echo " <A HREF=\"delete_message.php?mailbox=$urlMailbox&message=$passed_id&sort=$sort&startMessage=1\">";
240 }
241 echo _("Delete");
242 echo "</A>&nbsp;&nbsp;";
243 echo " </SMALL>";
244 echo " </TD><TD WIDTH=33% ALIGN=CENTER>";
245 echo " <SMALL>\n";
246 if ($where && $what) {
247 } else {
248 if ($currentArrayIndex == -1) {
249 echo "Previous&nbsp;|&nbsp;Next";
250 } else {
251 $prev = findPreviousMessage();
252 $next = findNextMessage();
253 if ($prev != -1)
254 echo "<a href=\"read_body.php?passed_id=$prev&mailbox=$urlMailbox&sort=$sort&startMessage=$startMessage&show_more=0\">" . _("Previous") . "</A>&nbsp;|&nbsp;";
255 else
256 echo _("Previous") . "&nbsp;|&nbsp;";
257 if ($next != -1)
258 echo "<a href=\"read_body.php?passed_id=$next&mailbox=$urlMailbox&sort=$sort&startMessage=$startMessage&show_more=0\">" . _("Next") . "</A>";
259 else
260 echo _("Next");
261 }
262 }
263 echo " </SMALL>\n";
264 echo " </TD><TD WIDTH=33% ALIGN=RIGHT>";
265 echo " <SMALL>";
266 echo " <A HREF=\"compose.php?forward_id=$passed_id&forward_subj=$url_subj&mailbox=$urlMailbox&ent_num=$ent_num\">";
267 echo _("Forward");
268 echo "</A>&nbsp;|&nbsp;";
269 echo " <A HREF=\"compose.php?send_to=$url_replyto&reply_subj=$url_subj&reply_id=$passed_id&mailbox=$urlMailbox&ent_num=$ent_num\">";
270 echo _("Reply");
271 echo "</A>&nbsp;|&nbsp;";
272 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\">";
273 echo _("Reply All");
274 echo "</A>&nbsp;&nbsp;";
275 echo " </SMALL>";
276 echo " </TD>";
277 echo " </TR>";
278 echo " </TABLE>";
279 echo " </TD></TR>";
280 echo " <TR><TD CELLSPACING=0 WIDTH=100%>";
281 echo " <TABLE COLS=2 WIDTH=100% BORDER=0 CELLSPACING=0 CELLPADDING=3>\n";
282 echo " <TR>\n";
283 /** subject **/
284 echo " <TD BGCOLOR=\"$color[0]\" WIDTH=15% ALIGN=RIGHT>\n";
285 echo _("Subject:");
286 echo " </TD><TD BGCOLOR=\"$color[0]\" WIDTH=84%>\n";
287 echo " <B>$subject</B>&nbsp;\n";
288 echo " </TD>\n";
289 if ($where && $what) {
290 // Got here from a search
291 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>";
292 } else {
293 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>";
294 }
295 echo " </TR>\n";
296 /** from **/
297 echo " <TR>\n";
298 echo " <TD BGCOLOR=\"$color[0]\" WIDTH=15% ALIGN=RIGHT>\n";
299 echo _("From:");
300 echo " </TD><TD BGCOLOR=\"$color[0]\" WIDTH=85% colspan=2>\n";
301 echo " <B>$from_name</B>&nbsp;\n";
302 echo " </TD>\n";
303 echo " </TR>\n";
304 /** date **/
305 echo " <TR>\n";
306 echo " <TD BGCOLOR=\"$color[0]\" WIDTH=15% ALIGN=RIGHT>\n";
307 echo _("Date:");
308 echo " </TD><TD BGCOLOR=\"$color[0]\" WIDTH=85% colspan=2>\n";
309 echo " <B>$dateString</B>&nbsp;\n";
310 echo " </TD>\n";
311 echo " </TR>\n";
312 /** to **/
313 echo " <TR>\n";
314 echo " <TD BGCOLOR=\"$color[0]\" WIDTH=15% ALIGN=RIGHT VALIGN=TOP>\n";
315 echo _("To:");
316 echo " </TD><TD BGCOLOR=\"$color[0]\" WIDTH=85% VALIGN=TOP colspan=2>\n";
317 echo " <B>$to_string</B>&nbsp;\n";
318 echo " </TD>\n";
319 echo " </TR>\n";
320 /** cc **/
321 if ($message->header->cc) {
322 echo " <TR>\n";
323 echo " <TD BGCOLOR=\"$color[0]\" WIDTH=15% ALIGN=RIGHT VALIGN=TOP>\n";
324 echo " Cc:\n";
325 echo " </TD><TD BGCOLOR=\"$color[0]\" WIDTH=85% VALIGN=TOP colspan=2>\n";
326 echo " <B>$cc_string</B>&nbsp;\n";
327 echo " </TD>\n";
328 echo " </TR>\n";
329 }
330 echo "</TABLE>";
331 echo " </TD></TR>";
332 echo "</table>";
333 echo "<TABLE COLS=1 CELLSPACING=0 WIDTH=97% BORDER=0 ALIGN=CENTER CELLPADDING=0>\n";
334
335 echo " <TR><TD BGCOLOR=\"$color[4]\" WIDTH=100%>\n";
336 echo "<BR>";
337
338 $body = formatBody($imapConnection, $message, $color, $wrap_at);
339
340 echo "$body";
341
342 echo "<TABLE COLS=1 CELLSPACING=0 WIDTH=100% BORDER=0 ALIGN=CENTER CELLPADDING=0>\n";
343 echo " <TR><TD BGCOLOR=\"$color[9]\">&nbsp;</TD></TR>";
344 echo "</TABLE>\n";
345
346 do_hook("read_body_bottom");
347 sqimap_logout($imapConnection);
348 ?>