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