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