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