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