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