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