summaryrefslogtreecommitdiffstats
path: root/tinydns-edit-html.lsp
blob: 33169d527a9433c2d5fd72f3dec57436578be468 (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<% local form, viewlibrary, page_info = ... %>
<% require("viewfunctions") %>

<% local domain = string.gsub(form.value.filename.value, "^.*/", "") %>
<script type="text/javascript" src="<%= html.html_escape(page_info.wwwprefix) %>/js/jquery-latest.js"></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-add.png" width="16" height="16" title="Insert record"></a> \
		<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 record"></a> \
		<a href="javascript:;"><img src="<%= html.html_escape(page_info.wwwprefix..page_info.staticdir) %>/tango/16x16/actions/document-properties.png" width="16" height="16" title="Edit record"></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 record"></a> \
		</td></tr>';

	function Entry(entryType,descr,num,descr0,descr1,descr2,descr3,descr4,descr5,descr6,descr7,descr8,descr9,descr10){
		this.entryType=entryType;
		this.descr=descr;
		this.num=num;this.descriptions=new Array(11);
		this.descriptions[0]=descr0;this.descriptions[1]=descr1;this.descriptions[2]=descr2;this.descriptions[3]=descr3;this.descriptions[4]=descr4;this.descriptions[5]=descr5;this.descriptions[6]=descr6;this.descriptions[7]=descr7;this.descriptions[8]=descr8;this.descriptions[9]=descr9;this.descriptions[10]=descr10;
	}

	var entryTypes = new Array(14);
	entryTypes[0]=new Entry("","Comment line",1,"Comment");
	entryTypes[1]=new Entry(".","Name server",6,"Domain","IP address","Name server","Time to live","Timestamp","Location");
	entryTypes[2]=new Entry("&","Delegate subdomain",6,"Domain","IP address","Name server","Time to live","Timestamp","Location");
	entryTypes[3]=new Entry("=","Host",5,"Host","IP address","Time to live","Timestamp","Location");
	entryTypes[4]=new Entry("+","Alias",5,"Alias","IP address","Time to live","Timestamp","Location");
	entryTypes[5]=new Entry("@","Mail exchanger",7,"Domain","IP address","Mail exchanger","Distance","Time to live","Timestamp","Location"); 
	entryTypes[6]=new Entry("'","Text record",5,"Domain","Text Record","Time to live","Timestamp","Location");
	entryTypes[7]=new Entry("^","Reverse record",5,"PTR","Domain name","Time to live","Timestamp","Location");
	entryTypes[8]=new Entry("C","Canonical name",5,"Domain","Canonical name","Time to live","Timestamp","Location");
	entryTypes[9]=new Entry("Z","SOA record",11,"Domain","Primary name server","Contact address","Serial number","Refresh time","Retry time","Expire time","Minimum time","Time to live","Timestamp","Location");
	entryTypes[10]=new Entry(":","Generic record",6,"Domain","Record type","Record data","Time to live","Timestamp","Location");
	entryTypes[11]=new Entry("%","Client location",2,"Location","IP prefix");
	entryTypes[12]=new Entry("S","Service location",8,"Domain Service", "IP address", "Server", "Port", "Weight", "Priority", "Time to live", "Timestamp");
	entryTypes[13]=new Entry("N","Naming authority pointer",9,"Domain", "Order", "Preference", "Flags", "Service", "Regular expression", "Replacement", "Time to live", "Timestamp");

	function octal_to_char(val){
		return String.fromCharCode(parseInt(val.substring(1), 8))
	}
	function char_to_octal(val){
		var code = "000" + val.charCodeAt(0).toString(8)
		return "\\" + code.slice(-3)
	}
	function finishForm(entry, force){
		var addresspresent = false;
		var notindomain = false;
		var entrytext = entry.find("select").val() + entry.find("input:not(.submit)").map(function(){
			var val = $(this).val().replace(/^\s\s*/, '').replace(/\s\s*$/, '');
			if (this.name != "dontconvertme") {
				val = val.replace(/[\\:\r\n ]/g, char_to_octal);
			}
			if (this.name == "fixme") {
				if (val != "")
					val = val + ".<%= html.html_escape(domain) %>";
				else
					val = "<%= html.html_escape(domain) %>";
			}
			if (this.name == "address" && val != "")
				addresspresent = true;
			if (this.name == "testme" && val != "" && null == val.match(/\.?<%= string.gsub(html.html_escape(domain), "[\\%[%]%.%*%?%+%{%^%$]", "\\%1") %>$/i))
				notindomain = true;
			return val
		}).get().join(":");
		if (addresspresent && notindomain) {
			if (force)
				entry.empty().append(entrytext);
			return false;
		}
		else
			entry.empty().append(entrytext);
		return true;
	}
	function createForm(entry){
		var entrytext = entry.text();
		var entryType = entryTypes[0];
		var form = '<select>';
		var typechar = entrytext.charAt(0);
		if (typechar === null) { typechar = ""; }
		for (i=0; i<14; i++) {
			form = form + '<option ';
			if (typechar == entryTypes[i].entryType){
				entryType=entryTypes[i];
				form = form + 'selected ';
			}
			form = form + 'value="' + entryTypes[i].entryType + '">' + entryTypes[i].descr + '</option>';
		}
		form = form + "</select><br><dl>";
		var entries;
		if (entryType.entryType == ""){
			entries = new Array(1);
			entries[0] = entrytext;
		} else {
			entrytext = entrytext.substring(1,entrytext.length) + "::::::::::";
			entries = entrytext.split(":");
		}
		for (i=0; i<entryType.num; i++){
			if (entries[i] === null) { entries[i] = ""; }
			var name = "";
			var value = entries[i];
			var extra = "";
			if (entryType.descriptions[i] == "Comment")
				name = "dontconvertme"
			else {
				value = value.replace(/\\\d{3}/g, octal_to_char);
				if (entryType.descriptions[i] == "Domain" || entryType.descriptions[i] == "Alias" || entryType.descriptions[i] == "Host" || entryType.descriptions[i] == "Domain Service" ) {
					name = "fixme";
					value = value.replace(/\.?<%= string.gsub(html.html_escape(domain), "[\\%[%]%.%*%?%+%{%^%$]", "\\%1") %>$/i, "")
					extra = " . <%= html.html_escape(domain) %>"
				}
				else if (entryType.descriptions[i] == "IP address")
					name = "address";
				else if (entryType.descriptions[i] == "Name server" || entryType.descriptions[i] == "Mail exchanger" || entryType.descriptions[i] == "Server")
					name = "testme";

			}
			form = form + '<dt>' + entryType.descriptions[i] + '</dt><dd><input type="text" value="' + value + '" name="' + name + '">' + extra + '</dd>';
		}
		form = form + "\n</dl>";
		entry.empty().append(form);
		entry.find("select").change(function(){
			var entry = $(this).parent();
			finishForm(entry, true);
			createForm(entry);
		});
		entry.find("input.submit").click(function() {
			$("input[name='<%= html.html_escape(form.option) %>']").click();
		});
	}
	function editLine(){
		var parent = $(this).parent();
		if (parent.is("td")) {
			parent = parent.parent();
		}
		var entry = parent.find("td:eq(1)");
		if (entry.find("select").is("select")){
			if ( ! finishForm(entry) )
				alert("Error: Cannot set IP address for host outside of <%= html.html_escape(domain) %> domain");
		} else {
			createForm(entry);
		}
	}
	function deleteLine(){
		$(this).parent().parent().replaceWith();
	}
	function addLine(){
		addLinks($(this).parent().parent().before('<tr><td></td></tr>').prev());
	}
	function addLinks(rows){
		rows.each(function(){
			$(this).prepend(editEntry);
			$(this).find("td > a:eq(0)").click(addLine);
			$(this).find("td > a:eq(1)").click(deleteLine);
			$(this).find("td > a:eq(2)").click(editLine);
			$(this).find("td:eq(1)").dblclick(editLine);
		});
	}
	function submitFile(){
		var success = true;
		var file = "";
		var lines = $("#entries").find("tr").find("td:eq(1)");
		lines.each(function(){
			var elem = jQuery(this);
			if (elem.find("select").is("select")){
				success = finishForm(elem) && success;
			}
			file = file + elem.text() + "\n";
		});
		$("input[name='filecontent']").val(file);
		if ( ! success ) {
			alert("Error: Cannot set IP address for host outside of <%= html.html_escape(domain) %> domain");
			return false;
		}
	}

	$(function(){
		addLinks($("#entries").find("tr"));
		$("#entries").append(addEntry).find("a:last").click(addLine);
		$("form").submit(submitFile);
		<% if form.value.filecontent.linenumber then %>
			$("#entries tr:eq(<%= form.value.filecontent.linenumber - 1 %>)").find("a:eq(2)").click();
			var top = $("#entries tr:eq(<%= form.value.filecontent.linenumber - 1 %>)").offset().top;
			$("html,body").scrollTop(top);
		<% end %>
	});
</script>

<H1>Configuration</H1>
<H2>Expert Configuration</H2>
<H3>File Details</H3>
<DL>
<% 
displayitem(form.value.filename)
displayitem(form.value.filesize)
displayitem(form.value.mtime)
%>
</DL>

<H3>File Entries</H3>
<% if form.descr then %><P CLASS='descr'><%= string.gsub(html.html_escape(form.descr), "\n", "<BR>") %></P><% end %>
<% if form.errtxt then %><P CLASS='error'><%= string.gsub(html.html_escape(form.errtxt), "\n", "<BR>") %></P><% end %>
<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>
<% if form.value.filecontent.errtxt then %><P CLASS='error'><%= string.gsub(html.html_escape(form.value.filecontent.errtxt), "\n", "<BR>") %></P><% end %>

<% form.action = page_info.script .. page_info.prefix .. page_info.controller .. "/" .. page_info.action %>
<% displayformstart(form) %>
<input type="hidden" name="filename" value="<%= html.html_escape(form.value.filename.value) %>">
<input type="hidden" name="filecontent" value="<%= html.html_escape(form.value.filecontent.value) %>">
<H2>Save and Apply Above Settings</H2>
<% displayformend(form) %>