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