示例#1
0
function ParseEvents()
{
    $path = "/var/log/artica-postfix/events";
    $f = new filesClasses();
    $hash = $f->DirListTable($path);
    if (!is_array($hash)) {
        return null;
    }
    echo date('Y-m-d h:i:s') . " " . count($hash) . " file(s) notifications...\n";
    $mysql = new mysql();
    while (list($num, $file) = each($hash)) {
        $text = null;
        $processname = null;
        $date = null;
        $context = null;
        $subject = null;
        $recipient = null;
        $bigtext = @file_get_contents($path . '/' . $file);
        echo date('Y-m-d h:i:s') . " Parsing {$file} " . strlen($bigtext) . " bytes text\n";
        $ini = new Bs_IniHandler();
        if (preg_match("#<text>(.+?)</text>#is", $bigtext, $re)) {
            $text = $re[1];
            if (strlen($text) > 0) {
                $bigtext = str_replace($re[0], '', $bigtext);
            }
        }
        if (preg_match("#<attachedfiles>(.+?)</attachedfiles>#is", $bigtext, $re)) {
            $files_text = addslashes($re[1]);
        }
        $ini->loadString($bigtext);
        $processname = $ini->_params["LOG"]["processname"];
        $date = $ini->_params["LOG"]["date"];
        $context = $ini->_params["LOG"]["context"];
        $context = addslashes($context);
        if (strlen($text) < 2) {
            $text = $ini->_params["LOG"]["text"];
        }
        $text = addslashes($text);
        $subject = $ini->_params["LOG"]["subject"];
        $recipient = $ini->_params["LOG"]["recipient"];
        $subject = addslashes($subject);
        echo date('Y-m-d h:i:s') . " Parsing subject {$subject} " . strlen($text) . " bytes text\n";
        writelogs("New notification: {$subject} (" . strlen($text) . " bytes)", __FUNCTION__, __FILE__, __LINE__);
        $sql = "INSERT INTO events (zDate,hostname,\n        \tprocess,text,context,content,attached_files,recipient) VALUES(\n        \t'{$date}',\n        \t'{$mysql->hostname}',\n        \t'{$processname}',\n        \t'{$subject}',\n        \t'{$context}','{$text}','{$files_text}','{$recipient}')";
        echo date('Y-m-d h:i:s') . " run mysql query\n";
        if ($mysql->QUERY_SQL($sql, 'artica_events')) {
            unlink($path . '/' . $file);
        } else {
            error_log("Mysql error keep {$path}/{$file};");
            error_log("{$mysql->mysql_error}");
            if (preg_match("#Unknown column#", $mysql->mysql_error)) {
                error_log("->BuildTables()");
                $mysql->BuildTables();
            }
        }
    }
    if (count($hash) > 0) {
        events(count($hash) . " events queue parsed...");
    }
}
示例#2
0
function ParseEvents()
{
    ParseReboot();
    $unix = new unix();
    $path = "/var/log/artica-postfix/events";
    $f = new filesClasses();
    $hash = $f->DirListTable($path);
    if (!is_array($hash)) {
        return null;
    }
    $users = new usersMenus();
    $sock = new sockets();
    $ArticaMetaEnabled = $sock->GET_INFO("ArticaMetaEnabled");
    echo date('Y-m-d h:i:s') . " " . count($hash) . " file(s) notifications...\n";
    events(count($hash) . " file(s) notifications...");
    if (count($hash) == 0) {
        return;
    }
    if ($ArticaMetaEnabled == 1) {
        $meta = new artica_meta();
        $datasToSend = base64_encode(serialize($meta->GLOBAL_ARRAY));
        $ArticaMetaHostname = $meta->ArticaMetaHostname;
        events("ArticaMetaEnabled: \"{$ArticaMetaEnabled}\" -> ({$ArticaMetaHostname})");
    }
    $mysql = new mysql();
    while (list($num, $file) = each($hash)) {
        $text = null;
        $processname = null;
        $date = null;
        $context = null;
        $subject = null;
        $recipient = null;
        $FULL_PATH = "{$path}/{$file}";
        if ($unix->file_time_min($FULL_PATH) > 480) {
            @unlink($FULL_PATH);
            continue;
        }
        $bigtext = @file_get_contents($FULL_PATH);
        echo date('Y-m-d h:i:s') . " Parsing {$file} " . strlen($bigtext) . " bytes text\n";
        $ini = new Bs_IniHandler();
        if (preg_match("#<text>(.+?)</text>#is", $bigtext, $re)) {
            $text = $re[1];
            $bigtext = str_replace($re[0], '', $bigtext);
            $bigtext = str_replace("'", "`", $bigtext);
        }
        if (preg_match("#<attachedfiles>(.+?)</attachedfiles>#is", $bigtext, $re)) {
            $bigtext = str_replace($re[0], '', $bigtext);
            $files_text = addslashes($re[1]);
        }
        $ini->loadString($bigtext);
        $processname = $ini->_params["LOG"]["processname"];
        $date = $ini->_params["LOG"]["date"];
        $context = $ini->_params["LOG"]["context"];
        if ($context == "YTowOnt9") {
            $context = "system";
        }
        $subject = $ini->_params["LOG"]["subject"];
        $recipient = $ini->_params["LOG"]["recipient"];
        if (strlen($text) < 2) {
            $text = $ini->_params["LOG"]["text"];
        }
        $arrayToSend["context"] = $context;
        $arrayToSend["subject"] = $subject;
        $arrayToSend["text"] = $text;
        $arrayToSend["date"] = $date;
        echo date('Y-m-d h:i:s') . " Parsing subject {$subject} " . strlen($text) . " bytes text\n";
        writelogs("New notification: {$subject} (" . strlen($text) . " bytes) {$date}", __FUNCTION__, __FILE__, __LINE__);
        $event_id = time();
        //$text=addslashes($text);
        $text = str_replace("'", "`", $text);
        $subject = str_replace("'", "`", $subject);
        $text = addslashes($text);
        $subject = addslashes($subject);
        if ($subject == null) {
            if (strlen($text) > 5) {
                if (strpos($text, "\n") == 0) {
                    $subject = $text;
                } else {
                    $subject = substr($text, 0, 75) . "...";
                }
            }
        }
        if (strlen($subject) < 5) {
            writelogs("Warning New notification: Subject seems to be empty ! \"{$text}\" removing {$path}/{$file}", __FUNCTION__, __FILE__, __LINE__);
            @unlink("{$path}/{$file}");
            continue;
        }
        if (strpos($users->hostname, ".") > 0) {
            $ty = explode($users->hostname, ".");
            $users->hostname = $ty[0];
        }
        if (strlen($text) < 5) {
            $text = "No content body as been added for this notification";
        }
        $sql = "INSERT IGNORE INTO events (zDate,hostname,process,text,context,content,attached_files,recipient,event_id) VALUES(\n        \t'{$date}',\n        \t'{$users->hostname}',\n        \t'{$processname}',\n        \t'{$subject}',\n        \t'{$context}','{$text}','{$files_text}','{$recipient}','{$event_id}')";
        if (!$mysql->UseMysql) {
            $sql = "INSERT IGNORE INTO events (id,zDate,hostname,\n        \tprocess,text,context,content,attached_files,recipient,event_id) VALUES(\n        \t'{$event_id}','{$date}',\n        \t'{$users->hostname}',\n        \t'{$processname}',\n        \t'{$subject}',\n        \t'{$context}','{$text}','{$files_text}','{$recipient}','{$event_id}')";
        }
        events(date('Y-m-d h:i:s') . " run mysql query -> {$subject}");
        if (!$mysql->QUERY_SQL($sql, 'artica_events')) {
            events("Mysql error keep {$path}/{$file};");
            events("Fatal: {$mysql->mysql_error}", __FUNCTION__, __FILE__, __LINE__);
            if (preg_match("#Access denied for user.+?using password:#", $mysql->mysql_error)) {
                events("Access denied for user password: {$mysql->mysql_server}@{$mysql->mysql_admin}:{$mysql->mysql_password} detected");
                if ($mysql->mysql_server == "127.0.0.1" or $mysql->mysql_server == "localhost") {
                    $ldap = new clladp();
                    $unix = new unix();
                    $ldap->ldap_password = $unix->shellEscapeChars($ldap->ldap_password);
                    writelogs("Try to change the mysql password: /usr/share/artica-postfix/bin/artica-install --change-mysqlroot --inline root \"secret\"", __FUNCTION__, __FILE__, __LINE__);
                    exec("/usr/share/artica-postfix/bin/artica-install --change-mysqlroot --inline root \"secret\" 2>&1", $chroot);
                    writelogs("Chaning password="******"\n", $chroot), __FUNCTION__, __FILE__, __LINE__);
                    die;
                }
            }
            if (preg_match("#Unknown column#", $mysql->mysql_error)) {
                events("->BuildTables()");
                $mysql->BuildTables();
            }
            if (preg_match("#Unknown database#", $mysql->mysql_error)) {
                events("->BuildTables()");
                $mysql->BuildTables();
            }
            if (preg_match("#connect to local MySQL server through socket#", $mysql->mysql_error)) {
                shell_exec("/etc/init.d/artica-postfix start mysql &");
            }
            writelogs("{$sql}", __FUNCTION__, __FILE__, __LINE__);
            break;
        }
        unlink($path . '/' . $file);
        if ($ArticaMetaEnabled == 1) {
            $pidfile = "/etc/artica-postfix/pids/exec.artica.meta.php.SendStatus.pid";
            $sock = new sockets();
            $ArticaMetaPoolTimeMin = $sock->GET_INFO("ArticaMetaPoolTimeMin");
            if (!is_numeric($ArticaMetaPoolTimeMin)) {
                $ArticaMetaPoolTimeMin = 15;
            }
            if ($ArticaMetaPoolTimeMin < 2) {
                $ArticaMetaPoolTimeMin = 15;
            }
            $minutes = file_time_min($pidfile);
            if ($minutes < round($ArticaMetaPoolTimeMin / 2.5)) {
                $meta->events(basename($pidfile) . ":{$minutes}<" . round($ArticaMetaPoolTimeMin / 2.5) . "Mn, aborting", __FUNCTION__, __FILE__, __LINE__);
                return;
            }
            $http = new httpget();
            $meta->events("Send notification \"{$arrayToSend["subject"]}\" to Meta Server", __FUNCTION__, __FILE__, __LINE__);
            $metaconsole = $http->send("{$ArticaMetaHostname}/lic.status.notifs.php", "post", array("DATAS" => $datasToSend, "NOTIF" => base64_encode(serialize($arrayToSend))));
            events("META CONSOLE: {$metaconsole} aborting notifications");
            if ($metaconsole == "FAILED_CONNECT") {
                $meta->events("Result:\"{$metaconsole}\"", __FUNCTION__, __FILE__, __LINE__);
                return;
            }
            if (!is_file("/etc/artica-postfix/artica-meta.tasks")) {
                if (preg_match("#<TASKS>(.+?)</TASKS>#is", $metaconsole, $re)) {
                    $meta->events("Save tasks to /etc/artica-postfix/artica-meta.tasks", __FUNCTION__, __FILE__, __LINE__);
                    @file_put_contents("/etc/artica-postfix/artica-meta.tasks", $re[1]);
                    $cmd = LOCATE_PHP5_BIN() . " " . dirname(__FILE__) . "/exec.artica.meta.tasks.php >/dev/null 2>&1 &";
                    $meta->events("TASKS ->{$cmd}", __FUNCTION__, __FILE__, __LINE__);
                    shell_exec($cmd);
                }
            }
        }
        $text = addslashes($text);
        $context = addslashes($context);
        $subject = addslashes($subject);
    }
    if (count($hash) > 0) {
        events(count($hash) . " events queue parsed...");
    }
    if ($ArticaMetaEnabled == 1) {
        if (is_file("/etc/artica-postfix/artica-meta.tasks")) {
            $NICE = EXEC_NICE();
            shell_exec($NICE . LOCATE_PHP5_BIN2() . " " . dirname(__FILE__) . "/exec.artica.meta.tasks.php &");
        }
    }
}