charset encode /decode fixes
[squirrelmail.git] / src / read_body.php
CommitLineData
59177427 1<?php
895905c0 2
35586184 3/**
4 * read_body.php
5 *
76911253 6 * Copyright (c) 1999-2003 The SquirrelMail Project Team
35586184 7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * This file is used for reading the msgs array and displaying
10 * the resulting emails in the right frame.
11 *
12 * $Id$
a07cd1a4 13 */
35586184 14
86725763 15/* Path for SquirrelMail required files. */
16define('SM_PATH','../');
17
18/* SquirrelMail required files. */
08185f2a 19require_once(SM_PATH . 'include/validate.php');
86725763 20require_once(SM_PATH . 'functions/imap.php');
21require_once(SM_PATH . 'functions/mime.php');
22require_once(SM_PATH . 'functions/date.php');
23require_once(SM_PATH . 'functions/url_parser.php');
24require_once(SM_PATH . 'functions/html.php');
e95c04ec 25require_once(SM_PATH . 'functions/global.php');
38c944cc 26
a07cd1a4 27/**
1fca12b5 28 * Given an IMAP message id number, this will look it up in the cached
29 * and sorted msgs array and return the index. Used for finding the next
30 * and previous messages.
31 *
32 * @return the index of the next valid message from the array
33 */
c615b1da 34function findNextMessage($passed_id) {
35 global $msort, $msgs, $sort,
cf710efe 36 $thread_sort_messages, $allow_server_sort,
60a3e687 37 $server_sort_array;
2728fa19 38 if (!is_array($server_sort_array)) {
39 $thread_sort_messages = 0;
794d59c0 40 $allow_server_sort = FALSE;
2728fa19 41 }
a07cd1a4 42 $result = -1;
c615b1da 43 if ($thread_sort_messages || $allow_server_sort) {
d9c1dccc 44 $count = count($server_sort_array) - 1;
45 foreach($server_sort_array as $key=>$value) {
c615b1da 46 if ($passed_id == $value) {
d9c1dccc 47 if ($key == $count) {
60a3e687 48 break;
49 }
1fca12b5 50 $result = $server_sort_array[$key + 1];
60a3e687 51 break;
789b4d79 52 }
60a3e687 53 }
d9c1dccc 54 } else {
7e818af0 55 if (is_array($msort)) {
56 for (reset($msort); ($key = key($msort)), (isset($key)); next($msort)) {
57 if ($passed_id == $msgs[$key]['ID']) {
58 next($msort);
59 $key = key($msort);
60 if (isset($key)){
61 $result = $msgs[$key]['ID'];
62 break;
d9c1dccc 63 }
1fca12b5 64 }
10f0ce72 65 }
66 }
10f0ce72 67 }
d9c1dccc 68 return $result;
a07cd1a4 69}
70
a07cd1a4 71/** returns the index of the previous message from the array. */
c615b1da 72function findPreviousMessage($numMessages, $passed_id) {
73 global $msort, $sort, $msgs,
74 $thread_sort_messages,
60a3e687 75 $allow_server_sort, $server_sort_array;
a07cd1a4 76 $result = -1;
2728fa19 77 if (!is_array($server_sort_array)) {
78 $thread_sort_messages = 0;
794d59c0 79 $allow_server_sort = FALSE;
2728fa19 80 }
c615b1da 81 if ($thread_sort_messages || $allow_server_sort ) {
d9c1dccc 82 foreach($server_sort_array as $key=>$value) {
c615b1da 83 if ($passed_id == $value) {
60a3e687 84 if ($key == 0) {
60a3e687 85 break;
86 }
d9c1dccc 87 $result = $server_sort_array[$key - 1];
60a3e687 88 break;
789b4d79 89 }
60a3e687 90 }
d9c1dccc 91 } else {
7e818af0 92 if (is_array($msort)) {
93 for (reset($msort); ($key = key($msort)), (isset($key)); next($msort)) {
94 if ($passed_id == $msgs[$key]['ID']) {
95 prev($msort);
96 $key = key($msort);
97 if (isset($key)) {
98 $result = $msgs[$key]['ID'];
99 break;
d9c1dccc 100 }
10f0ce72 101 }
102 }
10f0ce72 103 }
a07cd1a4 104 }
d9c1dccc 105 return $result;
a07cd1a4 106}
10f0ce72 107
a07cd1a4 108/**
1fca12b5 109 * Displays a link to a page where the message is displayed more
110 * "printer friendly".
111 */
c615b1da 112function printer_friendly_link($mailbox, $passed_id, $passed_ent_id, $color) {
113 global $javascript_on;
a07cd1a4 114
c615b1da 115 $params = '?passed_ent_id=' . $passed_ent_id .
c2324b26 116 '&mailbox=' . urlencode($mailbox) .
117 '&passed_id=' . $passed_id;
10f0ce72 118
a07cd1a4 119 $print_text = _("View Printable Version");
10f0ce72 120
c615b1da 121 $result = '';
a07cd1a4 122 /* Output the link. */
123 if ($javascript_on) {
9f42bfc8 124 $result = '<script language="javascript" type="text/javascript">' . "\n" .
125 '<!--' . "\n" .
126 " function printFormat() {\n" .
127 ' window.open("../src/printer_friendly_main.php' .
128 $params . '","Print","width=800,height=600");' . "\n".
129 " }\n" .
130 "// -->\n" .
131 "</script>\n" .
132 "<a href=\"javascript:printFormat();\">$print_text</a>\n";
a07cd1a4 133 } else {
c2324b26 134 $result = '<a target="_blank" href="../src/printer_friendly_bottom.php' .
9f42bfc8 135 "$params\">$print_text</a>\n";
a07cd1a4 136 }
d9c1dccc 137 return $result;
a07cd1a4 138}
139
d9c1dccc 140function ServerMDNSupport($read) {
f69feefe 141 /* escaping $ doesn't work -> \x36 */
d9c1dccc 142 $ret = preg_match('/(\x36MDNSent|\\\*)/i', $read);
143 return $ret;
57257333 144}
145
fc224f68 146function SendMDN ( $mailbox, $passed_id, $sender, $message, $imapConnection) {
e95c04ec 147 global $username, $attachment_dir,
fc224f68 148 $version, $attachments, $squirrelmail_language, $default_charset,
d9c1dccc 149 $languages, $useSendmail, $domain, $sent_folder,
150 $popuser, $data_dir, $username;
57257333 151
e95c04ec 152 sqgetGlobalVar('SERVER_NAME', $SERVER_NAME, SQ_SERVER);
0b97a708 153
38bca81c 154 $header = $message->rfc822_header;
57257333 155 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
156
fc224f68 157 $rfc822_header = new Rfc822Header();
d9c1dccc 158 $content_type = new ContentType('multipart/report');
fc224f68 159 $content_type->properties['report-type']='disposition-notification';
d9c1dccc 160
fc224f68 161 set_my_charset();
162 if ($default_charset) {
d9c1dccc 163 $content_type->properties['charset']=$default_charset;
fc224f68 164 }
165 $rfc822_header->content_type = $content_type;
166 $rfc822_header->to[] = $header->dnt;
9549758e 167 $rfc822_header->subject = _("Read:") . ' ' . decodeHeader($header->subject);
fc224f68 168
169
170 $reply_to = '';
171 if (isset($identity) && $identity != 'default') {
172 $from_mail = getPref($data_dir, $username,
d9c1dccc 173 'email_address' . $identity);
fc224f68 174 $full_name = getPref($data_dir, $username,
d9c1dccc 175 'full_name' . $identity);
fc224f68 176 $from_addr = '"'.$full_name.'" <'.$from_mail.'>';
d9c1dccc 177 $reply_to = getPref($data_dir, $username,
178 'reply_to' . $identity);
fc224f68 179 } else {
180 $from_mail = getPref($data_dir, $username, 'email_address');
181 $full_name = getPref($data_dir, $username, 'full_name');
182 $from_addr = '"'.$full_name.'" <'.$from_mail.'>';
d9c1dccc 183 $reply_to = getPref($data_dir, $username,'reply_to');
fc224f68 184 }
185 if (!$from_addr) {
186 $from_addr = "$popuser@$domain";
187 $from_mail = $from_addr;
188 }
189 $rfc822_header->from = $rfc822_header->parseAddress($from_addr,true);
190 if ($reply_to) {
191 $rfc822_header->reply_to = $rfc822_header->parseAddress($reply_to,true);
192 }
d9c1dccc 193
57257333 194 // part 1 (RFC2298)
57257333 195 $senton = getLongDateString( $header->date );
196 $to_array = $header->to;
197 $to = '';
198 foreach ($to_array as $line) {
af568a82 199 $to .= ' '.$line->getAddress();
57257333 200 }
57257333 201 $now = getLongDateString( time() );
78db1583 202 set_my_charset();
46bb8da8 203 $body = _("Your message") . "\r\n\r\n" .
9549758e 204 "\t" . _("To:") . ' ' . decodeHeader($to) . "\r\n" .
205 "\t" . _("Subject:") . ' ' . decodeHeader($header->subject) . "\r\n" .
46bb8da8 206 "\t" . _("Sent:") . ' ' . $senton . "\r\n" .
207 "\r\n" .
208 sprintf( _("Was displayed on %s"), $now );
f69feefe 209
fc224f68 210 $special_encoding = '';
211 if (isset($languages[$squirrelmail_language]['XTRA_CODE']) &&
212 function_exists($languages[$squirrelmail_language]['XTRA_CODE'])) {
6fbd125b 213 $body = $languages[$squirrelmail_language]['XTRA_CODE']('encode', $body);
d9c1dccc 214 if (strtolower($default_charset) == 'iso-2022-jp') {
215 if (mb_detect_encoding($body) == 'ASCII') {
216 $special_encoding = '8bit';
217 } else {
218 $body = mb_convert_encoding($body, 'JIS');
219 $special_encoding = '7bit';
220 }
221 }
6fbd125b 222 }
fc224f68 223 $part1 = new Message();
224 $part1->setBody($body);
225 $mime_header = new MessageHeader;
226 $mime_header->type0 = 'text';
227 $mime_header->type1 = 'plain';
228 if ($special_encoding) {
229 $mime_header->encoding = $special_encoding;
d9c1dccc 230 } else {
fc224f68 231 $mime_header->encoding = 'us-ascii';
232 }
233 if ($default_charset) {
234 $mime_header->parameters['charset'] = $default_charset;
235 }
236 $part1->mime_header = $mime_header;
237
57257333 238 // part2 (RFC2298)
d9c1dccc 239 $original_recipient = $to;
57257333 240 $original_message_id = $header->message_id;
241
fc224f68 242 $report = "Reporting-UA : $SERVER_NAME ; SquirrelMail (version $version) \r\n";
57257333 243 if ($original_recipient != '') {
fc224f68 244 $report .= "Original-Recipient : $original_recipient\r\n";
57257333 245 }
246 $final_recipient = $sender;
fc224f68 247 $report .= "Final-Recipient: rfc822; $final_recipient\r\n" .
57257333 248 "Original-Message-ID : $original_message_id\r\n" .
249 "Disposition: manual-action/MDN-sent-manually; displayed\r\n";
250
fc224f68 251 $part2 = new Message();
252 $part2->setBody($report);
253 $mime_header = new MessageHeader;
254 $mime_header->type0 = 'message';
255 $mime_header->type1 = 'disposition-notification';
256 $mime_header->encoding = 'us-ascii';
257 $part2->mime_header = $mime_header;
258
259 $composeMessage = new Message();
260 $composeMessage->rfc822_header = $rfc822_header;
261 $composeMessage->addEntity($part1);
262 $composeMessage->addEntity($part2);
263
264
d9c1dccc 265 if ($useSendmail) {
266 require_once(SM_PATH . 'class/deliver/Deliver_SendMail.class.php');
267 global $sendmail_path;
268 $deliver = new Deliver_SendMail();
269 $stream = $deliver->initStream($composeMessage,$sendmail_path);
fc224f68 270 } else {
d9c1dccc 271 require_once(SM_PATH . 'class/deliver/Deliver_SMTP.class.php');
272 $deliver = new Deliver_SMTP();
47a29326 273 global $smtpServerAddress, $smtpPort, $smtp_auth_mech, $pop_before_smtp;
274 if ($smtp_auth_mech == 'none') {
275 $user = '';
276 $pass = '';
277 } else {
d9c1dccc 278 global $key, $onetimepad;
279 $user = $username;
280 $pass = OneTimePadDecrypt($key, $onetimepad);
d9c1dccc 281 }
282 $authPop = (isset($pop_before_smtp) && $pop_before_smtp) ? true : false;
283 $stream = $deliver->initStream($composeMessage,$domain,0,
70ce2218 284 $smtpServerAddress, $smtpPort, $user, $pass, $authPop);
d9c1dccc 285 }
286 $success = false;
fc224f68 287 if ($stream) {
d9c1dccc 288 $length = $deliver->mail($composeMessage, $stream);
289 $success = $deliver->finalizeStream($stream);
fc224f68 290 }
d9c1dccc 291 if (!$success) {
00ac2f42 292 $msg = $deliver->dlv_msg . '<br>' .
293 _("Server replied: ") . $deliver->dlv_ret_nr . ' '.
294 $deliver->dlv_server_msg;
d9c1dccc 295 require_once(SM_PATH . 'functions/display_messages.php');
fc224f68 296 plain_error_message($msg, $color);
297 } else {
298 unset ($deliver);
d9c1dccc 299 if (sqimap_mailbox_exists ($imapConnection, $sent_folder)) {
300 sqimap_append ($imapConnection, $sent_folder, $length);
301 require_once(SM_PATH . 'class/deliver/Deliver_IMAP.class.php');
302 $imap_deliver = new Deliver_IMAP();
303 $imap_deliver->mail($composeMessage, $imapConnection);
304 sqimap_append_done ($imapConnection);
305 unset ($imap_deliver);
306 }
307 }
308 return $success;
57257333 309}
310
d9c1dccc 311function ToggleMDNflag ($set ,$imapConnection, $mailbox, $passed_id, $uid_support) {
312 $sg = $set?'+':'-';
313 $cmd = 'STORE ' . $passed_id . ' ' . $sg . 'FLAGS ($MDNSent)';
0892e427 314 $read = sqimap_run_command ($imapConnection, $cmd, true, $response,
507d14ea 315 $readmessage, $uid_support);
57257333 316}
317
318function ClearAttachments() {
d9c1dccc 319 global $username, $attachments, $attachment_dir;
320
321 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
322
323 $rem_attachments = array();
6191bcb1 324 if (isset($attachments)) {
325 foreach ($attachments as $info) {
326 if ($info['session'] == -1) {
327 $attached_file = "$hashed_attachment_dir/$info[localfilename]";
328 if (file_exists($attached_file)) {
329 unlink($attached_file);
330 }
331 } else {
332 $rem_attachments[] = $info;
333 }
334 }
d9c1dccc 335 }
336 $attachments = $rem_attachments;
57257333 337}
338
4d0cd98b 339function formatRecipientString($recipients, $item ) {
d9c1dccc 340 global $show_more_cc, $show_more, $show_more_bcc,
341 $PHP_SELF;
4d0cd98b 342
d9c1dccc 343 $string = '';
38c944cc 344 if ((is_array($recipients)) && (isset($recipients[0]))) {
d9c1dccc 345 $show = false;
38c944cc 346
347 if ($item == 'to') {
d9c1dccc 348 if ($show_more) {
349 $show = true;
350 $url = set_url_var($PHP_SELF, 'show_more',0);
351 } else {
352 $url = set_url_var($PHP_SELF, 'show_more',1);
353 }
354 } else if ($item == 'cc') {
355 if ($show_more_cc) {
356 $show = true;
357 $url = set_url_var($PHP_SELF, 'show_more_cc',0);
358 } else {
359 $url = set_url_var($PHP_SELF, 'show_more_cc',1);
360 }
361 } else if ($item == 'bcc') {
362 if ($show_more_bcc) {
363 $show = true;
364 $url = set_url_var($PHP_SELF, 'show_more_bcc',0);
365 } else {
366 $url = set_url_var($PHP_SELF, 'show_more_bcc',1);
367 }
368 }
369
370 $cnt = count($recipients);
371 foreach($recipients as $r) {
a91189d6 372 $add = decodeHeader($r->getAddress(true));
d9c1dccc 373 if ($string) {
374 $string .= '<BR>' . $add;
375 } else {
376 $string = $add;
377 if ($cnt > 1) {
38c944cc 378 $string .= '&nbsp;(<A HREF="'.$url;
d9c1dccc 379 if ($show) {
380 $string .= '">'._("less").'</A>)';
381 } else {
382 $string .= '">'._("more").'</A>)';
383 break;
384 }
385 }
386 }
1fca12b5 387 }
4d0cd98b 388 }
c615b1da 389 return $string;
390}
391
af568a82 392function formatEnvheader($mailbox, $passed_id, $passed_ent_id, $message,
393 $color, $FirstTimeSee) {
a94c1db1 394 global $msn_user_support, $default_use_mdn, $default_use_priority,
77836429 395 $show_xmailer_default, $mdn_user_support, $PHP_SELF, $javascript_on,
396 $squirrelmail_language;
38bca81c 397
d9c1dccc 398 $header = $message->rfc822_header;
399 $env = array();
a91189d6 400 $env[_("Subject")] = decodeHeader($header->subject);
d9c1dccc 401 $from_name = $header->getAddr_s('from');
402 if (!$from_name) {
403 $from_name = $header->getAddr_s('sender');
404 if (!$from_name) {
405 $from_name = _("Unknown sender");
406 }
407 }
a91189d6 408 $env[_("From")] = decodeHeader($from_name);
d9c1dccc 409 $env[_("Date")] = getLongDateString($header->date);
410 $env[_("To")] = formatRecipientString($header->to, "to");
411 $env[_("Cc")] = formatRecipientString($header->cc, "cc");
412 $env[_("Bcc")] = formatRecipientString($header->bcc, "bcc");
413 if ($default_use_priority) {
0e8006dc 414 $env[_("Priority")] = htmlspecialchars(getPriorityStr($header->priority));
d9c1dccc 415 }
416 if ($show_xmailer_default) {
a91189d6 417 $env[_("Mailer")] = decodeHeader($header->xmailer);
d9c1dccc 418 }
419 if ($default_use_mdn) {
420 if ($mdn_user_support) {
421 if ($header->dnt) {
422 if ($message->is_mdnsent) {
423 $env[_("Read receipt")] = _("send");
424 } else {
425 $env[_("Read receipt")] = _("requested");
8e1d27aa 426 if (!(handleAsSent($mailbox) ||
d9c1dccc 427 $message->is_deleted ||
428 $passed_ent_id)) {
429 $mdn_url = $PHP_SELF . '&sendreceipt=1';
430 if ($FirstTimeSee && $javascript_on) {
431 $script = '<script language="JavaScript" type="text/javascript">' . "\n";
432 $script .= '<!--'. "\n";
433 $script .= 'if(window.confirm("' .
434 _("The message sender has requested a response to indicate that you have read this message. Would you like to send a receipt?") .
435 '")) { '."\n" .
436 ' sendMDN()'.
437 '}' . "\n";
438 $script .= '// -->'. "\n";
439 $script .= '</script>'. "\n";
440 echo $script;
441 }
442 $env[_("Read receipt")] .= '&nbsp;<a href="' . $mdn_url . '">[' .
443 _("Send read receipt now") . ']</a>';
444 }
445 }
446 }
447 }
448 }
c615b1da 449
a94c1db1 450 $s = '<TABLE WIDTH="100%" CELLPADDING="0" CELLSPACING="2" BORDER="0"';
0e02d604 451 $s .= ' ALIGN="center" BGCOLOR="'.$color[0].'">';
d9c1dccc 452 foreach ($env as $key => $val) {
453 if ($val) {
454 $s .= '<TR>';
455 $s .= html_tag('TD', '<B>' . $key . ':&nbsp;&nbsp;</B>', 'RIGHT', '', 'VALIGN="TOP" WIDTH="20%"') . "\n";
456 $s .= html_tag('TD', $val, 'left', '', 'VALIGN="TOP" WIDTH="80%"') . "\n";
457 $s .= '</TR>';
458 }
459 }
a94c1db1 460 echo '<TABLE BGCOLOR="'.$color[9].'" WIDTH="100%" CELLPADDING="1"'.
feb5ec1b 461 ' CELLSPACING="0" BORDER="0" ALIIGN="center">'."\n";
a94c1db1 462 echo '<TR><TD HEIGHT="5" COLSPAN="2" BGCOLOR="'.
463 $color[4].'"></TD></TR><TR><TD align=center>'."\n";
d9c1dccc 464 echo $s;
00ac2f42 465 do_hook('read_body_header');
d9c1dccc 466 formatToolbar($mailbox, $passed_id, $passed_ent_id, $message, $color);
467 echo '</TABLE>';
a94c1db1 468 echo '</TD></TR><TR><TD HEIGHT="5" COLSPAN="2" BGCOLOR="'.$color[4].'"></TD></TR>'."\n";
feb5ec1b 469 echo '</TABLE>';
d9c1dccc 470}
c615b1da 471
7172cad7 472function formatMenubar($mailbox, $passed_id, $passed_ent_id, $message, $mbx_response) {
a94c1db1 473 global $base_uri, $draft_folder, $where, $what, $color, $sort,
d9c1dccc 474 $startMessage, $compose_new_win, $PHP_SELF, $save_as_draft,
475 $enable_forward_as_attachment;
c615b1da 476
d9c1dccc 477 $topbar_delimiter = '&nbsp;|&nbsp;';
478 $urlMailbox = urlencode($mailbox);
a94c1db1 479 $s = '<table width="100%" cellpadding="3" cellspacing="0" align="center"'.
98fb28fd 480 ' border="0" bgcolor="'.$color[9].'"><tr>' .
481 html_tag( 'td', '', 'left', '', 'width="33%"' ) . '<small>';
e55c441a 482
d9c1dccc 483 $msgs_url = $base_uri . 'src/';
484 if (isset($where) && isset($what)) {
485 $msgs_url .= 'search.php?where=' . urlencode($where) .
486 '&amp;what=' . urlencode($what) . '&amp;mailbox=' . $urlMailbox;
487 $msgs_str = _("Search results");
488 } else {
489 $msgs_url .= 'right_main.php?sort=' . $sort . '&amp;startMessage=' .
490 $startMessage . '&amp;mailbox=' . $urlMailbox;
491 $msgs_str = _("Message List");
492 }
e55c441a 493 $s .= '<a href="' . $msgs_url . '">' . $msgs_str . '</a>';
d9c1dccc 494
495 $delete_url = $base_uri . 'src/delete_message.php?mailbox=' . $urlMailbox .
496 '&amp;message=' . $passed_id . '&amp;';
497 if (!(isset($passed_ent_id) && $passed_ent_id)) {
498 if ($where && $what) {
499 $delete_url .= 'where=' . urlencode($where) . '&amp;what=' . urlencode($what);
500 } else {
501 $delete_url .= 'sort=' . $sort . '&amp;startMessage=' . $startMessage;
502 }
9f42bfc8 503 $s .= $topbar_delimiter;
e55c441a 504 $s .= '<a href="' . $delete_url . '">' . _("Delete") . '</a>';
d9c1dccc 505 }
c615b1da 506
d9c1dccc 507 $comp_uri = $base_uri . 'src/compose.php' .
508 '?passed_id=' . $passed_id .
509 '&amp;mailbox=' . $urlMailbox .
510 (isset($passed_ent_id)?'&amp;passed_ent_id='.$passed_ent_id:'');
2ffa3f57 511
d9c1dccc 512 if ($compose_new_win == '1') {
513 $link_open = '<a href="javascript:void(0)" onclick="comp_in_new(\'';
514 $link_close = '\')">';
515 } else {
516 $link_open = '<a href="';
517 $link_close = '">';
518 }
519 if (($mailbox == $draft_folder) && ($save_as_draft)) {
520 $comp_alt_uri = $comp_uri . '&amp;action=draft';
521 $comp_alt_string = _("Resume Draft");
8e1d27aa 522 } else if (handleAsSent($mailbox)) {
d9c1dccc 523 $comp_alt_uri = $comp_uri . '&amp;action=edit_as_new';
524 $comp_alt_string = _("Edit Message as New");
525 }
526 if (isset($comp_alt_uri)) {
e55c441a 527 $s .= $topbar_delimiter;
528 $s .= $link_open . $comp_alt_uri . $link_close . $comp_alt_string . '</a>';
d9c1dccc 529 }
2ffa3f57 530
e55c441a 531 $s .= '</small></td><td align="center" width="33%"><small>';
532
d9c1dccc 533 if (!(isset($where) && isset($what)) && !$passed_ent_id) {
534 $prev = findPreviousMessage($mbx_response['EXISTS'], $passed_id);
535 $next = findNextMessage($passed_id);
536 if ($prev != -1) {
537 $uri = $base_uri . 'src/read_body.php?passed_id='.$prev.
538 '&amp;mailbox='.$urlMailbox.'&amp;sort='.$sort.
539 '&amp;startMessage='.$startMessage.'&amp;show_more=0';
e55c441a 540 $s .= '<a href="'.$uri.'">'._("Previous").'</a>';
d9c1dccc 541 } else {
e55c441a 542 $s .= _("Previous");
d9c1dccc 543 }
e55c441a 544 $s .= $topbar_delimiter;
d9c1dccc 545 if ($next != -1) {
546 $uri = $base_uri . 'src/read_body.php?passed_id='.$next.
547 '&amp;mailbox='.$urlMailbox.'&amp;sort='.$sort.
548 '&amp;startMessage='.$startMessage.'&amp;show_more=0';
e55c441a 549 $s .= '<a href="'.$uri.'">'._("Next").'</a>';
d9c1dccc 550 } else {
e55c441a 551 $s .= _("Next");
d9c1dccc 552 }
553 } else if (isset($passed_ent_id) && $passed_ent_id) {
554 /* code for navigating through attached message/rfc822 messages */
555 $url = set_url_var($PHP_SELF, 'passed_ent_id',0);
e55c441a 556 $s .= '<a href="'.$url.'">'._("View Message").'</a>';
70bdc740 557 $entities = array();
558 $entity_count = array();
559 $c = 0;
f2f03410 560
70bdc740 561 foreach($message->parent->entities as $ent) {
9f42bfc8 562 if ($ent->type0 == 'message' && $ent->type1 == 'rfc822') {
563 $c++;
564 $entity_count[$c] = $ent->entity_id;
565 $entities[$ent->entity_id] = $c;
566 }
70bdc740 567 }
568 $prev_link = _("Previous");
569 $next_link = _("Next");
570 if($entities[$passed_ent_id] > 1) {
571 $prev_ent_id = $entity_count[$entities[$passed_ent_id] - 1];
572 $prev_link = '<a href="'
573 . set_url_var($PHP_SELF, 'passed_ent_id', $prev_ent_id)
574 . '">' . $prev_link . '</a>';
575 }
576 if($entities[$passed_ent_id] < $c) {
577 $next_ent_id = $entity_count[$entities[$passed_ent_id] + 1];
578 $next_link = '<a href="'
579 . set_url_var($PHP_SELF, 'passed_ent_id', $next_ent_id)
580 . '">' . $next_link . '</a>';
581 }
e55c441a 582 $s .= $topbar_delimiter . $prev_link;
d9c1dccc 583 $par_ent_id = $message->parent->entity_id;
584 if ($par_ent_id) {
585 $par_ent_id = substr($par_ent_id,0,-2);
e55c441a 586 $s .= $topbar_delimiter;
d9c1dccc 587 $url = set_url_var($PHP_SELF, 'passed_ent_id',$par_ent_id);
e55c441a 588 $s .= '<a href="'.$url.'">'._("Up").'</a>';
d9c1dccc 589 }
e55c441a 590 $s .= $topbar_delimiter . $next_link;
d9c1dccc 591 }
c615b1da 592
98fb28fd 593 $s .= '</small></td>' . "\n" .
594 html_tag( 'td', '', 'right', '', 'width="33%" nowrap' ) . '<small>';
d9c1dccc 595 $comp_action_uri = $comp_uri . '&amp;action=forward';
e55c441a 596 $s .= $link_open . $comp_action_uri . $link_close . _("Forward") . '</a>';
c615b1da 597
d9c1dccc 598 if ($enable_forward_as_attachment) {
599 $comp_action_uri = $comp_uri . '&amp;action=forward_as_attachment';
e55c441a 600 $s .= $topbar_delimiter;
9f42bfc8 601 $s .= $link_open . $comp_action_uri . $link_close . _("Forward as Attachment") . '</a>';
d9c1dccc 602 }
a128c967 603
a91189d6 604 $comp_action_uri = $comp_uri . '&amp;action=reply';
e55c441a 605 $s .= $topbar_delimiter;
9f42bfc8 606 $s .= $link_open . $comp_action_uri . $link_close . _("Reply") . '</a>';
d9c1dccc 607
608 $comp_action_uri = $comp_uri . '&amp;action=reply_all';
9f42bfc8 609 $s .= $topbar_delimiter;
e55c441a 610 $s .= $link_open . $comp_action_uri . $link_close . _("Reply All") . '</a>';
611 $s .= '</small></td></tr></table>';
d9c1dccc 612 do_hook("read_body_menu_top");
e55c441a 613 echo $s;
d9c1dccc 614 do_hook("read_body_menu_bottom");
c615b1da 615}
616
617function formatToolbar($mailbox, $passed_id, $passed_ent_id, $message, $color) {
a94c1db1 618 global $base_uri;
fe369c70 619
d9c1dccc 620 $urlMailbox = urlencode($mailbox);
e95c04ec 621 sqgetGlobalVar('QUERY_STRING', $query_string, SQ_SERVER);
622 $url = $base_uri.'src/view_header.php?'.$query_string;
d9c1dccc 623
a94c1db1 624 $s = "<TR>\n" .
98fb28fd 625 html_tag( 'td', '', 'right', '', 'VALIGN="MIDDLE" WIDTH="20%"' ) . '<B>' . _("Options") . ":&nbsp;&nbsp;</B></TD>\n" .
626 html_tag( 'td', '', 'left', '', 'VALIGN="MIDDLE" WIDTH="80%"' ) . '<SMALL>' .
d9c1dccc 627 '<a href="'.$url.'">'._("View Full Header").'</a>';
628
629 /* Output the printer friendly link if we are in subtle mode. */
630 $s .= '&nbsp;|&nbsp;' .
631 printer_friendly_link($mailbox, $passed_id, $passed_ent_id, $color);
632 echo $s;
633 do_hook("read_body_header_right");
634 $s = "</SMALL></TD>\n" .
635 "</TR>\n";
636 echo $s;
a128c967 637
4d0cd98b 638}
639
6206f6c4 640/***************************/
2ffa3f57 641/* Main of read_body.php */
6206f6c4 642/***************************/
57257333 643
0b97a708 644/* get the globals we may need */
645
e95c04ec 646/** SESSION VARS */
647sqgetGlobalVar('username', $username, SQ_SESSION);
648sqgetGlobalVar('onetimepad',$onetimepad, SQ_SESSION);
649sqgetGlobalVar('msgs', $msgs, SQ_SESSION);
650sqgetGlobalVar('base_uri', $base_uri, SQ_SESSION);
651sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION);
652sqgetGlobalVar('msgs', $msgs, SQ_SESSION);
653sqgetGlobalVar('msort', $msort, SQ_SESSION);
654sqgetGlobalVar('lastTargetMailbox', $lastTargetMailbox, SQ_SESSION);
655sqgetGlobalVar('server_sort_array', $server_sort_array, SQ_SESSION);
656if (!sqgetGlobalVar('messages', $messages, SQ_SESSION) ) {
657 $messages = array();
ce274df9 658}
659
e95c04ec 660/** COOKIE VARS */
661sqgetGlobalVar('key', $key, SQ_COOKIE);
0b97a708 662
e95c04ec 663/** GET VARS */
664sqgetGlobalVar('sendreceipt', $sendreceipt, SQ_GET);
665sqgetGlobalVar('where', $where, SQ_GET);
666sqgetGlobalVar('what', $what, SQ_GET);
667if ( sqgetGlobalVar('show_more', $temp, SQ_GET) ) {
668 $show_more = (int) $temp;
1b9f3c04 669}
e95c04ec 670if ( sqgetGlobalVar('show_more_cc', $temp, SQ_GET) ) {
671 $show_more_cc = (int) $temp;
0b97a708 672}
e95c04ec 673if ( sqgetGlobalVar('show_more_bcc', $temp, SQ_GET) ) {
674 $show_more_bcc = (int) $temp;
0b97a708 675}
e95c04ec 676if ( sqgetGlobalVar('view_hdr', $temp, SQ_GET) ) {
677 $view_hdr = (int) $temp;
0b97a708 678}
e95c04ec 679
680/** POST VARS */
681sqgetGlobalVar('move_id', $move_id, SQ_POST);
682
683/** GET/POST VARS */
684sqgetGlobalVar('passed_ent_id', $passed_ent_id);
685sqgetGlobalVar('mailbox', $mailbox);
686
687if ( sqgetGlobalVar('passed_id', $temp) ) {
688 $passed_id = (int) $temp;
0b97a708 689}
e95c04ec 690if ( sqgetGlobalVar('sort', $temp) ) {
691 $sort = (int) $temp;
0b97a708 692}
e95c04ec 693if ( sqgetGlobalVar('startMessage', $temp) ) {
694 $startMessage = (int) $temp;
ce274df9 695}
0b97a708 696
ce274df9 697/* end of get globals */
38c944cc 698global $uid_support, $sqimap_capabilities;
699
6a108031 700if (isset($mailbox)) {
4366bea4 701 $mailbox = urldecode( $mailbox );
702}
38c944cc 703
6206f6c4 704$imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
9f42bfc8 705$mbx_response = sqimap_mailbox_select($imapConnection, $mailbox, false, false, true);
38c944cc 706
38c944cc 707
708/**
709 * $message contains all information about the message
710 * including header and body
711 */
37d2a6ee 712
713$uidvalidity = $mbx_response['UIDVALIDITY'];
9f42bfc8 714
37d2a6ee 715if (!isset($messages[$uidvalidity])) {
716 $messages[$uidvalidity] = array();
9f42bfc8 717}
5200c026 718if (!isset($messages[$uidvalidity][$passed_id]) || !$uid_support) {
719 $message = sqimap_get_message($imapConnection, $passed_id, $mailbox);
fc224f68 720 $FirstTimeSee = !$message->is_seen;
721 $message->is_seen = true;
5200c026 722 $messages[$uidvalidity][$passed_id] = $message;
38c944cc 723} else {
fc224f68 724// $message = sqimap_get_message($imapConnection, $passed_id, $mailbox);
725 $message = $messages[$uidvalidity][$passed_id];
726 $FirstTimeSee = !$message->is_seen;
5200c026 727}
af568a82 728
dd628162 729if (isset($passed_ent_id) && $passed_ent_id) {
5200c026 730 $message = $message->getEntity($passed_ent_id);
ff9d4297 731 if ($message->type0 != 'message' && $message->type1 != 'rfc822') {
732 $message = $message->parent;
733 }
141a16b2 734 $read = sqimap_run_command ($imapConnection, "FETCH $passed_id BODY[$passed_ent_id.HEADER]", true, $response, $msg, $uid_support);
19d470aa 735 $rfc822_header = new Rfc822Header();
141a16b2 736 $rfc822_header->parseHeader($read);
737 $message->rfc822_header = $rfc822_header;
c615b1da 738} else {
739 $passed_ent_id = 0;
38c944cc 740}
5200c026 741$header = $message->header;
57257333 742
6c31f818 743do_hook('html_top');
57257333 744
6206f6c4 745/****************************************/
746/* Block for handling incoming url vars */
747/****************************************/
5200c026 748
5200c026 749if (isset($sendreceipt)) {
750 if ( !$message->is_mdnsent ) {
751 if (isset($identity) ) {
752 $final_recipient = getPref($data_dir, $username, 'email_address' . '0', '' );
753 } else {
754 $final_recipient = getPref($data_dir, $username, 'email_address', '' );
755 }
756
757 $final_recipient = trim($final_recipient);
758 if ($final_recipient == '' ) {
759 $final_recipient = getPref($data_dir, $username, 'email_address', '' );
760 }
af568a82 761 $supportMDN = ServerMDNSupport($mbx_response["PERMANENTFLAGS"]);
fc224f68 762 if ( SendMDN( $mailbox, $passed_id, $final_recipient, $message, $imapConnection ) > 0 && $supportMDN ) {
c615b1da 763 ToggleMDNflag( true, $imapConnection, $mailbox, $passed_id, $uid_support);
5200c026 764 $message->is_mdnsent = true;
6a108031 765 $messages[$uidvalidity][$passed_id]=$message;
5200c026 766 }
767 ClearAttachments();
768 }
769}
6206f6c4 770/***********************************************/
771/* End of block for handling incoming url vars */
772/***********************************************/
773
38c944cc 774$msgs[$passed_id]['FLAG_SEEN'] = true;
9f42bfc8 775
776$messagebody = '';
c4ad259b 777do_hook('read_body_top');
778if ($show_html_default == 1) {
779 $ent_ar = $message->findDisplayEntity(array());
780} else {
781 $ent_ar = $message->findDisplayEntity(array(), array('text/plain'));
782}
a128c967 783$cnt = count($ent_ar);
784for ($i = 0; $i < $cnt; $i++) {
6a108031 785 $messagebody .= formatBody($imapConnection, $message, $color, $wrap_at, $ent_ar[$i], $passed_id, $mailbox);
a128c967 786 if ($i != $cnt-1) {
787 $messagebody .= '<hr noshade size=1>';
788 }
38c944cc 789}
e55c441a 790
c615b1da 791displayPageHeader($color, $mailbox);
7172cad7 792formatMenuBar($mailbox, $passed_id, $passed_ent_id, $message, $mbx_response);
af568a82 793formatEnvheader($mailbox, $passed_id, $passed_ent_id, $message, $color, $FirstTimeSee);
a94c1db1 794echo '<table width="100%" cellpadding="0" cellspacing="0" align="center" border="0">';
6206f6c4 795echo ' <tr><td>';
a94c1db1 796echo ' <table width="100%" cellpadding="1" cellspacing="0" align="center" border="0" bgcolor="'.$color[9].'">';
a128c967 797echo ' <tr><td>';
a94c1db1 798echo ' <table width="100%" cellpadding="3" cellspacing="0" align="center" border="0">';
6206f6c4 799echo ' <tr bgcolor="'.$color[4].'"><td>';
98fb28fd 800// echo ' <table cellpadding="1" cellspacing="5" align="left" border="0">';
801echo html_tag( 'table' ,'' , 'left', '', 'cellpadding="1" cellspacing="5" border="0"' );
ce810ea7 802echo ' <tr>' . html_tag( 'td', '<br>'. $messagebody."\n", 'left')
6c7b5d8c 803 . '</tr>';
6206f6c4 804echo ' </table>';
805echo ' </td></tr>';
806echo ' </table></td></tr>';
2ffa3f57 807echo ' </table>';
808echo ' </td></tr>';
a128c967 809
d0a2476a 810echo '<TR><TD HEIGHT="5" COLSPAN="2" BGCOLOR="'.
6c7b5d8c 811 $color[4].'"></TD></TR>'."\n";
d0a2476a 812
a128c967 813$attachmentsdisplay = formatAttachments($message,$ent_ar,$mailbox, $passed_id);
814if ($attachmentsdisplay) {
2ffa3f57 815 echo ' <tr><td>';
6206f6c4 816 echo ' <table width="100%" cellpadding="1" cellspacing="0" align="center"'.' border="0" bgcolor="'.$color[9].'">';
2ffa3f57 817 echo ' <tr><td>';
d0a2476a 818 echo ' <table width="100%" cellpadding="0" cellspacing="0" align="center" border="0" bgcolor="'.$color[4].'">';
98fb28fd 819 // echo ' <tr><td ALIGN="left" bgcolor="'.$color[9].'">';
820 echo ' <tr>' . html_tag( 'td', '', 'left', $color[9] );
6206f6c4 821 echo ' <b>' . _("Attachments") . ':</b>';
822 echo ' </td></tr>';
823 echo ' <tr><td>';
824 echo ' <table width="100%" cellpadding="2" cellspacing="2" align="center"'.' border="0" bgcolor="'.$color[0].'"><tr><td>';
825 echo $attachmentsdisplay;
826 echo ' </td></tr></table>';
6c7b5d8c 827 echo ' </td></tr></table>';
828 echo ' </td></tr></table>';
6206f6c4 829 echo ' </td></tr>';
6c7b5d8c 830 echo '<TR><TD HEIGHT="5" COLSPAN="2" BGCOLOR="'.
831 $color[4].'"></TD></TR>';
a128c967 832}
c615b1da 833echo '</table>';
a07cd1a4 834
835/* show attached images inline -- if pref'fed so */
5be9f195 836if (($attachment_common_show_images) &&
a07cd1a4 837 is_array($attachment_common_show_images_list)) {
838 foreach ($attachment_common_show_images_list as $img) {
ce274df9 839 $imgurl = SM_PATH . 'src/download.php' .
57257333 840 '?' .
cd7b8833 841 'passed_id=' . urlencode($img['passed_id']) .
3d570ba0 842 '&amp;mailbox=' . urlencode($mailbox) .
ff9d4297 843 '&amp;ent_id=' . urlencode($img['ent_id']) .
3d570ba0 844 '&amp;absolute_dl=true';
5be9f195 845
b5058e9e 846 echo html_tag( 'table', "\n" .
d9c1dccc 847 html_tag( 'tr', "\n" .
848 html_tag( 'td', '<img src="' . $imgurl . '">' ."\n", 'left'
849 )
850 ) ,
b5058e9e 851 'center', '', 'cellspacing=0 border="0" cellpadding="2"');
10f0ce72 852 }
a07cd1a4 853}
854
c615b1da 855do_hook('read_body_bottom');
856do_hook('html_bottom');
a07cd1a4 857sqimap_logout($imapConnection);
ce274df9 858/* sessions are written at the end of the script. it's better to register
859 them at the end so we avoid double session_register calls */
860sqsession_register($messages,'messages');
e55c441a 861
a07cd1a4 862?>
c615b1da 863</body>
864</html>