function _table_hours_perform($tablename) { if (!isset($GLOBALS["Q"])) { $GLOBALS["Q"] = new mysql_squid_builder(); } if (!preg_match("#squidhour_([0-9]+)#", $tablename, $re)) { writelogs_squid("NOT AN HOUR TABLE `{$tablename}`", __FUNCTION__, __FILE__, __LINE__, "stats"); return; } $hour = $re[1]; $year = substr($hour, 0, 4); $month = substr($hour, 4, 2); $day = substr($hour, 6, 2); $compressed = false; $f = array(); $dansguardian_table = "dansguardian_events_{$year}{$month}{$day}"; $accounts = $GLOBALS["Q"]->ACCOUNTS_ISP(); if (!$GLOBALS["Q"]->Check_dansguardian_events_table($dansguardian_table)) { return false; } if (!$GLOBALS["Q"]->FIELD_EXISTS("{$tablename}", "category")) { $GLOBALS["Q"]->QUERY_SQL("ALTER TABLE `{$tablename}` ADD `category` VARCHAR( 60 ) NOT NULL ,ADD INDEX ( `category` )"); } $sql = "SELECT COUNT(ID) as hits,SUM(QuerySize) as QuerySize,DATE_FORMAT(zDate,'%Y-%m-%d %H:00:00') as zDate,sitename,uri,TYPE,REASON,CLIENT,uid,remote_ip,country,cached,MAC,hostname,category FROM {$tablename} GROUP BY sitename,uri,TYPE,REASON,CLIENT,uid,remote_ip,country,cached,MAC,zDate,hostname,category"; if ($GLOBALS["VERBOSE"]) { echo $sql . "\n"; } $results = $GLOBALS["Q"]->QUERY_SQL($sql); if (!$GLOBALS["Q"]->ok) { stats_admin_events(1, "Fatal: {$GLOBALS["Q"]->mysql_error} on `{$tablename}`", @implode("\n", $GLOBALS["REPAIR_MYSQL_TABLE"]), __FILE__, __LINE__, "stats"); if (strpos(" {$GLOBALS["Q"]->mysql_error}", "is marked as crashed and should be repaired") > 0) { $q1 = new mysql(); $q1->REPAIR_TABLE("squidlogs", $tablename); stats_admin_events(2, "try to repair table `{$tablename}`", $GLOBALS["REPAIR_MYSQL_TABLE"], __FILE__, __LINE__, "stats"); } return false; } $prefix = "INSERT IGNORE INTO {$dansguardian_table} (sitename,uri,TYPE,REASON,CLIENT,MAC,zDate,zMD5,uid,remote_ip,country,QuerySize,hits,cached,hostname,account,category) VALUES "; $ip = new IP(); $SUM = mysql_num_rows($results); $d = 0; while ($ligne = @mysql_fetch_array($results, MYSQL_ASSOC)) { $zmd = array(); while (list($key, $value) = each($ligne)) { $ligne[$key] = mysql_escape_string2($value); $zmd[] = $value; } $zMD5 = md5(@implode("", $zmd)); $accountclient = null; if (isset($accounts[$ligne["CLIENT"]])) { $accountclient = $accounts[$ligne["CLIENT"]]; } $uid = $ligne["uid"]; if ($uid == null) { $uid = $GLOBALS["Q"]->MacToUid($ligne["MAC"]); if (is_numeric($uid)) { $uid = null; } } if ($uid == null) { $uid = $GLOBALS["Q"]->IpToUid($ligne["CLIENT"]); if (is_numeric($uid)) { $uid = null; } } $uid = mysql_escape_string2($uid); $hostname = $ligne["hostname"]; if ($hostname == null) { $hostname = $GLOBALS["Q"]->MacToHost($ligne["MAC"]); if (is_numeric($uid)) { $uid = null; } } if ($hostname == null) { $hostname = $GLOBALS["Q"]->IpToHost($ligne["CLIENT"]); if (is_numeric($uid)) { $uid = null; } } if ($ip->isValid($hostname)) { $hostname = null; } $hostname = mysql_escape_string2($hostname); $d++; $f[] = "('{$ligne["sitename"]}','{$ligne["uri"]}','{$ligne["TYPE"]}','{$ligne["REASON"]}','{$ligne["CLIENT"]}','{$ligne["MAC"]}','{$ligne["zDate"]}','{$zMD5}','{$uid}','{$ligne["remote_ip"]}','{$ligne["country"]}','{$ligne["QuerySize"]}','{$ligne["hits"]}','{$ligne["cached"]}','{$hostname}','{$accountclient}','{$ligne["category"]}')"; if (count($f) > 500) { ToSyslog("{$dansguardian_table}: {$d}/{$SUM}"); $GLOBALS["Q"]->UncompressTable($dansguardian_table); $GLOBALS["Q"]->QUERY_SQL($prefix . @implode(",", $f)); $f = array(); if (!$GLOBALS["Q"]->ok) { stats_admin_events(1, "MySQL Error ", "{$GLOBALS["Q"]->mysql_error} on `{$dansguardian_table}`", __FILE__, __LINE__, "stats"); return; } } } if (count($f) > 0) { $GLOBALS["Q"]->UncompressTable($dansguardian_table); $GLOBALS["Q"]->QUERY_SQL($prefix . @implode(",", $f)); if (!$GLOBALS["Q"]->ok) { stats_admin_events(1, "MySQL Error ", "{$GLOBALS["Q"]->mysql_error} on `{$dansguardian_table}`", __FILE__, __LINE__, "stats"); return; } $squid_stats_tools = new squid_stats_tools(); $squid_stats_tools->NoCategorize = true; $squid_stats_tools->check_table_days(); } stats_admin_events(2, "Success updating {$dansguardian_table} with {$d} events", __FILE__, __LINE__); return true; }