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