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