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