rg=0 fixes; ReplyCitation fix, Draft save fix
[squirrelmail.git] / src / compose.php
CommitLineData
59177427 1<?php
895905c0 2
35586184 3/**
4 * compose.php
5 *
15e6162e 6 * Copyright (c) 1999-2002 The SquirrelMail Project Team
35586184 7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * This code sends a mail.
10 *
11 * There are 4 modes of operation:
12 * - Start new mail
13 * - Add an attachment
14 * - Send mail
15 * - Save As Draft
16 *
17 * $Id$
18 */
f7fb20fe 19
86725763 20/* Path for SquirrelMail required files. */
21define('SM_PATH','../');
22
23/* SquirrelMail required files. */
08185f2a 24require_once(SM_PATH . 'include/validate.php');
86725763 25require_once(SM_PATH . 'functions/imap.php');
26require_once(SM_PATH . 'functions/date.php');
27require_once(SM_PATH . 'functions/mime.php');
86725763 28require_once(SM_PATH . 'functions/plugin.php');
29require_once(SM_PATH . 'functions/display_messages.php');
30require_once(SM_PATH . 'class/deliver/Deliver.class.php');
91f2085b 31
0b97a708 32/* --------------------- Get globals ------------------------------------- */
33$username = $_SESSION['username'];
34$onetimepad = $_SESSION['onetimepad'];
35$base_uri = $_SESSION['base_uri'];
36$delimiter = $_SESSION['delimiter'];
37
38if (isset($_POST['return'])) {
39 $html_addr_search_done = 'Use Addresses';
40}
41if ( isset($_SESSION['composesession']) ) {
42 $composesession = $_SESSION['composesession'];
43}
fe369c70 44sqextractGlobalVar('action');
0b97a708 45sqextractGlobalVar('session');
46sqextractGlobalVar('mailbox');
47sqextractGlobalVar('identity');
48sqextractGlobalVar('send_to');
49sqextractGlobalVar('send_to_cc');
50sqextractGlobalVar('send_to_bcc');
51sqextractGlobalVar('subject');
52sqextractGlobalVar('body');
53sqextractGlobalVar('mailprio');
54sqextractGlobalVar('request_mdn');
55sqextractGlobalVar('request_dr');
56sqextractGlobalVar('html_addr_search');
57sqextractGlobalVar('mail_sent');
58sqextractGlobalVar('passed_id');
fe369c70 59sqextractGlobalVar('passed_ent_id');
4dfb9db7 60sqextractGlobalVar('send');
0b97a708 61
62if ( isset($_POST['sigappend']) ) {
63 $sigappend = $_POST['sigappend'];
64}
65/* From addressbook search */
66if ( isset($_POST['from_htmladdr_search']) ) {
67 $from_htmladdr_search = $_POST['from_htmladdr_search'];
68}
69if ( isset($_POST['addr_search_done']) ) {
70 $html_addr_search_done = $_POST['addr_search_done'];
71}
72if ( isset($_POST['send_to_search']) ) {
73 $send_to_search = &$_POST['send_to_search'];
74}
75
76/* Attachments */
77sqextractGlobalVar('attach');
78if ( isset($_POST['do_delete']) ) {
79 $do_delete = $_POST['do_delete'];
80}
81if ( isset($_POST['delete']) ) {
82 $delete = &$_POST['delete'];
83}
4dfb9db7 84if ( isset($_SESSION['compose_messages']) ) {
85 $compose_messages = &$_SESSION['compose_messages'];
0b97a708 86}
87
4dfb9db7 88
0b97a708 89/* Forward message as attachment */
90if ( isset($_GET['attachedmessages']) ) {
91 $attachedmessages = $_GET['attachedmessages'];
92}
93
94/* Drafts */
95sqextractGlobalVar('draft');
96sqextractGlobalVar('draft_id');
97sqextractGlobalVar('ent_num');
98sqextractGlobalVar('saved_draft');
99sqextractGlobalVar('delete_draft');
100
101$key = $_COOKIE['key'];
102
09044055 103/* --------------------- Specific Functions ------------------------------ */
0b97a708 104
41b94d65 105function replyAllString($header) {
106 global $include_self_reply_all, $username, $data_dir;
107 $excl_arr = array();
108 /**
109 * 1) Remove the addresses we'll be sending the message 'to'
110 */
111 $url_replytoall_avoid_addrs = '';
112 if (isset($header->replyto)) {
113 $excl_ar = $header->getAddr_a('replyto');
114 }
115 /**
116 * 2) Remove our identities from the CC list (they still can be in the
117 * TO list) only if $include_self_reply_all is turned off
118 */
119 if (!$include_self_reply_all) {
120 $email_address = trim(getPref($data_dir, $username, 'email_address'));
121 $excl_ar[$email_address] = '';
122
123 $idents = getPref($data_dir, $username, 'identities');
124 if ($idents != '' && $idents > 1) {
125 for ($i = 1; $i < $idents; $i ++) {
126 $cur_email_address = getPref($data_dir, $username,
127 'email_address' . $i);
128 $cur_email_address = strtolower($cur_email_address);
a61878d0 129 $excl_ar[$cur_email_address] = '';
41b94d65 130 }
131 }
132 }
133
134 /**
135 * 3) get the addresses.
136 */
137 $url_replytoall_ar = $header->getAddr_a(array('to','cc'), $excl_ar);
138
139 /**
140 * 4) generate the string.
141 */
142 $url_replytoallcc = '';
143 foreach( $url_replytoall_ar as $email => $personal) {
144 if ($personal) {
a61878d0 145 $url_replytoallcc .= ", \"$personal\" <$email>";
41b94d65 146 } else {
a61878d0 147 $url_replytoallcc .= ', '. $email;
41b94d65 148 }
149 }
150 $url_replytoallcc = substr($url_replytoallcc,2);
151 return $url_replytoallcc;
09044055 152}
153
41b94d65 154function getforwardHeader($orig_header) {
19c6f7a7 155 global $editor_size;
156
a61878d0 157 $display = array( _("Subject") => strlen(_("Subject")),
158 _("From") => strlen(_("From")),
159 _("Date") => strlen(_("Date")),
160 _("To") => strlen(_("To")),
161 _("Cc") => strlen(_("Cc")) );
a45887d7 162 $maxsize = max($display);
163 $indent = str_pad('',$maxsize+2);
164 foreach($display as $key => $val) {
165 $display[$key] = $key .': '. str_pad('', $maxsize - $val);
166 }
a61878d0 167 $bodyTop = str_pad(' '._("Original Message").' ',$editor_size -2,'-',STR_PAD_BOTH) .
168 "\n". $display[_("Subject")] .
169 decodeHeader($orig_header->subject) . "\n" .
170 $display[_("From")] .
171 decodeHeader($orig_header->getAddr_s('from',"\n$indent")) . "\n" .
172 $display[_("Date")] .
173 getLongDateString( $orig_header->date ). "\n" .
174 $display[_("To")] .
175 decodeHeader($orig_header->getAddr_s('to',"\n$indent")) ."\n";
41b94d65 176 if ($orig_header->cc != array() && $orig_header->cc !='') {
c4d02444 177 $bodyTop .= $display[_("Cc")] . decodeHeader($orig_header->getAddr_s('cc',"\n$indent")) . "\n";
41b94d65 178 }
a61878d0 179 $bodyTop .= str_pad('', $editor_size -2 , '-') .
180 "\n";
41b94d65 181 return $bodyTop;
182}
09044055 183/* ----------------------------------------------------------------------- */
184
5628fdde 185/* OLD: should be adapted to composeMessage */
186
44560457 187/*
188 * If the session is expired during a post this restores the compose session
189 * vars.
190 */
191//$session_expired = false;
4dfb9db7 192sqsession_unregister('session_expired_post');
193if (false && session_is_registered('session_expired_post')) {
40934000 194 global $session_expired_post, $session_expired;
195 /*
196 * extra check for username so we don't display previous post data from
197 * another user during this session.
198 */
199 if ($session_expired_post['username'] != $username) {
0b97a708 200 sqsession_unregister('session_expired_post');
201 sqsession_unregister('session_expired');
40934000 202 } else {
203 foreach ($session_expired_post as $postvar => $val) {
204 if (isset($val)) {
205 $$postvar = $val;
206 } else {
207 $$postvar = '';
208 }
209 }
210 if (isset($send)) {
211 unset($send);
212 }
213 $session_expired = true;
214 }
0b97a708 215 sqsession_unregister('session_expired_post');
216 sqsession_unregister('session_expired');
40934000 217 if (!isset($mailbox)) {
218 $mailbox = '';
219 }
4dfb9db7 220
40934000 221 if ($compose_new_win == '1') {
222 compose_Header($color, $mailbox);
223 } else {
224 displayPageHeader($color, $mailbox);
225 }
226 showInputForm($session, false);
227 exit();
44560457 228}
da95c4b6 229if (!isset($composesession)) {
230 $composesession = 0;
a43e4b90 231 sqsession_register(0,'composesession');
da95c4b6 232}
233
d7f8e6e6 234if (!isset($session) || (isset($newmessage) && $newmessage)) {
0b97a708 235 sqsession_unregister('composesession');
da95c4b6 236 $session = "$composesession" +1;
91f2085b 237 $composesession = $session;
a43e4b90 238 sqsession_register($composesession,'composesession');
d7f8e6e6 239}
da95c4b6 240
a43e4b90 241if (!isset($compose_messages)) {
242 $compose_messages = array();
243}
40934000 244if (!isset($compose_messages[$session]) || ($compose_messages[$session] == NULL)) {
245/* if (!array_key_exists($session, $compose_messages)) { /* We can only do this in PHP >= 4.1 */
5628fdde 246 $composeMessage = new Message();
a43e4b90 247 $rfc822_header = new Rfc822Header();
248 $composeMessage->rfc822_header = $rfc822_header;
249 $composeMessage->reply_rfc822_header = '';
250 $compose_messages[$session] = $composeMessage;
5628fdde 251 sqsession_register($compose_messages,'compose_messages');
252} else {
253 $composeMessage=$compose_messages[$session];
a43e4b90 254}
a43e4b90 255
00793a25 256if (!isset($mailbox) || $mailbox == '' || ($mailbox == 'None')) {
257 $mailbox = 'INBOX';
258}
259
4dfb9db7 260if ($draft) {
261 /*
262 * Set $default_charset to correspond with the user's selection
263 * of language interface.
264 */
265 set_my_charset();
266 $composeMessage=$compose_messages[$session];
b48d3c53 267 if (! sendMessage($composeMessage, true)) {
da95c4b6 268 showInputForm($session);
00793a25 269 exit();
734f4ee6 270 } else {
00793a25 271 $draft_message = _("Draft Email Saved");
272 /* If this is a resumed draft, then delete the original */
273 if(isset($delete_draft)) {
7058a2a9 274 Header("Location: delete_message.php?mailbox=" . urlencode($draft_folder) .
fae72101 275 "&message=$delete_draft&sort=$sort&startMessage=1&saved_draft=yes");
00793a25 276 exit();
7058a2a9 277 }
9c3e6cd4 278 else {
279 if ($compose_new_win == '1') {
da95c4b6 280 Header("Location: compose.php?saved_draft=yes&session=$composesession");
a61878d0 281 exit();
9c3e6cd4 282 }
283 else {
a61878d0 284 Header("Location: right_main.php?mailbox=$draft_folder&sort=$sort".
285 "&startMessage=1&note=".urlencode($draft_message));
286 exit();
9c3e6cd4 287 }
00793a25 288 }
289 }
290}
291
4dfb9db7 292if ($send) {
0b97a708 293 if (isset($_FILES['attachfile']) &&
294 $_FILES['attachfile']['tmp_name'] &&
295 $_FILES['attachfile']['tmp_name'] != 'none') {
da95c4b6 296 $AttachFailure = saveAttachedFiles($session);
00793a25 297 }
298 if (checkInput(false) && !isset($AttachFailure)) {
299 $urlMailbox = urlencode (trim($mailbox));
3f6b1b6f 300 if (! isset($passed_id)) {
301 $passed_id = 0;
00793a25 302 }
303 /*
304 * Set $default_charset to correspond with the user's selection
7058a2a9 305 * of language interface.
00793a25 306 */
307 set_my_charset();
00793a25 308 /*
309 * This is to change all newlines to \n
7058a2a9 310 * We'll change them to \r\n later (in the sendMessage function)
00793a25 311 */
312 $body = str_replace("\r\n", "\n", $body);
313 $body = str_replace("\r", "\n", $body);
314
315 /*
316 * Rewrap $body so that no line is bigger than $editor_size
317 * This should only really kick in the sqWordWrap function
f302d704 318 * if the browser doesn't support "VIRTUAL" as the wrap type.
00793a25 319 */
320 $body = explode("\n", $body);
321 $newBody = '';
322 foreach ($body as $line) {
323 if( $line <> '-- ' ) {
324 $line = rtrim($line);
325 }
326 if (strlen($line) <= $editor_size + 1) {
327 $newBody .= $line . "\n";
734f4ee6 328 } else {
e0858036 329 sqWordWrap($line, $editor_size);
330 $newBody .= $line . "\n";
00793a25 331 }
332 }
333 $body = $newBody;
e02775fe 334 do_hook('compose_send');
a43e4b90 335 $composeMessage=$compose_messages[$session];
336 $Result = sendMessage($composeMessage);
00793a25 337 if (! $Result) {
da95c4b6 338 showInputForm($session);
00793a25 339 exit();
340 }
341 if ( isset($delete_draft)) {
7058a2a9 342 Header("Location: delete_message.php?mailbox=" . urlencode( $draft_folder ).
fae72101 343 "&message=$delete_draft&sort=$sort&startMessage=1&mail_sent=yes");
00793a25 344 exit();
345 }
9c3e6cd4 346 if ($compose_new_win == '1') {
d7f8e6e6 347 Header("Location: compose.php?mail_sent=yes");
9c3e6cd4 348 }
349 else {
fae72101 350 Header("Location: right_main.php?mailbox=$urlMailbox&sort=$sort".
351 "&startMessage=1");
9c3e6cd4 352 }
734f4ee6 353 } else {
9c3e6cd4 354 if ($compose_new_win == '1') {
355 compose_Header($color, $mailbox);
356 }
357 else {
358 displayPageHeader($color, $mailbox);
359 }
00793a25 360 if (isset($AttachFailure)) {
361 plain_error_message(_("Could not move/copy file. File not attached"),
362 $color);
363 }
00793a25 364 checkInput(true);
da95c4b6 365 showInputForm($session);
00793a25 366 /* sqimap_logout($imapConnection); */
367 }
e02775fe 368} elseif (isset($html_addr_search_done)) {
9c3e6cd4 369 if ($compose_new_win == '1') {
370 compose_Header($color, $mailbox);
371 }
372 else {
373 displayPageHeader($color, $mailbox);
374 }
00793a25 375
376 if (isset($send_to_search) && is_array($send_to_search)) {
377 foreach ($send_to_search as $k => $v) {
378 if (substr($k, 0, 1) == 'T') {
379 if ($send_to) {
380 $send_to .= ', ';
381 }
382 $send_to .= $v;
383 }
384 elseif (substr($k, 0, 1) == 'C') {
385 if ($send_to_cc) {
386 $send_to_cc .= ', ';
387 }
388 $send_to_cc .= $v;
389 }
390 elseif (substr($k, 0, 1) == 'B') {
391 if ($send_to_bcc) {
392 $send_to_bcc .= ', ';
393 }
394 $send_to_bcc .= $v;
395 }
396 }
397 }
da95c4b6 398 showInputForm($session);
e02775fe 399} elseif (isset($html_addr_search)) {
0b97a708 400 if (isset($_FILES['attachfile']) &&
401 $_FILES['attachfile']['tmp_name'] &&
402 $_FILES['attachfile']['tmp_name'] != 'none') {
403 if(saveAttachedFiles($session)) {
00793a25 404 plain_error_message(_("Could not move/copy file. File not attached"), $color);
405 }
406 }
407 /*
408 * I am using an include so as to elminiate an extra unnecessary
409 * click. If you can think of a better way, please implement it.
410 */
411 include_once('./addrbook_search_html.php');
e02775fe 412} elseif (isset($attach)) {
da95c4b6 413 if (saveAttachedFiles($session)) {
00793a25 414 plain_error_message(_("Could not move/copy file. File not attached"), $color);
415 }
9c3e6cd4 416 if ($compose_new_win == '1') {
417 compose_Header($color, $mailbox);
418 }
419 else {
420 displayPageHeader($color, $mailbox);
421 }
da95c4b6 422 showInputForm($session);
01265fba 423}
424elseif (isset($sigappend)) {
425 $idents = getPref($data_dir, $username, 'identities', 0);
426 if ($idents > 1) {
427 if ($identity == 'default') {
428 $no = 'g';
429 } else {
430 $no = $identity;
431 }
432 $signature = getSig($data_dir, $username, $no);
433 }
434 $body .= "\n\n".($prefix_sig==true? "-- \n":'').$signature;
435 if ($compose_new_win == '1') {
436 compose_Header($color, $mailbox);
437 } else {
438 displayPageHeader($color, $mailbox);
439 }
da95c4b6 440 showInputForm($session);
e02775fe 441} elseif (isset($do_delete)) {
9c3e6cd4 442 if ($compose_new_win == '1') {
443 compose_Header($color, $mailbox);
444 }
445 else {
446 displayPageHeader($color, $mailbox);
447 }
00793a25 448
00793a25 449 if (isset($delete) && is_array($delete)) {
a43e4b90 450 $composeMessage = $compose_messages[$session];
00793a25 451 foreach($delete as $index) {
a43e4b90 452 $attached_file = $composeMessage->entities[$index]->att_local_name;
a61878d0 453 unlink ($attached_file);
a43e4b90 454 unset ($composeMessage->entities[$index]);
00793a25 455 }
a43e4b90 456 $new_entities = array();
457 foreach ($composeMessage->entities as $entity) {
458 $new_entities[] = $entity;
459 }
460 $composeMessage->entities = $new_entities;
461 $compose_messages[$session] = $composeMessage;
462 sqsession_register($compose_messages, 'compose_messages');
463// setPref($data_dir, $username, 'attachments', serialize($attachments));
00793a25 464 }
da95c4b6 465 showInputForm($session);
734f4ee6 466} else {
00793a25 467 /*
468 * This handles the default case as well as the error case
469 * (they had the same code) --> if (isset($smtpErrors))
470 */
44560457 471
472 if ($compose_new_win == '1') {
473 compose_Header($color, $mailbox);
474 } else {
475 displayPageHeader($color, $mailbox);
476 }
00793a25 477
478 $newmail = true;
479
a61878d0 480 if (!isset($passed_ent_id)) {
481 $passed_ent_id = '';
482 }
483 if (!isset($passed_id)) {
484 $passed_id = '';
485 }
486 if (!isset($mailbox)) {
487 $mailbox = '';
488 }
489 if (!isset($action)) {
490 $action = '';
491 }
41b94d65 492
44560457 493 $values = newMail($mailbox,$passed_id,$passed_ent_id, $action, $session);
b9928adc 494
495 /* in case the origin is not read_body.php */
496 if (isset($send_to)) {
497 $values['send_to'] = $send_to;
498 }
499 if (isset($send_to_cc)) {
44560457 500 $values['send_to_cc'] = $send_to_cc;
b9928adc 501 }
502 if (isset($send_to_bcc)) {
44560457 503 $values['send_to_bcc'] = $send_to_bcc;
b9928adc 504 }
41b94d65 505 showInputForm($session, $values);
00793a25 506}
507
508exit();
509
00793a25 510/**************** Only function definitions go below *************/
511
512
48985d59 513/* This function is used when not sending or adding attachments */
44560457 514function newMail ($mailbox='', $passed_id='', $passed_ent_id='', $action='', $session='') {
91f2085b 515 global $editor_size, $default_use_priority, $body,
44560457 516 $use_signature, $composesession, $data_dir, $username,
a43e4b90 517 $username, $key, $imapServerAddress, $imapPort, $compose_messages,
518 $composeMessage;
e7f1a81d 519
91f2085b 520 $send_to = $send_to_cc = $send_to_bcc = $subject = $identity = '';
bdb92db3 521 $mailprio = 3;
44560457 522
41b94d65 523 if ($passed_id) {
44560457 524 $imapConnection = sqimap_login($username, $key, $imapServerAddress,
a61878d0 525 $imapPort, 0);
526
48985d59 527 sqimap_mailbox_select($imapConnection, $mailbox);
41b94d65 528 $message = sqimap_get_message($imapConnection, $passed_id, $mailbox);
a43e4b90 529
a61878d0 530 $body = '';
531 if ($passed_ent_id) {
532 /* redefine the messsage in case of message/rfc822 */
533 $message = $message->getEntity($passed_ent_id);
534 /* message is an entity which contains the envelope and type0=message
535 * and type1=rfc822. The actual entities are childs from
536 * $message->entities[0]. That's where the encoding and is located
537 */
538
539 $entities = $message->entities[0]->findDisplayEntity
540 (array(), $alt_order = array('text/plain'));
541 if (!count($entities)) {
542 $entities = $message->entities[0]->findDisplayEntity
543 (array(), $alt_order = array('text/plain','html/plain'));
544 }
545 $orig_header = $message->rfc822_header; /* here is the envelope located */
546 /* redefine the message for picking up the attachments */
547 $message = $message->entities[0];
548
549 } else {
550 $entities = $message->findDisplayEntity (array(), $alt_order = array('text/plain'));
551 if (!count($entities)) {
552 $entities = $message->findDisplayEntity (array(), $alt_order = array('text/plain','html/plain'));
553 }
554 $orig_header = $message->rfc822_header;
555 }
a43e4b90 556
41b94d65 557 $encoding = $message->header->encoding;
a61878d0 558 $type0 = $message->type0;
559 $type1 = $message->type1;
41b94d65 560 foreach ($entities as $ent) {
a61878d0 561 $unencoded_bodypart = mime_fetch_body($imapConnection, $passed_id, $ent);
562 $body_part_entity = $message->getEntity($ent);
563 $bodypart = decodeBody($unencoded_bodypart,
564 $body_part_entity->header->encoding);
565 if ($type1 == 'html') {
566 $bodypart = strip_tags($bodypart);
567 }
568 $body .= $bodypart;
569 }
570 if ($default_use_priority) {
571 $mailprio = substr($orig_header->priority,0,1);
572 if (!$mailprio) {
573 $mailprio = 3;
574 }
575 } else {
576 $mailprio = '';
577 }
a43e4b90 578 //ClearAttachments($session);
bdb92db3 579
580 $identity = '';
581 $idents = getPref($data_dir, $username, 'identities');
a45887d7 582 $from_o = $orig_header->from;
bdb92db3 583 if (is_object($from_o)) {
584 $orig_from = $from_o->getAddress();
585 } else {
586 $orig_from = '';
a61878d0 587 }
bdb92db3 588 if (!empty($idents) && $idents > 1) {
a61878d0 589 for ($i = 1; $i < $idents; $i++) {
590 $enc_from_name = '"'.
591 getPref($data_dir,
592 $username,
593 'full_name' . $i) .
594 '" <' . getPref($data_dir, $username,
595 'email_address' . $i) . '>';
596 if ($enc_from_name == $orig_from) {
597 $identity = $i;
598 break;
599 }
600 }
bdb92db3 601 }
a61878d0 602
603 switch ($action) {
604 case ('draft'):
605 $use_signature = FALSE;
c3d1d2cf 606 $send_to = decodeHeader($orig_header->getAddr_s('to'));
607 $send_to_cc = decodeHeader($orig_header->getAddr_s('cc'));
608 $send_to_bcc = decodeHeader($orig_header->getAddr_s('bcc'));
a61878d0 609 $subject = decodeHeader($orig_header->subject);
610
611 $body_ary = explode("\n", $body);
612 $cnt = count($body_ary) ;
613 $body = '';
614 for ($i=0; $i < $cnt; $i++) {
615 if (!ereg("^[>\\s]*$", $body_ary[$i]) || !$body_ary[$i]) {
616 sqWordWrap($body_ary[$i], $editor_size );
617 $body .= $body_ary[$i] . "\n";
618 }
619 unset($body_ary[$i]);
620 }
621 sqUnWordWrap($body);
a43e4b90 622 $composeMessage = getAttachments($message, $composeMessage, $passed_id, $entities, $imapConnection);
a61878d0 623 break;
a45887d7 624 case ('edit_as_new'):
c3d1d2cf 625 $send_to = decodeHeader($orig_header->getAddr_s('to'));
626 $send_to_cc = decodeHeader($orig_header->getAddr_s('cc'));
627 $send_to_bcc = decodeHeader($orig_header->getAddr_s('bcc'));
a61878d0 628 $subject = decodeHeader($orig_header->subject);
629 $mailprio = $orig_header->priority;
630 $orig_from = '';
a43e4b90 631 $composeMessage = getAttachments($message, $composeMessage, $passed_id, $entities, $imapConnection);
a61878d0 632 sqUnWordWrap($body);
633 break;
634 case ('forward'):
635 $send_to = '';
636 $subject = decodeHeader($orig_header->subject);
637 if ((substr(strtolower($subject), 0, 4) != 'fwd:') &&
638 (substr(strtolower($subject), 0, 5) != '[fwd:') &&
639 (substr(strtolower($subject), 0, 6) != '[ fwd:')) {
640 $subject = '[Fwd: ' . $subject . ']';
641 }
642 $body = getforwardHeader($orig_header) . $body;
643 sqUnWordWrap($body);
a43e4b90 644 $composeMessage = getAttachments($message, $composeMessage, $passed_id, $entities, $imapConnection);
a61878d0 645 break;
646 case ('forward_as_attachment'):
a43e4b90 647 $composeMessage = getMessage_RFC822_Attachment($message, $composeMessage, $passed_id, $passed_ent_id, $imapConnection);
a61878d0 648 $body = '';
649 break;
a45887d7 650 case ('reply_all'):
a61878d0 651 $send_to_cc = replyAllString($orig_header);
652 case ('reply'):
653 $send_to = $orig_header->reply_to;
654 if (is_object($send_to)) {
c3d1d2cf 655 $send_to = decodeHeader($send_to->getAddr_s('reply_to'));
a61878d0 656 } else {
c3d1d2cf 657 $send_to = decodeHeader($orig_header->getAddr_s('from'));
a61878d0 658 }
659 $subject = decodeHeader($orig_header->subject);
660 $subject = str_replace('"', "'", $subject);
661 $subject = trim($subject);
662 if (substr(strtolower($subject), 0, 3) != 're:') {
663 $subject = 'Re: ' . $subject;
664 }
665 /* this corrects some wrapping/quoting problems on replies */
666 $rewrap_body = explode("\n", $body);
667
4dfb9db7 668 $from = (is_array($orig_header->from)) ?
669 $orig_header->from[0] : $orig_header->from;
670 $body = getReplyCitation($from->getAddress(false));
671
a61878d0 672 $cnt = count($rewrap_body);
673 for ($i=0;$i<$cnt;$i++) {
4dfb9db7 674// sqWordWrap($rewrap_body[$i], ($editor_size - 2));
a61878d0 675 if (preg_match("/^(>+)/", $rewrap_body[$i], $matches)) {
676 $gt = $matches[1];
677 $body .= '>' . str_replace("\n", "\n$gt ", $rewrap_body[$i]) ."\n";
678 } else {
679 $body .= '> ' . $rewrap_body[$i] . "\n";
680 }
681 unset($rewrap_body[$i]);
682 }
a43e4b90 683 $composeMessage->reply_rfc822_header = $orig_header;
a61878d0 684 break;
685 default:
686 break;
41b94d65 687 }
a43e4b90 688 $compose_messages[$session] = $composeMessage;
689 sqsession_register($compose_messages, 'compose_messages');
690
a61878d0 691 sqimap_logout($imapConnection);
41b94d65 692 }
a61878d0 693 $ret = array( 'send_to' => $send_to,
694 'send_to_cc' => $send_to_cc,
695 'send_to_bcc' => $send_to_bcc,
696 'subject' => $subject,
697 'mailprio' => $mailprio,
698 'body' => $body,
699 'identity' => $identity );
700
41b94d65 701 return ($ret);
48985d59 702} /* function newMail() */
703
a43e4b90 704function getAttachments($message, &$composeMessage, $passed_id, $entities, $imapConnection) {
59edcad6 705 global $attachment_dir, $username, $data_dir, $squirrelmail_language;
48985d59 706 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
41b94d65 707 if (!count($message->entities) ||
708 ($message->type0 == 'message' && $message->type1 == 'rfc822')) {
709 if ( !in_array($message->entity_id, $entities) && $message->entity_id) {
a43e4b90 710 switch ($message->type0) {
711 case 'message':
712 if ($message->type1 == 'rfc822') {
713 $filename = decodeHeader($message->rfc822_header->subject.'.eml');
714 if ($filename == "") {
715 $filename = "untitled-".$message->entity_id.'.eml';
716 }
717 } else {
718 $filename = decodeHeader($message->getFilename());
719 }
720 break;
721 default:
722 $filename = decodeHeader($message->getFilename());
723 break;
724 }
725 if (isset($languages[$squirrelmail_language]['XTRA_CODE']) &&
726 function_exists($languages[$squirrelmail_language]['XTRA_CODE'])) {
727 $filename = $languages[$squirrelmail_language]['XTRA_CODE']('encode', $filename);
728 }
729
730 $localfilename = GenerateRandomString(32, '', 7);
731 $full_localfilename = "$hashed_attachment_dir/$localfilename";
732 while (file_exists($full_localfilename)) {
733 $localfilename = GenerateRandomString(32, '', 7);
734 $full_localfilename = "$hashed_attachment_dir/$localfilename";
735 }
736 $message->att_local_name = $full_localfilename;
737 if (!$message->mime_header) { /* temporary hack */
738 $message->mime_header = $message->header;
739 }
740
741 $composeMessage->addEntity($message);
742
743 /* Write Attachment to file */
744 $fp = fopen ("$hashed_attachment_dir/$localfilename", 'wb');
745 fputs($fp, decodeBody(mime_fetch_body($imapConnection,
746 $passed_id, $message->entity_id),
747 $message->header->encoding));
748 fclose ($fp);
48985d59 749 }
734f4ee6 750 } else {
a43e4b90 751 for ($i=0, $entCount=count($message->entities); $i<$entCount;$i++) {
752 $composeMessage=getAttachments($message->entities[$i], $composeMessage, $passed_id, $entities, $imapConnection);
48985d59 753 }
754 }
a43e4b90 755// setPref($data_dir, $username, 'attachments', serialize($attachments));
756 return $composeMessage;
48985d59 757}
758
a43e4b90 759function getMessage_RFC822_Attachment($message, $composeMessage, $passed_id,
756406df 760 $passed_ent_id='', $imapConnection) {
a6ec592e 761 global $attachments, $attachment_dir, $username, $data_dir, $uid_support;
762 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
756406df 763 if (!$passed_ent_id) {
a61878d0 764 $body_a = sqimap_run_command($imapConnection,
765 'FETCH '.$passed_id.' RFC822',
766 TRUE, $response, $readmessage,
767 $uid_support);
756406df 768 } else {
769 $body_a = sqimap_run_command($imapConnection,
a61878d0 770 'FETCH '.$passed_id.' BODY['.$passed_ent_id.']',
771 TRUE, $response, $readmessage, $uid_support);
772 $message = $message->parent;
756406df 773 }
a6ec592e 774 if ($response = 'OK') {
a61878d0 775 $subject = encodeHeader($message->rfc822_header->subject);
776 array_shift($body_a);
777 $body = implode('', $body_a) . "\r\n";
778
779 $localfilename = GenerateRandomString(32, 'FILE', 7);
780 $full_localfilename = "$hashed_attachment_dir/$localfilename";
781
a43e4b90 782 $fp = fopen( $full_localfilename, 'w');
a61878d0 783 fwrite ($fp, $body);
784 fclose($fp);
a43e4b90 785 $composeMessage->initAttachment('message/rfc822',$subject.'.eml',
786 $full_localfilename);
787 }
788 return $composeMessage;
a6ec592e 789}
790
41b94d65 791function showInputForm ($session, $values=false) {
792 global $send_to, $send_to_cc, $body,
48985d59 793 $passed_body, $color, $use_signature, $signature, $prefix_sig,
794 $editor_size, $attachments, $subject, $newmail,
41b94d65 795 $use_javascript_addr_book, $send_to_bcc, $passed_id, $mailbox,
48985d59 796 $from_htmladdr_search, $location_of_buttons, $attachment_dir,
797 $username, $data_dir, $identity, $draft_id, $delete_draft,
9c3e6cd4 798 $mailprio, $default_use_mdn, $mdn_user_support, $compose_new_win,
44560457 799 $saved_draft, $mail_sent, $sig_first, $edit_as_new, $action,
a43e4b90 800 $username, $compose_messages;
801
802 $composeMessage = $compose_messages[$session];
48985d59 803
3b487216 804 $subject = decodeHeader($subject, false);
41b94d65 805 if ($values) {
806 $send_to = $values['send_to'];
807 $send_to_cc = $values['send_to_cc'];
808 $send_to_bcc = $values['send_to_bcc'];
809 $subject = $values['subject'];
810 $mailprio = $values['mailprio'];
811 $body = $values['body'];
bdb92db3 812 $identity = $values['identity'];
41b94d65 813 }
814
48985d59 815 if ($use_javascript_addr_book) {
816 echo "\n". '<SCRIPT LANGUAGE=JavaScript><!--' . "\n" .
817 'function open_abook() { ' . "\n" .
818 ' var nwin = window.open("addrbook_popup.php","abookpopup",' .
819 '"width=670,height=300,resizable=yes,scrollbars=yes");' . "\n" .
820 ' if((!nwin.opener) && (document.windows != null))' . "\n" .
821 ' nwin.opener = document.windows;' . "\n" .
822 "}\n" .
823 '// --></SCRIPT>' . "\n\n";
824 }
825
41b94d65 826 echo "\n" . '<FORM name=compose action="compose.php" METHOD=POST ' .
827 'ENCTYPE="multipart/form-data"';
48985d59 828 do_hook("compose_form");
57257333 829
48985d59 830 echo ">\n";
831
41b94d65 832 if ($action == 'draft') {
833 echo '<input type="hidden" name="delete_draft" value="' . $passed_id . "\">\n";
48985d59 834 }
835 if (isset($delete_draft)) {
836 echo '<input type="hidden" name="delete_draft" value="' . $delete_draft. "\">\n";
837 }
da95c4b6 838 if (isset($session)) {
44560457 839 echo '<input type="hidden" name="session" value="' . $session . "\">\n";
da95c4b6 840 }
08bad2b1 841
842 if (isset($passed_id)) {
843 echo '<input type="hidden" name="passed_id" value="' . $passed_id . "\">\n";
844 }
44560457 845
9c3e6cd4 846 if ($saved_draft == 'yes') {
847 echo '<BR><CENTER><B>'. _("Draft Saved").'</CENTER></B>';
848 }
849 if ($mail_sent == 'yes') {
850 echo '<BR><CENTER><B>'. _("Your Message has been sent").'</CENTER></B>';
851 }
6206f6c4 852 echo '<TABLE ALIGN=center CELLSPACING=0 BORDER=0>' . "\n";
9c3e6cd4 853 if ($compose_new_win == '1') {
a61878d0 854 echo '<TABLE ALIGN=CENTER BGCOLOR="'.$color[0].'" WIDTH="100%" BORDER=0>'."\n" .
855 ' <TR><TD></TD><TD ALIGN="RIGHT"><INPUT TYPE="BUTTON" NAME="Close" onClick="return self.close()" VALUE='._("Close").'></TD></TR>'."\n";
9c3e6cd4 856 }
78a35fcd 857 if ($location_of_buttons == 'top') {
858 showComposeButtonRow();
859 }
48985d59 860
715225af 861 $idents = getPref($data_dir, $username, 'identities', 0);
862 if ($idents > 1) {
41b94d65 863 echo ' <TR>' . "\n" .
864 ' <TD BGCOLOR="' . $color[4] . '" WIDTH="10%" ALIGN=RIGHT>' .
865 "\n" .
866 _("From:") .
867 ' </TD><TD BGCOLOR="' . $color[4] . '" WIDTH="90%">' . "\n" .
48985d59 868 '<select name=identity>' . "\n" .
869 '<option value=default>' .
870 htmlspecialchars(getPref($data_dir, $username, 'full_name'));
871 $em = getPref($data_dir, $username, 'email_address');
872 if ($em != '') {
248bfebb 873 echo htmlspecialchars(' <' . $em . '>') . "\n";
48985d59 874 }
875 for ($i = 1; $i < $idents; $i ++) {
248bfebb 876 echo '<option value="' . $i . '"';
48985d59 877 if (isset($identity) && $identity == $i) {
78a35fcd 878 echo ' SELECTED';
48985d59 879 }
880 echo '>' . htmlspecialchars(getPref($data_dir, $username,
881 'full_name' . $i));
248bfebb 882 $em = getPref($data_dir, $username, 'email_address' . $i);
48985d59 883 if ($em != '') {
78a35fcd 884 echo htmlspecialchars(' <' . $em . '>') . "\n";
48985d59 885 }
9f599fe3 886 echo '</option>';
48985d59 887 }
888 echo '</select>' . "\n" .
41b94d65 889 ' </TD>' . "\n" .
890 ' </TR>' . "\n";
891 }
892 echo ' <TR>' . "\n" .
893 ' <TD BGCOLOR="' . $color[4] . '" WIDTH="10%" ALIGN=RIGHT>' . "\n" .
894 _("To:") .
895 ' </TD><TD BGCOLOR="' . $color[4] . '" WIDTH="90%">' . "\n" .
896 ' <INPUT TYPE=text NAME="send_to" VALUE="' .
897 htmlspecialchars($send_to) . '" SIZE=60><BR>' . "\n" .
898 ' </TD>' . "\n" .
899 ' </TR>' . "\n" .
900 ' <TR>' . "\n" .
901 ' <TD BGCOLOR="' . $color[4] . '" ALIGN=RIGHT>' . "\n" .
902 _("CC:") .
903 ' </TD><TD BGCOLOR="' . $color[4] . '" ALIGN=LEFT>' . "\n" .
904 ' <INPUT TYPE=text NAME="send_to_cc" SIZE=60 VALUE="' .
905 htmlspecialchars($send_to_cc) . '"><BR>' . "\n" .
906 ' </TD>' . "\n" .
907 ' </TR>' . "\n" .
908 ' <TR>' . "\n" .
909 ' <TD BGCOLOR="' . $color[4] . '" ALIGN=RIGHT>' . "\n" .
910 _("BCC:") .
911 ' </TD><TD BGCOLOR="' . $color[4] . '" ALIGN=LEFT>' . "\n" .
912 ' <INPUT TYPE=text NAME="send_to_bcc" VALUE="' .
913 htmlspecialchars($send_to_bcc) . '" SIZE=60><BR>' . "\n" .
914 '</TD></TR>' . "\n" .
915 ' <TR>' . "\n" .
916 ' <TD BGCOLOR="' . $color[4] . '" ALIGN=RIGHT>' . "\n" .
917 _("Subject:") .
918 ' </TD><TD BGCOLOR="' . $color[4] . '" ALIGN=LEFT>' . "\n";
919 echo ' <INPUT TYPE=text NAME=subject SIZE=60 VALUE="' .
920 htmlspecialchars($subject) . '">';
921 echo '</td></tr>' . "\n\n";
48985d59 922
78a35fcd 923 if ($location_of_buttons == 'between') {
924 showComposeButtonRow();
925 }
4dfb9db7 926
fdc83c55 927 if ($compose_new_win == '1') {
41b94d65 928 echo ' <TR>' . "\n" .
929 ' <TD BGCOLOR="' . $color[0] . '" COLSPAN=2 ALIGN=CENTER>' . "\n" .
930 ' <TEXTAREA NAME=body ROWS=20 COLS="' .
931 $editor_size . '" WRAP="VIRTUAL">';
fdc83c55 932 }
933 else {
41b94d65 934 echo ' <TR>' . "\n" .
935 ' <TD BGCOLOR="' . $color[4] . '" COLSPAN=2>' . "\n" .
936 ' &nbsp;&nbsp;<TEXTAREA NAME=body ROWS=20 COLS="' .
937 $editor_size . '" WRAP="VIRTUAL">';
fdc83c55 938 }
48985d59 939 if ($use_signature == true && $newmail == true && !isset($from_htmladdr_search)) {
3b17e952 940 if ($sig_first == '1') {
83be314a 941 if ($charset == 'iso-2022-jp') {
942 echo "\n\n".($prefix_sig==true? "-- \n":'').mb_convert_encoding($signature, 'EUC-JP');
943 } else {
3b17e952 944 echo "\n\n".($prefix_sig==true? "-- \n":'').htmlspecialchars($signature);
83be314a 945 }
3b17e952 946 echo "\n\n".htmlspecialchars($body);
947 }
948 else {
949 echo "\n\n".htmlspecialchars($body);
83be314a 950 if ($charset == 'iso-2022-jp') {
951 echo "\n\n".($prefix_sig==true? "-- \n":'').mb_convert_encoding($signature, 'EUC-JP');
952 }else{
3b17e952 953 echo "\n\n".($prefix_sig==true? "-- \n":'').htmlspecialchars($signature);
954 }
955 }
83be314a 956 }
3b17e952 957 else {
958 echo htmlspecialchars($body);
48985d59 959 }
41b94d65 960 echo '</TEXTAREA><BR>' . "\n" .
961 ' </TD>' . "\n" .
962 ' </TR>' . "\n";
48985d59 963
964 if ($location_of_buttons == 'bottom') {
965 showComposeButtonRow();
966 } else {
a61878d0 967 echo ' <TR><TD COLSPAN=2 ALIGN=RIGHT>' .
968 ' <INPUT TYPE=SUBMIT NAME=send VALUE="' . _("Send") . '">' .
969 ' &nbsp;&nbsp;&nbsp;&nbsp;<BR><BR>' .
970 ' </TD></TR>' . "\n";
48985d59 971 }
46bb8da8 972
48985d59 973 /* This code is for attachments */
a61878d0 974 echo '<table width="100%" cellpadding="0" cellspacing="4" align="center" border="0">' .
975 ' <tr><td>' .
976 ' <table width="100%" cellpadding="1" cellspacing="0" align="center"'.' border="0" bgcolor="'.$color[9].'">' .
977 ' <tr><td>' .
978 ' <table width="100%" cellpadding="3" cellspacing="0" align="center" border="0">' .
979 ' <TR>' . "\n" .
41b94d65 980 ' <TD VALIGN=MIDDLE ALIGN=RIGHT>' . "\n" .
981 _("Attach:") .
982 ' </TD>' . "\n" .
983 ' <TD VALIGN=MIDDLE ALIGN=LEFT>' . "\n" .
984 ' <INPUT NAME="attachfile" SIZE=48 TYPE="file">' . "\n" .
985 ' &nbsp;&nbsp;<input type="submit" name="attach"' .
986 ' value="' . _("Add") .'">' . "\n" .
987 ' </TD>' . "\n" .
988 ' </TR>' . "\n";
91f2085b 989
41b94d65 990
91f2085b 991 $s_a = array();
4dfb9db7 992 if ($composeMessage->entities) {
993 foreach ($composeMessage->entities as $key => $attachment) {
a43e4b90 994 $attached_file = $attachment->att_local_name;
995 if ($attachment->att_local_name || $attachment->body_part) {
996 $attached_filename = decodeHeader($attachment->mime_header->getParameter('name'));
997 $type = $attachment->mime_header->type0.'/'.
998 $attachment->mime_header->type1;
999 $s_a[] = '<input type="checkbox" name="delete[]" value="' .
1000 $key . "\">\n" . $attached_filename . ' - ' . $type .
1001 ' ('.show_readable_size( filesize( $attached_file ) )
1002 . ')<br>'."\n";
1003 }
4dfb9db7 1004 }
91f2085b 1005 }
1006 if (count($s_a)) {
1007 foreach ($s_a as $s) {
1008 echo '<tr><td align=left colspan="2" bgcolor="' . $color[0] . '">'.$s.'</td></tr>';
a61878d0 1009 }
91f2085b 1010 echo '<tr><td colspan="2"><input type="submit" name="do_delete" value="' .
1011 _("Delete selected attachments") . "\">\n" .
1012 '</td></tr>';
1013 }
a61878d0 1014 echo ' </table></td></tr>' .
1015 ' </table>' .
1016 ' </td></tr>';
41b94d65 1017
41b94d65 1018 /* End of attachment code */
07687736 1019 if ($compose_new_win == '1') {
41b94d65 1020 echo '</TABLE>'."\n";
07687736 1021 }
a61878d0 1022 echo '</TABLE>' . "\n" .
1023 '<input type="hidden" name="username" value="'. $username . "\">\n" .
1024 '<input type=hidden name=action value=' . $action . ">\n" .
1025 '<INPUT TYPE=hidden NAME=mailbox VALUE="' . htmlspecialchars($mailbox) .
4dfb9db7 1026 "\">\n";
1027 echo '</FORM>';
9f599fe3 1028 do_hook('compose_bottom');
48985d59 1029 echo '</BODY></HTML>' . "\n";
1030}
1031
1032
70c4fd84 1033function showComposeButtonRow() {
78a35fcd 1034 global $use_javascript_addr_book, $save_as_draft,
a61878d0 1035 $default_use_priority, $mailprio, $default_use_mdn,
1036 $request_mdn, $request_dr,
1037 $data_dir, $username;
70c4fd84 1038
41b94d65 1039 echo " <TR><TD>\n</TD><TD>\n";
ae25968c 1040 if ($default_use_priority) {
1041 if(!isset($mailprio)) {
1042 $mailprio = "3";
70c4fd84 1043 }
1044 echo _("Priority") .': <select name="mailprio">'.
1045 '<option value="1"'.($mailprio=='1'?' selected':'').'>'. _("High") .'</option>'.
1046 '<option value="3"'.($mailprio=='3'?' selected':'').'>'. _("Normal") .'</option>'.
1047 '<option value="5"'.($mailprio=='5'?' selected':'').'>'. _("Low").'</option>'.
1048 "</select>";
ae25968c 1049 }
1050 $mdn_user_support=getPref($data_dir, $username, 'mdn_user_support',$default_use_mdn);
1051 if ($default_use_mdn) {
70c4fd84 1052 if ($mdn_user_support) {
1053 echo "\n\t". _("Receipt") .': '.
b2a7e5bc 1054 '<input type="checkbox" name="request_mdn" value=1'.
a61878d0 1055 ($request_mdn=='1'?' checked':'') .'>'. _("On Read").
b2a7e5bc 1056 ' <input type="checkbox" name="request_dr" value=1'.
a61878d0 1057 ($request_dr=='1'?' checked':'') .'>'. _("On Delivery");
70c4fd84 1058 }
ae25968c 1059 }
48985d59 1060
a61878d0 1061 echo " </td></tr>\n <TR><td>\n </td><td>\n" .
1062 "\n <INPUT TYPE=SUBMIT NAME=\"sigappend\" VALUE=\"". _("Signature") . "\">\n";
78a35fcd 1063 if ($use_javascript_addr_book) {
46bb8da8 1064 echo " <SCRIPT LANGUAGE=JavaScript><!--\n document.write(\"".
1065 " <input type=button value=\\\""._("Addresses").
1066 "\\\" onclick='javascript:open_abook();'>\");".
1067 " // --></SCRIPT><NOSCRIPT>\n".
1068 " <input type=submit name=\"html_addr_search\" value=\"".
1069 _("Addresses")."\">".
1070 " </NOSCRIPT>\n";
734f4ee6 1071 } else {
78a35fcd 1072 echo " <input type=submit name=\"html_addr_search\" value=\"".
1073 _("Addresses")."\">";
1074 }
48985d59 1075
78a35fcd 1076 if ($save_as_draft) {
1077 echo '<input type="submit" name ="draft" value="' . _("Save Draft") . "\">\n";
1078 }
0a17f9dd 1079
4dfb9db7 1080 echo '<INPUT TYPE=submit NAME=send VALUE="'. _("Send") . "\">\n";
78a35fcd 1081 do_hook('compose_button_row');
441f2d33 1082
41b94d65 1083 echo " </TD></TR>\n\n";
78a35fcd 1084}
b278172f 1085
70c4fd84 1086function checkInput ($show) {
78a35fcd 1087 /*
1088 * I implemented the $show variable because the error messages
1089 * were getting sent before the page header. So, I check once
1090 * using $show=false, and then when i'm ready to display the error
1091 * message, show=true
1092 */
6bf2a88f 1093 global $body, $send_to, $send_to_bcc, $subject, $color;
78a35fcd 1094
6bf2a88f 1095 if ($send_to == '' && $send_to_bcc == '') {
78a35fcd 1096 if ($show) {
0ad7dbda 1097 plain_error_message(_("You have not filled in the \"To:\" field."), $color);
78a35fcd 1098 }
1099 return false;
1100 }
1101 return true;
1102} /* function checkInput() */
df15de21 1103
3806fa52 1104
00793a25 1105/* True if FAILURE */
da95c4b6 1106function saveAttachedFiles($session) {
0b97a708 1107 global $_FILES, $attachment_dir, $attachments, $username,
a43e4b90 1108 $data_dir, $compose_messages;
4c9d2242 1109
45cdd1b5 1110 /* get out of here if no file was attached at all */
1111 if (! is_uploaded_file($_FILES['attachfile']['tmp_name']) ) {
1112 return true;
1113 }
1114
4c9d2242 1115 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
1116 $localfilename = GenerateRandomString(32, '', 7);
1117 $full_localfilename = "$hashed_attachment_dir/$localfilename";
1118 while (file_exists($full_localfilename)) {
1119 $localfilename = GenerateRandomString(32, '', 7);
1120 $full_localfilename = "$hashed_attachment_dir/$localfilename";
1121 }
1122
0b97a708 1123 if (!@rename($_FILES['attachfile']['tmp_name'], $full_localfilename)) {
a43e4b90 1124 if (function_exists("move_uploaded_file")) {
0b97a708 1125 if (!@move_uploaded_file($_FILES['attachfile']['tmp_name'],$full_localfilename)) {
a43e4b90 1126 return true;
1127 }
1128 } else {
0b97a708 1129 if (!@copy($_FILES['attachfile']['tmp_name'], $full_localfilename)) {
a61878d0 1130 return true;
1131 }
a43e4b90 1132 }
a61878d0 1133 }
a43e4b90 1134 $message = $compose_messages[$session];
0b97a708 1135 $type = strtolower($_FILES['attachfile']['type']);
1136 $name = $_FILES['attachfile']['name'];
a43e4b90 1137 $message->initAttachment($type, $name, $full_localfilename);
1138 $compose_messages[$session] = $message;
4c9d2242 1139}
1140
a43e4b90 1141function ClearAttachments($composeMessage) {
b48d3c53 1142 if ($composeMessage->att_local_name) {
1143 $attached_file = $composeMessage->att_local_name;
a43e4b90 1144 if (file_exists($attached_file)) {
1145 unlink($attached_file);
8712abea 1146 }
da95c4b6 1147 }
a43e4b90 1148 for ($i=0, $entCount=count($composeMessage->entities);$i< $entCount; ++$i) {
1149 ClearAttachments($composeMessage->entities[$i]);
1150 }
4c9d2242 1151}
1152
da95c4b6 1153
a43e4b90 1154
1155function getReplyCitation($orig_from) {
4c9d2242 1156 global $reply_citation_style, $reply_citation_start, $reply_citation_end;
1157
1158 /* First, return an empty string when no citation style selected. */
1159 if (($reply_citation_style == '') || ($reply_citation_style == 'none')) {
1160 return '';
1161 }
1162
4c9d2242 1163 /* Make sure our final value isn't an empty string. */
1164 if ($orig_from == '') {
1165 return '';
1166 }
1167
1168 /* Otherwise, try to select the desired citation style. */
1169 switch ($reply_citation_style) {
1170 case 'author_said':
1171 $start = '';
1172 $end = ' ' . _("said") . ':';
1173 break;
1174 case 'quote_who':
1175 $start = '<' . _("quote") . ' ' . _("who") . '="';
1176 $end = '">';
1177 break;
1178 case 'user-defined':
55b321f2 1179 $start = $reply_citation_start .
a61878d0 1180 ($reply_citation_start == '' ? '' : ' ');
4c9d2242 1181 $end = $reply_citation_end;
1182 break;
1183 default:
1184 return '';
1185 }
1186
1187 /* Build and return the citation string. */
1188 return ($start . $orig_from . $end . "\n");
1189}
1190
a43e4b90 1191/* temporary function to make use of the deliver class.
1192 In the future the responsable backend should be automaticly loaded
1193 and conf.pl should show a list of available backends.
1194 The message also should be constructed by the message class.
1195*/
1196
b48d3c53 1197function sendMessage($composeMessage, $draft=false) {
a43e4b90 1198 global $send_to, $send_to_cc, $send_to_bcc, $mailprio, $subject, $body,
1199 $username, $popuser, $usernamedata, $identity, $data_dir,
1200 $request_mdn, $request_dr, $default_charset, $color, $useSendmail,
b48d3c53 1201 $domain, $action;
a43e4b90 1202 global $imapServerAddress, $imapPort, $sent_folder, $key;
1203
1204 $rfc822_header = $composeMessage->rfc822_header;
1205 $rfc822_header->to = $rfc822_header->parseAddress($send_to,true, array(), '', $domain);
1206 $rfc822_header->cc = $rfc822_header->parseAddress($send_to_cc,true,array(), '',$domain);
1207 $rfc822_header->bcc = $rfc822_header->parseAddress($send_to_bcc,true, array(), '',$domain);
1208 $rfc822_header->priority = $mailprio;
1209 $rfc822_header->subject = $subject;
1210 $special_encoding='';
1211 if (strtolower($default_charset) == 'iso-2022-jp') {
1212 if (mb_detect_encoding($body) == 'ASCII') {
1213 $special_encoding = '8bit';
1214 } else {
1215 $body = mb_convert_encoding($body, 'JIS');
1216 $special_encoding = '7bit';
1217 }
1218 }
1219 $composeMessage->setBody($body);
1220
1221 if (ereg("^([^@%/]+)[@%/](.+)$", $username, $usernamedata)) {
1222 $popuser = $usernamedata[1];
1223 $domain = $usernamedata[2];
1224 unset($usernamedata);
1225 } else {
1226 $popuser = $username;
1227 }
1228 $reply_to = '';
1229 if (isset($identity) && $identity != 'default') {
045714fd 1230 $from_mail = getPref($data_dir, $username,'email_address' . $identity);
1231 $full_name = getPref($data_dir, $username,'full_name' . $identity);
1232 $reply_to = getPref($data_dir, $username,'reply_to' . $identity);
a43e4b90 1233 } else {
1234 $from_mail = getPref($data_dir, $username, 'email_address');
1235 $full_name = getPref($data_dir, $username, 'full_name');
a43e4b90 1236 $reply_to = getPref($data_dir, $username,'reply_to');
1237 }
045714fd 1238 if (!$from_mail) {
1239 $from_mail = "$popuser@$domain";
1240 $full_name = '';
1241 }
1242 $rfc822_header->from = $rfc822_header->parseAddress($from_mail,true);
1243 if ($full_name) {
9783f396 1244 $from = $rfc822_header->from[0];
045714fd 1245 if (!$from->host) $from->host = $domain;
1246 $from_addr = $full_name .' <'.$from->mailbox.'@'.$from->host.'>';
1247 $rfc822_header->from = $rfc822_header->parseAddress($from_addr,true);
a43e4b90 1248 }
a43e4b90 1249 if ($reply_to) {
1250 $rfc822_header->reply_to = $rfc822_header->parseAddress($reply_to,true);
1251 }
1252 /* Receipt: On Read */
1253 if (isset($request_mdn) && $request_mdn) {
1254 $rfc822_header->dnt = $rfc822_header->parseAddress($from_mail,true);
1255 }
1256 /* Receipt: On Delivery */
1257 if (isset($request_dr) && $request_dr) {
1258 $rfc822_header->more_headers['Return-Receipt-To'] = $from_mail;
1259 }
1260 /* multipart messages */
1261 if (count($composeMessage->entities)) {
1262 $message_body = new Message();
1263 $message_body->body_part = $composeMessage->body_part;
1264 $composeMessage->body_part = '';
1265 $mime_header = new MessageHeader;
1266 $mime_header->type0 = 'text';
1267 $mime_header->type1 = 'plain';
1268 if ($special_encoding) {
1269 $mime_header->encoding = $special_encoding;
1270 } else {
1271 $mime_header->encoding = 'us-ascii';
1272 }
1273 if ($default_charset) {
1274 $mime_header->parameters['charset'] = $default_charset;
1275 }
1276 $message_body->mime_header = $mime_header;
1277 array_unshift($composeMessage->entities, $message_body);
1278 $content_type = new ContentType('multipart/mixed');
1279 } else {
1280 $content_type = new ContentType('text/plain');
1281 }
1282 if ($default_charset) {
1283 $content_type->properties['charset']=$default_charset;
1284 }
1285
1286 $rfc822_header->content_type = $content_type;
1287 $composeMessage->rfc822_header = $rfc822_header;
1288
b48d3c53 1289 if (!$useSendmail && !$draft) {
86725763 1290 require_once(SM_PATH . 'class/deliver/Deliver_SMTP.class.php');
a43e4b90 1291 $deliver = new Deliver_SMTP();
1292 global $smtpServerAddress, $smtpPort, $use_authenticated_smtp, $pop_before_smtp;
1293 if ($use_authenticated_smtp) {
1294 global $key, $onetimepad;
1295 $user = $username;
1296 $pass = OneTimePadDecrypt($key, $onetimepad);
1297 } else {
1298 $user = '';
1299 $pass = '';
1300 }
b48d3c53 1301 $authPop = (isset($pop_before_smtp) && $pop_before_smtp) ? true : false;
a43e4b90 1302 $stream = $deliver->initStream($composeMessage,$domain,0,
1303 $smtpServerAddress, $smtpPort, $authPop);
b48d3c53 1304 } elseif (!$draft) {
86725763 1305 require_once(SM_PATH . 'class/deliver/Deliver_SendMail.class.php');
b48d3c53 1306 global $sendmail_path;
1307 $deliver = new Deliver_SendMail();
1308 $stream = $deliver->initStream($composeMessage,$sendmail_path);
1309 } elseif ($draft) {
1310 global $draft_folder;
86725763 1311 require_once(SM_PATH . 'class/deliver/Deliver_IMAP.class.php');
b48d3c53 1312 $imap_stream = sqimap_login($username, $key, $imapServerAddress,
1313 $imapPort, 0);
1314 if (sqimap_mailbox_exists ($imap_stream, $draft_folder)) {
4dfb9db7 1315 require_once(SM_PATH . 'class/deliver/Deliver_IMAP.class.php');
1316 $imap_deliver = new Deliver_IMAP();
1317 $length = $imap_deliver->mail($composeMessage);
1318 sqimap_append ($imap_stream, $draft_folder, $length);
1319 $imap_deliver->mail($composeMessage, $imap_stream);
1320 sqimap_append_done ($imap_stream);
1321 sqimap_logout($imap_stream);
1322 unset ($imap_deliver);
1323 return $length;
1324 } else {
1325 $msg = '<br>Error: '._("Draft folder")." $draft_folder" . ' does not exist.';
1326 plain_error_message($msg, $color);
1327 return false;
b48d3c53 1328 }
a43e4b90 1329 }
1330 $succes = false;
1331 if ($stream) {
1332 $length = $deliver->mail($composeMessage, $stream);
1333 $succes = $deliver->finalizeStream($stream);
1334 }
1335 if (!$succes) {
1336 $msg = $deliver->dlv_msg . '<br>Server replied: '.$deliver->dlv_ret_nr;
1337 plain_error_message($msg, $color);
1338 } else {
1339 unset ($deliver);
1340 $imap_stream = sqimap_login($username, $key, $imapServerAddress,
1341 $imapPort, 0);
1342 if (sqimap_mailbox_exists ($imap_stream, $sent_folder)) {
1343 sqimap_append ($imap_stream, $sent_folder, $length);
86725763 1344 require_once(SM_PATH . 'class/deliver/Deliver_IMAP.class.php');
a43e4b90 1345 $imap_deliver = new Deliver_IMAP();
1346 $imap_deliver->mail($composeMessage, $imap_stream);
1347 sqimap_append_done ($imap_stream);
a43e4b90 1348 unset ($imap_deliver);
1349 }
b48d3c53 1350 global $passed_id, $mailbox, $action;
a43e4b90 1351 ClearAttachments($composeMessage);
b48d3c53 1352 if ($action == 'reply' || $action == 'reply_all') {
1353 sqimap_mailbox_select ($imap_stream, $mailbox);
1354 sqimap_messages_flag ($imap_stream, $passed_id, $passed_id, 'Answered', true);
1355 }
1356 sqimap_logout($imap_stream);
a43e4b90 1357 }
1358 return $succes;
1359}
1360
6bf2a88f 1361?>