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