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