Removing hook that, AFAIK is only used to add output to compose screen; other hooks...
[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
876fdb60 386if (empty($mailbox)) {
00793a25 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
762853f4 1307 if ($compose_new_win=='1') {
1308 $oTemplate->display('compose_newwin_close.tpl');
1309 }
1638beb6 1310
762853f4 1311 $oErrorHandler->setDelayedErrors(false);
5c4ff7bf 1312 $oTemplate->display('footer.tpl');
48985d59 1313}
1314
1315
70c4fd84 1316function showComposeButtonRow() {
78a35fcd 1317 global $use_javascript_addr_book, $save_as_draft,
73ad81bf 1318 $default_use_priority, $mailprio, $default_use_mdn,
1319 $request_mdn, $request_dr,
1320 $data_dir, $username;
70c4fd84 1321
762853f4 1322 global $oTemplate, $buffer_hook;
1638beb6 1323
1324 if ($default_use_priority) {
762853f4 1325 $priorities = array('1'=>_("High"), '3'=>_("Normal"), '5'=>_("Low"));
1326 $priority = isset($mailprio) ? $mailprio : 3;
1327 } else {
1328 $priorities = array();
1329 $priority = NULL;
ae25968c 1330 }
1638beb6 1331
ae25968c 1332 $mdn_user_support=getPref($data_dir, $username, 'mdn_user_support',$default_use_mdn);
48985d59 1333
c40a269e 1334 if ($use_javascript_addr_book && checkForJavascript()) {
1335 $addr_book = addButton(_("Addresses"), null, array('onclick' => 'javascript:open_abook();'));
734f4ee6 1336 } else {
c40a269e 1337 $addr_book = addSubmit(_("Addresses"), 'html_addr_search');
78a35fcd 1338 }
48985d59 1339
762853f4 1340 $oTemplate->assign('allow_priority', $default_use_priority==1);
1341 $oTemplate->assign('priority_list', $priorities);
1342 $oTemplate->assign('current_priority', $priority);
1638beb6 1343
762853f4 1344 $oTemplate->assign('notifications_enabled', $mdn_user_support==1);
1345 $oTemplate->assign('read_receipt', $request_mdn=='1');
1346 $oTemplate->assign('delivery_receipt', $request_dr=='1');
1638beb6 1347
762853f4 1348 $oTemplate->assign('drafts_enabled', $save_as_draft);
1349 $oTemplate->assign('address_book_button', $addr_book);
441f2d33 1350
762853f4 1351 $oTemplate->display('compose_buttons.tpl');
78a35fcd 1352}
b278172f 1353
70c4fd84 1354function checkInput ($show) {
78a35fcd 1355 /*
1356 * I implemented the $show variable because the error messages
1357 * were getting sent before the page header. So, I check once
1358 * using $show=false, and then when i'm ready to display the error
1359 * message, show=true
1360 */
c6f28eb1 1361 global $send_to, $send_to_bcc;
78a35fcd 1362
6bf2a88f 1363 if ($send_to == '' && $send_to_bcc == '') {
78a35fcd 1364 if ($show) {
cb34dbd0 1365 plain_error_message(_("You have not filled in the \"To:\" field."));
78a35fcd 1366 }
1367 return false;
1368 }
1369 return true;
1370} /* function checkInput() */
df15de21 1371
3806fa52 1372
00793a25 1373/* True if FAILURE */
da95c4b6 1374function saveAttachedFiles($session) {
628bce99 1375 global $compose_messages;
bfa54da7 1376
45cdd1b5 1377 /* get out of here if no file was attached at all */
1378 if (! is_uploaded_file($_FILES['attachfile']['tmp_name']) ) {
1379 return true;
1380 }
1381
628bce99 1382 $localfilename = sq_get_attach_tempfile();
4c9d2242 1383
a42c236f 1384 // m_u_f works better with restricted PHP installs (safe_mode, open_basedir),
1385 // if that doesn't work, try a simple rename.
628bce99 1386 if (!@move_uploaded_file($_FILES['attachfile']['tmp_name'],$localfilename)) {
1387 if (!@rename($_FILES['attachfile']['tmp_name'], $localfilename)) {
73ad81bf 1388 return true;
1389 }
a61878d0 1390 }
a43e4b90 1391 $message = $compose_messages[$session];
0b97a708 1392 $type = strtolower($_FILES['attachfile']['type']);
1393 $name = $_FILES['attachfile']['name'];
628bce99 1394 $message->initAttachment($type, $name, $localfilename);
a43e4b90 1395 $compose_messages[$session] = $message;
b0314f04 1396 sqsession_register($compose_messages , 'compose_messages');
4c9d2242 1397}
1398
0a2c3218 1399/* parse values like 8M and 2k into bytes */
1400function getByteSize($ini_size) {
1401
4d30dc83 1402 if(!$ini_size) {
1403 return FALSE;
1404 }
da95c4b6 1405
0a2c3218 1406 $ini_size = trim($ini_size);
1407
5b9716de 1408 // if there's some kind of letter at the end of the string we need to multiply.
1409 if(!is_numeric(substr($ini_size, -1))) {
1410
1411 switch(strtoupper(substr($ini_size, -1))) {
1412 case 'G':
73ad81bf 1413 $bytesize = 1073741824;
1414 break;
5b9716de 1415 case 'M':
73ad81bf 1416 $bytesize = 1048576;
1417 break;
5b9716de 1418 case 'K':
73ad81bf 1419 $bytesize = 1024;
1420 break;
5b9716de 1421 }
1422
4d30dc83 1423 return ($bytesize * (int)substr($ini_size, 0, -1));
0a2c3218 1424 }
1c044820 1425
4d30dc83 1426 return $ini_size;
0a2c3218 1427}
a43e4b90 1428
4c9d2242 1429
50706f77 1430/**
1431 * temporary function to make use of the deliver class.
a42c236f 1432 * In the future the responsible backend should be automaticly loaded
50706f77 1433 * and conf.pl should show a list of available backends.
1434 * The message also should be constructed by the message class.
73ad81bf 1435 */
b7ff469f 1436function deliverMessage($composeMessage, $draft=false) {
a43e4b90 1437 global $send_to, $send_to_cc, $send_to_bcc, $mailprio, $subject, $body,
73ad81bf 1438 $username, $popuser, $usernamedata, $identity, $idents, $data_dir,
1439 $request_mdn, $request_dr, $default_charset, $color, $useSendmail,
1440 $domain, $action, $default_move_to_sent, $move_to_sent;
a43e4b90 1441 global $imapServerAddress, $imapPort, $sent_folder, $key;
1442
1443 $rfc822_header = $composeMessage->rfc822_header;
24192f77 1444
1445 $abook = addressbook_init(false, true);
310dfeb6 1446 $rfc822_header->to = $rfc822_header->parseAddress($send_to,true, array(), '', $domain, array(&$abook,'lookup'));
1447 $rfc822_header->cc = $rfc822_header->parseAddress($send_to_cc,true,array(), '',$domain, array(&$abook,'lookup'));
1448 $rfc822_header->bcc = $rfc822_header->parseAddress($send_to_bcc,true, array(), '',$domain, array(&$abook,'lookup'));
a43e4b90 1449 $rfc822_header->priority = $mailprio;
1450 $rfc822_header->subject = $subject;
310dfeb6 1451
a43e4b90 1452 $special_encoding='';
1453 if (strtolower($default_charset) == 'iso-2022-jp') {
1454 if (mb_detect_encoding($body) == 'ASCII') {
a91189d6 1455 $special_encoding = '8bit';
a43e4b90 1456 } else {
1457 $body = mb_convert_encoding($body, 'JIS');
1458 $special_encoding = '7bit';
1459 }
1460 }
1461 $composeMessage->setBody($body);
1462
1463 if (ereg("^([^@%/]+)[@%/](.+)$", $username, $usernamedata)) {
73ad81bf 1464 $popuser = $usernamedata[1];
1465 $domain = $usernamedata[2];
1466 unset($usernamedata);
a43e4b90 1467 } else {
73ad81bf 1468 $popuser = $username;
a43e4b90 1469 }
1470 $reply_to = '';
1e2a6ff6 1471 $from_mail = $idents[$identity]['email_address'];
1472 $full_name = $idents[$identity]['full_name'];
1473 $reply_to = $idents[$identity]['reply_to'];
9ca455db 1474 if (!$from_mail) {
73ad81bf 1475 $from_mail = "$popuser@$domain";
045714fd 1476 }
1477 $rfc822_header->from = $rfc822_header->parseAddress($from_mail,true);
1478 if ($full_name) {
9783f396 1479 $from = $rfc822_header->from[0];
a91189d6 1480 if (!$from->host) $from->host = $domain;
12a0ed01 1481 $full_name_encoded = encodeHeader($full_name);
1482 if ($full_name_encoded != $full_name) {
1483 $from_addr = $full_name_encoded .' <'.$from->mailbox.'@'.$from->host.'>';
1484 } else {
1485 $from_addr = '"'.$full_name .'" <'.$from->mailbox.'@'.$from->host.'>';
1486 }
045714fd 1487 $rfc822_header->from = $rfc822_header->parseAddress($from_addr,true);
a43e4b90 1488 }
a43e4b90 1489 if ($reply_to) {
73ad81bf 1490 $rfc822_header->reply_to = $rfc822_header->parseAddress($reply_to,true);
a43e4b90 1491 }
1492 /* Receipt: On Read */
1493 if (isset($request_mdn) && $request_mdn) {
73ad81bf 1494 $rfc822_header->dnt = $rfc822_header->parseAddress($from_mail,true);
762853f4 1495 } elseif (isset($rfc822_header->dnt)) {
1496 unset($rfc822_header->dnt);
a43e4b90 1497 }
1638beb6 1498
a43e4b90 1499 /* Receipt: On Delivery */
1500 if (isset($request_dr) && $request_dr) {
73ad81bf 1501 $rfc822_header->more_headers['Return-Receipt-To'] = $from_mail;
762853f4 1502 } elseif (isset($rfc822_header->more_headers['Return-Receipt-To'])) {
1503 unset($rfc822_header->more_headers['Return-Receipt-To']);
a43e4b90 1504 }
762853f4 1505
a43e4b90 1506 /* multipart messages */
1507 if (count($composeMessage->entities)) {
1508 $message_body = new Message();
a91189d6 1509 $message_body->body_part = $composeMessage->body_part;
1510 $composeMessage->body_part = '';
1511 $mime_header = new MessageHeader;
1512 $mime_header->type0 = 'text';
1513 $mime_header->type1 = 'plain';
1514 if ($special_encoding) {
1515 $mime_header->encoding = $special_encoding;
1c044820 1516 } else {
12a0ed01 1517 $mime_header->encoding = '8bit';
a91189d6 1518 }
1519 if ($default_charset) {
1520 $mime_header->parameters['charset'] = $default_charset;
1521 }
1c044820 1522 $message_body->mime_header = $mime_header;
a43e4b90 1523 array_unshift($composeMessage->entities, $message_body);
a91189d6 1524 $content_type = new ContentType('multipart/mixed');
a43e4b90 1525 } else {
1e2026df 1526 $content_type = new ContentType('text/plain');
1527 if ($special_encoding) {
1528 $rfc822_header->encoding = $special_encoding;
1c044820 1529 } else {
1e2026df 1530 $rfc822_header->encoding = '8bit';
1c044820 1531 }
426e0b72 1532 if ($default_charset) {
1533 $content_type->properties['charset']=$default_charset;
73ad81bf 1534 }
181538ac 1535 }
1c044820 1536
a43e4b90 1537 $rfc822_header->content_type = $content_type;
1538 $composeMessage->rfc822_header = $rfc822_header;
1638beb6 1539
1c044820 1540 /* Here you can modify the message structure just before we hand
6e515418 1541 it over to deliver; plugin authors note that $composeMessage
1542 is sent and modified by reference since 1.5.2 */
1543 do_hook('compose_send', $composeMessage);
a43e4b90 1544
b48d3c53 1545 if (!$useSendmail && !$draft) {
a91189d6 1546 require_once(SM_PATH . 'class/deliver/Deliver_SMTP.class.php');
1547 $deliver = new Deliver_SMTP();
ce68b76b 1548 global $smtpServerAddress, $smtpPort, $pop_before_smtp;
a91189d6 1549
a91189d6 1550 $authPop = (isset($pop_before_smtp) && $pop_before_smtp) ? true : false;
9bd3b1e6 1551 get_smtp_user($user, $pass);
a91189d6 1552 $stream = $deliver->initStream($composeMessage,$domain,0,
73ad81bf 1553 $smtpServerAddress, $smtpPort, $user, $pass, $authPop);
b48d3c53 1554 } elseif (!$draft) {
73ad81bf 1555 require_once(SM_PATH . 'class/deliver/Deliver_SendMail.class.php');
fd7ab795 1556 global $sendmail_path, $sendmail_args;
f3dc9c62 1557 // Check for outdated configuration
1558 if (!isset($sendmail_args)) {
1559 if ($sendmail_path=='/var/qmail/bin/qmail-inject') {
1560 $sendmail_args = '';
1561 } else {
1562 $sendmail_args = '-i -t';
1563 }
1564 }
fd7ab795 1565 $deliver = new Deliver_SendMail(array('sendmail_args'=>$sendmail_args));
73ad81bf 1566 $stream = $deliver->initStream($composeMessage,$sendmail_path);
b48d3c53 1567 } elseif ($draft) {
73ad81bf 1568 global $draft_folder;
1569 require_once(SM_PATH . 'class/deliver/Deliver_IMAP.class.php');
906f7e9f 1570 $imap_stream = sqimap_login($username, false, $imapServerAddress,
73ad81bf 1571 $imapPort, 0);
1572 if (sqimap_mailbox_exists ($imap_stream, $draft_folder)) {
1573 require_once(SM_PATH . 'class/deliver/Deliver_IMAP.class.php');
1574 $imap_deliver = new Deliver_IMAP();
1575 $length = $imap_deliver->mail($composeMessage);
1576 sqimap_append ($imap_stream, $draft_folder, $length);
1577 $imap_deliver->mail($composeMessage, $imap_stream);
1578 sqimap_append_done ($imap_stream, $draft_folder);
1579 sqimap_logout($imap_stream);
1580 unset ($imap_deliver);
c077ffeb 1581 $composeMessage->purgeAttachments();
73ad81bf 1582 return $length;
4dfb9db7 1583 } else {
fd7ab795 1584 $msg = '<br />'.sprintf(_("Error: Draft folder %s does not exist."), htmlspecialchars($draft_folder));
cb34dbd0 1585 plain_error_message($msg);
73ad81bf 1586 return false;
a91189d6 1587 }
a43e4b90 1588 }
0c59bbe1 1589 $success = false;
a43e4b90 1590 if ($stream) {
a91189d6 1591 $length = $deliver->mail($composeMessage, $stream);
0c59bbe1 1592 $success = $deliver->finalizeStream($stream);
a43e4b90 1593 }
0c59bbe1 1594 if (!$success) {
fd7ab795 1595 // $deliver->dlv_server_msg is not always server's reply
6c3d00b5 1596 $msg = _("Message not sent.") . "<br />\n" .
1597 $deliver->dlv_msg;
a15f9d93 1598 if (!empty($deliver->dlv_server_msg)) {
1599 // add 'server replied' part only when it is not empty.
1600 // Delivery error can be generated by delivery class itself
1601 $msg.='<br />' .
1602 _("Server replied:") . ' ' . $deliver->dlv_ret_nr . ' ' .
1603 nl2br(htmlspecialchars($deliver->dlv_server_msg));
1604 }
cb34dbd0 1605 plain_error_message($msg);
a43e4b90 1606 } else {
1607 unset ($deliver);
20152d80 1608 $move_to_sent = getPref($data_dir,$username,'move_to_sent');
906f7e9f 1609 $imap_stream = sqimap_login($username, false, $imapServerAddress, $imapPort, 0);
e4a1f097 1610
1611 /* Move to sent code */
1612 if (isset($default_move_to_sent) && ($default_move_to_sent != 0)) {
1613 $svr_allow_sent = true;
1614 } else {
1615 $svr_allow_sent = false;
1616 }
1617
1c044820 1618 if (isset($sent_folder) && (($sent_folder != '') || ($sent_folder != 'none'))
73ad81bf 1619 && sqimap_mailbox_exists( $imap_stream, $sent_folder)) {
e4a1f097 1620 $fld_sent = true;
1621 } else {
1622 $fld_sent = false;
1623 }
1624
1625 if ((isset($move_to_sent) && ($move_to_sent != 0)) || (!isset($move_to_sent))) {
1626 $lcl_allow_sent = true;
1627 } else {
1628 $lcl_allow_sent = false;
1629 }
1630
1631 if (($fld_sent && $svr_allow_sent && !$lcl_allow_sent) || ($fld_sent && $lcl_allow_sent)) {
eceefdfe 1632 global $passed_id, $mailbox, $action;
1633 if ($action == 'reply' || $action == 'reply_all') {
1634 $save_reply_with_orig=getPref($data_dir,$username,'save_reply_with_orig');
1635 if ($save_reply_with_orig) {
1636 $sent_folder = $mailbox;
1637 }
1638 }
e4a1f097 1639 sqimap_append ($imap_stream, $sent_folder, $length);
a91189d6 1640 require_once(SM_PATH . 'class/deliver/Deliver_IMAP.class.php');
1641 $imap_deliver = new Deliver_IMAP();
1642 $imap_deliver->mail($composeMessage, $imap_stream);
e4a1f097 1643 sqimap_append_done ($imap_stream, $sent_folder);
a91189d6 1644 unset ($imap_deliver);
1645 }
8780308f 1646
1647 global $passed_id, $mailbox, $action, $what, $iAccount,$startMessage;
1648
c077ffeb 1649 $composeMessage->purgeAttachments();
bc29bf70 1650 if ($action=='reply' || $action=='reply_all' || $action=='forward' || $action=='forward_as_attachment') {
202bcbcc 1651 require(SM_PATH . 'functions/mailbox_display.php');
8780308f 1652 $aMailbox = sqm_api_mailbox_select($imap_stream, $iAccount, $mailbox,array('setindex' => $what, 'offset' => $startMessage),array());
bc29bf70 1653 switch($action) {
1654 case 'reply':
1655 case 'reply_all':
1656 // check if we are allowed to set the \\Answered flag
1657 if (in_array('\\answered',$aMailbox['PERMANENTFLAGS'], true)) {
1658 $aUpdatedMsgs = sqimap_toggle_flag($imap_stream, array($passed_id), '\\Answered', true, false);
1659 if (isset($aUpdatedMsgs[$passed_id]['FLAGS'])) {
1660 /**
1661 * Only update the cached headers if the header is
1662 * cached.
1663 */
1664 if (isset($aMailbox['MSG_HEADERS'][$passed_id])) {
1665 $aMailbox['MSG_HEADERS'][$passed_id]['FLAGS'] = $aMsg['FLAGS'];
1666 }
1667 }
1668 }
1669 break;
1670 case 'forward':
1671 case 'forward_as_attachment':
1672 // check if we are allowed to set the $Forwarded flag (RFC 4550 paragraph 2.8)
1638beb6 1673 if (in_array('$forwarded',$aMailbox['PERMANENTFLAGS'], true) ||
bc29bf70 1674 in_array('\\*',$aMailbox['PERMANENTFLAGS'])) {
1675
1676 $aUpdatedMsgs = sqimap_toggle_flag($imap_stream, array($passed_id), '$Forwarded', true, false);
1677 if (isset($aUpdatedMsgs[$passed_id]['FLAGS'])) {
1678 if (isset($aMailbox['MSG_HEADERS'][$passed_id])) {
1679 $aMailbox['MSG_HEADERS'][$passed_id]['FLAGS'] = $aMsg['FLAGS'];
1680 }
bda07b93 1681 }
8780308f 1682 }
bc29bf70 1683 break;
8780308f 1684 }
bc29bf70 1685
4d1cb59a 1686 /**
1687 * Write mailbox with updated seen flag information back to cache.
1688 */
bc29bf70 1689 if(isset($aUpdatedMsgs[$passed_id])) {
1690 $mailbox_cache[$iAccount.'_'.$aMailbox['NAME']] = $aMailbox;
1691 sqsession_register($mailbox_cache,'mailbox_cache');
1692 }
1693
1694 sqimap_logout($imap_stream);
a91189d6 1695 }
a43e4b90 1696 }
0c59bbe1 1697 return $success;
a43e4b90 1698}
6e515418 1699?>