Beispiel #1
0
function mailcwp_compose_callback()
{
    $message_id = $_POST["unique_id"];
    if (isset($_POST["subject"])) {
        $subject = mailcwp_get_imap_decoded_string(base64_decode($_POST["subject"]));
    }
    $mailcwp_session = mailcwp_get_session();
    if (!isset($mailcwp_session) || empty($mailcwp_session)) {
        return;
    }
    //write_log("COMPOSE MESSAGE SESSION " . print_r($mailcwp_session, true));
    //write_log("COMPOSE MESSAGE POST " . print_r($_POST, true));
    $account = $mailcwp_session["account"];
    $original = isset($_POST['original']) ? intval($_POST['original']) : -1;
    $mode = isset($_POST['mode']) ? intval($_POST['mode']) : -1;
    if ($mode == COMPOSE_EDIT_DRAFT) {
        $draft_id = $original;
    } else {
        $draft_id = "";
    }
    if ($original != -1) {
        //write_log($_POST);
        $folder = $mailcwp_session["folder"];
        $mbox = mailcwp_imap_connect($account, 0, $folder);
        $mail_message = new MailMessage($mbox, $original);
        $html_message = $mail_message->getHtmlMessage();
        //print_r($html_message);
        $char_set = $mail_message->getCharSet();
        if (!empty($char_set) && !empty($html_message)) {
            $html_message = iconv($char_set, "UTF-8", $html_message);
        }
        $plain_message = $mail_message->getPlainMessage();
        //print_r($plain_message);
        //exit;
        $attachments = $mail_message->getAttachments();
        $headers = $mail_message->getHeaders();
        imap_close($mbox);
        //process inline images
        if (isset($html_message)) {
            $vStartSrc = -1;
            while (($vStartSrc = strpos($html_message, "src=\"cid:", $vStartSrc + 1)) !== FALSE) {
                $vEndSrc = strpos($html_message, "\"", $vStartSrc + 5);
                if ($vEndSrc !== FALSE) {
                    $vCidName = substr($html_message, $vStartSrc + 9, $vEndSrc - $vStartSrc - 9);
                    $vPartId = strpos($vCidName, "@");
                    if ($vPartId !== FALSE) {
                        $vCidName = substr($vCidName, 0, $vPartId);
                    }
                    //write_log($vCidName);
                    if (isset($attachments[$vCidName])) {
                        $path_parts = pathinfo($vCidName);
                        if (isset($path_parts['extension'])) {
                            $extension = strtolower($path_parts['extension']);
                        } else {
                            $extension = "";
                        }
                        $html = "";
                        if (preg_match("(png|gif|jpg)", $extension) == 1) {
                            $encoded_image = base64_encode($attachments[$vCidName]);
                            $html = "src=\"data:image/{$extension};base64,{$encoded_image}\"";
                        }
                        $html_message = substr($html_message, 0, $vStartSrc) . $html . substr($html_message, $vEndSrc + 1);
                        unset($attachments[$vCidName]);
                    }
                }
            }
        }
        $from = "{$account['name']} <{$account['email']}>";
        $timezone = null;
        if (array_key_exists("timezone", $account)) {
            $tz_name = $account["timezone"];
            if (!empty($tz_name)) {
                $timezone = new DateTimeZone($tz_name);
            }
        }
        $original_datetime = new DateTime();
        $original_datetime->setTimestamp(mailcwp_get_imap_decoded_string($headers->udate));
        if (isset($timezone)) {
            $original_datetime->setTimeZone($timezone);
        }
        $original_date = $original_datetime->format("D j F Y");
        //date("D j F Y", mailcwp_get_imap_decoded_string($headers->udate));
        $original_time = $original_datetime->format("g:ia");
        //date("g:ia", mailcwp_get_imap_decoded_string($headers->udate));
        $original_from = mailcwp_get_imap_decoded_string($headers->fromaddress);
        $original_to = isset($headers->toaddress) ? mailcwp_get_imap_decoded_string($headers->toaddress) : "";
        //write_log($mode);
        if ($mode == COMPOSE_REPLY || $mode == COMPOSE_REPLY_ALL) {
            $to = $original_from;
            if ($mode == COMPOSE_REPLY_ALL) {
                //write_log($headers->to);
                foreach ($headers->to as $reply_to) {
                    $reply_address = "{$reply_to->mailbox}@{$reply_to->host}";
                    if ($reply_address != $account["email"]) {
                        $to .= ", {$reply_address}";
                    }
                }
                if (isset($headers->ccaddress)) {
                    $cc = mailcwp_get_imap_decoded_string($headers->ccaddress);
                } else {
                    $cc = "";
                }
            } else {
                $cc = "";
            }
        } else {
            if ($mode == COMPOSE_EDIT_DRAFT) {
                $to = $original_to;
                if (isset($headers->ccaddress)) {
                    $cc = mailcwp_get_imap_decoded_string($headers->ccaddress);
                } else {
                    $cc = "";
                }
                if (isset($headers->bccaddress)) {
                    $bcc = mailcwp_get_imap_decoded_string($headers->ccaddress);
                } else {
                    $bcc = "";
                }
            } else {
                $to = "";
                $cc = "";
                $bcc = "";
            }
        }
        if (property_exists($headers, "subject")) {
            $subject = mailcwp_get_imap_decoded_string($headers->subject);
        } else {
            $subject = "";
        }
        if ($html_message) {
            $html_message = apply_filters("mailcwp_get_html_message", $html_message, true);
            $message = $html_message;
            if ($mode != COMPOSE_EDIT_DRAFT) {
                $message = preg_replace("/(^|<br\\/>)/i", "<br/>&gt; ", $message);
            }
            $message = "On {$original_date}, at {$original_time}, {$original_from} wrote:<br/>{$message}";
        } else {
            $plain_message = apply_filters("mailcwp_get_plain_message", $plain_message, true);
            $message = $plain_message;
            if ($mode != COMPOSE_EDIT_DRAFT) {
                //$message = preg_replace("/(^|\\n)/", "\n> ", $message);
                $message = preg_replace("/(^|\\n)/", "<br/>&gt; ", $message);
            }
            $message = "On {$original_date}, at {$original_time}, {$original_from} wrote:\n{$message}";
        }
    } else {
        $from = "{$account['name']} <{$account['email']}>";
        $to = "";
        $cc = "";
        $subject = "";
        $message = "";
    }
    $from = htmlentities($from);
    $to = htmlentities($to);
    $cc = htmlentities($cc);
    if (!empty($subject)) {
        $title = $subject;
    } else {
        $title = __("New Message", "mailcwp");
    }
    $html = "<div id=\"mailcwp_compose_dialog_{$message_id}\" title=\"{$title}\">";
    $html .= "  <form>";
    $html .= "    <fieldset>";
    $html .= "      <input type=\"hidden\" id=\"mailcwp_draft_id_{$message_id}\" value=\"{$draft_id}\">";
    $html .= "      <input type=\"hidden\" id=\"mailcwp_sent_{$message_id}\">";
    $html .= "      <label for=\"mailcwp_from_{$message_id}\">From</label>";
    $html .= "      <input type=\"text\" disabled=\"disabled\" id=\"mailcwp_from_{$message_id}\" name=\"mailcwp_from_{$message_id}\" value=\"{$from}\">";
    $html .= "      <label for=\"mailcwp_to_{$message_id}\">To</label>";
    $html .= "      <input id=\"mailcwp_to_{$message_id}\" name=\"mailcwp_to_{$message_id}\" value=\"{$to}\">";
    $html .= apply_filters("mailcwp_compose_address", "", "mailcwp_to_{$message_id}");
    $html .= "      <div id=\"mailcwp_extra_addresses_{$message_id}\"" . (empty($cc) ? " style=\"display: none\"" : "") . ">";
    $html .= "        <label for=\"mailcwp_cc_{$message_id}\">CC</label>";
    $html .= "        <input id=\"mailcwp_cc_{$message_id}\" name=\"mailcwp_cc_{$message_id}\" value=\"{$cc}\">";
    $html .= apply_filters("mailcwp_compose_address", "", "mailcwp_cc_{$message_id}");
    $html .= "        <label for=\"mailcwp_bcc_{$message_id}\">BCC</label>";
    $html .= "        <input id=\"mailcwp_bcc_{$message_id}\" name=\"mailcwp_bcc_{$message_id}\">";
    $html .= apply_filters("mailcwp_compose_address", "", "mailcwp_bcc_{$message_id}");
    $html .= "      </div>";
    $html .= "      <p><label for=\"mailcwp_subject\">Subject</label>";
    $subject_prefix = "";
    if (($mode == COMPOSE_REPLY || $mode == COMPOSE_REPLY_ALL) && $original != -1) {
        if (empty($subject) || stripos($subject, "Re:") === FALSE) {
            $subject_prefix = "Re: ";
        }
    } else {
        if ($mode == COMPOSE_FORWARD && $original != -1) {
            if (empty($subject) || stripos($subject, "Fw:") === FALSE) {
                $subject_prefix = "Fw: ";
            }
        }
    }
    $html .= "      <input type=\"text\" id=\"mailcwp_subject_{$message_id}\" name=\"mailcwp_subject_{$message_id}\" value=\"" . $subject_prefix . $subject . "\"></p>";
    $compose_toolbar_html = "  <div id=\"mailcwp_compose_toolbar_{$message_id}\">" . "    <button id=\"mailcwp_attach_{$message_id}\">" . __("Attachments", "mailcwp") . "</button>" . "    <button id=\"mailcwp_extra_addresses_toggle_{$message_id}\"><sup>CC</sup>/<sub>BCC</sub></button>";
    //"    <button id=\"mailcwp_toggle_format_$message_id\">" . __("Text/HTML", "mailcwp") . "</button>";
    if (isset($attachments) && is_array($attachments) && count($attachments) > 0) {
        $compose_toolbar_html .= "    <div id=\"forward_attachment_set_{$message_id}\" style=\"display: inline\">" . "      <button id=\"forward_attachment_{$message_id}\" onclick=\"jQuery('#forward-all-{$message_id}').submit();\">" . __("Forward Attachments", "mailcwp") . "</button>" . "      <button id=\"remove_attachment_{$message_id}\">" . __("Remove an attachment", "mailcwp") . "</button>";
        $compose_toolbar_html .= "    </div>" . "    <ul id=\"forward_attachment_list_{$message_id}\">";
        foreach ($attachments as $filename => $data) {
            $compose_toolbar_html .= "      <li><a href=\"javascript:void(0)\" onclick=\"jQuery('#download-{$filename}-{$message_id}').submit();\">{$filename}</a></li>";
        }
        $compose_toolbar_html .= "    </ul>";
        $compose_toolbar_html .= "<script type=\"text/javascript\">";
        $compose_toolbar_html .= "  jQuery(\"#forward_attachment_{$message_id}\").button();";
        $compose_toolbar_html .= "  jQuery(\"#remove_attachment_{$message_id}\").button({text: false, icons: {primary: \"ui-icon-triangle-1-s\"}}).click(function() {jQuery(\"#forward_attachment_list_{$message_id}\").show().position({my: \"left top\", at: \"left bottom\", of: this}); jQuery(document).one(\"click\", function() {jQuery(\"#forward_attachment_list_{$message_id}\").hide();}); return false;});";
        $compose_toolbar_html .= "  jQuery(\"#forward_attachment_set_{$message_id}\").buttonset();";
        $compose_toolbar_html .= "  jQuery(\"#forward_attachment_list_{$message_id}\").hide().menu();";
        $compose_toolbar_html .= "</script>";
    }
    $html .= apply_filters("mailcwp_compose_toolbar", $compose_toolbar_html, $message_id);
    $html .= "  </div>";
    $html .= "      <textarea id=\"mailcwp_message_{$message_id}\" name=\"mailcwp_message_{$message_id}\" cols=\"80\" rows=\"40\">";
    if ($mode != COMPOSE_EDIT_DRAFT) {
        $html .= apply_filters("mailcwp_compose_before_message", "", $account["id"]);
    }
    $html .= "\n\n";
    $html .= $message;
    if ($mode != COMPOSE_EDIT_DRAFT) {
        $html .= apply_filters("mailcwp_compose_after_message", "", $account["id"]);
    }
    $html .= "</textarea>";
    $html .= "    </fieldset>";
    $html .= "  </form>";
    /*$html .= "<a name=\"mailcwp_compose_uploader_$message_id\"/><form id=\"mailcwp_compose_uploader_$message_id\" method=\"post\" action=\"";
      $html .= plugins_url("mailcwp-upload.php", __FILE__);
      $html .= "\">";
      $html .= "	<div id=\"uploader_$message_id\">";
      $html .= "		<p>Your browser doesn't have Flash, Silverlight or HTML5 support.</p>";
      $html .= "	</div>";
      $html .= "</form>";*/
    $html .= "<h4>" . __("Attachments", "mailcwp") . "</h4>";
    $html .= "<a name=\"mailcwp_compose_uploader_{$message_id}\"></a>";
    $html .= "<p>";
    $html .= "<div id=\"filelist_{$message_id}\">MailCWP is unable to attach files. Please contact the system administrator.</div>";
    $html .= "</p>";
    $html .= "<div id=\"uploader_{$message_id}\">";
    $html .= "    <button id=\"pickfiles_{$message_id}\">Select files</button>";
    $html .= "    <button id=\"removefiles_{$message_id}\" disabled>Remove files</button>";
    //$html .= "    <button id=\"uploadfiles_$message_id\">Upload files</button>";
    $html .= "</div>";
    $html .= "</div>";
    $current_user_id = get_current_user_id();
    $user_contacts = get_user_meta($current_user_id, "mailcwp_contacts", true);
    //write_log("CONTACTS " . print_r($user_contacts, true));
    $javascript = "  jQuery(document).ready(function() {var vContacts = [";
    if (is_array($user_contacts) && !empty($user_contacts)) {
        foreach ($user_contacts as $user_contact) {
            $javascript .= "\"" . addslashes($user_contact) . "\",";
        }
    }
    $javascript .= "  ];";
    $javascript .= " jQuery(\"#mailcwp_to_{$message_id}, #mailcwp_cc_{$message_id}, #mailcwp_bcc_{$message_id}\").autocomplete({";
    $javascript .= "   source: vContacts";
    $javascript .= " });});";
    $javascript .= apply_filters("mailcwp_compose_javascript", "", $message_id);
    $upload_dir = wp_upload_dir();
    $mailcwp_upload_dir = "{$upload_dir['basedir']}/mailcwp/uploads";
    $upload_url = plugins_url("/mailcwp-upload.php", __FILE__);
    $utf8_html = @utf8_encode($html);
    if ($utf8_html == null) {
        $utf8_html = $html;
    }
    $result = array("result" => "OK", "upload_dir" => $mailcwp_upload_dir, "upload_url" => $upload_url, "html" => $utf8_html, "javascript" => $javascript);
    //write_log($result);
    echo json_encode($result);
    die;
}