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