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