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