assign('lang', $_LANG);
/* act操作项的初始化 */
if (empty($_REQUEST['act']))
{
$_REQUEST['act'] = 'view';
}
else
{
$_REQUEST['act'] = trim($_REQUEST['act']);
}
if ($_REQUEST['act'] == 'view')
{
if ($_CFG['visit_stats'] == 'off')
{
sys_msg($_LANG['stats_off']);
exit();
}
admin_priv('client_flow_stats');
$is_multi = empty($_POST['is_multi']) ? false : true;
/* 时间参数 */
if (isset($_POST['start_date']) && !empty($_POST['end_date']))
{
$start_date = local_strtotime($_POST['start_date']);
$end_date = local_strtotime($_POST['end_date']);
}
else
{
$today = local_strtotime(local_date('Y-m-d'));
$start_date = $today - 86400 * 7;
$end_date = $today;
}
$start_date_arr = array();
$end_date_arr = array();
if(!empty($_POST['year_month']))
{
$tmp = $_POST['year_month'];
for ($i = 0; $i < count($tmp); $i++)
{
if (!empty($tmp[$i]))
{
$tmp_time = local_strtotime($tmp[$i] . '-1');
$start_date_arr[] = $tmp_time;
$end_date_arr[] = local_strtotime($tmp[$i] . '-' . date('t', $tmp_time));
}
}
}
else
{
$tmp_time = local_strtotime(local_date('Y-m-d'));
$start_date_arr[] = local_strtotime(local_date('Y-m') . '-1');
$end_date_arr[] = local_strtotime(local_date('Y-m') . '-31');;
}
/* ------------------------------------- */
/* --综合流量
/* ------------------------------------- */
$max = 0;
if(!$is_multi)
{
$general_xml = "";
$sql = "SELECT FLOOR((access_time - $start_date) / (24 * 3600)) AS sn, access_time, COUNT(*) AS access_count".
" FROM " .$ecs->table('stats').
" WHERE access_time >= '$start_date' AND access_time <= " .($end_date + 86400).
" GROUP BY sn";
$res = $db->query($sql);
$key = 0;
while ($val = $db->fetchRow($res))
{
$val['access_date'] = gmdate('m-d',$val['access_time'] + $timezone * 3600);
$general_xml .= "";
if ($val['access_count'] > $max)
{
$max = $val['access_count'];
}
$key++;
}
$general_xml .= '';
$general_xml = sprintf($general_xml, $max);
}
else
{
$general_xml = "";
foreach($start_date_arr AS $k => $val)
{
$seriesName = local_date('Y-m', $start_date_arr[$k]);
$general_xml .= "";
$sql = "SELECT FLOOR((access_time - $start_date_arr[$k]) / (24 * 3600)) AS sn, access_time, COUNT(*) AS access_count".
" FROM " .$ecs->table('stats').
" WHERE access_time >= '$start_date_arr[$k]' AND access_time <= " .($end_date_arr[$k] + 86400).
" GROUP BY sn";
$res = $db->query($sql);
$lastDay = 0;
while ($val = $db->fetchRow($res))
{
$day = gmdate('d',$val['access_time'] + $timezone * 3600);
if ($lastDay == 0)
{
$time_span = (($day-1) - $lastDay);
$lastDay++;
for (; $lastDay < $day; $lastDay++)
{
$general_xml .= "";
}
}
$general_xml .= "";
$lastDay = $day;
}
$general_xml .= "";
}
$general_xml .= "";
for ($i = 1;$i<=31;$i++)
{
$general_xml .= "";
}
$general_xml .= "";
$general_xml .= "";
}
/* ------------------------------------- */
/* --地域分布
/* ------------------------------------- */
$area_xml = '';
if(!$is_multi)
{
$area_xml .= "";
$sql = "SELECT COUNT(*) AS access_count, area FROM " . $ecs->table('stats') .
" WHERE access_time >= '$start_date' AND access_time < " .($end_date + 86400).
" GROUP BY area ORDER BY access_count DESC LIMIT 20";
$res = $db->query($sql);
$key = 0;
while ($val = $db->fetchRow($res))
{
$area = empty($val['area']) ? 'unknow' : $val['area'];
$area_xml .= "";
$key++;
}
$area_xml .= '';
}
else
{
$where = '';
foreach($start_date_arr AS $k => $val)
{
if ($where != '')
{
$where .= ' or ';
}
$where .= "(access_time >= '$start_date_arr[$k]' AND access_time <= " .($end_date_arr[$k] + 86400).")";
}
$sql = "SELECT access_time, area FROM " . $ecs->table('stats') .
" WHERE $where";
$res = $db->query($sql);
$area_arr = array();
while ($val = $db->fetchRow($res))
{
$date = local_date('Y-m', $val['access_time']);
$area_arr[$val['area']] = null;
if (isset($category[$date][$val['area']]))
{
$category[$date][$val['area']]++;
}
else
{
$category[$date][$val['area']] = 1;
}
}
$area_xml = "";
$area_xml .= "";
foreach ($area_arr AS $k => $v)
{
$area_xml .= "";
}
$area_xml .= "";
$key = 0;
foreach($start_date_arr AS $val)
{
$key++;
$date = local_date('Y-m', $val);
$area_xml .= "";
foreach ($area_arr AS $k => $v)
{
if (isset($category[$date][$k]))
{
$area_xml .= "";
}
else
{
$area_xml .= "";
}
}
$area_xml .= "";
}
$area_xml .= "";
}
/* ------------------------------------- */
/* --来源网站
/* ------------------------------------- */
if(!$is_multi)
{
$from_xml = "";
$sql = "SELECT COUNT(*) AS access_count, referer_domain FROM " . $ecs->table('stats') .
" WHERE access_time >= '$start_date' AND access_time <= " .($end_date + 86400).
" GROUP BY referer_domain ORDER BY access_count DESC LIMIT 20";
$res = $db->query($sql);
$key = 0;
while ($val = $db->fetchRow($res))
{
$from = empty($val['referer_domain']) ? $_LANG['input_url'] : $val['referer_domain'];
$from_xml .= "";
$key++;
}
$from_xml .= '';
}
else
{
$where = '';
foreach($start_date_arr AS $k => $val)
{
if ($where != '')
{
$where .= ' or ';
}
$where .= "(access_time >= '$start_date_arr[$k]' AND access_time <= " .($end_date_arr[$k] + 86400).")";
}
$sql = "SELECT access_time, referer_domain FROM " . $ecs->table('stats') .
" WHERE $where";
$res = $db->query($sql);
$domain_arr = array();
while ($val = $db->fetchRow($res))
{
$date = local_date('Y-m', $val['access_time']);
$domain_arr[$val['referer_domain']] = null;
if (isset($category[$date][$val['referer_domain']]))
{
$category[$date][$val['referer_domain']]++;
}
else
{
$category[$date][$val['referer_domain']] = 1;
}
}
$from_xml = "";
$from_xml .= "";
foreach ($domain_arr AS $k => $v)
{
$from = $k == '' ? $_LANG['input_url'] : $k;
$from_xml .= "";
}
$from_xml .= "";
$key = 0;
foreach($start_date_arr AS $val)
{
$key++;
$date = local_date('Y-m', $val);
$from_xml .= "";
foreach ($domain_arr AS $k => $v)
{
if (isset($category[$date][$k]))
{
$from_xml .= "";
}
else
{
$from_xml .= "";
}
}
$from_xml .= "";
}
$from_xml .= "";
}
/* 模板赋值 */
$smarty->assign('ur_here', $_LANG['flow_stats']);
$smarty->assign('general_data', $general_xml);
$smarty->assign('area_data', $area_xml);
$smarty->assign('is_multi', $is_multi);
$smarty->assign('from_data', $from_xml);
/* 显示日期 */
$smarty->assign('start_date', local_date('Y-m-d', $start_date));
$smarty->assign('end_date', local_date('Y-m-d', $end_date));
for ($i = 0; $i < 5; $i++)
{
if (isset($start_date_arr[$i]))
{
$start_date_arr[$i] = local_date('Y-m', $start_date_arr[$i]);
}
else
{
$start_date_arr[$i] = null;
}
}
$smarty->assign('start_date_arr', $start_date_arr);
if (!$is_multi)
{
$filename = gmdate($_CFG['date_format'], $start_date + $timezone * 3600) . '_' .
gmdate($_CFG['date_format'], $end_date + $timezone * 3600);
$smarty->assign('action_link', array('text' => $_LANG['down_flow_stats'],
'href'=>'flow_stats.php?act=download&filename=' . $filename .
'&start_date=' . $start_date . '&end_date=' . $end_date));
}
/* 显示页面 */
assign_query_info();
$smarty->display('flow_stats.htm');
}
/* 报表下载 */
elseif ($act = 'download')
{
$filename = !empty($_REQUEST['filename']) ? trim($_REQUEST['filename']) : '';
header("Content-type: application/vnd.ms-excel; charset=utf-8");
header("Content-Disposition: attachment; filename=$filename.xls");
$start_date = empty($_GET['start_date']) ? strtotime('-20 day') : intval($_GET['start_date']);
$end_date = empty($_GET['end_date']) ? time() : intval($_GET['end_date']);
$sql = "SELECT FLOOR((access_time - $start_date) / (24 * 3600)) AS sn, access_time, COUNT(*) AS access_count".
" FROM " . $GLOBALS['ecs']->table('stats') .
" WHERE access_time >= '$start_date' AND access_time <= " .($end_date + 86400).
" GROUP BY sn";
$res = $GLOBALS['db']->query($sql);
$data = $_LANG['general_stats'] . "\t\n";
$data .= $_LANG['date'] . "\t";
$data .= $_LANG['access_count'] . "\t\n";
while ($val = $GLOBALS['db']->fetchRow($res))
{
$val['access_date'] = gmdate('m-d',$val['access_time'] + $timezone * 3600);
$data .= $val['access_date'] . "\t";
$data .= $val['access_count'] . "\t\n";
}
$sql = "SELECT COUNT(*) AS access_count, area FROM " . $GLOBALS['ecs']->table('stats') .
" WHERE access_time >= '$start_date' AND access_time <= " .($end_date + 86400).
" GROUP BY area ORDER BY access_count DESC LIMIT 20";
$res = $GLOBALS['db']->query($sql);
$data .= $_LANG['area_stats'] . "\t\n";
$data .= $_LANG['area'] . "\t";
$data .= $_LANG['access_count'] . "\t\n";
while ($val = $GLOBALS['db']->fetchRow($res))
{
$data .= $val['area'] . "\t";
$data .= $val['access_count'] . "\t\n";
}
$sql = "SELECT COUNT(*) AS access_count, referer_domain FROM " . $GLOBALS['ecs']->table('stats') .
" WHERE access_time >= '$start_date' AND access_time <= " .($end_date + 86400).
" GROUP BY referer_domain ORDER BY access_count DESC LIMIT 20";
$res = $GLOBALS['db']->query($sql);
$data .= "\n" . $_LANG['from_stats'] . "\t\n";
$data .= $_LANG['url'] . "\t";
$data .= $_LANG['access_count'] . "\t\n";
while ($val = $GLOBALS['db']->fetchRow($res))
{
$data .= ($val['referer_domain'] == "" ? $_LANG['input_url'] : $val['referer_domain']) . "\t";
$data .= $val['access_count'] . "\t\n";
}
if (EC_CHARSET != 'gbk')
{
echo ecs_iconv(EC_CHARSET, 'gbk', $data) . "\t";
}
else
{
echo $data. "\t";
}
}
?>