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