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