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