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