<?php
error_reporting(7);
////////////////////////////////
$time = microtime(true);
define("ROOTPATH", "/home/www/tur255/top.setroot.rul");
////////////////////////////////
define("MYSQLHOST", "localhost");
define("DBNAME", "tur255_traf");
define("DBUSER", "tur255_traf");
define("DBPASS", "369dmw");
//TABLE OF BASE
define("SHOWS", "top_shows");
define("USERS", "top_users");
define("HOSTS", "top_hosts");
define("HITS", "top_hits");
define("ONLINE", "top_is_online");
define("STATS_DAY_2008", "top_stats_days_2008");
define("INHOST", "top_inhost");
for($i = 1; $i < 12; $i++)
{
$sql = mysql_connect(MYSQLHOST, DBUSER, DBPASS);
if($sql) break;
else echo($i);
if($i == 11) echo("FULL LIMIT CONNECTIONS - 10. TOP NOT UPDATED.");
sleep(15);
}
$sql = mysql_select_db(DBNAME);
$sql = mysql_unbuffered_query("SET NAMES 'utf8';");
////////////////////////////////////
$month = file_get_contents(ROOTPATH."/tmp/date/month");
$date = file_get_contents(ROOTPATH."/tmp/date/date");
//MONTH UPDATE
if(($month) && date("m") != $month)
{
mysql_unbuffered_query("UPDATE `".USERS."` SET `month_hosts` = 0, `month_hits` = 0;");
$fp = fopen(ROOTPATH."/tmp/date/month", "w");
fwrite($fp, date("m"));
fclose($fp);
}
//DATE UPDATE
$fp = fopen(ROOTPATH."/tmp/date/date", "w");
fwrite($fp, date("j"));
fclose($fp);
//DAY_STATS
$sql = mysql_query("SELECT `id`, `hosts`, `hits` FROM `".USERS."` WHERE `status` IN('user', 'vip');") or print(mysql_error());
if(mysql_affected_rows() > 0)
{
while($update = mysql_fetch_assoc($sql)) $q = mysql_unbuffered_query("INSERT INTO `".STATS_DAY_2008."` SET `id` = ".$update['id'].", `hosts` = ".$update['hosts'].", `hits` = ".$update['hits'].", `date` = '2008-".$month."-".$date."';");
}
//END DAY STATS
//NO ACTIVE
$sql = mysql_unbuffered_query("UPDATE `".USERS."` SET `days` = (`days` +1) WHERE `hosts` = 0 AND `hits` = 0 AND `status` != 'locked';") or print(mysql_error());
$no_active = mysql_affected_rows();
$sql = mysql_unbuffered_query("UPDATE `".USERS."` SET `days` = 0 WHERE `status` IN('user', 'vip') AND `hosts` > 0 AND `hits` > 0;") or print(mysql_error());
$active = mysql_affected_rows();
//DELETE
$sql = mysql_query("UPDATE `".USERS."` SET `status` = 'delete' WHERE `days` > 10;");
$delete = mysql_affected_rows();
//Statistic
$sql = mysql_query("SELECT COUNT(*) FROM `".USERS."`;") or print(mysql_error());
$all_users = mysql_result($sql, 0);
$sql = mysql_query("SELECT SUM(`hosts`) FROM `".USERS."`;") or print(mysql_error());
$hosts = mysql_result($sql, 0);
$sql = mysql_query("SELECT SUM(`hits`) FROM `".USERS."`;") or print(mysql_error());
$hits = mysql_result($sql, 0);
//STAT UPDATE
$sql = mysql_unbuffered_query("UPDATE `".USERS."` SET `yday_hosts` = `hosts`, `yday_hits` = `hits`, `hosts` = 0, `hits` = 0, `in` = 0, `out` = 0;") or print(mysql_error());
mysql_unbuffered_query("TRUNCATE TABLE `".SHOWS."`;") or print(mysql_error());
mysql_unbuffered_query("OPTIMIZE TABLE `".ONLINE."`;") or print(mysql_error());
$logs = "Date: ".$date."-".$month."-2008\r\n".
"All users: ".$all_users."\r\n".
"NEW Today active: ".$active."\r\n".
"No active: ".intval($no_active)."\r\n".
"Delete users: ".intval($delete)."\r\n".
"-----\r\n".
"Statistic:\r\n".
"Today hosts: ".intval($hosts)."\r\n".
"Today hits: ".intval($hits)."\r\n".
"-----\r\n".
"Generate time: ".(microtime(true) - $time)." sec\r\n";
echo($logs);
$fp = fopen(ROOTPATH."/tmp/logs/".$date."-".$month."-2008", "w");
fwrite($fp, $logs);
fclose($fp);
//DIE
die();
?>