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