function sync_policies_group($gpid) { $q = new mysql_meta(); $sql = "SELECT policies.ID FROM policies,metapolicies_link WHERE metapolicies_link.`policy-id`=policies.ID AND metapolicies_link.gpid={$gpid}"; $results = $q->QUERY_SQL($sql); if (!$q->ok) { meta_admin_mysql(0, "Fatal error: Mysql Error", $q->mysql_error, __FILE__, __LINE__); return; } while ($ligne = mysql_fetch_assoc($results)) { meta_events("Building Policy ID: {$ligne["ID"]}"); $content = BuildPolicy($ligne["ID"]); if (!$content) { meta_admin_mysql(0, "Warning invalid Policy {$ligne["ID"]}", "No Array", __FILE__, __LINE__); continue; } Replicate_policy_to_group($gpid, $content); } }
function artica_meta_server_ping_group() { $gpid = $_GET["meta-ping-group"]; $unix = new unix(); $php5 = $unix->LOCATE_PHP5_BIN(); $nohup = $unix->find_program("nohup"); meta_events("Framework, send ping to Group {$gpid}"); $cmd = trim("{$nohup} {$php5} /usr/share/artica-postfix/exec.artica-meta-server.php --ping-group {$gpid} >/dev/null 2>&1 &"); writelogs_framework("{$cmd}", __FUNCTION__, __FILE__, __LINE__); meta_events($cmd); shell_exec($cmd); }
function philesight_client($uuid) { $unix = new unix(); $workingfile = "/usr/share/artica-postfix/ressources/conf/meta/hosts/uploaded/{$uuid}/philesight.tgz"; if (!is_file($workingfile)) { meta_events("{$uuid}: {$workingfile} no such file"); return; } $tar = $unix->find_program("tar"); $rm = $unix->find_program("rm"); $sql = "CREATE TABLE IF NOT EXISTS `philesight` (\n\t\t\t`zmd5` VARCHAR( 90 ) NOT NULL,\n\t\t\t`uuid` VARCHAR( 90 ) NOT NULL,\n\t\t\t`directory` VARCHAR( 255 ) NOT NULL,\n\t\t\t`partition` VARCHAR( 128 ) NOT NULL,\n\t\t\t`image` longblob NOT NULL,\n\t\t\t`hd` VARCHAR( 60 ) NOT NULL,\n\t\t\t`lastscan` INT(10) NOT NULL DEFAULT 0,\n\t\t\t`USED` FLOAT NOT NULL DEFAULT 0,\n\t\t\t`FREEMB` INT UNSIGNED NOT NULL DEFAULT 0,\n\t\t\t PRIMARY KEY ( `zmd5` ),\n\t\t\t KEY `lastscan` ( `lastscan` ),\n\t\t\t KEY `partition` ( `partition` ),\n\t\t\t KEY `uuid` ( `uuid` ),\n\t\t\t KEY `directory` ( `directory` ),\n\t\t\t KEY `hd` ( `hd` )\n\t\t\t) ENGINE=MYISAM;"; $q = new mysql_meta(); $q->QUERY_SQL($sql); if (!$q->ok) { return; } $temppath = $unix->TEMP_DIR() . "/{$uuid}-philesight"; @mkdir($temppath, 0755, true); shell_exec("{$tar} xf {$workingfile} -C {$temppath}/"); $prefix = "INSERT INTO `philesight` (zmd5,uuid,directory,partition,image,lastscan,USED,FREEMB,hd) VALUES "; if (!is_file("{$temppath}/dump.db")) { meta_admin_mysql(1, "{$uuid}: philesight {$temppath}/dump.db no such file", $workingfile, __FILE__, __LINE__); @unlink($workingfile); shell_exec("{$rm} -rf {$temppath}"); return; } $data = unserialize(@file_get_contents("{$temppath}/dump.db")); if (!is_array($data)) { @unlink($workingfile); shell_exec("{$rm} -rf {$temppath}"); meta_admin_mysql(1, "{$uuid}: philesight not an array", $workingfile, __FILE__, __LINE__); return; } if (count($data) == 0) { @unlink($workingfile); shell_exec("{$rm} -rf {$temppath}"); meta_admin_mysql(1, "{$uuid}: philesight empty array", $workingfile, __FILE__, __LINE__); return; } $TR = array(); while (list($directory, $ARRAY) = each($data)) { $md5 = md5($uuid . $directory); $md5_image = $ARRAY["MD5"]; $directory = mysql_escape_string2($directory); $partition = $ARRAY["PARTITION"]; $image = @file_get_contents("{$temppath}/{$md5_image}.png"); $lastscan = $ARRAY["lastscan"]; $image = mysql_escape_string2($image); $USED = $ARRAY["USED"]; $hd = $ARRAY["HD"]; $FREEMB = $ARRAY["FREEMB"]; $TR[] = "('{$md5}','{$uuid}','{$directory}','{$partition}','{$image}','{$lastscan}','{$USED}','{$FREEMB}','{$hd}')"; } if (count($TR) > 0) { if ($GLOBALS["VERBOSE"]) { echo "Adding " . count($TR) . " rows\n"; } $q->QUERY_SQL("DELETE FROM `philesight` WHERE uuid='{$uuid}'"); $q->QUERY_SQL($prefix . @implode(",", $TR)); meta_events("{$uuid}: INSERTING " . count($TR) . " elements..."); } @unlink($workingfile); shell_exec("{$rm} -rf {$temppath}"); }
function _daily_to_monthly($tablename, $uuid) { $q = new mysql_uuid_meta($uuid); $f = array(); $sql = "SELECT DATE_FORMAT(`zDate`,'%Y-%m-%d') as `zDate`,DATE_FORMAT(`zDate`,'%Y%m') as `NextDate`,`sitename`,\n\t`mac`,`uid`,`ipaddr`,SUM(`hits`) as `hits`,SUM(`size`) as `size` FROM {$tablename}\n\tGROUP BY `sitename`,`mac`,`uid`,`ipaddr`,DATE_FORMAT(`zDate`,'%Y-%m-%d'),DATE_FORMAT(`zDate`,'%Y%m%d')"; $results = $q->QUERY_SQL($sql); while ($ligne = @mysql_fetch_array($results, MYSQL_ASSOC)) { $md5 = md5(serialize($ligne)); $date = $ligne["zDate"]; $hits = $ligne["hits"]; $size = $ligne["size"]; $mac = $ligne["mac"]; $uid = $ligne["uid"]; $ipaddr = $ligne["ipaddr"]; $sitename = $ligne["sitename"]; $Nexttablename = "squid_monthly_" . $ligne["NextDate"]; $f[$Nexttablename][] = "('{$md5}','{$date}','{$sitename}','{$mac}','{$uid}','{$ipaddr}','{$hits}','{$size}')"; if (count($f[$Nexttablename]) > 2000) { $prefix = "INSERT IGNORE INTO `{$Nexttablename}` (`zdm5`,`zDate`,`sitename`,`mac`,`uid`,`ipaddr`,`hits`,`size`) VALUES "; if (!$q->create_squid_hourly($Nexttablename)) { return false; } if (!$q->TABLE_EXISTS($Nexttablename)) { return false; } $q->QUERY_SQL($prefix . @implode(",", $f[$Nexttablename])); if (!$q->ok) { meta_events($q->mysql_error); return false; } $f[$Nexttablename] = array(); } } if (count($f) > 0) { while (list($tablename, $rows) = each($f)) { $prefix = "INSERT IGNORE INTO `{$tablename}` (`zmd5`,`zDate`,`sitename`,`mac`,`uid`,`ipaddr`,`hits`,`size`) VALUES "; if (!$q->create_squid_hourly($tablename)) { return false; } if (!$q->TABLE_EXISTS($tablename)) { return false; } $q->QUERY_SQL($prefix . @implode(",", $rows)); if (!$q->ok) { return false; } } } return true; }
function meta_admin_mysql_check($nopid = false) { $f = array(); $unix = new unix(); $sock = new sockets(); $users = new usersMenus(); $hostname = $unix->hostname_g(); $BaseWorkDir = "{$GLOBALS["ARTICALOGDIR"]}/meta_admin_mysql"; $uuid = $unix->GetUniqueID(); if (!($handle = opendir($BaseWorkDir))) { echo "Failed open {$BaseWorkDir}\n"; return; } $hostname = "master"; $EnableArticaMetaServer = intval($sock->GET_INFO("EnableArticaMetaServer")); if ($EnableArticaMetaServer == 0) { $hostname = $unix->hostname_g(); } $q = new mysql(); if (!$q->test_mysql_connection()) { return; } if (!$q->TABLE_EXISTS("meta_admin_mysql", "artica_events")) { return; } if (!$q->ok) { meta_events("{$q->mysql_error}", __FUNCTION__, __FILE__, __LINE__); return; } while (false !== ($filename = readdir($handle))) { if ($filename == ".") { continue; } if ($filename == "..") { continue; } $targetFile = "{$BaseWorkDir}/{$filename}"; if ($unix->file_time_min($targetFile) > 240) { @unlink($targetFile); continue; } $array = unserialize(@file_get_contents($targetFile)); if (!is_array($array)) { @unlink($targetFile); continue; } if (!is_numeric($array["TASKID"])) { $array["TASKID"] = 0; } $content = mysql_escape_string2($array["text"]); $subject = mysql_escape_string2($array["subject"]); $zdate = $array["zdate"]; $function = $array["function"]; $file = $array["file"]; $line = $array["line"]; $TASKID = $array["TASKID"]; $severity = $array["severity"]; $zm5 = md5("{$zdate}{$subject}{$function}{$file}{$line}{$uuid}"); meta_events("[meta_admin_mysql]:: {$subject} ", __FUNCTION__, __FILE__, __LINE__); $q->QUERY_SQL("INSERT IGNORE INTO `meta_admin_mysql`\n\t\t\t\t(`zmd5`,`uuid`,`zDate`,`content`,`subject`,`function`,`filename`,`line`,`severity`,`hostname`) VALUES\n\t\t\t\t('{$zm5}','{$uuid}','{$zdate}','{$content}','{$subject}','{$function}','{$file}','{$line}','{$severity}','{$hostname}')", "artica_events"); $q->QUERY_SQL("INSERT IGNORE INTO `meta_admin_mysql`\n\t\t\t\t(`zDate`,`content`,`subject`,`function`,`filename`,`line`,`severity`,`hostname`) VALUES\n\t\t\t\t('{$zdate}','{$content}','{$subject}','{$function}','{$file}','{$line}','{$severity}','{$hostname}')", "artica_events"); if (!$q->ok) { meta_events("{$q->mysql_error}", __FUNCTION__, __FILE__, __LINE__); return; } @unlink($targetFile); } }
function scan_directories() { $UPDATED = false; if ($GLOBALS["VERBOSE"]) { $GLOBALS["FORCE"] = true; } if ($GLOBALS["FORCE"]) { $UPDATED = true; ini_set('html_errors', 0); ini_set('display_errors', 1); ini_set('error_reporting', E_ALL); } $unix = new unix(); $pidFile = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".pid"; $TimeFile = "/etc/artica-postfix/pids/exec.philesight.php.scan_directories.time"; $pid = $unix->get_pid_from_file($pidFile); if ($unix->process_exists($pid)) { return; } if (!$GLOBALS["FORCE"]) { $time = $unix->file_time_min($TimeFile); if ($time < 120) { return; } if (system_is_overloaded(__FILE__)) { return; } } @unlink($TimeFile); @file_put_contents($TimeFile, time()); if (!is_file("/usr/bin/ruby1.8")) { build_progress("{please_wait}, /usr/bin/ruby1.8 no such binary [installing]...", 5); $unix->DEBIAN_INSTALL_PACKAGE("ruby1.8"); } if (!is_file("/usr/bin/ruby1.8")) { build_progress("{failed}, /usr/bin/ruby1.8 no such binary...", 110); system_admin_events("/usr/bin/ruby1.8 no such binary, philesight cannot be used!", __FUNCTION__, __FILE__, __LINE__); } $sock = new sockets(); $EnableArticaMetaClient = intval($sock->GET_INFO("EnableArticaMetaClient")); if ($EnableArticaMetaClient == 1) { meta_events("Meta Client Enabled", __FUNCTION__, __LINE__); } $q = new mysql(); $results = $q->QUERY_SQL("SELECT * FROM philesight WHERE enabled=1", "artica_backup"); @mkdir("/usr/share/artica-postfix/img/philesight", 0755, true); @mkdir("/home/artica/philesight", 0755, true); $NICE = $unix->EXEC_NICE(); build_progress("{please_wait}, {scaning_directories}...", 10); $pr = 10; while ($ligne = @mysql_fetch_array($results, MYSQL_ASSOC)) { $directory = $ligne["directory"]; $partition = $ligne["partition"]; $md5 = md5($directory); $maxtime = $ligne["maxtime"]; if ($maxtime == 0) { continue; } $lastscan = $ligne["lastscan"]; if ($lastscan == 0) { $lastscan = 1000000; } $sql_time_min = sql_time_min($lastscan); $partition = $unix->DIRPART_OF($directory); $hd = $unix->DIRDISK_OF($directory); $ARRAY = $unix->DF_SATUS_K($directory); $USED = $ARRAY["POURC"]; $FREEMB = round($ARRAY["AIVA"] / 1024); $ARRAY_META[$directory]["MD5"] = $md5; $ARRAY_META[$directory]["lastscan"] = $lastscan; $ARRAY_META[$directory]["HD"] = $hd; $ARRAY_META[$directory]["PARTITION"] = $partition; $ARRAY_META[$directory]["USED"] = $USED; $ARRAY_META[$directory]["FREEMB"] = $FREEMB; $directoryXXX = mysql_escape_string2($directory); $sql = "UPDATE `philesight` SET `hd`='{$hd}',\n\t\tpartition='{$partition}',\n\t\thd='{$hd}',\n\t\tUSED='{$USED}',\n\t\tFREEMB='{$FREEMB}'\n\t\tWHERE `directory`='{$directoryXXX}'"; $q->QUERY_SQL($sql, 'artica_backup'); if (is_file("/home/artica/philesight/{$md5}.db")) { if (!$GLOBALS["FORCE"]) { if ($sql_time_min < $maxtime) { continue; } } } $pr++; if ($GLOBALS["FORCE"]) { echo "Partition............: {$partition}\n"; } if ($GLOBALS["FORCE"]) { echo "Hard disk............: {$hd}\n"; } if ($GLOBALS["FORCE"]) { echo "Used.................: {$USED}%\n"; } if ($GLOBALS["FORCE"]) { echo "Free.................: {$FREEMB}MB\n"; } build_progress("{please_wait}, {scaning_directory} {$directory}...", $pr); $UPDATED = true; $cmd = "{$NICE} /usr/share/artica-postfix/bin/philesight --db /home/artica/philesight/{$md5}.db --only-dirs --index \"{$directory}\" 2>&1"; if ($GLOBALS["FORCE"]) { echo "{$cmd}\n"; } system($cmd); if (!is_file("/home/artica/philesight/{$md5}.db")) { if ($GLOBALS["FORCE"]) { echo "/home/artica/philesight/{$md5}.db no such file.\n"; } } build_progress("{please_wait}, Generating report on {$directory}...", $pr); $cmd = "{$NICE} /usr/share/artica-postfix/bin/philesight --db /home/artica/philesight/{$md5}.db --path \"{$directory}\" --draw /usr/share/artica-postfix/img/philesight/{$md5}.png"; if ($GLOBALS["FORCE"]) { echo "{$cmd}\n"; } system($cmd); $directory = mysql_escape_string2($directory); $sql = "UPDATE `philesight` SET `hd`='{$hd}',\n\t\tpartition='{$partition}',\n\t\tlastscan=" . time() . ",\n\t\thd='{$hd}',\n\t\tUSED='{$USED}',\n\t\tFREEMB='{$FREEMB}'\n\t\tWHERE `directory`='{$directory}'"; $q->QUERY_SQL($sql, 'artica_backup'); if (!$q->ok) { echo $q->mysql_error . "\n"; build_progress("{failed}, MySQL error", 110); return; } } build_progress("{success}", 100); if ($EnableArticaMetaClient) { meta_events("UPDATED={$UPDATED}", __FUNCTION__, __LINE__); } if ($UPDATED) { $sock = new sockets(); $EnableArticaMetaClient = intval($sock->GET_INFO("EnableArticaMetaClient")); if ($EnableArticaMetaClient == 1) { $cp = $unix->find_program("cp"); $tar = $unix->find_program("tar"); $rm = $unix->find_program("rm"); @mkdir("/home/artica/metaclient/upload/philesight", true, 0755); @file_put_contents("/home/artica/metaclient/upload/philesight/dump.db", serialize($ARRAY_META)); shell_exec("{$cp} -f /usr/share/artica-postfix/img/philesight/* /home/artica/metaclient/upload/philesight/"); @chdir("/home/artica/metaclient/upload/philesight"); system("cd /home/artica/metaclient/upload/philesight"); if (is_file("/home/artica/metaclient/upload/philesight.tgz")) { @unlink("/home/artica/metaclient/upload/philesight.tgz"); } shell_exec("{$tar} -cf /home/artica/metaclient/upload/philesight.tgz *"); shell_exec("{$rm} -rf /home/artica/metaclient/upload/philesight/*"); } } if ($GLOBALS["META_PING"]) { if (is_file("/home/artica/metaclient/upload/philesight.tgz")) { $php = $unix->LOCATE_PHP5_BIN(); system("{$php} /usr/share/artica-postfix/exec.artica-meta-client.php --ping --force >/dev/null 2>&1 &"); } } }