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