function UpdateUtility()
{
    if ($GLOBALS["VERBOSE"]) {
        echo "Line: " . __LINE__ . ":: " . __FUNCTION__ . "\n";
    }
    $unix = new unix();
    $sock = new sockets();
    $unix = new unix();
    $pidfile = "/etc/artica-postfix/" . basename(__FILE__) . ".pid";
    $pidtime = "/etc/artica-postfix/" . basename(__FILE__) . ".time";
    $pidtimeT = $unix->file_time_min($pidtime);
    if ($pidtimeT < 3) {
        if (!$GLOBALS["FORCE"]) {
            if ($GLOBALS["VERBOSE"]) {
                echo "Line: " . __LINE__ . ":: last execution time {$pidtimeT} (require 3mn) or set --force " . __FUNCTION__ . "\n";
            }
            die;
        }
    }
    $pid = @file_get_contents($pidfile);
    if ($unix->process_exists($pid, basename(__FILE__))) {
        $ProcessTime = $unix->PROCCESS_TIME_MIN($pid);
        if ($GLOBALS["VERBOSE"]) {
            echo "Line: " . __LINE__ . ":: Process {$pid}  already in memory since {$ProcessTime} minutes - " . __FUNCTION__ . "\n";
        }
        system_admin_events("Process {$pid}  already in memory since {$ProcessTime} minutes", "MAIN", __FILE__, __LINE__, "updateutility");
        die;
    }
    @unlink($pidtime);
    @file_put_contents($pidtime, time());
    @file_put_contents($pidfile, getmypid());
    $UpdateUtility_Console = $unix->find_program("UpdateUtility-Console");
    if (!is_file($UpdateUtility_Console)) {
        if ($GLOBALS["VERBOSE"]) {
            echo "Line: " . __LINE__ . ":: UpdateUtility-Console no such binary - " . __FUNCTION__ . "\n";
        }
        system_admin_events("UpdateUtility-Console no such binary", __FUNCTION__, __FILE__, __LINE__, "update");
    }
    if ($GLOBALS["VERBOSE"]) {
        echo "Line: " . __LINE__ . ":: UpdateUtility_Console - {$UpdateUtility_Console}" . __FUNCTION__ . "\n";
    }
    @copy($UpdateUtility_Console, "/etc/UpdateUtility/UpdateUtility-Console");
    $UpdateUtilityAllProducts = $sock->GET_INFO("UpdateUtilityAllProducts");
    $UpdateUtilityOnlyForKav4Proxy = $sock->GET_INFO("UpdateUtilityOnlyForKav4Proxy");
    $UpdateUtilityUseLoop = $sock->GET_INFO("UpdateUtilityUseLoop");
    if (!is_numeric($UpdateUtilityAllProducts)) {
        $UpdateUtilityAllProducts = 1;
    }
    $UpdateUtilityStorePath = $sock->GET_INFO("UpdateUtilityStorePath");
    if ($UpdateUtilityStorePath == null) {
        $UpdateUtilityStorePath = "/home/kaspersky/UpdateUtility";
    }
    if (is_file("/opt/kaspersky/kav4proxy/sbin/kav4proxy-kavicapserver")) {
        if (!is_numeric($UpdateUtilityOnlyForKav4Proxy)) {
            $UpdateUtilityOnlyForKav4Proxy = 1;
        }
    }
    if ($UpdateUtilityUseLoop == 1) {
        $UpdateUtilityStorePath = "/automounts/UpdateUtility";
        $dev = $unix->MOUNTED_DIR($UpdateUtilityStorePath);
        if ($dev == null) {
            if ($GLOBALS["VERBOSE"]) {
                echo "Line: " . __LINE__ . ":: {$UpdateUtilityStorePath} -> NOT MOUNTED " . __FUNCTION__ . "\n";
            }
            @file_put_contents("/var/log/artica-postfix/UpdateUtility-report-" . time() . ".log", "Report finished at " . date("Y-m-d H:i:s") . "\n{$UpdateUtilityStorePath} not mounted!\n");
            return;
        }
        if ($GLOBALS["VERBOSE"]) {
            echo "Line: " . __LINE__ . ":: {$UpdateUtilityStorePath} -> {$dev} OK" . __FUNCTION__ . "\n";
        }
    }
    @mkdir("{$UpdateUtilityStorePath}/databases/Updates", 0755, true);
    if (!is_dir($UpdateUtilityStorePath)) {
        @file_put_contents("/var/log/artica-postfix/UpdateUtility-report-" . time() . ".log", "Report finished at " . date("Y-m-d H:i:s") . "\n{$UpdateUtilityStorePath}/databases/Updates permission denied!\n");
        return;
    }
    $updateutility = new updateutilityv2();
    if ($UpdateUtilityAllProducts == 1) {
        system_admin_events("All products as been set...", __FUNCTION__, __FILE__, __LINE__, "update");
        while (list($key, $line) = each($updateutility->ALL_PKEYS)) {
            $updateutility->MAIN_ARRAY["ComponentSettings"][$key] = "true";
        }
        if (!isset($updateutility->MAIN_ARRAY["ComponentSettings"]["DownloadAllDatabases"])) {
            $updateutility->MAIN_ARRAY["ComponentSettings"]["DownloadAllDatabases"] = "true";
        }
        if (!isset($updateutility->MAIN_ARRAY["ComponentSettings"]["KasperskyAdministrationKit_8_0_2048_2090"])) {
            $updateutility->MAIN_ARRAY["ComponentSettings"]["KasperskyAdministrationKit_8_0_2048_2090"] = "true";
        }
        if (!isset($updateutility->MAIN_ARRAY["ComponentSettings"]["KasperskySecurityCenter_9"])) {
            $updateutility->MAIN_ARRAY["ComponentSettings"]["KasperskySecurityCenter_9"] = "true";
        }
    }
    if ($UpdateUtilityOnlyForKav4Proxy == 1) {
        system_admin_events("Kav4Proxy as been set...", __FUNCTION__, __FILE__, __LINE__, "update");
        reset($updateutility->ALL_PKEYS);
        while (list($key, $line) = each($updateutility->ALL_PKEYS)) {
            $updateutility->MAIN_ARRAY["ComponentSettings"][$key] = "false";
        }
        $updateutility->MAIN_ARRAY["ComponentSettings"]["KasperskyAntiVirusProxyServer_5_5"] = "true";
        $updateutility->MAIN_ARRAY["ComponentSettings"]["KasperskyAntiVirusProxyServer_5_5_41_51"] = "true";
        $updateutility->MAIN_ARRAY["ComponentSettings"]["KasperskyAntiVirusProxyServer_5_5_62"] = "true";
        $updateutility->MAIN_ARRAY["ComponentSettings"]["KasperskyAntiVirusProxyServer_5_5_62"] = "true";
        $updateutility->MAIN_ARRAY["ComponentSettings"]["DownloadAllDatabases"] = "false";
    }
    $updateutility->MAIN_ARRAY["ShedulerSettings"]["LastUpdate"] = '@Variant(\\0\\0\\0\\x10\\0\\0\\0\\0\\xff\\xff\\xff\\xff\\xff)';
    $updateutility->MAIN_ARRAY["ShedulerSettings"]["Time"] = '@Variant(\\0\\0\\0\\xf\\0\\0\\0\\0)';
    $t = time();
    $ini = new Bs_IniHandler();
    $ini2 = new Bs_IniHandler();
    $ini->_params = $updateutility->MAIN_ARRAY;
    $ini->_params["DirectoriesSettings"]["UpdatesFolder"] = "{$UpdateUtilityStorePath}/databases";
    $ini->_params["DirectoriesSettings"]["TempFolder"] = "{$UpdateUtilityStorePath}/TempFolder";
    @mkdir("{$UpdateUtilityStorePath}/databases", 0755, true);
    @mkdir("{$UpdateUtilityStorePath}/TempFolder", 0755, true);
    $report_file = "/var/log/artica-postfix/UpdateUtility-report-" . time() . ".log";
    $ini->_params["ConnectionSettings"]["UseSpecifiedProxyServerSettings"] = "false";
    $ini->_params["ConnectionSettings"]["UseAuthenticationProxyServer"] = "false";
    $ini->_params["ReportSettings"]["ReportFileName"] = "{$report_file}";
    $datas = $sock->GET_INFO("ArticaProxySettings");
    if (trim($datas) != null) {
        $ini2->loadString($datas);
        $ArticaProxyServerEnabled = $ini->_params["PROXY"]["ArticaProxyServerEnabled"];
        $ArticaProxyServerName = $ini->_params["PROXY"]["ArticaProxyServerName"];
        $ArticaProxyServerPort = $ini->_params["PROXY"]["ArticaProxyServerPort"];
        $ArticaProxyServerUsername = trim($ini->_params["PROXY"]["ArticaProxyServerUsername"]);
        $ArticaProxyServerUserPassword = $ini->_params["PROXY"]["ArticaProxyServerUserPassword"];
        if ($ArticaProxyServerEnabled == "yes") {
            $ini->_params["ConnectionSettings"]["UseSpecifiedProxyServerSettings"] = "true";
            $ini->_params["ConnectionSettings"]["AddressProxyServer"] = $ArticaProxyServerName;
            $ini->_params["ConnectionSettings"]["PortProxyServer"] = $ArticaProxyServerPort;
            if ($ArticaProxyServerUsername != null) {
                $ini->_params["ConnectionSettings"]["UseAuthenticationProxyServer"] = "true";
                $ini->_params["ConnectionSettings"]["UserNameProxyServer"] = "{$ArticaProxyServerUsername}";
                $ini->_params["ConnectionSettings"]["PasswordProxyServer"] = "{$ArticaProxyServerUserPassword}";
            }
        }
    }
    $ini->saveFile("/etc/UpdateUtility/updater.ini");
    chdir("/etc/UpdateUtility");
    @chmod("/etc/UpdateUtility/UpdateUtility-Console", 0755);
    $cmd = "./UpdateUtility-Console -u -o /etc/UpdateUtility/updater.ini -r 2>&1";
    $Restart1 = false;
    writelogs("Running `{$cmd}`", __FUNCTION__, __FILE__, __LINE__);
    exec("{$cmd}", $results);
    while (list($key, $line) = each($results)) {
        if (preg_match("#Total downloading:\\s+([0-9]+)", $line, $re)) {
            $PERCT = $re[1];
        }
        if (preg_match("#Segmentation fault#", $line)) {
            $text = @implode("\n", $results);
            $Restart1 = true;
            system_admin_events("Segmentation fault at {$PERCT}% on UpdateUtility restart again...\n{$text}", __FUNCTION__, __FILE__, __LINE__, "update");
            $results = array();
            break;
        }
        writelogs("{$line}", __FUNCTION__, __FILE__, __LINE__);
    }
    if ($Restart1) {
        exec("{$cmd}", $results);
        while (list($key, $line) = each($results)) {
            if (preg_match("#Total downloading:\\s+([0-9]+)#", $line, $re)) {
                $PERCT = $re[1];
            }
            if (preg_match("#Segmentation fault#", $line)) {
                $text = @implode("\n", $results);
                system_admin_events("Segmentation fault at {$PERCT}%  on UpdateUtility Aborting...\n{$text}", __FUNCTION__, __FILE__, __LINE__, "update");
            }
            writelogs("{$line}", __FUNCTION__, __FILE__, __LINE__);
        }
    }
    $t2 = time();
    $timehuman = $unix->distanceOfTimeInWords($t, $t2);
    $text = @implode("\n", $results);
    system_admin_events("Executing UpdateUtility Success took {$timehuman}\n{$text}", __FUNCTION__, __FILE__, __LINE__, "update");
    $nohup = $unix->find_program("nohup");
    $php = $unix->LOCATE_PHP5_BIN();
    shell_exec("{$nohup} {$php} " . dirname(__FILE__) . "/exec.freeweb.php --reconfigure-updateutility >/dev/null 2>&1 &");
    ScanUpdateUtilityLogs(true);
    UpdateUtilitySize(true);
}
function ScanUpdateUtilityLogs($force = false)
{
    $unix = new unix();
    $sock = new sockets();
    $unix = new unix();
    $timefile = "/etc/artica-postfix/pids/ScanUpdateUtilityLogs.time";
    $pidfile = "/etc/artica-postfix/pids/ScanUpdateUtilityLogs.pid";
    if (!$force) {
        $pid = $unix->get_pid_from_file($pidfile);
        if ($unix->process_exists($pid, basename(__FILE__))) {
            $time = $unix->PROCCESS_TIME_MIN($pid);
            if ($GLOBALS["VERBOSE"]) {
                echo "Already process exists {$pid}\n";
            }
            return;
        }
        @file_put_contents($pidfile, getmypid());
        $time = $unix->file_time_min($timefile);
        if ($timefile < 10) {
            if ($GLOBALS["VERBOSE"]) {
                echo "Only each 10mn\n";
            }
            return;
        }
    }
    @unlink($timefile);
    @file_put_contents($timefile, time());
    $UpdateUtilityStorePath = $sock->GET_INFO("UpdateUtilityStorePath");
    if ($UpdateUtilityStorePath == null) {
        $UpdateUtilityStorePath = "/home/kaspersky/UpdateUtility";
    }
    $UpdateUtilityUseLoop = $sock->GET_INFO("UpdateUtilityUseLoop");
    if (!is_numeric($UpdateUtilityUseLoop)) {
        $UpdateUtilityUseLoop = 0;
    }
    if ($UpdateUtilityUseLoop == 1) {
        $UpdateUtilityStorePath = "/automounts/UpdateUtility";
    }
    if ($GLOBALS["VERBOSE"]) {
        echo "Scanning /var/log/artica-postfix/UpdateUtility-*.log...\n";
    }
    foreach (glob("/var/log/artica-postfix/UpdateUtility-*.log") as $filename) {
        $timefile = $unix->file_time_min($filename);
        $time = filemtime($filename);
        $details = @file_get_contents($filename);
        $f = explode("\n", $details);
        $isSuccess = 1;
        $files = 0;
        $size = 0;
        $rp_finish = false;
        if ($timefile > 720) {
            $rp_finish = true;
        }
        while (list($key, $line) = each($f)) {
            $nextFile = null;
            if (preg_match("#File downloaded '(.*?)'#", $line, $re)) {
                $nextFile = "{$UpdateUtilityStorePath}/databases/Updates/{$re[1]}";
                if (!is_file($nextFile)) {
                    if ($GLOBALS["VERBOSE"]) {
                        echo "{$nextFile}, no such file\n";
                    }
                    $nextFile = "{$UpdateUtilityStorePath}/TempFolder/temporaryFolder/{$re[1]}";
                }
            }
            if (preg_match("#Segmentation fault#", $line)) {
                $isSuccess = 0;
                continue;
                $rp_finish = true;
            }
            if (preg_match("#Bus error#", $line)) {
                $isSuccess = 0;
                continue;
                $rp_finish = true;
            }
            if (preg_match("#Report finished at#", $line, $re)) {
                $rp_finish = true;
                continue;
            }
            if (preg_match("#Insufficient disk space#i", $line, $re)) {
                $isSuccess = 0;
                continue;
                $rp_finish = true;
            }
            if (preg_match("#Failed to#i", $line, $re)) {
                $isSuccess = 0;
                continue;
            }
            if (preg_match("#not retranslated#i", $line, $re)) {
                $isSuccess = 0;
                continue;
                $rp_finish = true;
            }
            if (preg_match("#Retranslation operation result 'Success'#i", $line, $re)) {
                $isSuccess = 1;
                continue;
            }
            if ($nextFile == null) {
                continue;
            }
            if (!is_file($nextFile)) {
                continue;
            }
            $size = $size + $unix->file_size($nextFile);
            $files++;
            continue;
        }
        if (!$rp_finish) {
            if ($GLOBALS["VERBOSE"]) {
                echo "Not finished {$timefile}Mn/720Mn {$filename}\n";
            }
            continue;
        }
        $date = date("Y-m-d H:i:s", $time);
        if (preg_match("#UpdateUtility-.*?([0-9]+)\\.log\$#", basename($filename), $re)) {
            $date = date("Y-m-d H:i:s", $re[1]);
        }
        echo "{$date} {$files} downloaded {$size} bytes\n";
        $q = new mysql();
        $details = mysql_escape_string2($details);
        $q->QUERY_SQL("INSERT INTO updateutilityev (`zDate`,`filesize`,`filesnum`,`details`,`isSuccess`) \n\t\t\t\tVALUES ('{$date}','{$files}','{$size}','{$details}','{$isSuccess}')", "artica_events");
        if (!$q->ok) {
            continue;
        }
        $q->QUERY_SQL("DELETE FROM updateutilityev WHERE zDate<DATE_SUB(NOW(),INTERVAL 15 DAY) ORDER BY zDate", "artica_events");
        @unlink($filename);
        UpdateUtilitySize(true);
        continue;
    }
}