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