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