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