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