rg=0 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');
d7c82551 193if (false && sqsession_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];
d5181a1d 336
a43e4b90 337 $Result = sendMessage($composeMessage);
00793a25 338 if (! $Result) {
da95c4b6 339 showInputForm($session);
00793a25 340 exit();
341 }
342 if ( isset($delete_draft)) {
7058a2a9 343 Header("Location: delete_message.php?mailbox=" . urlencode( $draft_folder ).
fae72101 344 "&message=$delete_draft&sort=$sort&startMessage=1&mail_sent=yes");
00793a25 345 exit();
346 }
9c3e6cd4 347 if ($compose_new_win == '1') {
d7f8e6e6 348 Header("Location: compose.php?mail_sent=yes");
9c3e6cd4 349 }
350 else {
fae72101 351 Header("Location: right_main.php?mailbox=$urlMailbox&sort=$sort".
352 "&startMessage=1");
9c3e6cd4 353 }
734f4ee6 354 } else {
9c3e6cd4 355 if ($compose_new_win == '1') {
356 compose_Header($color, $mailbox);
357 }
358 else {
359 displayPageHeader($color, $mailbox);
360 }
00793a25 361 if (isset($AttachFailure)) {
362 plain_error_message(_("Could not move/copy file. File not attached"),
363 $color);
364 }
00793a25 365 checkInput(true);
da95c4b6 366 showInputForm($session);
00793a25 367 /* sqimap_logout($imapConnection); */
368 }
e02775fe 369} elseif (isset($html_addr_search_done)) {
9c3e6cd4 370 if ($compose_new_win == '1') {
371 compose_Header($color, $mailbox);
372 }
373 else {
374 displayPageHeader($color, $mailbox);
375 }
00793a25 376
377 if (isset($send_to_search) && is_array($send_to_search)) {
378 foreach ($send_to_search as $k => $v) {
379 if (substr($k, 0, 1) == 'T') {
380 if ($send_to) {
381 $send_to .= ', ';
382 }
383 $send_to .= $v;
384 }
385 elseif (substr($k, 0, 1) == 'C') {
386 if ($send_to_cc) {
387 $send_to_cc .= ', ';
388 }
389 $send_to_cc .= $v;
390 }
391 elseif (substr($k, 0, 1) == 'B') {
392 if ($send_to_bcc) {
393 $send_to_bcc .= ', ';
394 }
395 $send_to_bcc .= $v;
396 }
397 }
398 }
da95c4b6 399 showInputForm($session);
e02775fe 400} elseif (isset($html_addr_search)) {
0b97a708 401 if (isset($_FILES['attachfile']) &&
402 $_FILES['attachfile']['tmp_name'] &&
403 $_FILES['attachfile']['tmp_name'] != 'none') {
404 if(saveAttachedFiles($session)) {
00793a25 405 plain_error_message(_("Could not move/copy file. File not attached"), $color);
406 }
407 }
408 /*
409 * I am using an include so as to elminiate an extra unnecessary
410 * click. If you can think of a better way, please implement it.
411 */
412 include_once('./addrbook_search_html.php');
e02775fe 413} elseif (isset($attach)) {
da95c4b6 414 if (saveAttachedFiles($session)) {
00793a25 415 plain_error_message(_("Could not move/copy file. File not attached"), $color);
416 }
9c3e6cd4 417 if ($compose_new_win == '1') {
418 compose_Header($color, $mailbox);
419 }
420 else {
421 displayPageHeader($color, $mailbox);
422 }
da95c4b6 423 showInputForm($session);
01265fba 424}
425elseif (isset($sigappend)) {
426 $idents = getPref($data_dir, $username, 'identities', 0);
427 if ($idents > 1) {
428 if ($identity == 'default') {
429 $no = 'g';
430 } else {
431 $no = $identity;
432 }
433 $signature = getSig($data_dir, $username, $no);
434 }
435 $body .= "\n\n".($prefix_sig==true? "-- \n":'').$signature;
436 if ($compose_new_win == '1') {
437 compose_Header($color, $mailbox);
438 } else {
439 displayPageHeader($color, $mailbox);
440 }
da95c4b6 441 showInputForm($session);
e02775fe 442} elseif (isset($do_delete)) {
9c3e6cd4 443 if ($compose_new_win == '1') {
444 compose_Header($color, $mailbox);
445 }
446 else {
447 displayPageHeader($color, $mailbox);
448 }
00793a25 449
00793a25 450 if (isset($delete) && is_array($delete)) {
a43e4b90 451 $composeMessage = $compose_messages[$session];
00793a25 452 foreach($delete as $index) {
a43e4b90 453 $attached_file = $composeMessage->entities[$index]->att_local_name;
a61878d0 454 unlink ($attached_file);
a43e4b90 455 unset ($composeMessage->entities[$index]);
00793a25 456 }
a43e4b90 457 $new_entities = array();
458 foreach ($composeMessage->entities as $entity) {
459 $new_entities[] = $entity;
460 }
461 $composeMessage->entities = $new_entities;
462 $compose_messages[$session] = $composeMessage;
463 sqsession_register($compose_messages, 'compose_messages');
464// setPref($data_dir, $username, 'attachments', serialize($attachments));
00793a25 465 }
da95c4b6 466 showInputForm($session);
734f4ee6 467} else {
00793a25 468 /*
469 * This handles the default case as well as the error case
470 * (they had the same code) --> if (isset($smtpErrors))
471 */
44560457 472
473 if ($compose_new_win == '1') {
474 compose_Header($color, $mailbox);
475 } else {
476 displayPageHeader($color, $mailbox);
477 }
00793a25 478
479 $newmail = true;
480
a61878d0 481 if (!isset($passed_ent_id)) {
482 $passed_ent_id = '';
483 }
484 if (!isset($passed_id)) {
485 $passed_id = '';
486 }
487 if (!isset($mailbox)) {
488 $mailbox = '';
489 }
490 if (!isset($action)) {
491 $action = '';
492 }
41b94d65 493
44560457 494 $values = newMail($mailbox,$passed_id,$passed_ent_id, $action, $session);
b9928adc 495
496 /* in case the origin is not read_body.php */
497 if (isset($send_to)) {
498 $values['send_to'] = $send_to;
499 }
500 if (isset($send_to_cc)) {
44560457 501 $values['send_to_cc'] = $send_to_cc;
b9928adc 502 }
503 if (isset($send_to_bcc)) {
44560457 504 $values['send_to_bcc'] = $send_to_bcc;
b9928adc 505 }
41b94d65 506 showInputForm($session, $values);
00793a25 507}
508
509exit();
510
00793a25 511/**************** Only function definitions go below *************/
512
513
48985d59 514/* This function is used when not sending or adding attachments */
44560457 515function newMail ($mailbox='', $passed_id='', $passed_ent_id='', $action='', $session='') {
91f2085b 516 global $editor_size, $default_use_priority, $body,
44560457 517 $use_signature, $composesession, $data_dir, $username,
a43e4b90 518 $username, $key, $imapServerAddress, $imapPort, $compose_messages,
519 $composeMessage;
e7f1a81d 520
91f2085b 521 $send_to = $send_to_cc = $send_to_bcc = $subject = $identity = '';
bdb92db3 522 $mailprio = 3;
44560457 523
41b94d65 524 if ($passed_id) {
44560457 525 $imapConnection = sqimap_login($username, $key, $imapServerAddress,
a61878d0 526 $imapPort, 0);
527
48985d59 528 sqimap_mailbox_select($imapConnection, $mailbox);
41b94d65 529 $message = sqimap_get_message($imapConnection, $passed_id, $mailbox);
a43e4b90 530
a61878d0 531 $body = '';
532 if ($passed_ent_id) {
533 /* redefine the messsage in case of message/rfc822 */
534 $message = $message->getEntity($passed_ent_id);
535 /* message is an entity which contains the envelope and type0=message
536 * and type1=rfc822. The actual entities are childs from
537 * $message->entities[0]. That's where the encoding and is located
538 */
539
540 $entities = $message->entities[0]->findDisplayEntity
541 (array(), $alt_order = array('text/plain'));
542 if (!count($entities)) {
543 $entities = $message->entities[0]->findDisplayEntity
544 (array(), $alt_order = array('text/plain','html/plain'));
545 }
546 $orig_header = $message->rfc822_header; /* here is the envelope located */
547 /* redefine the message for picking up the attachments */
548 $message = $message->entities[0];
549
550 } else {
551 $entities = $message->findDisplayEntity (array(), $alt_order = array('text/plain'));
552 if (!count($entities)) {
553 $entities = $message->findDisplayEntity (array(), $alt_order = array('text/plain','html/plain'));
554 }
555 $orig_header = $message->rfc822_header;
556 }
a43e4b90 557
41b94d65 558 $encoding = $message->header->encoding;
a61878d0 559 $type0 = $message->type0;
560 $type1 = $message->type1;
41b94d65 561 foreach ($entities as $ent) {
a61878d0 562 $unencoded_bodypart = mime_fetch_body($imapConnection, $passed_id, $ent);
563 $body_part_entity = $message->getEntity($ent);
564 $bodypart = decodeBody($unencoded_bodypart,
565 $body_part_entity->header->encoding);
566 if ($type1 == 'html') {
567 $bodypart = strip_tags($bodypart);
568 }
569 $body .= $bodypart;
570 }
571 if ($default_use_priority) {
572 $mailprio = substr($orig_header->priority,0,1);
573 if (!$mailprio) {
574 $mailprio = 3;
575 }
576 } else {
577 $mailprio = '';
578 }
a43e4b90 579 //ClearAttachments($session);
bdb92db3 580
581 $identity = '';
582 $idents = getPref($data_dir, $username, 'identities');
a45887d7 583 $from_o = $orig_header->from;
bdb92db3 584 if (is_object($from_o)) {
585 $orig_from = $from_o->getAddress();
586 } else {
587 $orig_from = '';
a61878d0 588 }
bdb92db3 589 if (!empty($idents) && $idents > 1) {
a61878d0 590 for ($i = 1; $i < $idents; $i++) {
591 $enc_from_name = '"'.
592 getPref($data_dir,
593 $username,
594 'full_name' . $i) .
595 '" <' . getPref($data_dir, $username,
596 'email_address' . $i) . '>';
597 if ($enc_from_name == $orig_from) {
598 $identity = $i;
599 break;
600 }
601 }
bdb92db3 602 }
a61878d0 603
604 switch ($action) {
605 case ('draft'):
606 $use_signature = FALSE;
c3d1d2cf 607 $send_to = decodeHeader($orig_header->getAddr_s('to'));
608 $send_to_cc = decodeHeader($orig_header->getAddr_s('cc'));
609 $send_to_bcc = decodeHeader($orig_header->getAddr_s('bcc'));
a61878d0 610 $subject = decodeHeader($orig_header->subject);
611
612 $body_ary = explode("\n", $body);
613 $cnt = count($body_ary) ;
614 $body = '';
615 for ($i=0; $i < $cnt; $i++) {
616 if (!ereg("^[>\\s]*$", $body_ary[$i]) || !$body_ary[$i]) {
617 sqWordWrap($body_ary[$i], $editor_size );
618 $body .= $body_ary[$i] . "\n";
619 }
620 unset($body_ary[$i]);
621 }
622 sqUnWordWrap($body);
a43e4b90 623 $composeMessage = getAttachments($message, $composeMessage, $passed_id, $entities, $imapConnection);
a61878d0 624 break;
a45887d7 625 case ('edit_as_new'):
c3d1d2cf 626 $send_to = decodeHeader($orig_header->getAddr_s('to'));
627 $send_to_cc = decodeHeader($orig_header->getAddr_s('cc'));
628 $send_to_bcc = decodeHeader($orig_header->getAddr_s('bcc'));
a61878d0 629 $subject = decodeHeader($orig_header->subject);
630 $mailprio = $orig_header->priority;
631 $orig_from = '';
a43e4b90 632 $composeMessage = getAttachments($message, $composeMessage, $passed_id, $entities, $imapConnection);
a61878d0 633 sqUnWordWrap($body);
634 break;
635 case ('forward'):
636 $send_to = '';
637 $subject = decodeHeader($orig_header->subject);
638 if ((substr(strtolower($subject), 0, 4) != 'fwd:') &&
639 (substr(strtolower($subject), 0, 5) != '[fwd:') &&
640 (substr(strtolower($subject), 0, 6) != '[ fwd:')) {
641 $subject = '[Fwd: ' . $subject . ']';
642 }
643 $body = getforwardHeader($orig_header) . $body;
644 sqUnWordWrap($body);
a43e4b90 645 $composeMessage = getAttachments($message, $composeMessage, $passed_id, $entities, $imapConnection);
d5181a1d 646 $body = "\n" . $body;
a61878d0 647 break;
648 case ('forward_as_attachment'):
a43e4b90 649 $composeMessage = getMessage_RFC822_Attachment($message, $composeMessage, $passed_id, $passed_ent_id, $imapConnection);
a61878d0 650 $body = '';
651 break;
a45887d7 652 case ('reply_all'):
a61878d0 653 $send_to_cc = replyAllString($orig_header);
654 case ('reply'):
655 $send_to = $orig_header->reply_to;
656 if (is_object($send_to)) {
c3d1d2cf 657 $send_to = decodeHeader($send_to->getAddr_s('reply_to'));
a61878d0 658 } else {
c3d1d2cf 659 $send_to = decodeHeader($orig_header->getAddr_s('from'));
a61878d0 660 }
661 $subject = decodeHeader($orig_header->subject);
662 $subject = str_replace('"', "'", $subject);
663 $subject = trim($subject);
664 if (substr(strtolower($subject), 0, 3) != 're:') {
665 $subject = 'Re: ' . $subject;
666 }
667 /* this corrects some wrapping/quoting problems on replies */
668 $rewrap_body = explode("\n", $body);
cf7a1725 669 $from = (is_array($orig_header->from)) ?
670 $orig_header->from[0] : $orig_header->from;
4dfb9db7 671 $body = getReplyCitation($from->getAddress(false));
6339f68f 672 sqUnWordWrap($body);
a61878d0 673 $cnt = count($rewrap_body);
674 for ($i=0;$i<$cnt;$i++) {
cf7a1725 675 sqWordWrap($rewrap_body[$i], ($editor_size));
a61878d0 676 if (preg_match("/^(>+)/", $rewrap_body[$i], $matches)) {
677 $gt = $matches[1];
cf7a1725 678 $body .= '>' . str_replace("\n", "\n>$gt ", rtrim($rewrap_body[$i])) ."\n";
a61878d0 679 } else {
cf7a1725 680 $body .= '> ' . str_replace("\n", "\n> ", rtrim($rewrap_body[$i])) . "\n";
a61878d0 681 }
682 unset($rewrap_body[$i]);
683 }
a43e4b90 684 $composeMessage->reply_rfc822_header = $orig_header;
a61878d0 685 break;
686 default:
687 break;
41b94d65 688 }
a43e4b90 689 $compose_messages[$session] = $composeMessage;
690 sqsession_register($compose_messages, 'compose_messages');
691
a61878d0 692 sqimap_logout($imapConnection);
41b94d65 693 }
a61878d0 694 $ret = array( 'send_to' => $send_to,
695 'send_to_cc' => $send_to_cc,
696 'send_to_bcc' => $send_to_bcc,
697 'subject' => $subject,
698 'mailprio' => $mailprio,
699 'body' => $body,
700 'identity' => $identity );
701
41b94d65 702 return ($ret);
48985d59 703} /* function newMail() */
704
a43e4b90 705function getAttachments($message, &$composeMessage, $passed_id, $entities, $imapConnection) {
59edcad6 706 global $attachment_dir, $username, $data_dir, $squirrelmail_language;
48985d59 707 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
41b94d65 708 if (!count($message->entities) ||
709 ($message->type0 == 'message' && $message->type1 == 'rfc822')) {
710 if ( !in_array($message->entity_id, $entities) && $message->entity_id) {
a43e4b90 711 switch ($message->type0) {
712 case 'message':
713 if ($message->type1 == 'rfc822') {
714 $filename = decodeHeader($message->rfc822_header->subject.'.eml');
715 if ($filename == "") {
716 $filename = "untitled-".$message->entity_id.'.eml';
717 }
718 } else {
719 $filename = decodeHeader($message->getFilename());
720 }
721 break;
722 default:
723 $filename = decodeHeader($message->getFilename());
724 break;
725 }
726 if (isset($languages[$squirrelmail_language]['XTRA_CODE']) &&
727 function_exists($languages[$squirrelmail_language]['XTRA_CODE'])) {
728 $filename = $languages[$squirrelmail_language]['XTRA_CODE']('encode', $filename);
729 }
730
731 $localfilename = GenerateRandomString(32, '', 7);
732 $full_localfilename = "$hashed_attachment_dir/$localfilename";
733 while (file_exists($full_localfilename)) {
734 $localfilename = GenerateRandomString(32, '', 7);
735 $full_localfilename = "$hashed_attachment_dir/$localfilename";
736 }
737 $message->att_local_name = $full_localfilename;
738 if (!$message->mime_header) { /* temporary hack */
739 $message->mime_header = $message->header;
740 }
741
742 $composeMessage->addEntity($message);
743
744 /* Write Attachment to file */
745 $fp = fopen ("$hashed_attachment_dir/$localfilename", 'wb');
746 fputs($fp, decodeBody(mime_fetch_body($imapConnection,
747 $passed_id, $message->entity_id),
748 $message->header->encoding));
749 fclose ($fp);
48985d59 750 }
734f4ee6 751 } else {
a43e4b90 752 for ($i=0, $entCount=count($message->entities); $i<$entCount;$i++) {
753 $composeMessage=getAttachments($message->entities[$i], $composeMessage, $passed_id, $entities, $imapConnection);
48985d59 754 }
755 }
a43e4b90 756// setPref($data_dir, $username, 'attachments', serialize($attachments));
757 return $composeMessage;
48985d59 758}
759
a43e4b90 760function getMessage_RFC822_Attachment($message, $composeMessage, $passed_id,
756406df 761 $passed_ent_id='', $imapConnection) {
a6ec592e 762 global $attachments, $attachment_dir, $username, $data_dir, $uid_support;
763 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
756406df 764 if (!$passed_ent_id) {
a61878d0 765 $body_a = sqimap_run_command($imapConnection,
766 'FETCH '.$passed_id.' RFC822',
767 TRUE, $response, $readmessage,
768 $uid_support);
756406df 769 } else {
770 $body_a = sqimap_run_command($imapConnection,
a61878d0 771 'FETCH '.$passed_id.' BODY['.$passed_ent_id.']',
772 TRUE, $response, $readmessage, $uid_support);
773 $message = $message->parent;
756406df 774 }
a6ec592e 775 if ($response = 'OK') {
a61878d0 776 $subject = encodeHeader($message->rfc822_header->subject);
777 array_shift($body_a);
778 $body = implode('', $body_a) . "\r\n";
779
780 $localfilename = GenerateRandomString(32, 'FILE', 7);
781 $full_localfilename = "$hashed_attachment_dir/$localfilename";
782
a43e4b90 783 $fp = fopen( $full_localfilename, 'w');
a61878d0 784 fwrite ($fp, $body);
785 fclose($fp);
a43e4b90 786 $composeMessage->initAttachment('message/rfc822',$subject.'.eml',
787 $full_localfilename);
788 }
789 return $composeMessage;
a6ec592e 790}
791
41b94d65 792function showInputForm ($session, $values=false) {
793 global $send_to, $send_to_cc, $body,
48985d59 794 $passed_body, $color, $use_signature, $signature, $prefix_sig,
795 $editor_size, $attachments, $subject, $newmail,
41b94d65 796 $use_javascript_addr_book, $send_to_bcc, $passed_id, $mailbox,
48985d59 797 $from_htmladdr_search, $location_of_buttons, $attachment_dir,
798 $username, $data_dir, $identity, $draft_id, $delete_draft,
9c3e6cd4 799 $mailprio, $default_use_mdn, $mdn_user_support, $compose_new_win,
44560457 800 $saved_draft, $mail_sent, $sig_first, $edit_as_new, $action,
a43e4b90 801 $username, $compose_messages;
802
803 $composeMessage = $compose_messages[$session];
48985d59 804
3b487216 805 $subject = decodeHeader($subject, false);
41b94d65 806 if ($values) {
807 $send_to = $values['send_to'];
808 $send_to_cc = $values['send_to_cc'];
809 $send_to_bcc = $values['send_to_bcc'];
810 $subject = $values['subject'];
811 $mailprio = $values['mailprio'];
812 $body = $values['body'];
bdb92db3 813 $identity = $values['identity'];
41b94d65 814 }
815
48985d59 816 if ($use_javascript_addr_book) {
817 echo "\n". '<SCRIPT LANGUAGE=JavaScript><!--' . "\n" .
818 'function open_abook() { ' . "\n" .
819 ' var nwin = window.open("addrbook_popup.php","abookpopup",' .
820 '"width=670,height=300,resizable=yes,scrollbars=yes");' . "\n" .
821 ' if((!nwin.opener) && (document.windows != null))' . "\n" .
822 ' nwin.opener = document.windows;' . "\n" .
823 "}\n" .
824 '// --></SCRIPT>' . "\n\n";
825 }
826
41b94d65 827 echo "\n" . '<FORM name=compose action="compose.php" METHOD=POST ' .
828 'ENCTYPE="multipart/form-data"';
48985d59 829 do_hook("compose_form");
57257333 830
48985d59 831 echo ">\n";
832
41b94d65 833 if ($action == 'draft') {
834 echo '<input type="hidden" name="delete_draft" value="' . $passed_id . "\">\n";
48985d59 835 }
836 if (isset($delete_draft)) {
837 echo '<input type="hidden" name="delete_draft" value="' . $delete_draft. "\">\n";
838 }
da95c4b6 839 if (isset($session)) {
44560457 840 echo '<input type="hidden" name="session" value="' . $session . "\">\n";
da95c4b6 841 }
08bad2b1 842
843 if (isset($passed_id)) {
844 echo '<input type="hidden" name="passed_id" value="' . $passed_id . "\">\n";
845 }
44560457 846
9c3e6cd4 847 if ($saved_draft == 'yes') {
848 echo '<BR><CENTER><B>'. _("Draft Saved").'</CENTER></B>';
849 }
850 if ($mail_sent == 'yes') {
851 echo '<BR><CENTER><B>'. _("Your Message has been sent").'</CENTER></B>';
852 }
6206f6c4 853 echo '<TABLE ALIGN=center CELLSPACING=0 BORDER=0>' . "\n";
9c3e6cd4 854 if ($compose_new_win == '1') {
a61878d0 855 echo '<TABLE ALIGN=CENTER BGCOLOR="'.$color[0].'" WIDTH="100%" BORDER=0>'."\n" .
856 ' <TR><TD></TD><TD ALIGN="RIGHT"><INPUT TYPE="BUTTON" NAME="Close" onClick="return self.close()" VALUE='._("Close").'></TD></TR>'."\n";
9c3e6cd4 857 }
78a35fcd 858 if ($location_of_buttons == 'top') {
859 showComposeButtonRow();
860 }
48985d59 861
715225af 862 $idents = getPref($data_dir, $username, 'identities', 0);
863 if ($idents > 1) {
41b94d65 864 echo ' <TR>' . "\n" .
865 ' <TD BGCOLOR="' . $color[4] . '" WIDTH="10%" ALIGN=RIGHT>' .
866 "\n" .
867 _("From:") .
868 ' </TD><TD BGCOLOR="' . $color[4] . '" WIDTH="90%">' . "\n" .
48985d59 869 '<select name=identity>' . "\n" .
870 '<option value=default>' .
871 htmlspecialchars(getPref($data_dir, $username, 'full_name'));
872 $em = getPref($data_dir, $username, 'email_address');
873 if ($em != '') {
248bfebb 874 echo htmlspecialchars(' <' . $em . '>') . "\n";
48985d59 875 }
876 for ($i = 1; $i < $idents; $i ++) {
248bfebb 877 echo '<option value="' . $i . '"';
48985d59 878 if (isset($identity) && $identity == $i) {
78a35fcd 879 echo ' SELECTED';
48985d59 880 }
881 echo '>' . htmlspecialchars(getPref($data_dir, $username,
882 'full_name' . $i));
248bfebb 883 $em = getPref($data_dir, $username, 'email_address' . $i);
48985d59 884 if ($em != '') {
78a35fcd 885 echo htmlspecialchars(' <' . $em . '>') . "\n";
48985d59 886 }
9f599fe3 887 echo '</option>';
48985d59 888 }
889 echo '</select>' . "\n" .
41b94d65 890 ' </TD>' . "\n" .
891 ' </TR>' . "\n";
892 }
893 echo ' <TR>' . "\n" .
894 ' <TD BGCOLOR="' . $color[4] . '" WIDTH="10%" ALIGN=RIGHT>' . "\n" .
895 _("To:") .
896 ' </TD><TD BGCOLOR="' . $color[4] . '" WIDTH="90%">' . "\n" .
897 ' <INPUT TYPE=text NAME="send_to" VALUE="' .
898 htmlspecialchars($send_to) . '" SIZE=60><BR>' . "\n" .
899 ' </TD>' . "\n" .
900 ' </TR>' . "\n" .
901 ' <TR>' . "\n" .
902 ' <TD BGCOLOR="' . $color[4] . '" ALIGN=RIGHT>' . "\n" .
903 _("CC:") .
904 ' </TD><TD BGCOLOR="' . $color[4] . '" ALIGN=LEFT>' . "\n" .
905 ' <INPUT TYPE=text NAME="send_to_cc" SIZE=60 VALUE="' .
906 htmlspecialchars($send_to_cc) . '"><BR>' . "\n" .
907 ' </TD>' . "\n" .
908 ' </TR>' . "\n" .
909 ' <TR>' . "\n" .
910 ' <TD BGCOLOR="' . $color[4] . '" ALIGN=RIGHT>' . "\n" .
911 _("BCC:") .
912 ' </TD><TD BGCOLOR="' . $color[4] . '" ALIGN=LEFT>' . "\n" .
913 ' <INPUT TYPE=text NAME="send_to_bcc" VALUE="' .
914 htmlspecialchars($send_to_bcc) . '" SIZE=60><BR>' . "\n" .
915 '</TD></TR>' . "\n" .
916 ' <TR>' . "\n" .
917 ' <TD BGCOLOR="' . $color[4] . '" ALIGN=RIGHT>' . "\n" .
918 _("Subject:") .
919 ' </TD><TD BGCOLOR="' . $color[4] . '" ALIGN=LEFT>' . "\n";
920 echo ' <INPUT TYPE=text NAME=subject SIZE=60 VALUE="' .
921 htmlspecialchars($subject) . '">';
922 echo '</td></tr>' . "\n\n";
48985d59 923
78a35fcd 924 if ($location_of_buttons == 'between') {
925 showComposeButtonRow();
926 }
4dfb9db7 927
fdc83c55 928 if ($compose_new_win == '1') {
41b94d65 929 echo ' <TR>' . "\n" .
930 ' <TD BGCOLOR="' . $color[0] . '" COLSPAN=2 ALIGN=CENTER>' . "\n" .
931 ' <TEXTAREA NAME=body ROWS=20 COLS="' .
932 $editor_size . '" WRAP="VIRTUAL">';
fdc83c55 933 }
934 else {
41b94d65 935 echo ' <TR>' . "\n" .
936 ' <TD BGCOLOR="' . $color[4] . '" COLSPAN=2>' . "\n" .
937 ' &nbsp;&nbsp;<TEXTAREA NAME=body ROWS=20 COLS="' .
938 $editor_size . '" WRAP="VIRTUAL">';
fdc83c55 939 }
48985d59 940 if ($use_signature == true && $newmail == true && !isset($from_htmladdr_search)) {
3b17e952 941 if ($sig_first == '1') {
83be314a 942 if ($charset == 'iso-2022-jp') {
943 echo "\n\n".($prefix_sig==true? "-- \n":'').mb_convert_encoding($signature, 'EUC-JP');
944 } else {
3b17e952 945 echo "\n\n".($prefix_sig==true? "-- \n":'').htmlspecialchars($signature);
83be314a 946 }
3b17e952 947 echo "\n\n".htmlspecialchars($body);
948 }
949 else {
950 echo "\n\n".htmlspecialchars($body);
83be314a 951 if ($charset == 'iso-2022-jp') {
952 echo "\n\n".($prefix_sig==true? "-- \n":'').mb_convert_encoding($signature, 'EUC-JP');
953 }else{
3b17e952 954 echo "\n\n".($prefix_sig==true? "-- \n":'').htmlspecialchars($signature);
955 }
956 }
83be314a 957 }
3b17e952 958 else {
959 echo htmlspecialchars($body);
48985d59 960 }
41b94d65 961 echo '</TEXTAREA><BR>' . "\n" .
962 ' </TD>' . "\n" .
963 ' </TR>' . "\n";
48985d59 964
965 if ($location_of_buttons == 'bottom') {
966 showComposeButtonRow();
967 } else {
a61878d0 968 echo ' <TR><TD COLSPAN=2 ALIGN=RIGHT>' .
969 ' <INPUT TYPE=SUBMIT NAME=send VALUE="' . _("Send") . '">' .
970 ' &nbsp;&nbsp;&nbsp;&nbsp;<BR><BR>' .
971 ' </TD></TR>' . "\n";
48985d59 972 }
46bb8da8 973
48985d59 974 /* This code is for attachments */
a61878d0 975 echo '<table width="100%" cellpadding="0" cellspacing="4" align="center" border="0">' .
976 ' <tr><td>' .
977 ' <table width="100%" cellpadding="1" cellspacing="0" align="center"'.' border="0" bgcolor="'.$color[9].'">' .
978 ' <tr><td>' .
979 ' <table width="100%" cellpadding="3" cellspacing="0" align="center" border="0">' .
980 ' <TR>' . "\n" .
41b94d65 981 ' <TD VALIGN=MIDDLE ALIGN=RIGHT>' . "\n" .
982 _("Attach:") .
983 ' </TD>' . "\n" .
984 ' <TD VALIGN=MIDDLE ALIGN=LEFT>' . "\n" .
985 ' <INPUT NAME="attachfile" SIZE=48 TYPE="file">' . "\n" .
986 ' &nbsp;&nbsp;<input type="submit" name="attach"' .
987 ' value="' . _("Add") .'">' . "\n" .
988 ' </TD>' . "\n" .
989 ' </TR>' . "\n";
91f2085b 990
41b94d65 991
91f2085b 992 $s_a = array();
4dfb9db7 993 if ($composeMessage->entities) {
994 foreach ($composeMessage->entities as $key => $attachment) {
a43e4b90 995 $attached_file = $attachment->att_local_name;
996 if ($attachment->att_local_name || $attachment->body_part) {
997 $attached_filename = decodeHeader($attachment->mime_header->getParameter('name'));
998 $type = $attachment->mime_header->type0.'/'.
999 $attachment->mime_header->type1;
1000 $s_a[] = '<input type="checkbox" name="delete[]" value="' .
1001 $key . "\">\n" . $attached_filename . ' - ' . $type .
1002 ' ('.show_readable_size( filesize( $attached_file ) )
1003 . ')<br>'."\n";
1004 }
4dfb9db7 1005 }
91f2085b 1006 }
1007 if (count($s_a)) {
1008 foreach ($s_a as $s) {
1009 echo '<tr><td align=left colspan="2" bgcolor="' . $color[0] . '">'.$s.'</td></tr>';
a61878d0 1010 }
91f2085b 1011 echo '<tr><td colspan="2"><input type="submit" name="do_delete" value="' .
1012 _("Delete selected attachments") . "\">\n" .
1013 '</td></tr>';
1014 }
a61878d0 1015 echo ' </table></td></tr>' .
1016 ' </table>' .
1017 ' </td></tr>';
41b94d65 1018
41b94d65 1019 /* End of attachment code */
07687736 1020 if ($compose_new_win == '1') {
41b94d65 1021 echo '</TABLE>'."\n";
07687736 1022 }
a61878d0 1023 echo '</TABLE>' . "\n" .
1024 '<input type="hidden" name="username" value="'. $username . "\">\n" .
1025 '<input type=hidden name=action value=' . $action . ">\n" .
1026 '<INPUT TYPE=hidden NAME=mailbox VALUE="' . htmlspecialchars($mailbox) .
4dfb9db7 1027 "\">\n";
1028 echo '</FORM>';
9f599fe3 1029 do_hook('compose_bottom');
48985d59 1030 echo '</BODY></HTML>' . "\n";
1031}
1032
1033
70c4fd84 1034function showComposeButtonRow() {
78a35fcd 1035 global $use_javascript_addr_book, $save_as_draft,
a61878d0 1036 $default_use_priority, $mailprio, $default_use_mdn,
1037 $request_mdn, $request_dr,
1038 $data_dir, $username;
70c4fd84 1039
41b94d65 1040 echo " <TR><TD>\n</TD><TD>\n";
ae25968c 1041 if ($default_use_priority) {
1042 if(!isset($mailprio)) {
1043 $mailprio = "3";
70c4fd84 1044 }
1045 echo _("Priority") .': <select name="mailprio">'.
1046 '<option value="1"'.($mailprio=='1'?' selected':'').'>'. _("High") .'</option>'.
1047 '<option value="3"'.($mailprio=='3'?' selected':'').'>'. _("Normal") .'</option>'.
1048 '<option value="5"'.($mailprio=='5'?' selected':'').'>'. _("Low").'</option>'.
1049 "</select>";
ae25968c 1050 }
1051 $mdn_user_support=getPref($data_dir, $username, 'mdn_user_support',$default_use_mdn);
1052 if ($default_use_mdn) {
70c4fd84 1053 if ($mdn_user_support) {
1054 echo "\n\t". _("Receipt") .': '.
b2a7e5bc 1055 '<input type="checkbox" name="request_mdn" value=1'.
a61878d0 1056 ($request_mdn=='1'?' checked':'') .'>'. _("On Read").
b2a7e5bc 1057 ' <input type="checkbox" name="request_dr" value=1'.
a61878d0 1058 ($request_dr=='1'?' checked':'') .'>'. _("On Delivery");
70c4fd84 1059 }
ae25968c 1060 }
48985d59 1061
a61878d0 1062 echo " </td></tr>\n <TR><td>\n </td><td>\n" .
1063 "\n <INPUT TYPE=SUBMIT NAME=\"sigappend\" VALUE=\"". _("Signature") . "\">\n";
78a35fcd 1064 if ($use_javascript_addr_book) {
46bb8da8 1065 echo " <SCRIPT LANGUAGE=JavaScript><!--\n document.write(\"".
1066 " <input type=button value=\\\""._("Addresses").
1067 "\\\" onclick='javascript:open_abook();'>\");".
1068 " // --></SCRIPT><NOSCRIPT>\n".
1069 " <input type=submit name=\"html_addr_search\" value=\"".
1070 _("Addresses")."\">".
1071 " </NOSCRIPT>\n";
734f4ee6 1072 } else {
78a35fcd 1073 echo " <input type=submit name=\"html_addr_search\" value=\"".
1074 _("Addresses")."\">";
1075 }
48985d59 1076
78a35fcd 1077 if ($save_as_draft) {
1078 echo '<input type="submit" name ="draft" value="' . _("Save Draft") . "\">\n";
1079 }
0a17f9dd 1080
4dfb9db7 1081 echo '<INPUT TYPE=submit NAME=send VALUE="'. _("Send") . "\">\n";
78a35fcd 1082 do_hook('compose_button_row');
441f2d33 1083
41b94d65 1084 echo " </TD></TR>\n\n";
78a35fcd 1085}
b278172f 1086
70c4fd84 1087function checkInput ($show) {
78a35fcd 1088 /*
1089 * I implemented the $show variable because the error messages
1090 * were getting sent before the page header. So, I check once
1091 * using $show=false, and then when i'm ready to display the error
1092 * message, show=true
1093 */
6bf2a88f 1094 global $body, $send_to, $send_to_bcc, $subject, $color;
78a35fcd 1095
6bf2a88f 1096 if ($send_to == '' && $send_to_bcc == '') {
78a35fcd 1097 if ($show) {
0ad7dbda 1098 plain_error_message(_("You have not filled in the \"To:\" field."), $color);
78a35fcd 1099 }
1100 return false;
1101 }
1102 return true;
1103} /* function checkInput() */
df15de21 1104
3806fa52 1105
00793a25 1106/* True if FAILURE */
da95c4b6 1107function saveAttachedFiles($session) {
0b97a708 1108 global $_FILES, $attachment_dir, $attachments, $username,
a43e4b90 1109 $data_dir, $compose_messages;
4c9d2242 1110
45cdd1b5 1111 /* get out of here if no file was attached at all */
1112 if (! is_uploaded_file($_FILES['attachfile']['tmp_name']) ) {
1113 return true;
1114 }
1115
4c9d2242 1116 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
1117 $localfilename = GenerateRandomString(32, '', 7);
1118 $full_localfilename = "$hashed_attachment_dir/$localfilename";
1119 while (file_exists($full_localfilename)) {
1120 $localfilename = GenerateRandomString(32, '', 7);
1121 $full_localfilename = "$hashed_attachment_dir/$localfilename";
1122 }
1123
0b97a708 1124 if (!@rename($_FILES['attachfile']['tmp_name'], $full_localfilename)) {
a43e4b90 1125 if (function_exists("move_uploaded_file")) {
0b97a708 1126 if (!@move_uploaded_file($_FILES['attachfile']['tmp_name'],$full_localfilename)) {
a43e4b90 1127 return true;
1128 }
1129 } else {
0b97a708 1130 if (!@copy($_FILES['attachfile']['tmp_name'], $full_localfilename)) {
a61878d0 1131 return true;
1132 }
a43e4b90 1133 }
a61878d0 1134 }
a43e4b90 1135 $message = $compose_messages[$session];
0b97a708 1136 $type = strtolower($_FILES['attachfile']['type']);
1137 $name = $_FILES['attachfile']['name'];
a43e4b90 1138 $message->initAttachment($type, $name, $full_localfilename);
1139 $compose_messages[$session] = $message;
4c9d2242 1140}
1141
a43e4b90 1142function ClearAttachments($composeMessage) {
b48d3c53 1143 if ($composeMessage->att_local_name) {
1144 $attached_file = $composeMessage->att_local_name;
a43e4b90 1145 if (file_exists($attached_file)) {
1146 unlink($attached_file);
8712abea 1147 }
da95c4b6 1148 }
a43e4b90 1149 for ($i=0, $entCount=count($composeMessage->entities);$i< $entCount; ++$i) {
1150 ClearAttachments($composeMessage->entities[$i]);
1151 }
4c9d2242 1152}
1153
da95c4b6 1154
a43e4b90 1155
1156function getReplyCitation($orig_from) {
4c9d2242 1157 global $reply_citation_style, $reply_citation_start, $reply_citation_end;
1158
1159 /* First, return an empty string when no citation style selected. */
1160 if (($reply_citation_style == '') || ($reply_citation_style == 'none')) {
1161 return '';
1162 }
1163
4c9d2242 1164 /* Make sure our final value isn't an empty string. */
1165 if ($orig_from == '') {
1166 return '';
1167 }
1168
1169 /* Otherwise, try to select the desired citation style. */
1170 switch ($reply_citation_style) {
1171 case 'author_said':
1172 $start = '';
1173 $end = ' ' . _("said") . ':';
1174 break;
1175 case 'quote_who':
1176 $start = '<' . _("quote") . ' ' . _("who") . '="';
1177 $end = '">';
1178 break;
1179 case 'user-defined':
55b321f2 1180 $start = $reply_citation_start .
a61878d0 1181 ($reply_citation_start == '' ? '' : ' ');
4c9d2242 1182 $end = $reply_citation_end;
1183 break;
1184 default:
1185 return '';
1186 }
1187
1188 /* Build and return the citation string. */
1189 return ($start . $orig_from . $end . "\n");
1190}
1191
a43e4b90 1192/* temporary function to make use of the deliver class.
1193 In the future the responsable backend should be automaticly loaded
1194 and conf.pl should show a list of available backends.
1195 The message also should be constructed by the message class.
1196*/
1197
b48d3c53 1198function sendMessage($composeMessage, $draft=false) {
a43e4b90 1199 global $send_to, $send_to_cc, $send_to_bcc, $mailprio, $subject, $body,
1200 $username, $popuser, $usernamedata, $identity, $data_dir,
1201 $request_mdn, $request_dr, $default_charset, $color, $useSendmail,
b48d3c53 1202 $domain, $action;
a43e4b90 1203 global $imapServerAddress, $imapPort, $sent_folder, $key;
1204
1205 $rfc822_header = $composeMessage->rfc822_header;
1206 $rfc822_header->to = $rfc822_header->parseAddress($send_to,true, array(), '', $domain);
1207 $rfc822_header->cc = $rfc822_header->parseAddress($send_to_cc,true,array(), '',$domain);
1208 $rfc822_header->bcc = $rfc822_header->parseAddress($send_to_bcc,true, array(), '',$domain);
1209 $rfc822_header->priority = $mailprio;
1210 $rfc822_header->subject = $subject;
1211 $special_encoding='';
1212 if (strtolower($default_charset) == 'iso-2022-jp') {
1213 if (mb_detect_encoding($body) == 'ASCII') {
1214 $special_encoding = '8bit';
1215 } else {
1216 $body = mb_convert_encoding($body, 'JIS');
1217 $special_encoding = '7bit';
1218 }
1219 }
1220 $composeMessage->setBody($body);
1221
1222 if (ereg("^([^@%/]+)[@%/](.+)$", $username, $usernamedata)) {
1223 $popuser = $usernamedata[1];
1224 $domain = $usernamedata[2];
1225 unset($usernamedata);
1226 } else {
1227 $popuser = $username;
1228 }
1229 $reply_to = '';
1230 if (isset($identity) && $identity != 'default') {
045714fd 1231 $from_mail = getPref($data_dir, $username,'email_address' . $identity);
1232 $full_name = getPref($data_dir, $username,'full_name' . $identity);
1233 $reply_to = getPref($data_dir, $username,'reply_to' . $identity);
a43e4b90 1234 } else {
1235 $from_mail = getPref($data_dir, $username, 'email_address');
1236 $full_name = getPref($data_dir, $username, 'full_name');
a43e4b90 1237 $reply_to = getPref($data_dir, $username,'reply_to');
1238 }
045714fd 1239 if (!$from_mail) {
1240 $from_mail = "$popuser@$domain";
1241 $full_name = '';
1242 }
1243 $rfc822_header->from = $rfc822_header->parseAddress($from_mail,true);
1244 if ($full_name) {
9783f396 1245 $from = $rfc822_header->from[0];
045714fd 1246 if (!$from->host) $from->host = $domain;
1247 $from_addr = $full_name .' <'.$from->mailbox.'@'.$from->host.'>';
1248 $rfc822_header->from = $rfc822_header->parseAddress($from_addr,true);
a43e4b90 1249 }
a43e4b90 1250 if ($reply_to) {
1251 $rfc822_header->reply_to = $rfc822_header->parseAddress($reply_to,true);
1252 }
1253 /* Receipt: On Read */
1254 if (isset($request_mdn) && $request_mdn) {
1255 $rfc822_header->dnt = $rfc822_header->parseAddress($from_mail,true);
1256 }
1257 /* Receipt: On Delivery */
1258 if (isset($request_dr) && $request_dr) {
1259 $rfc822_header->more_headers['Return-Receipt-To'] = $from_mail;
1260 }
1261 /* multipart messages */
1262 if (count($composeMessage->entities)) {
1263 $message_body = new Message();
1264 $message_body->body_part = $composeMessage->body_part;
1265 $composeMessage->body_part = '';
1266 $mime_header = new MessageHeader;
1267 $mime_header->type0 = 'text';
1268 $mime_header->type1 = 'plain';
1269 if ($special_encoding) {
1270 $mime_header->encoding = $special_encoding;
1271 } else {
1272 $mime_header->encoding = 'us-ascii';
1273 }
1274 if ($default_charset) {
1275 $mime_header->parameters['charset'] = $default_charset;
1276 }
1277 $message_body->mime_header = $mime_header;
1278 array_unshift($composeMessage->entities, $message_body);
1279 $content_type = new ContentType('multipart/mixed');
1280 } else {
1281 $content_type = new ContentType('text/plain');
1282 }
1283 if ($default_charset) {
1284 $content_type->properties['charset']=$default_charset;
1285 }
1286
1287 $rfc822_header->content_type = $content_type;
1288 $composeMessage->rfc822_header = $rfc822_header;
1289
b48d3c53 1290 if (!$useSendmail && !$draft) {
86725763 1291 require_once(SM_PATH . 'class/deliver/Deliver_SMTP.class.php');
a43e4b90 1292 $deliver = new Deliver_SMTP();
1293 global $smtpServerAddress, $smtpPort, $use_authenticated_smtp, $pop_before_smtp;
1294 if ($use_authenticated_smtp) {
1295 global $key, $onetimepad;
1296 $user = $username;
1297 $pass = OneTimePadDecrypt($key, $onetimepad);
1298 } else {
1299 $user = '';
1300 $pass = '';
1301 }
b48d3c53 1302 $authPop = (isset($pop_before_smtp) && $pop_before_smtp) ? true : false;
a43e4b90 1303 $stream = $deliver->initStream($composeMessage,$domain,0,
1304 $smtpServerAddress, $smtpPort, $authPop);
b48d3c53 1305 } elseif (!$draft) {
86725763 1306 require_once(SM_PATH . 'class/deliver/Deliver_SendMail.class.php');
b48d3c53 1307 global $sendmail_path;
1308 $deliver = new Deliver_SendMail();
1309 $stream = $deliver->initStream($composeMessage,$sendmail_path);
1310 } elseif ($draft) {
1311 global $draft_folder;
86725763 1312 require_once(SM_PATH . 'class/deliver/Deliver_IMAP.class.php');
b48d3c53 1313 $imap_stream = sqimap_login($username, $key, $imapServerAddress,
1314 $imapPort, 0);
1315 if (sqimap_mailbox_exists ($imap_stream, $draft_folder)) {
4dfb9db7 1316 require_once(SM_PATH . 'class/deliver/Deliver_IMAP.class.php');
1317 $imap_deliver = new Deliver_IMAP();
1318 $length = $imap_deliver->mail($composeMessage);
1319 sqimap_append ($imap_stream, $draft_folder, $length);
1320 $imap_deliver->mail($composeMessage, $imap_stream);
1321 sqimap_append_done ($imap_stream);
1322 sqimap_logout($imap_stream);
1323 unset ($imap_deliver);
1324 return $length;
1325 } else {
1326 $msg = '<br>Error: '._("Draft folder")." $draft_folder" . ' does not exist.';
1327 plain_error_message($msg, $color);
1328 return false;
b48d3c53 1329 }
a43e4b90 1330 }
1331 $succes = false;
1332 if ($stream) {
1333 $length = $deliver->mail($composeMessage, $stream);
1334 $succes = $deliver->finalizeStream($stream);
1335 }
1336 if (!$succes) {
1337 $msg = $deliver->dlv_msg . '<br>Server replied: '.$deliver->dlv_ret_nr;
1338 plain_error_message($msg, $color);
1339 } else {
1340 unset ($deliver);
1341 $imap_stream = sqimap_login($username, $key, $imapServerAddress,
1342 $imapPort, 0);
1343 if (sqimap_mailbox_exists ($imap_stream, $sent_folder)) {
1344 sqimap_append ($imap_stream, $sent_folder, $length);
86725763 1345 require_once(SM_PATH . 'class/deliver/Deliver_IMAP.class.php');
a43e4b90 1346 $imap_deliver = new Deliver_IMAP();
1347 $imap_deliver->mail($composeMessage, $imap_stream);
1348 sqimap_append_done ($imap_stream);
a43e4b90 1349 unset ($imap_deliver);
1350 }
b48d3c53 1351 global $passed_id, $mailbox, $action;
a43e4b90 1352 ClearAttachments($composeMessage);
b48d3c53 1353 if ($action == 'reply' || $action == 'reply_all') {
1354 sqimap_mailbox_select ($imap_stream, $mailbox);
1355 sqimap_messages_flag ($imap_stream, $passed_id, $passed_id, 'Answered', true);
1356 }
1357 sqimap_logout($imap_stream);
a43e4b90 1358 }
1359 return $succes;
1360}
1361
6bf2a88f 1362?>