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