示例#1
0
function restart_lighttpd()
{
    $t = time();
    exec("/etc/init.d/artica-webinterface restart 2>&1", $results);
    $unix = new unix();
    $took = $unix->distanceOfTimeInWords($t, time());
    system_admin_events("Restart Web interface service done took:{$took}\n" . @implode("\n", $results), __FUNCTION__, __FILE__, __LINE__, "system");
}
示例#2
0
function start()
{
    $pidtime = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".time";
    $pidFile = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".pid";
    $unix = new unix();
    $pid = $unix->get_pid_from_file($pidFile);
    if ($unix->process_exists($pid)) {
        return;
    }
    @file_put_contents($pidFile, getmypid());
    $time = $unix->file_time_min($pidtime);
    if ($time < 5) {
        return;
    }
    @file_put_contents($pidtime, time());
    $free = $unix->find_program("free");
    $echo = $unix->find_program("echo");
    $sync = $unix->find_program("sync");
    $swapoff = $unix->find_program("swapoff");
    $swapon = $unix->find_program("swapon");
    exec("{$free} 2>&1", $results);
    $used = 0;
    $total = 0;
    while (list($num, $ligne) = each($results)) {
        if (preg_match("#Swap:\\s+([0-9]+)\\s+([0-9]+)\\s+([0-9]+)#", $ligne, $re)) {
            $total = $re[1];
            $used = $re[2];
        }
    }
    if (!is_numeric($total)) {
        return;
    }
    if ($total == 0) {
        return;
    }
    if ($used == 0) {
        return;
    }
    if ($total == $used) {
        return;
    }
    $tot1 = $used / $total;
    $tot1 = $tot1 * 100;
    if ($GLOBALS["VERBOSE"]) {
        echo "Swap:{$used}/{$total} - {$tot1}\n";
    }
    $perc = round($tot1);
    if ($GLOBALS["VERBOSE"]) {
        echo "Swap:{$used}/{$total} {$perc}%\n";
    }
    $t = time();
    $GLOBALS["ALL_SCORES"]++;
    shell_exec("{$swapoff} -a && {$swapon} -a");
    $usedTXT = FormatBytes($used);
    $report = $unix->ps_mem_report();
    $distance = $unix->distanceOfTimeInWords($t, time(), true);
    squid_admin_mysql(0, "System swap exceed rule: {$perc}%", "Used {$usedTXT}\nSystem cache was flushed took {$distance}\nThis means you did have enough memory for this computer.\n{$report}", __FILE__, __LINE__);
}
示例#3
0
function mailboxes_ou_lang($ou)
{
    $unix = new unix();
    $sock = new sockets();
    $t = time();
    $pidfile = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".pid";
    $unix = new unix();
    $me = basename(__FILE__);
    if ($unix->process_exists(@file_get_contents($pidfile), $me)) {
        if ($GLOBALS["VERBOSE"]) {
            echo " --> Already executed.. " . @file_get_contents($pidfile) . " aborting the process\n";
        }
        system_admin_events("--> Already executed.. " . @file_get_contents($pidfile) . " aborting the process", __FUNCTION__, __FILE__, __LINE__, "zarafa");
        die;
    }
    @file_put_contents($pidfile, getmypid());
    $oumd5 = md5(strtolower(trim($ou)));
    $OuDefaultLang = $sock->GET_INFO("zarafaMBXLang{$oumd5}");
    if ($OuDefaultLang == null) {
        system_admin_events("`{$ou}` no such default language, aborting", __FUNCTION__, __FILE__, __LINE__, "zarafa");
        return;
    }
    $ldap = new clladp();
    $members = $ldap->hash_users_ou($ou);
    $CountMembers = count($members);
    system_admin_events("{$ou} {$CountMembers} to change to {$OuDefaultLang}", __FUNCTION__, __FILE__, __LINE__, "zarafa");
    $c = 0;
    while (list($uid, $name) = each($members)) {
        $ct = new user($uid);
        if ($ct->zarafaMbxLang == null) {
            $ct->SaveZarafaMbxLang($OuDefaultLang);
            $c++;
            $sock->getFrameWork("cmd.php?zarafa-admin=yes");
            $sock->getFrameWork("zarafa.php?zarafa-user-create-store={$uid}&lang={$OuDefaultLang}");
            $sock->getFrameWork("zarafa.php?foldersnames=yes&uid={$uid}&lang={$OuDefaultLang}");
        }
    }
    $took = $unix->distanceOfTimeInWords($t, time(), true);
    system_admin_events("{$ou} {$c}/{$CountMembers} changed to {$OuDefaultLang} done took: {$took}", __FUNCTION__, __FILE__, __LINE__, "zarafa");
}
function Compile()
{
    $unix = new unix();
    $pidfile = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".pid";
    $pid = @file_get_contents($pidfile);
    if ($unix->process_exists($pid, basename(__FILE__))) {
        if ($GLOBALS["VERBOSE"]) {
            echo "Already executed pid {$pid}\n";
        }
        return;
    }
    $t = time();
    $q = new mysql_squid_builder();
    echo "**** LIST_TABLES_CATEGORIES_PERSO *****\n";
    $tablescat = $q->LIST_TABLES_CATEGORIES_PERSO();
    $source_dir = "/home/artica/categories_perso";
    if (count($tablescat) == 0) {
        echo "tablescat = 0\n";
        return;
    }
    $i = 0;
    while (list($tablename, $ligne) = each($tablescat)) {
        if (preg_match("#^categoryuris#", $tablename)) {
            continue;
        }
        $COUNT_ROWS = $q->COUNT_ROWS($tablename);
        if ($COUNT_ROWS == 0) {
            continue;
        }
        echo " **** {$tablename} {$COUNT_ROWS} ITEMS *****\n";
        $Dir = "{$source_dir}/{$tablename}";
        @mkdir("{$Dir}", 0777, true);
        echo "{$tablename}: Building {$Dir}/domains";
        @unlink("{$Dir}/domains");
        @chmod("{$Dir}/domains", 0777);
        $sql = "SELECT pattern FROM {$tablename} WHERE enabled=1 ORDER BY pattern INTO OUTFILE '{$Dir}/domains' LINES TERMINATED BY '\n';";
        $q->QUERY_SQL($sql);
        if (!$q->ok) {
            echo "{$tablename}: {$q->mysql_error}\n";
            continue;
        }
        $handle = @fopen("{$Dir}/domains", "r");
        if (!$handle) {
            echo "Failed to open file {$Dir}/domains\n";
            continue;
        }
        $DestDB = "{$Dir}/domains.db";
        @unlink($DestDB);
        $db_desttmp = dba_open($DestDB, "n", "db4");
        if (!$db_desttmp) {
            echo "Unable to Create {$DestDB}\n";
            continue;
        }
        dba_close($db_desttmp);
        $db_dest = dba_open($DestDB, "w", "db4");
        @chmod($DestDB, 0777);
        if (!$db_dest) {
            echo "Unable to open for `writing` \"{$DestDB}\"\n";
            continue;
        }
        while (!feof($handle)) {
            $www = trim(fgets($handle, 4096));
            $www = trim(str_replace('"', "", $www));
            if ($www == null) {
                continue;
            }
            $www = strtolower($www);
            if (!dba_insert("{$www}", "yes", $db_dest)) {
                echo "dba_insert({$www},yes... false\n";
                continue;
            }
        }
        $i++;
        dba_close($db_dest);
        @fclose($handle);
        @unlink("{$Dir}/domains");
    }
    stats_admin_events(2, "1%) {$i} Personal tables compiled took:" . $unix->distanceOfTimeInWords($t, time()), null, __FILE__, __LINE__);
}
示例#5
0
function ActiveDirectoryToMysql()
{
    $sock = new sockets();
    $EnableManageUsersTroughActiveDirectory = $sock->GET_INFO("EnableManageUsersTroughActiveDirectory");
    if (!is_numeric($EnableManageUsersTroughActiveDirectory)) {
        $EnableManageUsersTroughActiveDirectory = 0;
    }
    if ($EnableManageUsersTroughActiveDirectory == 0) {
        die;
    }
    $unix = new unix();
    $pidfile = "/etc/artica-postfix/pids/" . basename(__FILE__) . ".pid";
    $pidTime = "/etc/artica-postfix/pids/" . basename(__FILE__) . ".time";
    $oldpid = $unix->get_pid_from_file($pidfile);
    $sock = new sockets();
    $ActiveDirectoryMysqlSinc = $sock->GET_INFO($ActiveDirectoryMysqlSinc);
    if (!is_numeric($ActiveDirectoryMysqlSinc)) {
        $ActiveDirectoryMysqlSinc = 5;
    }
    if ($ActiveDirectoryMysqlSinc == 0) {
        $ActiveDirectoryMysqlSinc = 1;
    }
    $ActiveDirectoryMysqlSinc = $ActiveDirectoryMysqlSinc * 60;
    if ($unix->process_exists($oldpid, basename(__FILE__))) {
        writelogs("Process {$oldpid} already exists", __FUNCTION__, __FILE__, __LINE__);
        return;
    }
    if (system_is_overloaded(basename(__FILE__))) {
        writelogs("Overloaded system, aborting", __FUNCTION__, __FILE__, __LINE__);
        return;
    }
    @file_put_contents($pidfile, getmypid());
    if (!$GLOBALS["FORCE"]) {
        if ($unix->file_time_min($pidTime) < $ActiveDirectoryMysqlSinc) {
            return;
        }
    }
    @unlink($pidTime);
    @file_put_contents($pidTime, time());
    $t1 = time();
    $ldap = new ldapAD();
    $hash = $ldap->Ldap_search($ldap->suffix, "(objectClass=organizationalUnit)", array("name", "ou", "dn"), 5000);
    if (!is_numeric($hash["count"])) {
        $hash["count"] = 0;
    }
    if ($hash["count"] == 0) {
        return;
    }
    $q = new mysql();
    $q->QUERY_SQL("TRUNCATE TABLE `activedirectory_users`", "artica_backup");
    if (!$q->ok) {
        $unix->send_email_events("ActiveDirectory: mysql error {$q->mysql_error}", "process aborted. Will restart in next cycle", "system");
        return;
    }
    $q->QUERY_SQL("TRUNCATE TABLE `activedirectory_groups`", "artica_backup");
    $q->QUERY_SQL("TRUNCATE TABLE `activedirectory_groupsNames`", "artica_backup");
    $sql = "SELECT ou,dn,enabled,OnlyBranch FROM activedirectory_orgs ORDER BY ou";
    $results = $q->QUERY_SQL($sql, "artica_backup");
    $BranchsInMyql = mysql_num_rows($results);
    while ($ligne = @mysql_fetch_array($results, MYSQL_ASSOC)) {
        $OUCONFIG[$ligne["dn"]]["PARAMS"]["ENABLED"] = $ligne["enabled"];
        $OUCONFIG[$ligne["dn"]]["PARAMS"]["OnlyBranch"] = $ligne["OnlyBranch"];
    }
    $GLOBALS["MEMORY_COUNT_USERS"] = 0;
    $GLOBALS["MEMORY_COUNT_GROUPS"] = 0;
    for ($i = 0; $i < $hash["count"]; $i++) {
        $OrganizationDN = utf8_encode($hash[$i]["dn"]);
        if (isset($OUCONFIG[$OrganizationDN])) {
            if ($OUCONFIG[$OrganizationDN]["PARAMS"]["ENABLED"] == 0) {
                echo "Importing users from {$hash[$i]["ou"][0]} {$OrganizationDN} aborted (disabled)\n";
                continue;
            }
        }
        if ($BranchsInMyql > 0) {
            if (!isset($OUCONFIG[$OrganizationDN])) {
                echo "Importing users from {$hash[$i]["ou"][0]} {$OrganizationDN} is not in mysql database (disabled)\n";
                continue;
            }
        }
        $OnlyBranch = $OUCONFIG[$OrganizationDN]["PARAMS"]["OnlyBranch"];
        $dn = utf8_encode($hash[$i]["dn"]);
        $ou = utf8_encode($hash[$i]["ou"][0]);
        $dn = addslashes($dn);
        $ou = addslashes($ou);
        $sql = "INSERT IGNORE INTO activedirectory_orgs (ou,dn) VALUES('{$ou}','{$dn}')";
        $q->QUERY_SQL($sql, "artica_backup");
        echo "Importing users from {$hash[$i]["ou"][0]} {$hash[$i]["dn"]} OnlyBranch={$OnlyBranch}\n";
        importuser($hash[$i]["dn"], $ou, $OnlyBranch);
    }
    if ($GLOBALS["MEMORY_COUNT_USERS"] == 0) {
        @unlink($pidTime);
    }
    checksGroups();
    $distanceOfTimeInWords = $unix->distanceOfTimeInWords($t1, time());
    $unix->send_email_events("ActiveDirectory: {$GLOBALS["MEMORY_COUNT_USERS"]} members / {$GLOBALS["MEMORY_COUNT_GROUPS"]} groups imported", "These items has been imported into the cache database in {$distanceOfTimeInWords}", "system");
}
function tables_hours()
{
    $unix = new unix();
    $pidfile = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".pid";
    $timefile = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".time";
    if ($GLOBALS["VERBOSE"]) {
        echo "timefile={$timefile}\n";
    }
    $pid = @file_get_contents($pidfile);
    if (!$GLOBALS["FORCE"]) {
        if ($pid < 100) {
            $pid = null;
        }
        $unix = new unix();
        if ($unix->process_exists($pid, basename(__FILE__))) {
            if ($GLOBALS["VERBOSE"]) {
                echo "Already executed pid {$pid}\n";
            }
            return;
        }
        $timeexec = $unix->file_time_min($timefile);
        if ($timeexec < 60) {
            if ($GLOBALS["VERBOSE"]) {
                echo "Only each 60mn - current {$timeexec}mn, use --force to bypass\n";
            }
            return;
        }
        $mypid = getmypid();
        @file_put_contents($pidfile, $mypid);
    }
    @unlink($timefile);
    @file_put_contents($timefile, time());
    $sock = new sockets();
    $GLOBALS["Q"] = new mysql_squid_builder();
    $prefix = date("YmdH");
    $currenttable = "ngixattck_{$prefix}";
    if ($GLOBALS["VERBOSE"]) {
        echo "Current Table: {$currenttable}\n";
    }
    $tablesBrutes = $GLOBALS["Q"]->LIST_TABLES_NGINX_BLOCKED_RT();
    while (list($tablename, $none) = each($tablesBrutes)) {
        if ($tablename == $currenttable) {
            if ($GLOBALS["VERBOSE"]) {
                echo "Skip table: {$tablename}\n";
            }
            continue;
        }
        $t = time();
        if ($GLOBALS["VERBOSE"]) {
            echo "_table_hours_perform({$tablename})\n";
        }
        if (_table_hours_perform($tablename)) {
            $took = $unix->distanceOfTimeInWords($t, time());
            if ($GLOBALS["VERBOSE"]) {
                echo "Remove table: {$tablename}\n";
            }
            $GLOBALS["Q"]->QUERY_SQL("DROP TABLE `{$tablename}`");
            if (system_is_overloaded()) {
                ufdbguard_admin_events("Fatal: Overloaded system: {$GLOBALS["SYSTEM_INTERNAL_LOAD"]} sleeping stopping function", __FUNCTION__, __FILE__, __LINE__, "stats");
                return true;
            }
        }
    }
}
示例#7
0
function repair_action($database, $tablename, $expl)
{
    $unix = new unix();
    $q = new mysql();
    if (preg_match("#Can.*?t find file#", $expl)) {
        system_admin_events("{$tablename} is destroyed, remove it..", __FUNCTION__, __FILE__, __LINE__);
        echo "Removing table {$database}/{$tablename}\n";
        $q->DELETE_TABLE($tablename, $database);
        return;
    }
    if (preg_match("#is marked as crashed#", $expl)) {
        $results = array();
        $t = time();
        if (is_file("/var/lib/mysql/{$database}/{$tablename}.TMD")) {
            @copy("/var/lib/mysql/{$database}/{$tablename}.TMD", "/var/lib/mysql/{$database}/{$tablename}.TMD-" . time());
            @unlink("/var/lib/mysql/{$database}/{$tablename}.TMD");
        }
        $myisamchk = $unix->find_program("myisamchk");
        $cmd = "{$myisamchk} -r /var/lib/mysql/{$database}/{$tablename}.MYI";
        if ($GLOBALS["VERBOSE"]) {
            echo "{$cmd}\n";
        }
        exec($cmd, $results);
        $took = $unix->distanceOfTimeInWords($t, time());
        system_admin_events("{$tablename} repaired took: {$took}", @implode("\r\n", $results), __FUNCTION__, __FILE__, __LINE__);
        return;
    }
    if ($GLOBALS["VERBOSE"]) {
        echo "{$tablename} nothing to do...\n";
    }
}
示例#8
0
function restore_id($storeid)
{
    $unix = new unix();
    $pidfile = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".{$storeid}.pid";
    $pid = @file_get_contents("{$pidfile}");
    if ($unix->process_exists($pid, basename(__FILE__))) {
        events("Process {$pid} already exists...aborting");
        die;
    }
    @file_put_contents($pidfile, getmypid());
    include_once dirname(__FILE__) . "/ressources/class.mysql.syslogs.inc";
    $sock = new sockets();
    $SargOutputDir = $sock->GET_INFO("SargOutputDir");
    if ($SargOutputDir == null) {
        $SargOutputDir = "/var/www/html/squid-reports";
    }
    $sarg_bin = $unix->find_program("sarg");
    if (!is_file($sarg_bin)) {
        sarg_admin_events("Fatal, unable to locate sarg binary, aborting...", __FUNCTION__, __FILE__, __LINE__, "sarg");
        return;
    }
    $bzip2 = $unix->find_program("bzip2");
    $gunzip = $unix->find_program("gunzip");
    $TempDir = "/home/artica-extract-temp";
    @mkdir($TempDir, 0777);
    @chown($TempDir, "mysql");
    @chdir($TempDir, "mysql");
    if (!is_file("/etc/squid3/sarg.conf")) {
        buildconf();
    }
    $q = new mysql_storelogs();
    $ligne = @mysql_fetch_array($q->QUERY_SQL("SELECT filename FROM files_info WHERE storeid='{$storeid}'"));
    $filename = $ligne["filename"];
    events("Extracting infos from {$filename}");
    $EnableSyslogDB = @file_get_contents("/etc/artica-postfix/settings/Daemons/EnableSyslogDB");
    if (!is_numeric($EnableSyslogDB)) {
        $EnableSyslogDB = 0;
    }
    if ($EnableSyslogDB == 0) {
        events("Extracting infos from {$filename} failed, SyslogDB is not enabled");
        return;
    }
    $q = new mysql_storelogs();
    $sql = "SELECT filecontent INTO DUMPFILE '{$TempDir}/{$filename}' FROM files_store WHERE ID = '{$storeid}'";
    $q->QUERY_SQL($sql);
    if (!$q->ok) {
        events("Failed!!! {$q->mysql_error}", 100);
        return;
    }
    $file_extension = file_extension($filename);
    progress("Extract {$filename} extension: {$file_extension}", 5);
    $newtFile = $filename . ".log";
    if ($file_extension == "bz2") {
        $cmdline = "{$bzip2} -d \"{$TempDir}/{$filename}\" -c >\"{$TempDir}/{$newtFile}.log\" 2>&1";
        exec($cmdline, $results);
    }
    if ($file_extension == "gz") {
        $cmdline = "{$gunzip} -d \"{$TempDir}/{$filename}\" -c >\"{$TempDir}/{$newtFile}.log\" 2>&1";
    }
    if ($cmdline != null) {
        exec($cmdline, $results);
        progress("Extract done " . @implode(" ", $results), 7);
    } else {
        if (!@copy("{$TempDir}/{$filename}", "{$TempDir}/{$newtFile}.log")) {
            progress("Failed!!! Copy error", 100);
            return;
        }
    }
    @unlink("{$TempDir}/{$filename}");
    if (!is_file("{$TempDir}/{$newtFile}.log")) {
        progress("Failed!!! {$TempDir}/{$newtFile}.log error no such file", 100);
        return;
    }
    $t = time();
    sargToFile("{$TempDir}/{$newtFile}.log");
    progress("{$filename} ({$storeid}) done " . $unix->distanceOfTimeInWords($t, time()));
    backup();
}
示例#9
0
function exec2($cmd)
{
    $t = time();
    $unix = new unix();
    ouputz("Please wait... Executing a new task...", __LINE__);
    ouputz($cmd, __LINE__);
    exec($cmd . " 2>&1", $results);
    while (list($pattern, $line) = each($results)) {
        ouputz($line, __LINE__);
    }
    ouputz("Success took " . $unix->distanceOfTimeInWords($t, time(), true), __LINE__);
}
示例#10
0
function logrotate()
{
    $unix = new unix();
    $sock = new sockets();
    $BackupMailLogPath = $sock->GET_INFO("BackupMailLogPath");
    $BackupMailLogMaxTimeCompressed = $sock->GET_INFO("BackupMailLogMaxTimeCompressed");
    if (!is_numeric($BackupMailLogMaxTimeCompressed)) {
        $BackupMailLogMaxTimeCompressed = 10080;
    }
    if ($BackupMailLogPath == null) {
        $BackupMailLogPath = "/home/logrotate_backup";
    }
    $du = $unix->find_program("du");
    $gzip = $unix->find_program("gzip");
    @mkdir("{$BackupMailLogPath}", true, 660);
    if (!is_dir("{$BackupMailLogPath}")) {
        $unix->send_email_events("PostFinder:Error while creating {$BackupMailLogPath}");
        return;
    }
    $nice = $unix->EXEC_NICE();
    $timestart = time();
    $log = array();
    foreach (glob("{$BackupMailLogPath}/*.log") as $filename) {
        $timefile = $unix->file_time_min($filename);
        $basename = basename($filename);
        if ($GLOBALS["VERBOSE"]) {
            echo "{$basename}: {$timefile} minutes (need {$BackupMailLogMaxTimeCompressed} minutes)\n";
        }
        if ($timefile > $BackupMailLogMaxTimeCompressed) {
            $targetgzip = "{$BackupMailLogPath}/{$basename}.gz";
            $cmd = trim("{$nice}{$gzip} -c {$filename} >{$targetgzip}");
            if ($GLOBALS["VERBOSE"]) {
                echo "{$cmd}\n";
            }
            $time = time();
            shell_exec($cmd);
            if (is_file($targetgzip)) {
                $ev = "{$basename} -> {$targetgzip} " . $unix->distanceOfTimeInWords($time, time());
                if ($GLOBALS["VERBOSE"]) {
                    echo "{$ev}\n";
                }
                $log[] = $ev;
                @unlink($filename);
            }
        }
    }
    $strings = null;
    exec("{$du} -h -s {$BackupMailLogPath}", $results);
    $strings = @implode("", $results);
    if (preg_match("#^(.+?)\\s+#", $strings, $re)) {
        $final_size = $re[1];
    }
    @file_put_contents("/usr/share/artica-postfix/ressources/logs/postfinder.dirsize.txt", $final_size);
    @chmod("/usr/share/artica-postfix/ressources/logs/postfinder.dirsize.txt", 777);
    if (count($log) > 0) {
        $unix->send_email_events("Postfinder: Directory size: {$strings} - " . count($log) . " maillog compressed (" . $unix->distanceOfTimeInWords($timestart, time()) . ")", "Directory: {$BackupMailLogPath}\n" . @implode("\n", $log), "postfix");
    }
}
function updatev2_manu()
{
    $sock = new sockets();
    $unix = new unix();
    $t = time();
    $GLOBALS["TEMP_PATH"] = $unix->TEMP_DIR();
    $ManualArticaDBPath = $sock->GET_INFO("ManualArticaDBPath");
    $ArticaDbReplicate = $sock->GET_INFO("ArticaDbReplicate");
    if ($ManualArticaDBPath == null) {
        $ManualArticaDBPath = "/home/manualupdate/articadb.tar.gz";
    }
    $ArticaDBPath = $sock->GET_INFO("ArticaDBPath");
    if ($ArticaDBPath == null) {
        $ArticaDBPath = "/opt/articatech";
    }
    if (!is_numeric($ArticaDbReplicate)) {
        $ArticaDbReplicate = 0;
    }
    $tar = $unix->find_program("tar");
    updatev2_progress(80, "{installing}...");
    if ($GLOBALS["VERBOSE"]) {
        echo "uncompressing {$ManualArticaDBPath}\n";
    }
    @mkdir($ArticaDBPath, 0755, true);
    updatev2_progress(85, "{stopping_service}...");
    shell_exec("/etc/init.d/artica-postfix stop articadb");
    updatev2_progress(95, "{extracting_package}...");
    shell_exec("{$tar} -xf {$ManualArticaDBPath} -C {$ArticaDBPath}/");
    updatev2_progress(96, "{cleaning}...");
    if ($ArticaDbReplicate == 1) {
        @copy("/usr/share/artica-postfix/ressources/logs/web/cache/CATZ_ARRAY", "/home/articatechdb.version");
        @copy("{$ManualArticaDBPath}", "/home/articadb.tar.gz");
        @chmod("/home/articadb.tar.gz", 0755);
        @chmod("/home/articatechdb.version", 0755);
    }
    @unlink($ManualArticaDBPath);
    updatev2_progress(89, "{starting_service}...");
    if ($GLOBALS["VERBOSE"]) {
        echo "starting Articadb\n";
    }
    shell_exec("/etc/init.d/artica-postfix start articadb");
    updatev2_progress(90, "{checking}");
    $q = new mysql();
    if (!$q->DATABASE_EXISTS("catz")) {
        updatev2_progress(95, "Removing old database catz");
        $q->DELETE_DATABASE("catz");
    }
    updatev2_progress(99, "{finish}");
    $took = $unix->distanceOfTimeInWords($t, time());
    $LOCAL_VERSION = @file_get_contents("{$ArticaDBPath}/VERSION");
    squid_admin_mysql(2, "New Artica Database statistics {$LOCAL_VERSION} updated took:{$took}", "");
    // ufdbguard_admin_events("New Artica Database statistics $LOCAL_VERSION updated took:$took.",__FUNCTION__,__FILE__,__LINE__,"ufbd-artica");
    updatev2_progress(100, "{done}");
    $q->QUERY_SQL("TRUNCATE TABLE `catztemp`");
    $nohup = $unix->find_program("nohup");
    $php5 = $unix->LOCATE_PHP5_BIN();
    $cmd = trim("{$nohup} {$php5} /usr/share/artica-postfix/exec.squid.visited.sites.php --schedule-id={$GLOBALS["SCHEDULE_ID"]} >/dev/null 2>&1 &");
    shell_exec($cmd);
    return true;
}
function compile()
{
    if (!ifMustBeExecuted()) {
        WriteMyLogs("No make sense to execute this script...", __FUNCTION__, __FILE__, __LINE__);
        if ($GLOBALS["VERBOSE"]) {
            echo "No make sense to execute this script...\n";
        }
        die;
    }
    $ufdb = new compile_ufdbguard();
    $q = new mysql_squid_builder();
    $unix = new unix();
    $t = time();
    $myFile = basename(__FILE__);
    $pidfile = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".pid";
    $unix = new unix();
    $ufdbGenTable = $unix->find_program("ufdbGenTable");
    if (!is_file($ufdbGenTable)) {
        return;
    }
    $pid = @file_get_contents($pidfile);
    $getmypid = $GLOBALS["MYPID"];
    if (!$GLOBALS["FORCE"]) {
        if ($unix->process_exists($pid, $myFile)) {
            $timePid = $unix->PROCCESS_TIME_MIN($pid);
            ufdbguard_admin_events("Already executed PID:{$pid}, since {$timePid}Mn die() ", __FUNCTION__, __FILE__, __LINE__, "Toulouse DB");
            die;
        }
    }
    $Conversion = $q->TLSE_CONVERTION();
    $workdir = "/var/lib/ftpunivtlse1fr";
    $c = 0;
    while (list($directory, $line) = each($Conversion)) {
        $c++;
        $ufdb->UfdbGenTable("{$workdir}/{$directory}", $directory);
        $unix->chown_func("squid", "squid", "{$workdir}/{$directory}");
    }
    ufdbguard_admin_events("Compiling {$c} databases done, took:" . $unix->distanceOfTimeInWords($t, time(), true), __FUNCTION__, __FILE__, __LINE__, "Toulouse DB");
    $unix = new unix();
    $php5 = $unix->LOCATE_PHP5_BIN();
    shell_exec("{$php5} /usr/share/artica-postfix/exec.squidguard.php --build schedule-id={$GLOBALS["SCHEDULE_ID"]}");
}
示例#13
0
function execBackup()
{
    $unix = new unix();
    $pidfile = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".pid";
    $pid = $unix->get_pid_from_file($pidfile);
    if ($unix->process_exists($pid)) {
        $TTL = $unix->PROCESS_TTL($pid);
        if ($TTL < 240) {
            return;
        }
        $kill = $unix->find_program("kill");
        unix_system_kill_force($pid);
    }
    @file_put_contents($pidfile, getmypid());
    $hostname = $unix->hostname_g();
    $sock = new sockets();
    $BackupArticaBackUseNas = intval($sock->GET_INFO("BackupArticaBackUseNas"));
    $BackupArticaBackLocalFolder = intval($sock->GET_INFO("BackupArticaBackLocalFolder"));
    $BackupArticaBackLocalDir = $sock->GET_INFO("BackupArticaBackLocalDir");
    if ($BackupArticaBackLocalDir == null) {
        $BackupArticaBackLocalDir = "/home/artica/backup";
    }
    if ($BackupArticaBackUseNas == 0) {
        if ($BackupArticaBackLocalFolder == 0) {
            progress(100, "No destination defined");
        }
    }
    progress(10, "{mounting}");
    if ($BackupArticaBackUseNas == 1) {
        if (!TestNas()) {
            system_admin_events("Mounting NAS filesystem report false", __FUNCTION__, __FILE__, __LINE__);
            progress(100, "{disabled}");
            return;
        }
    }
    $t = time();
    $mountPoint = "/mnt/BackupArticaBackNAS";
    $Workdir = "{$mountPoint}/{$hostname}/system-backup/" . date("Y-m-d-H");
    if ($BackupArticaBackLocalFolder == 1) {
        $Workdir = "{$BackupArticaBackLocalDir}/{$hostname}/system-backup/" . date("Y-m-d-H");
    }
    @mkdir($Workdir, 0755, true);
    progress(20, "{backup_ldap}");
    backup_ldap($Workdir);
    progress(30, "{backup_artica_settings}");
    backup_artica_settings($Workdir);
    progress(60, "{backup_artica_database}");
    backup_mysql_artica_backup($Workdir);
    progress(70, "{backup_proxy_databases}");
    backup_mysql_artica_squidlogs($Workdir);
    @file_put_contents("{$Workdir}/BKVERSION.txt", time());
    @file_put_contents("{$Workdir}/DURATION.txt", $unix->distanceOfTimeInWords($t, time(), true));
    progress(90, "{backup_done}");
    if ($BackupArticaBackLocalFolder == 0) {
        $mount = new mount("/var/log/artica-postfix/backup.debug");
        $mountPoint = "/mnt/BackupArticaBackNAS";
        if ($mount->ismounted($mountPoint)) {
            $mount->umount($mountPoint);
        }
    }
    progress(100, "{done}");
}
示例#14
0
function Parse($filename)
{
    $t1 = time();
    $unix = new unix();
    $workfile = $filename;
    $stampfile = "{$filename}.last";
    if (is_file($stampfile)) {
        $LastScannLine = intval(@file_get_contents($stampfile));
    }
    $handle = @fopen($workfile, "r");
    if (!$handle) {
        events("Fopen failed on {$workfile}");
        return false;
    }
    if ($LastScannLine > 0) {
        fseek($handle, $LastScannLine, SEEK_SET);
    }
    while (!feof($handle)) {
        //1444514181;www.safe-demo.com;46.4.32.75;200;1;42354
        $buffer = trim(fgets($handle));
        if ($buffer == null) {
            continue;
        }
        $ARRAY = explode(";", $buffer);
        $TIME = $ARRAY[0];
        $SITENAME = $ARRAY[1];
        $IPADDR = $ARRAY[2];
        if ($IPADDR == "127.0.0.1") {
            continue;
        }
        $HTTP_CODE = $ARRAY[3];
        $RQS = $ARRAY[4];
        $SIZE = $ARRAY[5];
        $TIME_HOUR = date("Y-m-d H:00:00", $TIME);
        $KEYMD5 = md5("{$TIME_HOUR}{$SITENAME}");
        $KEYMD5FULL = md5("{$TIME_HOUR}{$SITENAME}{$IPADDR}{$HTTP_CODE}");
        if (!isset($GENERIC[$KEYMD5]["SIZE"])) {
            $GENERIC[$KEYMD5]["DATE"] = $TIME_HOUR;
            $GENERIC[$KEYMD5]["INFLUX_TIME"] = TimeToInflux($TIME);
            $GENERIC[$KEYMD5]["SIZE"] = intval($SIZE);
            $GENERIC[$KEYMD5]["RQS"] = intval($RQS);
            $GENERIC[$KEYMD5]["SITENAME"] = $SITENAME;
        } else {
            $GENERIC[$KEYMD5]["SIZE"] = $GENERIC[$KEYMD5]["SIZE"] + $SIZE;
            $GENERIC[$KEYMD5]["RQS"] = $GENERIC[$KEYMD5]["RQS"] + $RQS;
        }
        if (!isset($FULL[$KEYMD5FULL]["SIZE"])) {
            $FULL[$KEYMD5FULL]["DATE"] = $TIME_HOUR;
            $FULL[$KEYMD5FULL]["INFLUX_TIME"] = QueryToUTC(strtotime($TIME_HOUR), true);
            $FULL[$KEYMD5FULL]["SIZE"] = intval($SIZE);
            $FULL[$KEYMD5FULL]["RQS"] = intval($RQS);
            $FULL[$KEYMD5FULL]["SITENAME"] = $SITENAME;
            $FULL[$KEYMD5FULL]["IPADDR"] = $IPADDR;
            $FULL[$KEYMD5FULL]["HTTP_CODE"] = $HTTP_CODE;
        } else {
            $FULL[$KEYMD5FULL]["SIZE"] = $GENERIC[$KEYMD5]["SIZE"] + $SIZE;
            $FULL[$KEYMD5FULL]["RQS"] = $GENERIC[$KEYMD5]["RQS"] + $RQS;
        }
        if (count($GENERIC) > 500) {
            if (!DUMP_GENERIC($GENERIC)) {
                return;
            }
            $GENERIC = array();
        }
        if (count($FULL) > 500) {
            if (!DUMP_FULL($FULL)) {
                return;
            }
            $FULL = array();
        }
    }
    if (count($GENERIC) > 0) {
        if (!DUMP_GENERIC($GENERIC)) {
            return;
        }
    }
    if (count($FULL) > 0) {
        if (!DUMP_FULL($FULL)) {
            return;
        }
        $FULL = array();
    }
    $took = $unix->distanceOfTimeInWords($t1, time(), true);
    apache_admin_mysql(2, "Success injecting data from " . basename($workfile) . " took:{$took}", null, __FILE__, __LINE__);
    @unlink($workfile);
}
示例#15
0
function refresh_ticket($nopid = false)
{
    $unix = new unix();
    $sock = new sockets();
    $users = new usersMenus();
    $t = time();
    $timefile = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".time";
    $pidfile = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".pid";
    $pid = $unix->get_pid_from_file($pidfile);
    if ($unix->process_exists($pid, basename(__FILE__))) {
        $timeExec = intval($unix->PROCCESS_TIME_MIN($pid));
        writelogs("Process {$pid} already exists since {$timeExec}Mn", __FUNCTION__, __FILE__, __LINE__);
        if ($timeExec > 5) {
            $kill = $unix->find_program("kill");
            system_admin_events("killing old pid {$pid} (already exists since {$timeExec}Mn)", __FUNCTION__, __FILE__, __LINE__);
            unix_system_kill_force($pid);
        } else {
            return;
        }
    }
    $EnableKerbAuth = intval($sock->GET_INFO("EnableKerbAuth"));
    if ($EnableKerbAuth == 0) {
        system_admin_events("EnableKerbAuth is disabled, aborting", __FUNCTION__, __FILE__, __LINE__);
        return;
    }
    if (!$users->SAMBA_INSTALLED) {
        system_admin_events("Samba is not installed, aborting", __FUNCTION__, __FILE__, __LINE__);
        return;
    }
    $net = $unix->find_program("net");
    $msktutil = $unix->find_program("msktutil");
    $cmdline = "{$net} rpc changetrustpw -d 3 2>&1";
    exec($cmdline, $results);
    if (!is_file($msktutil)) {
        $results[] = "msktutil no such binary...";
        $took = $unix->distanceOfTimeInWords($t, time(), true);
        system_admin_events("Update kerberos done took:{$took}\n" . @implode("\n", $results), __FUNCTION__, __FILE__, __LINE__);
        return;
    }
    $myNetBiosName = $unix->hostname_simple();
    $msktutil_version = msktutil_version();
    if ($msktutil == 3) {
        $cmd = "{$msktutil} --update --verbose --computer-name {$myNetBiosName}";
    }
    if ($msktutil == 4) {
        $cmd = "{$msktutil} --auto-update --verbose --computer-name {$myNetBiosName}";
    }
    $results[] = $cmd;
    exec($cmd . " 2>&1", $results);
    $took = $unix->distanceOfTimeInWords($t, time(), true);
    system_admin_events("Update kerberos done took:{$took}\n" . @implode("\n", $results), __FUNCTION__, __FILE__, __LINE__);
}
示例#16
0
function nightly()
{
    @mkdir("/var/log/artica-postfix", 0755, true);
    $GLOBALS["MasterIndexFile"] = "/usr/share/artica-postfix/ressources/index.ini";
    $unix = new unix();
    $sock = new sockets();
    $autoinstall = true;
    $timefile = "/etc/artica-postfix/croned.1/nightly";
    $pidfile = "/etc/artica-postfix/pids/" . basename(__FILE__) . ".pid";
    $pid = @file_get_contents($pidfile);
    $kill = $unix->find_program("kill");
    $tmpdir = $unix->TEMP_DIR();
    $php5 = $unix->LOCATE_PHP5_BIN();
    $nohup = $unix->find_program("nohup");
    if ($unix->process_exists($pid, basename(__FILE__))) {
        $time = $unix->PROCCESS_TIME_MIN($pid);
        echo "Starting......: " . date("H:i:s") . " nightly build already executed PID: {$pid} since {$time}Mn\n";
        system_admin_events("nightly build already executed PID: {$pid} since {$time}Mn", __FUNCTION__, __FILE__, __LINE__, "artica-update");
        if ($time < 120) {
            if (!$GLOBALS["FORCE"]) {
                die;
            }
        }
        unix_system_kill_force($pid);
    }
    $mypid = getmypid();
    @file_put_contents($pidfile, $mypid);
    $EnableScheduleUpdates = $sock->GET_INFO("EnableScheduleUpdates");
    if (!is_numeric($EnableScheduleUpdates)) {
        $EnableScheduleUpdates = 0;
    }
    if ($GLOBALS["FORCE"]) {
        _artica_update_event(1, "Update task pid {$mypid} is forced by an human.", null, __FILE__, __LINE__);
    }
    if ($EnableScheduleUpdates == 1) {
        if (!$GLOBALS["FORCE"]) {
            if (!$GLOBALS["BYCRON"]) {
                _artica_update_event(2, "Operation must be only executed by scheduler ( use --force to by pass)", null, __FILE__, __LINE__);
                @file_put_contents("/usr/share/artica-postfix/download_progress", 100);
                return;
            }
        }
    }
    $ini = new iniFrameWork();
    $ini->loadFile('/etc/artica-postfix/artica-update.conf');
    if (!isset($ini->_params["AUTOUPDATE"]["enabled"])) {
        $ini->_params["AUTOUPDATE"]["enabled"] = "yes";
    }
    if (trim($ini->_params["AUTOUPDATE"]["enabled"]) == null) {
        $ini->_params["AUTOUPDATE"]["enabled"] = "yes";
    }
    if ($ini->_params["AUTOUPDATE"]["enabled"] == null) {
        $ini->_params["AUTOUPDATE"]["enabled"] = "yes";
    }
    if (trim($ini->_params["AUTOUPDATE"]["uri"]) == null) {
        $ini->_params["AUTOUPDATE"]["uri"] = "http://www.articatech.net/auto.update.php";
    }
    if ($ini->_params["AUTOUPDATE"]["enabled"] == 1) {
        $ini->_params["AUTOUPDATE"]["enabled"] = 'yes';
    }
    if (!is_numeric(trim($ini->_params["AUTOUPDATE"]["CheckEveryMinutes"]))) {
        $ini->_params["AUTOUPDATE"]["CheckEveryMinutes"] = 60;
    }
    if ($ini->_params["AUTOUPDATE"]["enabled"] != 'yes') {
        echo "Starting......: " . date("H:i:s") . " Artica Update feature is disabled (enabled = {$ini->_params["AUTOUPDATE"]["enabled"]} )\n";
        return;
    }
    $CheckEveryMinutes = $ini->_params["AUTOUPDATE"]["CheckEveryMinutes"];
    $uri = $ini->_params["AUTOUPDATE"]["uri"];
    $arrayURI = parse_url($uri);
    $MAIN_URI = "{$arrayURI["scheme"]}://{$arrayURI["host"]}";
    $MAIN_URI = str_replace("www.artica.fr", "www.articatech.net", $MAIN_URI);
    if (!$GLOBALS["FORCE"]) {
        if ($EnableScheduleUpdates == 0) {
            if ($unix->file_time_min($timefile) < $CheckEveryMinutes) {
                echo "Starting......: " . date("H:i:s") . " update feature (too short time, require {$CheckEveryMinutes}mn)\n";
                @file_put_contents("/usr/share/artica-postfix/download_progress", 100);
                return;
            }
        }
    }
    @unlink($timefile);
    @file_put_contents($timefile, time());
    if ($GLOBALS["FORCE"]) {
        if (is_file("/root/artica-latest.tgz")) {
            echo "Starting......: " . date("H:i:s") . " Installing old downloaded package\n";
            if (install_package("/root/artica-latest.tgz")) {
                return;
            }
        }
    }
    // ----------------------- LANCEMENT ------------------------------------------------------------------------------
    $EnableArticaMetaClient = intval($sock->GET_INFO("EnableArticaMetaClient"));
    if ($EnableArticaMetaClient == 1) {
        echo "Starting......: " . date("H:i:s") . " Nightly builds using Artica Meta console\n";
        system("{$nohup} {$php5} /usr/share/artica-postfix/exec.artica-meta-client.php --artica-updates >/dev/null 2>&1 &");
        die;
    }
    echo "Starting......: " . date("H:i:s") . " Nightly builds checking an official release first\n";
    if (update_release()) {
        return;
    }
    $nightly = trim(strtolower($ini->_params["AUTOUPDATE"]["nightlybuild"]));
    if ($nightly == 1) {
        $nightly = "yes";
    }
    if ($GLOBALS["FORCE"]) {
        $nightly = "yes";
    }
    if ($GLOBALS["FORCE_NIGHTLY"]) {
        $nightly = "yes";
    }
    if ($nightly != 'yes') {
        echo "Starting......: " . date("H:i:s") . " Nightly builds feature is disabled [{$nightly}]\n";
        @file_put_contents("/usr/share/artica-postfix/ressources/logs/web/download_progress", 100);
        return;
    }
    if ($ini->_params["AUTOUPDATE"]["autoinstall"] == 1) {
        $ini->_params["AUTOUPDATE"]["autoinstall"] = "yes";
    }
    if (trim($ini->_params["AUTOUPDATE"]["autoinstall"]) == null) {
        $ini->_params["AUTOUPDATE"]["autoinstall"] = "yes";
    }
    if ($ini->_params["AUTOUPDATE"]["autoinstall"] != "yes") {
        $autoinstall = false;
    }
    @file_put_contents("/usr/share/artica-postfix/download_progress", 0);
    $MyCurrentVersion = GetCurrentVersion();
    echo "Starting......: " . date("H:i:s") . " Current version: {$MyCurrentVersion}\n";
    $Lastest = trim(strtolower($GLOBALS["lastest-nightly"]));
    echo "Starting......: " . date("H:i:s") . " Nightly builds version \"{$Lastest}\" on repository\n";
    $MyNextVersion = intval(str_replace(".", "", $Lastest));
    echo "Starting......: " . date("H:i:s") . " nightly builds Cur:{$MyCurrentVersion}, Next:{$MyNextVersion}\n";
    if ($MyNextVersion == $MyCurrentVersion) {
        echo "Starting......: " . date("H:i:s") . " nightly builds {$MyCurrentVersion}/{$MyNextVersion} \"Up to date - Same version\"\n";
        @file_put_contents("/usr/share/artica-postfix/download_progress", 100);
        return;
    }
    if ($MyCurrentVersion > $MyNextVersion) {
        echo "Starting......: " . date("H:i:s") . " nightly builds {$MyCurrentVersion}/{$MyNextVersion} \"Up to date - Most updated\"\n";
        @file_put_contents("/usr/share/artica-postfix/download_progress", 100);
        return;
    }
    $t1 = time();
    _artica_update_event(2, "nightly builds Downloading new version {$Lastest}", null, __FILE__, __LINE__);
    echo "Starting......: " . date("H:i:s") . " nightly builds Downloading new version {$Lastest}, please wait\n";
    events("Downloading new version {$Lastest}");
    $uri = "{$MAIN_URI}/nightbuilds/artica-{$Lastest}.tgz";
    $ArticaFileTemp = "{$tmpdir}/{$Lastest}/artica-{$Lastest}.tgz";
    @mkdir("{$tmpdir}/{$Lastest}", 0755, true);
    $curl = new ccurl($uri);
    $curl->Timeout = 2400;
    $curl->WriteProgress = true;
    $curl->ProgressFunction = "nightly_progress";
    $t = time();
    if (!$curl->GetFile($ArticaFileTemp)) {
        _artica_update_event(0, "nightly builds Unable to download latest nightly build with error {$curl->error}", null, __FILE__, __LINE__);
        events("Unable to download latest nightly build with error {$curl->error}");
        system_admin_events("Unable to download latest nightly build with error {$curl->error}", __FUNCTION__, __FILE__, __LINE__, "artica-update");
        @unlink($ArticaFileTemp);
        return;
    }
    $took = $unix->distanceOfTimeInWords($t, time(), true);
    _artica_update_event(2, "artica-{$Lastest}.tgz download, took {$took}", null, __FILE__, __LINE__);
    system_admin_events("artica-{$Lastest}.tgz download, took {$took}", __FUNCTION__, __FILE__, __LINE__, "artica-update");
    events("artica-{$Lastest}.tgz download, took {$took}");
    $size = @filesize($ArticaFileTemp);
    $size = FormatBytes($size / 1024, true);
    ArticaMeta_nightly($ArticaFileTemp);
    echo "Starting......: " . date("H:i:s") . " nightly builds took {$took}\n";
    if (!$GLOBALS["FORCE"]) {
        if ($autoinstall == false) {
            _artica_update_event(2, "artica-latest.tgz will be stored in /root", null, __FILE__, __LINE__);
            @copy("{$ArticaFileTemp}", "/root/artica-latest.tgz");
            @unlink($ArticaFileTemp);
            _artica_update_event(1, "Nightly builds New Artica update v.{$Lastest} ({$size}) waiting order", null, __FILE__, __LINE__);
            system_admin_events("New Artica update v.{$Lastest} waiting your order", __FUNCTION__, __FILE__, __LINE__, "artica-update");
            return;
        } else {
        }
    }
    events("Now, installing the newest version in {$ArticaFileTemp} package...");
    $size = @filesize($ArticaFileTemp);
    $size = FormatBytes($size / 1024, true);
    if (!install_package($ArticaFileTemp, $Lastest)) {
        events("Install package Failed...");
        return false;
    }
    events("New Artica update v.{$Lastest}");
    _artica_update_event(1, "Nightly builds New Artica update v.{$Lastest} ({$size})", null, __FILE__, __LINE__);
    system_admin_events("New Artica update v.{$Lastest}", __FUNCTION__, __FILE__, __LINE__, "artica-update");
}
示例#17
0
function UFDBGUARD_COMPILE_SINGLE_DB($path)
{
    $unix = new unix();
    $path = str_replace(".ufdb", "", $path);
    $pidpath = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . md5($path) . ".pid";
    $oldpid = @file_get_contents($pidpath);
    if ($unix->process_exists($oldpid)) {
        events_ufdb_tail("Check \"{$path}\"... Already process PID \"{$oldpid}\" running task has been aborted");
        return;
    }
    $category = null;
    $ufdbGenTable = $unix->find_program("ufdbGenTable");
    events_ufdb_tail("Check \"{$path}\"...", __LINE__);
    if (preg_match("#\\/var\\/lib\\/squidguard\\/(.+?)\\/(.+?)/(.+?)\$#", $path, $re)) {
        $category = $re[2];
        $domain_path = "/var/lib/squidguard/{$re[1]}/{$re[2]}/domains";
    }
    if ($category == null) {
        if (preg_match("#\\/var\\/lib\\/squidguard\\/(.+?)\\/domains#", $path, $re)) {
            $category = $re[1];
            $domain_path = "/var/lib/squidguard/{$re[1]}/domains";
        }
    }
    if (preg_match("#web-filter-plus\\/BL\\/(.+?)\\/domains#", $path, $re)) {
        $category = $re[1];
        $domain_path = "/var/lib/squidguard/web-filter-plus/BL/{$category}/domains";
    }
    if (preg_match("#blacklist-artica\\/(.+?)\\/(.+?)\\/domains#", $path, $re)) {
        events_ufdb_tail("find double category \"{$re[1]}-{$re[2]}\"...", __LINE__);
        $category = "{$re[1]}-{$re[2]}";
        $domain_path = "/var/lib/squidguard/blacklist-artica/{$re[1]}/{$re[2]}/domains";
    }
    if (preg_match("#blacklist-artica\\/sex\\/(.+?)\\/domains#", $path, $re)) {
        $category = $re[1];
        $domain_path = "/var/lib/squidguard/blacklist-artica/sex/{$category}/domains";
    }
    if ($category == null) {
        events_ufdb_tail("exec.squidguard.php:: \"{$path}\" cannot understand...");
    }
    events_ufdb_tail("exec.squidguard.php:: Found category \"{$category}\"", __LINE__);
    if (!is_file($path)) {
        events_ufdb_tail("exec.squidguard.php:{$category}: \"{$path}\" no such file, build it", __LINE__);
        @file_put_contents($domain_path, " ");
    }
    $category_compile = substr($category, 0, 15);
    events_ufdb_tail("exec.squidguard.php:: category \"{$category}\" retranslated to \"{$category_compile}\"", __LINE__);
    if (is_file("{$domain_path}.ufdb")) {
        events_ufdb_tail("exec.squidguard.php:: removing \"{$domain_path}.ufdb\" ...");
        @unlink("{$domain_path}.ufdb");
    }
    if (!is_file($domain_path)) {
        events_ufdb_tail("exec.squidguard.php:: {$domain_path} no such file, create an empty one", __LINE__);
        @mkdir(dirname($domain_path), 755, true);
        @file_put_contents($domain_path, "#");
    }
    $d = " -d {$domain_path}";
    $cmd = "{$ufdbGenTable} -n -D -W -t {$category_compile}{$d} 2>&1";
    events_ufdb_tail("exec.squidguard.php:{$category}:{$cmd}");
    $time = time();
    exec($cmd, $results);
    exec($cmd, $results);
    while (list($a, $b) = each($results)) {
        if (strpos($b, "is not added because it was already matched")) {
            continue;
        }
        if (strpos($b, "has optimised subdomains")) {
            continue;
        }
        events_ufdb_tail("exec.squidguard.php:{$category}:{$b}");
    }
    events_ufdb_tail("exec.squidguard.php:{$category_compile}: execution :" . $unix->distanceOfTimeInWords($time, time()), __LINE__);
    events_ufdb_tail("exec.squidguard.php:{$category}:done..");
    $user = GetSquidUser();
    $chown = $unix->find_program("chown");
    if (is_file($chown)) {
        events_ufdb_tail("exec.squidguard.php:{$category}:{$chown} -R {$user} /var/lib/squidguard");
        shell_exec("{$chown} -R {$user} /var/lib/squidguard/*");
        shell_exec("{$chown} -R {$user} /var/log/squid/*");
    }
    PersonalCategoriesRepair();
    events_ufdb_tail("/etc/init.d/ufdb reconfig");
    shell_exec("/etc/init.d/ufdb reconfig");
}
示例#18
0
function visited_sites()
{
    $pidfile = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".pid";
    $oldpid = @file_get_contents($pidfile);
    if ($oldpid < 100) {
        $oldpid = null;
    }
    $unix = new unix();
    if ($unix->process_exists($oldpid)) {
        if ($GLOBALS["VERBOSE"]) {
            echo "Already executed pid {$oldpid}\n";
        }
        return;
    }
    $mypid = getmypid();
    @file_put_contents($pidfile, $mypid);
    $t1 = time();
    $sql = "SELECT sitename,country,category FROM visited_sites";
    $results = $GLOBALS["Q"]->QUERY_SQL($sql);
    $num_rows = mysql_num_rows($results);
    if ($num_rows == 0) {
        if ($GLOBALS["VERBOSE"]) {
            echo "No datas " . __FUNCTION__ . " " . __LINE__ . "\n";
        }
        return;
    }
    while ($ligne = @mysql_fetch_array($results, MYSQL_ASSOC)) {
        $country = null;
        $array = _visited_sites_calculate($ligne["sitename"]);
        if (!is_array($array)) {
            continue;
        }
        if (trim($ligne["country"] == null)) {
            $array_country = GeoIP($ligne["sitename"]);
        }
        if (isset($array_country)) {
            if (isset($array_country[0])) {
                $country = $array_country[0];
            }
        }
        if ($country != null) {
            $country = ",country='" . addslashes($country) . "'";
        }
        if ($GLOBALS["VERBOSE"]) {
            echo "{$ligne["sitename"]} {$array[0]} hits, {$array[1]} size Country '{$country}' on {$array[2]} tables\n";
        }
        $categories = $GLOBALS["Q"]->GET_CATEGORIES($ligne["sitename"], true);
        $categories = addslashes($categories);
        $sql = "UPDATE visited_sites SET HitsNumber='{$array[0]}',Querysize='{$array[1]}'{$country},category='{$categories}' WHERE sitename='{$ligne["sitename"]}'";
        $GLOBALS["Q"]->QUERY_SQL($sql);
    }
    $took = $unix->distanceOfTimeInWords($t1, time());
    writelogs_squid("Scanned {$num_rows} visisted websites {$took}", __FUNCTION__, __FILE__, __LINE__);
}
示例#19
0
function backup_webget($ID, $array)
{
    if ($GLOBALS["VERBOSE"]) {
        print_r($array);
    }
    $RemoteArticaServer = $array["RemoteArticaServer"];
    $RemoteArticaPort = $array["RemoteArticaPort"];
    $RemoteArticaUser = $array["RemoteArticaUser"];
    $RemoteArticaPassword = $array["RemoteArticaPassword"];
    $RemoteArticaSite = $array["RemoteArticaSite"];
    $AutoRestore = $array["AutoRestore"];
    $AutoRestoreSqlInstance = $array["AutoRestoreSqlInstance"];
    $AutoRestoreSiteName = $array["AutoRestoreSiteName"];
    if (!is_numeric($AutoRestore)) {
        $AutoRestore = 0;
    }
    if (!is_numeric($AutoRestoreSqlInstance)) {
        $AutoRestoreSqlInstance = 0;
    }
    $array["RemoteArticaPassword"] = md5($array["RemoteArticaPassword"]);
    $unix = new unix();
    $rm = $unix->find_program("rm");
    $php5 = $unix->LOCATE_PHP5_BIN();
    $RESULTS = null;
    if (trim($RemoteArticaServer) == null) {
        backup_events($ID, $RemoteArticaSite, "ERROR, No remote Artica server defined...", __LINE__);
        return;
    }
    if (!is_numeric($RemoteArticaPort)) {
        $RemoteArticaPort = 9000;
    }
    $uri = "https://{$RemoteArticaServer}:{$RemoteArticaPort}";
    backup_events($ID, $RemoteArticaSite, "INFO, Connecting to {$uri} in order to send backup order.", __LINE__);
    include_once dirname(__FILE__) . "/ressources/class.ccurl.inc";
    $params = base64_encode(serialize($array));
    $curl = new ccurl("{$uri}/exec.articaget.php?params={$params}");
    $curl->Timeout = 5600;
    $curl->NoHTTP_POST = true;
    if (!$curl->get()) {
        backup_events($ID, $RemoteArticaSite, "ERROR, Connecting to {$uri} {$curl->error}", __LINE__);
        return;
    }
    $datas = $curl->data;
    if (preg_match("#<LOGS>(.*?)</LOGS>#is", $datas, $re)) {
        $events = $re[1];
    }
    if (preg_match("#<RESULTS>(.*?)</RESULTS>#is", $datas, $re)) {
        $RESULTS = $re[1];
    }
    if ($RESULTS == null) {
        backup_events($ID, $RemoteArticaSite, "ERROR, No answer from {$RemoteArticaServer} ??? {$datas}", __LINE__);
        return;
    }
    if ($RESULTS != "SUCCESS") {
        if ($GLOBALS["VERBOSE"]) {
            echo "\nFAILED\n\n";
        }
        backup_events($ID, $RemoteArticaSite, "ERROR, Failed -> `{$RESULTS}`", __LINE__);
        backup_events($ID, $RemoteArticaSite, "INFO, {$events}", __LINE__);
        return;
    }
    backup_events($ID, $RemoteArticaSite, "INFO,downloading {$RemoteArticaSite}.tar.gz package", __LINE__);
    $curl = new ccurl("{$uri}/ressources/logs/web/{$RemoteArticaSite}.tar.gz");
    $curl->Timeout = 5600;
    $curl->NoHTTP_POST = true;
    @mkdir("/var/tmp/{$RemoteArticaSite}", 0755, true);
    if ($GLOBALS["VERBOSE"]) {
        echo "\nDownloading {$uri}/ressources/logs/web/{$RemoteArticaSite}.tar.gz\n\n";
    }
    $curdate = date("YmdH");
    if (!$curl->GetFile("/var/tmp/{$RemoteArticaSite}/{$RemoteArticaSite}-{$curdate}.tar.gz")) {
        backup_events($ID, $RemoteArticaSite, "ERROR, Failed downloading {$RemoteArticaSite}.tar.gz {$curl->error}", __LINE__);
        return;
    }
    if ($GLOBALS["VERBOSE"]) {
        echo "\n\n";
    }
    backup_events($ID, $RemoteArticaSite, "INFO, Connecting to {$uri} in order to cleaning the backup container.", __LINE__);
    $curl = new ccurl("{$uri}/exec.articaget.php?params={$params}&remove=yes");
    $curl->NoHTTP_POST = true;
    if (!$curl->get()) {
        backup_events($ID, $RemoteArticaSite, "ERROR, ordering to cleaning container...{$uri} {$curl->error}", __LINE__);
    }
    //Auto-restore
    if ($AutoRestore == 0) {
        backup_events($ID, $RemoteArticaSite, "INFO the Autorestore feature is disabled...", __LINE__);
    }
    if ($AutoRestore == 1) {
        backup_events($ID, $RemoteArticaSite, "INFO the Autorestore feature is enabled on `{$AutoRestoreSiteName}` local website...", __LINE__);
        if ($AutoRestoreSiteName != null) {
            $tt1 = time();
            backup_events($ID, $RemoteArticaSite, "INFO, auto-restore {$RemoteArticaSite}.tar.gz to {$AutoRestoreSiteName}", __LINE__);
            $cmd = "{$php5} " . dirname(__FILE__) . "/exec.freeweb.php --restore \"{$AutoRestoreSiteName}\" \"/var/tmp/{$RemoteArticaSite}/{$RemoteArticaSite}-{$curdate}.tar.gz\" {$AutoRestoreSqlInstance} --verbose 2>&1";
            exec($cmd, $AutorestoreResults);
            $tt2 = time();
            $took = $unix->distanceOfTimeInWords($tt1, $tt2, true);
            backup_events($ID, $RemoteArticaSite, "INFO, Auto-restore finish took {$took}, see details:\n" . @implode("\n", $AutorestoreResults), __LINE__);
        } else {
            backup_events($ID, $RemoteArticaSite, "ERROR, Autorestore disabled `AutoRestoreSiteName` is null ", __LINE__);
        }
    }
    $DestinationPath = "{$GLOBALS["MOUNTED_PATH_FINAL"]}/freewebs/webget.{$RemoteArticaSite}";
    backup_mkdir($DestinationPath);
    if (!backup_isdir($DestinationPath)) {
        backup_events($ID, $RemoteArticaSite, "ERROR, {$DestinationPath} permission denied or no such file or directory", __LINE__);
        @unlink("/var/tmp/{$RemoteArticaSite}/{$RemoteArticaSite}.tar.gz");
        return false;
    }
    $info = backup_copy("/var/tmp/{$RemoteArticaSite}/", "{$DestinationPath}", $ID);
    backup_events($ID, $RemoteArticaSite, "INFO,/var/tmp/{$RemoteArticaSite}\n{$info}", __LINE__);
    backup_events($ID, $RemoteArticaSite, "INFO,cleaning /var/tmp/{$RemoteArticaSite}", __LINE__);
    if (is_dir("/var/tmp/{$RemoteArticaSite}")) {
        shell_exec("{$rm} -rf /var/tmp/{$RemoteArticaSite}");
    }
}
示例#20
0
function inject_stored_items($nopid = false)
{
    $unix = new unix();
    $pidfile = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".pid";
    if (system_is_overloaded(basename(__FILE__))) {
        $php = $unix->LOCATE_PHP5_BIN();
        ufdbguard_admin_events("Overloaded system... ask to run this task later...", __FUNCTION__, __FILE__, __LINE__, "proxy");
        $unix->THREAD_COMMAND_SET("{$php} " . __FILE__ . " --inject");
    }
    if (!$nopid) {
        $pid = @file_get_contents($pidfile);
        $myfile = basename(__FILE__);
        if ($unix->process_exists($pid, $myfile)) {
            ufdbguard_admin_events("Task already running PID: {$pid}, aborting current task", __FUNCTION__, __FILE__, __LINE__, "proxy");
            return;
        }
    }
    $mypid = getmypid();
    @file_put_contents($pidfile, $mypid);
    $t1 = time();
    $file = "/var/cache/purge.calculated.db";
    if (!is_file($file)) {
        echo "{$file} no such file\n";
        return;
    }
    $q = new mysql_blackbox();
    if (!$q->TABLE_EXISTS("cacheitems_localhost")) {
        $q->build_cached_items_table("localhost");
    }
    $BIGARRAY = inject_stored_array();
    $prefix = "INSERT IGNORE INTO cacheitems_localhost(sitename,familysite,size,items) VALUES ";
    $q->QUERY_SQL("TRUNCATE TABLE cacheitems_localhost");
    $f = array();
    $c = 0;
    while (list($sitename, $array) = each($BIGARRAY)) {
        $c++;
        $f[] = "('{$sitename}','{$array["FAMILY"]}','{$array["SIZE"]}','{$array["ITEMS"]}')";
        if (count($f) > 500) {
            $q->QUERY_SQL($prefix . @implode(",", $f));
            if (!$q->ok) {
                squid_admin_mysql(0, "MySQL error!", $q->mysql_error, __FILE__, __LINE__);
                return;
            }
        }
    }
    if (count($f) > 0) {
        $q->QUERY_SQL($prefix . @implode(",", $f));
        if (!$q->ok) {
            squid_admin_mysql(0, "MySQL error!", $q->mysql_error, __FILE__, __LINE__);
            return;
        }
    }
    $took = $unix->distanceOfTimeInWords($t1, time());
    if ($c > 0) {
        squid_admin_mysql(2, "Sucess adding {$c} cached websites took:{$took}", null, __FILE__, __LINE__);
    }
    @unlink($file);
}
示例#21
0
function execute_mysql($OnlyID = 0)
{
    $GLOBALS["INDEXED"] = 0;
    $GLOBALS["SKIPPED"] = 0;
    $GLOBALS["DIRS"] = array();
    $unix = new unix();
    $httrack = $unix->find_program("httrack");
    if (!is_file($httrack)) {
        system_admin_events("httrack no such binary", __FUNCTION__, __FILE__, __LINE__, "webcopy");
        return;
    }
    $pidfile = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".pid";
    $pid = $unix->get_pid_from_file($pidfile);
    if ($unix->process_exists($pid, basename(__FILE__))) {
        system_admin_events("Already instance executed pid:{$olpid}", __FUNCTION__, __FILE__, __LINE__, "webcopy");
        return;
    }
    $getmypid = getmypid();
    @file_put_contents($pidfile, $getmypid);
    $q = new mysql();
    $nice = EXEC_NICE();
    $sql = "SELECT * FROM httrack_sites WHERE enabled=1";
    $results = $q->QUERY_SQL($sql, "artica_backup");
    if (!$q->ok) {
        system_admin_events("Fatal: {$q->mysql_error}", __FUNCTION__, __FILE__, __LINE__, "webcopy");
        return;
    }
    $t1 = time();
    $count = 0;
    if ($OnlyID > 0) {
        $ligne2 = mysql_fetch_array($q->QUERY_SQL("SELECT sitename FROM httrack_sites WHERE ID={$OnlyID}", "artica_backup"));
        $log_exp = " only for [{$ligne2["sitename"]}] ";
    }
    system_admin_events("Starting executing WebCopy task {$log_exp} pid:{$getmypid}", __FUNCTION__, __FILE__, __LINE__, "webcopy");
    while ($ligne = mysql_fetch_assoc($results)) {
        if ($OnlyID > 0) {
            if ($ligne["ID"] != $OnlyID) {
                continue;
            }
        }
        $t = time();
        $count++;
        $workingdir = $ligne["workingdir"];
        $sitename = $ligne["sitename"];
        $maxsize = $ligne["maxsize"];
        $minrate = $ligne["minrate"];
        $maxfilesize = $ligne["maxfilesize"];
        $maxsitesize = $ligne["maxsitesize"];
        $maxfilesize = $maxfilesize * 1000;
        $maxsitesize = $maxsitesize * 1000;
        $minrate = $minrate * 1000;
        $update = null;
        $resultsCMD = array();
        if (!is_dir($workingdir)) {
            @mkdir($workingdir, 0755, true);
        }
        if (is_file("{$workingdir}/hts-cache")) {
            $update = " --update";
        }
        $cmdline = "{$httrack} \"{$sitename}\" --quiet{$update} --max-files={$maxfilesize} --max-size={$maxsitesize} --max-rate={$minrate} -O \"{$workingdir}\" 2>&1";
        if ($GLOBALS["VERBOSE"]) {
            echo "{$cmdline}\n";
        }
        exec($cmdline, $resultsCMD);
        if ($GLOBALS["VERBOSE"]) {
            echo @implode("\n", $resultsCMD);
        }
        $dirsize = $unix->DIRSIZE_BYTES($workingdir);
        $took = $unix->distanceOfTimeInWords($t, time(), true);
        $dirsizeText = round($dirsize / 1024 / 1000, 2);
        system_admin_events("{$sitename} scrapped took {$took} size={$dirsizeText} MB", __FUNCTION__, __FILE__, __LINE__, "webcopy");
        $q->QUERY_SQL("UPDATE httrack_sites SET size='{$dirsize}' WHERE ID={$ligne["ID"]}", "artica_backup");
    }
    $took = $unix->distanceOfTimeInWords($t1, time(), true);
    system_admin_events("{$count} web sites scrapped took {$took}", __FUNCTION__, __FILE__, __LINE__, "webcopy");
}
示例#22
0
function parse($filename)
{
    $unix = new unix();
    $LastScannLine = 0;
    $GLOBALS["MYSQL_CATZ"] = new mysql_catz();
    $GLOBALS["SQUID_FAMILY_CLASS"] = new squid_familysite();
    if (!isset($GLOBALS["MYHOSTNAME"])) {
        $unix = new unix();
        $GLOBALS["MYHOSTNAME"] = $unix->hostname_g();
    }
    $filesource = dirname(__FILE__) . "/ressources/conf/upload/{$filename}";
    if (!is_file($filesource)) {
        build_progress(110, "{$filename} no such file");
        return;
    }
    $tmpfile = $unix->FILE_TEMP();
    if (!@copy($filesource, $tmpfile)) {
        @unlink($filesource);
        build_progress(110, "{$filename} -> {$tmpfile} {failed}");
        return;
    }
    @unlink($filesource);
    $SumOflines = $unix->COUNT_LINES_OF_FILE($tmpfile);
    echo "{$tmpfile} {$SumOflines} lines\n";
    $handle = @fopen($tmpfile, "r");
    if (!$handle) {
        echo "Fopen failed on {$tmpfile}\n";
        build_progress(110, "{$tmpfile} {failed}");
        @unlink($tmpfile);
        return false;
    }
    $c = 0;
    $d = 0;
    $e = 0;
    $prc = 0;
    $prc_text = 0;
    $mysql_first_time = 0;
    $SITES = array();
    $NOT_CATEGORIZED = array();
    $CATEGORIZED = array();
    $RQS = array();
    $IPClass = new IP();
    $FIRSTTIME = 0;
    $LASTTIME = 0;
    $TIME1 = time();
    while (!feof($handle)) {
        $c++;
        $d++;
        $e++;
        $prc = $c / $SumOflines;
        $prc = round($prc * 100);
        $buffer = trim(fgets($handle));
        if ($buffer == null) {
            continue;
        }
        $stats_sites = count($SITES);
        $stats_categorized = count($CATEGORIZED);
        $stats_not_categorized = count($NOT_CATEGORIZED);
        if (!isset($GLOBALS["LAST_PRC"])) {
            build_progress($prc, "{$c}/{$SumOflines} {please_wait}");
            $GLOBALS["LAST_PRC"] = $prc;
        }
        if ($prc > 5) {
            if ($prc < 95) {
                if ($GLOBALS["LAST_PRC"] != $prc) {
                    $array_load = sys_getloadavg();
                    $internal_load = $array_load[0];
                    $mem = round(memory_get_usage() / 1024 / 1000, 2);
                    echo "Load: {$internal_load}, Memory {$mem}MB\n";
                    echo "Categorized: " . FormatNumber($stats_categorized) . "\n";
                    echo "Unknown....: " . FormatNumber($stats_not_categorized) . "\n";
                    echo "Web sites..: " . FormatNumber($stats_sites) . "\n";
                    build_progress($prc, FormatNumber($c) . "/" . FormatNumber($SumOflines) . " {please_wait} - {$mem}MB {memory}");
                    $GLOBALS["LAST_PRC"] = $prc;
                }
            }
        }
        $array = parseAccessLine($buffer);
        if (count($array) == 0) {
            continue;
        }
        $TIME = $array["TIME"];
        $LASTTIME = $TIME;
        if ($FIRSTTIME == 0) {
            $FIRSTTIME = $TIME;
        }
        $CATEGORY = $array["CATEGORY"];
        $FAMILYSITE = $array["FAMILYSITE"];
        $SIZE = intval($array["SIZE"]);
        if ($IPClass->isIPAddress($FAMILYSITE)) {
            if (!isset($IPADDRESSES[$FAMILYSITE]["RQS"])) {
                $IPADDRESSES[$FAMILYSITE]["RQS"] = 1;
                $IPADDRESSES[$FAMILYSITE]["SIZE"] = 0;
                $IPADDRESSES[$FAMILYSITE]["CATEGORY"] = $CATEGORY;
            } else {
                $IPADDRESSES[$FAMILYSITE]["RQS"] = $IPADDRESSES[$FAMILYSITE]["RQS"] + 1;
                $IPADDRESSES[$FAMILYSITE]["SIZE"] = $IPADDRESSES[$FAMILYSITE]["SIZE"] + $SIZE;
            }
            continue;
        }
        if (!isset($SITES[$FAMILYSITE])) {
            $SITES[$FAMILYSITE] = 0;
        }
        if (!isset($RQS[$FAMILYSITE])) {
            $RQS[$FAMILYSITE] = 0;
        }
        $SITES[$FAMILYSITE] = $SITES[$FAMILYSITE] + $SIZE;
        $RQS[$FAMILYSITE] = $RQS[$FAMILYSITE] + 1;
        if ($CATEGORY != null) {
            $CATEGORIZED[$FAMILYSITE] = $CATEGORY;
            continue;
        }
        $NOT_CATEGORIZED[$FAMILYSITE] = true;
    }
    fclose($handle);
    @unlink($tmpfile);
    build_progress(91, "{building_report}");
    $TIME2 = time();
    $stats_sites = count($SITES);
    $stats_categorized = count($CATEGORIZED);
    $stats_not_categorized = count($NOT_CATEGORIZED);
    $ARRAY["DURATION"] = $unix->distanceOfTimeInWords($TIME1, $TIME2);
    $ARRAY["SumOflines"] = $SumOflines;
    $ARRAY["stats_sites"] = $stats_sites;
    $ARRAY["stats_ip"] = count($IPADDRESSES);
    $ARRAY["firsttime"] = $FIRSTTIME;
    $ARRAY["lasttime"] = $LASTTIME;
    $ARRAY["stats_categorized"] = $stats_categorized;
    $ARRAY["stats_not_categorized"] = $stats_not_categorized;
    build_progress(92, "{building_report}");
    $CSV1[] = array("website", "size", "requests");
    while (list($familysite, $ligne) = each($NOT_CATEGORIZED)) {
        $CSV1[] = array($familysite, $SITES[$familysite], $RQS[$familysite]);
    }
    build_progress(95, "{building_report}");
    $CSV2[] = array("website", "category", "size", "requests");
    while (list($familysite, $category) = each($CATEGORIZED)) {
        $CSV2[] = array($familysite, $category, $SITES[$familysite], $RQS[$familysite]);
    }
    build_progress(97, "{building_report}");
    $CSV3[] = array("Public IP addresses", "category", "size", "requests");
    while (list($ip, $ARRAYIPS) = each($IPADDRESSES)) {
        $category = $ARRAYIPS["CATEGORY"];
        $size = $ARRAYIPS["SIZE"];
        $RQS = $ARRAYIPS["RQS"];
        $CSV3[] = array($ip, $category, $size, $RQS);
    }
    build_progress(99, "{saving_reports}");
    outputCSV($CSV1, "/usr/share/artica-postfix/ressources/logs/notcategorized.csv");
    outputCSV($CSV2, "/usr/share/artica-postfix/ressources/logs/categorized.csv");
    outputCSV($CSV3, "/usr/share/artica-postfix/ressources/logs/ipcategorized.csv");
    @file_put_contents("/usr/share/artica-postfix/ressources/logs/categorized.array", serialize($ARRAY));
    @chmod("/usr/share/artica-postfix/ressources/logs/notcategorized.csv", 0755);
    @chmod("/usr/share/artica-postfix/ressources/logs/ipcategorized.csv", 0755);
    @chmod("/usr/share/artica-postfix/ressources/logs/categorized.csv", 0755);
    @chmod("/usr/share/artica-postfix/ressources/logs/categorized.array", 0755);
    build_progress(100, "{done}");
}
示例#23
0
function runProc($norestart = false)
{
    $unix = new unix();
    $sock = new sockets();
    $t = time();
    $timeFile = "/etc/artica-postfix/pids/" . basename(__FILE__) . ".time";
    $timePID = "/etc/artica-postfix/pids/" . basename(__FILE__) . ".pid";
    $EnableBandwithCalculation = $sock->GET_INFO("EnableBandwithCalculation");
    if (!is_numeric($EnableBandwithCalculation)) {
        $EnableBandwithCalculation = 1;
    }
    $GetInstances = GetInstances();
    if ($GetInstances > 0) {
        system_admin_events("{$GetInstances} instance(s) already running.. aborting...", __FUNCTION__, __FILE__, __LINE__, "testspeed");
        return;
    }
    if ($EnableBandwithCalculation == 0) {
        system_admin_events("Feature disabled trough the Interface (EnableBandwithCalculation) you have to disable the schedule too...", __FUNCTION__, __FILE__, __LINE__, "testspeed");
        return;
    }
    if (!$GLOBALS["FORCE"]) {
        $pid = $unix->get_pid_from_file($timePID);
        if ($unix->process_exists($pid, basename(__FILE__))) {
            $timexec = $unix->PROCCESS_TIME_MIN($pid);
            if ($timexec < 30) {
                system_admin_events("Already instance executed pid {$pid} since {$timexec}Mn", __FUNCTION__, __FILE__, __LINE__, "testspeed");
                return;
            } else {
                $kill = $unix->find_program("kill");
                unix_system_kill_force($pid);
                system_admin_events("Instance pid {$pid} since {$timexec}Mn was killed", __FUNCTION__, __FILE__, __LINE__, "testspeed");
            }
        }
        $ExecTimefile = $unix->file_time_min($timeFile);
        if ($ExecTimefile < 30) {
            system_admin_events("Must run minimal 30mn ({$ExecTimefile}Mn), aborting", __FUNCTION__, __FILE__, __LINE__, "testspeed");
            return;
        }
        @unlink($timeFile);
        @file_put_contents($timeFile, time());
        @file_put_contents($timePID, getmypid());
    }
    $python = $unix->find_program("python");
    if (!is_file($python)) {
        system_admin_events("python, no such binary", __FUNCTION__, __FILE__, __LINE__, "testspeed");
        return;
    }
    $speedDNum = 0;
    $speedYNum = 0;
    $IP = null;
    $ISP = null;
    if (!is_file("/usr/lib/pyshared/python2.7/lxml/etree.so")) {
        $unix->DEBIAN_INSTALL_PACKAGE("python-lxml");
    }
    @chdir("/usr/share/artica-postfix/bin");
    exec("{$python} /usr/share/artica-postfix/bin/tespeed.py -dt 1 -ut 1 2>&1", $results);
    @chdir("/root");
    while (list($index, $line) = each($results)) {
        if ($GLOBALS["VERBOSE"]) {
            echo "{$line}\n";
        }
        if (preg_match("#IP:\\s+(.+?);.*?ISP:\\s+(.+?)\$#", $line, $re)) {
            $IP = trim($re[1]);
            $ISP = trim($re[2]);
        }
        if (preg_match("#No module named lxml#", $line)) {
            system_admin_events("Error,{$line}", __FUNCTION__, __FILE__, __LINE__, "testspeed");
            if (!$norestart) {
                install_lxml();
            }
            return;
        }
        if (preg_match("#No module named argparse#", $line)) {
            system_admin_events("Error,{$line}", __FUNCTION__, __FILE__, __LINE__, "testspeed");
            if (!$norestart) {
                install_argparse();
            }
            return;
        }
        if (preg_match("#Download speed:\\s+([0-9\\.]+)\\s+Mbit#", $line, $re)) {
            $speedDNum = $re[1];
            $speedDNum = $speedDNum * 1024;
        }
        if (preg_match("#Upload speed:\\s+([0-9\\.]+)\\s+Mbit#", $line, $re)) {
            $speedUNum = $re[1];
            $speedYNum = $speedUNum * 1024;
        }
    }
    $took = $unix->distanceOfTimeInWords($t, time(), true);
    if ($GLOBALS["VERBOSE"]) {
        echo "ISP:{$ISP} , IP:{$IP} Download: {$speedDNum} Kbi/s upload {$speedYNum} Kbi/s\n";
    }
    if ($speedDNum == 0) {
        return;
    }
    if ($speedYNum == 0) {
        return;
    }
    system_admin_events("ISP:{$ISP} , IP:{$IP} Download: {$speedDNum} Kbi/s upload {$speedYNum} Kbi/s took {$took}", __FUNCTION__, __FILE__, __LINE__, "testspeed");
    $array["ISP"] = $ISP;
    $array["PUBLIC_IP"] = $IP;
    $array["DOWNLOAD"] = $speedDNum;
    $array["UPLOAD"] = $speedYNum;
    $array["DATE"] = time();
    @mkdir("/usr/share/artica-postfix/ressources/web/cache1", 0777, true);
    @file_put_contents("/usr/share/artica-postfix/ressources/web/cache1/bandwith.stats", serialize($array));
    if (strlen($speedDNum) > 2) {
        $q = new mysql();
        $q->BuildTables();
        $q->QUERY_SQL("INSERT INTO speedtests (zDate,ISP,download,upload) VALUES (NOW(),'{$ISP}','{$speedDNum}','{$speedYNum}')", "artica_events");
        if (!$q->ok) {
            system_admin_events("Fatal error, {$q->mysql_error}", __FUNCTION__, __FILE__, __LINE__, "testspeed");
        }
    }
    $q->QUERY_SQL("DELETE FROM speedtests WHERE zDate<DATE_SUB(NOW(),INTERVAL 35 DAY)", "artica_events");
}
示例#24
0
function UFDBGUARD_DOWNLOAD_ALL_CATEGORIES()
{
    include_once dirname(__FILE__) . "/ressources/class.ccurl.inc";
    $unix = new unix();
    $sock = new sockets();
    $tar = $unix->find_program("tar");
    $rm = $unix->find_program("rm");
    $RemoteStatisticsApplianceSettings = unserialize(base64_decode($sock->GET_INFO("RemoteStatisticsApplianceSettings")));
    if (!is_numeric($RemoteStatisticsApplianceSettings["SSL"])) {
        $RemoteStatisticsApplianceSettings["SSL"] = 1;
    }
    if (!is_numeric($RemoteStatisticsApplianceSettings["PORT"])) {
        $RemoteStatisticsApplianceSettings["PORT"] = 9000;
    }
    $GLOBALS["REMOTE_SSERVER"] = $RemoteStatisticsApplianceSettings["SERVER"];
    $GLOBALS["REMOTE_SPORT"] = $RemoteStatisticsApplianceSettings["PORT"];
    $GLOBALS["REMOTE_SSL"] = $RemoteStatisticsApplianceSettings["SSL"];
    if ($GLOBALS["REMOTE_SSL"] == 1) {
        $refix = "https";
    } else {
        $refix = "http";
    }
    $uri = "{$refix}://{$GLOBALS["REMOTE_SSERVER"]}:{$GLOBALS["REMOTE_SPORT"]}/ressources/databases/blacklist.tar.gz";
    $curl = new ccurl($uri, true);
    if (!$curl->GetFile("/tmp/blacklist.tar.gz")) {
        ufdbguard_admin_events("Failed to download blacklist.tar.gz aborting `{$curl->error}`", __FUNCTION__, __FILE__, __LINE__, "global-compile");
        return;
    }
    $t = time();
    shell_exec("{$rm} -rf /var/lib/squidguard/*");
    exec("{$tar} -xf /tmp/blacklist.tar.gz -C /var/lib/squidguard/ 2>&1", $results);
    $ttook = $unix->distanceOfTimeInWords($t, time(), true);
    ufdbguard_admin_events("Extracting blacklist.tar.gz took {$ttook} `" . @implode("\n", $results), __FUNCTION__, __FILE__, __LINE__, "global-compile");
    $array = $unix->dirdir("/var/lib/squidguard");
    $GLOBALS["NORESTART"] = true;
    while (list($index, $directoryPath) = each($array)) {
        if (!is_file("{$directoryPath}/domains.ufdb")) {
            UFDBGUARD_COMPILE_SINGLE_DB("{$directoryPath}/domains");
        }
    }
    build_ufdbguard_HUP();
}
示例#25
0
function scanarp_mysql()
{
    $unix = new unix();
    $t = time();
    if (systemMaxOverloaded()) {
        return;
    }
    $pidfile = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".pid";
    $unix = new unix();
    $me = basename(__FILE__);
    $pid = @file_get_contents($pidfile);
    if ($unix->process_exists($pid, $me)) {
        if ($GLOBALS["VERBOSE"]) {
            echo " --> Already executed.. {$pid} aborting the process\n";
        }
        system_admin_events("--> Already executed.. {$pid} aborting the process", __FUNCTION__, __FILE__, __LINE__, "network");
        die;
    }
    $sock = new sockets();
    $EnableArpDaemon = $sock->GET_INFO("EnableArpDaemon");
    $ArpdKernelLevel = $sock->GET_INFO("ArpdKernelLevel");
    if (!is_numeric($EnableArpDaemon)) {
        $EnableArpDaemon = 1;
    }
    $articastatus_pidfile = "/etc/artica-postfix/exec.status.php.pid";
    $pid = $unix->get_pid_from_file($articastatus_pidfile);
    if (!$unix->process_exists($pid)) {
        syslog_status("artica status doesn't run, start it, old pid was: {$pid}");
        shell_exec("/etc/init.d/artica-status start");
    }
    $list = $unix->PIDOF_PATTERN_ALL($me);
    if (count($list) > 2) {
        system_admin_events("--> Already executed.." . count($list) . " Processes executed");
        die;
    }
    @file_put_contents($pidfile, getmypid());
    $arpbin = $unix->find_program("arp");
    $arpdbin = $unix->find_program("arpd");
    if (!is_file($arpbin)) {
        echo "arp, no such binary...\n";
        return;
    }
    exec("{$arpbin} -a 2>&1", $results);
    if ($GLOBALS["VERBOSE"]) {
        echo "{$arpbin} -a 2>&1\n";
    }
    $prefix = "INSERT INTO arpcache (`mac`,`ipaddr`,`hostname`,`HWtype`,`iface`) VALUES ";
    while (list($num, $ligne) = each($results)) {
        if (preg_match("#^(.+?)\\s+\\((.+?)\\)\\s+.+?\\s+(.+?)\\s+\\[(.+?)\\]\\s+.+?\\s+(.+)#", $ligne, $re)) {
            $mac = trim($re[3]);
            $hostname = trim($re[1]);
            $ipaddr = trim($re[2]);
            $HWtype = trim($re[4]);
            $iface = trim($re[5]);
            if ($GLOBALS["VERBOSE"]) {
                echo "MATCH `{$ligne}` '{$mac}','{$ipaddr}','{$hostname}','{$HWtype}','{$iface}'\n";
            }
            $f[] = "('{$mac}','{$ipaddr}','{$hostname}','{$HWtype}','{$iface}')";
            continue;
        }
        if (preg_match("#^([a-z0-9\\.\\-\\_\\?]+)\\s+\\((.+?)\\).+?incomplete.+?[a-z]+\\s+(.+)\$#", $ligne, $re)) {
            $mac = null;
            $hostname = trim($re[1]);
            $ipaddr = trim($re[2]);
            $HWtype = null;
            $iface = trim($re[3]);
            if ($GLOBALS["VERBOSE"]) {
                echo "MATCH `{$ligne}` '{$mac}','{$ipaddr}','{$hostname}','{$HWtype}','{$iface}'\n";
            }
            $f[] = "('{$mac}','{$ipaddr}','{$hostname}','{$HWtype}','{$iface}')";
            continue;
        }
        if ($GLOBALS["VERBOSE"]) {
            echo "No match `{$ligne}`\n";
        }
    }
    if (is_file($arpdbin)) {
        $results = array();
        exec("{$arpdbin} -l 2>&1", $results);
        while (list($num, $ligne) = each($results)) {
            if (preg_match("#^[0-9]+\\s+([0-9\\.]+)\\s+(.+)#", $ligne, $re)) {
                $mac = trim($re[2]);
                if (preg_match("#FAILED:#", $mac)) {
                    $mac = null;
                }
                $hostname = null;
                $ipaddr = trim($re[1]);
                $HWtype = null;
                $iface = "arpd";
                $f[] = "('{$mac}','{$ipaddr}','{$hostname}','{$HWtype}','{$iface}')";
                continue;
            }
        }
    }
    if (count($f) > 0) {
        $q = new mysql();
        $q->QUERY_SQL("TRUNCATE TABLE `arpcache`", "artica_backup");
        if ($GLOBALS["VERBOSE"]) {
            echo count($f) . " entries\n";
        }
        $sql = $prefix . @implode(",", $f);
        $q->QUERY_SQL($sql, "artica_backup");
        if (!$q->ok) {
            system_admin_events("Fatal, {$q->mysql_error}", __FUNCTION__, __FILE__, __LINE__, "network");
            return;
        }
        $took = $unix->distanceOfTimeInWords($t, time(), true);
        system_admin_events(count($f) . " ARP entries added into MySQL server", __FUNCTION__, __FILE__, __LINE__, "network");
    }
}
示例#26
0
function Zexec()
{
    $sock = new sockets();
    $unix = new unix();
    $pidfile = "/etc/artica-postfix/pids/" . basename(__FILE__) . ".pid";
    if (!$GLOBALS["FORCE"]) {
        $pid = $unix->get_pid_from_file($pidfile);
        if ($unix->process_exists($pid)) {
            $timeTTL = $unix->PROCCESS_TIME_MIN($pid);
            system_admin_events("Already running PID {$pid} since {$timeTTL}Mn", __FUNCTION__, __FILE__, __LINE__, "antivirus");
            return;
        }
        $rm = $unix->find_program("rm");
        $timepid = $unix->file_time_min($pidfile);
        if ($timepid < 120) {
            system_admin_events("Require 120Mn minimal (current: {$timepid})", __FUNCTION__, __FILE__, __LINE__, "antivirus");
            return;
        }
    }
    @unlink($pidfile);
    @file_put_contents($pidfile, getmypid());
    $curlcmdline = null;
    $puser = null;
    $ini = new Bs_IniHandler();
    $datas = $sock->GET_INFO("ArticaProxySettings");
    if (trim($datas) != null) {
        $ini->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") {
            if ($ArticaProxyServerUserPassword != null) {
                $puser = "******";
            }
            $curlcmdline = " --proxy {$ArticaProxyServerName}:{$ArticaProxyServerPort}{$puser}";
        }
    }
    $f[] = "# This is the /etc/scamp/default file.";
    $f[] = "# Created " . date('F') . " " . date('d') . ", " . date('Y') . " @ " . date('H:i:s') . "";
    $f[] = "";
    $f[] = "SCAMP_VERSION=5.3b";
    $f[] = "CLAMAV_DB=/var/lib/clamav";
    $f[] = "T_DIR=/var/lib/clamav/tmp";
    $f[] = "C_GROUP=clamav";
    $f[] = "C_PID=/var/run/clamav/clamd.pid";
    $f[] = "C_USER=clamav";
    $f[] = "GET_LDB=1";
    $f[] = "GET_MALWARE=1";
    $f[] = "GET_MSRBL=0";
    $f[] = "GET_SANE=1";
    $f[] = "GET_SECURITE=4";
    $f[] = "GET_WINNOW=1";
    $f[] = "gpg_key_url=http://www.sanesecurity.net/publickey.gpg";
    $f[] = "L_TYPE=0";
    $f[] = "MK_LOG=1";
    $f[] = "MSRBL=rsync://rsync.mirror.msrbl.com/msrbl/";
    $f[] = "msrbl_Images=MSRBL-Images.hdb";
    $f[] = "msrbl_SPAM=MSRBL-SPAM.ndb";
    $f[] = "msrbl_SPAM_CR=MSRBL-SPAM-CR.ndb";
    $f[] = "MSR_DIR=/var/lib/clamav/tmp/msr";
    $f[] = "MW_DIR=/var/lib/clamav/tmp/malware";
    $f[] = "MW_FILE=mbl.ndb";
    $f[] = "MW_URL=http://www.malwarepatrol.com.br/cgi/submit?action=list_clamav_ext";
    $f[] = "RELOAD=0";
    $f[] = "REST=1";
    $f[] = "SANE=rsync://rsync.sanesecurity.net/sanesecurity";
    $f[] = "SANE_DB=/var/lib/clamav/tmp/sane";
    $f[] = "SI_DIR=/var/lib/clamav/tmp/securite";
    $f[] = "SYS_LOG=1";
    $f[] = "WPC=3";
    $f[] = "W_SUM=0";
    if (!is_dir("/etc/scamp")) {
        @mkdir("/etc/scamp", 0755, true);
    }
    if (!is_dir("/var/lib/clamav")) {
        @mkdir("/var/lib/clamav", 0755, true);
    }
    @file_put_contents("/etc/scamp/default", @implode("\n", $f));
    $t = time();
    $l = explode("\n", @file_get_contents("/usr/share/artica-postfix/bin/scamp.sh"));
    while (list($num, $line) = each($l)) {
        if (preg_match("#^CURL_PROXY_CMD#", $line)) {
            $l[$num] = "CURL_PROXY_CMD=\"{$curlcmdline}\"";
        }
    }
    @file_put_contents("/usr/share/artica-postfix/bin/scamp.sh", @implode("\n", $l));
    exec("/usr/share/artica-postfix/bin/scamp.sh -L -q -R");
    $took = $unix->distanceOfTimeInWords($t, time());
    $content = @file_get_contents("/var/log/scamp.log");
    system_admin_events("Update done, took {$took}\n{$content}", __FUNCTION__, __FILE__, __LINE__, "antivirus");
    @unlink("/var/log/scamp.log");
    if (is_file("/var/run/c-icap/c-icap.ctl")) {
        shell_exec("echo -n \"srv_clamav:dbreload\" > /var/run/c-icap/c-icap.ctl");
        shell_exec("echo -n \"virus_scan:dbreload\" > /var/run/c-icap/c-icap.ctl");
    }
}
function last_days()
{
    $pidfile = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".pid";
    $timefile = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".time";
    if ($GLOBALS["VERBOSE"]) {
        echo "last_days Loading done...\nTimeFile:{$timefile}\n";
    }
    $unix = new unix();
    if (!$GLOBALS["VERBOSE"]) {
        if (SquidStatisticsTasksOverTime()) {
            stats_admin_events(1, "Statistics overtime... Aborting", null, __FILE__, __LINE__);
            return;
        }
    }
    $pid = @file_get_contents($pidfile);
    if (!$GLOBALS["FORCE"]) {
        if ($pid < 100) {
            $pid = null;
        }
        if ($unix->process_exists($pid, basename(__FILE__))) {
            if ($GLOBALS["VERBOSE"]) {
                echo "Already executed pid {$pid}\n";
            }
            return;
        }
        $timeexec = $unix->file_time_min($timefile);
        if ($timeexec < 7200) {
            if ($GLOBALS["VERBOSE"]) {
                echo "{$timeexec} <> 7200...\n";
            }
            return;
        }
    }
    $q = new mysql_squid_builder();
    $mypid = getmypid();
    @file_put_contents($pidfile, $mypid);
    @file_put_contents($timefile, time());
    $current_table = date("Ymd") . "_hour";
    $t = time();
    $sql = "SELECT DATE_FORMAT(zDate,'%Y%m%d') AS `suffix` FROM tables_day \n\t\t\tWHERE DAY(zDate)<DAY(NOW()) AND YEAR(zDate) = YEAR(NOW()) AND MONTH(zDate) = MONTH(NOW()) AND zDate>DATE_SUB(NOW(),INTERVAL 7 DAY)";
    $results = $q->QUERY_SQL($sql);
    $num = mysql_num_rows($results);
    if ($num == 0) {
        return;
    }
    while ($ligne = @mysql_fetch_array($results, MYSQL_ASSOC)) {
        $current_table = $ligne["suffix"] . "_hour";
        if (!$q->TABLE_EXISTS($current_table)) {
            if ($GLOBALS["VERBOSE"]) {
                echo "{$current_table} no such table\n";
            }
            continue;
        }
        if ($GLOBALS["VERBOSE"]) {
            echo "Processing {$current_table}\n";
        }
        if (!$GLOBALS["VERBOSE"]) {
            if (SquidStatisticsTasksOverTime()) {
                stats_admin_events(1, "Statistics overtime... Aborting", null, __FILE__, __LINE__);
                return;
            }
        }
        _xprocess_table($current_table, true);
        $f[] = $current_table;
    }
    $took = $unix->distanceOfTimeInWords($t, time(), true);
    stats_admin_events(2, "Processing categorization of {$num} tables {$took}", @implode("\n", $f), __FILE__, __LINE__);
}
示例#28
0
function WATCHDOG_MYSQL()
{
    $unix = new unix();
    $mysqladmin = $unix->find_program("mysqladmin");
    $pidfile = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".pid";
    $pidTime = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".time";
    $pid = @file_get_contents($pidfile);
    $kill = $unix->find_program("kill");
    if (!$GLOBALS["FORCE"]) {
        $LastExec = $unix->file_time_min($pidTime);
        if ($LastExec < 5) {
            return;
        }
        if ($unix->process_exists($pid, basename(__FILE__))) {
            $time = $unix->PROCCESS_TIME_MIN($pid);
            if ($time < 5) {
                return;
            }
            unix_system_kill_force($pid);
        }
    }
    @unlink($pidfile);
    @unlink($pidTime);
    @file_put_contents($pidfile, getmypid());
    @file_put_contents($pidTime, time());
    $socket = "/var/run/mysqld/mysqld.sock";
    $php5 = $unix->LOCATE_PHP5_BIN();
    $nohup = $unix->find_program("nohup");
    if (!$unix->is_socket($socket)) {
        mysql_admin_mysql(0, "{$socket}, no such file, restarting MySQL service", null, __FILE__, __LINE__);
        system_admin_events("{$socket}, no such file, restarting MySQL service", __FUNCTION__, __FILE__, __LINE__, "mysql");
        shell_exec("{$nohup} /etc/init.d/mysql start >/dev/null 2>&1 &");
        return;
    }
    $MYSQLPid = PID_NUM();
    if (!$unix->process_exists($MYSQLPid)) {
        mysql_admin_mysql(0, "Error, MySQL service is not running", null, __FILE__, __LINE__);
        system_admin_events("Error, \"MySQL service is not running\", Starting MySQL service", __FUNCTION__, __FILE__, __LINE__, "mysql");
        shell_exec("{$nohup} /etc/init.d/mysql start >/dev/null 2>&1 &");
        return;
    }
    $RunningScince = $unix->PROCCESS_TIME_MIN($MYSQLPid);
    if ($GLOBALS["VERBOSE"]) {
        echo "MySQL PID: {$MYSQLPid} running since \"{$RunningScince}mn\"\n";
    }
    if (!is_file($mysqladmin)) {
        return;
    }
    $q = new mysql();
    $cmds[] = $mysqladmin;
    $cmds[] = "--user={$q->mysql_admin}";
    if ($q->mysql_password != null) {
        $password = $unix->shellEscapeChars($q->mysql_password);
        $cmds[] = "--password={$password}";
    }
    $cmds[] = "--socket={$socket}";
    $cmds[] = "processlist";
    $cmd = @implode(" ", $cmds) . " 2>&1";
    exec("{$cmd}", $results);
    if ($GLOBALS["VERBOSE"]) {
        echo "Receive: " . count($results) . " rows\n";
    }
    while (list($num, $line) = each($results)) {
        if (strpos($line, "-----------------------------") > 0) {
            continue;
        }
        if (preg_match("#show processlist#", $line)) {
            continue;
        }
        if (preg_match("#Can't connect to local MySQL server through socket#", $line)) {
            mysql_admin_mysql(0, "Error, Can't connect to local MySQL server through socket", $line, __FILE__, __LINE__);
            system_admin_events("Error, \"Can't connect to local MySQL server through socket\", restarting MySQL service", __FUNCTION__, __FILE__, __LINE__, "mysql");
            shell_exec("{$nohup} /etc/init.d/mysql restart --framework=" . __FILE__ . " >/dev/null 2>&1 &");
            return;
        }
        if (preg_match("#error:\\s+'(.*?)'#", $line, $re)) {
            mysql_admin_mysql(0, "Error, {$re[1]} restarting MySQL service", $line, __FILE__, __LINE__);
            system_admin_events("Error, \"{$re[1]}\", restarting MySQL service", __FUNCTION__, __FILE__, __LINE__, "mysql");
            shell_exec("{$nohup} /etc/init.d/mysql restart --framework=" . __FILE__ . " >/dev/null 2>&1 &");
            return;
        }
        if (preg_match("#\\|\\s+([0-9]+)\\s+\\|\\s+(.*?)\\s+\\|\\s+(.*?)\\s+\\|\\s+(.*?)\\s+\\|\\s+(Query|Sleep)\\s+\\|\\s+([0-9]+)\\s+#", $line, $re)) {
            $time = time();
            $seconds = $time - $re[6];
            $dist = $unix->distanceOfTimeInWords($seconds, $time);
            if ($GLOBALS["VERBOSE"]) {
                echo "\"Process {$re[1]}/{$re[5]}\" running since {$dist}\n";
            }
            $countPid[$re[1]] = $re[5];
            continue;
        }
        if ($GLOBALS["VERBOSE"]) {
            echo "\"{$line}\"\n";
        }
    }
    if ($GLOBALS["VERBOSE"]) {
        echo count($countPid) . " Threads...\n";
    }
}
示例#29
0
function create_package($t)
{
    $unix = new unix();
    $wget = $unix->find_program("wget");
    $tar = $unix->find_program("tar");
    $rm = $unix->find_program("rm");
    $cp = $unix->find_program("cp");
    $Architecture = Architecture();
    $version = squid_version();
    shell_exec("wget http://www.articatech.net/download/anthony-icons.tar.gz -O /tmp/anthony-icons.tar.gz");
    @mkdir("/usr/share/squid3/icons", 0755, true);
    shell_exec("tar -xf /tmp/anthony-icons.tar.gz -C /usr/share/squid3/icons/");
    shell_exec("/bin/chown -R squid:squid /usr/share/squid3/icons/");
    mkdir("/root/squid-builder/usr/share/squid3", 0755, true);
    mkdir("/root/squid-builder/etc/squid3", 0755, true);
    mkdir("/root/squid-builder/lib/squid3", 0755, true);
    mkdir("/root/squid-builder/usr/sbin", 0755, true);
    mkdir("/root/squid-builder/usr/bin", 0755, true);
    mkdir("/root/squid-builder/usr/share/squid-langpack", 0755, true);
    shell_exec("{$cp} -rf /usr/share/squid3/* /root/squid-builder/usr/share/squid3/");
    shell_exec("{$cp} -rf /etc/squid3/* /root/squid-builder/etc/squid3/");
    shell_exec("{$cp} -rf /lib/squid3/* /root/squid-builder/lib/squid3/");
    shell_exec("{$cp} -rf /usr/share/squid-langpack/* /root/squid-builder/usr/share/squid-langpack/");
    shell_exec("{$cp} -rf /usr/sbin/squid /root/squid-builder/usr/sbin/squid");
    shell_exec("{$cp} -rf /usr/bin/purge /root/squid-builder/usr/bin/purge");
    shell_exec("{$cp} -rf /usr/bin/squidclient /root/squid-builder/usr/bin/squidclient");
    shell_exec("{$cp} -rf /usr/bin/mysar /root/squid-builder/usr/bin/mysar");
    echo "Compile SARG....\n";
    compile_sarg();
    if ($Architecture == 64) {
        $Architecture = "x64";
    }
    if ($Architecture == 32) {
        $Architecture = "i386";
    }
    echo "Compile Arch {$Architecture} v:{$version}\n";
    chdir("/root/squid-builder");
    $version = squid_version();
    echo "Compressing....\n";
    shell_exec("{$tar} -czf squid32-{$Architecture}-{$version}.tar.gz *");
    system_admin_events("/root/squid-builder/squid32-{$Architecture}-{$version}.tar.gz  ready...", __FUNCTION__, __FILE__, __LINE__);
    if (is_file("/root/ftp-password")) {
        echo "/root/squid-builder/squid32-{$Architecture}-{$version}.tar.gz is now ready to be uploaded\n";
        shell_exec("curl -T /root/squid-builder/squid32-{$Architecture}-{$version}.tar.gz ftp://www.articatech.net/download/ --user " . @file_get_contents("/root/ftp-password"));
        system_admin_events("Uploading squid32-{$Architecture}-{$version}.tar.gz done.", __FUNCTION__, __FILE__, __LINE__);
        if (is_file("/root/rebuild-artica")) {
            shell_exec("{$wget} \"" . @file_get_contents("/root/rebuild-artica") . "\" -O /tmp/rebuild.html");
        }
    }
    shell_exec("/etc/init.d/artica-postfix restart squid-cache");
    $took = $unix->distanceOfTimeInWords($t, time(), true);
    system_admin_events("Installing the new squid-cache {$version} success took:{$took}", __FUNCTION__, __FILE__, __LINE__, "software");
}
示例#30
0
function HyperCacheMirror($JustID = 0)
{
    $unix = new unix();
    $httrack = $unix->find_program("httrack");
    if (!is_file($httrack)) {
        if ($GLOBALS["VERBOSE"]) {
            echo "httrack no such binary\n";
        }
        return;
    }
    $q = new mysql_squid_builder();
    if (!$q->FIELD_EXISTS("artica_caches_mirror", "ToDelete")) {
        $sql = "ALTER TABLE `artica_caches_mirror` ADD `ToDelete` SMALLINT(1) NOT NULL DEFAULT '0',ADD INDEX(`ToDelete`)";
        $q->QUERY_SQL($sql);
        if (!$q->ok) {
            squid_admin_enforce(1, "Fatal: MySQL error", $q->mysql_error, __FILE__, __LINE__);
            return;
        }
    }
    if (!$q->FIELD_EXISTS("artica_caches_mirror", "RunEvents")) {
        $sql = "ALTER TABLE `artica_caches_mirror` ADD `RunEvents` TEXT";
        $q->QUERY_SQL($sql);
        if (!$q->ok) {
            squid_admin_enforce(1, "Fatal: MySQL error", $q->mysql_error, __FILE__, __LINE__);
            return;
        }
    }
    $nice = EXEC_NICE();
    $sql = "SELECT * FROM artica_caches_mirror WHERE enabled=1 AND `ToDelete`=0";
    $results = $q->QUERY_SQL($sql);
    if (!$q->ok) {
        squid_admin_enforce(1, "Fatal: MySQL error", $q->mysql_error, __FILE__, __LINE__);
        return;
    }
    if (!isset($GLOBALS["HyperCacheStoragePath"])) {
        $sock = new sockets();
        $GLOBALS["HyperCacheStoragePath"] = $sock->GET_INFO("HyperCacheStoragePath");
        if ($GLOBALS["HyperCacheStoragePath"] == null) {
            $GLOBALS["HyperCacheStoragePath"] = "/home/artica/proxy-cache";
        }
    }
    $t1 = time();
    $count = 0;
    if (mysql_num_rows($results) == 0) {
        return;
    }
    $proxyport = $unix->squid_internal_port();
    $HyperCache = new HyperCache();
    while ($ligne = mysql_fetch_assoc($results)) {
        if ($JustID > 0) {
            if ($ligne["ID"] != $JustID) {
                events("Scrapping rule ID {$ligne["ID"]} !skipped", 0, 2, __LINE__);
                continue;
            }
        }
        $t = time();
        $count++;
        $sitename = $ligne["sitename"];
        $sitename_path = $HyperCache->HyperCacheUriToHostname($sitename);
        $workingdir = $GLOBALS["HyperCacheStoragePath"] . "/mirror/{$sitename_path}";
        $TimeExec = $ligne["TimeExec"];
        $TimeExecLast = $unix->file_time_min("{$workingdir}/TimeExec");
        if (!$GLOBALS["FORCE"]) {
            events("Scrapping {$sitename} require {$TimeExec}mn, current {$TimeExecLast}Mn", 0, 2, __LINE__);
            if ($TimeExecLast < $TimeExec) {
                continue;
            }
        }
        events("Scrapping rule ID {$ligne["ID"]} for {$sitename}", 0, 2, __LINE__);
        $minrate = $ligne["minrate"];
        $maxfilesize = $ligne["maxfilesize"];
        $maxsitesize = $ligne["maxsitesize"];
        $maxfilesize = $maxfilesize * 1000;
        $maxsitesize = $maxsitesize * 1000;
        $minrate = $minrate * 1000;
        $update = null;
        $resultsCMD = array();
        $pidpath = "{$GLOBALS["HyperCacheStoragePath"]}/mirror/{$sitename_path}/hts-in_progress.lock";
        if (!is_dir($workingdir)) {
            @mkdir($workingdir, 0755, true);
        }
        @chown("{$GLOBALS["HyperCacheStoragePath"]}/mirror", "squid");
        @chgrp("{$GLOBALS["HyperCacheStoragePath"]}/mirror", "squid");
        @chown("{$GLOBALS["HyperCacheStoragePath"]}/mirror/{$sitename_path}", "squid");
        @chgrp("{$GLOBALS["HyperCacheStoragePath"]}/mirror/{$sitename_path}", "squid");
        if (is_file($pidpath)) {
            $PID = HyperCacheMirror_pid($pidpath);
            if ($unix->process_exists($PID)) {
                events("Scrapping rule ID {$ligne["ID"]} for {$sitename} Process {$PID} already running since " . $unix->PROCESS_TIME_INT($PID), 0, 2, __LINE__);
                continue;
            }
        }
        @file_put_contents("{$workingdir}/TimeExec", time());
        if (is_file("{$workingdir}/hts-cache")) {
            $update = " --update";
        }
        $cmdline = array();
        $cmdline[] = "{$httrack} \"{$sitename}\" --quiet{$update} -%U squid --proxy 127.0.0.1:{$proxyport}";
        $cmdline[] = "--stay-on-same-domain -u2 -C1 -I0 -N100 --robots=0 --max-files={$maxfilesize}";
        $cmdline[] = "--max-size={$maxsitesize}";
        $cmdline[] = "-O \"{$workingdir}\" 2>&1";
        squid_admin_enforce(2, "Scrapping {$sitename} using proxy 127.0.0.1:{$proxyport}...", null, __FILE__, __LINE__);
        $cmd = @implode(" ", $cmdline);
        if ($GLOBALS["VERBOSE"]) {
            echo "{$cmd}\n";
        }
        exec($cmd, $resultsCMD);
        if ($GLOBALS["VERBOSE"]) {
            echo @implode("\n", $resultsCMD);
        }
        $dirsize = $unix->DIRSIZE_BYTES($workingdir);
        $took = $unix->distanceOfTimeInWords($t, time(), true);
        $dirsizeText = round($dirsize / 1024 / 1000, 2);
        squid_admin_enforce(2, "Mirror on {$sitename} done took {$took} size={$dirsizeText} MB", null, __FILE__, __LINE__);
        $logs = mysql_escape_string2(@file_get_contents("{$workingdir}/hts-log.txt"));
        $q->QUERY_SQL("UPDATE artica_caches_mirror SET \n\t\t\t\tsize='{$dirsize}',`RunEvents`='{$logs}' WHERE ID={$ligne["ID"]}", "artica_backup");
        if (!$q->ok) {
            squid_admin_enforce(1, "MySQL error", $q->mysql_error, __FILE__, __LINE__);
        }
    }
    $took = $unix->distanceOfTimeInWords($t1, time(), true);
    squid_admin_enforce(2, "{$count} web site(s) scrapped took {$took}", null, __FILE__, __LINE__);
}