+/**
+ * compose.php
+ *
+ * Copyright (c) 1999-2002 The SquirrelMail Project Team
+ * Licensed under the GNU GPL. For full terms see the file COPYING.
+ *
+ * This code sends a mail.
+ *
+ * There are 4 modes of operation:
+ * - Start new mail
+ * - Add an attachment
+ * - Send mail
+ * - Save As Draft
+ *
+ * $Id$
+ */
+
+require_once('../src/validate.php');
+require_once('../functions/imap.php');
+require_once('../functions/date.php');
+require_once('../functions/mime.php');
+require_once('../functions/smtp.php');
+require_once('../functions/display_messages.php');
+require_once('../functions/plugin.php');
+
+if (!isset($attachments)) {
+ $attachments = array();
+ session_register('attachments');
+}
+
+if (!isset($composesession)) {
+ $composesession = 0;
+ session_register('composesession');
+}
+
+if (!isset($session)) {
+ $session = "$composesession" +1;
+ $composesession = $session;
+}
+
+if (!isset($mailbox) || $mailbox == '' || ($mailbox == 'None')) {
+ $mailbox = 'INBOX';
+}
+
+if (isset($draft)) {
+ include_once ('../src/draft_actions.php');
+ if (! isset($reply_id)) {
+ $reply_id = 0;
+ }
+ if (! isset($MDN)) {
+ $MDN = 'False';
+ }
+ if (!saveMessageAsDraft($send_to, $send_to_cc, $send_to_bcc, $subject, $body, $reply_id, $MDN, $session)) {
+ showInputForm($session);
+ exit();
+ } else {
+ $draft_message = _("Draft Email Saved");
+ /* If this is a resumed draft, then delete the original */
+ if(isset($delete_draft)) {
+ Header("Location: delete_message.php?mailbox=" . urlencode($draft_folder) .
+ "&message=$delete_draft&sort=$sort&startMessage=1&saved_draft=yes");
+ exit();
+ }
+ else {
+ if ($compose_new_win == '1') {
+ Header("Location: compose.php?saved_draft=yes&session=$composesession");
+ exit();
+ }
+ else {
+ Header("Location: right_main.php?mailbox=$draft_folder&sort=$sort".
+ "&startMessage=1¬e=$draft_message");
+ exit();
+ }
+ }
+ }
+}
+
+if (isset($send)) {
+ if (isset($HTTP_POST_FILES['attachfile']) &&
+ $HTTP_POST_FILES['attachfile']['tmp_name'] &&
+ $HTTP_POST_FILES['attachfile']['tmp_name'] != 'none') {
+ $AttachFailure = saveAttachedFiles($session);
+ }
+ if (checkInput(false) && !isset($AttachFailure)) {
+ $urlMailbox = urlencode (trim($mailbox));
+ if (! isset($reply_id)) {
+ $reply_id = 0;
+ }
+ /*
+ * Set $default_charset to correspond with the user's selection
+ * of language interface.
+ */
+ set_my_charset();
+
+ /*
+ * This is to change all newlines to \n
+ * We'll change them to \r\n later (in the sendMessage function)
+ */
+ $body = str_replace("\r\n", "\n", $body);
+ $body = str_replace("\r", "\n", $body);
+
+ /*
+ * Rewrap $body so that no line is bigger than $editor_size
+ * This should only really kick in the sqWordWrap function
+ * if the browser doesn't support "HARD" as the wrap type
+ * Or, in Opera's case, something goes wrong.
+ */
+ $body = explode("\n", $body);
+ $newBody = '';
+ foreach ($body as $line) {
+ if( $line <> '-- ' ) {
+ $line = rtrim($line);
+ }
+ if (strlen($line) <= $editor_size + 1) {
+ $newBody .= $line . "\n";
+ } else {
+ sqWordWrap($line, $editor_size) . "\n";
+ $newBody .= $line;
+ }
+ }
+ $body = $newBody;
+
+ do_hook('compose_send');
+
+ $MDN = False; // we are not sending a mdn response
+ if (! isset($mailprio)) {
+ $Result = sendMessage($send_to, $send_to_cc, $send_to_bcc,
+ $subject, $body, $reply_id, $MDN, '', $session);
+ } else {
+ $Result = sendMessage($send_to, $send_to_cc, $send_to_bcc,
+ $subject, $body, $reply_id, $MDN, $mailprio, $session);
+ }
+ if (! $Result) {
+ showInputForm($session);
+ exit();
+ }
+ if ( isset($delete_draft)) {
+ Header("Location: delete_message.php?mailbox=" . urlencode( $draft_folder ).
+ "&message=$delete_draft&sort=$sort&startMessage=1&mail_sent=yes");
+ exit();
+ }
+ if ($compose_new_win == '1') {
+ Header("Location: compose.php?mail_sent=yes&session=$composesession");
+ }
+ else {
+ Header("Location: right_main.php?mailbox=$urlMailbox&sort=$sort".
+ "&startMessage=1");
+ }
+ } else {
+ /*
+ *$imapConnection = sqimap_login($username, $key, $imapServerAddress,
+ * $imapPort, 0);
+ */
+ if ($compose_new_win == '1') {
+ compose_Header($color, $mailbox);
+ }
+ else {
+ displayPageHeader($color, $mailbox);
+ }
+ if (isset($AttachFailure)) {
+ plain_error_message(_("Could not move/copy file. File not attached"),
+ $color);
+ }
+
+ checkInput(true);
+ showInputForm($session);
+ /* sqimap_logout($imapConnection); */
+ }
+} elseif (isset($html_addr_search_done)) {
+ if ($compose_new_win == '1') {
+ compose_Header($color, $mailbox);
+ }
+ else {
+ displayPageHeader($color, $mailbox);
+ }
+
+ if (isset($send_to_search) && is_array($send_to_search)) {
+ foreach ($send_to_search as $k => $v) {
+ if (substr($k, 0, 1) == 'T') {
+ if ($send_to) {
+ $send_to .= ', ';
+ }
+ $send_to .= $v;
+ }
+ elseif (substr($k, 0, 1) == 'C') {
+ if ($send_to_cc) {
+ $send_to_cc .= ', ';
+ }
+ $send_to_cc .= $v;
+ }
+ elseif (substr($k, 0, 1) == 'B') {
+ if ($send_to_bcc) {
+ $send_to_bcc .= ', ';
+ }
+ $send_to_bcc .= $v;
+ }
+ }
+ }
+ showInputForm($session);
+} elseif (isset($html_addr_search)) {
+ if (isset($HTTP_POST_FILES['attachfile']) &&
+ $HTTP_POST_FILES['attachfile']['tmp_name'] &&
+ $HTTP_POST_FILES['attachfile']['tmp_name'] != 'none') {
+ if (saveAttachedFiles($session)) {
+ plain_error_message(_("Could not move/copy file. File not attached"), $color);
+ }
+ }
+ /*
+ * I am using an include so as to elminiate an extra unnecessary
+ * click. If you can think of a better way, please implement it.
+ */
+ include_once('./addrbook_search_html.php');
+} elseif (isset($attach)) {
+ if (saveAttachedFiles($session)) {
+ plain_error_message(_("Could not move/copy file. File not attached"), $color);
+ }
+ if ($compose_new_win == '1') {
+ compose_Header($color, $mailbox);
+ }
+ else {
+ displayPageHeader($color, $mailbox);
+ }
+ showInputForm($session);
+}
+elseif (isset($sigappend)) {
+ $idents = getPref($data_dir, $username, 'identities', 0);
+ if ($idents > 1) {
+ if ($identity == 'default') {
+ $no = 'g';
+ } else {
+ $no = $identity;
+ }
+ $signature = getSig($data_dir, $username, $no);
+ }
+ $body .= "\n\n".($prefix_sig==true? "-- \n":'').$signature;
+ if ($compose_new_win == '1') {
+ compose_Header($color, $mailbox);
+ } else {
+ displayPageHeader($color, $mailbox);
+ }
+ showInputForm($session);
+} elseif (isset($do_delete)) {
+ if ($compose_new_win == '1') {
+ compose_Header($color, $mailbox);
+ }
+ else {
+ displayPageHeader($color, $mailbox);
+ }
+
+ $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
+ if (isset($delete) && is_array($delete)) {
+ foreach($delete as $index) {
+ $attached_file = $hashed_attachment_dir . '/'
+ . $attachments[$index]['localfilename'];
+ unlink ($attached_file);
+ unset ($attachments[$index]);
+ }
+ }
+
+ showInputForm($session);
+
+} elseif (isset($attachedmessages)) {
+
+ /*
+ * This handles the case if we attache message
+ */
+ $imapConnection = sqimap_login($username, $key, $imapServerAddress,
+ $imapPort, 0);
+ if ($compose_new_win == '1') {
+ compose_Header($color, $mailbox);
+ }
+ else {
+ displayPageHeader($color, $mailbox);
+ }
+
+ $newmail = true;
+
+ newMail();
+ showInputForm($session);
+ sqimap_logout($imapConnection);
+
+} else {
+ /*
+ * This handles the default case as well as the error case
+ * (they had the same code) --> if (isset($smtpErrors))
+ */
+ $imapConnection = sqimap_login($username, $key, $imapServerAddress,
+ $imapPort, 0);
+ if ($compose_new_win == '1') {
+ compose_Header($color, $mailbox);
+ }
+ else {
+ displayPageHeader($color, $mailbox);
+ }
+
+ $newmail = true;
+
+ ClearAttachments($session);
+
+ if (isset($forward_id) && $forward_id && isset($ent_num) && $ent_num) {
+ getAttachments(0, $session);
+ }
+
+ if (isset($draft_id) && $draft_id && isset($ent_num) && $ent_num) {
+ getAttachments(0, $session);
+ }
+
+ newMail($session);
+ showInputForm($session);
+ sqimap_logout($imapConnection);
+}
+
+exit();
+
+
+/**************** Only function definitions go below *************/
+
+
+/* This function is used when not sending or adding attachments */
+function newMail () {
+ global $forward_id, $imapConnection, $msg, $ent_num, $body_ary, $body,
+ $reply_id, $send_to, $send_to_cc, $mailbox, $send_to_bcc, $editor_size,
+ $draft_id, $use_signature, $composesession;
+
+ $send_to = decodeHeader($send_to, false);
+ $send_to_cc = decodeHeader($send_to_cc, false);
+ $send_to_bcc = decodeHeader($send_to_bcc, false);