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