fixed default setting for Exhcnage
[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
245a6892 14 **
15 ** $Id$
df15de21 16 **/
17
2a32fc83 18 session_start();
19
d068c0ec 20 if (!isset($strings_php))
21 include("../functions/strings.php");
245a6892 22 if (!isset($config_php))
23 include("../config/config.php");
d068c0ec 24 if (!isset($page_header_php))
25 include("../functions/page_header.php");
26 if (!isset($imap_php))
27 include("../functions/imap.php");
28 if (!isset($date_php))
29 include("../functions/date.php");
30 if (!isset($mime_php))
31 include("../functions/mime.php");
32 if (!isset($smtp_php))
33 include("../functions/smtp.php");
34 if (!isset($display_messages_php))
35 include("../functions/display_messages.php");
3c13b9fb 36 if (!isset($auth_php))
37 include ("../functions/auth.php");
15bfc1bc 38 if (!isset($plugin_php))
39 include ("../functions/plugin.php");
f7fb20fe 40
d3cdb279 41 include("../src/load_prefs.php");
8467bf00 42
79be8a0b 43 if (!isset($attachments))
44 $attachments = array();
45
4ba45d11 46 // This function is used when not sending or adding attachments
df15de21 47 function newMail () {
48 global $forward_id, $imapConnection, $msg, $ent_num, $body_ary, $body,
a794e82c 49 $reply_id, $send_to, $send_to_cc, $mailbox, $send_to_bcc, $editor_size;
e39d73e5 50
7aaa81fc 51 $send_to = sqStripSlashes(decodeHeader($send_to));
52 $send_to_cc = sqStripSlashes(decodeHeader($send_to_cc));
6e79bfe2 53 $send_to_bcc = sqStripSlashes(decodeHeader($send_to_bcc));
a53e5469 54
429f8906 55 if ($forward_id)
56 $id = $forward_id;
3b5b889f 57 elseif ($reply_id)
429f8906 58 $id = $reply_id;
59
1195c340 60
245a6892 61 if (isset($id)) {
813eba2f 62 sqimap_mailbox_select($imapConnection, $mailbox);
429f8906 63 $message = sqimap_get_message($imapConnection, $id, $mailbox);
8d8ab69a 64 $orig_header = $message->header;
1195c340 65 if ($ent_num)
66 $message = getEntity($message, $ent_num);
429f8906 67
68 if ($message->header->type0 == "text" || $message->header->type1 == "message") {
1195c340 69 if ($ent_num)
70 $body = decodeBody(mime_fetch_body($imapConnection, $id, $ent_num), $message->header->encoding);
71 else
72 $body = decodeBody(mime_fetch_body($imapConnection, $id, 1), $message->header->encoding);
429f8906 73 } else {
74 $body = "";
df15de21 75 }
76
429f8906 77 if ($message->header->type1 == "html")
78 $body = strip_tags($body);
01aab860 79
80 sqUnWordWrap($body);
a794e82c 81 $body_ary = explode("\n", $body);
fb6ce88e 82 $i = count($body_ary) - 1;
83 while (isset($body_ary[$i]) && ereg("^[>\s]*$", $body_ary[$i])) {
84 unset($body_ary[$i]);
85 $i --;
441f2d33 86 }
a794e82c 87 $body = "";
88 for ($i=0; $i < count($body_ary); $i++) {
01aab860 89 if (! $forward_id)
90 {
441f2d33 91 if (ereg('^[\s>]+', $body_ary[$i]))
01aab860 92 {
93 $body_ary[$i] = '>' . $body_ary[$i];
94 }
95 else
96 {
97 $body_ary[$i] = '> ' . $body_ary[$i];
98 }
99 }
a794e82c 100 sqWordWrap($body_ary[$i], $editor_size - 1);
01aab860 101 $body .= $body_ary[$i] . "\n";
102 $body_ary[$i] = '';
a794e82c 103 }
01aab860 104 if ($forward_id)
105 {
106 $bodyTop = "-------- " . _("Original Message") . " --------\n";
107 $bodyTop .= _("Subject") . ": " . $orig_header->subject . "\n";
108 $bodyTop .= _("From") . ": " . $orig_header->from . "\n";
109 $bodyTop .= _("To") . ": " . $orig_header->to[0] . "\n";
110 if (count($orig_header->to) > 1) {
111 for ($x=1; $x < count($orig_header->to); $x++) {
112 $bodyTop .= " " . $orig_header->to[$x] . "\n";
113 }
114 }
115 $bodyTop .= "\n";
116 $body = $bodyTop . $body;
78509c54 117 }
441f2d33 118
01aab860 119 return;
78509c54 120 }
429f8906 121
29d08a52 122 if (!$send_to) {
123 $send_to = sqimap_find_email($send_to);
124 }
125
df15de21 126 /** This formats a CC string if they hit "reply all" **/
127 if ($send_to_cc != "") {
a48fbf9b 128 $send_to_cc = ereg_replace( '"[^"]*"', "", $send_to_cc);
df15de21 129 $send_to_cc = ereg_replace(";", ",", $send_to_cc);
130 $sendcc = explode(",", $send_to_cc);
131 $send_to_cc = "";
132
133 for ($i = 0; $i < count($sendcc); $i++) {
134 $sendcc[$i] = trim($sendcc[$i]);
135 if ($sendcc[$i] == "")
136 continue;
137
a53e5469 138 $sendcc[$i] = sqimap_find_email($sendcc[$i]);
813eba2f 139 $whofrom = sqimap_find_displayable_name($msg["HEADER"]["FROM"]);
140 $whoreplyto = sqimap_find_email($msg["HEADER"]["REPLYTO"]);
df15de21 141
142 if ((strtolower(trim($sendcc[$i])) != strtolower(trim($whofrom))) &&
143 (strtolower(trim($sendcc[$i])) != strtolower(trim($whoreplyto))) &&
144 (trim($sendcc[$i]) != "")) {
145 $send_to_cc .= trim($sendcc[$i]) . ", ";
146 }
147 }
148 $send_to_cc = trim($send_to_cc);
149 if (substr($send_to_cc, -1) == ",") {
150 $send_to_cc = substr($send_to_cc, 0, strlen($send_to_cc) - 1);
151 }
152 }
153 } // function newMail()
78509c54 154
5713b4f9 155 function getAttachments($message) {
156 global $mailbox, $attachments, $attachment_dir, $imapConnection,
157 $ent_num, $forward_id;
158
159 if (!$message) {
160 sqimap_mailbox_select($imapConnection, $mailbox);
161 $message = sqimap_get_message($imapConnection, $forward_id, $mailbox); }
162
163 if (!$message->entities) {
164 if ($message->header->entity_id != $ent_num) {
165 $filename = decodeHeader($message->header->filename);
166
167 if ($filename == "")
168 $filename = "untitled-".$message->header->entity_id;
169
170 $localfilename = md5($filename.", $REMOTE_IP, REMOTE_PORT, $UNIQUE_ID, extra-stuff here");
171
172 // Write File Info
173 $fp = fopen ($attachment_dir.$localfilename.".info", "w");
174 fputs ($fp, strtolower($message->header->type0)."/".strtolower($message->header->type1)."\n".$filename."\n");
175 fclose ($fp);
176
177 // Write Attachment to file
178 $fp = fopen ($attachment_dir.$localfilename, "w");
179 fputs ($fp, decodeBody(mime_fetch_body($imapConnection, $forward_id, $message->header->entity_id), $message->header->encoding));
3b5b889f 180 fclose ($fp);
181
5713b4f9 182 $attachments[$localfilename] = $filename;
183
184 }
185 } else {
186 for ($i = 0; $i < count($message->entities); $i++) {
187 getAttachments($message->entities[$i]);
188 }
189 }
190 return;
191 }
192
df15de21 193 function showInputForm () {
194 global $send_to, $send_to_cc, $reply_subj, $forward_subj, $body,
4ba45d11 195 $passed_body, $color, $use_signature, $signature, $editor_size,
3806fa52 196 $attachments, $subject, $newmail, $use_javascript_addr_book,
441f2d33 197 $send_to_bcc, $reply_id, $mailbox, $from_htmladdr_search,
198 $location_of_buttons;
78509c54 199
5f104808 200 $subject = sqStripSlashes(decodeHeader($subject));
2e434774 201 $reply_subj = decodeHeader($reply_subj);
202 $forward_subj = decodeHeader($forward_subj);
162efb6f 203 $body = sqStripSlashes($body);
01aab860 204
3806fa52 205 if ($use_javascript_addr_book) {
206 echo "\n<SCRIPT LANGUAGE=JavaScript><!--\n";
207 echo "function open_abook() { \n";
208 echo " var nwin = window.open(\"addrbook_popup.php\",\"abookpopup\",";
209 echo "\"width=670,height=300,resizable=yes,scrollbars=yes\");\n";
210 echo " if((!nwin.opener) && (document.windows != null))\n";
211 echo " nwin.opener = document.windows;\n";
212 echo "}\n";
213 echo "// --></SCRIPT>\n\n";
214 }
5100704d 215
b3911477 216 echo "\n<FORM name=compose action=\"compose.php\" METHOD=POST ENCTYPE=\"multipart/form-data\"";
217 do_hook("compose_form");
218 echo ">\n";
966286ae 219 if ($reply_id) {
220 echo "<input type=hidden name=reply_id value=$reply_id>\n";
6e79bfe2 221 }
cf8758c7 222 printf("<INPUT TYPE=hidden NAME=mailbox VALUE=\"%s\">\n", htmlspecialchars($mailbox));
c5d828b3 223 echo "<TABLE WIDTH=\"100%\" ALIGN=center CELLSPACING=0 BORDER=0>\n";
441f2d33 224
225 if ($location_of_buttons == 'top') showComposeButtonRow();
226
df15de21 227 echo " <TR>\n";
c5d828b3 228 echo " <TD BGCOLOR=\"$color[4]\" ALIGN=RIGHT>\n";
df15de21 229 echo _("To:");
c5d828b3 230 echo " </TD><TD BGCOLOR=\"$color[4]\">\n";
cf8758c7 231 printf(" <INPUT TYPE=text NAME=\"send_to\" VALUE=\"%s\" SIZE=60><BR>\n",
6e79bfe2 232 htmlspecialchars($send_to));
df15de21 233 echo " </TD>\n";
234 echo " </TR>\n";
235 echo " <TR>\n";
c5d828b3 236 echo " <TD BGCOLOR=\"$color[4]\" ALIGN=RIGHT>\n";
aae41ae9 237 echo _("CC:");
c5d828b3 238 echo " </TD><TD BGCOLOR=\"$color[4]\" ALIGN=LEFT>\n";
cf8758c7 239 printf(" <INPUT TYPE=text NAME=\"send_to_cc\" SIZE=60 VALUE=\"%s\"><BR>\n",
6e79bfe2 240 htmlspecialchars($send_to_cc));
df15de21 241 echo " </TD>\n";
242 echo " </TR>\n";
243 echo " <TR>\n";
c5d828b3 244 echo " <TD BGCOLOR=\"$color[4]\" ALIGN=RIGHT>\n";
aae41ae9 245 echo _("BCC:");
761d149e 246 echo " </TD><TD BGCOLOR=\"$color[4]\" ALIGN=LEFT>\n";
cf8758c7 247 printf(" <INPUT TYPE=text NAME=\"send_to_bcc\" VALUE=\"%s\" SIZE=60><BR>\n",
6e79bfe2 248 htmlspecialchars($send_to_bcc));
3806fa52 249 echo "</TD></TR>\n";
5100704d 250
df15de21 251 echo " <TR>\n";
c5d828b3 252 echo " <TD BGCOLOR=\"$color[4]\" ALIGN=RIGHT>\n";
df15de21 253 echo _("Subject:");
761d149e 254 echo " </TD><TD BGCOLOR=\"$color[4]\" ALIGN=LEFT>\n";
df15de21 255 if ($reply_subj) {
256 $reply_subj = str_replace("\"", "'", $reply_subj);
7aaa81fc 257 $reply_subj = sqStripSlashes($reply_subj);
df15de21 258 $reply_subj = trim($reply_subj);
259 if (substr(strtolower($reply_subj), 0, 3) != "re:")
260 $reply_subj = "Re: $reply_subj";
cf8758c7 261 printf(" <INPUT TYPE=text NAME=subject SIZE=60 VALUE=\"%s\">",
6e79bfe2 262 htmlspecialchars($reply_subj));
df15de21 263 } else if ($forward_subj) {
264 $forward_subj = str_replace("\"", "'", $forward_subj);
7aaa81fc 265 $forward_subj = sqStripSlashes($forward_subj);
df15de21 266 $forward_subj = trim($forward_subj);
267 if ((substr(strtolower($forward_subj), 0, 4) != "fwd:") &&
268 (substr(strtolower($forward_subj), 0, 5) != "[fwd:") &&
269 (substr(strtolower($forward_subj), 0, 6) != "[ fwd:"))
270 $forward_subj = "[Fwd: $forward_subj]";
cf8758c7 271 printf(" <INPUT TYPE=text NAME=subject SIZE=60 VALUE=\"%s\">",
6e79bfe2 272 htmlspecialchars($forward_subj));
df15de21 273 } else {
6e79bfe2 274 printf(" <INPUT TYPE=text NAME=subject SIZE=60 VALUE=\"%s\">",
275 htmlspecialchars($subject));
31f3d7c0 276 }
480feea7 277 echo "</td></tr>\n\n";
278
441f2d33 279 if ($location_of_buttons == 'between') showComposeButtonRow();
4ba45d11 280
e5b23ff2 281 echo " <TR>\n";
c5d828b3 282 echo " <TD BGCOLOR=\"$color[4]\" COLSPAN=2>\n";
cf8758c7 283 echo " &nbsp;&nbsp;<TEXTAREA NAME=body ROWS=20 COLS=\"$editor_size\" WRAP=HARD>";
441f2d33 284 echo htmlspecialchars($body);
6e79bfe2 285 if ($use_signature == true && $newmail == true && !isset($from_htmladdr_search)) {
441f2d33 286 echo "\n\n-- \n" . htmlspecialchars($signature);
cf8758c7 287 }
288 echo "</TEXTAREA><BR>\n";
e5b23ff2 289 echo " </TD>\n";
290 echo " </TR>\n";
441f2d33 291
292 if ($location_of_buttons == 'bottom')
293 showComposeButtonRow();
294 else {
295 echo " <TR><TD>&nbsp;</TD><TD ALIGN=LEFT><INPUT TYPE=SUBMIT NAME=send VALUE=\""._("Send")."\"></TD></TR>\n";
296 }
e5b23ff2 297
4ba45d11 298 // This code is for attachments
299 echo " <tr>\n";
c5d828b3 300 echo " <TD BGCOLOR=\"$color[0]\" VALIGN=TOP ALIGN=RIGHT>\n";
aae41ae9 301 echo " <SMALL><BR></SMALL>"._("Attach:");
c5d828b3 302 echo " </td><td ALIGN=left BGCOLOR=\"$color[0]\">\n";
944eb785 303 echo " <INPUT NAME=\"attachfile\" SIZE=48 TYPE=\"file\">\n";
95de6c91 304 echo " &nbsp;&nbsp;<input type=\"submit\" name=\"attach\"";
305 echo " value=\"" . _("Add") ."\">\n";
469eb37b 306 echo " </td>\n";
469eb37b 307 echo " </tr>\n";
4ba45d11 308 if (isset($attachments) && count($attachments)>0) {
162efb6f 309 echo "<tr><td bgcolor=\"$color[0]\" align=right>\n";
e5b23ff2 310 echo "&nbsp;";
c5d828b3 311 echo "</td><td align=left bgcolor=\"$color[0]\">";
4ba45d11 312 while (list($localname, $remotename) = each($attachments)) {
313 echo "<input type=\"checkbox\" name=\"delete[]\" value=\"$localname\">\n";
314 echo "$remotename <input type=\"hidden\" name=\"attachments[$localname]\" value=\"$remotename\"><br>\n";
315 }
316
317 echo "<input type=\"submit\" name=\"do_delete\" value=\""._("Delete selected attachments")."\">\n";
469eb37b 318 echo "</td></tr>";
4ba45d11 319 }
4ba45d11 320 // End of attachment code
321
ffc2ccbc 322 echo "</TABLE>\n";
df15de21 323 echo "</FORM>";
d7d3c4d4 324 do_hook("compose_bottom");
31f3d7c0 325 }
441f2d33 326
327 function showComposeButtonRow() {
2037f4f3 328 global $use_javascript_addr_book;
441f2d33 329 echo " <TR><td>\n </td><td>\n";
330 if ($use_javascript_addr_book) {
331 echo " <SCRIPT LANGUAGE=JavaScript><!--\n document.write(\"";
332 echo " <input type=button value=\\\""._("Addresses")."\\\" onclick='javascript:open_abook();'>\");";
333 echo " // --></SCRIPT><NOSCRIPT>\n";
334 echo " <input type=submit name=\"html_addr_search\" value=\""._("Addresses")."\">";
335 echo " </NOSCRIPT>\n";
336 } else {
337 echo " <input type=submit name=\"html_addr_search\" value=\""._("Addresses")."\">";
338 }
339 echo "\n <INPUT TYPE=SUBMIT NAME=send VALUE=\"". _("Send") . "\">\n";
340
341 do_hook("compose_button_row");
342
343 echo " </TD>\n";
344 echo " </TR>\n\n";
345 }
8467bf00 346
df15de21 347 function showSentForm () {
df15de21 348 echo "<BR><BR><BR><CENTER><B>Message Sent!</B><BR><BR>";
9f2215a1 349 echo "You will be automatically forwarded.<BR>If not, <A HREF=\"right_main.php\">click here</A>";
aae41ae9 350 echo "</CENTER>";
df15de21 351 }
b278172f 352
0ad7dbda 353 function checkInput ($show) {
354 /** I implemented the $show variable because the error messages
355 were getting sent before the page header. So, I check once
356 using $show=false, and then when i'm ready to display the
357 error message, show=true **/
358 global $body, $send_to, $subject, $color;
b278172f 359
99fa2b21 360 if ($send_to == "") {
0ad7dbda 361 if ($show)
362 plain_error_message(_("You have not filled in the \"To:\" field."), $color);
df15de21 363 return false;
b278172f 364 }
df15de21 365 return true;
366 } // function checkInput()
367
3806fa52 368
056ddad7 369 // True if FAILURE
370 function saveAttachedFiles() {
371 global $HTTP_POST_FILES, $attachment_dir, $attachments;
372
373 is_logged_in();
374 $localfilename = GenerateRandomString(32, '', 7);
375
376 if (!@rename($HTTP_POST_FILES['attachfile']['tmp_name'], $attachment_dir.$localfilename)) {
377 if (!@copy($HTTP_POST_FILES['attachfile']['tmp_name'], $attachment_dir.$localfilename)) {
378 return true;
379 }
380 }
381
23fd3c8e 382 if (!isset($failed) || !$failed) {
056ddad7 383 // Write information about the file
384 $fp = fopen ($attachment_dir.$localfilename.".info", "w");
385 fputs ($fp, $HTTP_POST_FILES['attachfile']['type']."\n".$HTTP_POST_FILES['attachfile']['name']."\n");
386 fclose ($fp);
387
388 $attachments[$localfilename] = $HTTP_POST_FILES['attachfile']['name'];
389 }
390 }
391
ad775a2c 392 // Workaround for RedHat PHP 4.0.4pl1-3
393 // Also for Konq problems?
063a10aa 394 if (isset($mailbox)) $mailbox = trim($mailbox);
395 if (isset($send_to)) $send_to = trim($send_to);
396 if (isset($send_to_cc)) $send_to_cc = trim($send_to_cc);
397 if (isset($send_to_bcc)) $send_to_bcc = trim($send_to_bcc);
398 if (isset($subject)) $subject = trim($subject);
399 if (isset($body)) $body = trim($body);
400 if (isset($attachfile)) $attachfile = trim($attachfile);
ad775a2c 401 // End of workaround
402
23fd3c8e 403 if (!isset($mailbox) || $mailbox == "" || ($mailbox == "None"))
dcb7f454 404 $mailbox = "INBOX";
3806fa52 405
4ba45d11 406 if(isset($send)) {
245a6892 407 if (isset($HTTP_POST_FILES['attachfile']) &&
408 $HTTP_POST_FILES['attachfile']['tmp_name'] &&
78b4216e 409 $HTTP_POST_FILES['attachfile']['tmp_name'] != 'none')
056ddad7 410 $AttachFailure = saveAttachedFiles();
a7ea7540 411 if (checkInput(false) && !isset($AttachFailure)) {
35d520d3 412 $urlMailbox = urlencode (trim($mailbox));
413 if (! isset($reply_id))
414 $reply_id = 0;
966286ae 415 sendMessage($send_to, $send_to_cc, $send_to_bcc, $subject, $body, $reply_id);
416 header ("Location: right_main.php?mailbox=$urlMailbox&sort=$sort&startMessage=1");
df15de21 417 } else {
01aab860 418 //$imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
dcb7f454 419 displayPageHeader($color, $mailbox);
056ddad7 420
421 if ($AttachFailure)
422 plain_error_message(_("Could not move/copy file. File not attached"), $color);
423
0ad7dbda 424 checkInput(true);
425
df15de21 426 showInputForm();
01aab860 427 //sqimap_logout($imapConnection);
7c6cb7ca 428 }
245a6892 429 } else if (isset($html_addr_search_done)) {
3c13b9fb 430 is_logged_in();
dcb7f454 431 displayPageHeader($color, $mailbox);
3806fa52 432
7aaa81fc 433 $send_to = sqStripSlashes($send_to);
434 $send_to_cc = sqStripSlashes($send_to_cc);
435 $send_to_bcc = sqStripSlashes($send_to_bcc);
3806fa52 436
6c7fd6ca 437 for ($i=0; $i < count($send_to_search); $i++) {
438 if ($send_to)
439 $send_to .= ", ";
440 $send_to .= $send_to_search[$i];
441 }
442
443 for ($i=0; $i < count($send_to_cc_search); $i++) {
444 if ($send_to_cc)
445 $send_to_cc .= ", ";
446 $send_to_cc .= $send_to_cc_search[$i];
447 }
448
3806fa52 449 showInputForm();
245a6892 450 } else if (isset($html_addr_search)) {
591d2a88 451 // I am using an include so as to elminiate an extra unnecessary click. If you
452 // can think of a better way, please implement it.
2037f4f3 453 include ("./addrbook_search_html.php");
4ba45d11 454 } else if (isset($attach)) {
056ddad7 455 if (saveAttachedFiles())
22ef7536 456 plain_error_message(_("Could not move/copy file. File not attached"), $color);
21bc0570 457 displayPageHeader($color, $mailbox);
4ba45d11 458 showInputForm();
459 } else if (isset($do_delete)) {
3c13b9fb 460 is_logged_in();
dcb7f454 461 displayPageHeader($color, $mailbox);
fc3348ac 462
fb16d219 463 while (list($lkey, $localname) = each($delete)) {
90555fe7 464 unset ($attachments[$localname]);
c3c37167 465 unlink ($attachment_dir.$localname);
466 unlink ($attachment_dir.$localname.".info");
4ba45d11 467 }
4bfed9f3 468
4ba45d11 469 showInputForm();
245a6892 470 } else if (isset($smtpErrors)) {
66289791 471 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
472 displayPageHeader($color, $mailbox);
473
474 $newmail = true;
475 if ($forward_id && $ent_num) getAttachments(0);
476
477 newMail();
478 showInputForm();
479 sqimap_logout($imapConnection);
4ba45d11 480 } else {
a60b9989 481 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
dcb7f454 482 displayPageHeader($color, $mailbox);
fc3348ac 483
b57c4e63 484 $newmail = true;
66289791 485
245a6892 486 if (isset($forward_id) && isset($ent_num)) getAttachments(0);
5713b4f9 487
1220e677 488 newMail();
4ba45d11 489 showInputForm();
1195c340 490 sqimap_logout($imapConnection);
4ba45d11 491 }
da79853a 492?>