strip all whitespace from $domain, an accidental space breaks quite some
[squirrelmail.git] / src / read_body.php
CommitLineData
59177427 1<?php
134e4174 2
35586184 3/**
4 * read_body.php
5 *
35586184 6 * This file is used for reading the msgs array and displaying
7 * the resulting emails in the right frame.
8 *
47ccfad4 9 * @copyright &copy; 1999-2006 The SquirrelMail Project Team
4b4abf93 10 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
30967a1e 11 * @version $Id$
8f6f9ba5 12 * @package squirrelmail
a07cd1a4 13 */
35586184 14
30967a1e 15/**
202bcbcc 16 * Include the SquirrelMail initialization file.
30967a1e 17 */
202bcbcc 18require('../include/init.php');
86725763 19
20/* SquirrelMail required files. */
86725763 21require_once(SM_PATH . 'functions/imap.php');
202bcbcc 22require_once(SM_PATH . 'functions/imap_asearch.php'); // => move to mailbox_display
86725763 23require_once(SM_PATH . 'functions/mime.php');
24require_once(SM_PATH . 'functions/date.php');
25require_once(SM_PATH . 'functions/url_parser.php');
6d021521 26require_once(SM_PATH . 'functions/identity.php');
202bcbcc 27require_once(SM_PATH . 'functions/mailbox_display.php');
28require_once(SM_PATH . 'functions/forms.php');
29require_once(SM_PATH . 'functions/attachment_common.php');
38c944cc 30
a07cd1a4 31/**
1fca12b5 32 * Given an IMAP message id number, this will look it up in the cached
1e150c97 33 * and sorted msgs array and return the index of the next message
1fca12b5 34 *
1e150c97 35 * @param int $passed_id The current message UID
1fca12b5 36 * @return the index of the next valid message from the array
37 */
e0e30169 38function findNextMessage($uidset,$passed_id='backwards') {
324ac3c5 39 if (!is_array($uidset)) {
40 return -1;
41 }
e0e30169 42 if ($passed_id=='backwards' || !is_array($uidset)) { // check for backwards compattibilty gpg plugin
43 $passed_id = $uidset;
e0e30169 44 }
91c27aee 45 $result = sqm_array_get_value_by_offset($uidset,$passed_id,1);
46 if ($result === false) {
47 return -1;
48 } else {
49 return $result;
10f0ce72 50 }
a07cd1a4 51}
52
1e150c97 53/**
54 * Given an IMAP message id number, this will look it up in the cached
55 * and sorted msgs array and return the index of the previous message
56 *
57 * @param int $passed_id The current message UID
58 * @return the index of the next valid message from the array
59 */
e0e30169 60
61function findPreviousMessage($uidset, $passed_id) {
324ac3c5 62 if (!is_array($uidset)) {
63 return -1;
e0e30169 64 }
91c27aee 65 $result = sqm_array_get_value_by_offset($uidset,$passed_id,-1);
66 if ($result === false) {
67 return -1;
68 } else {
69 return $result;
a07cd1a4 70 }
a07cd1a4 71}
10f0ce72 72
7d020720 73function html_toggle_href ($mailbox, $passed_id, $passed_ent_id, $message) {
7277191e 74 global $base_uri, $show_html_default;
75
76 $has_html = false;
77 if ($message->header->type0 == 'message' && $message->header->type1 == 'rfc822') {
78 $type0 = $message->rfc822_header->content_type->type0;
79 $type1 = $message->rfc822_header->content_type->type1;
80 } else {
81 $type0 = $message->header->type0;
82 $type1 = $message->header->type1;
83 }
84 if($type0 == 'multipart' &&
7d020720 85 ($type1 == 'alternative' || $type1 == 'mixed' || $type1 == 'related' || $type1=='signed')) {
7277191e 86 if ($message->findDisplayEntity(array(), array('text/html'), true)) {
87 $has_html = true;
88 }
89 }
90 /*
91 * Normal single part message so check its type.
92 */
93 else {
94 if($type0 == 'text' && $type1 == 'html') {
95 $has_html = true;
96 }
97 }
98 if($has_html == true) {
202bcbcc 99 $vars = array('passed_ent_id', 'show_more', 'show_more_cc', 'override_type0', 'override_type1', 'startMessage','where', 'what');
7277191e 100
101 $new_link = $base_uri . 'src/read_body.php?passed_id=' . urlencode($passed_id) .
102 '&amp;passed_ent_id=' . urlencode($passed_ent_id) .
103 '&amp;mailbox=' . urlencode($mailbox);
104 foreach($vars as $var) {
105 if(sqgetGlobalVar($var, $temp)) {
106 $new_link .= '&amp;' . $var . '=' . urlencode($temp);
107 }
108 }
109
110 if($show_html_default == 1) {
111 $new_link .= '&amp;show_html_default=0';
7277191e 112 } else {
113 $new_link .= '&amp;show_html_default=1';
7277191e 114 }
7d020720 115 return $new_link;
7277191e 116 }
117 return '';
118}
119
4669e892 120function ServerMDNSupport($aFlags) {
121 /* escaping $ doesn't work -> \x36 */
c075fcfe 122 return ( in_array('$mdnsent',$aFlags,true) ||
123 in_array('\\*',$aFlags,true) ) ;
57257333 124}
125
fc224f68 126function SendMDN ( $mailbox, $passed_id, $sender, $message, $imapConnection) {
8d8da447 127 global $username, $attachment_dir, $popuser, $username, $color,
ce68b76b 128 $version, $squirrelmail_language, $default_charset,
129 $languages, $useSendmail, $domain, $sent_folder;
57257333 130
e95c04ec 131 sqgetGlobalVar('SERVER_NAME', $SERVER_NAME, SQ_SERVER);
0b97a708 132
38bca81c 133 $header = $message->rfc822_header;
57257333 134
fc224f68 135 $rfc822_header = new Rfc822Header();
d9c1dccc 136 $content_type = new ContentType('multipart/report');
fc224f68 137 $content_type->properties['report-type']='disposition-notification';
d9c1dccc 138
fc224f68 139 set_my_charset();
140 if ($default_charset) {
d9c1dccc 141 $content_type->properties['charset']=$default_charset;
fc224f68 142 }
143 $rfc822_header->content_type = $content_type;
144 $rfc822_header->to[] = $header->dnt;
a20855e4 145 $rfc822_header->subject = _("Read:") . ' ' . encodeHeader($header->subject);
fc224f68 146
27a1d10a 147 // Patch #793504 Return Receipt Failing with <@> from Tim Craig (burny_md)
148 // This merely comes from compose.php and only happens when there is no
149 // email_addr specified in user's identity (which is the startup config)
150 if (ereg("^([^@%/]+)[@%/](.+)$", $username, $usernamedata)) {
151 $popuser = $usernamedata[1];
152 $domain = $usernamedata[2];
153 unset($usernamedata);
154 } else {
155 $popuser = $username;
156 }
fc224f68 157
158 $reply_to = '';
6d021521 159 $ident = get_identities();
160 if(!isset($identity)) $identity = 0;
161 $full_name = $ident[$identity]['full_name'];
162 $from_mail = $ident[$identity]['email_address'];
163 $from_addr = '"'.$full_name.'" <'.$from_mail.'>';
164 $reply_to = $ident[$identity]['reply_to'];
165
27a1d10a 166 if (!$from_mail) {
167 $from_mail = "$popuser@$domain";
168 $from_addr = $from_mail;
fc224f68 169 }
170 $rfc822_header->from = $rfc822_header->parseAddress($from_addr,true);
171 if ($reply_to) {
172 $rfc822_header->reply_to = $rfc822_header->parseAddress($reply_to,true);
173 }
d9c1dccc 174
57257333 175 // part 1 (RFC2298)
57257333 176 $senton = getLongDateString( $header->date );
177 $to_array = $header->to;
178 $to = '';
179 foreach ($to_array as $line) {
af568a82 180 $to .= ' '.$line->getAddress();
57257333 181 }
57257333 182 $now = getLongDateString( time() );
78db1583 183 set_my_charset();
46bb8da8 184 $body = _("Your message") . "\r\n\r\n" .
48027e89 185 "\t" . _("To") . ': ' . decodeHeader($to,false,false) . "\r\n" .
186 "\t" . _("Subject") . ': ' . decodeHeader($header->subject,false,false) . "\r\n" .
187 "\t" . _("Sent") . ': ' . $senton . "\r\n" .
46bb8da8 188 "\r\n" .
189 sprintf( _("Was displayed on %s"), $now );
f69feefe 190
fc224f68 191 $special_encoding = '';
4669e892 192 if (isset($languages[$squirrelmail_language]['XTRA_CODE']) &&
f4bb5d22 193 function_exists($languages[$squirrelmail_language]['XTRA_CODE'] . '_encode')) {
194 $body = call_user_func($languages[$squirrelmail_language]['XTRA_CODE'] . '_encode', $body);
d9c1dccc 195 if (strtolower($default_charset) == 'iso-2022-jp') {
196 if (mb_detect_encoding($body) == 'ASCII') {
197 $special_encoding = '8bit';
198 } else {
199 $body = mb_convert_encoding($body, 'JIS');
200 $special_encoding = '7bit';
201 }
202 }
21461ca4 203 } elseif (sq_is8bit($body)) {
204 $special_encoding = '8bit';
6fbd125b 205 }
fc224f68 206 $part1 = new Message();
207 $part1->setBody($body);
208 $mime_header = new MessageHeader;
209 $mime_header->type0 = 'text';
210 $mime_header->type1 = 'plain';
211 if ($special_encoding) {
212 $mime_header->encoding = $special_encoding;
d9c1dccc 213 } else {
fc224f68 214 $mime_header->encoding = 'us-ascii';
215 }
216 if ($default_charset) {
217 $mime_header->parameters['charset'] = $default_charset;
218 }
219 $part1->mime_header = $mime_header;
220
57257333 221 // part2 (RFC2298)
d9c1dccc 222 $original_recipient = $to;
57257333 223 $original_message_id = $header->message_id;
224
fc224f68 225 $report = "Reporting-UA : $SERVER_NAME ; SquirrelMail (version $version) \r\n";
57257333 226 if ($original_recipient != '') {
fc224f68 227 $report .= "Original-Recipient : $original_recipient\r\n";
57257333 228 }
229 $final_recipient = $sender;
fc224f68 230 $report .= "Final-Recipient: rfc822; $final_recipient\r\n" .
57257333 231 "Original-Message-ID : $original_message_id\r\n" .
232 "Disposition: manual-action/MDN-sent-manually; displayed\r\n";
233
fc224f68 234 $part2 = new Message();
235 $part2->setBody($report);
236 $mime_header = new MessageHeader;
237 $mime_header->type0 = 'message';
238 $mime_header->type1 = 'disposition-notification';
239 $mime_header->encoding = 'us-ascii';
240 $part2->mime_header = $mime_header;
241
242 $composeMessage = new Message();
243 $composeMessage->rfc822_header = $rfc822_header;
244 $composeMessage->addEntity($part1);
245 $composeMessage->addEntity($part2);
246
247
d9c1dccc 248 if ($useSendmail) {
249 require_once(SM_PATH . 'class/deliver/Deliver_SendMail.class.php');
f3dc9c62 250 global $sendmail_path, $sendmail_args;
251 // Check for outdated configuration
252 if (!isset($sendmail_args)) {
253 if ($sendmail_path=='/var/qmail/bin/qmail-inject') {
254 $sendmail_args = '';
255 } else {
256 $sendmail_args = '-i -t';
257 }
258 }
259 $deliver = new Deliver_SendMail(array('sendmail_args'=>$sendmail_args));
d9c1dccc 260 $stream = $deliver->initStream($composeMessage,$sendmail_path);
fc224f68 261 } else {
d9c1dccc 262 require_once(SM_PATH . 'class/deliver/Deliver_SMTP.class.php');
263 $deliver = new Deliver_SMTP();
ce68b76b 264 global $smtpServerAddress, $smtpPort, $pop_before_smtp;
d9c1dccc 265 $authPop = (isset($pop_before_smtp) && $pop_before_smtp) ? true : false;
9bd3b1e6 266 get_smtp_user($user, $pass);
d9c1dccc 267 $stream = $deliver->initStream($composeMessage,$domain,0,
70ce2218 268 $smtpServerAddress, $smtpPort, $user, $pass, $authPop);
d9c1dccc 269 }
270 $success = false;
fc224f68 271 if ($stream) {
d9c1dccc 272 $length = $deliver->mail($composeMessage, $stream);
273 $success = $deliver->finalizeStream($stream);
fc224f68 274 }
d9c1dccc 275 if (!$success) {
a15f9d93 276 $msg = $deliver->dlv_msg;
277 if (! empty($deliver->dlv_server_msg)) {
278 $msg.= '<br />' .
0cc4fb0d 279 _("Server replied:") . ' ' . $deliver->dlv_ret_nr . ' ' .
a15f9d93 280 nl2br(htmlspecialchars($deliver->dlv_server_msg));
281 }
fc224f68 282 plain_error_message($msg, $color);
283 } else {
284 unset ($deliver);
d9c1dccc 285 if (sqimap_mailbox_exists ($imapConnection, $sent_folder)) {
81de00c0 286 $sid = sqimap_append ($imapConnection, $sent_folder, $length);
d9c1dccc 287 require_once(SM_PATH . 'class/deliver/Deliver_IMAP.class.php');
288 $imap_deliver = new Deliver_IMAP();
289 $imap_deliver->mail($composeMessage, $imapConnection);
81de00c0 290 sqimap_append_done ($imapConnection, $sent_folder);
d9c1dccc 291 unset ($imap_deliver);
292 }
293 }
294 return $success;
57257333 295}
296
6201339c 297function ToggleMDNflag ($set ,$imapConnection, $mailbox, $passed_id) {
d9c1dccc 298 $sg = $set?'+':'-';
299 $cmd = 'STORE ' . $passed_id . ' ' . $sg . 'FLAGS ($MDNSent)';
4669e892 300 $read = sqimap_run_command ($imapConnection, $cmd, true, $response,
6201339c 301 $readmessage, TRUE);
57257333 302}
303
4d0cd98b 304function formatRecipientString($recipients, $item ) {
d9c1dccc 305 global $show_more_cc, $show_more, $show_more_bcc,
7d020720 306 $PHP_SELF, $oTemplate;
4d0cd98b 307
d9c1dccc 308 $string = '';
38c944cc 309 if ((is_array($recipients)) && (isset($recipients[0]))) {
d9c1dccc 310 $show = false;
38c944cc 311
312 if ($item == 'to') {
d9c1dccc 313 if ($show_more) {
314 $show = true;
315 $url = set_url_var($PHP_SELF, 'show_more',0);
316 } else {
317 $url = set_url_var($PHP_SELF, 'show_more',1);
318 }
319 } else if ($item == 'cc') {
320 if ($show_more_cc) {
321 $show = true;
322 $url = set_url_var($PHP_SELF, 'show_more_cc',0);
323 } else {
324 $url = set_url_var($PHP_SELF, 'show_more_cc',1);
325 }
326 } else if ($item == 'bcc') {
327 if ($show_more_bcc) {
328 $show = true;
329 $url = set_url_var($PHP_SELF, 'show_more_bcc',0);
330 } else {
331 $url = set_url_var($PHP_SELF, 'show_more_bcc',1);
332 }
333 }
7d020720 334
335 $a = array();
336 foreach ($recipients as $r) {
337 $a[] = array(
338 'Name' => htmlspecialchars($r->getAddress(false)),
339 'Email' => htmlspecialchars($r->getEmail()),
340 'Full' => htmlspecialchars($r->getAddress(true))
341 );
1fca12b5 342 }
7d020720 343
344 $oTemplate->assign('which_field', $item);
345 $oTemplate->assign('recipients', $a);
346 $oTemplate->assign('more_less_toggle_href', $url);
347 $oTemplate->assign('show_more', $show);
348
349 $string = $oTemplate->fetch('read_recipient_list.tpl');
4d0cd98b 350 }
c615b1da 351 return $string;
352}
353
e0e30169 354function formatEnvheader($aMailbox, $passed_id, $passed_ent_id, $message,
af568a82 355 $color, $FirstTimeSee) {
ce68b76b 356 global $default_use_mdn, $default_use_priority,
457e8593 357 $show_xmailer_default, $mdn_user_support, $PHP_SELF,
7d020720 358 $squirrelmail_language, $oTemplate;
38bca81c 359
134e4174 360 $mailbox = $aMailbox['NAME'];
e0e30169 361
d9c1dccc 362 $header = $message->rfc822_header;
363 $env = array();
bdb9ce72 364 $env[_("Subject")] = str_replace("&nbsp;"," ",decodeHeader($header->subject));
7893c8a5 365
d9c1dccc 366 $from_name = $header->getAddr_s('from');
d6b6d221 367 if (!$from_name)
d9c1dccc 368 $from_name = $header->getAddr_s('sender');
d6b6d221 369 if (!$from_name)
370 $env[_("From")] = _("Unknown sender");
371 else
372 $env[_("From")] = decodeHeader($from_name);
d9c1dccc 373 $env[_("Date")] = getLongDateString($header->date);
374 $env[_("To")] = formatRecipientString($header->to, "to");
375 $env[_("Cc")] = formatRecipientString($header->cc, "cc");
376 $env[_("Bcc")] = formatRecipientString($header->bcc, "bcc");
377 if ($default_use_priority) {
7d020720 378 $oTemplate->assign('message_priority', $header->priority);
379 $env[_("Priority")] = $oTemplate->fetch('read_message_priority.tpl');
d9c1dccc 380 }
381 if ($show_xmailer_default) {
7d020720 382 $oTemplate->assign('xmailer', decodeHeader($header->xmailer));
383 $env[_("Mailer")] = $oTemplate->fetch('read_xmailer.tpl');
d9c1dccc 384 }
385 if ($default_use_mdn) {
386 if ($mdn_user_support) {
387 if ($header->dnt) {
7d020720 388 $mdn_url = $PHP_SELF;
389 $mdn_url = set_url_var($PHP_SELF, 'mailbox', urlencode($mailbox));
390 $mdn_url = set_url_var($PHP_SELF, 'passed_id', $passed_id);
391 $mdn_url = set_url_var($PHP_SELF, 'passed_ent_id', $passed_ent_id);
392 $mdn_url = set_url_var($PHP_SELF, 'sendreceipt', 1);
393
394 $oTemplate->assign('read_receipt_sent', $message->is_mdnsent);
395 $oTemplate->assign('first_time_reading', $FirstTimeSee);
396 $oTemplate->assign('send_receipt_href', $mdn_url);
397
398 $env[_("Read Receipt")] = $oTemplate->fetch('read_handle_receipt.tpl');
d9c1dccc 399 }
400 }
401 }
7d020720 402 $env[_("Options")] = formatToolbar($mailbox, $passed_id, $passed_ent_id, $message, $color);
c615b1da 403
7d020720 404 $oTemplate->assign('headers_to_display', $env);
405
406 $oTemplate->display('read_headers.tpl');
d9c1dccc 407}
c615b1da 408
1e150c97 409/**
4669e892 410 * Format message toolbar
411 *
1e150c97 412 * @param string $mailbox Name of current mailbox
413 * @param int $passed_id UID of current message
414 * @param int $passed_ent_id Id of entity within message
415 * @param object $message Current message object
416 * @param object $mbx_response
417 */
324ac3c5 418function formatMenubar($aMailbox, $passed_id, $passed_ent_id, $message, $removedVar, $nav_on_top = TRUE) {
7d020720 419 global $base_uri, $draft_folder, $where, $what, $sort,
4669e892 420 $startMessage, $PHP_SELF, $save_as_draft,
1e150c97 421 $enable_forward_as_attachment, $imapConnection, $lastTargetMailbox,
7d020720 422 $delete_prev_next_display, $show_copy_buttons,
457e8593 423 $compose_new_win, $compose_width, $compose_height,
7d020720 424 $oTemplate;
c615b1da 425
e0e30169 426 //FIXME cleanup argument list, use $aMailbox where possible
427 $mailbox = $aMailbox['NAME'];
428
d9c1dccc 429 $urlMailbox = urlencode($mailbox);
c615b1da 430
1e150c97 431 $msgs_url = $base_uri . 'src/';
2ffa3f57 432
1e150c97 433 // Create Prev & Next links
434 // Handle nested entities first (i.e. Mime Attach parts)
7d020720 435 $prev_href = $next_href = $up_href = $del_href = $del_prev_href = $del_next_href = '';
436 $msg_list_href = $search_href = $view_msg_href = '';
1e150c97 437 if (isset($passed_ent_id) && $passed_ent_id) {
438 // code for navigating through attached message/rfc822 messages
d9c1dccc 439 $url = set_url_var($PHP_SELF, 'passed_ent_id',0);
70bdc740 440 $entities = array();
441 $entity_count = array();
442 $c = 0;
f2f03410 443
70bdc740 444 foreach($message->parent->entities as $ent) {
9f42bfc8 445 if ($ent->type0 == 'message' && $ent->type1 == 'rfc822') {
7d020720 446
9f42bfc8 447 $c++;
448 $entity_count[$c] = $ent->entity_id;
449 $entities[$ent->entity_id] = $c;
450 }
70bdc740 451 }
1e150c97 452
91c27aee 453 if(isset($entities[$passed_ent_id]) && $entities[$passed_ent_id] > 1) {
70bdc740 454 $prev_ent_id = $entity_count[$entities[$passed_ent_id] - 1];
7d020720 455 $prev_href = set_url_var($PHP_SELF, 'passed_ent_id', $prev_ent_id);
70bdc740 456 }
1e150c97 457
91c27aee 458 if(isset($entities[$passed_ent_id]) && $entities[$passed_ent_id] < $c) {
70bdc740 459 $next_ent_id = $entity_count[$entities[$passed_ent_id] + 1];
7d020720 460 $next_href = set_url_var($PHP_SELF, 'passed_ent_id', $next_ent_id);
70bdc740 461 }
1e150c97 462
d9c1dccc 463 $par_ent_id = $message->parent->entity_id;
464 if ($par_ent_id) {
465 $par_ent_id = substr($par_ent_id,0,-2);
1e150c97 466 if ( $par_ent_id != 0 ) {
7d020720 467 $up_href = set_url_var($PHP_SELF, 'passed_ent_id',$par_ent_id);
1e150c97 468 }
469 }
470
7d020720 471 $view_msg_href = $url;
1e150c97 472
473 // Prev/Next links for regular messages
324ac3c5 474 } else if ( true ) { //!(isset($where) && isset($what)) ) {
324ac3c5 475 $prev = findPreviousMessage($aMailbox['UIDSET'][$what], $passed_id);
476 $next = findNextMessage($aMailbox['UIDSET'][$what],$passed_id);
1e150c97 477
1e150c97 478 if ($prev >= 0) {
7d020720 479 $prev_href = $base_uri . 'src/read_body.php?passed_id='.$prev.
1e150c97 480 '&amp;mailbox='.$urlMailbox.'&amp;sort='.$sort.
324ac3c5 481 "&amp;where=$where&amp;what=$what" .
1e150c97 482 '&amp;startMessage='.$startMessage.'&amp;show_more=0';
d9c1dccc 483 }
1e150c97 484
1e150c97 485 if ($next >= 0) {
7d020720 486 $next_href = $base_uri . 'src/read_body.php?passed_id='.$next.
1e150c97 487 '&amp;mailbox='.$urlMailbox.'&amp;sort='.$sort.
324ac3c5 488 "&amp;where=$where&amp;what=$what" .
1e150c97 489 '&amp;startMessage='.$startMessage.'&amp;show_more=0';
1e150c97 490 }
491
1e150c97 492 // Only bother with Delete & Prev and Delete & Next IF
6201339c 493 // top display is enabled.
4669e892 494 if ( $delete_prev_next_display == 1 &&
324ac3c5 495 in_array('\\deleted', $aMailbox['PERMANENTFLAGS'],true) ) {
1e150c97 496 if ($prev >= 0) {
7d020720 497 $del_prev_href = $base_uri . 'src/read_body.php?passed_id='.$prev.
1e150c97 498 '&amp;mailbox='.$urlMailbox.'&amp;sort='.$sort.
499 '&amp;startMessage='.$startMessage.'&amp;show_more=0'.
324ac3c5 500 "&amp;where=$where&amp;what=$what" .
1e150c97 501 '&amp;delete_id='.$passed_id;
1e150c97 502 }
503
1e150c97 504 if ($next >= 0) {
7d020720 505 $del_next_href = $base_uri . 'src/read_body.php?passed_id='.$next.
1e150c97 506 '&amp;mailbox='.$urlMailbox.'&amp;sort='.$sort.
507 '&amp;startMessage='.$startMessage.'&amp;show_more=0'.
324ac3c5 508 "&amp;where=$where&amp;what=$what" .
1e150c97 509 '&amp;delete_id='.$passed_id;
1e150c97 510 }
1e150c97 511 }
512 }
513
514 // Start with Search Results or Message List link.
7d020720 515 $list_xtra = "?where=read_body.php&amp;what=$what&amp;mailbox=" . $urlMailbox.
324ac3c5 516 "&amp;startMessage=$startMessage";
7d020720 517 $msg_list_href = $base_uri .'src/right_main.php'. $list_xtra;
518 $search_href = $where=='search.php' ? $base_uri .'src/search.php?'.$list_xtra : '';
1e150c97 519
98a9cc03 520 $comp_uri = $base_uri.'src/compose.php' .
521 '?passed_id=' . $passed_id .
522 '&amp;mailbox=' . $urlMailbox .
523 '&amp;startMessage=' . $startMessage .
524 (isset($passed_ent_id) ? '&amp;passed_ent_id='.$passed_ent_id : '');
4669e892 525
526 // Start form for reply/reply all/forward..
1a531551 527 $target = '';
528 $on_click='';
ee66175d 529 $method='method="post" ';
6190378c 530 $onsubmit='';
1a531551 531 if ($compose_new_win == '1') {
91c27aee 532 if (!preg_match("/^[0-9]{3,4}$/", $compose_width)) {
533 $compose_width = '640';
534 }
535 if (!preg_match("/^[0-9]{3,4}$/", $compose_height)) {
536 $compose_height = '550';
537 }
457e8593 538 if ( checkForJavascript() ) {
91c27aee 539 $on_click=' onclick="comp_in_new_form(\''.$comp_uri.'\', this, this.form,'. $compose_width .',' . $compose_height .')"';
1a531551 540 $comp_uri = 'javascript:void(0)';
ee66175d 541 $method='method="get" ';
6190378c 542 $onsubmit = 'onsubmit="return false" ';
1a531551 543 } else {
544 $target = 'target="_blank"';
1a531551 545 }
546 }
547
7d020720 548 $oTemplate->assign('nav_on_top', $nav_on_top);
98a9cc03 549
7d020720 550 $oTemplate->assign('prev_href', $prev_href);
551 $oTemplate->assign('up_href', $up_href);
552 $oTemplate->assign('next_href', $next_href);
553 $oTemplate->assign('del_prev_href', $del_prev_href);
554 $oTemplate->assign('del_next_href', $del_next_href);
555 $oTemplate->assign('view_msg_href', $view_msg_href);
556
557 $oTemplate->assign('message_list_href', $msg_list_href);
558 $oTemplate->assign('search_href', $search_href);
559
560 $oTemplate->assign('form_extra', $method . $target . $onsubmit);
561 $oTemplate->assign('compose_href', $comp_uri);
562 $oTemplate->assign('button_onclick', $on_click);
563 $oTemplate->assign('forward_as_attachment_enabled', $enable_forward_as_attachment==1);
564
d08a5945 565 //FIXME: I am surprised these aren't already given to the template; probably needs to be given at a higher level, so I have NO IDEA if this is the right place to do this... adding them so template can construct its own API calls... we can build those herein too if preferrable
566 $oTemplate->assign('mailbox', $aMailbox['NAME']);
567 $oTemplate->assign('passed_id', $passed_id);
568 $oTemplate->assign('what', $what);
569
1e150c97 570 // If Draft folder - create Resume link
7d020720 571 $resume_draft = $edit_as_new = false;
1e150c97 572 if (($mailbox == $draft_folder) && ($save_as_draft)) {
7d020720 573 $resume_draft = true; 'smaction_draft';
1e150c97 574 } else if (handleAsSent($mailbox)) {
7d020720 575 $edit_as_new = true;
1e150c97 576 }
7d020720 577 $oTemplate->assign('can_resume_draft', $resume_draft);
578 $oTemplate->assign('can_edit_as_new', $edit_as_new);
579
580 $oTemplate->assign('mailboxes', sqimap_mailbox_option_array($imapConnection));
581 if (in_array('\\deleted', $aMailbox['PERMANENTFLAGS'],true)) {
324ac3c5 582 $delete_url = $base_uri . "src/$where";
7d020720 583 $oTemplate->assign('can_be_deleted', true);
584 $oTemplate->assign('move_delete_form_action', $base_uri.'src/'.$where);
585 $oTemplate->assign('delete_form_extra', addHidden('mailbox', $aMailbox['NAME'])."\n" .
586 addHidden('msg[0]', $passed_id)."\n" .
587 addHidden('startMessage', $startMessage)."\n" );
4669e892 588 if (!(isset($passed_ent_id) && $passed_ent_id)) {
7d020720 589 $oTemplate->assign('can_be_moved', true);
590 $oTemplate->assign('move_form_extra', addHidden('mailbox', $aMailbox['NAME'])."\n" .
591 addHidden('msg[0]', $passed_id)."\n" );
592 $oTemplate->assign('last_move_target', isset($lastTargetMailbox) && !empty($lastTargetMailbox) ? $lastTargetMailbox : '');
593 $oTemplate->assign('can_be_copied', $show_copy_buttons==1);
1e150c97 594 } else {
7d020720 595 $oTemplate->assign('can_be_moved', false);
596 $oTemplate->assign('move_form_extra', '');
597 $oTemplate->assign('last_move_target', '');
598 $oTemplate->assign('can_be_copied', false);
988e24f6 599 }
7d020720 600 } else {
601 $oTemplate->assign('can_be_deleted', false);
602 $oTemplate->assign('move_delete_form_action', '');
603 $oTemplate->assign('delete_form_extra', '');
604 $oTemplate->assign('can_be_moved', false);
605 $oTemplate->assign('move_form_extra', '');
606 $oTemplate->assign('last_move_target', '');
607 $oTemplate->assign('can_be_copied', false);
608 }
609
610 if ($nav_on_top) {
611 $oTemplate->display('read_menubar_nav.tpl');
612 $oTemplate->display('read_menubar_buttons.tpl');
613 } else {
614 $oTemplate->display('read_menubar_buttons.tpl');
615 $oTemplate->display('read_menubar_nav.tpl');
764971df 616 }
7d020720 617
6e515418 618 global $null;
619 do_hook('read_body_menu_bottom', $null);
c615b1da 620}
621
622function formatToolbar($mailbox, $passed_id, $passed_ent_id, $message, $color) {
7d020720 623 global $base_uri, $where, $what, $show_html_default,
457e8593 624 $oTemplate, $download_href,
7d020720 625 $unsafe_image_toggle_href, $unsafe_image_toggle_text;
4669e892 626
d9c1dccc 627 $urlMailbox = urlencode($mailbox);
78909469 628 $urlPassed_id = urlencode($passed_id);
629 $urlPassed_ent_id = urlencode($passed_ent_id);
4669e892 630
78909469 631 $query_string = 'mailbox=' . $urlMailbox . '&amp;passed_id=' . $urlPassed_id . '&amp;passed_ent_id=' . $urlPassed_ent_id;
78909469 632 if (!empty($where)) {
633 $query_string .= '&amp;where=' . urlencode($where);
634 }
78909469 635 if (!empty($what)) {
636 $query_string .= '&amp;what=' . urlencode($what);
637 }
e95c04ec 638 $url = $base_uri.'src/view_header.php?'.$query_string;
d9c1dccc 639
d9c1dccc 640
7d020720 641 // Build the printer friend link
642 /* hackydiehack */
643 if( !sqgetGlobalVar('view_unsafe_images', $view_unsafe_images, SQ_GET) ) {
644 $view_unsafe_images = false;
645 } else {
646 $view_unsafe_images = true;
40a34e57 647 }
7d020720 648 $pf_params = '?passed_ent_id=' . $urlPassed_ent_id .
649 '&mailbox=' . $urlMailbox .
650 '&passed_id=' . $urlPassed_id .
651 '&view_unsafe_images='. (bool) $view_unsafe_images .
652 '&show_html_default=' . $show_html_default;
653 $links = array();
654 $links[] = array (
655 'URL' => $url,
656 'Text' => _("View Full Header")
657 );
658 $links[] = array (
659 'URL' => $pf_params,
660 'Text' => _("View Printable Version")
661 );
662 $links[] = array (
663 'URL' => $download_href,
210b5bb5 664 'Text' => _("Download this as a file")
7d020720 665 );
666 $toggle = html_toggle_href($mailbox, $passed_id, $passed_ent_id, $message);
667 if (!empty($toggle)) {
668 $links[] = array (
669 'URL' => $toggle,
670 'Text' => $show_html_default==1 ? _("View as plain text") : _("View as HTML")
671 );
672 }
673 if (!empty($unsafe_image_toggle_href)) {
674 $links[] = array (
675 'URL' => $unsafe_image_toggle_href,
676 'Text' => $unsafe_image_toggle_text
677 );
678 }
679
680 do_hook('read_body_header_right', $links);
681
682 $oTemplate->assign('links', $links);
683
684 return $oTemplate->fetch('read_toolbar.tpl');
91c27aee 685}
686
6206f6c4 687/***************************/
2ffa3f57 688/* Main of read_body.php */
6206f6c4 689/***************************/
57257333 690
0b97a708 691/* get the globals we may need */
692
e95c04ec 693sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION);
e95c04ec 694sqgetGlobalVar('lastTargetMailbox', $lastTargetMailbox, SQ_SESSION);
e95c04ec 695if (!sqgetGlobalVar('messages', $messages, SQ_SESSION) ) {
696 $messages = array();
ce274df9 697}
81de00c0 698sqgetGlobalVar('delayed_errors', $delayed_errors, SQ_SESSION);
699if (is_array($delayed_errors)) {
700 $oErrorHandler->AssignDelayedErrors($delayed_errors);
701 sqsession_unregister("delayed_errors");
702}
e95c04ec 703/** GET VARS */
704sqgetGlobalVar('sendreceipt', $sendreceipt, SQ_GET);
324ac3c5 705if (!sqgetGlobalVar('where', $where, SQ_GET) ) {
706 $where = 'right_main.php';
707}
d0b119a5 708/*
709 * Used as entry key to the list of uid's cached in the mailbox cache
710 * we use the cached uid's to get the next and prev message.
711 */
324ac3c5 712if (!sqgetGlobalVar('what', $what, SQ_GET) ){
713 $what = 0;
714}
e95c04ec 715if ( sqgetGlobalVar('show_more', $temp, SQ_GET) ) {
716 $show_more = (int) $temp;
1b9f3c04 717}
e95c04ec 718if ( sqgetGlobalVar('show_more_cc', $temp, SQ_GET) ) {
719 $show_more_cc = (int) $temp;
0b97a708 720}
e95c04ec 721if ( sqgetGlobalVar('show_more_bcc', $temp, SQ_GET) ) {
722 $show_more_bcc = (int) $temp;
0b97a708 723}
e95c04ec 724if ( sqgetGlobalVar('view_hdr', $temp, SQ_GET) ) {
725 $view_hdr = (int) $temp;
0b97a708 726}
e95c04ec 727
91c27aee 728if ( sqgetGlobalVar('account', $temp, SQ_GET) ) {
729 $iAccount = (int) $temp;
730} else {
731 $iAccount = 0;
732}
733
e95c04ec 734/** GET/POST VARS */
735sqgetGlobalVar('passed_ent_id', $passed_ent_id);
736sqgetGlobalVar('mailbox', $mailbox);
737
738if ( sqgetGlobalVar('passed_id', $temp) ) {
739 $passed_id = (int) $temp;
0b97a708 740}
e95c04ec 741if ( sqgetGlobalVar('sort', $temp) ) {
742 $sort = (int) $temp;
0b97a708 743}
e95c04ec 744if ( sqgetGlobalVar('startMessage', $temp) ) {
745 $startMessage = (int) $temp;
324ac3c5 746} else {
747 $startMessage = 1;
ce274df9 748}
7277191e 749if(sqgetGlobalVar('show_html_default', $temp)) {
750 $show_html_default = (int) $temp;
751}
752
753if(sqgetGlobalVar('view_unsafe_images', $temp)) {
754 $view_unsafe_images = (int) $temp;
755 if($view_unsafe_images == 1) {
756 $show_html_default = 1;
757 }
758} else {
759 $view_unsafe_images = 0;
760}
324ac3c5 761/**
762 * Retrieve mailbox cache
763 */
764sqgetGlobalVar('mailbox_cache',$mailbox_cache,SQ_SESSION);
0b97a708 765
ce274df9 766/* end of get globals */
6201339c 767global $sqimap_capabilities, $lastTargetMailbox;
38c944cc 768
906f7e9f 769$imapConnection = sqimap_login($username, false, $imapServerAddress, $imapPort, 0);
91c27aee 770$aMailbox = sqm_api_mailbox_select($imapConnection, $iAccount, $mailbox,array('setindex' => $what, 'offset' => $startMessage),array());
771
c3731db5 772
91c27aee 773/**
c3731db5 774 Start code to set the columns to fetch in case of hitting the next/prev link
775 The reason for this is the fact that the cache can be invalidated which means that the headers
776 to fetch aren't there anymore. Before they got calculated when the messagelist was shown.
777
778 Todo, better central handling of setting the mailbox options so we do not need to do the stuff below
779*/
780
781/**
782 * Replace From => To in case it concerns a draft or sent folder
783 */
199ff86a 784$aColumns = array();
c3731db5 785if (($mailbox == $sent_folder || $mailbox == $draft_folder) &&
786 !in_array(SQM_COL_TO,$index_order)) {
787 $aNewOrder = array(); // nice var name ;)
788 foreach($index_order as $iCol) {
789 if ($iCol == SQM_COL_FROM) {
790 $iCol = SQM_COL_TO;
791 }
199ff86a 792 $aColumns[$iCol] = array();
c3731db5 793 }
c3731db5 794} else {
199ff86a 795 foreach ($index_order as $iCol) {
796 $aColumns[$iCol] = array();
797 }
c3731db5 798}
799
800$aProps = array(
801 'columns' => $aColumns, // columns bound settings
802 'config' => array(
803 'highlight_list' => $message_highlight_list, // row highlighting rules
804 'trash_folder' => $trash_folder,
805 'sent_folder' => $sent_folder,
806 'draft_folder' => $draft_folder));
807
808calcFetchColumns($aMailbox,$aProps);
809
810/**
811 End code to set the columns to fetch in case of hitting the next/prev link
812*/
813
814
815
816/**
817 * Check if cache is still valid, $what contains the key
818 * which gives us acces to the array with uid's. At this moment
819 * 0 is used for a normal message list and search uses 1 as key. This can be
820 * changed / extended in the future.
821 * If on a select of a mailbox we detect that the cache should be invalidated due to
822 * the delete of messages or due to new messages we empty the list with uid's and
823 * that's what we detect below.
824 */
91c27aee 825if (!is_array($aMailbox['UIDSET'][$what])) {
d0b119a5 826 fetchMessageHeaders($imapConnection, $aMailbox);
91c27aee 827}
38c944cc 828
d0b119a5 829$iSetIndex = $aMailbox['SETINDEX'];
830$aMailbox['CURRENT_MSG'][$iSetIndex] = $passed_id;
831
324ac3c5 832/**
833 * Update the seen state
834 * and ignore in_array('\\seen',$aMailbox['PERMANENTFLAGS'],true)
835 */
836if (isset($aMailbox['MSG_HEADERS'][$passed_id]['FLAGS'])) {
837 $aMailbox['MSG_HEADERS'][$passed_id]['FLAGS']['\\seen'] = true;
e0e30169 838}
e0e30169 839
1e150c97 840/**
841 * Process Delete from delete-move-next
842 * but only if delete_id was set
843 */
844if ( sqgetGlobalVar('delete_id', $delete_id, SQ_GET) ) {
324ac3c5 845 handleMessageListForm($imapConnection,$aMailbox,$sButton='setDeleted', array($delete_id));
1e150c97 846}
847
38c944cc 848/**
849 * $message contains all information about the message
850 * including header and body
851 */
37d2a6ee 852
324ac3c5 853if (isset($aMailbox['MSG_HEADERS'][$passed_id]['MESSAGE_OBJECT'])) {
854 $message = $aMailbox['MSG_HEADERS'][$passed_id]['MESSAGE_OBJECT'];
855 $FirstTimeSee = !$message->is_seen;
38c944cc 856} else {
324ac3c5 857 $message = sqimap_get_message($imapConnection, $passed_id, $mailbox);
858 $FirstTimeSee = !$message->is_seen;
859 $message->is_seen = true;
860 $aMailbox['MSG_HEADERS'][$passed_id]['MESSAGE_OBJECT'] = $message;
5200c026 861}
dd628162 862if (isset($passed_ent_id) && $passed_ent_id) {
324ac3c5 863 $message = $message->getEntity($passed_ent_id);
864 if ($message->type0 != 'message' && $message->type1 != 'rfc822') {
865 $message = $message->parent;
866 }
867 $read = sqimap_run_command ($imapConnection, "FETCH $passed_id BODY[$passed_ent_id.HEADER]", true, $response, $msg, TRUE);
868 $rfc822_header = new Rfc822Header();
869 $rfc822_header->parseHeader($read);
870 $message->rfc822_header = $rfc822_header;
91c27aee 871} else if ($message->type0 == 'message' && $message->type1 == 'rfc822' && isset($message->entities[0])) {
872 $read = sqimap_run_command ($imapConnection, "FETCH $passed_id BODY[1.HEADER]", true, $response, $msg, TRUE);
873 $rfc822_header = new Rfc822Header();
874 $rfc822_header->parseHeader($read);
875 $message->rfc822_header = $rfc822_header;
c615b1da 876} else {
324ac3c5 877 $passed_ent_id = 0;
38c944cc 878}
5200c026 879$header = $message->header;
57257333 880
57257333 881
6206f6c4 882/****************************************/
883/* Block for handling incoming url vars */
884/****************************************/
5200c026 885
5200c026 886if (isset($sendreceipt)) {
887 if ( !$message->is_mdnsent ) {
27a1d10a 888 $final_recipient = '';
134e4174 889 if ((isset($identity)) && ($identity != 0)) //Main identity
27a1d10a 890 $final_recipient = trim(getPref($data_dir, $username, 'email_address' . $identity, '' ));
891 if ($final_recipient == '' )
892 $final_recipient = trim(getPref($data_dir, $username, 'email_address', '' ));
324ac3c5 893 $supportMDN = ServerMDNSupport($aMailbox["PERMANENTFLAGS"]);
fc224f68 894 if ( SendMDN( $mailbox, $passed_id, $final_recipient, $message, $imapConnection ) > 0 && $supportMDN ) {
6201339c 895 ToggleMDNflag( true, $imapConnection, $mailbox, $passed_id);
5200c026 896 $message->is_mdnsent = true;
1012f961 897 $aMailbox['MSG_HEADERS'][$passed_id]['MESSAGE_OBJECT'] = $message;
5200c026 898 }
5200c026 899 }
900}
6206f6c4 901/***********************************************/
902/* End of block for handling incoming url vars */
903/***********************************************/
904
9f42bfc8 905$messagebody = '';
6e515418 906do_hook('read_body_top', $null);
c4ad259b 907if ($show_html_default == 1) {
908 $ent_ar = $message->findDisplayEntity(array());
909} else {
910 $ent_ar = $message->findDisplayEntity(array(), array('text/plain'));
911}
a128c967 912$cnt = count($ent_ar);
913for ($i = 0; $i < $cnt; $i++) {
6a108031 914 $messagebody .= formatBody($imapConnection, $message, $color, $wrap_at, $ent_ar[$i], $passed_id, $mailbox);
a128c967 915 if ($i != $cnt-1) {
7d020720 916 $messagebody .= '<hr />';
a128c967 917 }
38c944cc 918}
e55c441a 919
202bcbcc 920/**
921 * Write mailbox with updated seen flag information back to cache.
922 */
923$mailbox_cache[$iAccount.'_'.$aMailbox['NAME']] = $aMailbox;
924sqsession_register($mailbox_cache,'mailbox_cache');
925$_SESSION['mailbox_cache'] = $mailbox_cache;
202bcbcc 926
b89ceca6 927displayPageHeader($color, $mailbox,'','');
324ac3c5 928formatMenuBar($aMailbox, $passed_id, $passed_ent_id, $message,false);
e0e30169 929formatEnvheader($aMailbox, $passed_id, $passed_ent_id, $message, $color, $FirstTimeSee);
7d020720 930
931$oTemplate->assign('message_body', $messagebody);
932$oTemplate->display('read_message_body.tpl');
933
934formatAttachments($message,$ent_ar,$mailbox, $passed_id);
a07cd1a4 935
936/* show attached images inline -- if pref'fed so */
7d020720 937if ($attachment_common_show_images && is_array($attachment_common_show_images_list)) {
938 $images = array();
a07cd1a4 939 foreach ($attachment_common_show_images_list as $img) {
ce274df9 940 $imgurl = SM_PATH . 'src/download.php' .
57257333 941 '?' .
cd7b8833 942 'passed_id=' . urlencode($img['passed_id']) .
3d570ba0 943 '&amp;mailbox=' . urlencode($mailbox) .
ff9d4297 944 '&amp;ent_id=' . urlencode($img['ent_id']) .
3d570ba0 945 '&amp;absolute_dl=true';
7d020720 946 $a = array();
947 $a['Name'] = $img['name'];
948 $a['DisplayURL'] = $imgurl;
949 $a['DownloadURL'] = $img['download_href'];
950 $images[] = $a;
10f0ce72 951 }
7d020720 952
953 $oTemplate->assign('images', $images);
954 $oTemplate->display('read_display_images_inline.tpl');
a07cd1a4 955}
956
324ac3c5 957formatMenuBar($aMailbox, $passed_id, $passed_ent_id, $message, false, FALSE);
1e150c97 958
6e515418 959do_hook('read_body_bottom', $null);
a07cd1a4 960sqimap_logout($imapConnection);
51d66ea2 961$oTemplate->display('footer.tpl');
210b5bb5 962?>