Example #1
0
function PostfixFullProcess($file)
{
    $q = new mysql();
    $org_file = $file;
    if (!is_file($file)) {
        return null;
    }
    $ini = new Bs_IniHandler($file);
    $delivery_success = $ini->_params["TIME"]["delivery_success"];
    $message_id = $ini->_params["TIME"]["message-id"];
    $time_end = $ini->_params["TIME"]["time_end"];
    $mailfrom = $ini->_params["TIME"]["mailfrom"];
    $mailto = $ini->_params["TIME"]["mailto"];
    $delivery_success = $ini->_params["TIME"]["delivery_success"];
    $bounce_error = $ini->_params["TIME"]["bounce_error"];
    $time_connect = $ini->_params["TIME"]["time_connect"];
    $delivery_user = $ini->_params["TIME"]["delivery_user"];
    $time_start = $ini->_params["TIME"]["time_start"];
    $smtp_sender = $ini->_params["TIME"]["smtp_sender"];
    $size = $ini->_params["TIME"]["size"];
    $search_postfix_id = true;
    if ($time_connect == null) {
        if ($time_start != null) {
            $time_connect = $time_start;
        }
    }
    $file = basename($file);
    $postfix_id = str_replace(".msg", "", $file);
    events("PostfixFullProcess()::[{$postfix_id}] file:{$file}");
    if (preg_match("#delivery temporarily suspended.+?Local configuration error#", $bounce_error)) {
        $bounce_error = "Remote Server error";
        $delivery_success = "no";
    }
    if (preg_match("#250.+?mail accepted for delivery#", $bounce_error)) {
        $bounce_error = "Sended";
        $delivery_success = "yes";
    }
    if (preg_match("#Scanning timed out#", $bounce_error)) {
        $bounce_error = "Scanning timed out";
        $delivery_success = "no";
    }
    if ($mailto == null) {
        if ($delivery_user != null) {
            $mailto = $delivery_user;
        }
    }
    if ($time_connect == null) {
        if ($time_end != null) {
            $time_connect = $time_end;
        }
    }
    if ($time_end == null) {
        if ($time_connect != null) {
            $time_end = $time_connect;
        }
    }
    if (preg_match('#(.+?)@(.+)#', $mailfrom, $re)) {
        $domain_from = $re[2];
    }
    if (preg_match('#(.+?)@(.+)#', $mailto, $re)) {
        $domain_to = $re[2];
    }
    $mailfrom = str_replace("'", '', $mailfrom);
    $mailto = str_replace("'", '', $mailto);
    if ($delivery_success == null) {
        $delivery_success = "no";
    }
    if ($message_id == null) {
        $message_id = md5(time() . $mailfrom . $mailto);
    }
    $bounce_error_array["RBL"] = true;
    $bounce_error_array["Helo command rejected"] = true;
    $bounce_error_array["Domain not found"] = true;
    $bounce_error_array["too many recipients"] = true;
    $bounce_error_array["PostScreen RBL"] = true;
    $bounce_error_array["PostScreen"] = true;
    $bounce_error_array["Scanning timed out"] = true;
    $bounce_error_array["blacklisted"] = true;
    if (preg_match("#lost connection with.+?\\[(.+?)\\]\\s+#", $bounce_error, $re)) {
        $bounce_error = "lost connection";
        $delivery_success = "no";
        $smtp_sender = $re[1];
    }
    if ($bounce_error_array[$bounce_error]) {
        $search_postfix_id = false;
    }
    if ($smtp_sender != null) {
        $array_geo = GeoIP($smtp_sender);
        $Country = $array_geo[0];
        $City = $array_geo[1];
        $City = addslashes($City);
        $Country = addslashes($Country);
    }
    if (preg_match("#,sender_user='******'#", $mailfrom, $re)) {
        $mailfrom = $re[1];
    }
    if ($search_postfix_id) {
        $sqlid = getid_from_postfixid($postfix_id, $q);
    }
    events("PostfixFullProcess():: {$time_connect}:: message-id=<{$message_id}> from=<{$mailfrom}> to=<{$mailto}> bounce_error=<{$bounce_error}> old id={$sqlid}");
    if ($sqlid == null) {
        $sql = "INSERT IGNORE INTO smtp_logs (delivery_id_text,msg_id_text,time_connect,time_sended,delivery_success,sender_user,sender_domain,delivery_user,delivery_domain,bounce_error,smtp_sender,Country  )\n\t\tVALUES('{$postfix_id}','{$message_id}','{$time_connect}','{$time_end}','{$delivery_success}','{$mailfrom}','{$domain_from}','{$mailto}','{$domain_to}','{$bounce_error}','{$smtp_sender}','{$Country}');\n\t\t";
        if (strlen($message_id) > 255) {
            $message_id = md5($message_id);
        }
        events_cnx(__FUNCTION__ . "() ADD:[{$message_id}] [{$smtp_sender}] from=<{$mailfrom}> to=<{$mailto}> \"{$bounce_error}\" line:" . __LINE__);
        $q->QUERY_SQL($sql, "artica_events");
        if ($q->ok) {
            events("PostfixFullProcess():: Delete {$org_file} line:" . __LINE__);
            @unlink($org_file);
            return true;
        } else {
            events_cnx("FAILED MYSQL {$org_file}");
            events("PostfixFullProcess():: {$q->mysql_error} line:" . __LINE__);
            if (preg_match("#Error.+?File .+?smtp_logs.+?not found#", $q->mysql_error)) {
                $unix = new unix();
                $unix->send_email_events("artica_events/smtp_logs table is crashed", "mysql claim:{$q->mysql_error}\nThe table has been deleted and rebuilded", "system");
                $sql = "DROP TABLE `smtp_logs`";
                $q->QUERY_SQL($sql, "artica_events");
                $q->BuildTables();
            }
            events($sql);
            return false;
        }
    } else {
        $mailfrom = str_replace(">, orig_to=", "", $mailfrom);
        events_cnx("EDIT:[{$sqlid}] from=<{$mailfrom}> to=<{$mailto}> bounce_error=\"{$bounce_error}\"");
        if ($mailfrom != null) {
            $mailfrom = " ,sender_user='******'";
        }
        if ($delivery_success != null) {
            $delivery_success = " ,delivery_success='{$delivery_success}'";
        }
        if ($domain_from != null) {
            $domain_from = " ,sender_domain='{$domain_from}'";
        }
        if ($domain_to != null) {
            $domain_to = " ,delivery_domain='{$domain_to}'";
        }
        if ($bounce_error != null) {
            $bounce_error = " ,bounce_error='{$bounce_error}'";
        }
        if ($time_connect != null) {
            $time_connect = " ,time_connect='{$time_connect}'";
        }
        if ($time_end != null) {
            $time_end = " ,time_sended='{$time_end}'";
        }
        if ($message_id != null) {
            $message_id = " ,msg_id_text='{$message_id}'";
        }
        if ($smtp_sender != null) {
            $smtp_sender = " ,smtp_sender='{$smtp_sender}'";
        }
        if ($size != null) {
            $size = " ,bytes='{$size}'";
        }
        $sql = "UPDATE smtp_logs SET delivery_id_text='{$postfix_id}'{$mailfrom}{$delivery_success}{$domain_from}{$domain_to}{$bounce_error}{$time_connect}{$time_end}{$message_id}{$size}\n\t\tWHERE id={$sqlid}";
        $q->QUERY_SQL($sql, "artica_events");
        if ($q->ok) {
            @unlink($org_file);
            return true;
        } else {
            events_cnx("FAILED MYSQL {$org_file}");
            events("{$q->mysql_error}");
            events($sql);
            return false;
        }
    }
}
function PostfixFullProcess($file)
{
    $q = new mysql();
    $org_file = $file;
    if (!is_file($file)) {
        return null;
    }
    $ini = new Bs_IniHandler($file);
    $delivery_success = $ini->_params["TIME"]["delivery_success"];
    $message_id = $ini->_params["TIME"]["message-id"];
    $time_end = $ini->_params["TIME"]["time_end"];
    $mailfrom = $ini->_params["TIME"]["mailfrom"];
    $mailto = $ini->_params["TIME"]["mailto"];
    $delivery_success = $ini->_params["TIME"]["delivery_success"];
    $bounce_error = $ini->_params["TIME"]["bounce_error"];
    $time_connect = $ini->_params["TIME"]["time_connect"];
    $delivery_user = $ini->_params["TIME"]["delivery_user"];
    $time_start = $ini->_params["TIME"]["time_start"];
    $smtp_sender = $ini->_params["TIME"]["smtp_sender"];
    $search_postfix_id = true;
    if ($time_connect == null) {
        if ($time_start != null) {
            $time_connect = $time_start;
        }
    }
    $file = basename($file);
    $postfix_id = str_replace(".msg", "", $file);
    if (preg_match("#delivery temporarily suspended.+?Local configuration error#", $bounce_error)) {
        $bounce_error = "Remote Server error";
        $delivery_success = "no";
    }
    if ($mailto == null) {
        if ($delivery_user != null) {
            $mailto = $delivery_user;
        }
    }
    if ($time_connect == null) {
        if ($time_end != null) {
            $time_connect = $time_end;
        }
    }
    if ($time_end == null) {
        if ($time_connect != null) {
            $time_end = $time_connect;
        }
    }
    if (preg_match('#(.+?)@(.+)#', $mailfrom, $re)) {
        $domain_from = $re[2];
    }
    if (preg_match('#(.+?)@(.+)#', $mailto, $re)) {
        $domain_to = $re[2];
    }
    $mailfrom = str_replace("'", '', $mailfrom);
    if ($delivery_success == null) {
        $delivery_success = "no";
    }
    if ($message_id == null) {
        $message_id = md5(time() . $mailfrom . $mailto);
    }
    $bounce_error_array["RBL"] = true;
    $bounce_error_array["Helo command rejected"] = true;
    $bounce_error_array["Domain not found"] = true;
    if ($bounce_error_array[$bounce_error]) {
        $search_postfix_id = false;
    } else {
        events("PostfixFullProcess():: bounce ERROR=\"{$bounce_error}\"");
    }
    if ($search_postfix_id) {
        $sqlid = getid_from_postfixid($postfix_id, $q);
    }
    events("PostfixFullProcess():: message-id={$message_id} from={$mailfrom} to={$mailto} bounce_error={$bounce_error}");
    if ($sqlid == null) {
        $sql = "INSERT INTO smtp_logs (delivery_id_text,msg_id_text,time_connect,time_sended,delivery_success,sender_user,sender_domain,delivery_user,delivery_domain,bounce_error,smtp_sender  )\n\t\tVALUES('{$postfix_id}','{$message_id}','{$time_connect}','{$time_end}','{$delivery_success}','{$mailfrom}','{$domain_from}','{$mailto}','{$domain_to}','{$bounce_error}','{$smtp_sender}');\n\t\t";
        events("PostfixFullProcess():: INSERT {$message_id} or {$postfix_id}");
        $q->QUERY_SQL($sql, "artica_events");
        events("PostfixFullProcess():: done..");
        if ($q->ok) {
            events("PostfixFullProcess():: ADD time=<{$time_connect}/{$time_end}> id=<{$postfix_id}/{$messageid}> rcpt=<{$mailto}> From=<{$mailfrom}> server=<{$smtp_sender}> bounce=<{$bounce_error}>");
            events("PostfixFullProcess():: Delete {$org_file} DONE... finish");
            @unlink($org_file);
            return true;
        } else {
            echo events("FAILED MYSQL {$org_file}");
            events($sql);
            return false;
        }
    } else {
        $mailfrom = str_replace(">, orig_to=", "", $mailfrom);
        if ($mailfrom != null) {
            $mailfrom = " ,sender_user='******'";
        }
        if ($delivery_success != null) {
            $delivery_success = " ,delivery_success='{$delivery_success}'";
        }
        if ($domain_from != null) {
            $domain_from = " ,sender_domain='{$domain_from}'";
        }
        if ($domain_to != null) {
            $domain_to = " ,delivery_domain='{$domain_to}'";
        }
        if ($bounce_error != null) {
            $bounce_error = " ,bounce_error='{$bounce_error}'";
        }
        if ($time_connect != null) {
            $time_connect = " ,time_connect='{$time_connect}'";
        }
        if ($time_end != null) {
            $time_end = " ,time_sended='{$time_end}'";
        }
        if ($message_id != null) {
            $message_id = " ,msg_id_text='{$message_id}'";
        }
        if ($smtp_sender != null) {
            $smtp_sender = " ,smtp_sender='{$smtp_sender}'";
        }
        events("PostfixFullProcess():: EDIT {$message_id} or {$postfix_id}");
        $sql = "UPDATE smtp_logs SET delivery_id_text='{$postfix_id}'{$mailfrom}{$delivery_success}{$domain_from}{$domain_to}{$bounce_error}{$time_connect}{$time_end}{$message_id}\n\t\tWHERE id={$sqlid}";
        $q->QUERY_SQL($sql, "artica_events");
        if ($q->ok) {
            events("EDIT time=<{$time_connect}/{$time_end}> id=<{$postfix_id}/{$messageid}> rcpt=<{$mailto}> From=<{$mailfrom}> server=<{$smtp_sender}> bounce=<{$bounce_error}>");
            events("PostfixFullProcess():: Delete {$org_file} DONE... finish");
            @unlink($org_file);
            return true;
        } else {
            events("FAILED MYSQL {$org_file}");
            events($sql);
            return false;
        }
    }
}