on second thought, revise r12527 to use one, generic constant
[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 {
c8dc86c9 437//FIXME: DON'T ECHO HTML FROM CORE!
f265009a 438 echo ' <br><br><div style="text-align: center;"><a href="' . $location
09047d19 439 . '/compose.php?saved_sent=yes&amp;session=' . $composesession . '">'
f265009a 440 . _("Return") . '</a></div>';
a6d3eff6 441 }
b034bca2 442 exit();
443 } else {
09047d19 444 if ( !isset($pageheader_sent) || !$pageheader_sent ) {
445 Header("Location: $location/right_main.php?mailbox=" . urlencode($draft_folder) .
b034bca2 446 "&startMessage=1&note=".urlencode($draft_message));
09047d19 447 } else {
c8dc86c9 448//FIXME: DON'T ECHO HTML FROM CORE!
f265009a 449 echo ' <br><br><div style="text-align: center;"><a href="' . $location
09047d19 450 . '/right_main.php?mailbox=' . urlencode($draft_folder)
451 . '&amp;startMessage=1&amp;note=' . urlencode($draft_message) .'">'
f265009a 452 . _("Return") . '</a></div>';
a6d3eff6 453 }
b034bca2 454 exit();
00793a25 455 }
456 }
457}
458
4dfb9db7 459if ($send) {
0b97a708 460 if (isset($_FILES['attachfile']) &&
73ad81bf 461 $_FILES['attachfile']['tmp_name'] &&
462 $_FILES['attachfile']['tmp_name'] != 'none') {
da95c4b6 463 $AttachFailure = saveAttachedFiles($session);
00793a25 464 }
465 if (checkInput(false) && !isset($AttachFailure)) {
73ad81bf 466 if ($mailbox == "All Folders") {
467 /* We entered compose via the search results page */
a42c236f 468 $mailbox = 'INBOX'; /* Send 'em to INBOX, that's safe enough */
73ad81bf 469 }
9a19cc66 470 $urlMailbox = urlencode($mailbox);
3f6b1b6f 471 if (! isset($passed_id)) {
472 $passed_id = 0;
00793a25 473 }
d4c5c50c 474 /**
00793a25 475 * Set $default_charset to correspond with the user's selection
7058a2a9 476 * of language interface.
00793a25 477 */
478 set_my_charset();
d4c5c50c 479 /**
00793a25 480 * This is to change all newlines to \n
7058a2a9 481 * We'll change them to \r\n later (in the sendMessage function)
00793a25 482 */
483 $body = str_replace("\r\n", "\n", $body);
484 $body = str_replace("\r", "\n", $body);
485
d4c5c50c 486 /**
18c9998a 487 * Rewrap $body so that no line is bigger than $editor_size
00793a25 488 */
18c9998a 489 $body = explode("\n", $body);
490 $newBody = '';
491 foreach ($body as $line) {
492 if( $line <> '-- ' ) {
73ad81bf 493 $line = rtrim($line);
18c9998a 494 }
774d79cc 495 if (sq_strlen($line, $default_charset) <= $editor_size + 1) {
18c9998a 496 $newBody .= $line . "\n";
497 } else {
774d79cc 498 sqWordWrap($line, $editor_size, $default_charset);
18c9998a 499 $newBody .= $line . "\n";
500
501 }
502
503 }
504 $body = $newBody;
1c044820 505
a43e4b90 506 $composeMessage=$compose_messages[$session];
d5181a1d 507
a91189d6 508 $Result = deliverMessage($composeMessage);
81de00c0 509
64793f9a 510 if ($Result)
511 $mail_sent = 'yes';
512 else
513 $mail_sent = 'no';
514
1638beb6 515 // NOTE: this hook changed in 1.5.2 from sending $Result and
6e515418 516 // $composeMessage as args #2 and #3 to being in an array
517 // under arg #2
64793f9a 518 do_hook('compose_send_after', $temp=array(&$Result, &$composeMessage, &$mail_sent));
00793a25 519 if (! $Result) {
da95c4b6 520 showInputForm($session);
00793a25 521 exit();
522 }
dd4a44cd 523 unset($compose_messages[$session]);
a7e002d2 524 sqsession_register($compose_messages,'compose_messages');
5c4ff7bf 525
b034bca2 526 /* if it is resumed draft, delete draft message */
00793a25 527 if ( isset($delete_draft)) {
906f7e9f 528 $imap_stream = sqimap_login($username, false, $imapServerAddress, $imapPort, false);
b034bca2 529 sqimap_mailbox_select($imap_stream, $draft_folder);
530 // bypass_trash=true because message should be saved when deliverMessage() returns true.
91c27aee 531 // in current implementation of sqimap_msgs_list_flag() single message id can
b034bca2 532 // be submitted as string. docs state that it should be array.
533 sqimap_msgs_list_delete($imap_stream, $draft_folder, $delete_draft, true);
534 if ($auto_expunge) {
535 sqimap_mailbox_expunge($imap_stream, $draft_folder, true);
536 }
537 sqimap_logout($imap_stream);
00793a25 538 }
81de00c0 539 /*
540 * Store the error array in the session because they will be lost on a redirect
541 */
762853f4 542 $oErrorHandler->saveDelayedErrors();
c077ffeb 543 session_write_close();
762853f4 544
9c3e6cd4 545 if ($compose_new_win == '1') {
09047d19 546 if ( !isset($pageheader_sent) || !$pageheader_sent ) {
64793f9a 547 Header("Location: $location/compose.php?mail_sent=$mail_sent");
09047d19 548 } else {
c8dc86c9 549//FIXME: DON'T ECHO HTML FROM CORE!
f265009a 550 echo ' <br><br><div style="text-align: center;"><a href="' . $location
64793f9a 551 . '/compose.php?mail_sent=$mail_sent">'
f265009a 552 . _("Return") . '</a></div>';
09047d19 553 }
554 exit();
555 } else {
556 if ( !isset($pageheader_sent) || !$pageheader_sent ) {
557 Header("Location: $location/right_main.php?mailbox=$urlMailbox".
64793f9a 558 "&startMessage=$startMessage&mail_sent=$mail_sent");
09047d19 559 } else {
c8dc86c9 560//FIXME: DON'T ECHO HTML FROM CORE!
f265009a 561 echo ' <br><br><div style="text-align: center;"><a href="' . $location
09047d19 562 . "/right_main.php?mailbox=$urlMailbox"
64793f9a 563 . "&amp;startMessage=$startMessage&amp;mail_sent=$mail_sent\">"
f265009a 564 . _("Return") . '</a></div>';
09047d19 565 }
566 exit();
9c3e6cd4 567 }
734f4ee6 568 } else {
9c3e6cd4 569 if ($compose_new_win == '1') {
570 compose_Header($color, $mailbox);
571 }
572 else {
573 displayPageHeader($color, $mailbox);
574 }
00793a25 575 if (isset($AttachFailure)) {
73ad81bf 576 plain_error_message(_("Could not move/copy file. File not attached"),
577 $color);
00793a25 578 }
00793a25 579 checkInput(true);
da95c4b6 580 showInputForm($session);
00793a25 581 /* sqimap_logout($imapConnection); */
582 }
e02775fe 583} elseif (isset($html_addr_search_done)) {
73ad81bf 584 if ($compose_new_win == '1') {
585 compose_Header($color, $mailbox);
586 }
587 else {
588 displayPageHeader($color, $mailbox);
589 }
00793a25 590
591 if (isset($send_to_search) && is_array($send_to_search)) {
592 foreach ($send_to_search as $k => $v) {
593 if (substr($k, 0, 1) == 'T') {
594 if ($send_to) {
595 $send_to .= ', ';
596 }
597 $send_to .= $v;
598 }
599 elseif (substr($k, 0, 1) == 'C') {
600 if ($send_to_cc) {
601 $send_to_cc .= ', ';
602 }
603 $send_to_cc .= $v;
604 }
605 elseif (substr($k, 0, 1) == 'B') {
606 if ($send_to_bcc) {
607 $send_to_bcc .= ', ';
608 }
609 $send_to_bcc .= $v;
610 }
611 }
612 }
da95c4b6 613 showInputForm($session);
167c6996 614} elseif (isset($html_addr_search) && !isset($html_addr_search_cancel)) {
0b97a708 615 if (isset($_FILES['attachfile']) &&
73ad81bf 616 $_FILES['attachfile']['tmp_name'] &&
617 $_FILES['attachfile']['tmp_name'] != 'none') {
0b97a708 618 if(saveAttachedFiles($session)) {
cb34dbd0 619 plain_error_message(_("Could not move/copy file. File not attached"));
00793a25 620 }
621 }
622 /*
623 * I am using an include so as to elminiate an extra unnecessary
624 * click. If you can think of a better way, please implement it.
625 */
626 include_once('./addrbook_search_html.php');
e02775fe 627} elseif (isset($attach)) {
73ad81bf 628 if ($compose_new_win == '1') {
629 compose_Header($color, $mailbox);
630 } else {
631 displayPageHeader($color, $mailbox);
632 }
5a3e52f3 633 if (saveAttachedFiles($session)) {
cb34dbd0 634 plain_error_message(_("Could not move/copy file. File not attached"));
5a3e52f3 635 }
da95c4b6 636 showInputForm($session);
01265fba 637}
638elseif (isset($sigappend)) {
1e2a6ff6 639 $signature = $idents[$identity]['signature'];
640
01265fba 641 $body .= "\n\n".($prefix_sig==true? "-- \n":'').$signature;
642 if ($compose_new_win == '1') {
73ad81bf 643 compose_Header($color, $mailbox);
01265fba 644 } else {
645 displayPageHeader($color, $mailbox);
646 }
da95c4b6 647 showInputForm($session);
e02775fe 648} elseif (isset($do_delete)) {
73ad81bf 649 if ($compose_new_win == '1') {
650 compose_Header($color, $mailbox);
651 } else {
652 displayPageHeader($color, $mailbox);
653 }
00793a25 654
00793a25 655 if (isset($delete) && is_array($delete)) {
a43e4b90 656 $composeMessage = $compose_messages[$session];
00793a25 657 foreach($delete as $index) {
a58b05b4 658 if (!empty($composeMessage->entities) && isset($composeMessage->entities[$index])) {
c077ffeb 659 $composeMessage->entities[$index]->purgeAttachments();
a58b05b4 660 unset ($composeMessage->entities[$index]);
661 }
a91189d6 662 }
663 $new_entities = array();
664 foreach ($composeMessage->entities as $entity) {
665 $new_entities[] = $entity;
00793a25 666 }
a91189d6 667 $composeMessage->entities = $new_entities;
668 $compose_messages[$session] = $composeMessage;
669 sqsession_register($compose_messages, 'compose_messages');
00793a25 670 }
da95c4b6 671 showInputForm($session);
734f4ee6 672} else {
00793a25 673 /*
674 * This handles the default case as well as the error case
1c044820 675 * (they had the same code) --> if (isset($smtpErrors))
00793a25 676 */
44560457 677
678 if ($compose_new_win == '1') {
73ad81bf 679 compose_Header($color, $mailbox);
44560457 680 } else {
73ad81bf 681 displayPageHeader($color, $mailbox);
44560457 682 }
00793a25 683
684 $newmail = true;
685
a61878d0 686 if (!isset($passed_ent_id)) {
687 $passed_ent_id = '';
688 }
689 if (!isset($passed_id)) {
1c044820 690 $passed_id = '';
a61878d0 691 }
692 if (!isset($mailbox)) {
693 $mailbox = '';
1c044820 694 }
a61878d0 695 if (!isset($action)) {
696 $action = '';
697 }
1c044820 698
44560457 699 $values = newMail($mailbox,$passed_id,$passed_ent_id, $action, $session);
b9928adc 700
701 /* in case the origin is not read_body.php */
702 if (isset($send_to)) {
73ad81bf 703 $values['send_to'] = $send_to;
b9928adc 704 }
705 if (isset($send_to_cc)) {
73ad81bf 706 $values['send_to_cc'] = $send_to_cc;
b9928adc 707 }
708 if (isset($send_to_bcc)) {
73ad81bf 709 $values['send_to_bcc'] = $send_to_bcc;
b9928adc 710 }
2a2f2185 711 if (isset($subject)) {
73ad81bf 712 $values['subject'] = $subject;
2a2f2185 713 }
41b94d65 714 showInputForm($session, $values);
00793a25 715}
716
717exit();
718
00793a25 719/**************** Only function definitions go below *************/
720
92c6f757 721function getforwardSubject($subject)
722{
723 if ((substr(strtolower($subject), 0, 4) != 'fwd:') &&
73ad81bf 724 (substr(strtolower($subject), 0, 5) != '[fwd:') &&
725 (substr(strtolower($subject), 0, 6) != '[ fwd:')) {
92c6f757 726 $subject = '[Fwd: ' . $subject . ']';
727 }
728 return $subject;
729}
00793a25 730
48985d59 731/* This function is used when not sending or adding attachments */
44560457 732function newMail ($mailbox='', $passed_id='', $passed_ent_id='', $action='', $session='') {
1e2a6ff6 733 global $editor_size, $default_use_priority, $body, $idents,
ce68b76b 734 $use_signature, $data_dir, $username,
c6f28eb1 735 $key, $imapServerAddress, $imapPort, $compose_messages,
74f66d27 736 $composeMessage, $body_quote, $request_mdn, $request_dr,
856e58ef 737 $mdn_user_support, $languages, $squirrelmail_language,
738 $default_charset;
e7f1a81d 739
d4f20027 740 /*
741 * Set $default_charset to correspond with the user's selection
742 * of language interface. $default_charset global is not correct,
743 * if message is composed in new window.
744 */
745 set_my_charset();
746
91f2085b 747 $send_to = $send_to_cc = $send_to_bcc = $subject = $identity = '';
bdb92db3 748 $mailprio = 3;
44560457 749
41b94d65 750 if ($passed_id) {
906f7e9f 751 $imapConnection = sqimap_login($username, false, $imapServerAddress,
73ad81bf 752 $imapPort, 0);
a61878d0 753
48985d59 754 sqimap_mailbox_select($imapConnection, $mailbox);
41b94d65 755 $message = sqimap_get_message($imapConnection, $passed_id, $mailbox);
1c044820 756
a61878d0 757 $body = '';
758 if ($passed_ent_id) {
759 /* redefine the messsage in case of message/rfc822 */
760 $message = $message->getEntity($passed_ent_id);
761 /* message is an entity which contains the envelope and type0=message
73ad81bf 762 * and type1=rfc822. The actual entities are childs from
763 * $message->entities[0]. That's where the encoding and is located
764 */
a61878d0 765
766 $entities = $message->entities[0]->findDisplayEntity
73ad81bf 767 (array(), $alt_order = array('text/plain'));
a61878d0 768 if (!count($entities)) {
769 $entities = $message->entities[0]->findDisplayEntity
9c462f8b 770 (array(), $alt_order = array('text/plain','text/html'));
a61878d0 771 }
772 $orig_header = $message->rfc822_header; /* here is the envelope located */
773 /* redefine the message for picking up the attachments */
774 $message = $message->entities[0];
775
776 } else {
777 $entities = $message->findDisplayEntity (array(), $alt_order = array('text/plain'));
778 if (!count($entities)) {
9c462f8b 779 $entities = $message->findDisplayEntity (array(), $alt_order = array('text/plain','text/html'));
a61878d0 780 }
781 $orig_header = $message->rfc822_header;
782 }
1c044820 783
a61878d0 784 $type0 = $message->type0;
785 $type1 = $message->type1;
41b94d65 786 foreach ($entities as $ent) {
b455e47b 787 $msg = $message->getEntity($ent);
788 $type0 = $msg->type0;
789 $type1 = $msg->type1;
a61878d0 790 $unencoded_bodypart = mime_fetch_body($imapConnection, $passed_id, $ent);
791 $body_part_entity = $message->getEntity($ent);
1638beb6 792 $bodypart = decodeBody($unencoded_bodypart,
73ad81bf 793 $body_part_entity->header->encoding);
a61878d0 794 if ($type1 == 'html') {
5b755d9f 795 $bodypart = str_replace("\n", ' ', $bodypart);
bb977394 796 $bodypart = preg_replace(array('/<\/?p>/i','/<div><\/div>/i','/<br\s*(\/)*>/i','/<\/?div>/i'), "\n", $bodypart);
5b755d9f 797 $bodypart = str_replace(array('&nbsp;','&gt;','&lt;'),array(' ','>','<'),$bodypart);
a61878d0 798 $bodypart = strip_tags($bodypart);
799 }
e842b215 800 if (isset($languages[$squirrelmail_language]['XTRA_CODE']) &&
73ad81bf 801 function_exists($languages[$squirrelmail_language]['XTRA_CODE'] . '_decode')) {
e842b215 802 if (mb_detect_encoding($bodypart) != 'ASCII') {
f4bb5d22 803 $bodypart = call_user_func($languages[$squirrelmail_language]['XTRA_CODE'] . '_decode', $bodypart);
e842b215 804 }
805 }
eceefdfe 806
bfa54da7 807 // charset encoding in compose form stuff
73ad81bf 808 if (isset($body_part_entity->header->parameters['charset'])) {
809 $actual = $body_part_entity->header->parameters['charset'];
810 } else {
811 $actual = 'us-ascii';
812 }
beca818e 813
73ad81bf 814 if ( $actual && is_conversion_safe($actual) && $actual != $default_charset){
815 $bodypart = charset_convert($actual,$bodypart,$default_charset,false);
816 }
bfa54da7 817 // end of charset encoding in compose
eceefdfe 818
a61878d0 819 $body .= $bodypart;
820 }
821 if ($default_use_priority) {
822 $mailprio = substr($orig_header->priority,0,1);
823 if (!$mailprio) {
824 $mailprio = 3;
825 }
826 } else {
827 $mailprio = '';
828 }
bdb92db3 829
a45887d7 830 $from_o = $orig_header->from;
fe868193 831 if (is_array($from_o)) {
832 if (isset($from_o[0])) {
833 $from_o = $from_o[0];
834 }
835 }
bdb92db3 836 if (is_object($from_o)) {
837 $orig_from = $from_o->getAddress();
838 } else {
839 $orig_from = '';
a61878d0 840 }
1e2a6ff6 841
a91189d6 842 $identities = array();
1e2a6ff6 843 if (count($idents) > 1) {
844 foreach($idents as $nr=>$data) {
845 $enc_from_name = '"'.$data['full_name'].'" <'. $data['email_address'].'>';
846 if($enc_from_name == $orig_from) {
847 $identity = $nr;
a61878d0 848 break;
849 }
a91189d6 850 $identities[] = $enc_from_name;
851 }
1e2a6ff6 852
a91189d6 853 $identity_match = $orig_header->findAddress($identities);
854 if ($identity_match) {
855 $identity = $identity_match;
a61878d0 856 }
bdb92db3 857 }
a61878d0 858
859 switch ($action) {
73ad81bf 860 case ('draft'):
861 $use_signature = FALSE;
862 $composeMessage->rfc822_header = $orig_header;
863 $send_to = decodeHeader($orig_header->getAddr_s('to'),false,false,true);
864 $send_to_cc = decodeHeader($orig_header->getAddr_s('cc'),false,false,true);
865 $send_to_bcc = decodeHeader($orig_header->getAddr_s('bcc'),false,false,true);
866 $send_from = $orig_header->getAddr_s('from');
867 $send_from_parts = new AddressStructure();
868 $send_from_parts = $orig_header->parseAddress($send_from);
869 $send_from_add = $send_from_parts->mailbox . '@' . $send_from_parts->host;
40e07136 870 $identity = find_identity(array($send_from_add));
73ad81bf 871 $subject = decodeHeader($orig_header->subject,false,false,true);
1638beb6 872
762853f4 873 // Remember the receipt settings
874 $request_mdn = $mdn_user_support && !empty($orig_header->dnt) ? '1' : '0';
875 $request_dr = $mdn_user_support && !empty($orig_header->drnt) ? '1' : '0';
1638beb6 876
73ad81bf 877 /* remember the references and in-reply-to headers in case of an reply */
878 $composeMessage->rfc822_header->more_headers['References'] = $orig_header->references;
879 $composeMessage->rfc822_header->more_headers['In-Reply-To'] = $orig_header->in_reply_to;
880 // rewrap the body to clean up quotations and line lengths
881 sqBodyWrap($body, $editor_size);
882 $composeMessage = getAttachments($message, $composeMessage, $passed_id, $entities, $imapConnection);
883 break;
884 case ('edit_as_new'):
885 $send_to = decodeHeader($orig_header->getAddr_s('to'),false,false,true);
886 $send_to_cc = decodeHeader($orig_header->getAddr_s('cc'),false,false,true);
887 $send_to_bcc = decodeHeader($orig_header->getAddr_s('bcc'),false,false,true);
888 $subject = decodeHeader($orig_header->subject,false,false,true);
889 $mailprio = $orig_header->priority;
890 $orig_from = '';
891 $composeMessage = getAttachments($message, $composeMessage, $passed_id, $entities, $imapConnection);
892 // rewrap the body to clean up quotations and line lengths
893 sqBodyWrap($body, $editor_size);
894 break;
895 case ('forward'):
896 $send_to = '';
897 $subject = getforwardSubject(decodeHeader($orig_header->subject,false,false,true));
898 $body = getforwardHeader($orig_header) . $body;
899 // the logic for calling sqUnWordWrap here would be to allow the browser to wrap the lines
900 // forwarded message text should be as undisturbed as possible, so commenting out this call
901 // sqUnWordWrap($body);
902 $composeMessage = getAttachments($message, $composeMessage, $passed_id, $entities, $imapConnection);
91c27aee 903
73ad81bf 904 //add a blank line after the forward headers
905 $body = "\n" . $body;
906 break;
907 case ('forward_as_attachment'):
908 $subject = getforwardSubject(decodeHeader($orig_header->subject,false,false,true));
909 $composeMessage = getMessage_RFC822_Attachment($message, $composeMessage, $passed_id, $passed_ent_id, $imapConnection);
910 $body = '';
911 break;
912 case ('reply_all'):
913 if(isset($orig_header->mail_followup_to) && $orig_header->mail_followup_to) {
914 $send_to = $orig_header->getAddr_s('mail_followup_to');
b268e66b 915 } else {
73ad81bf 916 $send_to_cc = replyAllString($orig_header);
917 $send_to_cc = decodeHeader($send_to_cc,false,false,true);
b268e66b 918 }
73ad81bf 919 case ('reply'):
920 // skip this if send_to was already set right above here
921 if(!$send_to) {
922 $send_to = $orig_header->reply_to;
923 if (is_array($send_to) && count($send_to)) {
924 $send_to = $orig_header->getAddr_s('reply_to');
925 } else if (is_object($send_to)) { /* unneccesarry, just for failsafe purpose */
926 $send_to = $orig_header->getAddr_s('reply_to');
927 } else {
928 $send_to = $orig_header->getAddr_s('from');
929 }
dd4a44cd 930 }
73ad81bf 931 $send_to = decodeHeader($send_to,false,false,true);
932 $subject = decodeHeader($orig_header->subject,false,false,true);
933 $subject = str_replace('"', "'", $subject);
934 $subject = trim($subject);
935 if (substr(strtolower($subject), 0, 3) != 're:') {
936 $subject = 'Re: ' . $subject;
937 }
938 /* this corrects some wrapping/quoting problems on replies */
939 $rewrap_body = explode("\n", $body);
3aaa3214 940 $from = (is_array($orig_header->from) && !empty($orig_header->from)) ? $orig_header->from[0] : $orig_header->from;
73ad81bf 941 $body = '';
942 $strip_sigs = getPref($data_dir, $username, 'strip_sigs');
943 foreach ($rewrap_body as $line) {
944 if ($strip_sigs && substr($line,0,3) == '-- ') {
945 break;
946 }
947 if (preg_match("/^(>+)/", $line, $matches)) {
948 $gt = $matches[1];
949 $body .= $body_quote . str_replace("\n", "\n$body_quote$gt ", rtrim($line)) ."\n";
950 } else {
951 $body .= $body_quote . (!empty($body_quote) ? ' ' : '') . str_replace("\n", "\n$body_quote" . (!empty($body_quote) ? ' ' : ''), rtrim($line)) . "\n";
952 }
a61878d0 953 }
c9d61baf 954
73ad81bf 955 //rewrap the body to clean up quotations and line lengths
956 $body = sqBodyWrap ($body, $editor_size);
c9d61baf 957
73ad81bf 958 $body = getReplyCitation($from , $orig_header->date) . $body;
959 $composeMessage->reply_rfc822_header = $orig_header;
12a0ed01 960
73ad81bf 961 break;
962 default:
963 break;
41b94d65 964 }
a91189d6 965 $compose_messages[$session] = $composeMessage;
966 sqsession_register($compose_messages, 'compose_messages');
5da08ef7 967 session_write_close();
a61878d0 968 sqimap_logout($imapConnection);
41b94d65 969 }
a61878d0 970 $ret = array( 'send_to' => $send_to,
73ad81bf 971 'send_to_cc' => $send_to_cc,
972 'send_to_bcc' => $send_to_bcc,
973 'subject' => $subject,
974 'mailprio' => $mailprio,
975 'body' => $body,
976 'identity' => $identity );
a61878d0 977
41b94d65 978 return ($ret);
48985d59 979} /* function newMail() */
980
50706f77 981/**
982 * downloads attachments from original message, stores them in attachment directory and adds
983 * them to composed message.
984 * @param object $message
985 * @param object $composeMessage
986 * @param integer $passed_id
987 * @param mixed $entities
988 * @param mixed $imapConnection
91c27aee 989 * @return object
50706f77 990 */
a43e4b90 991function getAttachments($message, &$composeMessage, $passed_id, $entities, $imapConnection) {
628bce99 992 global $squirrelmail_language, $languages;
993
1c044820 994 if (!count($message->entities) ||
73ad81bf 995 ($message->type0 == 'message' && $message->type1 == 'rfc822')) {
41b94d65 996 if ( !in_array($message->entity_id, $entities) && $message->entity_id) {
73ad81bf 997 switch ($message->type0) {
998 case 'message':
999 if ($message->type1 == 'rfc822') {
1000 $filename = $message->rfc822_header->subject;
1001 if ($filename == "") {
1002 $filename = "untitled-".$message->entity_id;
1003 }
1004 $filename .= '.msg';
1005 } else {
1006 $filename = $message->getFilename();
181538ac 1007 }
73ad81bf 1008 break;
1009 default:
1010 if (!$message->mime_header) { /* temporary hack */
1011 $message->mime_header = $message->header;
1012 }
1013 $filename = $message->getFilename();
1014 break;
1015 }
1016 $filename = str_replace('&#32;', ' ', decodeHeader($filename));
1017 if (isset($languages[$squirrelmail_language]['XTRA_CODE']) &&
1018 function_exists($languages[$squirrelmail_language]['XTRA_CODE'] . '_encode')) {
f4bb5d22 1019 $filename = call_user_func($languages[$squirrelmail_language]['XTRA_CODE'] . '_encode', $filename);
73ad81bf 1020 }
628bce99 1021 $localfilename = sq_get_attach_tempfile();
1022 $message->att_local_name = $localfilename;
73ad81bf 1023
1024 $composeMessage->initAttachment($message->type0.'/'.$message->type1,$filename,
628bce99 1025 $localfilename);
73ad81bf 1026
1027 /* Write Attachment to file */
628bce99 1028 $fp = fopen ($localfilename, 'wb');
91c27aee 1029 mime_print_body_lines ($imapConnection, $passed_id, $message->entity_id, $message->header->encoding, $fp);
73ad81bf 1030 fclose ($fp);
48985d59 1031 }
734f4ee6 1032 } else {
a43e4b90 1033 for ($i=0, $entCount=count($message->entities); $i<$entCount;$i++) {
1034 $composeMessage=getAttachments($message->entities[$i], $composeMessage, $passed_id, $entities, $imapConnection);
48985d59 1035 }
1036 }
a43e4b90 1037 return $composeMessage;
48985d59 1038}
1039
1c044820 1040function getMessage_RFC822_Attachment($message, $composeMessage, $passed_id,
73ad81bf 1041 $passed_ent_id='', $imapConnection) {
756406df 1042 if (!$passed_ent_id) {
1c044820 1043 $body_a = sqimap_run_command($imapConnection,
73ad81bf 1044 'FETCH '.$passed_id.' RFC822',
1045 TRUE, $response, $readmessage,
1046 TRUE);
756406df 1047 } else {
1c044820 1048 $body_a = sqimap_run_command($imapConnection,
73ad81bf 1049 'FETCH '.$passed_id.' BODY['.$passed_ent_id.']',
1050 TRUE, $response, $readmessage, TRUE);
a61878d0 1051 $message = $message->parent;
756406df 1052 }
d0519c03 1053 if ($response == 'OK') {
a61878d0 1054 $subject = encodeHeader($message->rfc822_header->subject);
1055 array_shift($body_a);
1c044820 1056 array_pop($body_a);
a61878d0 1057 $body = implode('', $body_a) . "\r\n";
1c044820 1058
628bce99 1059 $localfilename = sq_get_attach_tempfile();
1060 $fp = fopen($localfilename, 'wb');
a61878d0 1061 fwrite ($fp, $body);
1062 fclose($fp);
5a1f1da3 1063 $composeMessage->initAttachment('message/rfc822',$subject.'.msg',
628bce99 1064 $localfilename);
a43e4b90 1065 }
1066 return $composeMessage;
a6ec592e 1067}
1068
41b94d65 1069function showInputForm ($session, $values=false) {
856e58ef 1070 global $send_to, $send_to_cc, $send_to_bcc,
1071 $body, $startMessage, $action,
1072 $use_signature, $signature, $prefix_sig,
8d8da447 1073 $editor_size, $editor_height, $subject, $newmail,
856e58ef 1074 $use_javascript_addr_book, $passed_id, $mailbox,
73ad81bf 1075 $from_htmladdr_search, $location_of_buttons, $attachment_dir,
ce68b76b 1076 $username, $data_dir, $identity, $idents, $delete_draft,
1077 $mailprio, $compose_new_win, $saved_draft, $mail_sent, $sig_first,
c6f28eb1 1078 $compose_messages, $composesession, $default_charset,
762853f4 1079 $compose_onsubmit, $oTemplate, $oErrorHandler;
a43e4b90 1080
87745b9c 1081 if (checkForJavascript()) {
1082 $onfocus = ' onfocus="alreadyFocused=true;"';
1083 $onfocus_array = array('onfocus' => 'alreadyFocused=true;');
1084 }
1085 else {
1086 $onfocus = '';
1087 $onfocus_array = array();
1088 }
1089
a43e4b90 1090 $composeMessage = $compose_messages[$session];
41b94d65 1091 if ($values) {
73ad81bf 1092 $send_to = $values['send_to'];
1093 $send_to_cc = $values['send_to_cc'];
1094 $send_to_bcc = $values['send_to_bcc'];
1095 $subject = $values['subject'];
1096 $mailprio = $values['mailprio'];
1097 $body = $values['body'];
1098 $identity = (int) $values['identity'];
676bb189 1099 } else {
73ad81bf 1100 $send_to = decodeHeader($send_to, true, false);
1101 $send_to_cc = decodeHeader($send_to_cc, true, false);
1102 $send_to_bcc = decodeHeader($send_to_bcc, true, false);
41b94d65 1103 }
1c044820 1104
48985d59 1105 if ($use_javascript_addr_book) {
c8dc86c9 1106//FIXME: NO HTML IN CORE!
2c92ea9d 1107 echo "\n". '<script type="text/javascript">'."\n<!--\n" .
73ad81bf 1108 'function open_abook() { ' . "\n" .
1109 ' var nwin = window.open("addrbook_popup.php","abookpopup",' .
1110 '"width=670,height=300,resizable=yes,scrollbars=yes");' . "\n" .
1111 ' if((!nwin.opener) && (document.windows != null))' . "\n" .
1112 ' nwin.opener = document.windows;' . "\n" .
1113 "}\n" .
1114 "// -->\n</script>\n\n";
48985d59 1115 }
1116
c8dc86c9 1117//FIXME: NO HTML IN CORE!
4a1788b3 1118 echo "\n" . '<form name="compose" action="compose.php" method="post" ' .
73ad81bf 1119 'enctype="multipart/form-data"';
a34b07a5 1120
1121 $compose_onsubmit = array();
6e515418 1122 global $null;
1123 do_hook('compose_form', $null);
1c044820 1124
a34b07a5 1125 // Plugins that use compose_form hook can add an array entry
1126 // to the globally scoped $compose_onsubmit; we add them up
5c4ff7bf 1127 // here and format the form tag's full onsubmit handler.
1128 // Each plugin should use "return false" if they need to
a34b07a5 1129 // stop form submission but otherwise should NOT use "return
1130 // true" to give other plugins the chance to do what they need
1131 // to do; SquirrelMail itself will add the final "return true".
1132 // Onsubmit text is enclosed inside of double quotes, so plugins
1133 // need to quote accordingly.
1134 if (checkForJavascript()) {
1135 $onsubmit_text = ' onsubmit="';
5c4ff7bf 1136 if (empty($compose_onsubmit))
a34b07a5 1137 $compose_onsubmit = array();
5c4ff7bf 1138 else if (!is_array($compose_onsubmit))
a34b07a5 1139 $compose_onsubmit = array($compose_onsubmit);
1140
1141 foreach ($compose_onsubmit as $text) {
1142 $text = trim($text);
5c4ff7bf 1143 if (substr($text, -1) != ';' && substr($text, -1) != '}')
a34b07a5 1144 $text .= '; ';
1145 $onsubmit_text .= $text;
1146 }
1147
c8dc86c9 1148//FIXME: DON'T ECHO HTML FROM CORE!
a34b07a5 1149 echo $onsubmit_text . ' return true;"';
1150 }
5c4ff7bf 1151
a34b07a5 1152
c8dc86c9 1153//FIXME: NO HTML IN CORE!
48985d59 1154 echo ">\n";
1155
c8dc86c9 1156//FIXME: DON'T ECHO HTML FROM CORE!
df96b37a 1157 echo addHidden('startMessage', $startMessage);
4a1788b3 1158
41b94d65 1159 if ($action == 'draft') {
c8dc86c9 1160//FIXME: DON'T ECHO HTML FROM CORE!
df96b37a 1161 echo addHidden('delete_draft', $passed_id);
48985d59 1162 }
1163 if (isset($delete_draft)) {
c8dc86c9 1164//FIXME: DON'T ECHO HTML FROM CORE!
df96b37a 1165 echo addHidden('delete_draft', $delete_draft);
48985d59 1166 }
da95c4b6 1167 if (isset($session)) {
c8dc86c9 1168//FIXME: DON'T ECHO HTML FROM CORE!
df96b37a 1169 echo addHidden('session', $session);
da95c4b6 1170 }
1c044820 1171
08bad2b1 1172 if (isset($passed_id)) {
c8dc86c9 1173//FIXME: DON'T ECHO HTML FROM CORE!
df96b37a 1174 echo addHidden('passed_id', $passed_id);
08bad2b1 1175 }
44560457 1176
9c3e6cd4 1177 if ($saved_draft == 'yes') {
774d79cc 1178 $oTemplate->assign('note', _("Your draft has been saved."));
762853f4 1179 $oTemplate->display('note.tpl');
9c3e6cd4 1180 }
1181 if ($mail_sent == 'yes') {
774d79cc 1182 $oTemplate->assign('note', _("Your mail has been sent."));
762853f4 1183 $oTemplate->display('note.tpl');
9c3e6cd4 1184 }
9c3e6cd4 1185 if ($compose_new_win == '1') {
762853f4 1186 $oTemplate->display('compose_newwin_close.tpl');
9c3e6cd4 1187 }
1638beb6 1188
78a35fcd 1189 if ($location_of_buttons == 'top') {
c8dc86c9 1190//FIXME: DON'T ECHO HTML FROM CORE!
78a35fcd 1191 showComposeButtonRow();
1192 }
48985d59 1193
762853f4 1194 $identities = array();
1e2a6ff6 1195 if (count($idents) > 1) {
762853f4 1196 reset($idents);
73ad81bf 1197 foreach($idents as $id => $data) {
762853f4 1198 $identities[$id] = $data['full_name'].' &lt;'.$data['email_address'].'&gt;';
73ad81bf 1199 }
762853f4 1200 }
1638beb6 1201
762853f4 1202 $oTemplate->assign('identities', $identities);
1203 $oTemplate->assign('identity_def', $identity);
1204 $oTemplate->assign('input_onfocus', 'onfocus="'.join(' ', $onfocus_array).'"');
1638beb6 1205
762853f4 1206 $oTemplate->assign('to', htmlspecialchars($send_to));
1207 $oTemplate->assign('cc', htmlspecialchars($send_to_cc));
1208 $oTemplate->assign('bcc', htmlspecialchars($send_to_bcc));
1209 $oTemplate->assign('subject', htmlspecialchars($subject));
1638beb6 1210
762853f4 1211 $oTemplate->display('compose_header.tpl');
48985d59 1212
78a35fcd 1213 if ($location_of_buttons == 'between') {
c8dc86c9 1214//FIXME: DON'T ECHO HTML FROM CORE!
78a35fcd 1215 showComposeButtonRow();
1216 }
4dfb9db7 1217
762853f4 1218 $body_str = '';
48985d59 1219 if ($use_signature == true && $newmail == true && !isset($from_htmladdr_search)) {
1e2a6ff6 1220 $signature = $idents[$identity]['signature'];
d3c13a51 1221
3b17e952 1222 if ($sig_first == '1') {
50706f77 1223 /*
1224 * FIXME: test is specific to ja_JP translation implementation.
1225 * This test might apply incorrect conversion to other translations, but
91c27aee 1226 * use of 7bit iso-2022-jp charset in other translations might have other
50706f77 1227 * issues too.
1228 */
ab4700c3 1229 if ($default_charset == 'iso-2022-jp') {
762853f4 1230 $body_str = "\n\n".($prefix_sig==true? "-- \n":'').mb_convert_encoding($signature, 'EUC-JP');
83be314a 1231 } else {
762853f4 1232 $body_str = "\n\n".($prefix_sig==true? "-- \n":'').decodeHeader($signature,false,false);
83be314a 1233 }
762853f4 1234 $body_str .= "\n\n".htmlspecialchars(decodeHeader($body,false,false));
1235 } else {
1236 $body_str = "\n\n".htmlspecialchars(decodeHeader($body,false,false));
50706f77 1237 // FIXME: test is specific to ja_JP translation implementation. See above comments.
ab4700c3 1238 if ($default_charset == 'iso-2022-jp') {
762853f4 1239 $body_str .= "\n\n".($prefix_sig==true? "-- \n":'').mb_convert_encoding($signature, 'EUC-JP');
1240 } else {
1241 $body_str .= "\n\n".($prefix_sig==true? "-- \n":'').decodeHeader($signature,false,false);
73ad81bf 1242 }
3b17e952 1243 }
73ad81bf 1244 } else {
762853f4 1245 $body_str = htmlspecialchars(decodeHeader($body,false,false));
48985d59 1246 }
12a0ed01 1247
762853f4 1248 $oTemplate->assign('editor_width', (int)$editor_size);
1249 $oTemplate->assign('editor_height', (int)$editor_height);
1250 $oTemplate->assign('input_onfocus', 'onfocus="'.join(' ', $onfocus_array).'"');
1251 $oTemplate->assign('body', $body_str);
1252 $oTemplate->assign('show_bottom_send', $location_of_buttons!='bottom');
1638beb6 1253
762853f4 1254 $oTemplate->display ('compose_body.tpl');
1638beb6 1255
48985d59 1256 if ($location_of_buttons == 'bottom') {
c8dc86c9 1257//FIXME: DON'T ECHO HTML FROM CORE!
48985d59 1258 showComposeButtonRow();
48985d59 1259 }
46bb8da8 1260
48985d59 1261 /* This code is for attachments */
73ad81bf 1262 if ((bool) ini_get('file_uploads')) {
1263
1264 /* Calculate the max size for an uploaded file.
1265 * This is advisory for the user because we can't actually prevent
1266 * people to upload too large files. */
1267 $sizes = array();
1268 /* php.ini vars which influence the max for uploads */
1269 $configvars = array('post_max_size', 'memory_limit', 'upload_max_filesize');
1270 foreach($configvars as $var) {
4f21ba00 1271 /* skip 0 or empty values, and -1 which means 'unlimited' */
73ad81bf 1272 if( $size = getByteSize(ini_get($var)) ) {
4f21ba00 1273 if ( $size != '-1' ) {
1274 $sizes[] = $size;
1275 }
73ad81bf 1276 }
0a2c3218 1277 }
0a2c3218 1278
762853f4 1279 $attach = array();
ca8a7efc 1280 // composeMessage can be empty when coming from a restored session
1281 if (is_object($composeMessage) && $composeMessage->entities) {
73ad81bf 1282 foreach ($composeMessage->entities as $key => $attachment) {
1283 $attached_file = $attachment->att_local_name;
1284 if ($attachment->att_local_name || $attachment->body_part) {
1285 $attached_filename = decodeHeader($attachment->mime_header->getParameter('name'));
1286 $type = $attachment->mime_header->type0.'/'.
a91189d6 1287 $attachment->mime_header->type1;
1638beb6 1288
762853f4 1289 $a = array();
1290 $a['Key'] = $key;
1291 $a['FileName'] = $attached_filename;
1292 $a['ContentType'] = $type;
1293 $a['Size'] = filesize($attached_file);
1294 $attach[$key] = $a;
73ad81bf 1295 }
1296 }
4dfb9db7 1297 }
1638beb6 1298
762853f4 1299 $max = min($sizes);
1300 $oTemplate->assign('max_file_size', empty($max) ? -1 : $max);
1301 $oTemplate->assign('attachments', $attach);
1638beb6 1302
762853f4 1303 $oTemplate->display('compose_attachments.tpl');
73ad81bf 1304 } // End of file_uploads if-block
41b94d65 1305 /* End of attachment code */
762853f4 1306
60445d64 1307//FIXME: no direct echoing to browser, no HTML output in core!
762853f4 1308 echo addHidden('username', $username).
1309 addHidden('smaction', $action).
1310 addHidden('mailbox', $mailbox);
1c044820 1311 /*
1312 store the complete ComposeMessages array in a hidden input value
0ec1a14b 1313 so we can restore them in case of a session timeout.
73ad81bf 1314 */
953fa718 1315 sqgetGlobalVar('QUERY_STRING', $queryString, SQ_SERVER);
60445d64 1316//FIXME: no direct echoing to browser, no HTML output in core!
1638beb6 1317 echo addHidden('restoremessages', urlencode(serialize($compose_messages))).
73ad81bf 1318 addHidden('composesession', $composesession).
1319 addHidden('querystring', $queryString).
1320 "</form>\n";
a64f47e7 1321 if (!(bool) ini_get('file_uploads')) {
73ad81bf 1322 /* File uploads are off, so we didn't show that part of the form.
1323 To avoid bogus bug reports, tell the user why. */
60445d64 1324//FIXME: no direct echoing to browser, no HTML output in core!
50706f77 1325 echo '<p style="text-align:center">'
1326 . _("Because PHP file uploads are turned off, you can not attach files to this message. Please see your system administrator for details.")
1327 . "</p>\r\n";
a64f47e7 1328 }
1329
762853f4 1330 if ($compose_new_win=='1') {
1331 $oTemplate->display('compose_newwin_close.tpl');
1332 }
1638beb6 1333
d22879f7 1334 do_hook('compose_bottom', $null);
1335
762853f4 1336 $oErrorHandler->setDelayedErrors(false);
5c4ff7bf 1337 $oTemplate->display('footer.tpl');
48985d59 1338}
1339
1340
70c4fd84 1341function showComposeButtonRow() {
78a35fcd 1342 global $use_javascript_addr_book, $save_as_draft,
73ad81bf 1343 $default_use_priority, $mailprio, $default_use_mdn,
1344 $request_mdn, $request_dr,
1345 $data_dir, $username;
70c4fd84 1346
762853f4 1347 global $oTemplate, $buffer_hook;
1638beb6 1348
1349 if ($default_use_priority) {
762853f4 1350 $priorities = array('1'=>_("High"), '3'=>_("Normal"), '5'=>_("Low"));
1351 $priority = isset($mailprio) ? $mailprio : 3;
1352 } else {
1353 $priorities = array();
1354 $priority = NULL;
ae25968c 1355 }
1638beb6 1356
ae25968c 1357 $mdn_user_support=getPref($data_dir, $username, 'mdn_user_support',$default_use_mdn);
48985d59 1358
c40a269e 1359 if ($use_javascript_addr_book && checkForJavascript()) {
1360 $addr_book = addButton(_("Addresses"), null, array('onclick' => 'javascript:open_abook();'));
734f4ee6 1361 } else {
c40a269e 1362 $addr_book = addSubmit(_("Addresses"), 'html_addr_search');
78a35fcd 1363 }
48985d59 1364
762853f4 1365 $oTemplate->assign('allow_priority', $default_use_priority==1);
1366 $oTemplate->assign('priority_list', $priorities);
1367 $oTemplate->assign('current_priority', $priority);
1638beb6 1368
762853f4 1369 $oTemplate->assign('notifications_enabled', $mdn_user_support==1);
1370 $oTemplate->assign('read_receipt', $request_mdn=='1');
1371 $oTemplate->assign('delivery_receipt', $request_dr=='1');
1638beb6 1372
762853f4 1373 $oTemplate->assign('drafts_enabled', $save_as_draft);
1374 $oTemplate->assign('address_book_button', $addr_book);
441f2d33 1375
762853f4 1376 $oTemplate->display('compose_buttons.tpl');
78a35fcd 1377}
b278172f 1378
70c4fd84 1379function checkInput ($show) {
78a35fcd 1380 /*
1381 * I implemented the $show variable because the error messages
1382 * were getting sent before the page header. So, I check once
1383 * using $show=false, and then when i'm ready to display the error
1384 * message, show=true
1385 */
1356041d 1386 global $send_to, $send_to_cc, $send_to_bcc;
78a35fcd 1387
1356041d 1388 $send_to = trim($send_to);
1389 $send_to_cc = trim($send_to_cc);
1390 $send_to_bcc = trim($send_to_bcc);
1391 if (empty($send_to) && empty($send_to_cc) && empty($send_to_bcc)) {
78a35fcd 1392 if ($show) {
cb34dbd0 1393 plain_error_message(_("You have not filled in the \"To:\" field."));
78a35fcd 1394 }
1395 return false;
1396 }
1397 return true;
1398} /* function checkInput() */
df15de21 1399
3806fa52 1400
00793a25 1401/* True if FAILURE */
da95c4b6 1402function saveAttachedFiles($session) {
628bce99 1403 global $compose_messages;
bfa54da7 1404
45cdd1b5 1405 /* get out of here if no file was attached at all */
1406 if (! is_uploaded_file($_FILES['attachfile']['tmp_name']) ) {
1407 return true;
1408 }
1409
628bce99 1410 $localfilename = sq_get_attach_tempfile();
4c9d2242 1411
a42c236f 1412 // m_u_f works better with restricted PHP installs (safe_mode, open_basedir),
1413 // if that doesn't work, try a simple rename.
628bce99 1414 if (!@move_uploaded_file($_FILES['attachfile']['tmp_name'],$localfilename)) {
1415 if (!@rename($_FILES['attachfile']['tmp_name'], $localfilename)) {
73ad81bf 1416 return true;
1417 }
a61878d0 1418 }
a43e4b90 1419 $message = $compose_messages[$session];
0b97a708 1420 $type = strtolower($_FILES['attachfile']['type']);
1421 $name = $_FILES['attachfile']['name'];
628bce99 1422 $message->initAttachment($type, $name, $localfilename);
a43e4b90 1423 $compose_messages[$session] = $message;
b0314f04 1424 sqsession_register($compose_messages , 'compose_messages');
4c9d2242 1425}
1426
0a2c3218 1427/* parse values like 8M and 2k into bytes */
1428function getByteSize($ini_size) {
1429
4d30dc83 1430 if(!$ini_size) {
1431 return FALSE;
1432 }
da95c4b6 1433
0a2c3218 1434 $ini_size = trim($ini_size);
1435
5b9716de 1436 // if there's some kind of letter at the end of the string we need to multiply.
1437 if(!is_numeric(substr($ini_size, -1))) {
1438
1439 switch(strtoupper(substr($ini_size, -1))) {
1440 case 'G':
73ad81bf 1441 $bytesize = 1073741824;
1442 break;
5b9716de 1443 case 'M':
73ad81bf 1444 $bytesize = 1048576;
1445 break;
5b9716de 1446 case 'K':
73ad81bf 1447 $bytesize = 1024;
1448 break;
5b9716de 1449 }
1450
4d30dc83 1451 return ($bytesize * (int)substr($ini_size, 0, -1));
0a2c3218 1452 }
1c044820 1453
4d30dc83 1454 return $ini_size;
0a2c3218 1455}
a43e4b90 1456
4c9d2242 1457
50706f77 1458/**
1459 * temporary function to make use of the deliver class.
a42c236f 1460 * In the future the responsible backend should be automaticly loaded
50706f77 1461 * and conf.pl should show a list of available backends.
1462 * The message also should be constructed by the message class.
73ad81bf 1463 */
b7ff469f 1464function deliverMessage($composeMessage, $draft=false) {
a43e4b90 1465 global $send_to, $send_to_cc, $send_to_bcc, $mailprio, $subject, $body,
40e07136 1466 $username, $identity, $idents, $data_dir,
856e58ef 1467 $request_mdn, $request_dr, $default_charset, $useSendmail,
1468 $domain, $action, $default_move_to_sent, $move_to_sent,
1469 $imapServerAddress, $imapPort, $sent_folder, $key;
a43e4b90 1470
1471 $rfc822_header = $composeMessage->rfc822_header;
24192f77 1472
1473 $abook = addressbook_init(false, true);
310dfeb6 1474 $rfc822_header->to = $rfc822_header->parseAddress($send_to,true, array(), '', $domain, array(&$abook,'lookup'));
1475 $rfc822_header->cc = $rfc822_header->parseAddress($send_to_cc,true,array(), '',$domain, array(&$abook,'lookup'));
1476 $rfc822_header->bcc = $rfc822_header->parseAddress($send_to_bcc,true, array(), '',$domain, array(&$abook,'lookup'));
a43e4b90 1477 $rfc822_header->priority = $mailprio;
1478 $rfc822_header->subject = $subject;
310dfeb6 1479
a43e4b90 1480 $special_encoding='';
1481 if (strtolower($default_charset) == 'iso-2022-jp') {
1482 if (mb_detect_encoding($body) == 'ASCII') {
a91189d6 1483 $special_encoding = '8bit';
a43e4b90 1484 } else {
1485 $body = mb_convert_encoding($body, 'JIS');
1486 $special_encoding = '7bit';
1487 }
1488 }
1489 $composeMessage->setBody($body);
1490
a43e4b90 1491 $reply_to = '';
1e2a6ff6 1492 $reply_to = $idents[$identity]['reply_to'];
40e07136 1493
1494 $from_addr = build_from_header($identity);
1495 $rfc822_header->from = $rfc822_header->parseAddress($from_addr,true);
a43e4b90 1496 if ($reply_to) {
73ad81bf 1497 $rfc822_header->reply_to = $rfc822_header->parseAddress($reply_to,true);
a43e4b90 1498 }
1499 /* Receipt: On Read */
1500 if (isset($request_mdn) && $request_mdn) {
40e07136 1501 $rfc822_header->dnt = $rfc822_header->parseAddress($from_addr,true);
762853f4 1502 } elseif (isset($rfc822_header->dnt)) {
1503 unset($rfc822_header->dnt);
a43e4b90 1504 }
1638beb6 1505
a43e4b90 1506 /* Receipt: On Delivery */
1507 if (isset($request_dr) && $request_dr) {
40e07136 1508 $rfc822_header->more_headers['Return-Receipt-To'] = $from->mailbox.'@'.$from->domain;
762853f4 1509 } elseif (isset($rfc822_header->more_headers['Return-Receipt-To'])) {
1510 unset($rfc822_header->more_headers['Return-Receipt-To']);
a43e4b90 1511 }
762853f4 1512
a43e4b90 1513 /* multipart messages */
1514 if (count($composeMessage->entities)) {
1515 $message_body = new Message();
a91189d6 1516 $message_body->body_part = $composeMessage->body_part;
1517 $composeMessage->body_part = '';
1518 $mime_header = new MessageHeader;
1519 $mime_header->type0 = 'text';
1520 $mime_header->type1 = 'plain';
1521 if ($special_encoding) {
1522 $mime_header->encoding = $special_encoding;
1c044820 1523 } else {
12a0ed01 1524 $mime_header->encoding = '8bit';
a91189d6 1525 }
1526 if ($default_charset) {
1527 $mime_header->parameters['charset'] = $default_charset;
1528 }
1c044820 1529 $message_body->mime_header = $mime_header;
a43e4b90 1530 array_unshift($composeMessage->entities, $message_body);
a91189d6 1531 $content_type = new ContentType('multipart/mixed');
a43e4b90 1532 } else {
1e2026df 1533 $content_type = new ContentType('text/plain');
1534 if ($special_encoding) {
1535 $rfc822_header->encoding = $special_encoding;
1c044820 1536 } else {
1e2026df 1537 $rfc822_header->encoding = '8bit';
1c044820 1538 }
426e0b72 1539 if ($default_charset) {
1540 $content_type->properties['charset']=$default_charset;
73ad81bf 1541 }
181538ac 1542 }
1c044820 1543
a43e4b90 1544 $rfc822_header->content_type = $content_type;
1545 $composeMessage->rfc822_header = $rfc822_header;
1638beb6 1546
1c044820 1547 /* Here you can modify the message structure just before we hand
6e515418 1548 it over to deliver; plugin authors note that $composeMessage
1549 is sent and modified by reference since 1.5.2 */
1550 do_hook('compose_send', $composeMessage);
a43e4b90 1551
b48d3c53 1552 if (!$useSendmail && !$draft) {
a91189d6 1553 require_once(SM_PATH . 'class/deliver/Deliver_SMTP.class.php');
1554 $deliver = new Deliver_SMTP();
ce68b76b 1555 global $smtpServerAddress, $smtpPort, $pop_before_smtp;
a91189d6 1556
a91189d6 1557 $authPop = (isset($pop_before_smtp) && $pop_before_smtp) ? true : false;
9bd3b1e6 1558 get_smtp_user($user, $pass);
a91189d6 1559 $stream = $deliver->initStream($composeMessage,$domain,0,
73ad81bf 1560 $smtpServerAddress, $smtpPort, $user, $pass, $authPop);
b48d3c53 1561 } elseif (!$draft) {
73ad81bf 1562 require_once(SM_PATH . 'class/deliver/Deliver_SendMail.class.php');
fd7ab795 1563 global $sendmail_path, $sendmail_args;
f3dc9c62 1564 // Check for outdated configuration
1565 if (!isset($sendmail_args)) {
1566 if ($sendmail_path=='/var/qmail/bin/qmail-inject') {
1567 $sendmail_args = '';
1568 } else {
1569 $sendmail_args = '-i -t';
1570 }
1571 }
fd7ab795 1572 $deliver = new Deliver_SendMail(array('sendmail_args'=>$sendmail_args));
73ad81bf 1573 $stream = $deliver->initStream($composeMessage,$sendmail_path);
b48d3c53 1574 } elseif ($draft) {
73ad81bf 1575 global $draft_folder;
1576 require_once(SM_PATH . 'class/deliver/Deliver_IMAP.class.php');
906f7e9f 1577 $imap_stream = sqimap_login($username, false, $imapServerAddress,
73ad81bf 1578 $imapPort, 0);
1579 if (sqimap_mailbox_exists ($imap_stream, $draft_folder)) {
1580 require_once(SM_PATH . 'class/deliver/Deliver_IMAP.class.php');
1581 $imap_deliver = new Deliver_IMAP();
1582 $length = $imap_deliver->mail($composeMessage);
1583 sqimap_append ($imap_stream, $draft_folder, $length);
1584 $imap_deliver->mail($composeMessage, $imap_stream);
1585 sqimap_append_done ($imap_stream, $draft_folder);
1586 sqimap_logout($imap_stream);
1587 unset ($imap_deliver);
c077ffeb 1588 $composeMessage->purgeAttachments();
73ad81bf 1589 return $length;
4dfb9db7 1590 } else {
fd7ab795 1591 $msg = '<br />'.sprintf(_("Error: Draft folder %s does not exist."), htmlspecialchars($draft_folder));
cb34dbd0 1592 plain_error_message($msg);
73ad81bf 1593 return false;
a91189d6 1594 }
a43e4b90 1595 }
0c59bbe1 1596 $success = false;
a43e4b90 1597 if ($stream) {
a91189d6 1598 $length = $deliver->mail($composeMessage, $stream);
0c59bbe1 1599 $success = $deliver->finalizeStream($stream);
a43e4b90 1600 }
0c59bbe1 1601 if (!$success) {
fd7ab795 1602 // $deliver->dlv_server_msg is not always server's reply
6c3d00b5 1603 $msg = _("Message not sent.") . "<br />\n" .
1604 $deliver->dlv_msg;
a15f9d93 1605 if (!empty($deliver->dlv_server_msg)) {
1606 // add 'server replied' part only when it is not empty.
1607 // Delivery error can be generated by delivery class itself
1608 $msg.='<br />' .
1609 _("Server replied:") . ' ' . $deliver->dlv_ret_nr . ' ' .
1610 nl2br(htmlspecialchars($deliver->dlv_server_msg));
1611 }
cb34dbd0 1612 plain_error_message($msg);
a43e4b90 1613 } else {
1614 unset ($deliver);
20152d80 1615 $move_to_sent = getPref($data_dir,$username,'move_to_sent');
906f7e9f 1616 $imap_stream = sqimap_login($username, false, $imapServerAddress, $imapPort, 0);
e4a1f097 1617
1618 /* Move to sent code */
1619 if (isset($default_move_to_sent) && ($default_move_to_sent != 0)) {
1620 $svr_allow_sent = true;
1621 } else {
1622 $svr_allow_sent = false;
1623 }
1624
1c044820 1625 if (isset($sent_folder) && (($sent_folder != '') || ($sent_folder != 'none'))
73ad81bf 1626 && sqimap_mailbox_exists( $imap_stream, $sent_folder)) {
e4a1f097 1627 $fld_sent = true;
1628 } else {
1629 $fld_sent = false;
1630 }
1631
1632 if ((isset($move_to_sent) && ($move_to_sent != 0)) || (!isset($move_to_sent))) {
1633 $lcl_allow_sent = true;
1634 } else {
1635 $lcl_allow_sent = false;
1636 }
1637
856e58ef 1638 global $passed_id, $mailbox;
e4a1f097 1639 if (($fld_sent && $svr_allow_sent && !$lcl_allow_sent) || ($fld_sent && $lcl_allow_sent)) {
eceefdfe 1640 if ($action == 'reply' || $action == 'reply_all') {
1641 $save_reply_with_orig=getPref($data_dir,$username,'save_reply_with_orig');
1642 if ($save_reply_with_orig) {
1643 $sent_folder = $mailbox;
1644 }
1645 }
e4a1f097 1646 sqimap_append ($imap_stream, $sent_folder, $length);
a91189d6 1647 require_once(SM_PATH . 'class/deliver/Deliver_IMAP.class.php');
1648 $imap_deliver = new Deliver_IMAP();
1649 $imap_deliver->mail($composeMessage, $imap_stream);
e4a1f097 1650 sqimap_append_done ($imap_stream, $sent_folder);
a91189d6 1651 unset ($imap_deliver);
1652 }
8780308f 1653
856e58ef 1654 global $what, $iAccount, $startMessage;
8780308f 1655
c077ffeb 1656 $composeMessage->purgeAttachments();
bc29bf70 1657 if ($action=='reply' || $action=='reply_all' || $action=='forward' || $action=='forward_as_attachment') {
202bcbcc 1658 require(SM_PATH . 'functions/mailbox_display.php');
8780308f 1659 $aMailbox = sqm_api_mailbox_select($imap_stream, $iAccount, $mailbox,array('setindex' => $what, 'offset' => $startMessage),array());
bc29bf70 1660 switch($action) {
1661 case 'reply':
1662 case 'reply_all':
1663 // check if we are allowed to set the \\Answered flag
1664 if (in_array('\\answered',$aMailbox['PERMANENTFLAGS'], true)) {
1665 $aUpdatedMsgs = sqimap_toggle_flag($imap_stream, array($passed_id), '\\Answered', true, false);
1666 if (isset($aUpdatedMsgs[$passed_id]['FLAGS'])) {
1667 /**
1668 * Only update the cached headers if the header is
1669 * cached.
1670 */
1671 if (isset($aMailbox['MSG_HEADERS'][$passed_id])) {
1672 $aMailbox['MSG_HEADERS'][$passed_id]['FLAGS'] = $aMsg['FLAGS'];
1673 }
1674 }
1675 }
1676 break;
1677 case 'forward':
1678 case 'forward_as_attachment':
1679 // check if we are allowed to set the $Forwarded flag (RFC 4550 paragraph 2.8)
1638beb6 1680 if (in_array('$forwarded',$aMailbox['PERMANENTFLAGS'], true) ||
bc29bf70 1681 in_array('\\*',$aMailbox['PERMANENTFLAGS'])) {
1682
1683 $aUpdatedMsgs = sqimap_toggle_flag($imap_stream, array($passed_id), '$Forwarded', true, false);
1684 if (isset($aUpdatedMsgs[$passed_id]['FLAGS'])) {
1685 if (isset($aMailbox['MSG_HEADERS'][$passed_id])) {
1686 $aMailbox['MSG_HEADERS'][$passed_id]['FLAGS'] = $aMsg['FLAGS'];
1687 }
bda07b93 1688 }
8780308f 1689 }
bc29bf70 1690 break;
8780308f 1691 }
bc29bf70 1692
4d1cb59a 1693 /**
1694 * Write mailbox with updated seen flag information back to cache.
1695 */
bc29bf70 1696 if(isset($aUpdatedMsgs[$passed_id])) {
1697 $mailbox_cache[$iAccount.'_'.$aMailbox['NAME']] = $aMailbox;
1698 sqsession_register($mailbox_cache,'mailbox_cache');
1699 }
1700
1701 sqimap_logout($imap_stream);
a91189d6 1702 }
a43e4b90 1703 }
0c59bbe1 1704 return $success;
a43e4b90 1705}