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