<?
require_once("config.php");
$th = isset($_REQUEST["th"])?$_REQUEST["th"]:"";
$mg = isset($_REQUEST["mg"])?$_REQUEST["mg"]:"";
$df = isset($_REQUEST["df"])?$_REQUEST["df"]:0;
# type of compose - R / RA / F
$type = isset($_REQUEST["type"])?$_REQUEST["type"]:"";
$orig_df = isset($_REQUEST["orig_df"])?$_REQUEST["orig_df"]:"";
$draft_attach = isset($_REQUEST["draft_attach"])?$_REQUEST["draft_attach"]:"";
header("Content-type: text/xml; charset=utf-8"); // must use text/xml for ajax/xmlhttp to work :(
header("Pragma: no-cache"); // stop caching
$gm = new GMailer();
if (!$gm->created)
die("Failed to create GMailer.");
quick_init($gm);
if ($gm->connect()) {
if ($df == 0) {
// replying mail
$gm->fetchBox(GM_CONVERSATION, array($th,$mg), 0);
$ss = $gm->getSnapshot(GM_CONVERSATION);
// orig from (reply to)
$from = count($ss->conv[0]["reply_email"])==0 ? array($ss->conv[0]["sender_email"]) : $ss->conv[0]["reply_email"];
// sent to
$to = $ss->conv[0]["recv_email"];
// cc
$cc = $ss->conv[0]["cc_email"];
// subject
$subj = $ss->conv[0]["subj"];
$subj = ereg_replace("^Re:", "", $subj);
// $subj = (strpos($ss->conv[0]["subj"],"Re:")===false) ? "Re: ".$ss->conv[0]["subj"] : $ss->conv[0]["subj"]; // to avoid "Re: Re:" in mail
$final_label = '';
$final_to = array();
$final_cc = array();
$final_subj = '';
if($type == "R"){
$final_to = array_merge($from, $to);
$final_subj = "Re: " . $subj;
$final_label = "Reply";
}else if($type == "RA"){
$final_to = array_merge($from, $to);
$final_cc = $cc;
$final_subj = "Re: " . $subj;
$final_label = "Reply All";
}else if($type == "F"){
$final_subj = "Fwd: " . $subj;
$final_label = "Forward";
}else{
$final_to = $to;
$final_cc = $cc;
$final_subj = $subj;
}
$body = $ss->conv[0]["body"];
// extra prefix for reply/forward
if ($type) {
$body = "\r\n\r\n\r\n> " . strip_tags(
str_replace(array("<br/","<Br/","<bR/","<BR/", "<br /","<Br /","<bR /","<BR /"),
"\r\n", $body));
$body = ereg_replace(">", ">", $body);
$body = ereg_replace(">>", "> >", $body);
}
echo "<root>";
echo "<label>" . cleanData($final_label) . "</label>" ;
if($final_to){echo "<to>" . cleanData(join(',',$final_to)) . "</to>" ;}
if($final_cc){echo "<cc>" . cleanData(join(',',$final_cc)) . "</cc>" ;}
echo "<subj>" . cleanData($final_subj) . "</subj>" ;
echo "<body>" . cleanData($body) . "</body>";
// attachments
for ($i = 0; $i < count($ss->conv[0]["attachment"]); $i++) {
echo "<attachment>" .
"<url>". cleanData("dl.php?a=".$ss->conv[0]["attachment"][$i]["id"]."&m=".$ss->conv[0]["id"]."&f=".
urlencode($ss->conv[0]["attachment"][$i]["filename"]))."</url>".
"<filename>". cleanData($ss->conv[0]["attachment"][$i]["filename"]) . "</filename>".
"</attachment>\n";
}
if (count($ss->conv[0]["attachment"]) > 1) {
echo "<attachment_all_url>". cleanData("dl.php?a=dummy&m=".$ss->conv[0]["id"]."&f=dummy&z=1") . "</attachment_all_url>\n";
}
echo "</root>";
} else {
// resuming a draft
$gm->fetchBox(GM_CONVERSATION, $th, 0);
$ss = $gm->getSnapshot(GM_CONVERSATION);
$the_draft = false;
for ($i = 0; $i < count($ss->conv) && $the_draft == false; $i++) {
if ($ss->conv[$i]["is_draft"] == true && $ss->conv[$i]["draft_parent"] == $mg) {
$the_draft = $ss->conv[$i];
}
}
$to = $the_draft["recv_email"];
$cc = $the_draft["cc_email"];
$bcc = $the_draft["bcc_email"];
$subj = $the_draft["subj"];
$body = $the_draft["body"];
$orig_df = $the_draft["id"];
echo "<root>" .
"<to>" . cleanData($to) . "</to>" .
"<cc>" . cleanData($cc) . "</cc>" .
"<bcc>" . cleanData($bcc) . "</bcc>" .
"<subj>" . cleanData($subj) . "</subj>" .
"<body>" . cleanData($body) . "</body>" .
"<orig_df>" . cleanData($orig_df) . "</orig_df>" .
"</root>";
}
} else {
echo "<root>" .
"<error>Failed to connect</error>" .
"</root>";
}
function cleanData($contents){
//$contents = strip_tags($contents);
$contents = ereg_replace("&", "&", $contents);
$contents = ereg_replace(" ", " ", $contents);
$contents = ereg_replace("u003d", "=", $contents);
$contents = ereg_replace("src=\"/mail/", "src=\"http://mail.google.com/mail/", $contents);
$contents = ereg_replace("]]>", "]]>", $contents);
return "<![CDATA[" . $contents . "]]>";
}
?>