Removed a lot of the warnings generated when PHP has all warnings enabled.
[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 session_start();
15
16 if (!isset($strings_php))
17 include("../functions/strings.php");
18 if (!isset($config_php))
19 include("../config/config.php");
20 if (!isset($page_header_php))
21 include("../functions/page_header.php");
22 if (!isset($imap_php))
23 include("../functions/imap.php");
24 if (!isset($mime_php))
25 include("../functions/mime.php");
26 if (!isset($date_php))
27 include("../functions/date.php");
28 if (!isset($url_parser_php))
29 include("../functions/url_parser.php");
30
31 include("../src/load_prefs.php");
32 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
33 sqimap_mailbox_select($imapConnection, $mailbox);
34 do_hook("html_top");
35 displayPageHeader($color, $mailbox);
36
37 if (isset($view_hdr)) {
38 fputs ($imapConnection, "a003 FETCH $passed_id BODY[HEADER]\r\n");
39 $read = sqimap_read_data ($imapConnection, "a003", true, $a, $b);
40
41 echo "<br>";
42 echo "<table width=100% cellpadding=2 cellspacing=0 border=0 align=center>\n";
43 echo " <TR><TD BGCOLOR=\"$color[9]\" WIDTH=100%><center><b>" . _("Viewing full header") . "</b> - ";
44 if ($where && $what) {
45 // Got here from a search
46 echo "<a href=\"read_body.php?mailbox=".urlencode($mailbox)."&passed_id=$passed_id&where=".urlencode($where)."&what=".urlencode($what)."\">";
47 } else {
48 echo "<a href=\"read_body.php?mailbox=".urlencode($mailbox)."&passed_id=$passed_id&startMessage=$startMessage&show_more=$show_more\">";
49 }
50 echo ""._("View message") . "</a></b></center></td></tr></table>\n";
51 echo "<table width=99% cellpadding=2 cellspacing=0 border=0 align=center>\n";
52 echo "<tr><td>";
53
54 for ($i=1; $i < count($read)-1; $i++) {
55 $line = htmlspecialchars($read[$i]);
56 if (eregi("^&gt;", $line)) {
57 $second[$i] = $line;
58 $first[$i] = "&nbsp;";
59 $cnum++;
60 } else if (eregi("^[ |\t]", $line)) {
61 $second[$i] = $line;
62 $first[$i] = "";
63 } else if (eregi("^([^:]+):(.+)", $line, $regs)) {
64 $first[$i] = $regs[1] . ":";
65 $second[$i] = $regs[2];
66 $cnum++;
67 } else {
68 $second[$i] = trim($line);
69 $first[$i] = "";
70 }
71 }
72 for ($i=0; $i < count($second); $i = $j) {
73 $f = $first[$i];
74 $s = nl2br($second[$i]);
75 $j = $i + 1;
76 while ($first[$j] == "" && $j < count($first)) {
77 $s .= "&nbsp;&nbsp;&nbsp;&nbsp;" . nl2br($second[$j]);
78 $j++;
79 }
80 parseEmail($s);
81 echo "<nobr><tt><b>$f</b>$s</tt></nobr>";
82 }
83 echo "</td></tr></table>\n";
84 echo "</body></html>";
85 sqimap_logout($imapConnection);
86 exit;
87 }
88
89 // given an IMAP message id number, this will look it up in the cached and sorted msgs array and
90 // return the index. used for finding the next and previous messages
91
92 // returns the index of the next valid message from the array
93 function findNextMessage() {
94 global $msort, $currentArrayIndex, $msgs, $sort;
95
96 if ($sort == 6) {
97 if ($currentArrayIndex != 1) {
98 return $currentArrayIndex - 1;
99 }
100 } else {
101 for (reset($msort); ($key = key($msort)), (isset($key)); next($msort)) {
102 if ($currentArrayIndex == $msgs[$key]["ID"]) {
103 next($msort);
104 $key = key($msort);
105 if (isset($key))
106 return $msgs[$key]["ID"];
107 }
108 }
109 }
110 return -1;
111 }
112
113 // returns the index of the previous message from the array
114 function findPreviousMessage() {
115 global $msort, $currentArrayIndex, $sort, $msgs, $imapConnection, $mailbox;
116 if ($sort == 6) {
117 $numMessages = sqimap_get_num_messages($imapConnection, $mailbox);
118 if ($currentArrayIndex != $numMessages) {
119 return $currentArrayIndex + 1;
120 }
121 } else {
122 for (reset($msort); ($key = key($msort)), (isset($key)); next($msort)) {
123 if ($currentArrayIndex == $msgs[$key]["ID"]) {
124 prev($msort);
125 $key = key($msort);
126 if (isset($key))
127 return $msgs[$key]["ID"];
128 }
129 }
130 }
131 return -1;
132 }
133
134 if (isset($msgs)) {
135 $currentArrayIndex = $passed_id;
136 /*
137 for ($i=0; $i < count($msgs); $i++) {
138 if ($msgs[$i]["ID"] == $passed_id) {
139 $currentArrayIndex = $i;
140 break;
141 }
142 }
143 */
144 } else {
145 $currentArrayIndex = -1;
146 }
147
148 for ($i = 0; $i < count($msgs); $i++) {
149 if ($msgs[$i]["ID"] == $passed_id)
150 $msgs[$i]["FLAG_SEEN"] = true;
151 }
152
153 // $message contains all information about the message
154 // including header and body
155 $message = sqimap_get_message($imapConnection, $passed_id, $mailbox);
156
157 /** translate the subject and mailbox into url-able text **/
158 $url_subj = urlencode(trim(sqStripSlashes($message->header->subject)));
159 $urlMailbox = urlencode($mailbox);
160 $url_replyto = urlencode($message->header->replyto);
161
162 $url_replytoall = urlencode($message->header->replyto);
163
164 // If we are replying to all, then find all other addresses and
165 // add them to the list. Remove duplicates.
166 // This is somewhat messy, so I'll explain:
167 // 1) Take all addresses (from, to, cc) (avoid nasty join errors here)
168 $url_replytoall_extra_addrs = array_merge(array($message->header->from),
169 $message->header->to, $message->header->cc);
170
171 // 2) Make one big string out of them
172 $url_replytoall_extra_addrs = join(';', $url_replytoall_extra_addrs);
173
174 // 3) Parse that into an array of addresses
175 $url_replytoall_extra_addrs = parseAddrs($url_replytoall_extra_addrs);
176
177 // 4) Make them unique -- weed out duplicates
178 $url_replytoall_extra_addrs = array_unique($url_replytoall_extra_addrs);
179
180 // 5) Remove the addresses we'll be sending the message 'to'
181 $url_replytoall_avoid_addrs = parseAddrs($message->header->replyto);
182 foreach ($url_replytoall_avoid_addrs as $addr)
183 {
184 foreach (array_keys($url_replytoall_extra_addrs, $addr) as $key_to_delete)
185 {
186 unset($url_replytoall_extra_addrs[$key_to_delete]);
187 }
188 }
189
190 // 6) Smoosh back into one nice line
191 $url_replytoallcc = getLineOfAddrs($url_replytoall_extra_addrs);
192
193 // 7) urlencode() it
194 $url_replytoallcc = urlencode($url_replytoallcc);
195
196 $dateString = getLongDateString($message->header->date);
197 $ent_num = findDisplayEntity($message);
198
199 /** TEXT STRINGS DEFINITIONS **/
200 $echo_more = _("more");
201 $echo_less = _("less");
202
203 /** FORMAT THE TO STRING **/
204 $i = 0;
205 $to_string = "";
206 $to_ary = $message->header->to;
207 while ($i < count($to_ary)) {
208 $to_ary[$i] = htmlspecialchars(decodeHeader($to_ary[$i]));
209
210 if ($to_string)
211 $to_string = "$to_string<BR>$to_ary[$i]";
212 else
213 $to_string = "$to_ary[$i]";
214
215 $i++;
216 if (count($to_ary) > 1) {
217 if ($show_more == false) {
218 if ($i == 1) {
219 if ($where && $what) {
220 // from a search
221 $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>)";
222 } else {
223 $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>)";
224 }
225 $i = count($to_ary);
226 }
227 } else if ($i == 1) {
228 if ($where && $what) {
229 // from a search
230 $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>)";
231 } else {
232 $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>)";
233 }
234 }
235 }
236 }
237
238 /** FORMAT THE CC STRING **/
239 $i = 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 ($where && $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 ($where && $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 COLS=1 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 COLS=2 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 COLS=2 WIDTH=100% BORDER=0 CELLSPACING=0 CELLPADDING=3>\n";
338 echo " <TR>\n";
339 /** subject **/
340 echo " <TD BGCOLOR=\"$color[0]\" WIDTH=15% ALIGN=RIGHT>\n";
341 echo _("Subject:");
342 echo " </TD><TD BGCOLOR=\"$color[0]\" WIDTH=84%>\n";
343 echo " <B>$subject</B>&nbsp;\n";
344 echo " </TD>\n";
345 if ($where && $what) {
346 // Got here from a search
347 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>";
348 } else {
349 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>";
350 }
351 echo " </TR>\n";
352 /** from **/
353 echo " <TR>\n";
354 echo " <TD BGCOLOR=\"$color[0]\" WIDTH=15% ALIGN=RIGHT>\n";
355 echo _("From:");
356 echo " </TD><TD BGCOLOR=\"$color[0]\" WIDTH=85% colspan=2>\n";
357 echo " <B>$from_name</B>&nbsp;\n";
358 echo " </TD>\n";
359 echo " </TR>\n";
360 /** date **/
361 echo " <TR>\n";
362 echo " <TD BGCOLOR=\"$color[0]\" WIDTH=15% ALIGN=RIGHT>\n";
363 echo _("Date:");
364 echo " </TD><TD BGCOLOR=\"$color[0]\" WIDTH=85% colspan=2>\n";
365 echo " <B>$dateString</B>&nbsp;\n";
366 echo " </TD>\n";
367 echo " </TR>\n";
368 /** to **/
369 echo " <TR>\n";
370 echo " <TD BGCOLOR=\"$color[0]\" WIDTH=15% ALIGN=RIGHT VALIGN=TOP>\n";
371 echo _("To:");
372 echo " </TD><TD BGCOLOR=\"$color[0]\" WIDTH=85% VALIGN=TOP colspan=2>\n";
373 echo " <B>$to_string</B>&nbsp;\n";
374 echo " </TD>\n";
375 echo " </TR>\n";
376 /** cc **/
377 if ($message->header->cc) {
378 echo " <TR>\n";
379 echo " <TD BGCOLOR=\"$color[0]\" WIDTH=15% ALIGN=RIGHT VALIGN=TOP>\n";
380 echo " Cc:\n";
381 echo " </TD><TD BGCOLOR=\"$color[0]\" WIDTH=85% VALIGN=TOP colspan=2>\n";
382 echo " <B>$cc_string</B>&nbsp;\n";
383 echo " </TD>\n";
384 echo " </TR>\n";
385 }
386 do_hook("read_body_header");
387 echo "</TABLE>";
388 echo " </TD></TR>";
389 echo "</table>";
390 echo "<TABLE COLS=1 CELLSPACING=0 WIDTH=97% BORDER=0 ALIGN=CENTER CELLPADDING=0>\n";
391
392 echo " <TR><TD BGCOLOR=\"$color[4]\" WIDTH=100%>\n";
393 echo "<BR>";
394
395 $body = formatBody($imapConnection, $message, $color, $wrap_at);
396
397 echo $body;
398
399 echo "<TABLE COLS=1 CELLSPACING=0 WIDTH=100% BORDER=0 ALIGN=CENTER CELLPADDING=0>\n";
400 echo " <TR><TD BGCOLOR=\"$color[9]\">&nbsp;</TD></TR>";
401 echo "</TABLE>\n";
402
403 do_hook("read_body_bottom");
404 do_hook("html_bottom");
405 sqimap_logout($imapConnection);
406 ?>