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