function clients_hours($nopid = false) { $pidfile = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".pid"; $unix = new unix(); if (!$nopid) { $oldpid = @file_get_contents($pidfile); if ($unix->process_exists($oldpid)) { die; } $mypid = getmypid(); @file_put_contents($pidfile, $mypid); } $currenttable = "dansguardian_events_" . date('Ymd'); $next_table = date('Ymd') . "_hour"; _clients_hours_perfom($currenttable, $next_table); table_days(); $q = new mysql_squid_builder(); $sql = "SELECT DATE_FORMAT(zDate,'%Y%m%d') as suffix,tablename FROM tables_day WHERE Hour=0"; $results = $q->QUERY_SQL($sql); if (!$q->ok) { events_tail("{$q->mysql_error}"); return; } $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)) { $next_table = $ligne["suffix"] . "_hour"; if (!$q->CreateHourTable($next_table)) { events_tail("Failed to create {$next_table}"); return; } if (!_clients_hours_perfom($ligne["tablename"], $next_table)) { events_tail("Failed to process {$ligne["tablename"]} to {$next_table}"); return; } } }
function repair_from_sources_tables() { $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 "time: {$timefile}\n"; } $pid = @file_get_contents($pidfile); if (!$GLOBALS["VERBOSE"]) { if (!$GLOBALS["FORCE"]) { if ($unix->process_exists($pid, basename(__FILE__))) { if ($GLOBALS["VERBOSE"]) { echo "Already executed pid {$pid}\n"; } return; } if ($pid < 100) { $pid = null; } $mypid = getmypid(); @file_put_contents($pidfile, $mypid); } } if ($GLOBALS["FORCE_TIME"]) { $timeexec = $unix->file_time_min($timefile); if ($timeexec < 240) { return; } } @unlink($timefile); @file_put_contents($timefile, time()); $Prefix = "/usr/share/artica-postfix"; $php5 = $unix->LOCATE_PHP5_BIN(); $nohup = $unix->find_program("nohup"); $EXEC_NICE = $unix->EXEC_NICE(); $q = new mysql_squid_builder(); $C = 0; $array = $q->LIST_TABLES_dansguardian_events(); $current = "dansguardian_events_" . date("Ymd"); while (list($tablename, $none) = each($array)) { if ($tablename == $current) { continue; } $time = $q->TIME_FROM_DANSGUARDIAN_EVENTS_TABLE($tablename); $xtime = date("Y-m-d", $time); $hour_table = date("Ymd", $time) . "_hour"; $member_table = date("Ymd", $time) . "_members"; $SUM_SOURCE = $q->COUNT_ROWS($tablename); if ($SUM_SOURCE == 0) { continue; } $SUM_DEST = $q->COUNT_ROWS($hour_table); $PERC = $SUM_DEST / $SUM_SOURCE * 100; $PERC = intval($PERC); echo "{$xtime}] {$SUM_SOURCE} - {$SUM_DEST} = {$PERC}% - {$tablename}\n"; if ($PERC < 5) { if (!$q->CreateHourTable($hour_table)) { echo "{$xtime}] {$tablename} unable to create {$hour_table}\n"; continue; } _repair_from_sources_tables($tablename, $hour_table); $q->QUERY_SQL("UPDATE tables_day SET `totalsize`='0',`requests`=0,`MembersCount`=0,`month_flow`=0,weekdone=0,weekbdone=0 WHERE tablename='{$tablename}'"); $C++; } $ligne1 = mysql_fetch_array($q->QUERY_SQL("SELECT SUM(hits) as hits FROM {$tablename}")); if (!$q->ok) { echo $q->mysql_error; } $SumDehits_src = $ligne1["hits"]; $ligne1 = mysql_fetch_array($q->QUERY_SQL("SELECT SUM(hits) as hits FROM {$member_table}")); $SumDehits_dest = $ligne1["hits"]; $PERC = $SumDehits_dest / $SumDehits_src * 100; $PERC = intval($PERC); echo "{$xtime}] {$SumDehits_src} - {$SumDehits_dest} = {$PERC}% - {$member_table}\n"; if ($PERC < 90) { if (!$q->CreateMembersDayTable($hour_table)) { echo "{$xtime}] {$tablename} unable to create {$hour_table}\n"; continue; } _repair_members_sources_tables($tablename, $member_table); $C++; } } if ($C > 0) { shell_exec(trim("{$EXEC_NICE} {$php5} {$Prefix}/exec.squid.stats.totals.php --repair --byschedule --schedule-id={$GLOBALS["SCHEDULE_ID"]}")); } }
function save_category() { if ($_GET["website"] == null) { return; } $www = trim(strtolower(base64_decode($_GET["website"]))); if (preg_match("#^www\\.(.+?)\$#i", $www, $re)) { $www = $re[1]; } $category = $_GET["category"]; $md5 = md5($www . $category); $sock = new sockets(); $q = new mysql_squid_builder(); $uuid = base64_decode($sock->getFrameWork("cmd.php?system-unique-id=yes")); $enabled = $_GET["enabled"]; $q->CreateCategoryTable($category); $category_table = $q->category_transform_name($category); $sql = "SELECT zmd5 FROM category_{$category_table} WHERE pattern='{$www}'"; $ligne = @mysql_fetch_array($q->QUERY_SQL($sql)); $sql_add = "INSERT IGNORE INTO categorize (zmd5,zDate,category,pattern,uuid) VALUES('{$md5}',NOW(),'{$category}','{$www}','{$uuid}')"; $sql_add2 = "INSERT IGNORE INTO category_{$category_table} (zmd5,zDate,category,pattern,uuid) VALUES('{$md5}',NOW(),'{$category}','{$www}','{$uuid}')"; $sql_edit = "UPDATE category_{$category_table} SET enabled='{$enabled}' WHERE zmd5='{$ligne["zmd5"]}'"; writelogs("{$www}/{$category} = {$ligne["zmd5"]}", __FUNCTION__, __FILE__, __LINE__); if ($ligne["zmd5"] == null) { $q->QUERY_SQL($sql_add2); $q->QUERY_SQL($sql_add); } else { writelogs("{$sql_edit}", __FUNCTION__, __FILE__, __LINE__); $q->QUERY_SQL($sql_edit); } if (!$q->ok) { echo $q->mysql_error; return; } $q->QUERY_SQL("UPDATE visited_sites SET category='' WHERE sitename='{$www}'"); if (!$q->ok) { echo $q->mysql_error . "\n"; echo $sql . "\n"; } $cats = addslashes($q->GET_CATEGORIES($www, true)); $q->QUERY_SQL("UPDATE visited_sites SET category='{$cats}' WHERE sitename='{$www}'"); if (!$q->ok) { echo $q->mysql_error . "\n"; echo $sql . "\n"; } $newmd5 = md5("{$cats}{$www}"); $q->QUERY_SQL("INSERT IGNORE INTO categorize_changes (zmd5,sitename,category) VALUES('{$newmd5}','{$www}','{$cats}')"); if (!$q->ok) { echo $q->mysql_error . "\n"; echo $sql . "\n"; } if ($enabled == 1) { $q->QUERY_SQL("DELETE FROM categorize_delete WHERE zmd5='{$md5}'"); } else { $q->QUERY_SQL("INSERT IGNORE INTO categorize_delete(zmd5,sitename,category) VALUES('{$md5}','{$www}','{$category}')"); } if ($_GET["day"] != null) { $time = strtotime($_GET["day"] . " 00:00:00"); $tableSrc = date('Ymd') . "_hour"; if (!$q->TABLE_EXISTS($tableSrc)) { $q->CreateHourTable($tableSrc); } $q->QUERY_SQL("UPDATE {$tableSrc} SET category='{$cats}' WHERE sitename='{$www}'"); if (!$q->ok) { echo $q->mysql_error; } } $sock = new sockets(); $sock->getFrameWork("cmd.php?export-community-categories=yes"); $sock->getFrameWork("squid.php?re-categorize=yes"); }
function clients_hours($nopid = false) { if ($GLOBALS["VERBOSE"]) { echo "L.[" . __LINE__ . "]: processing clients_hours()\n"; } if (isset($GLOBALS["clients_hours_executed"])) { if ($GLOBALS["VERBOSE"]) { echo "clients_hours():: Already executed\n"; } return true; } $GLOBALS["clients_hours_executed"] = true; $pidfile = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".pid"; $unix = new unix(); if (!$nopid) { $pid = @file_get_contents($pidfile); if ($unix->process_exists($pid, basename(__FILE__))) { writelogs("Already executed pid:{$pid}", __FUNCTION__, __FILE__, __LINE__); return; } $mypid = getmypid(); @file_put_contents($pidfile, $mypid); } $currenttable = "dansguardian_events_" . date('Ymd'); $next_table = date('Ymd') . "_hour"; echo "L.[" . __LINE__ . "]:_clients_hours_perfom({$currenttable},{$next_table})\n"; _clients_hours_perfom($currenttable, $next_table); $q = new mysql_squid_builder(); $sql = "SELECT DATE_FORMAT(zDate,'%Y%m%d') as suffix,tablename FROM tables_day WHERE Hour=0"; $results = $q->QUERY_SQL($sql); if (!$q->ok) { events_tail("{$q->mysql_error}"); return; } $num_rows = mysql_num_rows($results); if ($num_rows == 0) { if ($GLOBALS["VERBOSE"]) { echo "clients_hours():: No datas " . __FUNCTION__ . " " . __LINE__ . "\n"; } return; } while ($ligne = @mysql_fetch_array($results, MYSQL_ASSOC)) { $next_table = $ligne["suffix"] . "_hour"; if (!$q->CreateHourTable($next_table)) { events_tail("Failed to create {$next_table}"); return; } if (!_clients_hours_perfom($ligne["tablename"], $next_table)) { events_tail("Failed to process {$ligne["tablename"]} to {$next_table}"); return; } $q->QUERY_SQL("UPDATE tables_day SET `Hour`=1 WHERE tablename='{$ligne["tablename"]}'"); } }