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