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