";
$stats_path = @$_SERVER['DOCUMENT_ROOT']."/statsdata/";
$sess_file = $stats_path."session.log";
$close_session = "5"; //how long user session lives
$rand_page_code = rand(0,100000);
function get_user_id($ip)
{
$user_id = "";
if (file_exists($GLOBALS["sess_file"]))
{
$t_rec = file($GLOBALS["sess_file"]);
$pattern="";
foreach($t_rec as $key=>$val)
{
$pattern="/([^ ]*?) ([^ ]*?) ([^ ]*?) \'(.*?)\' \'(.*?)\' \'(.*?)\' \'(.*?)\' \'(.*?)\' \'(.*?)\'/is";
if ((preg_match_all($pattern,$val,$matches)>0) && ($matches[3][0]==$ip))
{
$user_id=$matches[2][0];
break;
}
}
}
$user_id = ($user_id!="")? $user_id: generate_user_id();
store_action($user_id);
return $user_id;
}
function generate_user_id()
{
//key should depends on project, date
//and unic and randomly
$code = $GLOBALS["project_code"].date("dmYHis");
$str_help = '1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz';
for ($i = 0; $i < 128; $i++)
$code .= $str_help[rand(0,strlen($str_help)-1)];
return $code;
}
function store_action($user_id)
{
//save user data into file
$f = fopen($GLOBALS["sess_file"],"a+");
//rewind($f);
fwrite($f,time()." ".$user_id." ".$_SERVER["REMOTE_ADDR"]." '".@$_SERVER["HTTP_USER_AGENT"]."' '".(isset($_SERVER["HTTP_REFERER"]) ? $_SERVER["HTTP_REFERER"] : '')."' '".(isset($GLOBALS["stats_action"]) ? $GLOBALS["stats_action"] : '')."' '".(isset($GLOBALS["stats_param1"]) ? $GLOBALS["stats_param1"] : '')."' '".(isset($GLOBALS["stats_param2"]) ? $GLOBALS["stats_param2"] : '')."' '".(isset($GLOBALS["stats_param3"]) ? $GLOBALS["stats_param3"] : '')."' '".(isset($GLOBALS["rand_page_code"]) ? $GLOBALS["rand_page_code"] : '')."'\r\n");
fclose($f);
}
function clear_closed_session()
{
//seek file session which are older then 30 minutes
//write them into file corresponds day
//remove them from file
$close_from = time()-$GLOBALS["close_session"]*60;
echo "truncating records from ".$close_from."
";
$f = fopen($GLOBALS["sess_file"],"r+");
$res = "";
$open_files = array();
while ($s=fgets($f))
{
$dt = explode(" ",$s);
if ($dt[0]<$close_from)
{
if (!isset($open_files[date("d_m_Y",$dt[0])]))
$open_files[date("d_m_Y",$dt[0])]=fopen($GLOBALS["stats_path"].date("d_m_Y",$dt[0]).".txt","a+");
fwrite($open_files[date("d_m_Y",$dt[0])],$s);
}
else
$res.=$s;
}
foreach($open_files as $key=>$val)
fclose($val);
if ($res!=="")
{
fclose($f);
$f=fopen($GLOBALS["sess_file"],"w+");
fwrite($f,$res);
// ftruncate($f,$last);
// echo "ftruncate".$last."
";
}
fclose($f);
}
function get_statistyc($time_start)
{
if ($time_start==-1)
$time_start = time()-60*60*24*31;
$t = $time_start;
$cur_time = time();
while ($t<$cur_time)
{
$fname = $GLOBALS["stats_path"].date("d_m_Y",$t).".txt";
if (file_exists($fname))
{
echo file_get_contents($fname);
}
$t+=60*60*24;
}
}
//additional info, using javascript
if (isset($_GET["act"])&&($_GET["act"]=="make_cron_section"))
clear_closed_session();
elseif (isset($_GET["act"])&&($_GET["act"]=="get_statistyc")&&($_GET["project_code"]==$project_code))
get_statistyc($_GET["time_start"]);
else
{
if (!isset($nojs) || ($nojs!=1))
{
?>
if (isset($GLOBALS["videostats"])&&($GLOBALS["videostats"]==1)) {
?>
}
}
else
get_user_id($_SERVER["REMOTE_ADDR"]);
}
?>