e1b2d57007b8d211412ce2adbf48e36c2c831a0d
[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 if (!$message->mime_header) { /* temporary hack */
788 $message->mime_header = $message->header;
789 }
790 $filename = $message->getFilename();
791 break;
792 }
793 $filename = decodeHeader($filename);
794 if (isset($languages[$squirrelmail_language]['XTRA_CODE']) &&
795 function_exists($languages[$squirrelmail_language]['XTRA_CODE'])) {
796 $filename = $languages[$squirrelmail_language]['XTRA_CODE']('encode', $filename);
797 }
798 $localfilename = GenerateRandomString(32, '', 7);
799 $full_localfilename = "$hashed_attachment_dir/$localfilename";
800 while (file_exists($full_localfilename)) {
801 $localfilename = GenerateRandomString(32, '', 7);
802 $full_localfilename = "$hashed_attachment_dir/$localfilename";
803 }
804 $message->att_local_name = $full_localfilename;
805
806 $composeMessage->initAttachment($message->type0.'/'.$message->type1,$filename,
807 $full_localfilename);
808
809 /* Write Attachment to file */
810 $fp = fopen ("$hashed_attachment_dir/$localfilename", 'wb');
811 fputs($fp, decodeBody(mime_fetch_body($imapConnection,
812 $passed_id, $message->entity_id),
813 $message->header->encoding));
814 fclose ($fp);
815 }
816 } else {
817 for ($i=0, $entCount=count($message->entities); $i<$entCount;$i++) {
818 $composeMessage=getAttachments($message->entities[$i], $composeMessage, $passed_id, $entities, $imapConnection);
819 }
820 }
821 return $composeMessage;
822 }
823
824 function getMessage_RFC822_Attachment($message, $composeMessage, $passed_id,
825 $passed_ent_id='', $imapConnection) {
826 global $attachments, $attachment_dir, $username, $data_dir, $uid_support;
827 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
828 if (!$passed_ent_id) {
829 $body_a = sqimap_run_command($imapConnection,
830 'FETCH '.$passed_id.' RFC822',
831 TRUE, $response, $readmessage,
832 $uid_support);
833 } else {
834 $body_a = sqimap_run_command($imapConnection,
835 'FETCH '.$passed_id.' BODY['.$passed_ent_id.']',
836 TRUE, $response, $readmessage, $uid_support);
837 $message = $message->parent;
838 }
839 if ($response == 'OK') {
840 $subject = encodeHeader($message->rfc822_header->subject);
841 array_shift($body_a);
842 $body = implode('', $body_a) . "\r\n";
843
844 $localfilename = GenerateRandomString(32, 'FILE', 7);
845 $full_localfilename = "$hashed_attachment_dir/$localfilename";
846
847 $fp = fopen( $full_localfilename, 'w');
848 fwrite ($fp, $body);
849 fclose($fp);
850 $composeMessage->initAttachment('message/rfc822',$subject.'.eml',
851 $full_localfilename);
852 }
853 return $composeMessage;
854 }
855
856 function showInputForm ($session, $values=false) {
857 global $send_to, $send_to_cc, $body,
858 $passed_body, $color, $use_signature, $signature, $prefix_sig,
859 $editor_size, $attachments, $subject, $newmail,
860 $use_javascript_addr_book, $send_to_bcc, $passed_id, $mailbox,
861 $from_htmladdr_search, $location_of_buttons, $attachment_dir,
862 $username, $data_dir, $identity, $draft_id, $delete_draft,
863 $mailprio, $default_use_mdn, $mdn_user_support, $compose_new_win,
864 $saved_draft, $mail_sent, $sig_first, $edit_as_new, $action,
865 $username, $compose_messages, $composesession, $default_charset;
866
867 $composeMessage = $compose_messages[$session];
868
869 if ($values) {
870 $send_to = $values['send_to'];
871 $send_to_cc = $values['send_to_cc'];
872 $send_to_bcc = $values['send_to_bcc'];
873 $subject = $values['subject'];
874 $mailprio = $values['mailprio'];
875 $body = $values['body'];
876 $identity = (int) $values['identity'];
877 } else {
878 $send_to = decodeHeader($send_to);
879 $send_to_cc = decodeHeader($send_to_cc);
880 $send_to_bcc = decodeHeader($send_to_bcc);
881 }
882
883 if ($use_javascript_addr_book) {
884 echo "\n". '<SCRIPT LANGUAGE=JavaScript>'."\n<!--\n" .
885 'function open_abook() { ' . "\n" .
886 ' var nwin = window.open("addrbook_popup.php","abookpopup",' .
887 '"width=670,height=300,resizable=yes,scrollbars=yes");' . "\n" .
888 ' if((!nwin.opener) && (document.windows != null))' . "\n" .
889 ' nwin.opener = document.windows;' . "\n" .
890 "}\n" .
891 "// -->\n</SCRIPT>\n\n";
892 }
893
894 echo "\n" . '<FORM name=compose action="compose.php" METHOD=POST ' .
895 'ENCTYPE="multipart/form-data"';
896 do_hook("compose_form");
897
898 echo ">\n";
899
900 if ($action == 'draft') {
901 echo '<input type="hidden" name="delete_draft" value="' . $passed_id . "\">\n";
902 }
903 if (isset($delete_draft)) {
904 echo '<input type="hidden" name="delete_draft" value="' . $delete_draft. "\">\n";
905 }
906 if (isset($session)) {
907 echo '<input type="hidden" name="session" value="' . $session . "\">\n";
908 }
909
910 if (isset($passed_id)) {
911 echo '<input type="hidden" name="passed_id" value="' . $passed_id . "\">\n";
912 }
913
914 if ($saved_draft == 'yes') {
915 echo '<BR><CENTER><B>'. _("Draft Saved").'</CENTER></B>';
916 }
917 if ($mail_sent == 'yes') {
918 echo '<BR><CENTER><B>'. _("Your Message has been sent").'</CENTER></B>';
919 }
920 echo '<TABLE ALIGN=center CELLSPACING=0 BORDER=0>' . "\n";
921 if ($compose_new_win == '1') {
922 echo '<TABLE ALIGN=CENTER BGCOLOR="'.$color[0].'" WIDTH="100%" BORDER=0>'."\n" .
923 ' <TR><TD></TD>'. html_tag( 'td', '', 'right' ) . '<INPUT TYPE="BUTTON" NAME="Close" onClick="return self.close()" VALUE='._("Close").'></TD></TR>'."\n";
924 }
925 if ($location_of_buttons == 'top') {
926 showComposeButtonRow();
927 }
928
929 /* display select list for identities */
930 $idents = getPref($data_dir, $username, 'identities', 0);
931 if ($idents > 1) {
932 $fn = getPref($data_dir, $username, 'full_name');
933 $em = getPref($data_dir, $username, 'email_address');
934 echo ' <tr>' . "\n" .
935 html_tag( 'td', '', 'right', $color[4], 'width="10%"' ) .
936 _("From:") . '</td>' . "\n" .
937 html_tag( 'td', '', 'left', $color[4], 'width="90%"' ) .
938 ' <select name="identity">' . "\n" .
939 ' <option value="default">' .
940 htmlspecialchars($fn);
941 if ($em != '') {
942 if($fn != '') {
943 echo htmlspecialchars(' <' . $em . '>') . "\n";
944 } else {
945 echo htmlspecialchars($em) . "\n";
946 }
947 }
948 echo '</option>';
949 for ($i = 1; $i < $idents; $i ++) {
950 $fn = getPref($data_dir, $username, 'full_name' . $i);
951 $em = getPref($data_dir, $username, 'email_address' . $i);
952
953 echo '<option value="' . $i . '"';
954 if (isset($identity) && $identity == $i) {
955 echo ' selected';
956 }
957 echo '>' . htmlspecialchars($fn);
958 if ($em != '') {
959 if($fn != '') {
960 echo htmlspecialchars(' <' . $em . '>') . "\n";
961 } else {
962 echo htmlspecialchars($em) . "\n";
963 }
964 }
965 echo '</option>';
966 }
967 echo '</select>' . "\n" .
968 ' </td>' . "\n" .
969 ' </tr>' . "\n";
970 }
971 echo ' <tr>' . "\n" .
972 html_tag( 'td', '', 'right', $color[4], 'width="10%"' ) .
973 _("To:") . '</TD>' . "\n" .
974 html_tag( 'td', '', 'left', $color[4], 'width="90%"' ) .
975 ' <input type="text" name="send_to" value="' .
976 $send_to . '" size="60" /><br />' . "\n" .
977 ' </td>' . "\n" .
978 ' </tr>' . "\n" .
979 ' <tr>' . "\n" .
980 html_tag( 'td', '', 'right', $color[4] ) .
981 _("CC:") . '</td>' . "\n" .
982 html_tag( 'td', '', 'left', $color[4] ) .
983 ' <input type="text" name="send_to_cc" size="60" value="' .
984 $send_to_cc . '" /><br />' . "\n" .
985 ' </td>' . "\n" .
986 ' </tr>' . "\n" .
987 ' <tr>' . "\n" .
988 html_tag( 'td', '', 'right', $color[4] ) .
989 _("BCC:") . '</td>' . "\n" .
990 html_tag( 'td', '', 'left', $color[4] ) .
991 ' <input type="text" name="send_to_bcc" value="' .
992 $send_to_bcc . '" size="60" /><br />' . "\n" .
993 ' </td>' . "\n" .
994 ' </tr>' . "\n" .
995 ' <tr>' . "\n" .
996 html_tag( 'td', '', 'right', $color[4] ) .
997 _("Subject:") . '</td>' . "\n" .
998 html_tag( 'td', '', 'left', $color[4] ) . "\n";
999 echo ' <input type="text" name="subject" size="60" value="' .
1000 $subject . '" />' . "\n" .
1001 ' </td>' . "\n" .
1002 ' </tr>' . "\n\n";
1003
1004 if ($location_of_buttons == 'between') {
1005 showComposeButtonRow();
1006 }
1007
1008 /* why this distinction? */
1009 if ($compose_new_win == '1') {
1010 echo ' <TR>' . "\n" .
1011 ' <TD BGCOLOR="' . $color[0] . '" COLSPAN=2 ALIGN=CENTER>' . "\n" .
1012 ' <TEXTAREA NAME=body ROWS=20 COLS="' .
1013 $editor_size . '" WRAP="VIRTUAL">';
1014 }
1015 else {
1016 echo ' <TR>' . "\n" .
1017 ' <TD BGCOLOR="' . $color[4] . '" COLSPAN=2>' . "\n" .
1018 ' &nbsp;&nbsp;<TEXTAREA NAME=body ROWS=20 COLS="' .
1019 $editor_size . '" WRAP="VIRTUAL">';
1020 }
1021
1022 if ($use_signature == true && $newmail == true && !isset($from_htmladdr_search)) {
1023 if ($idents > 1) {
1024 if ($identity == 'default') {
1025 $no = 'g';
1026 } else {
1027 $no = $identity;
1028 }
1029 $signature = getSig($data_dir, $username, $no);
1030 }
1031
1032 if ($sig_first == '1') {
1033 if ($default_charset == 'iso-2022-jp') {
1034 echo "\n\n".($prefix_sig==true? "-- \n":'').mb_convert_encoding($signature, 'EUC-JP');
1035 } else {
1036 echo "\n\n".($prefix_sig==true? "-- \n":'').decodeHeader($signature,false,false);
1037 }
1038 echo "\n\n".decodeHeader($body,false,false);
1039 }
1040 else {
1041 echo "\n\n".decodeHeader($body,false,false);
1042 if ($default_charset == 'iso-2022-jp') {
1043 echo "\n\n".($prefix_sig==true? "-- \n":'').mb_convert_encoding($signature, 'EUC-JP');
1044 }else{
1045 echo "\n\n".($prefix_sig==true? "-- \n":'').decodeHeader($signature,false,false);
1046 }
1047 }
1048 }
1049 else {
1050 echo decodeHeader($body,false,false);
1051 }
1052 echo '</textarea><br />' . "\n" .
1053 ' </td>' . "\n" .
1054 ' </tr>' . "\n";
1055
1056
1057 if ($location_of_buttons == 'bottom') {
1058 showComposeButtonRow();
1059 } else {
1060 echo ' <tr>' . "\n" .
1061 html_tag( 'td', '', 'right', '', 'colspan="2"' ) . "\n" .
1062 ' <input type="submit" name="send" value="' . _("Send") . '" />' . "\n" .
1063 ' &nbsp;&nbsp;&nbsp;&nbsp;<br /><br />' . "\n" .
1064 ' </td>' . "\n" .
1065 ' </tr>' . "\n";
1066 }
1067
1068 /* This code is for attachments */
1069 if ((bool) ini_get('file_uploads')) {
1070
1071 /* Calculate the max size for an uploaded file.
1072 * This is advisory for the user because we can't actually prevent
1073 * people to upload too large files. */
1074 $sizes = array();
1075 /* php.ini vars which influence the max for uploads */
1076 $configvars = array('post_max_size', 'memory_limit', 'upload_max_filesize');
1077 foreach($configvars as $var) {
1078 /* skip 0 or empty values */
1079 if( $size = getByteSize(ini_get($var)) ) {
1080 $sizes[] = $size;
1081 }
1082 }
1083
1084 if(count($sizes) > 0) {
1085 $maxsize = '(max.&nbsp;' . show_readable_size( min( $sizes ) ) . ')';
1086 } else {
1087 $maxsize = '';
1088 }
1089 echo '<INPUT TYPE="hidden" name="MAX_FILE_SIZE" value="'.min( $sizes ).'">';
1090 echo ' <tr>' . "\n" .
1091 ' <td colspan="2">' . "\n" .
1092 ' <table width="100%" cellpadding="1" cellspacing="0" align="center"'.
1093 ' border="0" bgcolor="'.$color[9].'">' . "\n" .
1094 ' <tr>' . "\n" .
1095 ' <td>' . "\n" .
1096 ' <table width="100%" cellpadding="3" cellspacing="0" align="center"'.
1097 ' border="0">' . "\n" .
1098 ' <tr>' . "\n" .
1099 html_tag( 'td', '', 'right', '', 'valign="middle"' ) .
1100 _("Attach:") . '</td>' . "\n" .
1101 html_tag( 'td', '', 'left', '', 'valign="middle"' ) .
1102 ' <input name="attachfile" size="48" type="file" />' . "\n" .
1103 ' &nbsp;&nbsp;<input type="submit" name="attach"' .
1104 ' value="' . _("Add") .'">' . "\n" .
1105 $maxsize .
1106 ' </td>' . "\n" .
1107 ' </tr>' . "\n";
1108
1109
1110 $s_a = array();
1111 if ($composeMessage->entities) {
1112 foreach ($composeMessage->entities as $key => $attachment) {
1113 $attached_file = $attachment->att_local_name;
1114 if ($attachment->att_local_name || $attachment->body_part) {
1115 $attached_filename = decodeHeader($attachment->mime_header->getParameter('name'));
1116 $type = $attachment->mime_header->type0.'/'.
1117 $attachment->mime_header->type1;
1118
1119 $s_a[] = '<table bgcolor="'.$color[0].
1120 '" border="0"><tr><td><input type="checkbox" name="delete[]" value="' .
1121 $key . "\"></td><td>\n" . $attached_filename .
1122 '</td><td>-</td><td> ' . $type . '</td><td>('.
1123 show_readable_size( filesize( $attached_file ) ) . ')</td></tr></table>'."\n";
1124 }
1125 }
1126 }
1127 if (count($s_a)) {
1128 foreach ($s_a as $s) {
1129 echo '<tr>' . html_tag( 'td', '', 'left', $color[0], 'colspan="2"' ) . $s .'</td></tr>';
1130 }
1131 echo '<tr><td colspan="2"><input type="submit" name="do_delete" value="' .
1132 _("Delete selected attachments") . "\">\n" .
1133 '</td></tr>';
1134 }
1135 echo ' </table>' . "\n" .
1136 ' </td>' . "\n" .
1137 ' </tr>' . "\n" .
1138 ' </TABLE>' . "\n" .
1139 ' </TD>' . "\n" .
1140 ' </TR>' . "\n";
1141 } // End of file_uploads if-block
1142 /* End of attachment code */
1143 if ($compose_new_win == '1') {
1144 echo '</TABLE>'."\n";
1145 }
1146
1147 echo '</TABLE>' . "\n" .
1148 '<input type="hidden" name="username" value="'. $username . "\">\n" .
1149 '<input type=hidden name=action value="' . $action . "\">\n" .
1150 '<INPUT TYPE=hidden NAME=mailbox VALUE="' . htmlspecialchars($mailbox) .
1151 "\">\n";
1152 /*
1153 store the complete ComposeMessages array in a hidden input value
1154 so we can restore them in case of a session timeout.
1155 */
1156 sqgetGlobalVar('QUERY_STRING', $queryString, SQ_SERVER);
1157 echo '<input type=hidden name=restoremessages value="' . urlencode(serialize($compose_messages)) . "\">\n";
1158 echo '<input type=hidden name=composesession value="' . $composesession . "\">\n";
1159 echo '<input type=hidden name=querystring value="' . $queryString . "\">\n";
1160 echo '</FORM>';
1161 if (!(bool) ini_get('file_uploads')) {
1162 /* File uploads are off, so we didn't show that part of the form.
1163 To avoid bogus bug reports, tell the user why. */
1164 echo 'Because PHP file uploads are turned off, you can not attach files ';
1165 echo "to this message. Please see your system administrator for details.\r\n";
1166 }
1167
1168 do_hook('compose_bottom');
1169 echo '</BODY></HTML>' . "\n";
1170 }
1171
1172
1173 function showComposeButtonRow() {
1174 global $use_javascript_addr_book, $save_as_draft,
1175 $default_use_priority, $mailprio, $default_use_mdn,
1176 $request_mdn, $request_dr,
1177 $data_dir, $username;
1178
1179 echo ' <TR>' . "\n" .
1180 ' <TD></TD>' . "\n" .
1181 ' <TD>' . "\n";
1182 if ($default_use_priority) {
1183 if(!isset($mailprio)) {
1184 $mailprio = "3";
1185 }
1186 echo ' ' . _("Priority") .': <select name="mailprio">'.
1187 '<option value="1"'.($mailprio=='1'?' selected':'').'>'. _("High") .'</option>'.
1188 '<option value="3"'.($mailprio=='3'?' selected':'').'>'. _("Normal") .'</option>'.
1189 '<option value="5"'.($mailprio=='5'?' selected':'').'>'. _("Low").'</option>'.
1190 '</select>' . "\n";
1191 }
1192 $mdn_user_support=getPref($data_dir, $username, 'mdn_user_support',$default_use_mdn);
1193 if ($default_use_mdn) {
1194 if ($mdn_user_support) {
1195 echo ' ' . _("Receipt") .': '.
1196 '<input type="checkbox" name="request_mdn" value=1'.
1197 ($request_mdn=='1'?' checked':'') .'>'. _("On Read").
1198 ' <input type="checkbox" name="request_dr" value=1'.
1199 ($request_dr=='1'?' checked':'') .'>'. _("On Delivery");
1200 }
1201 }
1202
1203 echo ' </TD>' . "\n" .
1204 ' </TR>' . "\n" .
1205 ' <TR>' . "\n" .
1206 ' <TD></TD>' . "\n" .
1207 ' <TD>' . "\n" .
1208 ' <INPUT TYPE=SUBMIT NAME="sigappend" VALUE="' . _("Signature") . '">' . "\n";
1209 if ($use_javascript_addr_book) {
1210 echo " <SCRIPT LANGUAGE=JavaScript><!--\n document.write(\"".
1211 " <input type=button value=\\\""._("Addresses").
1212 "\\\" onclick='javascript:open_abook();'>\");".
1213 " // --></SCRIPT><NOSCRIPT>\n".
1214 " <input type=submit name=\"html_addr_search\" value=\"".
1215 _("Addresses")."\">".
1216 " </NOSCRIPT>\n";
1217 } else {
1218 echo ' <input type=submit name="html_addr_search" value="'.
1219 _("Addresses").'">' . "\n";
1220 }
1221
1222 if ($save_as_draft) {
1223 echo ' <input type="submit" name ="draft" value="' . _("Save Draft") . "\">\n";
1224 }
1225
1226 echo ' <INPUT TYPE=submit NAME=send VALUE="'. _("Send") . '">' . "\n";
1227 do_hook('compose_button_row');
1228
1229 echo ' </TD>' . "\n" .
1230 ' </TR>' . "\n\n";
1231 }
1232
1233 function checkInput ($show) {
1234 /*
1235 * I implemented the $show variable because the error messages
1236 * were getting sent before the page header. So, I check once
1237 * using $show=false, and then when i'm ready to display the error
1238 * message, show=true
1239 */
1240 global $body, $send_to, $send_to_bcc, $subject, $color;
1241
1242 if ($send_to == '' && $send_to_bcc == '') {
1243 if ($show) {
1244 plain_error_message(_("You have not filled in the \"To:\" field."), $color);
1245 }
1246 return false;
1247 }
1248 return true;
1249 } /* function checkInput() */
1250
1251
1252 /* True if FAILURE */
1253 function saveAttachedFiles($session) {
1254 global $_FILES, $attachment_dir, $attachments, $username,
1255 $data_dir, $compose_messages;
1256
1257 /* get out of here if no file was attached at all */
1258 if (! is_uploaded_file($_FILES['attachfile']['tmp_name']) ) {
1259 return true;
1260 }
1261
1262 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
1263 $localfilename = GenerateRandomString(32, '', 7);
1264 $full_localfilename = "$hashed_attachment_dir/$localfilename";
1265 while (file_exists($full_localfilename)) {
1266 $localfilename = GenerateRandomString(32, '', 7);
1267 $full_localfilename = "$hashed_attachment_dir/$localfilename";
1268 }
1269
1270 // FIXME: we SHOULD prefer move_uploaded_file over rename because
1271 // m_u_f works better with restricted PHP installes (safe_mode, open_basedir)
1272 if (!@rename($_FILES['attachfile']['tmp_name'], $full_localfilename)) {
1273 if (!@move_uploaded_file($_FILES['attachfile']['tmp_name'],$full_localfilename)) {
1274 return true;
1275 }
1276 }
1277 $message = $compose_messages[$session];
1278 $type = strtolower($_FILES['attachfile']['type']);
1279 $name = $_FILES['attachfile']['name'];
1280 $message->initAttachment($type, $name, $full_localfilename);
1281 $compose_messages[$session] = $message;
1282 sqsession_register($compose_messages , 'compose_messages');
1283 }
1284
1285 function ClearAttachments($composeMessage) {
1286 if ($composeMessage->att_local_name) {
1287 $attached_file = $composeMessage->att_local_name;
1288 if (file_exists($attached_file)) {
1289 unlink($attached_file);
1290 }
1291 }
1292 for ($i=0, $entCount=count($composeMessage->entities);$i< $entCount; ++$i) {
1293 ClearAttachments($composeMessage->entities[$i]);
1294 }
1295 }
1296
1297 /* parse values like 8M and 2k into bytes */
1298 function getByteSize($ini_size) {
1299
1300 if(!$ini_size) {
1301 return FALSE;
1302 }
1303
1304 $ini_size = trim($ini_size);
1305
1306 // if there's some kind of letter at the end of the string we need to multiply.
1307 if(!is_numeric(substr($ini_size, -1))) {
1308
1309 switch(strtoupper(substr($ini_size, -1))) {
1310 case 'G':
1311 $bytesize = 1073741824;
1312 break;
1313 case 'M':
1314 $bytesize = 1048576;
1315 break;
1316 case 'K':
1317 $bytesize = 1024;
1318 break;
1319 }
1320
1321 return ($bytesize * (int)substr($ini_size, 0, -1));
1322 }
1323
1324 return $ini_size;
1325 }
1326
1327
1328 /* temporary function to make use of the deliver class.
1329 In the future the responsable backend should be automaticly loaded
1330 and conf.pl should show a list of available backends.
1331 The message also should be constructed by the message class.
1332 */
1333
1334 function deliverMessage($composeMessage, $draft=false) {
1335 global $send_to, $send_to_cc, $send_to_bcc, $mailprio, $subject, $body,
1336 $username, $popuser, $usernamedata, $identity, $data_dir,
1337 $request_mdn, $request_dr, $default_charset, $color, $useSendmail,
1338 $domain, $action, $default_move_to_sent, $move_to_sent;
1339 global $imapServerAddress, $imapPort, $sent_folder, $key;
1340
1341 /* some browsers replace <space> by nonbreaking spaces &nbsp;
1342 by replacing them back to spaces addressparsing works */
1343 /* FIXME: How to handle in case of other charsets where "\240"
1344 is not a non breaking space ??? */
1345
1346 $send_to = str_replace("\240",' ',$send_to);
1347 $send_to_cc = str_replace("\240",' ',$send_to_cc);
1348 $send_to_bcc = str_replace("\240",' ',$send_to_bcc);
1349
1350 $rfc822_header = $composeMessage->rfc822_header;
1351
1352 $abook = addressbook_init(false, true);
1353 $rfc822_header->to = $rfc822_header->parseAddress($send_to,true, array(), '', $domain, array(&$abook,'lookup'));
1354 $rfc822_header->cc = $rfc822_header->parseAddress($send_to_cc,true,array(), '',$domain, array(&$abook,'lookup'));
1355 $rfc822_header->bcc = $rfc822_header->parseAddress($send_to_bcc,true, array(), '',$domain, array(&$abook,'lookup'));
1356 $rfc822_header->priority = $mailprio;
1357 $rfc822_header->subject = $subject;
1358 $special_encoding='';
1359 if (strtolower($default_charset) == 'iso-2022-jp') {
1360 if (mb_detect_encoding($body) == 'ASCII') {
1361 $special_encoding = '8bit';
1362 } else {
1363 $body = mb_convert_encoding($body, 'JIS');
1364 $special_encoding = '7bit';
1365 }
1366 }
1367 $composeMessage->setBody($body);
1368
1369 if (ereg("^([^@%/]+)[@%/](.+)$", $username, $usernamedata)) {
1370 $popuser = $usernamedata[1];
1371 $domain = $usernamedata[2];
1372 unset($usernamedata);
1373 } else {
1374 $popuser = $username;
1375 }
1376 $reply_to = '';
1377 if (isset($identity) && $identity != 'default') {
1378 $from_mail = getPref($data_dir, $username,'email_address' . $identity);
1379 $full_name = getPref($data_dir, $username,'full_name' . $identity);
1380 $reply_to = getPref($data_dir, $username,'reply_to' . $identity);
1381 } else {
1382 $from_mail = getPref($data_dir, $username, 'email_address');
1383 $full_name = getPref($data_dir, $username, 'full_name');
1384 $reply_to = getPref($data_dir, $username,'reply_to');
1385 }
1386 if (!$from_mail) {
1387 $from_mail = "$popuser@$domain";
1388 }
1389 $rfc822_header->from = $rfc822_header->parseAddress($from_mail,true);
1390 if ($full_name) {
1391 $from = $rfc822_header->from[0];
1392 if (!$from->host) $from->host = $domain;
1393 $full_name_encoded = encodeHeader($full_name);
1394 if ($full_name_encoded != $full_name) {
1395 $from_addr = $full_name_encoded .' <'.$from->mailbox.'@'.$from->host.'>';
1396 } else {
1397 $from_addr = '"'.$full_name .'" <'.$from->mailbox.'@'.$from->host.'>';
1398 }
1399 $rfc822_header->from = $rfc822_header->parseAddress($from_addr,true);
1400 }
1401 if ($reply_to) {
1402 $rfc822_header->reply_to = $rfc822_header->parseAddress($reply_to,true);
1403 }
1404 /* Receipt: On Read */
1405 if (isset($request_mdn) && $request_mdn) {
1406 $rfc822_header->dnt = $rfc822_header->parseAddress($from_mail,true);
1407 }
1408 /* Receipt: On Delivery */
1409 if (isset($request_dr) && $request_dr) {
1410 $rfc822_header->more_headers['Return-Receipt-To'] = $from_mail;
1411 }
1412 /* multipart messages */
1413 if (count($composeMessage->entities)) {
1414 $message_body = new Message();
1415 $message_body->body_part = $composeMessage->body_part;
1416 $composeMessage->body_part = '';
1417 $mime_header = new MessageHeader;
1418 $mime_header->type0 = 'text';
1419 $mime_header->type1 = 'plain';
1420 if ($special_encoding) {
1421 $mime_header->encoding = $special_encoding;
1422 } else {
1423 $mime_header->encoding = '8bit';
1424 }
1425 if ($default_charset) {
1426 $mime_header->parameters['charset'] = $default_charset;
1427 }
1428 $message_body->mime_header = $mime_header;
1429 array_unshift($composeMessage->entities, $message_body);
1430 $content_type = new ContentType('multipart/mixed');
1431 } else {
1432 $content_type = new ContentType('text/plain');
1433 if ($special_encoding) {
1434 $rfc822_header->encoding = $special_encoding;
1435 } else {
1436 $rfc822_header->encoding = '8bit';
1437 }
1438 if ($default_charset) {
1439 $content_type->properties['charset']=$default_charset;
1440 }
1441 }
1442
1443 $rfc822_header->content_type = $content_type;
1444 $composeMessage->rfc822_header = $rfc822_header;
1445
1446 /* Here you can modify the message structure just before we hand
1447 it over to deliver */
1448 do_hook('compose_send');
1449
1450 if (!$useSendmail && !$draft) {
1451 require_once(SM_PATH . 'class/deliver/Deliver_SMTP.class.php');
1452 $deliver = new Deliver_SMTP();
1453 global $smtpServerAddress, $smtpPort, $pop_before_smtp, $smtp_auth_mech;
1454
1455 if ($smtp_auth_mech == 'none') {
1456 $user = '';
1457 $pass = '';
1458 } else {
1459 global $key, $onetimepad;
1460 $user = $username;
1461 $pass = OneTimePadDecrypt($key, $onetimepad);
1462 }
1463
1464 $authPop = (isset($pop_before_smtp) && $pop_before_smtp) ? true : false;
1465 $stream = $deliver->initStream($composeMessage,$domain,0,
1466 $smtpServerAddress, $smtpPort, $user, $pass, $authPop);
1467 } elseif (!$draft) {
1468 require_once(SM_PATH . 'class/deliver/Deliver_SendMail.class.php');
1469 global $sendmail_path;
1470 $deliver = new Deliver_SendMail();
1471 $stream = $deliver->initStream($composeMessage,$sendmail_path);
1472 } elseif ($draft) {
1473 global $draft_folder;
1474 require_once(SM_PATH . 'class/deliver/Deliver_IMAP.class.php');
1475 $imap_stream = sqimap_login($username, $key, $imapServerAddress,
1476 $imapPort, 0);
1477 if (sqimap_mailbox_exists ($imap_stream, $draft_folder)) {
1478 require_once(SM_PATH . 'class/deliver/Deliver_IMAP.class.php');
1479 $imap_deliver = new Deliver_IMAP();
1480 $length = $imap_deliver->mail($composeMessage);
1481 sqimap_append ($imap_stream, $draft_folder, $length);
1482 $imap_deliver->mail($composeMessage, $imap_stream);
1483 sqimap_append_done ($imap_stream, $draft_folder);
1484 sqimap_logout($imap_stream);
1485 unset ($imap_deliver);
1486 return $length;
1487 } else {
1488 $msg = '<br>Error: '._("Draft folder")." $draft_folder" . ' does not exist.';
1489 plain_error_message($msg, $color);
1490 return false;
1491 }
1492 }
1493 $succes = false;
1494 if ($stream) {
1495 $length = $deliver->mail($composeMessage, $stream);
1496 $succes = $deliver->finalizeStream($stream);
1497 }
1498 if (!$succes) {
1499 $msg = $deliver->dlv_msg . '<br>' .
1500 _("Server replied: ") . $deliver->dlv_ret_nr . ' '.
1501 $deliver->dlv_server_msg;
1502 plain_error_message($msg, $color);
1503 } else {
1504 unset ($deliver);
1505 $move_to_sent = getPref($data_dir,$username,'move_to_sent');
1506 $imap_stream = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
1507
1508 /* Move to sent code */
1509 if (isset($default_move_to_sent) && ($default_move_to_sent != 0)) {
1510 $svr_allow_sent = true;
1511 } else {
1512 $svr_allow_sent = false;
1513 }
1514
1515 if (isset($sent_folder) && (($sent_folder != '') || ($sent_folder != 'none'))
1516 && sqimap_mailbox_exists( $imap_stream, $sent_folder)) {
1517 $fld_sent = true;
1518 } else {
1519 $fld_sent = false;
1520 }
1521
1522 if ((isset($move_to_sent) && ($move_to_sent != 0)) || (!isset($move_to_sent))) {
1523 $lcl_allow_sent = true;
1524 } else {
1525 $lcl_allow_sent = false;
1526 }
1527
1528 if (($fld_sent && $svr_allow_sent && !$lcl_allow_sent) || ($fld_sent && $lcl_allow_sent)) {
1529 sqimap_append ($imap_stream, $sent_folder, $length);
1530 require_once(SM_PATH . 'class/deliver/Deliver_IMAP.class.php');
1531 $imap_deliver = new Deliver_IMAP();
1532 $imap_deliver->mail($composeMessage, $imap_stream);
1533 sqimap_append_done ($imap_stream, $sent_folder);
1534 unset ($imap_deliver);
1535 }
1536 global $passed_id, $mailbox, $action;
1537 ClearAttachments($composeMessage);
1538 if ($action == 'reply' || $action == 'reply_all') {
1539 sqimap_mailbox_select ($imap_stream, $mailbox);
1540 sqimap_messages_flag ($imap_stream, $passed_id, $passed_id, 'Answered', true);
1541 }
1542 sqimap_logout($imap_stream);
1543 }
1544 return $succes;
1545 }
1546
1547 ?>