<%
include("language.html")
if _SESSION["logined"] ~= nil then

local domain = _GET["domain"] or _POST["domain"]
local type = _GET["type"] or _POST["type"] or ""
local username = _GET["username"] or _POST["username"] or ""
local starttime = _GET["starttime"] or _POST["starttime"] or ""
local endtime = _GET["endtime"] or _POST["endtime"] or ""
local reportname = ""
local strSQL = ""

if (_SESSION["admin_readonly"] ~= nil and _SESSION["admin_readonly"] == 1) then
	print(RESULT_STR[-1])
	exit()
end

if type == "0" then
	strSQL = "f_time >= datetime('now','localtime','-7 day')"
	reportname = LANG["title_weekly_report"]

	starttime = os.date("%Y-%m-%d %H:%M:%S",os.time()-3600*24*7)
	endtime = os.date("%Y-%m-%d %H:%M:%S",os.time())
end

if type == "1" then
	strSQL = "f_time >= datetime('now','localtime','-30 day')"
	reportname = LANG["title_monthly_report"]

	starttime = os.date("%Y-%m-%d %H:%M:%S",os.time()-3600*24*30)
	endtime = os.date("%Y-%m-%d %H:%M:%S",os.time())
end

if type == "2" then
	starttime = starttime.gsub(starttime,"'", "")
	endtime = endtime.gsub(endtime,"'", "")
	username = username.gsub(username,"'", "")
	strSQL = "f_time >= '"..starttime.."' and  f_time <= '"..endtime.."'"
	reportname = LANG["title_custom_report"]
	if username ~= "" then
		strSQL = strSQL.." and f_username = '"..username.."' "
		reportname = reportname.." for user ["..specialhtml_encode(username).."]"
	end
end

reportname = reportname.."<br>"..LANG["title_from"].." "..starttime.." "..LANG["title_to"].." "..endtime


--total sessions
local result = c_DoSQL("select count(*), count(DISTINCT(f_ip)) from wftp_dblogs where f_domain = '"..domain.."' and "..strSQL.." and f_action = 0")
local totalsessions = result[1][0] or 0

--total unique ip
local uniqueip = 0
if result[1] ~= nil then
	uniqueip = result[1][1] or 0
else
	uniqueip = "no"
end

--total download files
result = c_DoSQL("select count(*), sum(f_filesize) from wftp_dblogs where f_domain = '"..domain.."' and "..strSQL.." and f_action = 8")
local downloadfiles = result[1][0] or 0
local downloadbytes = result[1][1] or 0

--total upload files
result = c_DoSQL("select count(*), sum(f_filesize) from wftp_dblogs where f_domain = '"..domain.."' and "..strSQL.." and f_action = 10")
local uploadfiles = result[1][0] or 0
local uploadbytes = result[1][1] or 0

--top 10 downloads
local top10downloads = c_DoSQL("select f_filename, count(f_filename) as f_num from wftp_dblogs where f_domain = '"..domain.."' and "..strSQL.." and f_action = 8 group by f_filename order by f_num desc limit 10")

--top 10 uploads
local top10uploads = c_DoSQL("select f_filename, count(f_filename) as f_num from wftp_dblogs where f_domain = '"..domain.."' and "..strSQL.." and f_action = 10 group by f_filename order by f_num desc limit 10")

--top 10 logins
local top10logins = c_DoSQL("select f_ip, count(f_ip) as f_num from wftp_dblogs where f_domain = '"..domain.."' and "..strSQL.." and f_action = 0 group by f_ip order by f_num desc limit 10")

local weblinkfiles = 0
local weblinkbytes = 0
local top10users = {}
if username == "" then
	--total download weblink files
	result = c_DoSQL("select count(*),sum(f_filesize) from wftp_dblogs where f_domain = '"..domain.."' and "..strSQL.." and f_username = '<WEBLINK>' and f_action = 8")
	weblinkfiles = result[1][0] or 0
	weblinkbytes = result[1][1] or 0

	--top 10 login users
	top10users = c_DoSQL("select f_username, count(f_username) as f_num from wftp_dblogs where f_domain = '"..domain.."' and "..strSQL.." and f_action = 0 group by f_username order by f_num desc limit 10")
end

%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Audit Report</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
<%if _COOKIE["admin_lang"] == "schinese" or _COOKIE["admin_lang"] == "tchinese" or _COOKIE["admin_lang"] == "japanese" then%>
body,td,span {font:12px/20px Verdana, Geneva, Arial, Helvetica, sans-serif;}
<%else%>
body,td,span {font:12px/20px Verdana, Geneva, Arial, Helvetica, sans-serif;}
<%end%>
img {border:0px;}

table{ border-collapse:collapse; margin: 0px;}
table td{ border: 1px solid #999; padding: 5px;}
.bordertable td {border:solid 1px #CCC;}



</style>

<script language="javascript" src="include/common.js"></script>
</head>
<body style="background-color:#FFF;" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">


<div align="center">

<div style="padding:20px; width:540px;" align="left">
	<span style="font-size:12pt;font-weight:bold;">[<%=specialhtml_encode(unescape(domain))%>] <%=reportname%></span>
	<br><div id="btn_area"></div>
</div>

<table class="bordertable" width="540" align="center">
	<tr style="background:url(images/box-tail.gif) top repeat-x; ">
	  <td colspan="2"><div align="center"><span style="font-size:11pt;font-weight:bold;"> <%=LANG["title_statistics"]%></span></td>
	</tr>
	<tr>
	  <td width="220"><%=LANG["str_total_session"]%></td>
	  <td width="300"><%=totalsessions%></td>
	</tr>
<% if uniqueip ~= "no" then %>
	<tr>
	  <td><%=LANG["str_unique_ip"]%></td>
	  <td><%=uniqueip%></td>
	</tr>
<% end %>
	<tr>
	  <td><%=LANG["str_total_downfile"]%></td>
	  <td><%=downloadfiles%></td>
	</tr>
	<tr>
	  <td><%=LANG["str_total_upfile"]%></td>
	  <td><%=uploadfiles%></td>
	</tr>
	<tr>
	  <td><%=LANG["str_total_download"]%></td>
	  <td><%=formatSize(downloadbytes)%></td>
	</tr>
	<tr>
	  <td><%=LANG["str_total_upload"]%></td>
	  <td><%=formatSize(uploadbytes)%></td>
	</tr>
<%
if username == "" then
%>
	<tr>
	  <td><%=LANG["str_total_downfile"]%> (Weblink)</td>
	  <td><%=weblinkfiles%></td>
	</tr>
	<tr>
	  <td><%=LANG["str_total_download"]%> (Weblink)</td>
	  <td><%=formatSize(weblinkbytes)%></td>
	</tr>
<%
end
%>
</table>

<br>

<table class="bordertable" width="540" align="center">
	<tr style="background:url(images/box-tail.gif) top repeat-x; ">
	  <td colspan="2"><div align="center"><span style="font-size:11pt;font-weight:bold;"> Top 10 <%=LANG["str_downloads"]%></span></td>
	</tr>
	<tr>
	  <td width="220"><strong><%=LANG["str_filename"]%></strong></td>
	  <td width="300"><strong><%=LANG["str_count"]%></strong></td>
	</tr>
	<%
	for _,row in pairs(top10downloads) do
	print("<tr><td height='20'>"..row[0].."</td><td>"..row[1].."</td></tr>")
	end
	%>
</table>

<br>

<table class="bordertable" width="540" align="center">
	<tr style="background:url(images/box-tail.gif) top repeat-x; ">
	  <td colspan="2"><div align="center"><span style="font-size:11pt;font-weight:bold;"> Top 10 <%=LANG["str_uploads"]%></span></td>
	</tr>
	<tr>
	  <td width="220"><strong><%=LANG["str_filename"]%></strong></td>
	  <td width="300"><strong><%=LANG["str_count"]%></strong></td>
	</tr>
	<%
	for _,row in pairs(top10uploads) do
	print("<tr><td height='20'>"..row[0].."</td><td>"..row[1].."</td></tr>")
	end
	%>
</table>

<br>

<table class="bordertable" width="540" align="center">
	<tr style="background:url(images/box-tail.gif) top repeat-x; ">
	  <td colspan="2"><div align="center"><span style="font-size:11pt;font-weight:bold;"> Top 10 <%=LANG["str_ip"]%></span></td>
	</tr>
	<tr>
	  <td width="220"><strong><%=LANG["str_ip"]%></strong></td>
	  <td width="300"><strong><%=LANG["str_count"]%></strong></td>
	</tr>
	<%
	for _,row in pairs(top10logins) do
	print("<tr><td height='20'>"..row[0].."</td><td>"..row[1].."</td></tr>")
	end
	%>
</table>

<%
if username == "" then
%>
<br>

<table class="bordertable" width="540" align="center">
	<tr style="background:url(images/box-tail.gif) top repeat-x; ">
	  <td colspan="2"><div align="center"><span style="font-size:11pt;font-weight:bold;"> Top 10 <%=LANG["str_username"]%></span></td>
	</tr>
	<tr>
	  <td width="220"><strong><%=LANG["str_username"]%></strong></td>
	  <td width="300"><strong><%=LANG["str_count"]%></strong></td>
	</tr>
	<%
	for _,row in pairs(top10users) do
	print("<tr><td height='20'>"..row[0].."</td><td>"..row[1].."</td></tr>")
	end
	%>
</table>
<%
end
%>

<br>

</div>

</body>
</html>

<script language="javascript">

try
{
	parent.$('loading').style.display='none';
}
catch(e){}


if(isIE)
{
	var nowtime = new Date().getTime();
	$("btn_area").innerHTML = "<button onclick='document.execCommand(\"print\", false, null)'><span><em> <%=LANG['str_print']%> </em></span></button>&nbsp;<button onclick='document.execCommand(\"saveAs\", true, \"wingftp_report_"+nowtime+".htm\");'><span><em><%=LANG['str_save_as']%></em></span></button>";
}
else
{
	$("btn_area").innerHTML = "<button onclick='window.print();'><span><em> Print </em></span></button>";
}
</script>

<% 
else
	print("<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>")
	print("<script>alert('"..LANG["session_expire"].."');top.location='admin_login.html';</script>")
end 
%>