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