<% local data, viewlibrary, page_info, session = ... %>
<% htmlviewfunctions = require("htmlviewfunctions") %>
<% html = require("acf.html") %>
<% format = require("acf.format") %>

<%
-- Insert a string into another string
function string_insert(value, insert, place)
	if place == nil then
		place = string.len(value)+1
	end

	return string.sub(value, 1,place-1) .. tostring(insert) .. string.sub(value, place, string.len(value))
end

--Highlight occurences of a word in a string
function string_highlight(txtvalue, searchval, fcolour, bcolour)
	if txtvalue ~=nil and searchval ~= nil then
		sStart = string.find(string.lower(txtvalue),string.lower(searchval))
		if sStart ~= nil then
			sEnd = sStart + string.len(searchval)
			txtvalue = string_insert(txtvalue,"</font>", sEnd)
			txtvalue = string_insert(txtvalue,"<font style='color:"..fcolour.."; background-color:"..bcolour..";'>", sStart)
		end
	end 

	return txtvalue
end
%>

<script type="text/javascript">
	if (typeof jQuery == 'undefined') {
		document.write('<script type="text/javascript" src="<%= html.html_escape(page_info.wwwprefix) %>/js/jquery-latest.js"><\/script>');
	}
</script>

<script type="text/javascript">
	if (typeof $.tablesorter == 'undefined') {
		document.write('<script type="text/javascript" src="<%= html.html_escape(page_info.wwwprefix) %>/js/jquery.tablesorter.js"><\/script>');
	}
</script>

<script type="text/javascript">
	function clickhead(e) {
		var selectdata = "";
		var deselectdata = "";
		var array=[];
		$(".chktbl").each(function () {
			array.push(this.id);
		});
		if (this.checked == false) {
			$('.chktbl:checked').prop('checked', false).closest('tr').removeClass("selected");
			deselectdata = array.join("%0D%0A");
		} else {
			$('.chktbl:not(:checked)').prop('checked', true).closest('tr').addClass("selected");
			selectdata = array.join("%0D%0A");
		}
		$.getJSON(
			'<%= html.html_escape(page_info.script .. page_info.prefix .. page_info.controller .. "/updateselected") %>?viewtype=json&submit=true&select='+selectdata+'&deselect='+deselectdata,
			function(data) {}
		);
	}

	function clicktable(e) {
		var selectdata = "";
		var deselectdata = "";
		if (this.checked == true) {
			$(this).closest('tr').addClass("selected");
			selectdata = this.id;
		} else {
			// If clearing, clear the Head too
			$("#chkHead").prop("checked", false);
			$(this).closest('tr').removeClass("selected");
			deselectdata = this.id;
		}
		$.getJSON(
			'<%= html.html_escape(page_info.script .. page_info.prefix .. page_info.controller .. "/updateselected") %>?viewtype=json&submit=true&select='+selectdata+'&deselect='+deselectdata,
			function(data) {}
		);
	}

	$(function(){
		$("#loglist").tablesorter({headers: {0:{sorter: false}, 1:{sorter: 'text'}, 5:{sorter: false}, 6:{sorter: false}, 7:{sorter: false}}, widgets: ['zebra']});
		$('#chkHead').click(clickhead);
		$('.chktbl').click(clicktable);
<% if data.value.focus.value ~= "" then %>
		if ($("#focus").length) {
			var top = $("#focus").offset().top;
			$("html,body").scrollTop(top);
		}
<% end %>
	});
</script>

<% local subdata, pagedata = htmlviewfunctions.paginate(data.value.log.value, page_info.clientdata, 200) %>

<style type="text/css">
.tablesorter-blue tbody > tr.even.selected > td,
.tablesorter-blue tbody > tr.even.selected + tr.tablesorter-childRow > td,
.tablesorter-blue tbody > tr.even.selected + tr.tablesorter-childRow + tr.tablesorter-childRow > td {
	background: #FDAD31;
}
.tablesorter-blue tbody > tr.odd.selected > td,
.tablesorter-blue tbody > tr.odd.selected + tr.tablesorter-childRow > td,
.tablesorter-blue tbody > tr.odd.selected + tr.tablesorter-childRow + tr.tablesorter-childRow > td {
	background: #FC9A01;
}
.tablesorter-blue tbody > tr.even.selected:hover > td,
.tablesorter-blue tbody > tr.even.selected:hover + tr.tablesorter-childRow > td,
.tablesorter-blue tbody > tr.even.selected:hover + tr.tablesorter-childRow + tr.tablesorter-childRow > td {
	background: #D48101;
}
.tablesorter-blue tbody > tr.odd.selected:hover > td,
.tablesorter-blue tbody > tr.odd.selected:hover + tr.tablesorter-childRow > td,
.tablesorter-blue tbody > tr.odd.selected:hover + tr.tablesorter-childRow + tr.tablesorter-childRow > td {
	background: #CA7B01;
}
</style>

<H1>Search Parameters</H1>
<DL>
<% -- Display the form, but skip log, window, and focus fields
local log = data.value.log
data.value.log = nil
local window = data.value.window
data.value.window = nil
local focus = data.value.focus
data.value.focus = nil
htmlviewfunctions.displayform(data, nil, nil, page_info, 2)
data.value.log = log
data.value.window = window
data.value.focus = focus
%>
</DL>

<%
local clientinfo = "submit=true&badyesno=false&deniedyesno=false&bypassyesno=false&selected=false&"
if data.value.clientuserid.value ~= "" then
	clientinfo = clientinfo .. "clientuserid="..data.value.clientuserid.value.."&"
end
if data.value.clientip.value ~= "" then
	clientinfo = clientinfo .. "clientip="..data.value.clientip.value.."&"
end
%>

<% htmlviewfunctions.displaypagination(pagedata, page_info) %>
<H1><%= html.html_escape(data.label) %></H1>
<DL>
<TABLE id="loglist" class="tablesorter">
<THEAD>
<TR>
<% local checkhead = true
for i,watch in ipairs(subdata) do
	if watch.selected ~= "t" then
		checkhead = false
		break
	end
end %>
		<TH><input type="checkbox" id="chkHead"<% if checkhead then %> checked<% end %>></TH>
		<TH>Timestamp</TH>
		<TH>Source</TH>
		<TH>Client IP</TH>
		<TH>User ID</TH>
		<TH>Size</TH>
		<TH>Sus</TH>
		<TH>Den</TH>
		<TH>Byp</TH>
		<TH>Score</TH>
		<TH>Reason</TH>
		<TH>URL</TH>
		<TH>Bad Words</TH>
	</TR>
</THEAD>
<TBODY>
<% for i,watch in ipairs(subdata) do
	local a,b = math.modf((i/2))
	local mark = ''
	if watch.selected == "t" then mark=' class="selected"' end
	local time = {}
	time.year, time.month, time.day, time.hour, time.min, time.sec = 
		string.match(watch.logdatetime, "(%d+)%-(%d+)-(%d+)%s+(%d+):(%d+):(%d+)")
	time = os.time(time) %>
<TR<%= mark %>>
		<TD><input class="chktbl" type="checkbox" id="<%= html.html_escape(watch.id) %>"<% if watch.selected == "t" then %> checked <% end %>></TD>
		<TD <% if data.value.focus.value == watch.logdatetime then %> style="font-weight:bold;" id="focus" <% end %> ><%= html.link{value = "viewweblog?"..clientinfo..
			"starttime="..os.date("%Y-%m-%d %H:%M:%S", time - 60*(tonumber(data.value.window.value)))..
			"&endtime="..os.date("%Y-%m-%d %H:%M:%S", time + 60*(tonumber(data.value.window.value)))..
			"&focus="..watch.logdatetime,
			label=watch.logdatetime} %></TD>
		<TD> <%= html.html_escape(watch.sourcename) %></TD>
		<TD <% if data.value.clientip.value == watch.clientip then %> style="font-weight:bold;" <% end %> ><%= html.html_escape(watch.clientip) %></TD>
		<TD <% if data.value.clientuserid.value == watch.clientuserid then %> style="font-weight:bold;" <% end %> ><%= html.html_escape(watch.clientuserid) %></TD>
		<TD><%= html.html_escape(watch.bytes) %></TD>
		<TD><% if watch.badyesno ~= "0" then %><IMG SRC='<%= html.html_escape(page_info.wwwprefix..page_info.staticdir) %>/dodgy.png' width='13' height='13'><% end %></TD>
		<TD><% if watch.deniedyesno ~= "0" then %> <IMG SRC='<%= html.html_escape(page_info.wwwprefix..page_info.staticdir) %>/denied.png' width='13' height='13'><% end %></TD>
		<TD><% if watch.bypassyesno ~= "0" then %> <IMG SRC='<%= html.html_escape(page_info.wwwprefix..page_info.staticdir) %>/bypass.png' width='13' height='13'><% end %></TD>   
		<TD><%= html.html_escape(watch.score) %></TD>
		<TD<% if (watch.shortreason and watch.shortreason ~= "" and watch.reason and watch.reason ~= "") then %> title="<%= html.html_escape(watch.reason) %>"<% end %>>
			<% if (watch.shortreason and watch.shortreason ~= "") then %>
				<%= html.html_escape(watch.shortreason) %>
			<% elseif (watch.reason and watch.reason ~= "") then %>
				<%= html.html_escape(watch.reason) %>
			<% end %>
		</TD>
		<TD title="<%= html.html_escape(watch.uri) %>"><% highlight_uri=html.html_escape(watch.shorturi or watch.uri)
		if watch.wordloc ~= nil then 
			if string.find(watch.wordloc, "|") then 
				badwords = format.string_to_table(watch.wordloc, "|")
				for key,wrd in ipairs(badwords) do
					highlight_uri = string_highlight(highlight_uri, wrd, "yellow", "red")
				end
			else
				highlight_uri = string_highlight(highlight_uri, watch.wordloc, "yellow", "red")
			end
		end %> 
		<%= highlight_uri %></TD>
		<TD><%= watch.wordloc %></TD>
</TR>
<% end %>
</TBODY>
</TABLE>
<% htmlviewfunctions.displaypagination(pagedata, page_info) %>

<% if data.errtxt then %>
<p class="error"><%= html.html_escape(data.errtxt) %></p>
<% end %>
<% if #data.value.log.value == 0 then %>
<p>No results, try adjusting search parameters</p>
<% end %>

<% if viewlibrary.check_permission("downloadweblog") then
data.action = "downloadweblog"
data.option = "Download"
data.value.log = nil
data.value.window = nil
data.value.focus = nil
data.value.viewtype = cfe({ value="stream" })
for n,v in pairs(data.value) do
	v.type = "hidden"
	v.checked = nil
end
htmlviewfunctions.displayform(data, nil, nil, page_info, 2)
end %>
</DL>
