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