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