Testing CVS access
[squirrelmail.git] / src / compose.php
1 <?php
2 /**
3 ** compose.php
4 **
5 ** Copyright (c) 1999-2000 The SquirrelMail development team
6 ** Licensed under the GNU GPL. For full terms see the file COPYING.
7 **
8 ** This code sends a mail.
9 **
10 ** There are 3 modes of operation:
11 ** - Start new mail
12 ** - Add an attachment
13 ** - Send mail
14 **
15 ** $Id$
16 **/
17
18 include('../src/validate.php');
19 include("../functions/imap.php");
20 include("../functions/date.php");
21 include("../functions/mime.php");
22 include("../functions/smtp.php");
23 include("../functions/display_messages.php");
24 include ("../functions/plugin.php");
25
26 if (!isset($attachments))
27 {
28 $attachments = array();
29 session_register('attachments');
30 }
31
32
33 // This function is used when not sending or adding attachments
34 function newMail () {
35 global $forward_id, $imapConnection, $msg, $ent_num, $body_ary, $body,
36 $reply_id, $send_to, $send_to_cc, $mailbox, $send_to_bcc, $editor_size;
37
38 $send_to = decodeHeader($send_to);
39 $send_to_cc = decodeHeader($send_to_cc);
40 $send_to_bcc = decodeHeader($send_to_bcc);
41
42 if ($forward_id)
43 $id = $forward_id;
44 elseif ($reply_id)
45 $id = $reply_id;
46
47
48 if (isset($id)) {
49 sqimap_mailbox_select($imapConnection, $mailbox);
50 $message = sqimap_get_message($imapConnection, $id, $mailbox);
51 $orig_header = $message->header;
52 if ($ent_num)
53 $message = getEntity($message, $ent_num);
54
55 if ($message->header->type0 == "text" || $message->header->type1 == "message") {
56 if ($ent_num)
57 $body = decodeBody(mime_fetch_body($imapConnection, $id, $ent_num), $message->header->encoding);
58 else
59 $body = decodeBody(mime_fetch_body($imapConnection, $id, 1), $message->header->encoding);
60 } else {
61 $body = "";
62 }
63
64 if ($message->header->type1 == "html")
65 $body = strip_tags($body);
66
67 sqUnWordWrap($body);
68 $body_ary = explode("\n", $body);
69 $i = count($body_ary) - 1;
70 while ($i >= 0 && ereg("^[>\\s]*$", $body_ary[$i])) {
71 unset($body_ary[$i]);
72 $i --;
73 }
74 $body = "";
75 for ($i=0; isset($body_ary[$i]); $i++) {
76 if (! $forward_id)
77 {
78 if (ereg('^[ >]+', $body_ary[$i]))
79 {
80 $body_ary[$i] = '>' . $body_ary[$i];
81 }
82 else
83 {
84 $body_ary[$i] = '> ' . $body_ary[$i];
85 }
86 }
87 sqWordWrap($body_ary[$i], $editor_size - 1);
88 $body .= $body_ary[$i] . "\n";
89 unset($body_ary[$i]);
90 }
91 if ($forward_id)
92 {
93 $bodyTop = "-------- " . _("Original Message") . " --------\n";
94 $bodyTop .= _("Subject") . ": " . $orig_header->subject . "\n";
95 $bodyTop .= _("From") . ": " . $orig_header->from . "\n";
96 $bodyTop .= _("To") . ": " . $orig_header->to[0] . "\n";
97 if (count($orig_header->to) > 1) {
98 for ($x=1; $x < count($orig_header->to); $x++) {
99 $bodyTop .= " " . $orig_header->to[$x] . "\n";
100 }
101 }
102 $bodyTop .= "\n";
103 $body = $bodyTop . $body;
104 } else if ($reply_id) {
105 $orig_from = $orig_header->from;
106 $orig_from = trim(substr($orig_from,0,strpos($orig_from,'<')));
107 $orig_from = str_replace('"','',$orig_from);
108 $orig_from = str_replace("'",'',$orig_from);
109 $body = getReplyCitation($orig_from) . $body;
110 }
111
112 return;
113 }
114
115 if (!$send_to) {
116 $send_to = sqimap_find_email($send_to);
117 }
118
119 /** This formats a CC string if they hit "reply all" **/
120 if ($send_to_cc != "") {
121 $send_to_cc = ereg_replace( '"[^"]*"', "", $send_to_cc);
122 $send_to_cc = ereg_replace(";", ",", $send_to_cc);
123 $sendcc = explode(",", $send_to_cc);
124 $send_to_cc = "";
125
126 for ($i = 0; $i < count($sendcc); $i++) {
127 $sendcc[$i] = trim($sendcc[$i]);
128 if ($sendcc[$i] == "")
129 continue;
130
131 $sendcc[$i] = sqimap_find_email($sendcc[$i]);
132 $whofrom = sqimap_find_displayable_name($msg["HEADER"]["FROM"]);
133 $whoreplyto = sqimap_find_email($msg["HEADER"]["REPLYTO"]);
134
135 if ((strtolower(trim($sendcc[$i])) != strtolower(trim($whofrom))) &&
136 (strtolower(trim($sendcc[$i])) != strtolower(trim($whoreplyto))) &&
137 (trim($sendcc[$i]) != "")) {
138 $send_to_cc .= trim($sendcc[$i]) . ", ";
139 }
140 }
141 $send_to_cc = trim($send_to_cc);
142 if (substr($send_to_cc, -1) == ",") {
143 $send_to_cc = substr($send_to_cc, 0, strlen($send_to_cc) - 1);
144 }
145 }
146 } // function newMail()
147
148 function getAttachments($message) {
149 global $mailbox, $attachments, $attachment_dir, $imapConnection,
150 $ent_num, $forward_id;
151
152 if (!$message) {
153 sqimap_mailbox_select($imapConnection, $mailbox);
154 $message = sqimap_get_message($imapConnection, $forward_id,
155 $mailbox);
156 }
157
158 if (count($message->entities) == 0) {
159 if ($message->header->entity_id != $ent_num) {
160 $filename = decodeHeader($message->header->filename);
161
162 if ($filename == "")
163 $filename = "untitled-".$message->header->entity_id;
164
165 $localfilename = GenerateRandomString(32, '', 7);
166 while (file_exists($attachment_dir . $localfilename))
167 $localfilename = GenerateRandomString(32, '', 7);
168
169 $newAttachment = array();
170 $newAttachment['localfilename'] = $localfilename;
171 $newAttachment['remotefilename'] = $filename;
172 $newAttachment['type'] = strtolower($message->header->type0 .
173 '/' . $message->header->type1);
174
175 // Write Attachment to file
176 $fp = fopen ($attachment_dir.$localfilename, 'w');
177 fputs ($fp, decodeBody(mime_fetch_body($imapConnection,
178 $forward_id, $message->header->entity_id),
179 $message->header->encoding));
180 fclose ($fp);
181
182 $attachments[] = $newAttachment;
183 }
184 } else {
185 for ($i = 0; $i < count($message->entities); $i++) {
186 getAttachments($message->entities[$i]);
187 }
188 }
189 return;
190 }
191
192 function showInputForm () {
193 global $send_to, $send_to_cc, $reply_subj, $forward_subj, $body,
194 $passed_body, $color, $use_signature, $signature, $prefix_sig,
195 $editor_size, $attachments, $subject, $newmail,
196 $use_javascript_addr_book, $send_to_bcc, $reply_id, $mailbox,
197 $from_htmladdr_search, $location_of_buttons, $attachment_dir,
198 $username, $data_dir, $identity;
199
200 $subject = decodeHeader($subject);
201 $reply_subj = decodeHeader($reply_subj);
202 $forward_subj = decodeHeader($forward_subj);
203
204 if ($use_javascript_addr_book) {
205 echo "\n<SCRIPT LANGUAGE=JavaScript><!--\n";
206 echo "function open_abook() { \n";
207 echo " var nwin = window.open(\"addrbook_popup.php\",\"abookpopup\",";
208 echo "\"width=670,height=300,resizable=yes,scrollbars=yes\");\n";
209 echo " if((!nwin.opener) && (document.windows != null))\n";
210 echo " nwin.opener = document.windows;\n";
211 echo "}\n";
212 echo "// --></SCRIPT>\n\n";
213 }
214
215 echo "\n<FORM name=compose action=\"compose.php\" METHOD=POST ENCTYPE=\"multipart/form-data\"";
216 do_hook("compose_form");
217 echo ">\n";
218 if ($reply_id) {
219 echo "<input type=hidden name=reply_id value=$reply_id>\n";
220 }
221 printf("<INPUT TYPE=hidden NAME=mailbox VALUE=\"%s\">\n", htmlspecialchars($mailbox));
222 echo "<TABLE WIDTH=\"100%\" ALIGN=center CELLSPACING=0 BORDER=0>\n";
223
224 if ($location_of_buttons == 'top') showComposeButtonRow();
225
226 $idents = getPref($data_dir, $username, 'identities');
227 if ($idents != '' && $idents > 1)
228 {
229 echo " <TR>\n";
230 echo " <TD BGCOLOR=\"$color[4]\" WIDTH=\"10%\" ALIGN=RIGHT>\n";
231 echo _("From:");
232 echo " </TD><TD BGCOLOR=\"$color[4]\" WIDTH=\"90%\">\n";
233 echo "<select name=identity>\n";
234 echo "<option value=default>" .
235 htmlspecialchars(getPref($data_dir, $username, 'full_name'));
236 $em = getPref($data_dir, $username, 'email_address');
237 if ($em != '')
238 echo htmlspecialchars(' <' . $em . '>') . "\n";
239 for ($i = 1; $i < $idents; $i ++) {
240 echo '<option value="' . $i . '"';
241 if (isset($identity) && $identity == $i)
242 echo ' SELECTED';
243 echo '>';
244 echo htmlspecialchars(getPref($data_dir, $username, 'full_name' .
245 $i));
246 $em = getPref($data_dir, $username, 'email_address' . $i);
247 if ($em != '')
248 echo htmlspecialchars(' <' . $em . '>') . "\n";
249 }
250 echo "</select>\n";
251 echo " </TD>\n";
252 echo " </TR>\n";
253 }
254 echo " <TR>\n";
255 echo " <TD BGCOLOR=\"$color[4]\" WIDTH=\"10%\" ALIGN=RIGHT>\n";
256 echo _("To:");
257 echo " </TD><TD BGCOLOR=\"$color[4]\" WIDTH=\"90%\">\n";
258 printf(" <INPUT TYPE=text NAME=\"send_to\" VALUE=\"%s\" SIZE=60><BR>\n",
259 htmlspecialchars($send_to));
260 echo " </TD>\n";
261 echo " </TR>\n";
262 echo " <TR>\n";
263 echo " <TD BGCOLOR=\"$color[4]\" ALIGN=RIGHT>\n";
264 echo _("CC:");
265 echo " </TD><TD BGCOLOR=\"$color[4]\" ALIGN=LEFT>\n";
266 printf(" <INPUT TYPE=text NAME=\"send_to_cc\" SIZE=60 VALUE=\"%s\"><BR>\n",
267 htmlspecialchars($send_to_cc));
268 echo " </TD>\n";
269 echo " </TR>\n";
270 echo " <TR>\n";
271 echo " <TD BGCOLOR=\"$color[4]\" ALIGN=RIGHT>\n";
272 echo _("BCC:");
273 echo " </TD><TD BGCOLOR=\"$color[4]\" ALIGN=LEFT>\n";
274 printf(" <INPUT TYPE=text NAME=\"send_to_bcc\" VALUE=\"%s\" SIZE=60><BR>\n",
275 htmlspecialchars($send_to_bcc));
276 echo "</TD></TR>\n";
277
278 echo " <TR>\n";
279 echo " <TD BGCOLOR=\"$color[4]\" ALIGN=RIGHT>\n";
280 echo _("Subject:");
281 echo " </TD><TD BGCOLOR=\"$color[4]\" ALIGN=LEFT>\n";
282 if ($reply_subj) {
283 $reply_subj = str_replace("\"", "'", $reply_subj);
284 $reply_subj = trim($reply_subj);
285 if (substr(strtolower($reply_subj), 0, 3) != "re:")
286 $reply_subj = "Re: $reply_subj";
287 printf(" <INPUT TYPE=text NAME=subject SIZE=60 VALUE=\"%s\">",
288 htmlspecialchars($reply_subj));
289 } else if ($forward_subj) {
290 $forward_subj = trim($forward_subj);
291 if ((substr(strtolower($forward_subj), 0, 4) != 'fwd:') &&
292 (substr(strtolower($forward_subj), 0, 5) != '[fwd:') &&
293 (substr(strtolower($forward_subj), 0, 6) != '[ fwd:'))
294 $forward_subj = "[Fwd: $forward_subj]";
295 printf(" <INPUT TYPE=text NAME=subject SIZE=60 VALUE=\"%s\">",
296 htmlspecialchars($forward_subj));
297 } else {
298 printf(" <INPUT TYPE=text NAME=subject SIZE=60 VALUE=\"%s\">",
299 htmlspecialchars($subject));
300 }
301 echo "</td></tr>\n\n";
302
303 if ($location_of_buttons == 'between') showComposeButtonRow();
304
305 echo " <TR>\n";
306 echo " <TD BGCOLOR=\"$color[4]\" COLSPAN=2>\n";
307 echo " &nbsp;&nbsp;<TEXTAREA NAME=body ROWS=20 COLS=\"$editor_size\" WRAP=HARD>";
308 echo htmlspecialchars($body);
309 if ($use_signature == true && $newmail == true && !isset($from_htmladdr_search)) {
310 if ( $prefix_sig == true )
311 echo "\n\n-- \n" . htmlspecialchars($signature);
312 else
313 echo "\n\n" . htmlspecialchars($signature);
314 }
315 echo "</TEXTAREA><BR>\n";
316 echo " </TD>\n";
317 echo " </TR>\n";
318
319 if ($location_of_buttons == 'bottom')
320 showComposeButtonRow();
321 else {
322 echo " <TR><TD>&nbsp;</TD><TD ALIGN=LEFT><INPUT TYPE=SUBMIT NAME=send VALUE=\""._("Send")."\"></TD></TR>\n";
323 }
324
325 // This code is for attachments
326 echo " <tr>\n";
327 echo " <TD BGCOLOR=\"$color[0]\" VALIGN=TOP ALIGN=RIGHT>\n";
328 echo " <SMALL><BR></SMALL>"._("Attach:");
329 echo " </td><td ALIGN=left BGCOLOR=\"$color[0]\">\n";
330 echo " <INPUT NAME=\"attachfile\" SIZE=48 TYPE=\"file\">\n";
331 echo " &nbsp;&nbsp;<input type=\"submit\" name=\"attach\"";
332 echo " value=\"" . _("Add") ."\">\n";
333 echo " </td>\n";
334 echo " </tr>\n";
335 if (count($attachments))
336 {
337 echo "<tr><td bgcolor=\"$color[0]\" align=right>\n";
338 echo "&nbsp;";
339 echo "</td><td align=left bgcolor=\"$color[0]\">";
340 foreach ($attachments as $key => $info) {
341 echo "<input type=\"checkbox\" name=\"delete[]\" value=\"$key\">\n";
342 echo $info['remotefilename'] . " - " . $info['type'] . " (";
343 echo show_readable_size(filesize($attachment_dir .
344 $info['localfilename'])) . ")<br>\n";
345 }
346
347 echo "<input type=\"submit\" name=\"do_delete\" value=\""._("Delete selected attachments")."\">\n";
348 echo "</td></tr>";
349 }
350 // End of attachment code
351
352 echo "</TABLE>\n";
353 echo "</FORM>";
354 do_hook("compose_bottom");
355 }
356
357 function showComposeButtonRow() {
358 global $use_javascript_addr_book;
359
360 echo " <TR><td>\n </td><td>\n";
361 if ($use_javascript_addr_book) {
362 echo " <SCRIPT LANGUAGE=JavaScript><!--\n document.write(\"";
363 echo " <input type=button value=\\\""._("Addresses")."\\\" onclick='javascript:open_abook();'>\");";
364 echo " // --></SCRIPT><NOSCRIPT>\n";
365 echo " <input type=submit name=\"html_addr_search\" value=\""._("Addresses")."\">";
366 echo " </NOSCRIPT>\n";
367 } else {
368 echo " <input type=submit name=\"html_addr_search\" value=\""._("Addresses")."\">";
369 }
370 echo "\n <INPUT TYPE=SUBMIT NAME=send VALUE=\"". _("Send") . "\">\n";
371
372 do_hook("compose_button_row");
373
374 echo " </TD>\n";
375 echo " </TR>\n\n";
376 }
377
378 function checkInput ($show) {
379 /** I implemented the $show variable because the error messages
380 were getting sent before the page header. So, I check once
381 using $show=false, and then when i'm ready to display the
382 error message, show=true **/
383 global $body, $send_to, $subject, $color;
384
385 if ($send_to == "") {
386 if ($show)
387 plain_error_message(_("You have not filled in the \"To:\" field."), $color);
388 return false;
389 }
390 return true;
391 } // function checkInput()
392
393
394 // True if FAILURE
395 function saveAttachedFiles() {
396 global $HTTP_POST_FILES, $attachment_dir, $attachments;
397
398 $localfilename = GenerateRandomString(32, '', 7);
399 while (file_exists($attachment_dir . $localfilename))
400 $localfilename = GenerateRandomString(32, '', 7);
401
402 if (!@rename($HTTP_POST_FILES['attachfile']['tmp_name'], $attachment_dir.$localfilename)) {
403 if (!@copy($HTTP_POST_FILES['attachfile']['tmp_name'], $attachment_dir.$localfilename)) {
404 return true;
405 }
406 }
407
408 $newAttachment['localfilename'] = $localfilename;
409 $newAttachment['remotefilename'] = $HTTP_POST_FILES['attachfile']['name'];
410 $newAttachment['type'] =
411 strtolower($HTTP_POST_FILES['attachfile']['type']);
412
413 if ($newAttachment['type'] == "")
414 $newAttachment['type'] = 'application/octet-stream';
415
416 $attachments[] = $newAttachment;
417 }
418
419 if (!isset($mailbox) || $mailbox == "" || ($mailbox == "None"))
420 $mailbox = "INBOX";
421
422 if (isset($send)) {
423 if (isset($HTTP_POST_FILES['attachfile']) &&
424 $HTTP_POST_FILES['attachfile']['tmp_name'] &&
425 $HTTP_POST_FILES['attachfile']['tmp_name'] != 'none')
426 $AttachFailure = saveAttachedFiles();
427 if (checkInput(false) && !isset($AttachFailure)) {
428 $urlMailbox = urlencode (trim($mailbox));
429 if (! isset($reply_id))
430 $reply_id = 0;
431 // Set $default_charset to correspond with the user's selection
432 // of language interface.
433 set_my_charset();
434
435 // This is to change all newlines to \n
436 // We'll change them to \r\n later (in the sendMessage function)
437 $body = str_replace("\r\n", "\n", $body);
438 $body = str_replace("\r", "\n", $body);
439
440 // Rewrap $body so that no line is bigger than $editor_size
441 // This should only really kick in the sqWordWrap function
442 // if the browser doesn't support "HARD" as the wrap type
443 // Or, in Opera's case, something goes wrong.
444 $body = explode("\n", $body);
445 $newBody = '';
446 foreach ($body as $line) {
447 $line = rtrim($line);
448 if (strlen($line) <= $editor_size + 1)
449 $newBody .= $line . "\n";
450 else {
451 sqWordWrap($line, $editor_size) . "\n";
452 $newBody .= $line;
453 }
454 }
455 $body = $newBody;
456
457 do_hook("compose_send");
458
459 if (! sendMessage($send_to, $send_to_cc, $send_to_bcc, $subject, $body, $reply_id)) {
460 showInputForm();
461 exit();
462 }
463 Header("Location: right_main.php?mailbox=$urlMailbox&sort=$sort&startMessage=1");
464 } else {
465 //$imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
466 displayPageHeader($color, $mailbox);
467
468 if (isset($AttachFailure))
469 plain_error_message(_("Could not move/copy file. File not attached"), $color);
470
471 checkInput(true);
472
473 showInputForm();
474 //sqimap_logout($imapConnection);
475 }
476 } else if (isset($html_addr_search_done)) {
477 displayPageHeader($color, $mailbox);
478
479 if (isset($send_to_search) && is_array($send_to_search)) {
480 foreach ($send_to_search as $k => $v) {
481 if (substr($k, 0, 1) == 'T') {
482 if ($send_to)
483 $send_to .= ', ';
484 $send_to .= $v;
485 }
486 elseif (substr($k, 0, 1) == 'C') {
487 if ($send_to_cc)
488 $send_to_cc .= ', ';
489 $send_to_cc .= $v;
490 }
491 elseif (substr($k, 0, 1) == 'B') {
492 if ($send_to_bcc)
493 $send_to_bcc .= ', ';
494 $send_to_bcc .= $v;
495 }
496 }
497 }
498
499 showInputForm();
500 } else if (isset($html_addr_search)) {
501 if (isset($HTTP_POST_FILES['attachfile']) &&
502 $HTTP_POST_FILES['attachfile']['tmp_name'] &&
503 $HTTP_POST_FILES['attachfile']['tmp_name'] != 'none')
504 {
505 if (saveAttachedFiles())
506 plain_error_message(_("Could not move/copy file. File not attached"), $color);
507 }
508 // I am using an include so as to elminiate an extra unnecessary click. If you
509 // can think of a better way, please implement it.
510 include ("./addrbook_search_html.php");
511 } else if (isset($attach)) {
512 if (saveAttachedFiles())
513 plain_error_message(_("Could not move/copy file. File not attached"), $color);
514 displayPageHeader($color, $mailbox);
515 showInputForm();
516 } else if (isset($do_delete)) {
517 displayPageHeader($color, $mailbox);
518
519 if (isset($delete) && is_array($delete))
520 {
521 foreach($delete as $index)
522 {
523 unlink ($attachment_dir.$attachments[$index]['localfilename']);
524 unset ($attachments[$index]);
525 }
526 }
527
528 showInputForm();
529 } else {
530 // This handles the default case as well as the error case
531 // (they had the same code) --> if (isset($smtpErrors))
532 $imapConnection = sqimap_login($username, $key, $imapServerAddress,
533 $imapPort, 0);
534 displayPageHeader($color, $mailbox);
535
536 $newmail = true;
537
538 ClearAttachments();
539
540 if (isset($forward_id) && $forward_id && isset($ent_num) && $ent_num)
541 getAttachments(0);
542
543 newMail();
544 showInputForm();
545 sqimap_logout($imapConnection);
546 }
547
548 function ClearAttachments() {
549 global $attachments, $attachment_dir;
550
551 foreach ($attachments as $info) {
552 if (file_exists($attachment_dir . $info['localfilename'])) {
553 unlink($attachment_dir . $info['localfilename']);
554 }
555 }
556
557 $attachments = array();
558 }
559
560 function getReplyCitation($orig_from) {
561 global $reply_citation_style, $reply_citation_start, $reply_citation_end;
562
563 /* First, return an empty string when no citation style selected. */
564 if (($reply_citation_style == '') || ($reply_citation_style == 'none')) {
565 return ('');
566 }
567
568 /* Otherwise, try to select the desired citation style. */
569 switch ($reply_citation_style) {
570 case 'author_said':
571 $start = '';
572 $end = ' ' . _("said") . ':';
573 break;
574 case 'quote_who':
575 $start = '<' . _("quote") . ' ' . _("who") . '="';
576 $end = '">';
577 break;
578 case 'user-defined':
579 $start = $reply_citation_start;
580 $end = $reply_citation_end;
581 break;
582 default: return ('');
583 }
584
585 /* Build and return the citation string. */
586 return ($start . $orig_from . $end . "\n");
587 }
588 ?>