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