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