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