summaryrefslogtreecommitdiffstats
path: root/freeradius3-editmacauthfile-html.lsp
blob: 756abf335281d73cdc41ab6ee610e561c0f13e01 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<% local form, viewlibrary, page_info = ... %>
<% htmlviewfunctions = require("htmlviewfunctions") %>
<% html = require("acf.html") %>

<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">
	var editEntry = '<td> \
		<a href="javascript:;"><img src="<%= html.html_escape(page_info.wwwprefix..page_info.staticdir) %>/tango/16x16/actions/list-remove.png" width="16" height="16" title="Remove entry"></a> \
		</td>';
	var addEntry = '<tr><td> \
		<a href="javascript:;"><img src="<%= html.html_escape(page_info.wwwprefix..page_info.staticdir) %>/tango/16x16/actions/list-add.png" width="16" height="16" title="Insert entry"></a> \
		</td></tr>';

	function deleteLine(){
		$(this).parent().parent().replaceWith();
	}
	function addLine(){
		addLinks($(this).parent().parent().before('<tr><td><input type="text" value=""></td></tr>').prev());
	}
	function addLinks(rows){
		rows.each(function(){
			$(this).prepend(editEntry);
			$(this).find("td > a:last").click(deleteLine);
			$(this).find("input").change(validateLine);
		});
	}
	function validateLine(){
		var regex = /^[0-9a-f]{12}$/;
		var text = $(this).val().trim().replace(/[-:\s]/g, "").toLowerCase();
		if (regex.test(text)) {
			text = text.match(/.{2}/g).join("-");
			$(this).val(text);
			$(this).removeClass("error");
		} else {
			$(this).addClass("error");
		}
	}
	function submitFile(){
		var success = true;
		var file = "";
		var lines = $("#entries").find("input").each(function(index){
			if ($(this).val() == "") {
				$(this).parent().parent().replaceWith();
			} else {
				file = file + $(this).val() + "\n";
				if ($(this).hasClass("error")) {
					success = false;
				}
			}
		});
		$("input[name='filecontent']").val(file);
		if ( ! success ) {
			return false;
		}
	}

	$(function(){
		$("#entries").find("td:empty").parent().replaceWith();
		$("#entries").find("td").contents().each(function(){
			$(this).replaceWith("<input type='text' value='" + $(this).text() + "'>");
		});
		$("#entries").find("input").each(validateLine);
		addLinks($("#entries").find("tr"));
		$("#entries").append(addEntry).find("a:last").click(addLine);
		$("form").submit(submitFile);
	});
</script>

<%
local header_level = htmlviewfunctions.displaysectionstart(cfe({label="Edit MAC Authentication File"}), page_info)
htmlviewfunctions.displayitem(form.value.filename)
htmlviewfunctions.displayitem(form.value.filesize)
htmlviewfunctions.displayitem(form.value.mtime)
local header_level2 = htmlviewfunctions.displaysectionstart(cfe({label="Entries"}), page_info, htmlviewfunctions.incrementheader(header_level))
	htmlviewfunctions.displayformstart(form, page_info)
	form.value.filename.type = "hidden"
	htmlviewfunctions.displayformitem(form.value.filename, "filename")
	form.value.filecontent.type = "hidden"
	htmlviewfunctions.displayformitem(form.value.filecontent, "filecontent")
%>
<table id="entries">
<% for line in string.gmatch(html.html_escape(form.value.filecontent.value), "([^\n]*)\n?") do %>
	<tr><td><%= line %></td></tr>
<% end %>
</table>
<%
	htmlviewfunctions.displayinfo(form.value.filecontent)
	htmlviewfunctions.displayformend(form)
htmlviewfunctions.displaysectionend(header_level2)
htmlviewfunctions.displaysectionend(header_level)
%>