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