示例#1
0
function archive_process($file)
{
    $unix = new unix();
    $timeMessage = filemtime($file);
    $fullmessagesdir = "/opt/artica/share/www/original_messages";
    $target_file = $file;
    $filename = basename($target_file);
    if (!isset($GLOBALS["GREP"])) {
        $GLOBALS["GREP"] = $unix->find_program("grep");
    }
    if (!isset($GLOBALS["MYHOSTNAME"])) {
        $GLOBALS["MYHOSTNAME"] = $unix->hostname_g();
    }
    $grep = $GLOBALS["GREP"];
    $ARCHIVED = false;
    $MailArchiverEnabled = $GLOBALS["MailArchiverEnabled"];
    $MailArchiverToMySQL = $GLOBALS["MailArchiverToMySQL"];
    $MailArchiverToMailBox = $GLOBALS["MailArchiverToMailBox"];
    $MailArchiverMailBox = $GLOBALS["MailArchiverMailBox"];
    $MailArchiverToSMTP = $GLOBALS["MailArchiverToSMTP"];
    $MailArchiverSMTP = $GLOBALS["MailArchiverSMTP"];
    $MailArchiverSMTPINcoming = $GLOBALS["MailArchiverSMTPINcoming"];
    if (!is_numeric($MailArchiverSMTP)) {
        $MailArchiverSMTP = 0;
    }
    $realmailfrom = null;
    $realmailto = null;
    exec("{$grep} X-REAL- {$file} 2>&1", $resultsgrep);
    while (list($num, $line) = each($resultsgrep)) {
        events("[{$num}] {$line}", __LINE__);
        if (preg_match("#X-REAL-MAILFROM:\\s+<(.*?)>#", $line, $re)) {
            $realmailfrom = trim($re[1]);
            continue;
        }
        if (preg_match("#X-REAL-RCPTTO:\\s+<(.*?)>#", $line, $re)) {
            $realmailto = trim($re[1]);
            continue;
        }
        if ($realmailto == null) {
            if (preg_match("#X-REAL-RCPTTO:\\s+(.*)#", $line, $re)) {
                $realmailto = trim($re[1]);
                continue;
            }
        }
        if ($realmailfrom == null) {
            if (preg_match("#X-REAL-MAILFROM:\\s+(.*)#", $line, $re)) {
                $realmailfrom = trim($re[1]);
                continue;
            }
        }
        if (preg_match("#X-REAL-ARCHIVED#", $line, $re)) {
            events("{$file} detected as already archived...", __LINE__);
            $ARCHIVED = true;
        }
    }
    $realmailfrom = str_replace("<", "", $realmailfrom);
    $realmailfrom = str_replace(">", "", $realmailfrom);
    $realmailto = str_replace(">", "", $realmailto);
    $realmailto = str_replace("<", "", $realmailto);
    if ($GLOBALS["VERBOSE"]) {
        echo "X-REAL-MAILFROM: `{$realmailfrom}` X-REAL-RCPTTO: `{$realmailto}`\n";
    }
    if ($GLOBALS["VERBOSE"]) {
        echo "MailArchiverToMailBox = {$MailArchiverToMailBox};MailArchiverSMTP={$MailArchiverSMTP}; \n";
    }
    if ($MailArchiverToMailBox == 1) {
        if ($GLOBALS["VERBOSE"]) {
            echo "archive_process_smtp({$fullmessagesdir},{$realmailfrom})\n";
        }
        if (!$ARCHIVED) {
            if (!archive_process_smtp($file, $realmailfrom)) {
                return false;
            }
        }
    }
    if ($MailArchiverSMTP == 1) {
        if (!$ARCHIVED) {
            if (!archive_process_smtpsrv($file, $realmailfrom, $realmailto)) {
                return false;
            }
        }
    }
    if (!$ARCHIVED) {
        if (!archive_process_copyto($file, $realmailfrom, $realmailto)) {
            return false;
        }
    }
    if ($MailArchiverToMySQL == 0) {
        return true;
    }
    $ldap = new clladp();
    $q = new mysql_mailarchive_builder();
    events("Unpack {$target_file}");
    $mm = new demime($target_file);
    if (!$mm->unpack()) {
        events("Failed unpack with error \"{$mm->error}\"");
        if ($mm->MustkillMail) {
            @unlink($target_file);
        }
        return false;
    }
    $message_html = $mm->ExportToHtml($target_file);
    if (strlen($message_html) == 0) {
        system_admin_events("{$target_file}: HTML FAILED...", __FUNCTION__, __FILE__, __LINE__, "archive");
        return false;
    }
    if (count($mm->mailto_array) == 0) {
        if ($realmailto != null) {
            $mm->mailto_array[] = $realmailto;
        }
    }
    if (count($mm->mailto_array) == 0) {
        system_admin_events("{$target_file}: Fatal No recipients Aborting", __FUNCTION__, __FILE__, __LINE__, "archive");
        return true;
    }
    $filesize = @filesize($target_file);
    events("Message with " . count($mm->mailto_array) . " recipients html file:" . strlen($message_html) . " bytes");
    if ($realmailfrom != null) {
        $mm->mailfrom = $realmailfrom;
    }
    if (preg_match("#(.+?)@(.+)#", $mm->mailfrom, $re)) {
        $domain_from = $re[2];
    }
    $message_html = addslashes($message_html);
    $mm->message_date = date("Y-m-d H:i:s", $timeMessage);
    $tableDest = date("Ymd", $timeMessage);
    if (!$q->BuildDayTable($tableDest)) {
        system_admin_events("Fatal unable to create {$tableDest} date...", __FUNCTION__, __FILE__, __LINE__, "archive");
        return false;
    }
    $SubjectMysql = addslashes(mime_decode($mm->subject));
    while (list($num, $recipient) = each($mm->mailto_array)) {
        if (preg_match("#(.+?)@(.+)#", $recipient, $re)) {
            $recipient_domain = $re[2];
        }
        $ou = $mm->GetOuFromEmail($recipient);
        $sql_source_file = $target_file;
        events("(New message)time={$mm->message_date} message-id=<{$mm->message_id}> from=<{$mm->mailfrom}> to=<{$recipient}> size={$filesize}");
        $newmessageid = md5($mm->message_id . $recipient);
        $sqlfilesize = @filesize($target_file);
        $BinMessg = addslashes(fread(fopen($target_file, "r"), $sqlfilesize));
        $sql = "INSERT IGNORE INTO `{$tableDest}` (\n\t\t\t\tMessageID,\n\t\t\t\tzDate,\n\t\t\t\tmailfrom,\n\t\t\t\tmailfrom_domain,\n\t\t\t\tsubject,\n\t\t\t\tMessageBody,\n\t\t\t\torganization,\n\t\t\t\tmailto,\n\t\t\t\tfile_path,\n\t\t\t\toriginal_messageid,\n\t\t\t\tmessage_size,\n\t\t\t\tBinMessg,filename,filesize\n\t\t\t\t)\n\t\t\tVALUES(\n\t\t\t\t'{$newmessageid}',\n\t\t\t\t'{$mm->message_date}',\n\t\t\t\t'{$mm->mailfrom}',\n\t\t\t\t'{$domain_from}',\n\t\t\t\t'{$SubjectMysql}',\n\t\t\t\t'{$message_html}',\n\t\t\t\t'{$ou}',\n\t\t\t\t'{$recipient}',\n\t\t\t\t'{$sql_source_file}',\n\t\t\t\t'{$mm->message_id}',\n\t\t\t\t'{$filesize}','{$BinMessg}','{$filename}','{$sqlfilesize}')";
        if (!$q->QUERY_SQL($sql)) {
            system_admin_events("Fatal {$q->mysql_error}", __FUNCTION__, __FILE__, __LINE__, "archive");
            return false;
        }
    }
    events("Analyze sender {$mm->mailfrom}...");
    $ou = $mm->GetOuFromEmail($mm->mailfrom);
    if ($ou == null) {
        events("Not organization found for {$mm->mailfrom}...");
        return true;
    }
    $recipients = $mm->mailto_array;
    $impled_rctp = implode(";", $recipients);
    $sql = "INSERT IGNORE INTO `{$tableDest}` (\n\t\t\t\tMessageID,\n\t\t\t\tzDate,\n\t\t\t\tmailfrom,\n\t\t\t\tmailfrom_domain,\n\t\t\t\tsubject,\n\t\t\t\tMessageBody,\n\t\t\t\torganization,\n\t\t\t\tmailto,\n\t\t\t\tfile_path,\n\t\t\t\toriginal_messageid,\n\t\t\t\tmessage_size,BinMessg,filename,filesize\n\t\t\t\t)\n\t\t\tVALUES(\n\t\t\t\t'{$newmessageid}',\n\t\t\t\t'{$mm->message_date}',\n\t\t\t\t'{$mm->mailfrom}',\n\t\t\t\t'{$domain_from}',\n\t\t\t\t'{$SubjectMysql}',\n\t\t\t\t'{$message_html}',\n\t\t\t\t'{$ou}',\n\t\t\t\t'{$impled_rctp}',\n\t\t\t\t'{$sql_source_file}',\n\t\t\t\t'{$mm->message_id}',\n\t\t\t\t'{$filesize}','{$BinMessg}','{$filename}','{$sqlfilesize}')";
    $q->QUERY_SQL($sql);
    if (!$q->ok) {
        system_admin_events("Fatal {$q->mysql_error}", __FUNCTION__, __FILE__, __LINE__, "archive");
        return false;
    }
    WriteToSyslogMail("{$mm->message_id}: <{$mm->mailfrom}> to: <{$impled_rctp}> size={$filesize} bytes (saved into backup area)", __FILE__);
    events("time={$mm->message_date} message-id=<{$mm->message_id}> from=<{$mm->mailfrom}> to=<{$impled_rctp}> size={$filesize}");
    return true;
}
function MessageID_resend_popup()
{
    $tpl = new templates();
    $q = new mysql_mailarchive_builder();
    $sql = "SELECT mailto,mailfrom,message_size,original_messageid,zDate FROM `{$_GET["table"]}` WHERE MessageID='{$_GET["MessageID-resend-popup"]}'";
    $ligne = @mysql_fetch_array($q->QUERY_SQL($sql));
    $subkect = mime_decode($ligne["subject"]);
    $page = CurrentPageName();
    $t = time();
    $tpl = new templates();
    $ligne["zDate"] = date('{l} d {F} H:i:s', strtotime($ligne["zDate"]));
    $html = "\n\t<div class=BodyContent>\n\t<table style='width:100%'>\n\t<tr>\n\t\t<td class=legend style='font-size:16px'>{zDate}:</td>\n\t\t<td style='font-size:16px'>{$ligne["zDate"]}</td>\n\t</tr>\t\t\n\t<tr>\n\t\t<td class=legend style='font-size:16px'>{message_id}:</td>\n\t\t<td style='font-size:16px'>{$ligne["original_messageid"]}</td>\n\t</tr>\t\n\t<tr>\n\t\t<td class=legend style='font-size:16px'>{sender}:</td>\n\t\t<td>" . Field_text("mailfrom-{$t}", $ligne["mailfrom"], "font-size:16px;width:240px") . "</td>\n\t</tr>\n\t<tr>\n\t\t<td class=legend style='font-size:16px'>{recipient}:</td>\n\t\t<td>" . Field_text("mailto-{$t}", $ligne["mailto"], "font-size:16px;width:240px") . "</td>\n\t</tr>\n\t<tr>\n\t\t<td class=legend style='font-size:16px'>{size}:</td>\n\t\t<td style='font-size:16px'>" . FormatBytes($ligne["message_size"] / 1024) . "</td>\n\t</tr>\n\t<tr>\n\t\t<td colspan=2 align=right><hr>" . button("{resend}", "Resend{$t}()", "18px") . "</td>\n\t</tr>\n\t</table>\t\n\t</div>\n\t<span id='{$t}-div'></span>\n<script>\n\tvar x_Resend{$t}= function (obj) {\n\t\tvar results=obj.responseText;\n\t\tdocument.getElementById('{$t}-div').innerHTML=results;\n\t}\t\t\n\t\n\t\t\n\tfunction  Resend{$t}(){\n\t\t\n\t\t\n\t\tAnimateDiv('{$t}-div');\n\t\tvar mailfrom=document.getElementById('mailfrom-{$t}').value;\n\t\tvar mailto=document.getElementById('mailto-{$t}').value;\n\t\t\n\t\tvar XHR = new XHRConnection();\n\t\tXHR.appendData('mailfrom',mailfrom);\n\t\tXHR.appendData('mailto',mailto);\n\t\tXHR.appendData('MessageID-send','{$_GET["MessageID-resend-popup"]}');\n\t\tXHR.appendData('table','{$_GET["table"]}');\n\t\tXHR.sendAndLoad('{$page}', 'POST',x_Resend{$t});\n\t\t}\n</script>\t\n\t";
    echo $tpl->_ENGINE_parse_body($html);
}
function MessageID_js()
{
    $q = new mysql_mailarchive_builder();
    $sql = "SELECT subject FROM `{$_GET["table"]}` WHERE MessageID='{$_GET["MessageID-js"]}'";
    $ligne = @mysql_fetch_array($q->QUERY_SQL($sql));
    $subkect = mime_decode($ligne["subject"]);
    $page = CurrentPageName();
    echo "YahooWin('800','{$page}?MessageID={$_GET["MessageID-js"]}&table={$_GET["table"]}','{$subkect}');";
}