Support for $Forwarded IMAP keyword, when it is supported or when arbitrary keywords...
[squirrelmail.git] / src / compose.php
CommitLineData
59177427 1<?php
35586184 2/**
3 * compose.php
4 *
35586184 5 * This code sends a mail.
6 *
7 * There are 4 modes of operation:
8 * - Start new mail
9 * - Add an attachment
10 * - Send mail
11 * - Save As Draft
12 *
47ccfad4 13 * @copyright &copy; 1999-2006 The SquirrelMail Project Team
4b4abf93 14 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
30967a1e 15 * @version $Id$
8f6f9ba5 16 * @package squirrelmail
35586184 17 */
f7fb20fe 18
30967a1e 19/**
202bcbcc 20 * Include the SquirrelMail initialization file.
30967a1e 21 */
202bcbcc 22require('../include/init.php');
86725763 23
24/* SquirrelMail required files. */
202bcbcc 25require_once(SM_PATH . 'functions/imap_general.php');
26require_once(SM_PATH . 'functions/imap_messages.php');
86725763 27require_once(SM_PATH . 'functions/date.php');
28require_once(SM_PATH . 'functions/mime.php');
86725763 29require_once(SM_PATH . 'class/deliver/Deliver.class.php');
24192f77 30require_once(SM_PATH . 'functions/addressbook.php');
df96b37a 31require_once(SM_PATH . 'functions/forms.php');
a2b193bc 32require_once(SM_PATH . 'functions/identity.php');
91f2085b 33
0b97a708 34/* --------------------- Get globals ------------------------------------- */
0b97a708 35
953fa718 36/** SESSION VARS */
953fa718 37sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION);
38
39sqgetGlobalVar('composesession', $composesession, SQ_SESSION);
40sqgetGlobalVar('compose_messages', $compose_messages, SQ_SESSION);
81de00c0 41sqgetGlobalVar('delayed_errors', $delayed_errors, SQ_SESSION);
762853f4 42
43// Turn on delayed error handling in case we wind up redirecting below
44$oErrorHandler->setDelayedErrors(true);
953fa718 45
46/** SESSION/POST/GET VARS */
b455793d 47sqgetGlobalVar('session',$session);
48sqgetGlobalVar('mailbox',$mailbox);
1e2a6ff6 49if(!sqgetGlobalVar('identity',$identity)) {
50 $identity=0;
51}
edd82fcf 52sqgetGlobalVar('send_to',$send_to);
53sqgetGlobalVar('send_to_cc',$send_to_cc);
54sqgetGlobalVar('send_to_bcc',$send_to_bcc);
55sqgetGlobalVar('subject',$subject);
56sqgetGlobalVar('body',$body);
b455793d 57sqgetGlobalVar('mailprio',$mailprio);
58sqgetGlobalVar('request_mdn',$request_mdn);
59sqgetGlobalVar('request_dr',$request_dr);
60sqgetGlobalVar('html_addr_search',$html_addr_search);
61sqgetGlobalVar('mail_sent',$mail_sent);
62sqgetGlobalVar('passed_id',$passed_id);
63sqgetGlobalVar('passed_ent_id',$passed_ent_id);
64sqgetGlobalVar('send',$send);
0b97a708 65
b455793d 66sqgetGlobalVar('attach',$attach);
12a0ed01 67
b455793d 68sqgetGlobalVar('draft',$draft);
69sqgetGlobalVar('draft_id',$draft_id);
70sqgetGlobalVar('ent_num',$ent_num);
71sqgetGlobalVar('saved_draft',$saved_draft);
72sqgetGlobalVar('delete_draft',$delete_draft);
a6d3eff6 73if ( sqgetGlobalVar('startMessage',$startMessage) ) {
74 $startMessage = (int)$startMessage;
75} else {
76 $startMessage = 1;
77}
953fa718 78
8780308f 79
953fa718 80/** POST VARS */
167c6996 81sqgetGlobalVar('sigappend', $sigappend, SQ_POST);
82sqgetGlobalVar('from_htmladdr_search', $from_htmladdr_search, SQ_POST);
83sqgetGlobalVar('addr_search_done', $html_addr_search_done, SQ_POST);
84sqgetGlobalVar('addr_search_cancel', $html_addr_search_cancel, SQ_POST);
85sqgetGlobalVar('send_to_search', $send_to_search, SQ_POST);
86sqgetGlobalVar('do_delete', $do_delete, SQ_POST);
87sqgetGlobalVar('delete', $delete, SQ_POST);
88sqgetGlobalVar('restoremessages', $restoremessages, SQ_POST);
953fa718 89if ( sqgetGlobalVar('return', $temp, SQ_POST) ) {
73ad81bf 90 $html_addr_search_done = 'Use Addresses';
953fa718 91}
92
93/** GET VARS */
94sqgetGlobalVar('attachedmessages', $attachedmessages, SQ_GET);
8780308f 95if ( sqgetGlobalVar('account', $temp, SQ_GET) ) {
96 $iAccount = (int) $temp;
97} else {
98 $iAccount = 0;
99}
100
0b97a708 101
98a9cc03 102/** get smaction */
103if ( !sqgetGlobalVar('smaction',$action) )
104{
73ad81bf 105 if ( sqgetGlobalVar('smaction_reply',$tmp) ) $action = 'reply';
106 if ( sqgetGlobalVar('smaction_reply_all',$tmp) ) $action = 'reply_all';
107 if ( sqgetGlobalVar('smaction_forward',$tmp) ) $action = 'forward';
108 if ( sqgetGlobalVar('smaction_attache',$tmp) ) $action = 'forward_as_attachment';
109 if ( sqgetGlobalVar('smaction_draft',$tmp) ) $action = 'draft';
110 if ( sqgetGlobalVar('smaction_edit_new',$tmp) ) $action = 'edit_as_new';
98a9cc03 111}
112
3461167c 113/* Location (For HTTP 1.1 Header("Location: ...") redirects) */
114$location = get_location();
1e2a6ff6 115/* Identities (fetch only once) */
116$idents = get_identities();
3461167c 117
09044055 118/* --------------------- Specific Functions ------------------------------ */
0b97a708 119
41b94d65 120function replyAllString($header) {
73ad81bf 121 global $include_self_reply_all, $idents;
122 $excl_ar = array();
123 /**
124 * 1) Remove the addresses we'll be sending the message 'to'
125 */
73ad81bf 126 if (isset($header->replyto)) {
127 $excl_ar = $header->getAddr_a('replyto');
128 }
129 /**
130 * 2) Remove our identities from the CC list (they still can be in the
131 * TO list) only if $include_self_reply_all is turned off
132 */
133 if (!$include_self_reply_all) {
134 foreach($idents as $id) {
135 $excl_ar[strtolower(trim($id['email_address']))] = '';
136 }
137 }
138
139 /**
140 * 3) get the addresses.
141 */
142 $url_replytoall_ar = $header->getAddr_a(array('to','cc'), $excl_ar);
143
144 /**
145 * 4) generate the string.
146 */
147 $url_replytoallcc = '';
148 foreach( $url_replytoall_ar as $email => $personal) {
149 if ($personal) {
150 // if personal name contains address separator then surround
151 // the personal name with double quotes.
152 if (strpos($personal,',') !== false) {
153 $personal = '"'.$personal.'"';
154 }
155 $url_replytoallcc .= ", $personal <$email>";
156 } else {
157 $url_replytoallcc .= ', '. $email;
1e2a6ff6 158 }
73ad81bf 159 }
160 $url_replytoallcc = substr($url_replytoallcc,2);
161
162 return $url_replytoallcc;
09044055 163}
164
50706f77 165/**
166 * creates top line in reply citations
167 *
168 * Line style depends on user preferences.
169 * $orig_date argument is available only from 1.4.3 and 1.5.1 version.
170 * @param object $orig_from From: header object.
171 * @param integer $orig_date email's timestamp
172 * @return string reply citation
173 */
b0323712 174function getReplyCitation($orig_from, $orig_date) {
12a0ed01 175 global $reply_citation_style, $reply_citation_start, $reply_citation_end;
50706f77 176
05f7db7a 177 if (!is_object($orig_from)) {
d1205176 178 $sOrig_from = '';
05f7db7a 179 } else {
d1205176 180 $sOrig_from = decodeHeader($orig_from->getAddress(false),false,false,true);
05f7db7a 181 }
91c27aee 182
12a0ed01 183 /* First, return an empty string when no citation style selected. */
184 if (($reply_citation_style == '') || ($reply_citation_style == 'none')) {
185 return '';
186 }
187
188 /* Make sure our final value isn't an empty string. */
d1205176 189 if ($sOrig_from == '') {
12a0ed01 190 return '';
191 }
192
193 /* Otherwise, try to select the desired citation style. */
194 switch ($reply_citation_style) {
50706f77 195 case 'author_said':
196 /**
197 * To translators: %s is for author's name
198 */
d1205176 199 $full_reply_citation = sprintf(_("%s wrote:"),$sOrig_from);
50706f77 200 break;
201 case 'quote_who':
a42c236f 202 $start = '<quote who="';
50706f77 203 $end = '">';
d1205176 204 $full_reply_citation = $start . $sOrig_from . $end;
50706f77 205 break;
206 case 'date_time_author':
207 /**
208 * To translators:
91c27aee 209 * first %s is for date string, second %s is for author's name. Date uses
50706f77 210 * formating from "D, F j, Y g:i a" and "D, F j, Y H:i" translations.
211 * Example string:
0e1a248b 212 * "On Sat, December 24, 2004 23:59, Santa wrote:"
50706f77 213 * If you have to put author's name in front of date string, check comments about
214 * argument swapping at http://www.php.net/sprintf
215 */
d1205176 216 $full_reply_citation = sprintf(_("On %s, %s wrote:"), getLongDateString($orig_date), $sOrig_from);
50706f77 217 break;
218 case 'user-defined':
219 $start = $reply_citation_start .
220 ($reply_citation_start == '' ? '' : ' ');
221 $end = $reply_citation_end;
d1205176 222 $full_reply_citation = $start . $sOrig_from . $end;
50706f77 223 break;
224 default:
225 return '';
226 }
227
228 /* Add line feed and return the citation string. */
229 return ($full_reply_citation . "\n");
12a0ed01 230}
231
50706f77 232/**
233 * Creates header fields in forwarded email body
234 *
91c27aee 235 * $default_charset global must be set correctly before you call this function.
50706f77 236 * @param object $orig_header
91c27aee 237 * @return $string
50706f77 238 */
41b94d65 239function getforwardHeader($orig_header) {
50706f77 240 global $editor_size, $default_charset;
241
242 // using own strlen function in order to detect correct string length
243 $display = array( _("Subject") => sq_strlen(_("Subject"),$default_charset),
244 _("From") => sq_strlen(_("From"),$default_charset),
245 _("Date") => sq_strlen(_("Date"),$default_charset),
246 _("To") => sq_strlen(_("To"),$default_charset),
247 _("Cc") => sq_strlen(_("Cc"),$default_charset) );
73ad81bf 248 $maxsize = max($display);
249 $indent = str_pad('',$maxsize+2);
250 foreach($display as $key => $val) {
251 $display[$key] = $key .': '. str_pad('', $maxsize - $val);
252 }
253 $from = decodeHeader($orig_header->getAddr_s('from',"\n$indent"),false,false,true);
254 $from = str_replace('&nbsp;',' ',$from);
255 $to = decodeHeader($orig_header->getAddr_s('to',"\n$indent"),false,false,true);
256 $to = str_replace('&nbsp;',' ',$to);
257 $subject = decodeHeader($orig_header->subject,false,false,true);
258 $subject = str_replace('&nbsp;',' ',$subject);
50706f77 259
260 // using own str_pad function in order to create correct string pad
261 $bodyTop = sq_str_pad(' '._("Original Message").' ',$editor_size -2,'-',STR_PAD_BOTH,$default_charset) .
73ad81bf 262 "\n". $display[_("Subject")] . $subject . "\n" .
263 $display[_("From")] . $from . "\n" .
264 $display[_("Date")] . getLongDateString( $orig_header->date ). "\n" .
265 $display[_("To")] . $to . "\n";
266 if ($orig_header->cc != array() && $orig_header->cc !='') {
267 $cc = decodeHeader($orig_header->getAddr_s('cc',"\n$indent"),false,false,true);
268 $cc = str_replace('&nbsp;',' ',$cc);
269 $bodyTop .= $display[_("Cc")] .$cc . "\n";
270 }
271 $bodyTop .= str_pad('', $editor_size -2 , '-') .
272 "\n\n";
273 return $bodyTop;
41b94d65 274}
09044055 275/* ----------------------------------------------------------------------- */
276
44560457 277/*
1c044820 278 * If the session is expired during a post this restores the compose session
44560457 279 * vars.
280 */
5da08ef7 281if (sqsession_is_registered('session_expired_post')) {
953fa718 282 sqgetGlobalVar('session_expired_post', $session_expired_post, SQ_SESSION);
1c044820 283 /*
40934000 284 * extra check for username so we don't display previous post data from
285 * another user during this session.
286 */
287 if ($session_expired_post['username'] != $username) {
0ec1a14b 288 unset($session_expired_post);
0b97a708 289 sqsession_unregister('session_expired_post');
0ec1a14b 290 session_write_close();
40934000 291 } else {
c6f28eb1 292 // these are the vars that we can set from the expired composed session
293 $compo_var_list = array ( 'send_to', 'send_to_cc','body','startMessage',
294 'passed_body','use_signature','signature','attachments','subject','newmail',
295 'send_to_bcc', 'passed_id', 'mailbox', 'from_htmladdr_search', 'identity',
296 'draft_id', 'delete_draft', 'mailprio', 'edit_as_new', 'compose_messsages',
297 'composesession', 'request_mdn', 'request_dr');
298
299 foreach ($compo_var_list as $var) {
300 if ( isset($session_expired_post[$var]) && !isset($$var) ) {
301 $$var = $session_expired_post[$var];
61e96f7e 302 }
40934000 303 }
c6f28eb1 304
0ec1a14b 305 $compose_messages = unserialize(urldecode($restoremessages));
306 sqsession_register($compose_messages,'compose_messages');
307 sqsession_register($composesession,'composesession');
40934000 308 if (isset($send)) {
309 unset($send);
310 }
311 $session_expired = true;
312 }
5da08ef7 313 unset($session_expired_post);
0b97a708 314 sqsession_unregister('session_expired_post');
5da08ef7 315 session_write_close();
40934000 316 if (!isset($mailbox)) {
317 $mailbox = '';
318 }
319 if ($compose_new_win == '1') {
320 compose_Header($color, $mailbox);
321 } else {
91c27aee 322 $sHeaderJs = (isset($sHeaderJs)) ? $sHeaderJs : '';
323 if (strpos($action, 'reply') !== false && $reply_focus) {
324 $sBodyTagJs = 'onload="checkForm(\''.$replyfocus.'\');"';
325 } else {
326 $sBodyTagJs = 'onload="checkForm();"';
327 }
328 displayPageHeader($color, $mailbox,$sHeaderJs,$sBodyTagJs);
40934000 329 }
330 showInputForm($session, false);
331 exit();
44560457 332}
da95c4b6 333if (!isset($composesession)) {
334 $composesession = 0;
a43e4b90 335 sqsession_register(0,'composesession');
da95c4b6 336}
337
d7f8e6e6 338if (!isset($session) || (isset($newmessage) && $newmessage)) {
0b97a708 339 sqsession_unregister('composesession');
1c044820 340 $session = "$composesession" +1;
91f2085b 341 $composesession = $session;
a43e4b90 342 sqsession_register($composesession,'composesession');
1c044820 343}
a43e4b90 344if (!isset($compose_messages)) {
73ad81bf 345 $compose_messages = array();
a43e4b90 346}
91c27aee 347
40934000 348if (!isset($compose_messages[$session]) || ($compose_messages[$session] == NULL)) {
73ad81bf 349 $composeMessage = new Message();
350 $rfc822_header = new Rfc822Header();
351 $composeMessage->rfc822_header = $rfc822_header;
352 $composeMessage->reply_rfc822_header = '';
353 $compose_messages[$session] = $composeMessage;
91c27aee 354
73ad81bf 355 sqsession_register($compose_messages,'compose_messages');
5628fdde 356} else {
73ad81bf 357 $composeMessage=$compose_messages[$session];
a43e4b90 358}
a43e4b90 359
00793a25 360if (!isset($mailbox) || $mailbox == '' || ($mailbox == 'None')) {
361 $mailbox = 'INBOX';
362}
363
4dfb9db7 364if ($draft) {
365 /*
366 * Set $default_charset to correspond with the user's selection
367 * of language interface.
368 */
369 set_my_charset();
370 $composeMessage=$compose_messages[$session];
b7ff469f 371 if (! deliverMessage($composeMessage, true)) {
da95c4b6 372 showInputForm($session);
00793a25 373 exit();
734f4ee6 374 } else {
5da08ef7 375 unset($compose_messages[$session]);
00793a25 376 $draft_message = _("Draft Email Saved");
377 /* If this is a resumed draft, then delete the original */
378 if(isset($delete_draft)) {
906f7e9f 379 $imap_stream = sqimap_login($username, false, $imapServerAddress, $imapPort, false);
b034bca2 380 sqimap_mailbox_select($imap_stream, $draft_folder);
381 // force bypass_trash=true because message should be saved when deliverMessage() returns true.
91c27aee 382 // in current implementation of sqimap_msgs_list_flag() single message id can
b034bca2 383 // be submitted as string. docs state that it should be array.
384 sqimap_msgs_list_delete($imap_stream, $draft_folder, $delete_draft, true);
385 if ($auto_expunge) {
386 sqimap_mailbox_expunge($imap_stream, $draft_folder, true);
9c3e6cd4 387 }
b034bca2 388 sqimap_logout($imap_stream);
389 }
762853f4 390
391 $oErrorHandler->saveDelayedErrors();
c077ffeb 392 session_write_close();
762853f4 393
b034bca2 394 if ($compose_new_win == '1') {
09047d19 395 if ( !isset($pageheader_sent) || !$pageheader_sent ) {
396 Header("Location: $location/compose.php?saved_draft=yes&session=$composesession");
397 } else {
f265009a 398 echo ' <br><br><div style="text-align: center;"><a href="' . $location
09047d19 399 . '/compose.php?saved_sent=yes&amp;session=' . $composesession . '">'
f265009a 400 . _("Return") . '</a></div>';
a6d3eff6 401 }
b034bca2 402 exit();
403 } else {
09047d19 404 if ( !isset($pageheader_sent) || !$pageheader_sent ) {
405 Header("Location: $location/right_main.php?mailbox=" . urlencode($draft_folder) .
b034bca2 406 "&startMessage=1&note=".urlencode($draft_message));
09047d19 407 } else {
f265009a 408 echo ' <br><br><div style="text-align: center;"><a href="' . $location
09047d19 409 . '/right_main.php?mailbox=' . urlencode($draft_folder)
410 . '&amp;startMessage=1&amp;note=' . urlencode($draft_message) .'">'
f265009a 411 . _("Return") . '</a></div>';
a6d3eff6 412 }
b034bca2 413 exit();
00793a25 414 }
415 }
416}
417
4dfb9db7 418if ($send) {
0b97a708 419 if (isset($_FILES['attachfile']) &&
73ad81bf 420 $_FILES['attachfile']['tmp_name'] &&
421 $_FILES['attachfile']['tmp_name'] != 'none') {
da95c4b6 422 $AttachFailure = saveAttachedFiles($session);
00793a25 423 }
424 if (checkInput(false) && !isset($AttachFailure)) {
73ad81bf 425 if ($mailbox == "All Folders") {
426 /* We entered compose via the search results page */
a42c236f 427 $mailbox = 'INBOX'; /* Send 'em to INBOX, that's safe enough */
73ad81bf 428 }
00793a25 429 $urlMailbox = urlencode (trim($mailbox));
3f6b1b6f 430 if (! isset($passed_id)) {
431 $passed_id = 0;
00793a25 432 }
d4c5c50c 433 /**
00793a25 434 * Set $default_charset to correspond with the user's selection
7058a2a9 435 * of language interface.
00793a25 436 */
437 set_my_charset();
d4c5c50c 438 /**
00793a25 439 * This is to change all newlines to \n
7058a2a9 440 * We'll change them to \r\n later (in the sendMessage function)
00793a25 441 */
442 $body = str_replace("\r\n", "\n", $body);
443 $body = str_replace("\r", "\n", $body);
444
d4c5c50c 445 /**
18c9998a 446 * Rewrap $body so that no line is bigger than $editor_size
00793a25 447 */
18c9998a 448 $body = explode("\n", $body);
449 $newBody = '';
450 foreach ($body as $line) {
451 if( $line <> '-- ' ) {
73ad81bf 452 $line = rtrim($line);
18c9998a 453 }
200c9b8e 454 if (sq_strlen($line,$default_charset) <= $editor_size + 1) {
18c9998a 455 $newBody .= $line . "\n";
456 } else {
200c9b8e 457 sqWordWrap($line, $editor_size,$default_charset);
18c9998a 458 $newBody .= $line . "\n";
459
460 }
461
462 }
463 $body = $newBody;
1c044820 464
a43e4b90 465 $composeMessage=$compose_messages[$session];
d5181a1d 466
a91189d6 467 $Result = deliverMessage($composeMessage);
81de00c0 468
19c4e72d 469 do_hook('compose_send_after', $Result, $composeMessage);
00793a25 470 if (! $Result) {
da95c4b6 471 showInputForm($session);
00793a25 472 exit();
473 }
dd4a44cd 474 unset($compose_messages[$session]);
5c4ff7bf 475
b034bca2 476 /* if it is resumed draft, delete draft message */
00793a25 477 if ( isset($delete_draft)) {
906f7e9f 478 $imap_stream = sqimap_login($username, false, $imapServerAddress, $imapPort, false);
b034bca2 479 sqimap_mailbox_select($imap_stream, $draft_folder);
480 // bypass_trash=true because message should be saved when deliverMessage() returns true.
91c27aee 481 // in current implementation of sqimap_msgs_list_flag() single message id can
b034bca2 482 // be submitted as string. docs state that it should be array.
483 sqimap_msgs_list_delete($imap_stream, $draft_folder, $delete_draft, true);
484 if ($auto_expunge) {
485 sqimap_mailbox_expunge($imap_stream, $draft_folder, true);
486 }
487 sqimap_logout($imap_stream);
00793a25 488 }
81de00c0 489 /*
490 * Store the error array in the session because they will be lost on a redirect
491 */
762853f4 492 $oErrorHandler->saveDelayedErrors();
c077ffeb 493 session_write_close();
762853f4 494
9c3e6cd4 495 if ($compose_new_win == '1') {
09047d19 496 if ( !isset($pageheader_sent) || !$pageheader_sent ) {
497 Header("Location: $location/compose.php?mail_sent=yes");
498 } else {
f265009a 499 echo ' <br><br><div style="text-align: center;"><a href="' . $location
09047d19 500 . '/compose.php?mail_sent=yes">'
f265009a 501 . _("Return") . '</a></div>';
09047d19 502 }
503 exit();
504 } else {
505 if ( !isset($pageheader_sent) || !$pageheader_sent ) {
506 Header("Location: $location/right_main.php?mailbox=$urlMailbox".
507 "&startMessage=$startMessage&mail_sent=yes");
508 } else {
f265009a 509 echo ' <br><br><div style="text-align: center;"><a href="' . $location
09047d19 510 . "/right_main.php?mailbox=$urlMailbox"
511 . "&amp;startMessage=$startMessage&amp;mail_sent=yes\">"
f265009a 512 . _("Return") . '</a></div>';
09047d19 513 }
514 exit();
9c3e6cd4 515 }
734f4ee6 516 } else {
9c3e6cd4 517 if ($compose_new_win == '1') {
518 compose_Header($color, $mailbox);
519 }
520 else {
521 displayPageHeader($color, $mailbox);
522 }
00793a25 523 if (isset($AttachFailure)) {
73ad81bf 524 plain_error_message(_("Could not move/copy file. File not attached"),
525 $color);
00793a25 526 }
00793a25 527 checkInput(true);
da95c4b6 528 showInputForm($session);
00793a25 529 /* sqimap_logout($imapConnection); */
530 }
e02775fe 531} elseif (isset($html_addr_search_done)) {
73ad81bf 532 if ($compose_new_win == '1') {
533 compose_Header($color, $mailbox);
534 }
535 else {
536 displayPageHeader($color, $mailbox);
537 }
00793a25 538
539 if (isset($send_to_search) && is_array($send_to_search)) {
540 foreach ($send_to_search as $k => $v) {
541 if (substr($k, 0, 1) == 'T') {
542 if ($send_to) {
543 $send_to .= ', ';
544 }
545 $send_to .= $v;
546 }
547 elseif (substr($k, 0, 1) == 'C') {
548 if ($send_to_cc) {
549 $send_to_cc .= ', ';
550 }
551 $send_to_cc .= $v;
552 }
553 elseif (substr($k, 0, 1) == 'B') {
554 if ($send_to_bcc) {
555 $send_to_bcc .= ', ';
556 }
557 $send_to_bcc .= $v;
558 }
559 }
560 }
da95c4b6 561 showInputForm($session);
167c6996 562} elseif (isset($html_addr_search) && !isset($html_addr_search_cancel)) {
0b97a708 563 if (isset($_FILES['attachfile']) &&
73ad81bf 564 $_FILES['attachfile']['tmp_name'] &&
565 $_FILES['attachfile']['tmp_name'] != 'none') {
0b97a708 566 if(saveAttachedFiles($session)) {
cb34dbd0 567 plain_error_message(_("Could not move/copy file. File not attached"));
00793a25 568 }
569 }
570 /*
571 * I am using an include so as to elminiate an extra unnecessary
572 * click. If you can think of a better way, please implement it.
573 */
574 include_once('./addrbook_search_html.php');
e02775fe 575} elseif (isset($attach)) {
73ad81bf 576 if ($compose_new_win == '1') {
577 compose_Header($color, $mailbox);
578 } else {
579 displayPageHeader($color, $mailbox);
580 }
5a3e52f3 581 if (saveAttachedFiles($session)) {
cb34dbd0 582 plain_error_message(_("Could not move/copy file. File not attached"));
5a3e52f3 583 }
da95c4b6 584 showInputForm($session);
01265fba 585}
586elseif (isset($sigappend)) {
1e2a6ff6 587 $signature = $idents[$identity]['signature'];
588
01265fba 589 $body .= "\n\n".($prefix_sig==true? "-- \n":'').$signature;
590 if ($compose_new_win == '1') {
73ad81bf 591 compose_Header($color, $mailbox);
01265fba 592 } else {
593 displayPageHeader($color, $mailbox);
594 }
da95c4b6 595 showInputForm($session);
e02775fe 596} elseif (isset($do_delete)) {
73ad81bf 597 if ($compose_new_win == '1') {
598 compose_Header($color, $mailbox);
599 } else {
600 displayPageHeader($color, $mailbox);
601 }
00793a25 602
00793a25 603 if (isset($delete) && is_array($delete)) {
a43e4b90 604 $composeMessage = $compose_messages[$session];
00793a25 605 foreach($delete as $index) {
a58b05b4 606 if (!empty($composeMessage->entities) && isset($composeMessage->entities[$index])) {
c077ffeb 607 $composeMessage->entities[$index]->purgeAttachments();
a58b05b4 608 unset ($composeMessage->entities[$index]);
609 }
a91189d6 610 }
611 $new_entities = array();
612 foreach ($composeMessage->entities as $entity) {
613 $new_entities[] = $entity;
00793a25 614 }
a91189d6 615 $composeMessage->entities = $new_entities;
616 $compose_messages[$session] = $composeMessage;
617 sqsession_register($compose_messages, 'compose_messages');
00793a25 618 }
da95c4b6 619 showInputForm($session);
734f4ee6 620} else {
00793a25 621 /*
622 * This handles the default case as well as the error case
1c044820 623 * (they had the same code) --> if (isset($smtpErrors))
00793a25 624 */
44560457 625
626 if ($compose_new_win == '1') {
73ad81bf 627 compose_Header($color, $mailbox);
44560457 628 } else {
73ad81bf 629 displayPageHeader($color, $mailbox);
44560457 630 }
00793a25 631
632 $newmail = true;
633
a61878d0 634 if (!isset($passed_ent_id)) {
635 $passed_ent_id = '';
636 }
637 if (!isset($passed_id)) {
1c044820 638 $passed_id = '';
a61878d0 639 }
640 if (!isset($mailbox)) {
641 $mailbox = '';
1c044820 642 }
a61878d0 643 if (!isset($action)) {
644 $action = '';
645 }
1c044820 646
44560457 647 $values = newMail($mailbox,$passed_id,$passed_ent_id, $action, $session);
b9928adc 648
649 /* in case the origin is not read_body.php */
650 if (isset($send_to)) {
73ad81bf 651 $values['send_to'] = $send_to;
b9928adc 652 }
653 if (isset($send_to_cc)) {
73ad81bf 654 $values['send_to_cc'] = $send_to_cc;
b9928adc 655 }
656 if (isset($send_to_bcc)) {
73ad81bf 657 $values['send_to_bcc'] = $send_to_bcc;
b9928adc 658 }
2a2f2185 659 if (isset($subject)) {
73ad81bf 660 $values['subject'] = $subject;
2a2f2185 661 }
41b94d65 662 showInputForm($session, $values);
00793a25 663}
664
665exit();
666
00793a25 667/**************** Only function definitions go below *************/
668
92c6f757 669function getforwardSubject($subject)
670{
671 if ((substr(strtolower($subject), 0, 4) != 'fwd:') &&
73ad81bf 672 (substr(strtolower($subject), 0, 5) != '[fwd:') &&
673 (substr(strtolower($subject), 0, 6) != '[ fwd:')) {
92c6f757 674 $subject = '[Fwd: ' . $subject . ']';
675 }
676 return $subject;
677}
00793a25 678
48985d59 679/* This function is used when not sending or adding attachments */
44560457 680function newMail ($mailbox='', $passed_id='', $passed_ent_id='', $action='', $session='') {
1e2a6ff6 681 global $editor_size, $default_use_priority, $body, $idents,
ce68b76b 682 $use_signature, $data_dir, $username,
c6f28eb1 683 $key, $imapServerAddress, $imapPort, $compose_messages,
74f66d27 684 $composeMessage, $body_quote, $request_mdn, $request_dr,
762853f4 685 $default_use_mdn, $mdn_user_support;
4e519821 686 global $languages, $squirrelmail_language, $default_charset;
e7f1a81d 687
d4f20027 688 /*
689 * Set $default_charset to correspond with the user's selection
690 * of language interface. $default_charset global is not correct,
691 * if message is composed in new window.
692 */
693 set_my_charset();
694
91f2085b 695 $send_to = $send_to_cc = $send_to_bcc = $subject = $identity = '';
bdb92db3 696 $mailprio = 3;
44560457 697
41b94d65 698 if ($passed_id) {
906f7e9f 699 $imapConnection = sqimap_login($username, false, $imapServerAddress,
73ad81bf 700 $imapPort, 0);
a61878d0 701
48985d59 702 sqimap_mailbox_select($imapConnection, $mailbox);
41b94d65 703 $message = sqimap_get_message($imapConnection, $passed_id, $mailbox);
1c044820 704
a61878d0 705 $body = '';
706 if ($passed_ent_id) {
707 /* redefine the messsage in case of message/rfc822 */
708 $message = $message->getEntity($passed_ent_id);
709 /* message is an entity which contains the envelope and type0=message
73ad81bf 710 * and type1=rfc822. The actual entities are childs from
711 * $message->entities[0]. That's where the encoding and is located
712 */
a61878d0 713
714 $entities = $message->entities[0]->findDisplayEntity
73ad81bf 715 (array(), $alt_order = array('text/plain'));
a61878d0 716 if (!count($entities)) {
717 $entities = $message->entities[0]->findDisplayEntity
73ad81bf 718 (array(), $alt_order = array('text/plain','html/plain'));
a61878d0 719 }
720 $orig_header = $message->rfc822_header; /* here is the envelope located */
721 /* redefine the message for picking up the attachments */
722 $message = $message->entities[0];
723
724 } else {
725 $entities = $message->findDisplayEntity (array(), $alt_order = array('text/plain'));
726 if (!count($entities)) {
727 $entities = $message->findDisplayEntity (array(), $alt_order = array('text/plain','html/plain'));
728 }
729 $orig_header = $message->rfc822_header;
730 }
1c044820 731
a61878d0 732 $type0 = $message->type0;
733 $type1 = $message->type1;
41b94d65 734 foreach ($entities as $ent) {
b455e47b 735 $msg = $message->getEntity($ent);
736 $type0 = $msg->type0;
737 $type1 = $msg->type1;
a61878d0 738 $unencoded_bodypart = mime_fetch_body($imapConnection, $passed_id, $ent);
739 $body_part_entity = $message->getEntity($ent);
762853f4 740 $bodypart = decodeBody($unencoded_bodypart,
73ad81bf 741 $body_part_entity->header->encoding);
a61878d0 742 if ($type1 == 'html') {
5b755d9f 743 $bodypart = str_replace("\n", ' ', $bodypart);
bb977394 744 $bodypart = preg_replace(array('/<\/?p>/i','/<div><\/div>/i','/<br\s*(\/)*>/i','/<\/?div>/i'), "\n", $bodypart);
5b755d9f 745 $bodypart = str_replace(array('&nbsp;','&gt;','&lt;'),array(' ','>','<'),$bodypart);
a61878d0 746 $bodypart = strip_tags($bodypart);
747 }
e842b215 748 if (isset($languages[$squirrelmail_language]['XTRA_CODE']) &&
73ad81bf 749 function_exists($languages[$squirrelmail_language]['XTRA_CODE'] . '_decode')) {
e842b215 750 if (mb_detect_encoding($bodypart) != 'ASCII') {
f4bb5d22 751 $bodypart = call_user_func($languages[$squirrelmail_language]['XTRA_CODE'] . '_decode', $bodypart);
e842b215 752 }
753 }
eceefdfe 754
bfa54da7 755 // charset encoding in compose form stuff
73ad81bf 756 if (isset($body_part_entity->header->parameters['charset'])) {
757 $actual = $body_part_entity->header->parameters['charset'];
758 } else {
759 $actual = 'us-ascii';
760 }
beca818e 761
73ad81bf 762 if ( $actual && is_conversion_safe($actual) && $actual != $default_charset){
763 $bodypart = charset_convert($actual,$bodypart,$default_charset,false);
764 }
bfa54da7 765 // end of charset encoding in compose
eceefdfe 766
a61878d0 767 $body .= $bodypart;
768 }
769 if ($default_use_priority) {
770 $mailprio = substr($orig_header->priority,0,1);
771 if (!$mailprio) {
772 $mailprio = 3;
773 }
774 } else {
775 $mailprio = '';
776 }
bdb92db3 777
778 $identity = '';
a45887d7 779 $from_o = $orig_header->from;
fe868193 780 if (is_array($from_o)) {
781 if (isset($from_o[0])) {
782 $from_o = $from_o[0];
783 }
784 }
bdb92db3 785 if (is_object($from_o)) {
786 $orig_from = $from_o->getAddress();
787 } else {
788 $orig_from = '';
a61878d0 789 }
1e2a6ff6 790
a91189d6 791 $identities = array();
1e2a6ff6 792 if (count($idents) > 1) {
793 foreach($idents as $nr=>$data) {
794 $enc_from_name = '"'.$data['full_name'].'" <'. $data['email_address'].'>';
795 if($enc_from_name == $orig_from) {
796 $identity = $nr;
a61878d0 797 break;
798 }
a91189d6 799 $identities[] = $enc_from_name;
800 }
1e2a6ff6 801
a91189d6 802 $identity_match = $orig_header->findAddress($identities);
803 if ($identity_match) {
804 $identity = $identity_match;
a61878d0 805 }
bdb92db3 806 }
a61878d0 807
808 switch ($action) {
73ad81bf 809 case ('draft'):
810 $use_signature = FALSE;
811 $composeMessage->rfc822_header = $orig_header;
812 $send_to = decodeHeader($orig_header->getAddr_s('to'),false,false,true);
813 $send_to_cc = decodeHeader($orig_header->getAddr_s('cc'),false,false,true);
814 $send_to_bcc = decodeHeader($orig_header->getAddr_s('bcc'),false,false,true);
815 $send_from = $orig_header->getAddr_s('from');
816 $send_from_parts = new AddressStructure();
817 $send_from_parts = $orig_header->parseAddress($send_from);
818 $send_from_add = $send_from_parts->mailbox . '@' . $send_from_parts->host;
819 $identities = get_identities();
820 if (count($identities) > 0) {
821 foreach($identities as $iddata) {
822 if ($send_from_add == $iddata['email_address']) {
823 $identity = $iddata['index'];
824 break;
825 }
a656569f 826 }
827 }
73ad81bf 828 $subject = decodeHeader($orig_header->subject,false,false,true);
762853f4 829
830 // Remember the receipt settings
831 $request_mdn = $mdn_user_support && !empty($orig_header->dnt) ? '1' : '0';
832 $request_dr = $mdn_user_support && !empty($orig_header->drnt) ? '1' : '0';
833
73ad81bf 834 /* remember the references and in-reply-to headers in case of an reply */
835 $composeMessage->rfc822_header->more_headers['References'] = $orig_header->references;
836 $composeMessage->rfc822_header->more_headers['In-Reply-To'] = $orig_header->in_reply_to;
837 // rewrap the body to clean up quotations and line lengths
838 sqBodyWrap($body, $editor_size);
839 $composeMessage = getAttachments($message, $composeMessage, $passed_id, $entities, $imapConnection);
840 break;
841 case ('edit_as_new'):
842 $send_to = decodeHeader($orig_header->getAddr_s('to'),false,false,true);
843 $send_to_cc = decodeHeader($orig_header->getAddr_s('cc'),false,false,true);
844 $send_to_bcc = decodeHeader($orig_header->getAddr_s('bcc'),false,false,true);
845 $subject = decodeHeader($orig_header->subject,false,false,true);
846 $mailprio = $orig_header->priority;
847 $orig_from = '';
848 $composeMessage = getAttachments($message, $composeMessage, $passed_id, $entities, $imapConnection);
849 // rewrap the body to clean up quotations and line lengths
850 sqBodyWrap($body, $editor_size);
851 break;
852 case ('forward'):
853 $send_to = '';
854 $subject = getforwardSubject(decodeHeader($orig_header->subject,false,false,true));
855 $body = getforwardHeader($orig_header) . $body;
856 // the logic for calling sqUnWordWrap here would be to allow the browser to wrap the lines
857 // forwarded message text should be as undisturbed as possible, so commenting out this call
858 // sqUnWordWrap($body);
859 $composeMessage = getAttachments($message, $composeMessage, $passed_id, $entities, $imapConnection);
91c27aee 860
73ad81bf 861 //add a blank line after the forward headers
862 $body = "\n" . $body;
863 break;
864 case ('forward_as_attachment'):
865 $subject = getforwardSubject(decodeHeader($orig_header->subject,false,false,true));
866 $composeMessage = getMessage_RFC822_Attachment($message, $composeMessage, $passed_id, $passed_ent_id, $imapConnection);
867 $body = '';
868 break;
869 case ('reply_all'):
870 if(isset($orig_header->mail_followup_to) && $orig_header->mail_followup_to) {
871 $send_to = $orig_header->getAddr_s('mail_followup_to');
b268e66b 872 } else {
73ad81bf 873 $send_to_cc = replyAllString($orig_header);
874 $send_to_cc = decodeHeader($send_to_cc,false,false,true);
b268e66b 875 }
73ad81bf 876 case ('reply'):
877 // skip this if send_to was already set right above here
878 if(!$send_to) {
879 $send_to = $orig_header->reply_to;
880 if (is_array($send_to) && count($send_to)) {
881 $send_to = $orig_header->getAddr_s('reply_to');
882 } else if (is_object($send_to)) { /* unneccesarry, just for failsafe purpose */
883 $send_to = $orig_header->getAddr_s('reply_to');
884 } else {
885 $send_to = $orig_header->getAddr_s('from');
886 }
dd4a44cd 887 }
73ad81bf 888 $send_to = decodeHeader($send_to,false,false,true);
889 $subject = decodeHeader($orig_header->subject,false,false,true);
890 $subject = str_replace('"', "'", $subject);
891 $subject = trim($subject);
892 if (substr(strtolower($subject), 0, 3) != 're:') {
893 $subject = 'Re: ' . $subject;
894 }
895 /* this corrects some wrapping/quoting problems on replies */
896 $rewrap_body = explode("\n", $body);
897 $from = (is_array($orig_header->from)) ? $orig_header->from[0] : $orig_header->from;
898 $body = '';
899 $strip_sigs = getPref($data_dir, $username, 'strip_sigs');
900 foreach ($rewrap_body as $line) {
901 if ($strip_sigs && substr($line,0,3) == '-- ') {
902 break;
903 }
904 if (preg_match("/^(>+)/", $line, $matches)) {
905 $gt = $matches[1];
906 $body .= $body_quote . str_replace("\n", "\n$body_quote$gt ", rtrim($line)) ."\n";
907 } else {
908 $body .= $body_quote . (!empty($body_quote) ? ' ' : '') . str_replace("\n", "\n$body_quote" . (!empty($body_quote) ? ' ' : ''), rtrim($line)) . "\n";
909 }
a61878d0 910 }
c9d61baf 911
73ad81bf 912 //rewrap the body to clean up quotations and line lengths
913 $body = sqBodyWrap ($body, $editor_size);
c9d61baf 914
73ad81bf 915 $body = getReplyCitation($from , $orig_header->date) . $body;
916 $composeMessage->reply_rfc822_header = $orig_header;
12a0ed01 917
73ad81bf 918 break;
919 default:
920 break;
41b94d65 921 }
a91189d6 922 $compose_messages[$session] = $composeMessage;
923 sqsession_register($compose_messages, 'compose_messages');
5da08ef7 924 session_write_close();
a61878d0 925 sqimap_logout($imapConnection);
41b94d65 926 }
a61878d0 927 $ret = array( 'send_to' => $send_to,
73ad81bf 928 'send_to_cc' => $send_to_cc,
929 'send_to_bcc' => $send_to_bcc,
930 'subject' => $subject,
931 'mailprio' => $mailprio,
932 'body' => $body,
933 'identity' => $identity );
a61878d0 934
41b94d65 935 return ($ret);
48985d59 936} /* function newMail() */
937
50706f77 938/**
939 * downloads attachments from original message, stores them in attachment directory and adds
940 * them to composed message.
941 * @param object $message
942 * @param object $composeMessage
943 * @param integer $passed_id
944 * @param mixed $entities
945 * @param mixed $imapConnection
91c27aee 946 * @return object
50706f77 947 */
a43e4b90 948function getAttachments($message, &$composeMessage, $passed_id, $entities, $imapConnection) {
8df4c929 949 global $attachment_dir, $username, $data_dir, $squirrelmail_language, $languages;
48985d59 950 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
1c044820 951 if (!count($message->entities) ||
73ad81bf 952 ($message->type0 == 'message' && $message->type1 == 'rfc822')) {
41b94d65 953 if ( !in_array($message->entity_id, $entities) && $message->entity_id) {
73ad81bf 954 switch ($message->type0) {
955 case 'message':
956 if ($message->type1 == 'rfc822') {
957 $filename = $message->rfc822_header->subject;
958 if ($filename == "") {
959 $filename = "untitled-".$message->entity_id;
960 }
961 $filename .= '.msg';
962 } else {
963 $filename = $message->getFilename();
181538ac 964 }
73ad81bf 965 break;
966 default:
967 if (!$message->mime_header) { /* temporary hack */
968 $message->mime_header = $message->header;
969 }
970 $filename = $message->getFilename();
971 break;
972 }
973 $filename = str_replace('&#32;', ' ', decodeHeader($filename));
974 if (isset($languages[$squirrelmail_language]['XTRA_CODE']) &&
975 function_exists($languages[$squirrelmail_language]['XTRA_CODE'] . '_encode')) {
f4bb5d22 976 $filename = call_user_func($languages[$squirrelmail_language]['XTRA_CODE'] . '_encode', $filename);
73ad81bf 977 }
978 $localfilename = GenerateRandomString(32, '', 7);
979 $full_localfilename = "$hashed_attachment_dir/$localfilename";
980 while (file_exists($full_localfilename)) {
981 $localfilename = GenerateRandomString(32, '', 7);
982 $full_localfilename = "$hashed_attachment_dir/$localfilename";
983 }
984 $message->att_local_name = $full_localfilename;
985
986 $composeMessage->initAttachment($message->type0.'/'.$message->type1,$filename,
987 $full_localfilename);
988
989 /* Write Attachment to file */
990 $fp = fopen ("$hashed_attachment_dir/$localfilename", 'wb');
91c27aee 991 mime_print_body_lines ($imapConnection, $passed_id, $message->entity_id, $message->header->encoding, $fp);
73ad81bf 992 fclose ($fp);
48985d59 993 }
734f4ee6 994 } else {
a43e4b90 995 for ($i=0, $entCount=count($message->entities); $i<$entCount;$i++) {
996 $composeMessage=getAttachments($message->entities[$i], $composeMessage, $passed_id, $entities, $imapConnection);
48985d59 997 }
998 }
a43e4b90 999 return $composeMessage;
48985d59 1000}
1001
1c044820 1002function getMessage_RFC822_Attachment($message, $composeMessage, $passed_id,
73ad81bf 1003 $passed_ent_id='', $imapConnection) {
ce68b76b 1004 global $attachment_dir, $username, $data_dir;
a6ec592e 1005 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
756406df 1006 if (!$passed_ent_id) {
1c044820 1007 $body_a = sqimap_run_command($imapConnection,
73ad81bf 1008 'FETCH '.$passed_id.' RFC822',
1009 TRUE, $response, $readmessage,
1010 TRUE);
756406df 1011 } else {
1c044820 1012 $body_a = sqimap_run_command($imapConnection,
73ad81bf 1013 'FETCH '.$passed_id.' BODY['.$passed_ent_id.']',
1014 TRUE, $response, $readmessage, TRUE);
a61878d0 1015 $message = $message->parent;
756406df 1016 }
d0519c03 1017 if ($response == 'OK') {
a61878d0 1018 $subject = encodeHeader($message->rfc822_header->subject);
1019 array_shift($body_a);
1c044820 1020 array_pop($body_a);
a61878d0 1021 $body = implode('', $body_a) . "\r\n";
1c044820 1022
a61878d0 1023 $localfilename = GenerateRandomString(32, 'FILE', 7);
1024 $full_localfilename = "$hashed_attachment_dir/$localfilename";
1c044820 1025
1026 $fp = fopen($full_localfilename, 'w');
a61878d0 1027 fwrite ($fp, $body);
1028 fclose($fp);
5a1f1da3 1029 $composeMessage->initAttachment('message/rfc822',$subject.'.msg',
73ad81bf 1030 $full_localfilename);
a43e4b90 1031 }
1032 return $composeMessage;
a6ec592e 1033}
1034
41b94d65 1035function showInputForm ($session, $values=false) {
ce68b76b 1036 global $send_to, $send_to_cc, $body, $startMessage, $action,
1037 $color, $use_signature, $signature, $prefix_sig,
8d8da447 1038 $editor_size, $editor_height, $subject, $newmail,
73ad81bf 1039 $use_javascript_addr_book, $send_to_bcc, $passed_id, $mailbox,
1040 $from_htmladdr_search, $location_of_buttons, $attachment_dir,
ce68b76b 1041 $username, $data_dir, $identity, $idents, $delete_draft,
1042 $mailprio, $compose_new_win, $saved_draft, $mail_sent, $sig_first,
c6f28eb1 1043 $compose_messages, $composesession, $default_charset,
762853f4 1044 $compose_onsubmit, $oTemplate, $oErrorHandler;
a43e4b90 1045
87745b9c 1046 if (checkForJavascript()) {
1047 $onfocus = ' onfocus="alreadyFocused=true;"';
1048 $onfocus_array = array('onfocus' => 'alreadyFocused=true;');
1049 }
1050 else {
1051 $onfocus = '';
1052 $onfocus_array = array();
1053 }
1054
a43e4b90 1055 $composeMessage = $compose_messages[$session];
41b94d65 1056 if ($values) {
73ad81bf 1057 $send_to = $values['send_to'];
1058 $send_to_cc = $values['send_to_cc'];
1059 $send_to_bcc = $values['send_to_bcc'];
1060 $subject = $values['subject'];
1061 $mailprio = $values['mailprio'];
1062 $body = $values['body'];
1063 $identity = (int) $values['identity'];
676bb189 1064 } else {
73ad81bf 1065 $send_to = decodeHeader($send_to, true, false);
1066 $send_to_cc = decodeHeader($send_to_cc, true, false);
1067 $send_to_bcc = decodeHeader($send_to_bcc, true, false);
41b94d65 1068 }
1c044820 1069
48985d59 1070 if ($use_javascript_addr_book) {
2c92ea9d 1071 echo "\n". '<script type="text/javascript">'."\n<!--\n" .
73ad81bf 1072 'function open_abook() { ' . "\n" .
1073 ' var nwin = window.open("addrbook_popup.php","abookpopup",' .
1074 '"width=670,height=300,resizable=yes,scrollbars=yes");' . "\n" .
1075 ' if((!nwin.opener) && (document.windows != null))' . "\n" .
1076 ' nwin.opener = document.windows;' . "\n" .
1077 "}\n" .
1078 "// -->\n</script>\n\n";
48985d59 1079 }
1080
4a1788b3 1081 echo "\n" . '<form name="compose" action="compose.php" method="post" ' .
73ad81bf 1082 'enctype="multipart/form-data"';
a34b07a5 1083
1084 $compose_onsubmit = array();
4a1788b3 1085 do_hook('compose_form');
1c044820 1086
a34b07a5 1087 // Plugins that use compose_form hook can add an array entry
1088 // to the globally scoped $compose_onsubmit; we add them up
5c4ff7bf 1089 // here and format the form tag's full onsubmit handler.
1090 // Each plugin should use "return false" if they need to
a34b07a5 1091 // stop form submission but otherwise should NOT use "return
1092 // true" to give other plugins the chance to do what they need
1093 // to do; SquirrelMail itself will add the final "return true".
1094 // Onsubmit text is enclosed inside of double quotes, so plugins
1095 // need to quote accordingly.
1096 if (checkForJavascript()) {
1097 $onsubmit_text = ' onsubmit="';
5c4ff7bf 1098 if (empty($compose_onsubmit))
a34b07a5 1099 $compose_onsubmit = array();
5c4ff7bf 1100 else if (!is_array($compose_onsubmit))
a34b07a5 1101 $compose_onsubmit = array($compose_onsubmit);
1102
1103 foreach ($compose_onsubmit as $text) {
1104 $text = trim($text);
5c4ff7bf 1105 if (substr($text, -1) != ';' && substr($text, -1) != '}')
a34b07a5 1106 $text .= '; ';
1107 $onsubmit_text .= $text;
1108 }
1109
1110 echo $onsubmit_text . ' return true;"';
1111 }
5c4ff7bf 1112
a34b07a5 1113
48985d59 1114 echo ">\n";
1115
df96b37a 1116 echo addHidden('startMessage', $startMessage);
4a1788b3 1117
41b94d65 1118 if ($action == 'draft') {
df96b37a 1119 echo addHidden('delete_draft', $passed_id);
48985d59 1120 }
1121 if (isset($delete_draft)) {
df96b37a 1122 echo addHidden('delete_draft', $delete_draft);
48985d59 1123 }
da95c4b6 1124 if (isset($session)) {
df96b37a 1125 echo addHidden('session', $session);
da95c4b6 1126 }
1c044820 1127
08bad2b1 1128 if (isset($passed_id)) {
df96b37a 1129 echo addHidden('passed_id', $passed_id);
08bad2b1 1130 }
44560457 1131
9c3e6cd4 1132 if ($saved_draft == 'yes') {
762853f4 1133 $oTemplate->assign('note', _("Draft Saved"));
1134 $oTemplate->display('note.tpl');
9c3e6cd4 1135 }
1136 if ($mail_sent == 'yes') {
762853f4 1137 $oTemplate->assign('note', _("Your message has been sent."));
1138 $oTemplate->display('note.tpl');
9c3e6cd4 1139 }
9c3e6cd4 1140 if ($compose_new_win == '1') {
762853f4 1141 $oTemplate->display('compose_newwin_close.tpl');
9c3e6cd4 1142 }
762853f4 1143
78a35fcd 1144 if ($location_of_buttons == 'top') {
1145 showComposeButtonRow();
1146 }
48985d59 1147
762853f4 1148 $identities = array();
1e2a6ff6 1149 if (count($idents) > 1) {
762853f4 1150 reset($idents);
73ad81bf 1151 foreach($idents as $id => $data) {
762853f4 1152 $identities[$id] = $data['full_name'].' &lt;'.$data['email_address'].'&gt;';
73ad81bf 1153 }
762853f4 1154 }
1155
1156 $oTemplate->assign('identities', $identities);
1157 $oTemplate->assign('identity_def', $identity);
1158 $oTemplate->assign('input_onfocus', 'onfocus="'.join(' ', $onfocus_array).'"');
1159
1160 $oTemplate->assign('to', htmlspecialchars($send_to));
1161 $oTemplate->assign('cc', htmlspecialchars($send_to_cc));
1162 $oTemplate->assign('bcc', htmlspecialchars($send_to_bcc));
1163 $oTemplate->assign('subject', htmlspecialchars($subject));
1164
1165 $oTemplate->display('compose_header.tpl');
48985d59 1166
78a35fcd 1167 if ($location_of_buttons == 'between') {
1168 showComposeButtonRow();
1169 }
4dfb9db7 1170
762853f4 1171 $body_str = '';
48985d59 1172 if ($use_signature == true && $newmail == true && !isset($from_htmladdr_search)) {
1e2a6ff6 1173 $signature = $idents[$identity]['signature'];
d3c13a51 1174
3b17e952 1175 if ($sig_first == '1') {
50706f77 1176 /*
1177 * FIXME: test is specific to ja_JP translation implementation.
1178 * This test might apply incorrect conversion to other translations, but
91c27aee 1179 * use of 7bit iso-2022-jp charset in other translations might have other
50706f77 1180 * issues too.
1181 */
ab4700c3 1182 if ($default_charset == 'iso-2022-jp') {
762853f4 1183 $body_str = "\n\n".($prefix_sig==true? "-- \n":'').mb_convert_encoding($signature, 'EUC-JP');
83be314a 1184 } else {
762853f4 1185 $body_str = "\n\n".($prefix_sig==true? "-- \n":'').decodeHeader($signature,false,false);
83be314a 1186 }
762853f4 1187 $body_str .= "\n\n".htmlspecialchars(decodeHeader($body,false,false));
1188 } else {
1189 $body_str = "\n\n".htmlspecialchars(decodeHeader($body,false,false));
50706f77 1190 // FIXME: test is specific to ja_JP translation implementation. See above comments.
ab4700c3 1191 if ($default_charset == 'iso-2022-jp') {
762853f4 1192 $body_str .= "\n\n".($prefix_sig==true? "-- \n":'').mb_convert_encoding($signature, 'EUC-JP');
1193 } else {
1194 $body_str .= "\n\n".($prefix_sig==true? "-- \n":'').decodeHeader($signature,false,false);
73ad81bf 1195 }
3b17e952 1196 }
73ad81bf 1197 } else {
762853f4 1198 $body_str = htmlspecialchars(decodeHeader($body,false,false));
48985d59 1199 }
12a0ed01 1200
762853f4 1201 $oTemplate->assign('editor_width', (int)$editor_size);
1202 $oTemplate->assign('editor_height', (int)$editor_height);
1203 $oTemplate->assign('input_onfocus', 'onfocus="'.join(' ', $onfocus_array).'"');
1204 $oTemplate->assign('body', $body_str);
1205 $oTemplate->assign('show_bottom_send', $location_of_buttons!='bottom');
1206
1207 $oTemplate->display ('compose_body.tpl');
1208
48985d59 1209 if ($location_of_buttons == 'bottom') {
1210 showComposeButtonRow();
48985d59 1211 }
46bb8da8 1212
48985d59 1213 /* This code is for attachments */
73ad81bf 1214 if ((bool) ini_get('file_uploads')) {
1215
1216 /* Calculate the max size for an uploaded file.
1217 * This is advisory for the user because we can't actually prevent
1218 * people to upload too large files. */
1219 $sizes = array();
1220 /* php.ini vars which influence the max for uploads */
1221 $configvars = array('post_max_size', 'memory_limit', 'upload_max_filesize');
1222 foreach($configvars as $var) {
4f21ba00 1223 /* skip 0 or empty values, and -1 which means 'unlimited' */
73ad81bf 1224 if( $size = getByteSize(ini_get($var)) ) {
4f21ba00 1225 if ( $size != '-1' ) {
1226 $sizes[] = $size;
1227 }
73ad81bf 1228 }
0a2c3218 1229 }
0a2c3218 1230
762853f4 1231 $attach = array();
73ad81bf 1232 if ($composeMessage->entities) {
1233 foreach ($composeMessage->entities as $key => $attachment) {
1234 $attached_file = $attachment->att_local_name;
1235 if ($attachment->att_local_name || $attachment->body_part) {
1236 $attached_filename = decodeHeader($attachment->mime_header->getParameter('name'));
1237 $type = $attachment->mime_header->type0.'/'.
a91189d6 1238 $attachment->mime_header->type1;
762853f4 1239
1240 $a = array();
1241 $a['Key'] = $key;
1242 $a['FileName'] = $attached_filename;
1243 $a['ContentType'] = $type;
1244 $a['Size'] = filesize($attached_file);
1245 $attach[$key] = $a;
73ad81bf 1246 }
1247 }
4dfb9db7 1248 }
762853f4 1249
1250 $max = min($sizes);
1251 $oTemplate->assign('max_file_size', empty($max) ? -1 : $max);
1252 $oTemplate->assign('attachments', $attach);
1253
1254 $oTemplate->display('compose_attachments.tpl');
73ad81bf 1255 } // End of file_uploads if-block
41b94d65 1256 /* End of attachment code */
762853f4 1257
1258 echo addHidden('username', $username).
1259 addHidden('smaction', $action).
1260 addHidden('mailbox', $mailbox);
1c044820 1261 /*
1262 store the complete ComposeMessages array in a hidden input value
0ec1a14b 1263 so we can restore them in case of a session timeout.
73ad81bf 1264 */
953fa718 1265 sqgetGlobalVar('QUERY_STRING', $queryString, SQ_SERVER);
df96b37a 1266 echo addHidden('restoremessages', serialize($compose_messages)).
73ad81bf 1267 addHidden('composesession', $composesession).
1268 addHidden('querystring', $queryString).
1269 "</form>\n";
a64f47e7 1270 if (!(bool) ini_get('file_uploads')) {
73ad81bf 1271 /* File uploads are off, so we didn't show that part of the form.
1272 To avoid bogus bug reports, tell the user why. */
50706f77 1273 echo '<p style="text-align:center">'
1274 . _("Because PHP file uploads are turned off, you can not attach files to this message. Please see your system administrator for details.")
1275 . "</p>\r\n";
a64f47e7 1276 }
1277
9f599fe3 1278 do_hook('compose_bottom');
762853f4 1279
1280 if ($compose_new_win=='1') {
1281 $oTemplate->display('compose_newwin_close.tpl');
1282 }
1283
1284 $oErrorHandler->setDelayedErrors(false);
5c4ff7bf 1285 $oTemplate->display('footer.tpl');
48985d59 1286}
1287
1288
70c4fd84 1289function showComposeButtonRow() {
78a35fcd 1290 global $use_javascript_addr_book, $save_as_draft,
73ad81bf 1291 $default_use_priority, $mailprio, $default_use_mdn,
1292 $request_mdn, $request_dr,
1293 $data_dir, $username;
70c4fd84 1294
762853f4 1295 global $oTemplate, $buffer_hook;
1296
1297 if ($default_use_priority) {
1298 $priorities = array('1'=>_("High"), '3'=>_("Normal"), '5'=>_("Low"));
1299 $priority = isset($mailprio) ? $mailprio : 3;
1300 } else {
1301 $priorities = array();
1302 $priority = NULL;
ae25968c 1303 }
762853f4 1304
ae25968c 1305 $mdn_user_support=getPref($data_dir, $username, 'mdn_user_support',$default_use_mdn);
48985d59 1306
78a35fcd 1307 if ($use_javascript_addr_book) {
762853f4 1308 $addr_book = " <script type=\"text/javascript\"><!--\n document.write(\"".
73ad81bf 1309 " <input type=button value=\\\""._("Addresses").
1310 "\\\" onclick=\\\"javascript:open_abook();\\\" />\");".
1311 " // --></script><noscript>\n".
1312 ' <input type="submit" name="html_addr_search" value="'.
1313 _("Addresses").'" />'.
1314 " </noscript>\n";
734f4ee6 1315 } else {
762853f4 1316 $addr_book = ' <input type="submit" name="html_addr_search" value="'.
73ad81bf 1317 _("Addresses").'" />' . "\n";
78a35fcd 1318 }
48985d59 1319
762853f4 1320 $oTemplate->assign('allow_priority', $default_use_priority==1);
1321 $oTemplate->assign('priority_list', $priorities);
1322 $oTemplate->assign('current_priority', $priority);
1323
1324 $oTemplate->assign('notifications_enabled', $mdn_user_support==1);
1325 $oTemplate->assign('read_receipt', $request_mdn=='1');
1326 $oTemplate->assign('delivery_receipt', $request_dr=='1');
1327
1328 $oTemplate->assign('drafts_enabled', $save_as_draft);
1329 $oTemplate->assign('address_book_button', $addr_book);
441f2d33 1330
762853f4 1331 $oTemplate->display('compose_buttons.tpl');
78a35fcd 1332}
b278172f 1333
70c4fd84 1334function checkInput ($show) {
78a35fcd 1335 /*
1336 * I implemented the $show variable because the error messages
1337 * were getting sent before the page header. So, I check once
1338 * using $show=false, and then when i'm ready to display the error
1339 * message, show=true
1340 */
c6f28eb1 1341 global $send_to, $send_to_bcc;
78a35fcd 1342
6bf2a88f 1343 if ($send_to == '' && $send_to_bcc == '') {
78a35fcd 1344 if ($show) {
cb34dbd0 1345 plain_error_message(_("You have not filled in the \"To:\" field."));
78a35fcd 1346 }
1347 return false;
1348 }
1349 return true;
1350} /* function checkInput() */
df15de21 1351
3806fa52 1352
00793a25 1353/* True if FAILURE */
da95c4b6 1354function saveAttachedFiles($session) {
c077ffeb 1355 global $_FILES, $attachment_dir, $username,
73ad81bf 1356 $data_dir, $compose_messages;
bfa54da7 1357
45cdd1b5 1358 /* get out of here if no file was attached at all */
1359 if (! is_uploaded_file($_FILES['attachfile']['tmp_name']) ) {
1360 return true;
1361 }
1362
4c9d2242 1363 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
1364 $localfilename = GenerateRandomString(32, '', 7);
1365 $full_localfilename = "$hashed_attachment_dir/$localfilename";
1366 while (file_exists($full_localfilename)) {
1367 $localfilename = GenerateRandomString(32, '', 7);
1368 $full_localfilename = "$hashed_attachment_dir/$localfilename";
1369 }
1370
a42c236f 1371 // m_u_f works better with restricted PHP installs (safe_mode, open_basedir),
1372 // if that doesn't work, try a simple rename.
1373 if (!@move_uploaded_file($_FILES['attachfile']['tmp_name'],$full_localfilename)) {
1374 if (!@rename($_FILES['attachfile']['tmp_name'], $full_localfilename)) {
73ad81bf 1375 return true;
1376 }
a61878d0 1377 }
a43e4b90 1378 $message = $compose_messages[$session];
0b97a708 1379 $type = strtolower($_FILES['attachfile']['type']);
1380 $name = $_FILES['attachfile']['name'];
a43e4b90 1381 $message->initAttachment($type, $name, $full_localfilename);
1382 $compose_messages[$session] = $message;
b0314f04 1383 sqsession_register($compose_messages , 'compose_messages');
4c9d2242 1384}
1385
0a2c3218 1386/* parse values like 8M and 2k into bytes */
1387function getByteSize($ini_size) {
1388
4d30dc83 1389 if(!$ini_size) {
1390 return FALSE;
1391 }
da95c4b6 1392
0a2c3218 1393 $ini_size = trim($ini_size);
1394
5b9716de 1395 // if there's some kind of letter at the end of the string we need to multiply.
1396 if(!is_numeric(substr($ini_size, -1))) {
1397
1398 switch(strtoupper(substr($ini_size, -1))) {
1399 case 'G':
73ad81bf 1400 $bytesize = 1073741824;
1401 break;
5b9716de 1402 case 'M':
73ad81bf 1403 $bytesize = 1048576;
1404 break;
5b9716de 1405 case 'K':
73ad81bf 1406 $bytesize = 1024;
1407 break;
5b9716de 1408 }
1409
4d30dc83 1410 return ($bytesize * (int)substr($ini_size, 0, -1));
0a2c3218 1411 }
1c044820 1412
4d30dc83 1413 return $ini_size;
0a2c3218 1414}
a43e4b90 1415
4c9d2242 1416
50706f77 1417/**
1418 * temporary function to make use of the deliver class.
a42c236f 1419 * In the future the responsible backend should be automaticly loaded
50706f77 1420 * and conf.pl should show a list of available backends.
1421 * The message also should be constructed by the message class.
73ad81bf 1422 */
b7ff469f 1423function deliverMessage($composeMessage, $draft=false) {
a43e4b90 1424 global $send_to, $send_to_cc, $send_to_bcc, $mailprio, $subject, $body,
73ad81bf 1425 $username, $popuser, $usernamedata, $identity, $idents, $data_dir,
1426 $request_mdn, $request_dr, $default_charset, $color, $useSendmail,
1427 $domain, $action, $default_move_to_sent, $move_to_sent;
a43e4b90 1428 global $imapServerAddress, $imapPort, $sent_folder, $key;
1429
1430 $rfc822_header = $composeMessage->rfc822_header;
24192f77 1431
1432 $abook = addressbook_init(false, true);
310dfeb6 1433 $rfc822_header->to = $rfc822_header->parseAddress($send_to,true, array(), '', $domain, array(&$abook,'lookup'));
1434 $rfc822_header->cc = $rfc822_header->parseAddress($send_to_cc,true,array(), '',$domain, array(&$abook,'lookup'));
1435 $rfc822_header->bcc = $rfc822_header->parseAddress($send_to_bcc,true, array(), '',$domain, array(&$abook,'lookup'));
a43e4b90 1436 $rfc822_header->priority = $mailprio;
1437 $rfc822_header->subject = $subject;
310dfeb6 1438
a43e4b90 1439 $special_encoding='';
1440 if (strtolower($default_charset) == 'iso-2022-jp') {
1441 if (mb_detect_encoding($body) == 'ASCII') {
a91189d6 1442 $special_encoding = '8bit';
a43e4b90 1443 } else {
1444 $body = mb_convert_encoding($body, 'JIS');
1445 $special_encoding = '7bit';
1446 }
1447 }
1448 $composeMessage->setBody($body);
1449
1450 if (ereg("^([^@%/]+)[@%/](.+)$", $username, $usernamedata)) {
73ad81bf 1451 $popuser = $usernamedata[1];
1452 $domain = $usernamedata[2];
1453 unset($usernamedata);
a43e4b90 1454 } else {
73ad81bf 1455 $popuser = $username;
a43e4b90 1456 }
1457 $reply_to = '';
1e2a6ff6 1458 $from_mail = $idents[$identity]['email_address'];
1459 $full_name = $idents[$identity]['full_name'];
1460 $reply_to = $idents[$identity]['reply_to'];
9ca455db 1461 if (!$from_mail) {
73ad81bf 1462 $from_mail = "$popuser@$domain";
045714fd 1463 }
1464 $rfc822_header->from = $rfc822_header->parseAddress($from_mail,true);
1465 if ($full_name) {
9783f396 1466 $from = $rfc822_header->from[0];
a91189d6 1467 if (!$from->host) $from->host = $domain;
12a0ed01 1468 $full_name_encoded = encodeHeader($full_name);
1469 if ($full_name_encoded != $full_name) {
1470 $from_addr = $full_name_encoded .' <'.$from->mailbox.'@'.$from->host.'>';
1471 } else {
1472 $from_addr = '"'.$full_name .'" <'.$from->mailbox.'@'.$from->host.'>';
1473 }
045714fd 1474 $rfc822_header->from = $rfc822_header->parseAddress($from_addr,true);
a43e4b90 1475 }
a43e4b90 1476 if ($reply_to) {
73ad81bf 1477 $rfc822_header->reply_to = $rfc822_header->parseAddress($reply_to,true);
a43e4b90 1478 }
1479 /* Receipt: On Read */
1480 if (isset($request_mdn) && $request_mdn) {
73ad81bf 1481 $rfc822_header->dnt = $rfc822_header->parseAddress($from_mail,true);
762853f4 1482 } elseif (isset($rfc822_header->dnt)) {
1483 unset($rfc822_header->dnt);
a43e4b90 1484 }
762853f4 1485
a43e4b90 1486 /* Receipt: On Delivery */
1487 if (isset($request_dr) && $request_dr) {
73ad81bf 1488 $rfc822_header->more_headers['Return-Receipt-To'] = $from_mail;
762853f4 1489 } elseif (isset($rfc822_header->more_headers['Return-Receipt-To'])) {
1490 unset($rfc822_header->more_headers['Return-Receipt-To']);
a43e4b90 1491 }
762853f4 1492
a43e4b90 1493 /* multipart messages */
1494 if (count($composeMessage->entities)) {
1495 $message_body = new Message();
a91189d6 1496 $message_body->body_part = $composeMessage->body_part;
1497 $composeMessage->body_part = '';
1498 $mime_header = new MessageHeader;
1499 $mime_header->type0 = 'text';
1500 $mime_header->type1 = 'plain';
1501 if ($special_encoding) {
1502 $mime_header->encoding = $special_encoding;
1c044820 1503 } else {
12a0ed01 1504 $mime_header->encoding = '8bit';
a91189d6 1505 }
1506 if ($default_charset) {
1507 $mime_header->parameters['charset'] = $default_charset;
1508 }
1c044820 1509 $message_body->mime_header = $mime_header;
a43e4b90 1510 array_unshift($composeMessage->entities, $message_body);
a91189d6 1511 $content_type = new ContentType('multipart/mixed');
a43e4b90 1512 } else {
1e2026df 1513 $content_type = new ContentType('text/plain');
1514 if ($special_encoding) {
1515 $rfc822_header->encoding = $special_encoding;
1c044820 1516 } else {
1e2026df 1517 $rfc822_header->encoding = '8bit';
1c044820 1518 }
426e0b72 1519 if ($default_charset) {
1520 $content_type->properties['charset']=$default_charset;
73ad81bf 1521 }
181538ac 1522 }
1c044820 1523
a43e4b90 1524 $rfc822_header->content_type = $content_type;
1525 $composeMessage->rfc822_header = $rfc822_header;
762853f4 1526
1c044820 1527 /* Here you can modify the message structure just before we hand
5618924b 1528 it over to deliver */
5255585d 1529 $hookReturn = do_hook('compose_send', $composeMessage);
1530 /* Get any changes made by plugins to $composeMessage. */
1531 if ( is_object($hookReturn[1]) ) {
1532 $composeMessage = $hookReturn[1];
1533 }
a43e4b90 1534
b48d3c53 1535 if (!$useSendmail && !$draft) {
a91189d6 1536 require_once(SM_PATH . 'class/deliver/Deliver_SMTP.class.php');
1537 $deliver = new Deliver_SMTP();
ce68b76b 1538 global $smtpServerAddress, $smtpPort, $pop_before_smtp;
a91189d6 1539
a91189d6 1540 $authPop = (isset($pop_before_smtp) && $pop_before_smtp) ? true : false;
9bd3b1e6 1541 get_smtp_user($user, $pass);
a91189d6 1542 $stream = $deliver->initStream($composeMessage,$domain,0,
73ad81bf 1543 $smtpServerAddress, $smtpPort, $user, $pass, $authPop);
b48d3c53 1544 } elseif (!$draft) {
73ad81bf 1545 require_once(SM_PATH . 'class/deliver/Deliver_SendMail.class.php');
fd7ab795 1546 global $sendmail_path, $sendmail_args;
f3dc9c62 1547 // Check for outdated configuration
1548 if (!isset($sendmail_args)) {
1549 if ($sendmail_path=='/var/qmail/bin/qmail-inject') {
1550 $sendmail_args = '';
1551 } else {
1552 $sendmail_args = '-i -t';
1553 }
1554 }
fd7ab795 1555 $deliver = new Deliver_SendMail(array('sendmail_args'=>$sendmail_args));
73ad81bf 1556 $stream = $deliver->initStream($composeMessage,$sendmail_path);
b48d3c53 1557 } elseif ($draft) {
73ad81bf 1558 global $draft_folder;
1559 require_once(SM_PATH . 'class/deliver/Deliver_IMAP.class.php');
906f7e9f 1560 $imap_stream = sqimap_login($username, false, $imapServerAddress,
73ad81bf 1561 $imapPort, 0);
1562 if (sqimap_mailbox_exists ($imap_stream, $draft_folder)) {
1563 require_once(SM_PATH . 'class/deliver/Deliver_IMAP.class.php');
1564 $imap_deliver = new Deliver_IMAP();
1565 $length = $imap_deliver->mail($composeMessage);
1566 sqimap_append ($imap_stream, $draft_folder, $length);
1567 $imap_deliver->mail($composeMessage, $imap_stream);
1568 sqimap_append_done ($imap_stream, $draft_folder);
1569 sqimap_logout($imap_stream);
1570 unset ($imap_deliver);
c077ffeb 1571 $composeMessage->purgeAttachments();
73ad81bf 1572 return $length;
4dfb9db7 1573 } else {
fd7ab795 1574 $msg = '<br />'.sprintf(_("Error: Draft folder %s does not exist."), htmlspecialchars($draft_folder));
cb34dbd0 1575 plain_error_message($msg);
73ad81bf 1576 return false;
a91189d6 1577 }
a43e4b90 1578 }
0c59bbe1 1579 $success = false;
a43e4b90 1580 if ($stream) {
a91189d6 1581 $length = $deliver->mail($composeMessage, $stream);
0c59bbe1 1582 $success = $deliver->finalizeStream($stream);
a43e4b90 1583 }
0c59bbe1 1584 if (!$success) {
fd7ab795 1585 // $deliver->dlv_server_msg is not always server's reply
a15f9d93 1586 $msg = $deliver->dlv_msg;
1587 if (!empty($deliver->dlv_server_msg)) {
1588 // add 'server replied' part only when it is not empty.
1589 // Delivery error can be generated by delivery class itself
1590 $msg.='<br />' .
1591 _("Server replied:") . ' ' . $deliver->dlv_ret_nr . ' ' .
1592 nl2br(htmlspecialchars($deliver->dlv_server_msg));
1593 }
cb34dbd0 1594 plain_error_message($msg);
a43e4b90 1595 } else {
1596 unset ($deliver);
20152d80 1597 $move_to_sent = getPref($data_dir,$username,'move_to_sent');
906f7e9f 1598 $imap_stream = sqimap_login($username, false, $imapServerAddress, $imapPort, 0);
e4a1f097 1599
1600 /* Move to sent code */
1601 if (isset($default_move_to_sent) && ($default_move_to_sent != 0)) {
1602 $svr_allow_sent = true;
1603 } else {
1604 $svr_allow_sent = false;
1605 }
1606
1c044820 1607 if (isset($sent_folder) && (($sent_folder != '') || ($sent_folder != 'none'))
73ad81bf 1608 && sqimap_mailbox_exists( $imap_stream, $sent_folder)) {
e4a1f097 1609 $fld_sent = true;
1610 } else {
1611 $fld_sent = false;
1612 }
1613
1614 if ((isset($move_to_sent) && ($move_to_sent != 0)) || (!isset($move_to_sent))) {
1615 $lcl_allow_sent = true;
1616 } else {
1617 $lcl_allow_sent = false;
1618 }
1619
1620 if (($fld_sent && $svr_allow_sent && !$lcl_allow_sent) || ($fld_sent && $lcl_allow_sent)) {
eceefdfe 1621 global $passed_id, $mailbox, $action;
1622 if ($action == 'reply' || $action == 'reply_all') {
1623 $save_reply_with_orig=getPref($data_dir,$username,'save_reply_with_orig');
1624 if ($save_reply_with_orig) {
1625 $sent_folder = $mailbox;
1626 }
1627 }
e4a1f097 1628 sqimap_append ($imap_stream, $sent_folder, $length);
a91189d6 1629 require_once(SM_PATH . 'class/deliver/Deliver_IMAP.class.php');
1630 $imap_deliver = new Deliver_IMAP();
1631 $imap_deliver->mail($composeMessage, $imap_stream);
e4a1f097 1632 sqimap_append_done ($imap_stream, $sent_folder);
a91189d6 1633 unset ($imap_deliver);
1634 }
8780308f 1635
1636 global $passed_id, $mailbox, $action, $what, $iAccount,$startMessage;
1637
c077ffeb 1638 $composeMessage->purgeAttachments();
bc29bf70 1639 if ($action=='reply' || $action=='reply_all' || $action=='forward' || $action=='forward_as_attachment') {
202bcbcc 1640 require(SM_PATH . 'functions/mailbox_display.php');
8780308f 1641 $aMailbox = sqm_api_mailbox_select($imap_stream, $iAccount, $mailbox,array('setindex' => $what, 'offset' => $startMessage),array());
bc29bf70 1642 switch($action) {
1643 case 'reply':
1644 case 'reply_all':
1645 // check if we are allowed to set the \\Answered flag
1646 if (in_array('\\answered',$aMailbox['PERMANENTFLAGS'], true)) {
1647 $aUpdatedMsgs = sqimap_toggle_flag($imap_stream, array($passed_id), '\\Answered', true, false);
1648 if (isset($aUpdatedMsgs[$passed_id]['FLAGS'])) {
1649 /**
1650 * Only update the cached headers if the header is
1651 * cached.
1652 */
1653 if (isset($aMailbox['MSG_HEADERS'][$passed_id])) {
1654 $aMailbox['MSG_HEADERS'][$passed_id]['FLAGS'] = $aMsg['FLAGS'];
1655 }
1656 }
1657 }
1658 break;
1659 case 'forward':
1660 case 'forward_as_attachment':
1661 // check if we are allowed to set the $Forwarded flag (RFC 4550 paragraph 2.8)
1662 if (in_array('$forwarded',$aMailbox['PERMANENTFLAGS'], true) ||
1663 in_array('\\*',$aMailbox['PERMANENTFLAGS'])) {
1664
1665 $aUpdatedMsgs = sqimap_toggle_flag($imap_stream, array($passed_id), '$Forwarded', true, false);
1666 if (isset($aUpdatedMsgs[$passed_id]['FLAGS'])) {
1667 if (isset($aMailbox['MSG_HEADERS'][$passed_id])) {
1668 $aMailbox['MSG_HEADERS'][$passed_id]['FLAGS'] = $aMsg['FLAGS'];
1669 }
bda07b93 1670 }
8780308f 1671 }
bc29bf70 1672 break;
8780308f 1673 }
bc29bf70 1674
4d1cb59a 1675 /**
1676 * Write mailbox with updated seen flag information back to cache.
1677 */
bc29bf70 1678 if(isset($aUpdatedMsgs[$passed_id])) {
1679 $mailbox_cache[$iAccount.'_'.$aMailbox['NAME']] = $aMailbox;
1680 sqsession_register($mailbox_cache,'mailbox_cache');
1681 }
1682
1683 sqimap_logout($imap_stream);
a91189d6 1684 }
a43e4b90 1685 }
0c59bbe1 1686 return $success;
a43e4b90 1687}
762853f4 1688?>