Added refreshing of left folder list using META tags. Editeded HTML for
[squirrelmail.git] / src / compose.php
1 <?
2 /** This code sends a mail.
3 **
4 ** There are 3 modes of operation:
5 ** - Start new mail
6 ** - Add an attachment
7 ** - Send mail
8 **/
9
10 include("../config/config.php");
11 include("../functions/strings.php");
12 include("../functions/page_header.php");
13 include("../functions/imap.php");
14 include("../functions/mailbox.php");
15 include("../functions/date.php");
16 include("../functions/mime.php");
17 include("../functions/smtp.php");
18 include("../functions/display_messages.php");
19
20 include("../src/load_prefs.php");
21
22 echo "<HTML><BODY TEXT=\"$color[8]\" BGCOLOR=\"$color[4]\" LINK=\"$color[7]\" VLINK=\"$color[7]\" ALINK=\"$color[7]\">\n";
23 $imapConnection = loginToImapServer($username, $key, $imapServerAddress, 0);
24 displayPageHeader($color, "None");
25
26 // This function is used when not sending or adding attachments
27 function newMail () {
28 global $forward_id, $imapConnection, $msg, $ent_num, $body_ary, $body,
29 $reply_id, $send_to, $send_to_cc, $mailbox;
30
31 if ($forward_id) {
32 selectMailbox($imapConnection, $mailbox, $numMessages);
33 $msg = fetchMessage($imapConnection, $forward_id, $mailbox);
34
35 if (containsType($msg, "text", "html", $ent_num)) {
36 $body = decodeBody($msg["ENTITIES"][$ent_num]["BODY"], $msg["ENTITIES"][$ent_num]["ENCODING"]);
37 } else if (containsType($msg, "text", "plain", $ent_num)) {
38 $body = decodeBody($msg["ENTITIES"][$ent_num]["BODY"], $msg["ENTITIES"][$ent_num]["ENCODING"]);
39 }
40 // add other primary displaying msg types here
41 else {
42 // find any type that's displayable
43 if (containsType($msg, "text", "any_type", $ent_num)) {
44 $body = decodeBody($msg["ENTITIES"][$ent_num]["BODY"], $msg["ENTITIES"][$ent_num]["ENCODING"]);
45 } else if (containsType($msg, "msg", "any_type", $ent_num)) {
46 $body = decodeBody($msg["ENTITIES"][$ent_num]["BODY"], $msg["ENTITIES"][$ent_num]["ENCODING"]);
47 } else {
48 $body = _("No Message");
49 }
50 }
51
52 $type1 = $msg["ENTITIES"][$ent_num]["TYPE1"];
53
54 $tmp = _("-------- Original Message ---------\n");
55 $body_ary = explode("\n", $body);
56 $body = "";
57 for ($i=0;$i < count($body_ary);$i++) {
58 if ($type1 == "html")
59 $tmp .= strip_tags($body_ary[$i]);
60 else
61 $tmp .= $body_ary[$i];
62 $body = "$body$tmp\n";
63 $tmp = "";
64 }
65 }
66
67 if ($reply_id) {
68 selectMailbox($imapConnection, $mailbox, $numMessages);
69 $msg = fetchMessage($imapConnection, $reply_id, $mailbox);
70
71 if (containsType($msg, "text", "html", $ent_num)) {
72 $body = decodeBody($msg["ENTITIES"][$ent_num]["BODY"], $msg["ENTITIES"][$ent_num]["ENCODING"], false);
73 } else if (containsType($msg, "text", "plain", $ent_num)) {
74 $body = decodeBody($msg["ENTITIES"][$ent_num]["BODY"], $msg["ENTITIES"][$ent_num]["ENCODING"], false);
75 }
76 // add other primary displaying msg types here
77 else {
78 // find any type that's displayable
79 if (containsType($msg, "text", "any_type", $ent_num)) {
80 $body = decodeBody($msg["ENTITIES"][$ent_num]["BODY"], $msg["ENTITIES"][$ent_num]["ENCODING"], false);
81 } else if (containsType($msg, "msg", "any_type", $ent_num)) {
82 $body = decodeBody($msg["ENTITIES"][$ent_num]["BODY"], $msg["ENTITIES"][$ent_num]["ENCODING"], false);
83 } else {
84 $body = _("No Message");
85 }
86 }
87
88 $type1 = $msg["ENTITIES"][$ent_num]["TYPE1"];
89
90 $body_ary = explode("\n", $body);
91 $body = "";
92 for ($i=0;$i < count($body_ary);$i++) {
93 if ($type1 == "html")
94 $tmp = strip_tags($body_ary[$i]);
95 else
96 $tmp = $body_ary[$i];
97 $body = "$body> $tmp\n";
98 }
99 }
100
101 // Add some decoding information
102 $send_to = encodeEmailAddr($send_to);
103 // parses the field and returns only the email address
104 $send_to = decodeEmailAddr($send_to);
105
106 $send_to = strtolower($send_to);
107 $send_to = ereg_replace("\"", "", $send_to);
108 $send_to = stripslashes($send_to);
109
110 /** This formats a CC string if they hit "reply all" **/
111 if ($send_to_cc != "") {
112 $send_to_cc = ereg_replace(";", ",", $send_to_cc);
113 $sendcc = explode(",", $send_to_cc);
114 $send_to_cc = "";
115
116 for ($i = 0; $i < count($sendcc); $i++) {
117 $sendcc[$i] = trim($sendcc[$i]);
118 if ($sendcc[$i] == "")
119 continue;
120
121 $sendcc[$i] = encodeEmailAddr($sendcc[$i]);
122 $sendcc[$i] = decodeEmailAddr($sendcc[$i]);
123
124 $whofrom = encodeEmailAddr($msg["HEADER"]["FROM"]);
125 $whofrom = decodeEmailAddr($whofrom);
126
127 $whoreplyto = encodeEmailAddr($msg["HEADER"]["REPLYTO"]);
128 $whoreplyto = decodeEmailAddr($whoreplyto);
129
130 if ((strtolower(trim($sendcc[$i])) != strtolower(trim($whofrom))) &&
131 (strtolower(trim($sendcc[$i])) != strtolower(trim($whoreplyto))) &&
132 (trim($sendcc[$i]) != "")) {
133 $send_to_cc .= trim($sendcc[$i]) . ", ";
134 }
135 }
136 $send_to_cc = trim($send_to_cc);
137 if (substr($send_to_cc, -1) == ",") {
138 $send_to_cc = substr($send_to_cc, 0, strlen($send_to_cc) - 1);
139 }
140 }
141 } // function newMail()
142
143 function showInputForm () {
144 global $send_to, $send_to_cc, $reply_subj, $forward_subj, $body,
145 $passed_body, $color, $use_signature, $signature, $editor_size,
146 $attachments, $subject;
147
148 echo "\n<FORM action=\"compose.php\" METHOD=POST\n";
149 echo "ENCTYPE=\"multipart/form-data\">\n";
150 echo "<TABLE COLS=2 WIDTH=50 ALIGN=CENTER CELLSPACING=0 BORDER=0>\n";
151 echo " <TR>\n";
152 echo " <TD WIDTH=50 BGCOLOR=\"$color[4]\" ALIGN=RIGHT>\n";
153 echo " <FONT FACE=\"Arial,Helvetica\">";
154 echo _("To:");
155 echo " </FONT>\n";
156 echo " </TD><TD WIDTH=% BGCOLOR=\"$color[4]\" ALIGN=LEFT>\n";
157 if ($send_to)
158 echo " <INPUT TYPE=TEXT NAME=send_to VALUE=\"$send_to\" SIZE=60><BR>";
159 else
160 echo " <INPUT TYPE=TEXT NAME=send_to SIZE=60><BR>";
161 echo " </TD>\n";
162 echo " </TR>\n";
163 echo " <TR>\n";
164 echo " <TD WIDTH=50 BGCOLOR=\"$color[4]\" ALIGN=RIGHT>\n";
165 echo " <FONT FACE=\"Arial,Helvetica\">"._("CC").":</FONT>\n";
166 echo " </TD><TD WIDTH=% BGCOLOR=\"$color[4]\" ALIGN=LEFT>\n";
167 if ($send_to_cc)
168 echo " <INPUT TYPE=TEXT NAME=send_to_cc SIZE=60 VALUE=\"$send_to_cc\"><BR>";
169 else
170 echo " <INPUT TYPE=TEXT NAME=send_to_cc SIZE=60><BR>";
171 echo " </TD>\n";
172 echo " </TR>\n";
173 echo " <TR>\n";
174 echo " <TD WIDTH=50 BGCOLOR=\"$color[4]\" ALIGN=RIGHT>\n";
175 echo " <FONT FACE=\"Arial,Helvetica\">BCC:</FONT>\n";
176 echo " </TD><TD WIDTH=% BGCOLOR=\"$color[4]\" ALIGN=LEFT>\n";
177 if ($send_to_bcc)
178 echo " <INPUT TYPE=TEXT NAME=send_to_bcc VALUE=\"$send_to_bcc\" SIZE=60><BR>";
179 else
180 echo " <INPUT TYPE=TEXT NAME=send_to_bcc SIZE=60><BR>";
181 echo " </TD>\n";
182 echo " </TR>\n";
183 echo " <TR>\n";
184 echo " <TD WIDTH=50 BGCOLOR=\"$color[4]\" ALIGN=RIGHT>\n";
185 echo " <FONT FACE=\"Arial,Helvetica\">";
186 echo _("Subject:");
187 echo " </FONT>\n";
188 echo " </TD><TD WIDTH=% BGCOLOR=\"$color[4]\" ALIGN=LEFT>\n";
189 if ($reply_subj) {
190 $reply_subj = str_replace("\"", "'", $reply_subj);
191 $reply_subj = stripslashes($reply_subj);
192 $reply_subj = trim($reply_subj);
193 if (substr(strtolower($reply_subj), 0, 3) != "re:")
194 $reply_subj = "Re: $reply_subj";
195 echo " <INPUT TYPE=TEXT NAME=subject SIZE=60 VALUE=\"$reply_subj\">";
196 } else if ($forward_subj) {
197 $forward_subj = str_replace("\"", "'", $forward_subj);
198 $forward_subj = stripslashes($forward_subj);
199 $forward_subj = trim($forward_subj);
200 if ((substr(strtolower($forward_subj), 0, 4) != "fwd:") &&
201 (substr(strtolower($forward_subj), 0, 5) != "[fwd:") &&
202 (substr(strtolower($forward_subj), 0, 6) != "[ fwd:"))
203 $forward_subj = "[Fwd: $forward_subj]";
204 echo " <INPUT TYPE=TEXT NAME=subject SIZE=60 VALUE=\"$forward_subj\">";
205 } else {
206 echo " <INPUT TYPE=TEXT NAME=subject VALUE=\"$subject\" SIZE=60>";
207 }
208 echo " </TD>\n";
209 echo " </TR>\n";
210
211 // This code is for attachments
212 echo " <tr>\n";
213 echo " <TD WIDTH=50 BGCOLOR=\"$color[4]\" VALIGN=TOP ALIGN=RIGHT>\n";
214 echo " <FONT FACE=\"Arial,Helvetica\">";
215 echo " <SMALL><BR></SMALL>"._("Attach:")."</FONT>\n";
216 echo " </td><td width=% ALIGN=left>\n";
217 // echo " <INPUT TYPE=\"hidden\" name=\"MAX_FILE_SIZE\"\n";
218 // echo " value=\"10000\">\n";
219 echo " <INPUT NAME=\"attachfile\" TYPE=\"file\">\n";
220 echo " &nbsp;&nbsp;<input type=\"submit\" name=\"attach\"\n";
221 echo " value=\"" . _("Add") ."\">\n";
222 echo " </td>\n";
223 echo " </font>\n";
224 echo " </tr>\n";
225 if (isset($attachments) && count($attachments)>0) {
226 echo "</tr><tr><td width=50 align=right>\n";
227 echo "</td><td width=% align=left>";
228 while (list($localname, $remotename) = each($attachments)) {
229 echo "<input type=\"checkbox\" name=\"delete[]\" value=\"$localname\">\n";
230 echo "$remotename <input type=\"hidden\" name=\"attachments[$localname]\" value=\"$remotename\"><br>\n";
231 }
232
233 echo "<input type=\"submit\" name=\"do_delete\" value=\""._("Delete selected attachments")."\">\n";
234 echo "</td></tr>";
235 }
236 // End of attachment code
237
238 echo " <TR>\n";
239 echo " <TD BGCOLOR=\"$color[4]\" COLSPAN=2>\n";
240 if ($use_signature == true)
241 echo " &nbsp;&nbsp;<TEXTAREA NAME=body ROWS=20 COLS=\"$editor_size\" WRAP=HARD>$body\n\n$signature</TEXTAREA><BR>";
242 else
243 echo " &nbsp;&nbsp;<TEXTAREA NAME=body ROWS=20 COLS=\"$editor_size\" WRAP=HARD>$body</TEXTAREA><BR>\n";
244 echo " </TD>\n";
245 echo " </TR>\n";
246 echo " <TR><TD COLSPAN=2 ALIGN=CENTER><INPUT TYPE=SUBMIT NAME=send VALUE=\"";
247 echo _("Send");
248 echo "\"></TD></TR>\n";
249 echo "</TABLE>\n";
250 echo "</FORM>";
251 }
252
253 function showSentForm () {
254 echo "<FONT FACE=\"Arial,Helvetica\">";
255 echo "<BR><BR><BR><CENTER><B>Message Sent!</B><BR><BR>";
256 echo "You will be automatically forwarded.<BR>If not, <A HREF=\"right_main.php\">click here</A>";
257 echo "</CENTER></FONT>";
258 }
259
260 function checkInput () {
261 global $body, $send_to, $subject;
262
263 if ($body == "") {
264 plain_error_message(_("You have not entered a message body."), $color);
265 return false;
266 } else if ($send_to == "") {
267 displayPageHeader($color, "None");
268 plain_error_message(_("You have not filled in the \"To:\" field."), $color);
269 return false;
270 } else if ($subject == "") {
271 plain_error_message(_("You have not entered a subject."), $color);
272 return false;
273 }
274 return true;
275 } // function checkInput()
276
277 if(isset($send)) {
278 if (checkInput()) {
279 sendMessage($send_to, $send_to_cc, $send_to_bcc, $subject, $body);
280 showSentForm();
281 } else {
282 showInputForm();
283 }
284 } else if (isset($attach)) {
285 $localfilename = md5("$attachfile, $attachfile_name, $REMOTE_IP, $REMOTE_PORT, $UNIQUE_ID, and everything else that may add entropy");
286 $localfilename = $data_dir.$localfilename;
287
288 // Put the file in a better place
289 error_reporting(0); // Rename will produce error output if it fails
290 if (!rename($attachfile, $localfilename)) {
291 if (!copy($attachfile, $localfilename)) {
292 plain_error_message(_("Could not move/copy file. File not attached"));
293 }
294 }
295 // If it still exists, PHP will remove the original file
296
297 // Write information about the file
298 $fp = fopen ($localfilename.".info", "w");
299 fputs ($fp, "$attachfile_type\n$attachfile_name\n");
300 fclose ($fp);
301
302 $attachments[$localfilename] = $attachfile_name;
303
304 showInputForm();
305 } else if (isset($do_delete)) {
306 while (list($key, $localname) = each($delete)) {
307 array_splice ($attachments, $localname, 1);
308 unlink ($localname);
309 unlink ($localname.".info");
310 }
311
312 showInputForm();
313 } else {
314 Newmail();
315 showInputForm();
316 }
317 ?>