39d3d33bb7a97cf479966ee0ab470204666bda94
[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 else if ($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 while (ereg("^[>\s]*$", $body_ary[count($body_ary) - 1])) {
78 unset($body_ary[count($body_ary) - 1]);
79 }
80 $body = "";
81 for ($i=0; $i < count($body_ary); $i++) {
82 if (! $forward_id)
83 {
84 if (ereg('^[\s>]+', $body_ary[$i]))
85 {
86 $body_ary[$i] = '>' . $body_ary[$i];
87 }
88 else
89 {
90 $body_ary[$i] = '> ' . $body_ary[$i];
91 }
92 }
93 sqWordWrap($body_ary[$i], $editor_size - 1);
94 $body .= $body_ary[$i] . "\n";
95 $body_ary[$i] = '';
96 }
97 if ($forward_id)
98 {
99 $bodyTop = "-------- " . _("Original Message") . " --------\n";
100 $bodyTop .= _("Subject") . ": " . $orig_header->subject . "\n";
101 $bodyTop .= _("From") . ": " . $orig_header->from . "\n";
102 $bodyTop .= _("To") . ": " . $orig_header->to[0] . "\n";
103 if (count($orig_header->to) > 1) {
104 for ($x=1; $x < count($orig_header->to); $x++) {
105 $bodyTop .= " " . $orig_header->to[$x] . "\n";
106 }
107 }
108 $bodyTop .= "\n";
109 $body = $bodyTop . $body;
110 }
111
112 $body = ereg_replace('\\\\', '\\\\', $body);
113
114 sqimap_mailbox_close($imapConnection);
115 return;
116 }
117
118 if (!$send_to) {
119 $send_to = sqimap_find_email($send_to);
120 }
121
122 /** This formats a CC string if they hit "reply all" **/
123 if ($send_to_cc != "") {
124 $send_to_cc = ereg_replace( '"[^"]*"', "", $send_to_cc);
125 $send_to_cc = ereg_replace(";", ",", $send_to_cc);
126 $sendcc = explode(",", $send_to_cc);
127 $send_to_cc = "";
128
129 for ($i = 0; $i < count($sendcc); $i++) {
130 $sendcc[$i] = trim($sendcc[$i]);
131 if ($sendcc[$i] == "")
132 continue;
133
134 $sendcc[$i] = sqimap_find_email($sendcc[$i]);
135 $whofrom = sqimap_find_displayable_name($msg["HEADER"]["FROM"]);
136 $whoreplyto = sqimap_find_email($msg["HEADER"]["REPLYTO"]);
137
138 if ((strtolower(trim($sendcc[$i])) != strtolower(trim($whofrom))) &&
139 (strtolower(trim($sendcc[$i])) != strtolower(trim($whoreplyto))) &&
140 (trim($sendcc[$i]) != "")) {
141 $send_to_cc .= trim($sendcc[$i]) . ", ";
142 }
143 }
144 $send_to_cc = trim($send_to_cc);
145 if (substr($send_to_cc, -1) == ",") {
146 $send_to_cc = substr($send_to_cc, 0, strlen($send_to_cc) - 1);
147 }
148 }
149 } // function newMail()
150
151 function showInputForm () {
152 global $send_to, $send_to_cc, $reply_subj, $forward_subj, $body,
153 $passed_body, $color, $use_signature, $signature, $editor_size,
154 $attachments, $subject, $newmail, $use_javascript_addr_book,
155 $send_to_bcc, $reply_id, $mailbox, $from_htmladdr_search,
156 $location_of_buttons;
157
158 $subject = sqStripSlashes(decodeHeader($subject));
159 $reply_subj = decodeHeader($reply_subj);
160 $forward_subj = decodeHeader($forward_subj);
161 $body = sqStripSlashes($body);
162
163 if ($use_javascript_addr_book) {
164 echo "\n<SCRIPT LANGUAGE=JavaScript><!--\n";
165 echo "function open_abook() { \n";
166 echo " var nwin = window.open(\"addrbook_popup.php\",\"abookpopup\",";
167 echo "\"width=670,height=300,resizable=yes,scrollbars=yes\");\n";
168 echo " if((!nwin.opener) && (document.windows != null))\n";
169 echo " nwin.opener = document.windows;\n";
170 echo "}\n";
171 echo "// --></SCRIPT>\n\n";
172 }
173
174 echo "\n<FORM name=compose action=\"compose.php\" METHOD=POST ENCTYPE=\"multipart/form-data\">\n";
175 //echo "\n<FORM name=compose action=\"compose.php\" METHOD=POST>\n";
176 if ($reply_id) {
177 echo "<input type=hidden name=reply_id value=$reply_id>\n";
178 }
179 printf("<INPUT TYPE=hidden NAME=mailbox VALUE=\"%s\">\n", htmlspecialchars($mailbox));
180 echo "<TABLE WIDTH=\"100%\" ALIGN=center CELLSPACING=0 BORDER=0>\n";
181
182 if ($location_of_buttons == 'top') showComposeButtonRow();
183
184 echo " <TR>\n";
185 echo " <TD BGCOLOR=\"$color[4]\" ALIGN=RIGHT>\n";
186 echo _("To:");
187 echo " </TD><TD BGCOLOR=\"$color[4]\">\n";
188 printf(" <INPUT TYPE=text NAME=\"send_to\" VALUE=\"%s\" SIZE=60><BR>\n",
189 htmlspecialchars($send_to));
190 echo " </TD>\n";
191 echo " </TR>\n";
192 echo " <TR>\n";
193 echo " <TD BGCOLOR=\"$color[4]\" ALIGN=RIGHT>\n";
194 echo _("CC:");
195 echo " </TD><TD BGCOLOR=\"$color[4]\" ALIGN=LEFT>\n";
196 printf(" <INPUT TYPE=text NAME=\"send_to_cc\" SIZE=60 VALUE=\"%s\"><BR>\n",
197 htmlspecialchars($send_to_cc));
198 echo " </TD>\n";
199 echo " </TR>\n";
200 echo " <TR>\n";
201 echo " <TD BGCOLOR=\"$color[4]\" ALIGN=RIGHT>\n";
202 echo _("BCC:");
203 echo " </TD><TD BGCOLOR=\"$color[4]\" ALIGN=LEFT>\n";
204 printf(" <INPUT TYPE=text NAME=\"send_to_bcc\" VALUE=\"%s\" SIZE=60><BR>\n",
205 htmlspecialchars($send_to_bcc));
206 echo "</TD></TR>\n";
207
208 echo " <TR>\n";
209 echo " <TD BGCOLOR=\"$color[4]\" ALIGN=RIGHT>\n";
210 echo _("Subject:");
211 echo " </TD><TD BGCOLOR=\"$color[4]\" ALIGN=LEFT>\n";
212 if ($reply_subj) {
213 $reply_subj = str_replace("\"", "'", $reply_subj);
214 $reply_subj = sqStripSlashes($reply_subj);
215 $reply_subj = trim($reply_subj);
216 if (substr(strtolower($reply_subj), 0, 3) != "re:")
217 $reply_subj = "Re: $reply_subj";
218 printf(" <INPUT TYPE=text NAME=subject SIZE=60 VALUE=\"%s\">",
219 htmlspecialchars($reply_subj));
220 } else if ($forward_subj) {
221 $forward_subj = str_replace("\"", "'", $forward_subj);
222 $forward_subj = sqStripSlashes($forward_subj);
223 $forward_subj = trim($forward_subj);
224 if ((substr(strtolower($forward_subj), 0, 4) != "fwd:") &&
225 (substr(strtolower($forward_subj), 0, 5) != "[fwd:") &&
226 (substr(strtolower($forward_subj), 0, 6) != "[ fwd:"))
227 $forward_subj = "[Fwd: $forward_subj]";
228 printf(" <INPUT TYPE=text NAME=subject SIZE=60 VALUE=\"%s\">",
229 htmlspecialchars($forward_subj));
230 } else {
231 printf(" <INPUT TYPE=text NAME=subject SIZE=60 VALUE=\"%s\">",
232 htmlspecialchars($subject));
233 }
234 echo "</td></tr>\n\n";
235
236 if ($location_of_buttons == 'between') showComposeButtonRow();
237
238 echo " <TR>\n";
239 echo " <TD BGCOLOR=\"$color[4]\" COLSPAN=2>\n";
240 echo " &nbsp;&nbsp;<TEXTAREA NAME=body ROWS=20 COLS=\"$editor_size\" WRAP=HARD>";
241 echo htmlspecialchars($body);
242 if ($use_signature == true && $newmail == true && !isset($from_htmladdr_search)) {
243 echo "\n\n-- \n" . htmlspecialchars($signature);
244 }
245 echo "</TEXTAREA><BR>\n";
246 echo " </TD>\n";
247 echo " </TR>\n";
248
249 if ($location_of_buttons == 'bottom')
250 showComposeButtonRow();
251 else {
252 echo " <TR><TD>&nbsp;</TD><TD ALIGN=LEFT><INPUT TYPE=SUBMIT NAME=send VALUE=\""._("Send")."\"></TD></TR>\n";
253 }
254
255 // This code is for attachments
256 echo " <tr>\n";
257 echo " <TD BGCOLOR=\"$color[0]\" VALIGN=TOP ALIGN=RIGHT>\n";
258 echo " <SMALL><BR></SMALL>"._("Attach:");
259 echo " </td><td ALIGN=left BGCOLOR=\"$color[0]\">\n";
260 // echo " <INPUT TYPE=\"hidden\" name=\"MAX_FILE_SIZE\"\n";
261 // echo " value=\"10000\">\n";
262 echo " <INPUT NAME=\"attachfile\" SIZE=48 TYPE=\"file\">\n";
263 echo " &nbsp;&nbsp;<input type=\"submit\" name=\"attach\"";
264 echo " value=\"" . _("Add") ."\">\n";
265 echo " </td>\n";
266 echo " </tr>\n";
267 if (isset($attachments) && count($attachments)>0) {
268 echo "<tr><td bgcolor=\"$color[0]\" align=right>\n";
269 echo "&nbsp;";
270 echo "</td><td align=left bgcolor=\"$color[0]\">";
271 while (list($localname, $remotename) = each($attachments)) {
272 echo "<input type=\"checkbox\" name=\"delete[]\" value=\"$localname\">\n";
273 echo "$remotename <input type=\"hidden\" name=\"attachments[$localname]\" value=\"$remotename\"><br>\n";
274 }
275
276 echo "<input type=\"submit\" name=\"do_delete\" value=\""._("Delete selected attachments")."\">\n";
277 echo "</td></tr>";
278 }
279 // End of attachment code
280
281 echo "</TABLE>\n";
282 echo "</FORM>";
283 do_hook("compose_bottom");
284 }
285
286 function showComposeButtonRow() {
287 echo " <TR><td>\n </td><td>\n";
288 if ($use_javascript_addr_book) {
289 echo " <SCRIPT LANGUAGE=JavaScript><!--\n document.write(\"";
290 echo " <input type=button value=\\\""._("Addresses")."\\\" onclick='javascript:open_abook();'>\");";
291 echo " // --></SCRIPT><NOSCRIPT>\n";
292 echo " <input type=submit name=\"html_addr_search\" value=\""._("Addresses")."\">";
293 echo " </NOSCRIPT>\n";
294 } else {
295 echo " <input type=submit name=\"html_addr_search\" value=\""._("Addresses")."\">";
296 }
297 echo "\n <INPUT TYPE=SUBMIT NAME=send VALUE=\"". _("Send") . "\">\n";
298
299 do_hook("compose_button_row");
300
301 echo " </TD>\n";
302 echo " </TR>\n\n";
303 }
304
305 function showSentForm () {
306 echo "<BR><BR><BR><CENTER><B>Message Sent!</B><BR><BR>";
307 echo "You will be automatically forwarded.<BR>If not, <A HREF=\"right_main.php\">click here</A>";
308 echo "</CENTER>";
309 }
310
311 function checkInput ($show) {
312 /** I implemented the $show variable because the error messages
313 were getting sent before the page header. So, I check once
314 using $show=false, and then when i'm ready to display the
315 error message, show=true **/
316 global $body, $send_to, $subject, $color;
317
318 if ($send_to == "") {
319 if ($show)
320 plain_error_message(_("You have not filled in the \"To:\" field."), $color);
321 return false;
322 }
323 return true;
324 } // function checkInput()
325
326
327 if (($mailbox == "") || ($mailbox == "None"))
328 $mailbox = "INBOX";
329
330 if(isset($send)) {
331 if (checkInput(false)) {
332 $urlMailbox = urlencode ($mailbox);
333 sendMessage($send_to, $send_to_cc, $send_to_bcc, $subject, $body, $reply_id);
334 header ("Location: right_main.php?mailbox=$urlMailbox&sort=$sort&startMessage=1");
335 } else {
336 //$imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
337 displayPageHeader($color, $mailbox);
338 checkInput(true);
339
340 showInputForm();
341 //sqimap_logout($imapConnection);
342 }
343 } else if ($html_addr_search_done) {
344 is_logged_in();
345 displayPageHeader($color, $mailbox);
346
347 $send_to = sqStripSlashes($send_to);
348 $send_to_cc = sqStripSlashes($send_to_cc);
349 $send_to_bcc = sqStripSlashes($send_to_bcc);
350
351 for ($i=0; $i < count($send_to_search); $i++) {
352 if ($send_to)
353 $send_to .= ", ";
354 $send_to .= $send_to_search[$i];
355 }
356
357 for ($i=0; $i < count($send_to_cc_search); $i++) {
358 if ($send_to_cc)
359 $send_to_cc .= ", ";
360 $send_to_cc .= $send_to_cc_search[$i];
361 }
362
363 showInputForm();
364 } else if ($html_addr_search) {
365 // I am using an include so as to elminiate an extra unnecessary click. If you
366 // can think of a better way, please implement it.
367 include ("addrbook_search_html.php");
368 } else if (isset($attach)) {
369 is_logged_in();
370 displayPageHeader($color, $mailbox);
371
372 $localfilename = md5($HTTP_POST_FILES['attachfile']['tmp_name'].", ".$HTTP_POST_FILES['attachfile']['name'].", $REMOTE_IP, $REMOTE_PORT, $UNIQUE_ID, and everything else that may add entropy");
373 // $localfilename = $localfilename; // ??
374
375 // Put the file in a better place
376 // This shouldn't be here... Ondrej Sury <ondrej@sury.cz>
377 //$tmp=explode('/',$attachfile);
378 //$attachfile=$tmp[count($tmp)-1];
379 //$attachfile=ereg_replace('\.{2,}','',$attachfile);
380
381 //error_reporting(0); // Rename will produce error output if it fails
382 //if (!rename($attachfile, $attachment_dir.$localfilename)) {
383 // if (!copy($attachfile, $attachment_dir.$localfilename)) {
384 if (!@rename($HTTP_POST_FILES['attachfile']['tmp_name'], $attachment_dir.$localfilename)) {
385 if (!@copy($HTTP_POST_FILES['attachfile']['tmp_name'], $attachment_dir.$localfilename)) {
386 plain_error_message(_("Could not move/copy file. File not attached"), $color);
387 $failed = true;
388 }
389 }
390 // If it still exists, PHP will remove the original file
391
392 if (!$failed) {
393 // Write information about the file
394 $fp = fopen ($attachment_dir.$localfilename.".info", "w");
395 fputs ($fp, $HTTP_POST_FILES['attachfile']['type']."\n".$HTTP_POST_FILES['attachfile']['name']."\n");
396 fclose ($fp);
397
398 $attachments[$localfilename] = $HTTP_POST_FILES['attachfile']['name'];
399 }
400
401 showInputForm();
402 } else if (isset($do_delete)) {
403 is_logged_in();
404 displayPageHeader($color, $mailbox);
405
406 while (list($lkey, $localname) = each($delete)) {
407 array_splice ($attachments, $lkey, 1);
408 unlink ($attachment_dir.$localname);
409 unlink ($attachment_dir.$localname.".info");
410 }
411
412 showInputForm();
413 } else {
414 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
415 displayPageHeader($color, $mailbox);
416
417 $newmail = true;
418 newMail();
419 showInputForm();
420 sqimap_logout($imapConnection);
421 }
422 ?>