Another workaround for an extra newline inside a form.
[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 $attachments = array();
45
46 // This function is used when not sending or adding attachments
47 function newMail () {
48 global $forward_id, $imapConnection, $msg, $ent_num, $body_ary, $body,
49 $reply_id, $send_to, $send_to_cc, $mailbox, $send_to_bcc, $editor_size;
50
51 $send_to = sqStripSlashes(decodeHeader($send_to));
52 $send_to_cc = sqStripSlashes(decodeHeader($send_to_cc));
53 $send_to_bcc = sqStripSlashes(decodeHeader($send_to_bcc));
54
55 if ($forward_id)
56 $id = $forward_id;
57 elseif ($reply_id)
58 $id = $reply_id;
59
60
61 if (isset($id)) {
62 sqimap_mailbox_select($imapConnection, $mailbox);
63 $message = sqimap_get_message($imapConnection, $id, $mailbox);
64 $orig_header = $message->header;
65 if ($ent_num)
66 $message = getEntity($message, $ent_num);
67
68 if ($message->header->type0 == "text" || $message->header->type1 == "message") {
69 if ($ent_num)
70 $body = decodeBody(mime_fetch_body($imapConnection, $id, $ent_num), $message->header->encoding);
71 else
72 $body = decodeBody(mime_fetch_body($imapConnection, $id, 1), $message->header->encoding);
73 } else {
74 $body = "";
75 }
76
77 if ($message->header->type1 == "html")
78 $body = strip_tags($body);
79
80 sqUnWordWrap($body);
81 $body_ary = explode("\n", $body);
82 $i = count($body_ary) - 1;
83 while (isset($body_ary[$i]) && ereg("^[>\s]*$", $body_ary[$i])) {
84 unset($body_ary[$i]);
85 $i --;
86 }
87 $body = "";
88 for ($i=0; $i < count($body_ary); $i++) {
89 if (! $forward_id)
90 {
91 if (ereg('^[\s>]+', $body_ary[$i]))
92 {
93 $body_ary[$i] = '>' . $body_ary[$i];
94 }
95 else
96 {
97 $body_ary[$i] = '> ' . $body_ary[$i];
98 }
99 }
100 sqWordWrap($body_ary[$i], $editor_size - 1);
101 $body .= $body_ary[$i] . "\n";
102 $body_ary[$i] = '';
103 }
104 if ($forward_id)
105 {
106 $bodyTop = "-------- " . _("Original Message") . " --------\n";
107 $bodyTop .= _("Subject") . ": " . $orig_header->subject . "\n";
108 $bodyTop .= _("From") . ": " . $orig_header->from . "\n";
109 $bodyTop .= _("To") . ": " . $orig_header->to[0] . "\n";
110 if (count($orig_header->to) > 1) {
111 for ($x=1; $x < count($orig_header->to); $x++) {
112 $bodyTop .= " " . $orig_header->to[$x] . "\n";
113 }
114 }
115 $bodyTop .= "\n";
116 $body = $bodyTop . $body;
117 }
118
119 $body = ereg_replace('\\\\', '\\\\', $body);
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, $mailbox); }
164
165 if (!$message->entities) {
166 if ($message->header->entity_id != $ent_num) {
167 $filename = decodeHeader($message->header->filename);
168
169 if ($filename == "")
170 $filename = "untitled-".$message->header->entity_id;
171
172 $localfilename = md5($filename.", $REMOTE_IP, REMOTE_PORT, $UNIQUE_ID, extra-stuff here");
173
174 // Write File Info
175 $fp = fopen ($attachment_dir.$localfilename.".info", "w");
176 fputs ($fp, strtolower($message->header->type0)."/".strtolower($message->header->type1)."\n".$filename."\n");
177 fclose ($fp);
178
179 // Write Attachment to file
180 $fp = fopen ($attachment_dir.$localfilename, "w");
181 fputs ($fp, decodeBody(mime_fetch_body($imapConnection, $forward_id, $message->header->entity_id), $message->header->encoding));
182 fclose ($fp);
183
184 $attachments[$localfilename] = $filename;
185
186 }
187 } else {
188 for ($i = 0; $i < count($message->entities); $i++) {
189 getAttachments($message->entities[$i]);
190 }
191 }
192 return;
193 }
194
195 function showInputForm () {
196 global $send_to, $send_to_cc, $reply_subj, $forward_subj, $body,
197 $passed_body, $color, $use_signature, $signature, $editor_size,
198 $attachments, $subject, $newmail, $use_javascript_addr_book,
199 $send_to_bcc, $reply_id, $mailbox, $from_htmladdr_search,
200 $location_of_buttons;
201
202 $subject = sqStripSlashes(decodeHeader($subject));
203 $reply_subj = decodeHeader($reply_subj);
204 $forward_subj = decodeHeader($forward_subj);
205 $body = sqStripSlashes($body);
206
207 if ($use_javascript_addr_book) {
208 echo "\n<SCRIPT LANGUAGE=JavaScript><!--\n";
209 echo "function open_abook() { \n";
210 echo " var nwin = window.open(\"addrbook_popup.php\",\"abookpopup\",";
211 echo "\"width=670,height=300,resizable=yes,scrollbars=yes\");\n";
212 echo " if((!nwin.opener) && (document.windows != null))\n";
213 echo " nwin.opener = document.windows;\n";
214 echo "}\n";
215 echo "// --></SCRIPT>\n\n";
216 }
217
218 echo "\n<FORM name=compose action=\"compose.php\" METHOD=POST ENCTYPE=\"multipart/form-data\"";
219 do_hook("compose_form");
220 echo ">\n";
221 if ($reply_id) {
222 echo "<input type=hidden name=reply_id value=$reply_id>\n";
223 }
224 printf("<INPUT TYPE=hidden NAME=mailbox VALUE=\"%s\">\n", htmlspecialchars($mailbox));
225 echo "<TABLE WIDTH=\"100%\" ALIGN=center CELLSPACING=0 BORDER=0>\n";
226
227 if ($location_of_buttons == 'top') showComposeButtonRow();
228
229 echo " <TR>\n";
230 echo " <TD BGCOLOR=\"$color[4]\" ALIGN=RIGHT>\n";
231 echo _("To:");
232 echo " </TD><TD BGCOLOR=\"$color[4]\">\n";
233 printf(" <INPUT TYPE=text NAME=\"send_to\" VALUE=\"%s\" SIZE=60><BR>\n",
234 htmlspecialchars($send_to));
235 echo " </TD>\n";
236 echo " </TR>\n";
237 echo " <TR>\n";
238 echo " <TD BGCOLOR=\"$color[4]\" ALIGN=RIGHT>\n";
239 echo _("CC:");
240 echo " </TD><TD BGCOLOR=\"$color[4]\" ALIGN=LEFT>\n";
241 printf(" <INPUT TYPE=text NAME=\"send_to_cc\" SIZE=60 VALUE=\"%s\"><BR>\n",
242 htmlspecialchars($send_to_cc));
243 echo " </TD>\n";
244 echo " </TR>\n";
245 echo " <TR>\n";
246 echo " <TD BGCOLOR=\"$color[4]\" ALIGN=RIGHT>\n";
247 echo _("BCC:");
248 echo " </TD><TD BGCOLOR=\"$color[4]\" ALIGN=LEFT>\n";
249 printf(" <INPUT TYPE=text NAME=\"send_to_bcc\" VALUE=\"%s\" SIZE=60><BR>\n",
250 htmlspecialchars($send_to_bcc));
251 echo "</TD></TR>\n";
252
253 echo " <TR>\n";
254 echo " <TD BGCOLOR=\"$color[4]\" ALIGN=RIGHT>\n";
255 echo _("Subject:");
256 echo " </TD><TD BGCOLOR=\"$color[4]\" ALIGN=LEFT>\n";
257 if ($reply_subj) {
258 $reply_subj = str_replace("\"", "'", $reply_subj);
259 $reply_subj = sqStripSlashes($reply_subj);
260 $reply_subj = trim($reply_subj);
261 if (substr(strtolower($reply_subj), 0, 3) != "re:")
262 $reply_subj = "Re: $reply_subj";
263 printf(" <INPUT TYPE=text NAME=subject SIZE=60 VALUE=\"%s\">",
264 htmlspecialchars($reply_subj));
265 } else if ($forward_subj) {
266 $forward_subj = str_replace("\"", "'", $forward_subj);
267 $forward_subj = sqStripSlashes($forward_subj);
268 $forward_subj = trim($forward_subj);
269 if ((substr(strtolower($forward_subj), 0, 4) != "fwd:") &&
270 (substr(strtolower($forward_subj), 0, 5) != "[fwd:") &&
271 (substr(strtolower($forward_subj), 0, 6) != "[ fwd:"))
272 $forward_subj = "[Fwd: $forward_subj]";
273 printf(" <INPUT TYPE=text NAME=subject SIZE=60 VALUE=\"%s\">",
274 htmlspecialchars($forward_subj));
275 } else {
276 printf(" <INPUT TYPE=text NAME=subject SIZE=60 VALUE=\"%s\">",
277 htmlspecialchars($subject));
278 }
279 echo "</td></tr>\n\n";
280
281 if ($location_of_buttons == 'between') showComposeButtonRow();
282
283 echo " <TR>\n";
284 echo " <TD BGCOLOR=\"$color[4]\" COLSPAN=2>\n";
285 echo " &nbsp;&nbsp;<TEXTAREA NAME=body ROWS=20 COLS=\"$editor_size\" WRAP=HARD>";
286 echo htmlspecialchars($body);
287 if ($use_signature == true && $newmail == true && !isset($from_htmladdr_search)) {
288 echo "\n\n-- \n" . htmlspecialchars($signature);
289 }
290 echo "</TEXTAREA><BR>\n";
291 echo " </TD>\n";
292 echo " </TR>\n";
293
294 if ($location_of_buttons == 'bottom')
295 showComposeButtonRow();
296 else {
297 echo " <TR><TD>&nbsp;</TD><TD ALIGN=LEFT><INPUT TYPE=SUBMIT NAME=send VALUE=\""._("Send")."\"></TD></TR>\n";
298 }
299
300 // This code is for attachments
301 echo " <tr>\n";
302 echo " <TD BGCOLOR=\"$color[0]\" VALIGN=TOP ALIGN=RIGHT>\n";
303 echo " <SMALL><BR></SMALL>"._("Attach:");
304 echo " </td><td ALIGN=left BGCOLOR=\"$color[0]\">\n";
305 echo " <INPUT NAME=\"attachfile\" SIZE=48 TYPE=\"file\">\n";
306 echo " &nbsp;&nbsp;<input type=\"submit\" name=\"attach\"";
307 echo " value=\"" . _("Add") ."\">\n";
308 echo " </td>\n";
309 echo " </tr>\n";
310 if (isset($attachments) && count($attachments)>0) {
311 echo "<tr><td bgcolor=\"$color[0]\" align=right>\n";
312 echo "&nbsp;";
313 echo "</td><td align=left bgcolor=\"$color[0]\">";
314 while (list($localname, $remotename) = each($attachments)) {
315 echo "<input type=\"checkbox\" name=\"delete[]\" value=\"$localname\">\n";
316 echo "$remotename <input type=\"hidden\" name=\"attachments[$localname]\" value=\"$remotename\"><br>\n";
317 }
318
319 echo "<input type=\"submit\" name=\"do_delete\" value=\""._("Delete selected attachments")."\">\n";
320 echo "</td></tr>";
321 }
322 // End of attachment code
323
324 echo "</TABLE>\n";
325 echo "</FORM>";
326 do_hook("compose_bottom");
327 }
328
329 function showComposeButtonRow() {
330 global $use_javascript_addr_book;
331 echo " <TR><td>\n </td><td>\n";
332 if ($use_javascript_addr_book) {
333 echo " <SCRIPT LANGUAGE=JavaScript><!--\n document.write(\"";
334 echo " <input type=button value=\\\""._("Addresses")."\\\" onclick='javascript:open_abook();'>\");";
335 echo " // --></SCRIPT><NOSCRIPT>\n";
336 echo " <input type=submit name=\"html_addr_search\" value=\""._("Addresses")."\">";
337 echo " </NOSCRIPT>\n";
338 } else {
339 echo " <input type=submit name=\"html_addr_search\" value=\""._("Addresses")."\">";
340 }
341 echo "\n <INPUT TYPE=SUBMIT NAME=send VALUE=\"". _("Send") . "\">\n";
342
343 do_hook("compose_button_row");
344
345 echo " </TD>\n";
346 echo " </TR>\n\n";
347 }
348
349 function showSentForm () {
350 echo "<BR><BR><BR><CENTER><B>Message Sent!</B><BR><BR>";
351 echo "You will be automatically forwarded.<BR>If not, <A HREF=\"right_main.php\">click here</A>";
352 echo "</CENTER>";
353 }
354
355 function checkInput ($show) {
356 /** I implemented the $show variable because the error messages
357 were getting sent before the page header. So, I check once
358 using $show=false, and then when i'm ready to display the
359 error message, show=true **/
360 global $body, $send_to, $subject, $color;
361
362 if ($send_to == "") {
363 if ($show)
364 plain_error_message(_("You have not filled in the \"To:\" field."), $color);
365 return false;
366 }
367 return true;
368 } // function checkInput()
369
370
371 // True if FAILURE
372 function saveAttachedFiles() {
373 global $HTTP_POST_FILES, $attachment_dir, $attachments;
374
375 is_logged_in();
376 $localfilename = GenerateRandomString(32, '', 7);
377
378 if (!@rename($HTTP_POST_FILES['attachfile']['tmp_name'], $attachment_dir.$localfilename)) {
379 if (!@copy($HTTP_POST_FILES['attachfile']['tmp_name'], $attachment_dir.$localfilename)) {
380 return true;
381 }
382 }
383
384 if (!isset($failed) || !$failed) {
385 // Write information about the file
386 $fp = fopen ($attachment_dir.$localfilename.".info", "w");
387 fputs ($fp, $HTTP_POST_FILES['attachfile']['type']."\n".$HTTP_POST_FILES['attachfile']['name']."\n");
388 fclose ($fp);
389
390 $attachments[$localfilename] = $HTTP_POST_FILES['attachfile']['name'];
391 }
392 }
393
394 if (!isset($mailbox) || $mailbox == "" || ($mailbox == "None"))
395 $mailbox = "INBOX";
396
397 if(isset($send)) {
398 if (isset($HTTP_POST_FILES['attachfile']) &&
399 $HTTP_POST_FILES['attachfile']['tmp_name'] &&
400 $HTTP_POST_FILES['attachfile']['tmp_name'] != 'none')
401 $AttachFailure = saveAttachedFiles();
402 if (checkInput(false) && ! isset($AttachFailure)) {
403 $mailbox = ereg_replace("([\n|\r])", '', $mailbox);
404 $urlMailbox = urlencode ($mailbox);
405 if (! isset($reply_id))
406 $reply_id = 0;
407 sendMessage($send_to, $send_to_cc, $send_to_bcc, $subject, $body, $reply_id);
408 header ("Location: right_main.php?mailbox=$urlMailbox&sort=$sort&startMessage=1");
409 } else {
410 //$imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
411 displayPageHeader($color, $mailbox);
412
413 if ($AttachFailure)
414 plain_error_message(_("Could not move/copy file. File not attached"), $color);
415
416 checkInput(true);
417
418 showInputForm();
419 //sqimap_logout($imapConnection);
420 }
421 } else if (isset($html_addr_search_done)) {
422 is_logged_in();
423 displayPageHeader($color, $mailbox);
424
425 $send_to = sqStripSlashes($send_to);
426 $send_to_cc = sqStripSlashes($send_to_cc);
427 $send_to_bcc = sqStripSlashes($send_to_bcc);
428
429 for ($i=0; $i < count($send_to_search); $i++) {
430 if ($send_to)
431 $send_to .= ", ";
432 $send_to .= $send_to_search[$i];
433 }
434
435 for ($i=0; $i < count($send_to_cc_search); $i++) {
436 if ($send_to_cc)
437 $send_to_cc .= ", ";
438 $send_to_cc .= $send_to_cc_search[$i];
439 }
440
441 showInputForm();
442 } else if (isset($html_addr_search)) {
443 // I am using an include so as to elminiate an extra unnecessary click. If you
444 // can think of a better way, please implement it.
445 include ("./addrbook_search_html.php");
446 } else if (isset($attach)) {
447 if (saveAttachedFiles())
448 plain_error_message(_("Could not move/copy file. File not attached"), $color);
449 displayPageHeader($color, $mailbox);
450 showInputForm();
451 } else if (isset($do_delete)) {
452 is_logged_in();
453 displayPageHeader($color, $mailbox);
454
455 while (list($lkey, $localname) = each($delete)) {
456 unset ($attachments[$localname]);
457 unlink ($attachment_dir.$localname);
458 unlink ($attachment_dir.$localname.".info");
459 }
460
461 showInputForm();
462 } else if (isset($smtpErrors)) {
463 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
464 displayPageHeader($color, $mailbox);
465
466 $newmail = true;
467 if ($forward_id && $ent_num) getAttachments(0);
468
469 newMail();
470 showInputForm();
471 sqimap_logout($imapConnection);
472 } else {
473 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
474 displayPageHeader($color, $mailbox);
475
476 $newmail = true;
477
478 if (isset($forward_id) && isset($ent_num)) getAttachments(0);
479
480 newMail();
481 showInputForm();
482 sqimap_logout($imapConnection);
483 }
484 ?>