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