Forgot that one
[squirrelmail.git] / src / compose.php
CommitLineData
59177427 1<?php
895905c0 2
35586184 3/**
4 * compose.php
5 *
6 * Copyright (c) 1999-2001 The Squirrelmail Development Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * This code sends a mail.
10 *
11 * There are 4 modes of operation:
12 * - Start new mail
13 * - Add an attachment
14 * - Send mail
15 * - Save As Draft
16 *
17 * $Id$
18 */
f7fb20fe 19
35586184 20/*****************************************************************/
21/*** THIS FILE NEEDS TO HAVE ITS FORMATTING FIXED!!! ***/
22/*** PLEASE DO SO AND REMOVE THIS COMMENT SECTION. ***/
23/*** + Base level indent should begin at left margin, as ***/
24/*** the require_once below looks. ***/
25/*** + All identation should consist of four space blocks ***/
26/*** + Tab characters are evil. ***/
27/*** + all comments should use "slash-star ... star-slash" ***/
28/*** style -- no pound characters, no slash-slash style ***/
29/*** + FLOW CONTROL STATEMENTS (if, while, etc) SHOULD ***/
30/*** ALWAYS USE { AND } CHARACTERS!!! ***/
31/*** + Please use ' instead of ", when possible. Note " ***/
32/*** should always be used in _( ) function calls. ***/
33/*** Thank you for your help making the SM code more readable. ***/
34/*****************************************************************/
35
36require_once('../src/validate.php');
37require_once('../functions/imap.php');
38require_once('../functions/date.php');
39require_once('../functions/mime.php');
40require_once('../functions/smtp.php');
41require_once('../functions/display_messages.php');
42require_once('../functions/plugin.php');
8467bf00 43
79be8a0b 44 if (!isset($attachments))
f972eb46 45 {
79be8a0b 46 $attachments = array();
f972eb46 47 session_register('attachments');
48 }
a98bed68 49
d7981cd8 50
4ba45d11 51 // This function is used when not sending or adding attachments
df15de21 52 function newMail () {
53 global $forward_id, $imapConnection, $msg, $ent_num, $body_ary, $body,
0a17f9dd 54 $reply_id, $send_to, $send_to_cc, $mailbox, $send_to_bcc, $editor_size,
55 $draft_id, $use_signature;
e39d73e5 56
d51894be 57 $send_to = decodeHeader($send_to);
58 $send_to_cc = decodeHeader($send_to_cc);
59 $send_to_bcc = decodeHeader($send_to_bcc);
a53e5469 60
429f8906 61 if ($forward_id)
62 $id = $forward_id;
3b5b889f 63 elseif ($reply_id)
429f8906 64 $id = $reply_id;
65
0a17f9dd 66 if ($draft_id){
67 $id = $draft_id;
68 $use_signature = FALSE;
69 }
1195c340 70
245a6892 71 if (isset($id)) {
813eba2f 72 sqimap_mailbox_select($imapConnection, $mailbox);
429f8906 73 $message = sqimap_get_message($imapConnection, $id, $mailbox);
8d8ab69a 74 $orig_header = $message->header;
1195c340 75 if ($ent_num)
76 $message = getEntity($message, $ent_num);
429f8906 77
9487c2ff 78 if ($message->header->type0 == 'text' || $message->header->type1 == 'message') {
1195c340 79 if ($ent_num)
80 $body = decodeBody(mime_fetch_body($imapConnection, $id, $ent_num), $message->header->encoding);
81 else
82 $body = decodeBody(mime_fetch_body($imapConnection, $id, 1), $message->header->encoding);
429f8906 83 } else {
84 $body = "";
df15de21 85 }
9487c2ff 86
429f8906 87 if ($message->header->type1 == "html")
88 $body = strip_tags($body);
f82d9be2 89
9487c2ff 90 sqUnWordWrap($body);
a794e82c 91 $body_ary = explode("\n", $body);
fb6ce88e 92 $i = count($body_ary) - 1;
3bc5ef2d 93 while ($i >= 0 && ereg("^[>\\s]*$", $body_ary[$i])) {
fb6ce88e 94 unset($body_ary[$i]);
95 $i --;
441f2d33 96 }
a794e82c 97 $body = "";
3bc5ef2d 98 for ($i=0; isset($body_ary[$i]); $i++) {
0a17f9dd 99 if ($reply_id)
01aab860 100 {
59a29c4b 101 if (ereg('^[ >]+', $body_ary[$i]))
01aab860 102 {
103 $body_ary[$i] = '>' . $body_ary[$i];
104 }
105 else
106 {
107 $body_ary[$i] = '> ' . $body_ary[$i];
108 }
109 }
a951522b 110 if (!$draft_id) {
111 sqWordWrap($body_ary[$i], $editor_size - 1);
112 }
01aab860 113 $body .= $body_ary[$i] . "\n";
f923b93d 114 unset($body_ary[$i]);
a794e82c 115 }
01aab860 116 if ($forward_id)
117 {
118 $bodyTop = "-------- " . _("Original Message") . " --------\n";
9487c2ff 119 $bodyTop .= _("Subject") . ": " . $orig_header->subject . "\n";
120 $bodyTop .= _("From") . ": " . $orig_header->from . "\n";
121 $bodyTop .= _("To") . ": " . $orig_header->to[0] . "\n";
01aab860 122 if (count($orig_header->to) > 1) {
123 for ($x=1; $x < count($orig_header->to); $x++) {
124 $bodyTop .= " " . $orig_header->to[$x] . "\n";
125 }
126 }
127 $bodyTop .= "\n";
128 $body = $bodyTop . $body;
856af1e0 129 } else if ($reply_id) {
9af31e64 130 $orig_from = decodeHeader($orig_header->from);
4c06e15d 131 $orig_from = trim(substr($orig_from,0,strpos($orig_from,'<')));
132 $orig_from = str_replace('"','',$orig_from);
133 $orig_from = str_replace("'",'',$orig_from);
248bfebb 134 $body = getReplyCitation($orig_from) . $body;
78509c54 135 }
9487c2ff 136
01aab860 137 return;
78509c54 138 }
429f8906 139
29d08a52 140 if (!$send_to) {
141 $send_to = sqimap_find_email($send_to);
142 }
143
df15de21 144 /** This formats a CC string if they hit "reply all" **/
145 if ($send_to_cc != "") {
a48fbf9b 146 $send_to_cc = ereg_replace( '"[^"]*"', "", $send_to_cc);
df15de21 147 $send_to_cc = ereg_replace(";", ",", $send_to_cc);
148 $sendcc = explode(",", $send_to_cc);
149 $send_to_cc = "";
9487c2ff 150
df15de21 151 for ($i = 0; $i < count($sendcc); $i++) {
152 $sendcc[$i] = trim($sendcc[$i]);
153 if ($sendcc[$i] == "")
154 continue;
9487c2ff 155
a53e5469 156 $sendcc[$i] = sqimap_find_email($sendcc[$i]);
813eba2f 157 $whofrom = sqimap_find_displayable_name($msg["HEADER"]["FROM"]);
158 $whoreplyto = sqimap_find_email($msg["HEADER"]["REPLYTO"]);
9487c2ff 159
df15de21 160 if ((strtolower(trim($sendcc[$i])) != strtolower(trim($whofrom))) &&
161 (strtolower(trim($sendcc[$i])) != strtolower(trim($whoreplyto))) &&
162 (trim($sendcc[$i]) != "")) {
163 $send_to_cc .= trim($sendcc[$i]) . ", ";
164 }
165 }
166 $send_to_cc = trim($send_to_cc);
167 if (substr($send_to_cc, -1) == ",") {
168 $send_to_cc = substr($send_to_cc, 0, strlen($send_to_cc) - 1);
169 }
170 }
171 } // function newMail()
78509c54 172
5713b4f9 173 function getAttachments($message) {
174 global $mailbox, $attachments, $attachment_dir, $imapConnection,
3392dc86 175 $ent_num, $forward_id, $draft_id, $username;
0a17f9dd 176
177 if (isset($draft_id))
178 $id = $draft_id;
179 else
180 $id = $forward_id;
f972eb46 181
5713b4f9 182 if (!$message) {
183 sqimap_mailbox_select($imapConnection, $mailbox);
0a17f9dd 184 $message = sqimap_get_message($imapConnection, $id,
f923b93d 185 $mailbox);
991a059e 186 }
9487c2ff 187
3392dc86 188 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
991a059e 189 if (count($message->entities) == 0) {
190 if ($message->header->entity_id != $ent_num) {
191 $filename = decodeHeader($message->header->filename);
9487c2ff 192
991a059e 193 if ($filename == "")
194 $filename = "untitled-".$message->header->entity_id;
9487c2ff 195
991a059e 196 $localfilename = GenerateRandomString(32, '', 7);
3392dc86 197 $full_localfilename = "$hashed_attachment_dir/$localfilename";
198 while (file_exists($full_localfilename)) {
f923b93d 199 $localfilename = GenerateRandomString(32, '', 7);
3392dc86 200 $full_localfilename = "$hashed_attachment_dir/$localfilename";
201 }
f972eb46 202
7a813c24 203 $newAttachment = array();
f972eb46 204 $newAttachment['localfilename'] = $localfilename;
f923b93d 205 $newAttachment['remotefilename'] = $filename;
206 $newAttachment['type'] = strtolower($message->header->type0 .
207 '/' . $message->header->type1);
991a059e 208
209 // Write Attachment to file
3392dc86 210 $fp = fopen ("$hashed_attachment_dir/$localfilename", 'w');
9487c2ff 211 fputs ($fp, decodeBody(mime_fetch_body($imapConnection,
0a17f9dd 212 $id, $message->header->entity_id),
f923b93d 213 $message->header->encoding));
991a059e 214 fclose ($fp);
9487c2ff 215
f972eb46 216 $attachments[] = $newAttachment;
991a059e 217 }
5713b4f9 218 } else {
991a059e 219 for ($i = 0; $i < count($message->entities); $i++) {
5713b4f9 220 getAttachments($message->entities[$i]);
9487c2ff 221 }
5713b4f9 222 }
223 return;
9487c2ff 224 }
5713b4f9 225
df15de21 226 function showInputForm () {
227 global $send_to, $send_to_cc, $reply_subj, $forward_subj, $body,
9487c2ff 228 $passed_body, $color, $use_signature, $signature, $prefix_sig,
229 $editor_size, $attachments, $subject, $newmail,
230 $use_javascript_addr_book, $send_to_bcc, $reply_id, $mailbox,
aaf9abef 231 $from_htmladdr_search, $location_of_buttons, $attachment_dir,
020abcf3 232 $username, $data_dir, $identity, $draft_id, $delete_draft,
233 $mailprio;
78509c54 234
d51894be 235 $subject = decodeHeader($subject);
2e434774 236 $reply_subj = decodeHeader($reply_subj);
237 $forward_subj = decodeHeader($forward_subj);
9487c2ff 238
3806fa52 239 if ($use_javascript_addr_book) {
240 echo "\n<SCRIPT LANGUAGE=JavaScript><!--\n";
241 echo "function open_abook() { \n";
242 echo " var nwin = window.open(\"addrbook_popup.php\",\"abookpopup\",";
243 echo "\"width=670,height=300,resizable=yes,scrollbars=yes\");\n";
244 echo " if((!nwin.opener) && (document.windows != null))\n";
245 echo " nwin.opener = document.windows;\n";
246 echo "}\n";
247 echo "// --></SCRIPT>\n\n";
248 }
5100704d 249
b3911477 250 echo "\n<FORM name=compose action=\"compose.php\" METHOD=POST ENCTYPE=\"multipart/form-data\"";
251 do_hook("compose_form");
f923b93d 252 echo ">\n";
9487c2ff 253
f7b1b3b1 254 if (isset($draft_id)) {
0a17f9dd 255 echo "<input type=\"hidden\" name=\"delete_draft\" value=\"$draft_id\">\n";
f7b1b3b1 256 }
0fe622fd 257 if (isset($delete_draft)) {
258 echo "<input type=\"hidden\" name=\"delete_draft\" value=\"$delete_draft\">\n";
259 }
0a17f9dd 260
c5d828b3 261 echo "<TABLE WIDTH=\"100%\" ALIGN=center CELLSPACING=0 BORDER=0>\n";
441f2d33 262
263 if ($location_of_buttons == 'top') showComposeButtonRow();
264
aaf9abef 265 $idents = getPref($data_dir, $username, 'identities');
248bfebb 266 if ($idents != '' && $idents > 1) {
aaf9abef 267 echo " <TR>\n";
e7db48af 268 echo " <TD BGCOLOR=\"$color[4]\" WIDTH=\"10%\" ALIGN=RIGHT>\n";
aaf9abef 269 echo _("From:");
e7db48af 270 echo " </TD><TD BGCOLOR=\"$color[4]\" WIDTH=\"90%\">\n";
248bfebb 271 echo "<select name=identity>\n";
9487c2ff 272 echo "<option value=default>" .
248bfebb 273 htmlspecialchars(getPref($data_dir, $username, 'full_name'));
274 $em = getPref($data_dir, $username, 'email_address');
275 if ($em != '')
276 echo htmlspecialchars(' <' . $em . '>') . "\n";
277 for ($i = 1; $i < $idents; $i ++) {
278 echo '<option value="' . $i . '"';
279 if (isset($identity) && $identity == $i)
280 echo ' SELECTED';
281 echo '>';
9487c2ff 282 echo htmlspecialchars(getPref($data_dir, $username, 'full_name' .
248bfebb 283 $i));
284 $em = getPref($data_dir, $username, 'email_address' . $i);
285 if ($em != '')
286 echo htmlspecialchars(' <' . $em . '>') . "\n";
287 }
288 echo "</select>\n";
aaf9abef 289 echo " </TD>\n";
290 echo " </TR>\n";
291 }
df15de21 292 echo " <TR>\n";
e7db48af 293 echo " <TD BGCOLOR=\"$color[4]\" WIDTH=\"10%\" ALIGN=RIGHT>\n";
df15de21 294 echo _("To:");
e7db48af 295 echo " </TD><TD BGCOLOR=\"$color[4]\" WIDTH=\"90%\">\n";
cf8758c7 296 printf(" <INPUT TYPE=text NAME=\"send_to\" VALUE=\"%s\" SIZE=60><BR>\n",
6e79bfe2 297 htmlspecialchars($send_to));
df15de21 298 echo " </TD>\n";
299 echo " </TR>\n";
300 echo " <TR>\n";
c5d828b3 301 echo " <TD BGCOLOR=\"$color[4]\" ALIGN=RIGHT>\n";
aae41ae9 302 echo _("CC:");
c5d828b3 303 echo " </TD><TD BGCOLOR=\"$color[4]\" ALIGN=LEFT>\n";
cf8758c7 304 printf(" <INPUT TYPE=text NAME=\"send_to_cc\" SIZE=60 VALUE=\"%s\"><BR>\n",
6e79bfe2 305 htmlspecialchars($send_to_cc));
df15de21 306 echo " </TD>\n";
307 echo " </TR>\n";
308 echo " <TR>\n";
c5d828b3 309 echo " <TD BGCOLOR=\"$color[4]\" ALIGN=RIGHT>\n";
aae41ae9 310 echo _("BCC:");
761d149e 311 echo " </TD><TD BGCOLOR=\"$color[4]\" ALIGN=LEFT>\n";
cf8758c7 312 printf(" <INPUT TYPE=text NAME=\"send_to_bcc\" VALUE=\"%s\" SIZE=60><BR>\n",
6e79bfe2 313 htmlspecialchars($send_to_bcc));
3806fa52 314 echo "</TD></TR>\n";
5100704d 315
df15de21 316 echo " <TR>\n";
c5d828b3 317 echo " <TD BGCOLOR=\"$color[4]\" ALIGN=RIGHT>\n";
df15de21 318 echo _("Subject:");
761d149e 319 echo " </TD><TD BGCOLOR=\"$color[4]\" ALIGN=LEFT>\n";
df15de21 320 if ($reply_subj) {
321 $reply_subj = str_replace("\"", "'", $reply_subj);
df15de21 322 $reply_subj = trim($reply_subj);
323 if (substr(strtolower($reply_subj), 0, 3) != "re:")
324 $reply_subj = "Re: $reply_subj";
cf8758c7 325 printf(" <INPUT TYPE=text NAME=subject SIZE=60 VALUE=\"%s\">",
6e79bfe2 326 htmlspecialchars($reply_subj));
df15de21 327 } else if ($forward_subj) {
df15de21 328 $forward_subj = trim($forward_subj);
f972eb46 329 if ((substr(strtolower($forward_subj), 0, 4) != 'fwd:') &&
330 (substr(strtolower($forward_subj), 0, 5) != '[fwd:') &&
331 (substr(strtolower($forward_subj), 0, 6) != '[ fwd:'))
df15de21 332 $forward_subj = "[Fwd: $forward_subj]";
cf8758c7 333 printf(" <INPUT TYPE=text NAME=subject SIZE=60 VALUE=\"%s\">",
6e79bfe2 334 htmlspecialchars($forward_subj));
df15de21 335 } else {
6e79bfe2 336 printf(" <INPUT TYPE=text NAME=subject SIZE=60 VALUE=\"%s\">",
337 htmlspecialchars($subject));
31f3d7c0 338 }
480feea7 339 echo "</td></tr>\n\n";
340
441f2d33 341 if ($location_of_buttons == 'between') showComposeButtonRow();
4ba45d11 342
e5b23ff2 343 echo " <TR>\n";
c5d828b3 344 echo " <TD BGCOLOR=\"$color[4]\" COLSPAN=2>\n";
cf8758c7 345 echo " &nbsp;&nbsp;<TEXTAREA NAME=body ROWS=20 COLS=\"$editor_size\" WRAP=HARD>";
441f2d33 346 echo htmlspecialchars($body);
6e79bfe2 347 if ($use_signature == true && $newmail == true && !isset($from_htmladdr_search)) {
b8163cc4 348 if ( $prefix_sig == true )
349 echo "\n\n-- \n" . htmlspecialchars($signature);
350 else
351 echo "\n\n" . htmlspecialchars($signature);
cf8758c7 352 }
353 echo "</TEXTAREA><BR>\n";
e5b23ff2 354 echo " </TD>\n";
355 echo " </TR>\n";
441f2d33 356
9487c2ff 357 if ($location_of_buttons == 'bottom')
441f2d33 358 showComposeButtonRow();
359 else {
360 echo " <TR><TD>&nbsp;</TD><TD ALIGN=LEFT><INPUT TYPE=SUBMIT NAME=send VALUE=\""._("Send")."\"></TD></TR>\n";
361 }
9487c2ff 362
4ba45d11 363 // This code is for attachments
a98bed68 364 echo " <tr>\n";
889df970 365 echo " <TD BGCOLOR=\"$color[0]\" VALIGN=MIDDLE ALIGN=RIGHT>\n";
366 echo _("Attach:");
a98bed68 367 echo " </td><td ALIGN=left BGCOLOR=\"$color[0]\">\n";
944eb785 368 echo " <INPUT NAME=\"attachfile\" SIZE=48 TYPE=\"file\">\n";
95de6c91 369 echo " &nbsp;&nbsp;<input type=\"submit\" name=\"attach\"";
370 echo " value=\"" . _("Add") ."\">\n";
469eb37b 371 echo " </td>\n";
469eb37b 372 echo " </tr>\n";
3392dc86 373 if (count($attachments)) {
374 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
a98bed68 375 echo "<tr><td bgcolor=\"$color[0]\" align=right>\n";
e5b23ff2 376 echo "&nbsp;";
a98bed68 377 echo "</td><td align=left bgcolor=\"$color[0]\">";
f923b93d 378 foreach ($attachments as $key => $info) {
3392dc86 379 $attached_file = "$hashed_attachment_dir/$info[localfilename]";
f972eb46 380 echo "<input type=\"checkbox\" name=\"delete[]\" value=\"$key\">\n";
f923b93d 381 echo $info['remotefilename'] . " - " . $info['type'] . " (";
3392dc86 382 echo show_readable_size(filesize($attached_file)) . ")<br>\n";
4ba45d11 383 }
9487c2ff 384
4ba45d11 385 echo "<input type=\"submit\" name=\"do_delete\" value=\""._("Delete selected attachments")."\">\n";
469eb37b 386 echo "</td></tr>";
4ba45d11 387 }
4ba45d11 388 // End of attachment code
389
ffc2ccbc 390 echo "</TABLE>\n";
9487c2ff 391 if ($reply_id) {
392 echo "<input type=hidden name=reply_id value=$reply_id>\n";
393 }
394 printf("<INPUT TYPE=hidden NAME=mailbox VALUE=\"%s\">\n", htmlspecialchars($mailbox));
df15de21 395 echo "</FORM>";
d7d3c4d4 396 do_hook("compose_bottom");
4d1d3c86 397 echo "</BODY></HTML>\n";
31f3d7c0 398 }
9487c2ff 399
441f2d33 400 function showComposeButtonRow() {
020abcf3 401 global $use_javascript_addr_book, $save_as_draft,
402 $default_use_priority, $mailprio;
9487c2ff 403
441f2d33 404 echo " <TR><td>\n </td><td>\n";
405 if ($use_javascript_addr_book) {
406 echo " <SCRIPT LANGUAGE=JavaScript><!--\n document.write(\"";
407 echo " <input type=button value=\\\""._("Addresses")."\\\" onclick='javascript:open_abook();'>\");";
408 echo " // --></SCRIPT><NOSCRIPT>\n";
409 echo " <input type=submit name=\"html_addr_search\" value=\""._("Addresses")."\">";
410 echo " </NOSCRIPT>\n";
9487c2ff 411 } else {
441f2d33 412 echo " <input type=submit name=\"html_addr_search\" value=\""._("Addresses")."\">";
9487c2ff 413 }
441f2d33 414 echo "\n <INPUT TYPE=SUBMIT NAME=send VALUE=\"". _("Send") . "\">\n";
9487c2ff 415
f7b1b3b1 416 if ($save_as_draft) {
57155fe3 417 echo '<input type="submit" name ="draft" value="' . _("Save Draft") . "\">\n";
f7b1b3b1 418 }
020abcf3 419 if ($default_use_priority) {
420 if(!isset($mailprio)) {
421 $mailprio = "3";
422 }
423 echo "\n\t". _("Priority") .":<select name=\"mailprio\">".
424 "\n\t\t<option value=1".($mailprio=="1"?" selected":"").">". _("High") ."</option>".
425 "\n\t\t<option value=3".($mailprio=="3"?" selected":"").">". _("Normal") ."</option>".
426 "\n\t\t<option value=5".($mailprio=="5"?" selected":"").">". _("Low")."</option>".
427 "\n\t</select>";
428 }
0a17f9dd 429
441f2d33 430 do_hook("compose_button_row");
431
432 echo " </TD>\n";
433 echo " </TR>\n\n";
434 }
8467bf00 435
0ad7dbda 436 function checkInput ($show) {
437 /** I implemented the $show variable because the error messages
438 were getting sent before the page header. So, I check once
439 using $show=false, and then when i'm ready to display the
440 error message, show=true **/
441 global $body, $send_to, $subject, $color;
b278172f 442
99fa2b21 443 if ($send_to == "") {
0ad7dbda 444 if ($show)
445 plain_error_message(_("You have not filled in the \"To:\" field."), $color);
df15de21 446 return false;
b278172f 447 }
df15de21 448 return true;
449 } // function checkInput()
450
3806fa52 451
056ddad7 452 // True if FAILURE
453 function saveAttachedFiles() {
3392dc86 454 global $HTTP_POST_FILES, $attachment_dir, $attachments, $username;
020abcf3 455
3392dc86 456 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
056ddad7 457 $localfilename = GenerateRandomString(32, '', 7);
3392dc86 458 $full_localfilename = "$hashed_attachment_dir/$localfilename";
459 while (file_exists($full_localfilename)) {
a4fe5de0 460 $localfilename = GenerateRandomString(32, '', 7);
3392dc86 461 $full_localfilename = "$hashed_attachment_dir/$localfilename";
462 }
f972eb46 463
3392dc86 464 if (!@rename($HTTP_POST_FILES['attachfile']['tmp_name'], $full_localfilename)) {
465 if (!@copy($HTTP_POST_FILES['attachfile']['tmp_name'], $full_localfilename)) {
056ddad7 466 return true;
467 }
468 }
9487c2ff 469
f972eb46 470 $newAttachment['localfilename'] = $localfilename;
471 $newAttachment['remotefilename'] = $HTTP_POST_FILES['attachfile']['name'];
9487c2ff 472 $newAttachment['type'] =
f972eb46 473 strtolower($HTTP_POST_FILES['attachfile']['type']);
8ef72f33 474
475 if ($newAttachment['type'] == "")
476 $newAttachment['type'] = 'application/octet-stream';
477
f972eb46 478 $attachments[] = $newAttachment;
056ddad7 479 }
9487c2ff 480
2d367c68 481 if (!isset($mailbox) || $mailbox == '' || ($mailbox == 'None'))
dcb7f454 482 $mailbox = "INBOX";
3806fa52 483
0a17f9dd 484 if (isset($draft)) {
485 require_once ('../src/draft_actions.php');
f7b1b3b1 486 if (!saveMessageAsDraft($send_to, $send_to_cc, $send_to_bcc, $subject, $body, $reply_id)) {
0a17f9dd 487 showInputForm();
488 exit();
489 } else {
f7b1b3b1 490 $draft_message = _("Draft Email Saved");
45c4e0f2 491 /* If this is a resumed draft, then delete the original */
492 if(isset($delete_draft)) {
493 Header("Location: delete_message.php?mailbox=$draft_folder&message=$delete_draft&sort=$sort&startMessage=1");
494 exit();
495 } else {
496 Header("Location: right_main.php?mailbox=$draft_folder&sort=$sort&startMessage=1&note=$draft_message");
497 exit();
498 }
0a17f9dd 499 }
500 }
501
f972eb46 502 if (isset($send)) {
245a6892 503 if (isset($HTTP_POST_FILES['attachfile']) &&
504 $HTTP_POST_FILES['attachfile']['tmp_name'] &&
78b4216e 505 $HTTP_POST_FILES['attachfile']['tmp_name'] != 'none')
056ddad7 506 $AttachFailure = saveAttachedFiles();
a7ea7540 507 if (checkInput(false) && !isset($AttachFailure)) {
35d520d3 508 $urlMailbox = urlencode (trim($mailbox));
509 if (! isset($reply_id))
510 $reply_id = 0;
ebab5342 511 // Set $default_charset to correspond with the user's selection
f923b93d 512 // of language interface.
513 set_my_charset();
eedcdd97 514
515 // This is to change all newlines to \n
248bfebb 516 // We'll change them to \r\n later (in the sendMessage function)
517 $body = str_replace("\r\n", "\n", $body);
518 $body = str_replace("\r", "\n", $body);
9487c2ff 519
248bfebb 520 // Rewrap $body so that no line is bigger than $editor_size
521 // This should only really kick in the sqWordWrap function
522 // if the browser doesn't support "HARD" as the wrap type
523 // Or, in Opera's case, something goes wrong.
524 $body = explode("\n", $body);
525 $newBody = '';
526 foreach ($body as $line) {
527 if( $line <> '-- ' )
528 $line = rtrim($line);
529 if (strlen($line) <= $editor_size + 1)
530 $newBody .= $line . "\n";
531 else {
532 sqWordWrap($line, $editor_size) . "\n";
533 $newBody .= $line;
534 }
535 }
536 $body = $newBody;
9487c2ff 537
f923b93d 538 do_hook("compose_send");
9487c2ff 539
872c4fac 540 if (! isset($mailprio))
541 $Result = sendMessage($send_to, $send_to_cc, $send_to_bcc,
9bc3dc44 542 $subject, $body, $reply_id);
872c4fac 543 else
9bc3dc44 544 $Result = sendMessage($send_to, $send_to_cc, $send_to_bcc,
545 $subject, $body, $reply_id, $mailprio);
872c4fac 546 if (! $Result) {
9487c2ff 547 showInputForm();
248bfebb 548 exit();
549 }
0a17f9dd 550 if ( isset($delete_draft)) {
cc82b756 551 Header("Location: delete_message.php?mailbox=$draft_folder&message=$delete_draft&sort=$sort&startMessage=1");
0a17f9dd 552 exit();
553 }
554
248bfebb 555 Header("Location: right_main.php?mailbox=$urlMailbox&sort=$sort&startMessage=1");
df15de21 556 } else {
01aab860 557 //$imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
dcb7f454 558 displayPageHeader($color, $mailbox);
9487c2ff 559
7a813c24 560 if (isset($AttachFailure))
056ddad7 561 plain_error_message(_("Could not move/copy file. File not attached"), $color);
562
0ad7dbda 563 checkInput(true);
9487c2ff 564
df15de21 565 showInputForm();
01aab860 566 //sqimap_logout($imapConnection);
7c6cb7ca 567 }
245a6892 568 } else if (isset($html_addr_search_done)) {
dcb7f454 569 displayPageHeader($color, $mailbox);
3806fa52 570
b8796881 571 if (isset($send_to_search) && is_array($send_to_search)) {
572 foreach ($send_to_search as $k => $v) {
248bfebb 573 if (substr($k, 0, 1) == 'T') {
b8796881 574 if ($send_to)
575 $send_to .= ', ';
576 $send_to .= $v;
248bfebb 577 }
578 elseif (substr($k, 0, 1) == 'C') {
579 if ($send_to_cc)
580 $send_to_cc .= ', ';
581 $send_to_cc .= $v;
582 }
583 elseif (substr($k, 0, 1) == 'B') {
584 if ($send_to_bcc)
585 $send_to_bcc .= ', ';
586 $send_to_bcc .= $v;
587 }
e53f7484 588 }
a98bed68 589 }
9487c2ff 590
3806fa52 591 showInputForm();
245a6892 592 } else if (isset($html_addr_search)) {
f17728d1 593 if (isset($HTTP_POST_FILES['attachfile']) &&
594 $HTTP_POST_FILES['attachfile']['tmp_name'] &&
595 $HTTP_POST_FILES['attachfile']['tmp_name'] != 'none')
596 {
597 if (saveAttachedFiles())
598 plain_error_message(_("Could not move/copy file. File not attached"), $color);
599 }
591d2a88 600 // I am using an include so as to elminiate an extra unnecessary click. If you
601 // can think of a better way, please implement it.
ff8a98e7 602 include_once('./addrbook_search_html.php');
4ba45d11 603 } else if (isset($attach)) {
056ddad7 604 if (saveAttachedFiles())
22ef7536 605 plain_error_message(_("Could not move/copy file. File not attached"), $color);
21bc0570 606 displayPageHeader($color, $mailbox);
4ba45d11 607 showInputForm();
608 } else if (isset($do_delete)) {
dcb7f454 609 displayPageHeader($color, $mailbox);
fc3348ac 610
3392dc86 611 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
612 if (isset($delete) && is_array($delete)) {
613 foreach($delete as $index) {
614 $attached_file = $hashed_attachment_dir . '/'
615 . $attachments[$index]['localfilename'];
616 unlink ($attached_file);
f972eb46 617 unset ($attachments[$index]);
f923b93d 618 }
4ba45d11 619 }
4bfed9f3 620
4ba45d11 621 showInputForm();
622 } else {
991a059e 623 // This handles the default case as well as the error case
624 // (they had the same code) --> if (isset($smtpErrors))
9487c2ff 625 $imapConnection = sqimap_login($username, $key, $imapServerAddress,
991a059e 626 $imapPort, 0);
dcb7f454 627 displayPageHeader($color, $mailbox);
fc3348ac 628
b57c4e63 629 $newmail = true;
f972eb46 630
631 ClearAttachments();
632
991a059e 633 if (isset($forward_id) && $forward_id && isset($ent_num) && $ent_num)
634 getAttachments(0);
9487c2ff 635
0a17f9dd 636 if (isset($draft_id) && $draft_id && isset($ent_num) && $ent_num)
637 getAttachments(0);
638
1220e677 639 newMail();
4ba45d11 640 showInputForm();
1195c340 641 sqimap_logout($imapConnection);
4ba45d11 642 }
9487c2ff 643
f923b93d 644 function ClearAttachments() {
3392dc86 645 global $username, $attachments, $attachment_dir;
646 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
9487c2ff 647
f923b93d 648 foreach ($attachments as $info) {
3392dc86 649 $attached_file = "$hashed_attachment_dir/$info[localfilename]";
650 if (file_exists($attached_file)) {
651 unlink($attached_file);
f923b93d 652 }
f972eb46 653 }
9487c2ff 654
f972eb46 655 $attachments = array();
656 }
cd41fc8d 657
658 function getReplyCitation($orig_from) {
659 global $reply_citation_style, $reply_citation_start, $reply_citation_end;
660
661 /* First, return an empty string when no citation style selected. */
662 if (($reply_citation_style == '') || ($reply_citation_style == 'none')) {
663 return ('');
664 }
665
666 /* Otherwise, try to select the desired citation style. */
667 switch ($reply_citation_style) {
668 case 'author_said':
669 $start = '';
670 $end = ' ' . _("said") . ':';
671 break;
672 case 'quote_who':
673 $start = '<' . _("quote") . ' ' . _("who") . '="';
674 $end = '">';
675 break;
676 case 'user-defined':
677 $start = $reply_citation_start;
678 $end = $reply_citation_end;
679 break;
680 default: return ('');
681 }
682
683 /* Build and return the citation string. */
684 return ($start . $orig_from . $end . "\n");
9487c2ff 685 }
0a17f9dd 686?>