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