* Fixed problem for forwarding files too.
[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 session_start();
19
20 if (!isset($strings_php))
21 include("../functions/strings.php");
22 if (!isset($config_php))
23 include("../config/config.php");
24 if (!isset($page_header_php))
25 include("../functions/page_header.php");
26 if (!isset($imap_php))
27 include("../functions/imap.php");
28 if (!isset($date_php))
29 include("../functions/date.php");
30 if (!isset($mime_php))
31 include("../functions/mime.php");
32 if (!isset($smtp_php))
33 include("../functions/smtp.php");
34 if (!isset($display_messages_php))
35 include("../functions/display_messages.php");
36 if (!isset($auth_php))
37 include ("../functions/auth.php");
38 if (!isset($plugin_php))
39 include ("../functions/plugin.php");
40
41 include("../src/load_prefs.php");
42
43 if (!isset($attachments))
44 {
45 $attachments = array();
46 }
47
48 // This function is used when not sending or adding attachments
49 function newMail () {
50 global $forward_id, $imapConnection, $msg, $ent_num, $body_ary, $body,
51 $reply_id, $send_to, $send_to_cc, $mailbox, $send_to_bcc, $editor_size;
52
53 $send_to = decodeHeader($send_to);
54 $send_to_cc = decodeHeader($send_to_cc);
55 $send_to_bcc = decodeHeader($send_to_bcc);
56
57 if ($forward_id)
58 $id = $forward_id;
59 elseif ($reply_id)
60 $id = $reply_id;
61
62
63 if (isset($id)) {
64 sqimap_mailbox_select($imapConnection, $mailbox);
65 $message = sqimap_get_message($imapConnection, $id, $mailbox);
66 $orig_header = $message->header;
67 if ($ent_num)
68 $message = getEntity($message, $ent_num);
69
70 if ($message->header->type0 == "text" || $message->header->type1 == "message") {
71 if ($ent_num)
72 $body = decodeBody(mime_fetch_body($imapConnection, $id, $ent_num), $message->header->encoding);
73 else
74 $body = decodeBody(mime_fetch_body($imapConnection, $id, 1), $message->header->encoding);
75 } else {
76 $body = "";
77 }
78
79 if ($message->header->type1 == "html")
80 $body = strip_tags($body);
81
82 sqUnWordWrap($body);
83 $body_ary = explode("\n", $body);
84 $i = count($body_ary) - 1;
85 while ($i >= 0 && ereg("^[>\\s]*$", $body_ary[$i])) {
86 unset($body_ary[$i]);
87 $i --;
88 }
89 $body = "";
90 for ($i=0; isset($body_ary[$i]); $i++) {
91 if (! $forward_id)
92 {
93 if (ereg('^[\\s>]+', $body_ary[$i]))
94 {
95 $body_ary[$i] = '>' . $body_ary[$i];
96 }
97 else
98 {
99 $body_ary[$i] = '> ' . $body_ary[$i];
100 }
101 }
102 sqWordWrap($body_ary[$i], $editor_size - 1);
103 $body .= $body_ary[$i] . "\n";
104 unset($body_ary[$i]);
105 }
106 if ($forward_id)
107 {
108 $bodyTop = "-------- " . _("Original Message") . " --------\n";
109 $bodyTop .= _("Subject") . ": " . $orig_header->subject . "\n";
110 $bodyTop .= _("From") . ": " . $orig_header->from . "\n";
111 $bodyTop .= _("To") . ": " . $orig_header->to[0] . "\n";
112 if (count($orig_header->to) > 1) {
113 for ($x=1; $x < count($orig_header->to); $x++) {
114 $bodyTop .= " " . $orig_header->to[$x] . "\n";
115 }
116 }
117 $bodyTop .= "\n";
118 $body = $bodyTop . $body;
119 }
120
121 return;
122 }
123
124 if (!$send_to) {
125 $send_to = sqimap_find_email($send_to);
126 }
127
128 /** This formats a CC string if they hit "reply all" **/
129 if ($send_to_cc != "") {
130 $send_to_cc = ereg_replace( '"[^"]*"', "", $send_to_cc);
131 $send_to_cc = ereg_replace(";", ",", $send_to_cc);
132 $sendcc = explode(",", $send_to_cc);
133 $send_to_cc = "";
134
135 for ($i = 0; $i < count($sendcc); $i++) {
136 $sendcc[$i] = trim($sendcc[$i]);
137 if ($sendcc[$i] == "")
138 continue;
139
140 $sendcc[$i] = sqimap_find_email($sendcc[$i]);
141 $whofrom = sqimap_find_displayable_name($msg["HEADER"]["FROM"]);
142 $whoreplyto = sqimap_find_email($msg["HEADER"]["REPLYTO"]);
143
144 if ((strtolower(trim($sendcc[$i])) != strtolower(trim($whofrom))) &&
145 (strtolower(trim($sendcc[$i])) != strtolower(trim($whoreplyto))) &&
146 (trim($sendcc[$i]) != "")) {
147 $send_to_cc .= trim($sendcc[$i]) . ", ";
148 }
149 }
150 $send_to_cc = trim($send_to_cc);
151 if (substr($send_to_cc, -1) == ",") {
152 $send_to_cc = substr($send_to_cc, 0, strlen($send_to_cc) - 1);
153 }
154 }
155 } // function newMail()
156
157 function getAttachments($message) {
158 global $mailbox, $attachments, $attachment_dir, $imapConnection,
159 $ent_num, $forward_id;
160
161 if (!$message) {
162 sqimap_mailbox_select($imapConnection, $mailbox);
163 $message = sqimap_get_message($imapConnection, $forward_id,
164 $mailbox);
165 }
166
167 if (count($message->entities) == 0) {
168 if ($message->header->entity_id != $ent_num) {
169 $filename = decodeHeader($message->header->filename);
170
171 if ($filename == "")
172 $filename = "untitled-".$message->header->entity_id;
173
174 $localfilename = GenerateRandomString(32, '', 7);
175 while (isset($attachments[$localfilename]))
176 $localfilename = GenerateRandomString(32, '', 7);
177
178 // Write File Info
179 $fp = fopen ($attachment_dir.$localfilename.".info", "w");
180 fputs ($fp, strtolower($message->header->type0)."/".
181 strtolower($message->header->type1)."\n".$filename."\n");
182 fclose ($fp);
183
184 // Write Attachment to file
185 $fp = fopen ($attachment_dir.$localfilename, "w");
186 fputs ($fp, decodeBody(mime_fetch_body($imapConnection,
187 $forward_id, $message->header->entity_id),
188 $message->header->encoding));
189 fclose ($fp);
190
191 $attachments[$localfilename] = $filename;
192 }
193 } else {
194 for ($i = 0; $i < count($message->entities); $i++) {
195 getAttachments($message->entities[$i]);
196 }
197 }
198 return;
199 }
200
201 function showInputForm () {
202 global $send_to, $send_to_cc, $reply_subj, $forward_subj, $body,
203 $passed_body, $color, $use_signature, $signature, $prefix_sig,
204 $editor_size, $attachments, $subject, $newmail,
205 $use_javascript_addr_book, $send_to_bcc, $reply_id, $mailbox,
206 $from_htmladdr_search, $location_of_buttons;
207
208 $subject = decodeHeader($subject);
209 $reply_subj = decodeHeader($reply_subj);
210 $forward_subj = decodeHeader($forward_subj);
211
212 if ($use_javascript_addr_book) {
213 echo "\n<SCRIPT LANGUAGE=JavaScript><!--\n";
214 echo "function open_abook() { \n";
215 echo " var nwin = window.open(\"addrbook_popup.php\",\"abookpopup\",";
216 echo "\"width=670,height=300,resizable=yes,scrollbars=yes\");\n";
217 echo " if((!nwin.opener) && (document.windows != null))\n";
218 echo " nwin.opener = document.windows;\n";
219 echo "}\n";
220 echo "// --></SCRIPT>\n\n";
221 }
222
223 echo "\n<FORM name=compose action=\"compose.php\" METHOD=POST ENCTYPE=\"multipart/form-data\"";
224 do_hook("compose_form");
225 echo ">\n";
226 if ($reply_id) {
227 echo "<input type=hidden name=reply_id value=$reply_id>\n";
228 }
229 printf("<INPUT TYPE=hidden NAME=mailbox VALUE=\"%s\">\n", htmlspecialchars($mailbox));
230 echo "<TABLE WIDTH=\"100%\" ALIGN=center CELLSPACING=0 BORDER=0>\n";
231
232 if ($location_of_buttons == 'top') showComposeButtonRow();
233
234 echo " <TR>\n";
235 echo " <TD BGCOLOR=\"$color[4]\" ALIGN=RIGHT>\n";
236 echo _("To:");
237 echo " </TD><TD BGCOLOR=\"$color[4]\">\n";
238 printf(" <INPUT TYPE=text NAME=\"send_to\" VALUE=\"%s\" SIZE=60><BR>\n",
239 htmlspecialchars($send_to));
240 echo " </TD>\n";
241 echo " </TR>\n";
242 echo " <TR>\n";
243 echo " <TD BGCOLOR=\"$color[4]\" ALIGN=RIGHT>\n";
244 echo _("CC:");
245 echo " </TD><TD BGCOLOR=\"$color[4]\" ALIGN=LEFT>\n";
246 printf(" <INPUT TYPE=text NAME=\"send_to_cc\" SIZE=60 VALUE=\"%s\"><BR>\n",
247 htmlspecialchars($send_to_cc));
248 echo " </TD>\n";
249 echo " </TR>\n";
250 echo " <TR>\n";
251 echo " <TD BGCOLOR=\"$color[4]\" ALIGN=RIGHT>\n";
252 echo _("BCC:");
253 echo " </TD><TD BGCOLOR=\"$color[4]\" ALIGN=LEFT>\n";
254 printf(" <INPUT TYPE=text NAME=\"send_to_bcc\" VALUE=\"%s\" SIZE=60><BR>\n",
255 htmlspecialchars($send_to_bcc));
256 echo "</TD></TR>\n";
257
258 echo " <TR>\n";
259 echo " <TD BGCOLOR=\"$color[4]\" ALIGN=RIGHT>\n";
260 echo _("Subject:");
261 echo " </TD><TD BGCOLOR=\"$color[4]\" ALIGN=LEFT>\n";
262 if ($reply_subj) {
263 $reply_subj = str_replace("\"", "'", $reply_subj);
264 $reply_subj = trim($reply_subj);
265 if (substr(strtolower($reply_subj), 0, 3) != "re:")
266 $reply_subj = "Re: $reply_subj";
267 printf(" <INPUT TYPE=text NAME=subject SIZE=60 VALUE=\"%s\">",
268 htmlspecialchars($reply_subj));
269 } else if ($forward_subj) {
270 $forward_subj = str_replace("\"", "'", $forward_subj);
271 $forward_subj = trim($forward_subj);
272 if ((substr(strtolower($forward_subj), 0, 4) != "fwd:") &&
273 (substr(strtolower($forward_subj), 0, 5) != "[fwd:") &&
274 (substr(strtolower($forward_subj), 0, 6) != "[ fwd:"))
275 $forward_subj = "[Fwd: $forward_subj]";
276 printf(" <INPUT TYPE=text NAME=subject SIZE=60 VALUE=\"%s\">",
277 htmlspecialchars($forward_subj));
278 } else {
279 printf(" <INPUT TYPE=text NAME=subject SIZE=60 VALUE=\"%s\">",
280 htmlspecialchars($subject));
281 }
282 echo "</td></tr>\n\n";
283
284 if ($location_of_buttons == 'between') showComposeButtonRow();
285
286 echo " <TR>\n";
287 echo " <TD BGCOLOR=\"$color[4]\" COLSPAN=2>\n";
288 echo " &nbsp;&nbsp;<TEXTAREA NAME=body ROWS=20 COLS=\"$editor_size\" WRAP=HARD>";
289 echo htmlspecialchars($body);
290 if ($use_signature == true && $newmail == true && !isset($from_htmladdr_search)) {
291 if ( $prefix_sig == true )
292 echo "\n\n-- \n" . htmlspecialchars($signature);
293 else
294 echo "\n\n" . htmlspecialchars($signature);
295 }
296 echo "</TEXTAREA><BR>\n";
297 echo " </TD>\n";
298 echo " </TR>\n";
299
300 if ($location_of_buttons == 'bottom')
301 showComposeButtonRow();
302 else {
303 echo " <TR><TD>&nbsp;</TD><TD ALIGN=LEFT><INPUT TYPE=SUBMIT NAME=send VALUE=\""._("Send")."\"></TD></TR>\n";
304 }
305
306 // This code is for attachments
307 echo " <tr bgcolor=\"$color[0]\">\n";
308 echo " <TD VALIGN=TOP ALIGN=RIGHT>"._("Attach:");
309 echo " </td><td ALIGN=left>\n";
310 echo " <INPUT NAME=\"attachfile\" SIZE=48 TYPE=\"file\">\n";
311 echo " &nbsp;&nbsp;<input type=\"submit\" name=\"attach\"";
312 echo " value=\"" . _("Add") ."\">\n";
313 echo " </td>\n";
314 echo " </tr>\n";
315 if (count($attachments) > 0) {
316 echo "<tr bgcolor=\"$color[0]\"><td align=right>\n";
317 echo "&nbsp;";
318 echo "</td><td align=left>";
319 foreach ($attachments as $localname => $remotename) {
320 echo "<input type=\"checkbox\" name=\"delete[]\" value=\"$localname\">\n";
321 echo "$remotename <input type=\"hidden\" name=\"attachments[$localname]\" value=\"$remotename\"><br>\n";
322 }
323
324 echo "<input type=\"submit\" name=\"do_delete\" value=\""._("Delete selected attachments")."\">\n";
325 echo "</td></tr>";
326 }
327 // End of attachment code
328
329 echo "</TABLE>\n";
330 echo "</FORM>";
331 do_hook("compose_bottom");
332 }
333
334 function showComposeButtonRow() {
335 global $use_javascript_addr_book;
336 echo " <TR><td>\n </td><td>\n";
337 if ($use_javascript_addr_book) {
338 echo " <SCRIPT LANGUAGE=JavaScript><!--\n document.write(\"";
339 echo " <input type=button value=\\\""._("Addresses")."\\\" onclick='javascript:open_abook();'>\");";
340 echo " // --></SCRIPT><NOSCRIPT>\n";
341 echo " <input type=submit name=\"html_addr_search\" value=\""._("Addresses")."\">";
342 echo " </NOSCRIPT>\n";
343 } else {
344 echo " <input type=submit name=\"html_addr_search\" value=\""._("Addresses")."\">";
345 }
346 echo "\n <INPUT TYPE=SUBMIT NAME=send VALUE=\"". _("Send") . "\">\n";
347
348 do_hook("compose_button_row");
349
350 echo " </TD>\n";
351 echo " </TR>\n\n";
352 }
353
354 function showSentForm () {
355 echo "<BR><BR><BR><CENTER><B>Message Sent!</B><BR><BR>";
356 echo "You will be automatically forwarded.<BR>If not, <A HREF=\"right_main.php\">click here</A>";
357 echo "</CENTER>";
358 }
359
360 function checkInput ($show) {
361 /** I implemented the $show variable because the error messages
362 were getting sent before the page header. So, I check once
363 using $show=false, and then when i'm ready to display the
364 error message, show=true **/
365 global $body, $send_to, $subject, $color;
366
367 if ($send_to == "") {
368 if ($show)
369 plain_error_message(_("You have not filled in the \"To:\" field."), $color);
370 return false;
371 }
372 return true;
373 } // function checkInput()
374
375
376 // True if FAILURE
377 function saveAttachedFiles() {
378 global $HTTP_POST_FILES, $attachment_dir, $attachments;
379
380 is_logged_in();
381 $localfilename = GenerateRandomString(32, '', 7);
382 while (isset($attachments[$localfilename]))
383 $localfilename = GenerateRandomString(32, '', 7);
384
385 if (!@rename($HTTP_POST_FILES['attachfile']['tmp_name'], $attachment_dir.$localfilename)) {
386 if (!@copy($HTTP_POST_FILES['attachfile']['tmp_name'], $attachment_dir.$localfilename)) {
387 return true;
388 }
389 }
390
391 if (!isset($failed) || !$failed) {
392 // Write information about the file
393 $fp = fopen ($attachment_dir.$localfilename.".info", "w");
394 fputs ($fp, $HTTP_POST_FILES['attachfile']['type']."\n".$HTTP_POST_FILES['attachfile']['name']."\n");
395 fclose ($fp);
396
397 $attachments[$localfilename] = $HTTP_POST_FILES['attachfile']['name'];
398 }
399 }
400
401 function SqConvertRussianCharsets(){
402 //
403 // This function is here because Russian Apache is a bastard when it comes to
404 // attachments. The solution is to turn off attachment recoding for multipart
405 // forms and do it manually.
406 // See graf@relhum.org for support.
407 //
408 global $CHARSET, $SOURCE_CHARSET, $send_to, $send_to_cc, $send_to_bcc, $subject, $body;
409 $charset_ary = array("koi8-r" => "k",
410 "windows-1251" => "w",
411 "ibm866" => "a",
412 "ISO-8859-5" => "i");
413 $body = convert_cyr_string($body, $charset_ary[$CHARSET], $charset_ary[$SOURCE_CHARSET]);
414 $send_to = convert_cyr_string($send_to, $charset_ary[$CHARSET], $charset_ary[$SOURCE_CHARSET]);
415 $send_to_cc = convert_cyr_string($send_to_cc, $charset_ary[$CHARSET], $charset_ary[$SOURCE_CHARSET]);
416 $send_to_bcc = convert_cyr_string($send_to_bcc, $charset_ary[$CHARSET], $charset_ary[$SOURCE_CHARSET]);
417 $subject = convert_cyr_string($subject, $charset_ary[$CHARSET], $charset_ary[$SOURCE_CHARSET]);
418 } // end SqConvertRussianCharsets()
419
420 // Russian Apache sets $CHARSET. See if this is Russian Apache.
421 // If so, check if the source charset (koi8-r) is different from the
422 // one submitted by the browser. If so, recode the parts of the form
423 // to the needed format so SM can proceed and not mangle the cyrillic
424 // input.
425 // See graf@relhum.org for support.
426 //
427 if (isset($CHARSET) && $CHARSET != $SOURCE_CHARSET) SqConvertRussianCharsets();
428
429 if (!isset($mailbox) || $mailbox == "" || ($mailbox == "None"))
430 $mailbox = "INBOX";
431
432 if(isset($send)) {
433 if (isset($HTTP_POST_FILES['attachfile']) &&
434 $HTTP_POST_FILES['attachfile']['tmp_name'] &&
435 $HTTP_POST_FILES['attachfile']['tmp_name'] != 'none')
436 $AttachFailure = saveAttachedFiles();
437 if (checkInput(false) && !isset($AttachFailure)) {
438 $urlMailbox = urlencode (trim($mailbox));
439 if (! isset($reply_id))
440 $reply_id = 0;
441 // Set $default_charset to correspond with the user's selection
442 // of language interface.
443 set_my_charset();
444 sendMessage($send_to, $send_to_cc, $send_to_bcc, $subject, $body, $reply_id);
445 header ("Location: right_main.php?mailbox=$urlMailbox&sort=$sort&startMessage=1");
446 } else {
447 //$imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
448 displayPageHeader($color, $mailbox);
449
450 if ($AttachFailure)
451 plain_error_message(_("Could not move/copy file. File not attached"), $color);
452
453 checkInput(true);
454
455 showInputForm();
456 //sqimap_logout($imapConnection);
457 }
458 } else if (isset($html_addr_search_done)) {
459 is_logged_in();
460 displayPageHeader($color, $mailbox);
461
462 for ($i=0; $i < count($send_to_search); $i++) {
463 if ($send_to)
464 $send_to .= ", ";
465 $send_to .= $send_to_search[$i];
466 }
467
468 for ($i=0; $i < count($send_to_cc_search); $i++) {
469 if ($send_to_cc)
470 $send_to_cc .= ", ";
471 $send_to_cc .= $send_to_cc_search[$i];
472 }
473
474 showInputForm();
475 } else if (isset($html_addr_search)) {
476 // I am using an include so as to elminiate an extra unnecessary click. If you
477 // can think of a better way, please implement it.
478 include ("./addrbook_search_html.php");
479 } else if (isset($attach)) {
480 if (saveAttachedFiles())
481 plain_error_message(_("Could not move/copy file. File not attached"), $color);
482 displayPageHeader($color, $mailbox);
483 showInputForm();
484 } else if (isset($do_delete)) {
485 is_logged_in();
486 displayPageHeader($color, $mailbox);
487
488 while (list($lkey, $localname) = each($delete)) {
489 unset ($attachments[$localname]);
490 unlink ($attachment_dir.$localname);
491 unlink ($attachment_dir.$localname.".info");
492 }
493
494 showInputForm();
495 } else {
496 // This handles the default case as well as the error case
497 // (they had the same code) --> if (isset($smtpErrors))
498 $imapConnection = sqimap_login($username, $key, $imapServerAddress,
499 $imapPort, 0);
500 displayPageHeader($color, $mailbox);
501
502 $newmail = true;
503 if (isset($forward_id) && $forward_id && isset($ent_num) && $ent_num)
504 getAttachments(0);
505
506 newMail();
507 showInputForm();
508 sqimap_logout($imapConnection);
509 }
510 ?>