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