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