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