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