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