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