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