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