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