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