function ufdbtables_artica_meta()
{
    $unix = new unix();
    $WORKDIR = $GLOBALS["WORKDIR_LOCAL"];
    @mkdir($WORKDIR, 0755, true);
    @chmod($WORKDIR, 0755);
    $tmpdir = $unix->TEMP_DIR();
    $myVersion = intval(@file_get_contents("/etc/artica-postfix/ufdbartica.txt"));
    $meta = new artica_meta();
    $curl = $meta->buildCurl("/meta-updates/webfiltering/ufdbartica.txt");
    if (!$curl->GetFile("{$tmpdir}/ufdbartica.txt")) {
        artica_update_event(0, "Failed Downloading {$meta->LOG_URI}", @implode("\n", $curl->errors), __FILE__, __LINE__);
        meta_admin_mysql(0, "Failed Downloading {$meta->LOG_URI}", @implode("\n", $curl->errors), __FILE__, __LINE__);
        return false;
    }
    if (!is_file("/etc/artica-postfix/artica-webfilter-db-index.txt")) {
        $curl = $meta->buildCurl("/meta-updates/webfiltering/index.txt");
        if (!$curl->GetFile("/etc/artica-postfix/artica-webfilter-db-index.txt")) {
            artica_update_event(0, "Failed Downloading webfiltering/index.txt", @implode("\n", $curl->errors), __FILE__, __LINE__);
            meta_admin_mysql(0, "Failed Downloading webfiltering/index.txt", @implode("\n", $curl->errors), __FILE__, __LINE__);
        }
    }
    if (!is_file("/etc/artica-postfix/ufdbcounts.txt")) {
        $curl = $meta->buildCurl("/meta-updates/webfiltering/ufdbcounts.txt");
        if (!$curl->GetFile("/etc/artica-postfix/ufdbcounts.txt")) {
            artica_update_event(0, "Failed Downloading webfiltering/ufdbcounts.txt", @implode("\n", $curl->errors), __FILE__, __LINE__);
            meta_admin_mysql(0, "Failed Downloading webfiltering/ufdbcounts.txt", @implode("\n", $curl->errors), __FILE__, __LINE__);
        }
    }
    $Remote_version = intval(@file_get_contents("{$tmpdir}/ufdbartica.txt"));
    echo "Current............: {$myVersion}\n";
    echo "Available..........: {$Remote_version}\n";
    $Remote_versionTime = date("Y-m-d H:i:s", $Remote_version);
    $rm = $unix->find_program("rm");
    $cat = $unix->find_program("cat");
    $tar = $unix->find_program("tar");
    if ($myVersion == $Remote_version) {
        echo "My version {$myVersion} is the same than {$Remote_version} {$Remote_versionTime}, aborting\n";
        return;
    }
    if ($myVersion > $Remote_version) {
        echo "My version {$myVersion} is newest than {$Remote_version} {$Remote_versionTime}, aborting\n";
        return;
    }
    $curl = $meta->buildCurl("/meta-updates/webfiltering/ufdbartica/ufdbartica.txt");
    //***************************************************************************************************************
    if ($curl->GetFile("{$tmpdir}/ufdbartica.txt")) {
        $ufdbartica_tmp = "{$tmpdir}/ufdbartica_tmp";
        @mkdir($ufdbartica_tmp, 0755, true);
        $splitted = unserialize(@file_get_contents("{$tmpdir}/ufdbartica.txt"));
        if (is_array($splitted)) {
            if (count($splitted) > 2) {
                while (list($targetFile, $md5file) = each($ARRAY)) {
                    $BaseName = basename($targetFile);
                    $HTTP_LINK = "/meta-updates/webfiltering/ufdbartica/{$BaseName}";
                    $LOCAL_FILE = "{$ufdbartica_tmp}/{$BaseName}";
                    writelogs_meta("Checking {$LOCAL_FILE}", __FUNCTION__, __FILE__, __LINE__);
                    if (is_file($LOCAL_FILE)) {
                        $md5Local = md5_file($LOCAL_FILE);
                        if ($md5Local == $md5file) {
                            continue;
                        }
                        writelogs_meta("{$LOCAL_FILE} corrupted...", __FUNCTION__, __FILE__, __LINE__);
                        @unlink($LOCAL_FILE);
                    }
                    writelogs_meta("Downloading {$HTTP_LINK}", __FUNCTION__, __FILE__, __LINE__);
                    $curl = $meta->buildCurl($HTTP_LINK);
                    if (!$curl->GetFile($LOCAL_FILE)) {
                        writelogs_meta("Unable to download {$HTTP_LINK} {$curl->error}\n" . @implode("\n", $curl->errors), __FUNCTION__, __FILE__, __LINE__);
                        return true;
                    }
                    $md5Local = md5_file($LOCAL_FILE);
                    if ($md5Local == $md5file) {
                        writelogs_meta("{$HTTP_LINK} success...", __FUNCTION__, __FILE__, __LINE__);
                        continue;
                    }
                }
                system("{$cat} {$ufdbartica_tmp}/*.tgz.* >{$tmpdir}/ufdbartica.tgz");
            }
        }
    }
    //***************************************************************************************************************
    @unlink("{$tmpdir}/ufdbartica.txt");
    if (!is_file("{$tmpdir}/ufdbartica.tgz")) {
        $curl = $meta->buildCurl("/meta-updates/webfiltering/ufdbartica.tgz");
        if (!$curl->GetFile("{$tmpdir}/ufdbartica.tgz")) {
            artica_update_event(0, "Failed Downloading webfiltering/ufdbartica.tgz", @implode("\n", $curl->errors), __FILE__, __LINE__);
            meta_admin_mysql(0, "Failed Downloading webfiltering/ufdbartica.tgz", @implode("\n", $curl->errors), __FILE__, __LINE__);
            @unlink("{$tmpdir}/ufdbartica.tgz");
            return false;
        }
    }
    $curl = $meta->buildCurl("/meta-updates/webfiltering/index.txt");
    if (!$curl->GetFile("/etc/artica-postfix/artica-webfilter-db-index.txt")) {
        artica_update_event(0, "Failed Downloading webfiltering/index.txt", @implode("\n", $curl->errors), __FILE__, __LINE__);
        meta_admin_mysql(0, "Failed Downloading webfiltering/index.txt", @implode("\n", $curl->errors), __FILE__, __LINE__);
    }
    $curl = $meta->buildCurl("/meta-updates/webfiltering/ARTICAUFDB_LAST_DOWNLOAD");
    $curl->GetFile("/etc/artica-postfix/ARTICAUFDB_LAST_DOWNLOAD");
    $STATUS = unserialize(@file_get_contents("/etc/artica-postfix/ARTICAUFDB_LAST_DOWNLOAD"));
    $STATUS["LAST_DOWNLOAD"]["LAST_CHECK"] = time();
    @file_put_contents("/etc/artica-postfix/ARTICAUFDB_LAST_DOWNLOAD", serialize($STATUS));
    if (!$unix->TARGZ_TEST_CONTAINER("{$tmpdir}/ufdbartica.tgz")) {
        artica_update_event(0, "Failed {$tmpdir}/ufdbartica.tgz corrupted package", @implode("\n", $curl->errors), __FILE__, __LINE__);
        meta_admin_mysql(0, "Failed {$tmpdir}/ufdbartica.tgz corrupted package", @implode("\n", $curl->errors), __FILE__, __LINE__);
        @unlink("{$tmpdir}/ufdbartica.tgz");
        return false;
    }
    @file_put_contents("/etc/artica-postfix/ufdbartica.txt", $Remote_version);
    $tar = $unix->find_program("tar");
    $php5 = $unix->LOCATE_PHP5_BIN();
    shell_exec("{$tar} -xf {$tmpdir}/ufdbartica.tgz -C {$WORKDIR}/");
    @unlink("{$tmpdir}/ufdbartica.tgz");
    if (!is_file("/opt/ufdbcat/bin/ufdbcatdd")) {
        system("{$php5} /usr/share/artica-postfix/exec.ufdbcat.php --install --noupdate");
    }
    shell_exec("/etc/init.d/ufdbcat reload");
    artica_update_event(0, "Success Artica Webfiltering databases v.{$Remote_version}", @implode("\n", $curl->errors), __FILE__, __LINE__);
    meta_admin_mysql(0, "Success Artica Webfiltering databases v.{$Remote_version}", @implode("\n", $curl->errors), __FILE__, __LINE__);
    updatev2_progress(100, "{done} [" . __LINE__ . "]");
}
Example #2
0
function install_package($filename, $expected = null)
{
    $unix = new unix();
    $sock = new sockets();
    $php = $unix->LOCATE_PHP5_BIN();
    $nohup = $unix->find_program("nohup");
    $rm = $unix->find_program("rm");
    $RebootAfterArticaUpgrade = $sock->GET_INFO("RebootAfterArticaUpgrade");
    if (!is_numeric($RebootAfterArticaUpgrade)) {
        $RebootAfterArticaUpgrade = 0;
    }
    events("Starting......: " . date("H:i:s") . " install_package() Extracting package {$filename}, please wait... ");
    echo "Starting......: " . date("H:i:s") . " install_package() Extracting package {$filename}, please wait... \n";
    $tarbin = $unix->find_program("tar");
    $killall = $unix->find_program("killall");
    echo "Starting......: " . date("H:i:s") . " tar: {$tarbin}\n";
    echo "Starting......: " . date("H:i:s") . " killall: {$killall}\n";
    @file_put_contents("/usr/share/artica-postfix/download_progress", 10);
    events("Starting......: " . date("H:i:s") . " install_package() Testing Package");
    echo "Starting......: " . date("H:i:s") . " Testing Package " . basename($filename) . "\n";
    if (!$unix->TARGZ_TEST_CONTAINER($filename)) {
        echo "Starting......: " . date("H:i:s") . " Testing Package " . basename($filename) . " failed\n";
        _artica_update_event(0, "Compressed package seems corrupted", null, __FILE__, __LINE__);
        events("Fatal, Compressed package seems corrupted");
        events($GLOBALS["TARGZ_TEST_CONTAINER_ERROR"]);
        @unlink($filename);
        @file_put_contents("/usr/share/artica-postfix/download_progress", 100);
        return false;
    }
    events("Starting......: " . date("H:i:s") . " Purge directories...");
    @file_put_contents("/usr/share/artica-postfix/download_progress", 40);
    system("{$rm} -f /usr/share/artica-postfix/ressources/logs/*");
    system("{$rm} -f /usr/share/artica-postfix/ressources/logs/web/*");
    if (is_dir("/usr/share/artica-postfix/ressources/conf/upload")) {
        system("{$rm} -f /usr/share/artica-postfix/ressources/conf/upload/*");
    }
    if (is_dir("/usr/share/artica-postfix/ressources/conf/meta/hosts/uploaded")) {
        system("{$rm} -f /usr/share/artica-postfix/ressources/conf/meta/hosts/uploaded/*");
    }
    events("Starting......: " . date("H:i:s") . " Extracting...");
    exec("{$tarbin} xf {$filename} -C /usr/share/ 2>&1", $results);
    if (is_file("{$killall}")) {
        shell_exec("{$killall} artica-install >/dev/null 2>&1");
    }
    @unlink($filename);
    shell_exec("{$nohup} {$php} " . dirname(__FILE__) . "/exec.checkfolder-permissions.php --force >/dev/null 2>&1 &");
    $MyCurrentVersion = GetCurrentVersionString();
    if ($expected != null) {
        if ($MyCurrentVersion != $expected) {
            _artica_update_event(1, "install_package(): Expected version:{$expected} does not match {$MyCurrentVersion}", $results, __FILE__, __LINE__);
            return;
        }
    }
    _artica_update_event(2, "install_package(): Success updating to a new version v{$MyCurrentVersion}", $results, __FILE__, __LINE__);
    if ($RebootAfterArticaUpgrade == 1) {
        @file_put_contents("/usr/share/artica-postfix/download_progress", 100);
        _artica_update_event(1, "install_package() Reboot the server in 10s...", null, __FILE__, __LINE__);
        events("Reboot the server in 10s...");
        system_admin_events("Warning: Reboot the server in 10s...", __FUNCTION__, __FILE__, __LINE__, "artica-update");
        $shutdown = $unix->find_program("shutdown");
        shell_exec("shutdown -r -t 10");
        return true;
    }
    _artica_update_event(2, "install_package(): restart dedicated services...", null, __FILE__, __LINE__);
    system_admin_events("Warning: Restart Artica dedicated services after an upgrade...", __FUNCTION__, __FILE__, __LINE__, "artica-update");
    RestartDedicatedServices();
    _artica_update_event(2, "install_package(): finish", null, __FILE__, __LINE__);
    return true;
}
function artica_meta_client($force = false)
{
    $unix = new unix();
    $WORKDIR = "/var/lib/ftpunivtlse1fr";
    @mkdir($WORKDIR, 0755, true);
    @chmod($WORKDIR, 0755);
    $tmpdir = $unix->TEMP_DIR();
    $myVersion = intval(trim(@file_get_contents("/etc/artica-postfix/ftpunivtlse1fr.txt")));
    $tmpdir = $unix->TEMP_DIR();
    $meta = new artica_meta();
    $curl = $meta->buildCurl("/meta-updates/webfiltering/ftpunivtlse1fr.txt");
    if (!$curl->GetFile("{$tmpdir}/ftpunivtlse1fr.txt")) {
        artica_update_event(0, "Failed Downloading webfiltering/ftpunivtlse1fr.txt", @implode("\n", $curl->errors), __FILE__, __LINE__);
        $meta->events($curl->errors, __FUNCTION__, __FILE__, __LINE__);
        meta_admin_mysql(0, "Failed Downloading webfiltering/ftpunivtlse1fr.txt", @implode("\n", $curl->errors), __FILE__, __LINE__);
        return false;
    }
    $Remote_version = intval(trim(@file_get_contents("{$tmpdir}/ftpunivtlse1fr.txt")));
    @unlink("{$tmpdir}/ftpunivtlse1fr.txt");
    echo "Current............: {$myVersion}\n";
    echo "Available..........: {$Remote_version}\n";
    $datev = date("Y-m-d H:i:s", $myVersion);
    $STATUS = unserialize(@file_get_contents("/etc/artica-postfix/TLSE_LAST_DOWNLOAD"));
    $STATUS["LAST_CHECK"] = time();
    @file_put_contents("/etc/artica-postfix/TLSE_LAST_DOWNLOAD", serialize($STATUS));
    if ($myVersion > $Remote_version) {
        echo "My version {$myVersion} is newest than {$Remote_version}, aborting\n";
        build_progress("{version-up-to-date} {$datev}", 100);
        return;
    }
    if ($myVersion == $Remote_version) {
        build_progress("{version-up-to-date} {$datev}", 100);
        echo "My version {$myVersion} is the same than {$Remote_version}, aborting\n";
        return;
    }
    $curl = $meta->buildCurl("/meta-updates/webfiltering/ftpunivtlse1fr.tgz");
    $curl->Timeout = 120;
    if (!$curl->GetFile("{$tmpdir}/ftpunivtlse1fr.tgz")) {
        artica_update_event(0, "Failed Downloading webfiltering/ftpunivtlse1fr.tgz", @implode("\n", $curl->errors), __FILE__, __LINE__);
        $meta->events($curl->errors, __FUNCTION__, __FILE__, __LINE__);
        meta_admin_mysql(0, "Failed Downloading webfiltering/ftpunivtlse1fr.tgz", @implode("\n", $curl->errors), __FILE__, __LINE__);
        @unlink("{$tmpdir}/ftpunivtlse1fr.tgz");
        return false;
    }
    if (!$unix->TARGZ_TEST_CONTAINER("{$tmpdir}/ftpunivtlse1fr.tgz")) {
        artica_update_event(0, "Failed {$tmpdir}/ftpunivtlse1fr.tgz corrupted package", @implode("\n", $curl->errors), __FILE__, __LINE__);
        meta_admin_mysql(0, "Failed {$tmpdir}/ftpunivtlse1fr.tgz corrupted package", @implode("\n", $curl->errors), __FILE__, __LINE__);
        @unlink("{$tmpdir}/ftpunivtlse1fr.tgz");
        return false;
    }
    $tar = $unix->find_program("tar");
    shell_exec("{$tar} -xf {$tmpdir}/ftpunivtlse1fr.tgz -C {$WORKDIR}/");
    @unlink("{$tmpdir}/ftpunivtlse1fr.tgz");
    artica_update_event(0, "Success update categories statistics v.{$Remote_version}", @implode("\n", $curl->errors), __FILE__, __LINE__);
    meta_admin_mysql(0, "Success update categories statistics v.{$Remote_version}", @implode("\n", $curl->errors), __FILE__, __LINE__);
    @file_put_contents("/etc/artica-postfix/ftpunivtlse1fr.txt", $Remote_version);
    build_progress("Using Artica Meta server {done}", 100);
    CoherenceOffiels();
    CoherenceRepertoiresUfdb();
    BuildDatabaseStatus();
    remove_bad_files();
}
function backup_squidlogs($tablename)
{
    $q = new mysql_squid_builder();
    $unix = new unix();
    $mysqldump = $unix->find_program("mysqldump");
    $tar = $unix->find_program("tar");
    $mysqldump_prefix = "{$mysqldump} {$q->MYSQL_CMDLINES} --skip-add-locks --insert-ignore --quote-names --skip-add-drop-table --verbose --force {$q->database} ";
    $container = "/home/artica/squid/backup-statistics/{$tablename}.sql";
    if (is_file($container)) {
        return;
    }
    $cmdline = "{$mysqldump_prefix}{$tablename} >{$container}";
    echo "{$cmdline}\n";
    if ($GLOBALS["VERBOSE"]) {
        echo "\n*******\n{$cmdline}\n*******\n";
    }
    exec($cmdline, $resultsZ);
    if (!$unix->Mysql_TestDump($resultsZ, $container)) {
        stats_admin_events(0, "Fatal Error: day: Dump failed {$tablename}", "", __FILE__, __LINE__);
    }
    $size = @filesize($container);
    @mkdir("/home/artica/squid/backup-statistics", 0755, true);
    chdir("/home/artica/squid/backup-statistics");
    $cmdline = "{$tar} cfz {$container}.tar.gz {$container} 2>&1";
    $resultsZ = array();
    exec($cmdline, $resultsZ);
    if ($GLOBALS["VERBOSE"]) {
        while (list($a, $b) = each($resultsZ)) {
            echo "Compress: `{$b}`\n";
        }
    }
    if (!$unix->TARGZ_TEST_CONTAINER("{$container}.tar.gz")) {
        stats_admin_events(0, "Test container failed: {$container}.tar.gz", "", __FILE__, __LINE__);
        @unlink($container);
        @unlink("{$container}.tar.gz");
        return;
    }
    $size = FormatBytes($size / 1024);
    @unlink($container);
    return true;
}
function purge()
{
    $unix = new unix();
    $pidfile = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".pid";
    $pid = @file_get_contents($pidfile);
    if ($pid < 100) {
        $pid = null;
    }
    if ($unix->process_exists($pid, basename(__FILE__))) {
        $timepid = $unix->PROCCESS_TIME_MIN($pid);
        ufdbguard_admin_events("Already executed pid {$pid} since {$timepid}", __FUNCTION__, __FILE__, __LINE__, "reports");
        if ($GLOBALS["VERBOSE"]) {
            echo "Already executed pid {$pid}\n";
        }
        return;
    }
    @file_put_contents($pidfile, getmypid());
    $sock = new sockets();
    $users = new usersMenus();
    $LICENSE = 0;
    $mysqldump = $unix->find_program("mysqldump");
    $tar = $unix->find_program("tar");
    $EnableSquidRemoteMySQL = $sock->GET_INFO("EnableSquidRemoteMySQL");
    if (!is_numeric($EnableSquidRemoteMySQL)) {
        $EnableSquidRemoteMySQL = 0;
    }
    if ($EnableSquidRemoteMySQL == 1) {
        return;
    }
    if (!is_file($mysqldump)) {
        echo "mysqldump, no such binary\n";
        squid_admin_purge(0, "mysqldump, no such binary", "Backup process cannot be performed");
        ufdbguard_admin_events("mysqldump, no such binary", __FUNCTION__, __FILE__, __LINE__, "backup");
        return;
    }
    if (!is_file($tar)) {
        echo "tar, no such binary\n";
        squid_admin_purge(0, "tar, no such binary", "Backup process cannot be performed");
        ufdbguard_admin_events("tar, no such binary", __FUNCTION__, __FILE__, __LINE__, "backup");
        return;
    }
    $flic = @file_get_contents(base64_decode("L3Vzci9sb2NhbC9zaGFyZS9hcnRpY2EvLmxpYw=="));
    if (preg_match("#TRUE#is", $flic)) {
        $LICENSE = 1;
    }
    $ArticaProxyStatisticsBackupDays = $sock->GET_INFO("ArticaProxyStatisticsBackupDays");
    $ArticaProxyStatisticsBackupFolder = GetMountPoint();
    $BackupSquidStatsUseNas = intval($sock->GET_INFO("BackupSquidStatsUseNas"));
    if ($BackupSquidStatsUseNas == 0) {
        $BackupSquidLogsUseNas = intval($sock->GET_INFO("BackupSquidLogsUseNas"));
        if ($BackupSquidLogsUseNas == 1) {
            $BackupSquidStatsUseNas = 1;
        }
    }
    if ($BackupSquidStatsUseNas == 0) {
        $ArticaProxyStatisticsBackupFolder = $sock->GET_INFO("ArticaProxyStatisticsBackupFolder");
        if ($ArticaProxyStatisticsBackupFolder == null) {
            $ArticaProxyStatisticsBackupFolder = "/home/artica/squid/backup-statistics";
        }
        $percent = $unix->DIRECTORY_USEPERCENT($ArticaProxyStatisticsBackupFolder);
        if ($percent > 90) {
            squid_admin_purge(0, "Fatal backup partition is over 90% {$percent}%, aborting backup", "Directory is :{$ArticaProxyStatisticsBackupFolder}", __FILE__, __LINE__, "backup", null, __FILE__, __LINE__);
            squid_admin_mysql(0, "Fatal backup partition is over 90% {$percent}%, aborting backup", "Directory is :{$ArticaProxyStatisticsBackupFolder}", __FILE__, __LINE__);
            die;
        }
    }
    $ArticaProxyStatisticsBackupFolderORG = $ArticaProxyStatisticsBackupFolder;
    if (!is_numeric($ArticaProxyStatisticsBackupDays)) {
        $ArticaProxyStatisticsBackupDays = 90;
    }
    if ($GLOBALS["MAXDAYS"] > 0) {
        $ArticaProxyStatisticsBackupDays = $GLOBALS["MAXDAYS"];
    }
    if ($LICENSE == 0) {
        $ArticaProxyStatisticsBackupDays = 5;
    }
    if (!ScanDays()) {
        if ($GLOBALS["VERBOSE"]) {
            squid_admin_purge(2, "ScanDay() report failed", __FILE__, __LINE__);
            echo "Failed...\n";
        }
        KillMountPoint();
        return;
    }
    if ($GLOBALS["VERBOSE"]) {
        "Max Day: {$ArticaProxyStatisticsBackupDays}; folder:{$ArticaProxyStatisticsBackupFolder}\n";
    }
    $q = new mysql_squid_builder(true);
    $sql = "SELECT tablename,zDate FROM tables_day WHERE zDate<DATE_SUB(NOW(),INTERVAL {$ArticaProxyStatisticsBackupDays} DAY)";
    $results = $q->QUERY_SQL($sql);
    if (!$q->ok) {
        squid_admin_purge(0, "Fatal Error: {$q->mysql_error}", __FILE__, __LINE__, "backup");
        return;
    }
    ufdbguard_admin_events("Items: " . mysql_num_rows($results), __FUNCTION__, __FILE__, __LINE__, "backup");
    if ($GLOBALS["VERBOSE"]) {
        echo $sql . " => " . mysql_num_rows($results) . "\n";
    }
    squid_admin_purge(2, "Day retentions are: {$ArticaProxyStatisticsBackupDays} Days - " . mysql_num_rows($results) . " tables to purge", __FILE__, __LINE__);
    @mkdir("{$ArticaProxyStatisticsBackupFolder}", 0755, true);
    if (!is_dir($ArticaProxyStatisticsBackupFolder)) {
        squid_admin_purge(0, "Fatal {$ArticaProxyStatisticsBackupFolder} permission denied", "Backup process cannot be performed", __FILE__, __LINE__);
        if ($GLOBALS["VERBOSE"]) {
            echo "{$ArticaProxyStatisticsBackupFolder} permission denied\n";
        }
        ufdbguard_admin_events("Fatal Error: {$ArticaProxyStatisticsBackupFolder} permission denied", __FUNCTION__, __FILE__, __LINE__, "backup");
        KillMountPoint();
        return;
    }
    $t = time();
    if (!@file_put_contents("{$ArticaProxyStatisticsBackupFolder}/{$t}", time())) {
        squid_admin_purge(0, "Fatal {$ArticaProxyStatisticsBackupFolder}  write error", "Backup process cannot be performed", __FILE__, __LINE__);
        if ($GLOBALS["VERBOSE"]) {
            echo "{$ArticaProxyStatisticsBackupFolder} write error\n";
        }
        ufdbguard_admin_events("Fatal Error: {$ArticaProxyStatisticsBackupFolder} write error..", __FUNCTION__, __FILE__, __LINE__, "backup");
        KillMountPoint();
        return;
    }
    if (!is_file("{$ArticaProxyStatisticsBackupFolder}/{$t}")) {
        squid_admin_purge(0, "Fatal {$ArticaProxyStatisticsBackupFolder} permission denied", "Backup process cannot be performed", __FILE__, __LINE__);
        if ($GLOBALS["VERBOSE"]) {
            echo "{$ArticaProxyStatisticsBackupFolder} permission denied\n";
        }
        ufdbguard_admin_events("Fatal Error: {$ArticaProxyStatisticsBackupFolder} permission denied", __FUNCTION__, __FILE__, __LINE__, "backup");
        KillMountPoint();
        return;
    }
    @unlink("{$ArticaProxyStatisticsBackupFolder}/{$t}");
    $DeleteTables = 0;
    $TotalSize = 0;
    $mysqldump_prefix = "{$mysqldump} {$q->MYSQL_CMDLINES} --skip-add-locks --insert-ignore --quote-names --skip-add-drop-table --verbose --force {$q->database} ";
    while ($ligne = @mysql_fetch_array($results, MYSQL_ASSOC)) {
        $tablename = $ligne["tablename"];
        $TableKey = $tablename;
        $day = $ligne["zDate"];
        $DayTime = strtotime("{$day} 00:00:00");
        echo "To backup {$tablename} ({$day})\n";
        $container = "{$ArticaProxyStatisticsBackupFolder}/squidlogs.{$day}." . time() . ".sql";
        if (is_file($container)) {
            sleep(1);
        }
        $container = "{$ArticaProxyStatisticsBackupFolder}/squidlogs.{$day}." . time() . ".sql";
        if (!@file_put_contents($container, time())) {
            if ($GLOBALS["VERBOSE"]) {
                echo "{$container} permission denied\n";
            }
            squid_admin_purge(0, "Fatal Error: {$container} permission denied", "Backup process cannot be performed", __FILE__, __LINE__);
            ufdbguard_admin_events("Fatal Error: {$container} permission denied", __FUNCTION__, __FILE__, __LINE__, "backup");
            KillMountPoint();
            return;
        }
        @unlink($container);
        $tablesB = array();
        if ($q->TABLE_EXISTS($tablename)) {
            $tablesB[$tablename] = true;
        } else {
            if ($GLOBALS["VERBOSE"]) {
                echo "{$tablename} no such table, continue\n";
            }
        }
        $tableTMP = date("Ymd", $DayTime) . "_hour";
        if ($q->TABLE_EXISTS($tableTMP)) {
            $tablesB[$tableTMP] = true;
        } else {
            if ($GLOBALS["VERBOSE"]) {
                echo "{$tableTMP} no such table, continue\n";
            }
        }
        $tableTMP = date("Ymd", $DayTime) . "_members";
        if ($q->TABLE_EXISTS($tableTMP)) {
            $tablesB[$tableTMP] = true;
        } else {
            if ($GLOBALS["VERBOSE"]) {
                echo "{$tableTMP} no such table, continue\n";
            }
        }
        $tableTMP = date("Ymd", $DayTime) . "_visited";
        if ($q->TABLE_EXISTS($tableTMP)) {
            $tablesB[$tableTMP] = true;
        } else {
            if ($GLOBALS["VERBOSE"]) {
                echo "{$tableTMP} no such table, continue\n";
            }
        }
        $tableTMP = date("Ymd", $DayTime) . "_blocked";
        if ($q->TABLE_EXISTS($tableTMP)) {
            $tablesB[$tableTMP] = true;
        } else {
            if ($GLOBALS["VERBOSE"]) {
                echo "{$tableTMP} no such table, continue\n";
            }
        }
        $tableTMP = "searchwordsD_" . date("Ymd", $DayTime) . "";
        if ($q->TABLE_EXISTS($tableTMP)) {
            $tablesB[$tableTMP] = true;
        } else {
            if ($GLOBALS["VERBOSE"]) {
                echo "{$tableTMP} no such table, continue\n";
            }
        }
        $tableTMP = "UserSizeD_" . date("Ymd", $DayTime) . "";
        if ($q->TABLE_EXISTS($tableTMP)) {
            $tablesB[$tableTMP] = true;
        } else {
            if ($GLOBALS["VERBOSE"]) {
                echo "{$tableTMP} no such table, continue\n";
            }
        }
        $tableTMP = "youtubeday_" . date("Ymd", $DayTime) . "";
        if ($q->TABLE_EXISTS($tableTMP)) {
            $tablesB[$tableTMP] = true;
        } else {
            if ($GLOBALS["VERBOSE"]) {
                echo "{$tableTMP} no such table, continue\n";
            }
        }
        $tableTMP = "quotaday_" . date("Ymd", $DayTime) . "";
        if ($q->TABLE_EXISTS($tableTMP)) {
            $tablesB[$tableTMP] = true;
        } else {
            if ($GLOBALS["VERBOSE"]) {
                echo "{$tableTMP} no such table, continue\n";
            }
        }
        $tableTMP = date("Ymd", $DayTime) . "_catfam";
        if ($q->TABLE_EXISTS($tableTMP)) {
            $tablesB[$tableTMP] = true;
        } else {
            if ($GLOBALS["VERBOSE"]) {
                echo "{$tableTMP} no such table, continue\n";
            }
        }
        $c = array();
        while (list($a, $b) = each($tablesB)) {
            $c[] = $a;
        }
        reset($tablesB);
        echo "Backup tables: " . @implode(", ", $c) . "\n";
        if (count($tablesB) > 0) {
            $cmdline = "{$mysqldump_prefix}" . @implode(" ", $c) . " >{$container}";
            if ($GLOBALS["VERBOSE"]) {
                echo "\n*******\n{$cmdline}\n*******\n";
            }
            $resultsZ = array();
            exec($cmdline, $resultsZ);
            if (!TestDump($resultsZ, $container)) {
                ufdbguard_admin_events("Fatal Error: day: Dump failed {$day}", __FUNCTION__, __FILE__, __LINE__, "backup");
                KillMountPoint();
                return;
            }
            $size = @filesize($container);
            chdir($ArticaProxyStatisticsBackupFolder);
            $cmdline = "{$tar} cfz {$container}.tar.gz {$container} 2>&1";
            $resultsZ = array();
            exec($cmdline, $resultsZ);
            while (list($a, $b) = each($resultsZ)) {
                echo "Compress: `{$b}`\n";
            }
            if (!$unix->TARGZ_TEST_CONTAINER("{$container}.tar.gz")) {
                squid_admin_purge(0, "Error {$container} failed", __FILE__, __LINE__);
                ufdbguard_admin_events("Fatal Error: tar {$container} failed", __FUNCTION__, __FILE__, __LINE__, "backup");
                @unlink($container);
                @unlink("{$container}.tar.gz");
                KillMountPoint();
                return;
            }
            $TotalSize = $TotalSize + $size;
            @unlink($container);
            reset($tablesB);
            while (list($tablename, $line) = each($tablesB)) {
                if ($GLOBALS["VERBOSE"]) {
                    echo "Delete table `{$tablename}`\n";
                }
                if (!$q->DELETE_TABLE($tablename)) {
                    if ($GLOBALS["VERBOSE"]) {
                        echo "Delete {$tablename} failed {$q->mysql_error} ...\n";
                    }
                    ufdbguard_admin_events("Fatal Error: Delete {$tablename} failed {$q->mysql_error} ", __FUNCTION__, __FILE__, __LINE__, "backup");
                    KillMountPoint();
                    return;
                }
                $DeleteTables++;
            }
        }
        if ($GLOBALS["VERBOSE"]) {
            echo "Delete table `{$TableKey}` from tables_day\n";
        }
        $q->QUERY_SQL("DELETE FROM tables_day WHERE tablename='{$TableKey}'");
    }
    $container = "{$ArticaProxyStatisticsBackupFolder}/squidlogs.FULL.sql";
    $resultsZ = array();
    $cmd = "{$mysqldump_prefix} >{$container}";
    exec($cmd, $resultsZ);
    chdir($ArticaProxyStatisticsBackupFolder);
    $cmdline = "{$tar} cfz {$container}.tar.gz {$container} 2>&1";
    exec($cmdline);
    if (!$unix->TARGZ_TEST_CONTAINER("{$container}.tar.gz")) {
        squid_admin_purge(0, "Error {$container}.tar.gz, not a valid compressed file", __FILE__, __LINE__);
        ufdbguard_admin_events("Error {$container}.tar.gz, not a valid compressed file", __FUNCTION__, __FILE__, __LINE__, "backup");
        @unlink("{$container}.tar.gz");
    } else {
        $size = @filesize($container);
        $TotalSize = $TotalSize + $size;
        @unlink("{$container}");
    }
    if ($DeleteTables > 0) {
        $TotalSize = FormatBytes($TotalSize / 1024);
        $took = $unix->distanceOfTimeInWords($t, time(), true);
        squid_admin_purge(2, "Success backup and purge {$DeleteTables} table(s) ({$TotalSize})", "took:{$took}", __FILE__, __LINE__);
        ufdbguard_admin_events("Success backup and purge {$DeleteTables} table(s) ({$TotalSize}) took:{$took}", __FUNCTION__, __FILE__, __LINE__, "backup");
    }
    CleanQuotas();
    KillMountPoint();
}
function artica_updates_scheduled($aspid = false)
{
    $unix = new unix();
    $pidfile = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".pid";
    $pidTime = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".time";
    if (!$aspid) {
        $pid = @file_get_contents($pidfile);
        if ($unix->process_exists($pid, basename(__FILE__))) {
            $time = $unix->PROCCESS_TIME_MIN($pid);
            echo "Starting......: " . date("H:i:s") . " artica_updates_scheduled already executed PID: {$pid} since {$time}Mn\n";
            if ($time < 120) {
                if (!$GLOBALS["FORCE"]) {
                    die;
                }
            }
            unix_system_kill_force($pid);
        }
    } else {
        $pidTimeEx = $unix->file_time_min($pidTime);
        if ($pidTimeEx < 61) {
            events("Skip Artica updates, current {$pidTimeEx}Mn, require 60mn ", __FUNCTION__, __FILE__, __LINE__);
            return;
        }
    }
    @chdir("/root");
    @file_put_contents($pidfile, getmypid());
    @unlink($pidTime);
    @file_put_contents($pidTime, time());
    $tmpdir = $unix->TEMP_DIR();
    $meta = new artica_meta();
    $ARRAY = array();
    $sock = new sockets();
    $MetaClientAutoUpdate = intval($sock->GET_INFO("MetaClientAutoUpdate"));
    $ini = new iniFrameWork();
    $ini->loadFile('/etc/artica-postfix/artica-update.conf');
    $nightly = trim(strtolower($ini->_params["AUTOUPDATE"]["nightlybuild"]));
    if ($nightly == 1) {
        $nightly = "yes";
    }
    if ($GLOBALS["VERBOSE"]) {
        echo "Update nightly ? `{$nightly}`\n";
    }
    $curl = $meta->buildCurl("/meta-updates/releases/");
    if (!$curl->GetFile("{$tmpdir}/releases.txt")) {
        events("Fatal: /meta-updates/releases/releases.txt {$curl->errors}", __FUNCTION__, __FILE__, __LINE__);
        artica_update_event(0, "Failed Downloading /meta-updates/releases/", @implode("\n", $curl->errors), __FILE__, __LINE__);
        meta_admin_mysql(0, "Failed Downloading /meta-updates/releases/", @implode("\n", $curl->errors), __FILE__, __LINE__);
        return false;
    }
    $data = @file_get_contents("{$tmpdir}/releases.txt");
    $f = explode("\n", $data);
    @unlink("{$tmpdir}/releases.txt");
    while (list($index, $ligne) = each($f)) {
        if (!preg_match("#href.*?artica-([0-9\\.]+)\\.tgz#", $ligne, $re)) {
            if ($GLOBALS["VERBOSE"]) {
                echo "Not Found {$ligne}\n";
            }
            continue;
        }
        $verbin = intval(str_replace(".", "", $re[1]));
        events("Found : v{$verbin} /meta-updates/releases/artica-{$re[1]}.tgz", __FUNCTION__, __FILE__, __LINE__);
        $ARRAY[$verbin] = "/meta-updates/releases/artica-{$re[1]}.tgz";
    }
    if ($MetaClientAutoUpdate == 1) {
        $nightly = "yes";
    }
    if ($nightly == "yes") {
        $curl = $meta->buildCurl("/meta-updates/nightlys/");
        if (!$curl->GetFile("{$tmpdir}/nightlys.txt")) {
            artica_update_event(0, "Failed Downloading /meta-updates/nightlys/", @implode("\n", $curl->errors), __FILE__, __LINE__);
            meta_admin_mysql(0, "Failed Downloading /meta-updates/nightlys/", @implode("\n", $curl->errors), __FILE__, __LINE__);
        } else {
            $data = @file_get_contents("{$tmpdir}/nightlys.txt");
            $f = explode("\n", $data);
            @unlink("{$tmpdir}/nightlys.txt");
            while (list($index, $ligne) = each($f)) {
                if (!preg_match("#href.*?artica-([0-9\\.]+)\\.tgz#", $ligne, $re)) {
                    if ($GLOBALS["VERBOSE"]) {
                        echo "Not Found {$ligne}\n";
                    }
                    continue;
                }
                $verbin = intval(str_replace(".", "", $re[1]));
                events("Found : v{$verbin} /meta-updates/nightlys/artica-{$re[1]}.tgz", __FUNCTION__, __FILE__, __LINE__);
                $ARRAY[$verbin] = "/meta-updates/nightlys/artica-{$re[1]}.tgz";
            }
        }
    }
    krsort($ARRAY);
    while (list($index, $ligne) = each($ARRAY)) {
        if ($GLOBALS["VERBOSE"]) {
            echo "Found v.{$index} / {$ligne}\n";
        }
        $TA[] = $index;
        $TZ[] = $ligne;
    }
    $LASTVER = $TA[0];
    $LASTFILE = $TZ[0];
    $MYVERSION_TEXT = trim(@file_get_contents("/usr/share/artica-postfix/VERSION"));
    $MYVERSION = intval(str_replace(".", "", $MYVERSION_TEXT));
    events("Last version: {$LASTVER} -> {$LASTFILE} against {$MYVERSION}", __FUNCTION__, __FILE__, __LINE__);
    if ($MYVERSION > $LASTVER) {
        events("Most updated, aborting", __FUNCTION__, __FILE__, __LINE__);
        if ($GLOBALS["VERBOSE"]) {
            echo "Most updated, aborting\n";
        }
        return;
    }
    if ($MYVERSION == $LASTVER) {
        events("Most updated, aborting", __FUNCTION__, __FILE__, __LINE__);
        if ($GLOBALS["VERBOSE"]) {
            echo "Most updated, aborting\n";
        }
        return;
    }
    $curl = $meta->buildCurl($LASTFILE);
    $tmpfile = "{$tmpdir}/" . basename($LASTFILE);
    if (!$curl->GetFile($tmpfile)) {
        events("Failed Downloading {$LASTFILE} {$curl->errors}", __FUNCTION__, __FILE__, __LINE__);
        artica_update_event(0, "Failed Downloading {$LASTFILE}", @implode("\n", $curl->errors), __FILE__, __LINE__);
        meta_admin_mysql(0, "Failed Downloading {$LASTFILE}", @implode("\n", $curl->errors), __FILE__, __LINE__);
        @unlink($tmpfile);
        return;
    }
    if (!$unix->TARGZ_TEST_CONTAINER($tmpfile)) {
        artica_update_event(0, "Failed {$tmpfile} corrupted package", @implode("\n", $curl->errors), __FILE__, __LINE__);
        meta_admin_mysql(0, "Failed {$tmpfile} corrupted package", @implode("\n", $curl->errors), __FILE__, __LINE__);
        @unlink($tmpfile);
        return false;
    }
    if ($GLOBALS["VERBOSE"]) {
        echo "Extracting {$tmpfile}\n";
    }
    $tar = $unix->find_program("tar");
    shell_exec("{$tar} xf {$tmpfile} -C /usr/share/");
    @unlink($tmpfile);
    $MYVERSION_TEXT = trim(@file_get_contents("/usr/share/artica-postfix/VERSION"));
    $php = $unix->LOCATE_PHP5_BIN();
    shell_exec("{$php} /usr/share/artica-postfix/exec.nightly.php --restart-services >/dev/null 2>&1");
    artica_update_event(2, "Success update Artica to {$MYVERSION_TEXT}", null, __FILE__, __LINE__);
    meta_admin_mysql(2, "Success update Artica to {$MYVERSION_TEXT}", null, __FILE__, __LINE__);
}
Example #7
0
function install_package($filename, $expected = null)
{
    $unix = new unix();
    $sock = new sockets();
    $php = $unix->LOCATE_PHP5_BIN();
    $nohup = $unix->find_program("nohup");
    $rm = $unix->find_program("rm");
    $squidbin = $unix->LOCATE_SQUID_BIN();
    $RebootAfterArticaUpgrade = $sock->GET_INFO("RebootAfterArticaUpgrade");
    if (!is_numeric($RebootAfterArticaUpgrade)) {
        $RebootAfterArticaUpgrade = 0;
    }
    events("Starting......: " . date("H:i:s") . " install_package() Extracting package {$filename}, please wait... ");
    echo "Starting......: " . date("H:i:s") . " install_package() Extracting package {$filename}, please wait... \n";
    $tarbin = $unix->find_program("tar");
    $killall = $unix->find_program("killall");
    echo "Starting......: " . date("H:i:s") . " tar: {$tarbin}\n";
    echo "Starting......: " . date("H:i:s") . " killall: {$killall}\n";
    build_progress_manu("Testing Package", 50);
    events("Starting......: " . date("H:i:s") . " install_package() Testing Package");
    echo "Starting......: " . date("H:i:s") . " Testing Package " . basename($filename) . "\n";
    ini_set('display_errors', 1);
    ini_set('error_reporting', E_ALL);
    ini_set('error_prepend_string', " WARNING!!:");
    ini_set('error_append_string', "\n");
    echo "Starting......: " . date("H:i:s") . " Testing Package Please wait....\n";
    if (!$unix->TARGZ_TEST_CONTAINER($filename, false, true)) {
        echo "Starting......: " . date("H:i:s") . " Testing Package " . basename($filename) . " failed\n";
        _artica_update_event(0, "Compressed package seems corrupted", null, __FILE__, __LINE__);
        events("Fatal, Compressed package seems corrupted");
        events($GLOBALS["TARGZ_TEST_CONTAINER_ERROR"]);
        @unlink($filename);
        build_progress_manu("Corrupted!", 110);
        return false;
    }
    echo "Starting......: " . date("H:i:s") . " Purge directories\n";
    events("Starting......: " . date("H:i:s") . " Purge directories...");
    build_progress_manu("Purge directories", 55);
    if (is_dir("/usr/share/artica-postfix/ressources/conf/upload")) {
        system("{$rm} -f /usr/share/artica-postfix/ressources/conf/upload/*");
    }
    if (is_dir("/usr/share/artica-postfix/ressources/conf/meta/hosts/uploaded")) {
        system("{$rm} -f /usr/share/artica-postfix/ressources/conf/meta/hosts/uploaded/*");
    }
    events("Starting......: " . date("H:i:s") . " Extracting...");
    exec("{$tarbin} xpf {$filename} -C /usr/share/ 2>&1", $results);
    if (is_file($squidbin)) {
        $squidfiles = $unix->SquidPHPFiles();
        while (list($fileSquid, $None123) = each($squidfiles)) {
            @chgrp("/usr/share/artica-postfix/{$fileSquid}", "squid");
            @chown("/usr/share/artica-postfix/{$fileSquid}", "squid");
            @chmod("/usr/share/artica-postfix/{$fileSquid}", 0755);
        }
    }
    if (is_file("{$killall}")) {
        shell_exec("{$killall} artica-install >/dev/null 2>&1");
    }
    @unlink($filename);
    shell_exec("{$nohup} {$php} " . dirname(__FILE__) . "/exec.checkfolder-permissions.php --force >/dev/null 2>&1 &");
    $MyCurrentVersion = GetCurrentVersionString();
    if ($expected != null) {
        if ($MyCurrentVersion != $expected) {
            _artica_update_event(1, "install_package(): Expected version:{$expected} does not match {$MyCurrentVersion}", $results, __FILE__, __LINE__);
            return;
        }
    }
    _artica_update_event(2, "install_package(): Success updating to a new version v{$MyCurrentVersion}", $results, __FILE__, __LINE__);
    if ($RebootAfterArticaUpgrade == 1) {
        build_progress_manu("{rebooting}", 90);
        _artica_update_event(1, "install_package() Reboot the server in 10s...", null, __FILE__, __LINE__);
        events("Reboot the server in 10s...");
        system_admin_events("Warning: Reboot the server in 10s...", __FUNCTION__, __FILE__, __LINE__, "artica-update");
        $shutdown = $unix->find_program("shutdown");
        shell_exec("shutdown -r -t 10");
        sleep(5);
        build_progress_manu("{done}", 100);
        return true;
    }
    _artica_update_event(2, "install_package(): restart dedicated services...", null, __FILE__, __LINE__);
    system_admin_events("Warning: Restart Artica dedicated services after an upgrade...", __FUNCTION__, __FILE__, __LINE__, "artica-update");
    system("{$php} " . __FILE__ . " --restart-services");
    _artica_update_event(2, "install_package(): finish", null, __FILE__, __LINE__);
    build_progress_manu("{done}", 100);
    return true;
}