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