summaryrefslogtreecommitdiffstats
path: root/lib/modelfunctions.lua
blob: efc36933718ec36463867d60097ec2dd72aed3a4 (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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
module(..., package.seeall)

-- Load libraries
fs = require("acf.fs")
format = require("acf.format")
processinfo = require("acf.processinfo")

function getenabled(processname)
	local result = cfe({ label = "Program status", name=processname })
	result.value, result.errtxt = processinfo.daemoncontrol(processname, "status")
	if string.find(result.value, ": not found") then
		result.value = ""
		result.errtxt = "Program not installed"
	else
		result.value = string.gsub(result.value, "* status: ", "")
		result.value = string.gsub(result.value, "^%l", string.upper)
	end
	return result
end

function get_startstop(servicename)
	local service = cfe({ type="hidden", value=servicename, label="Service Name" })
        local actions, descr = processinfo.daemon_actions(servicename)
	local errtxt
	if not actions then
		errtxt = descr
	end
	return cfe({ type="group", label="Management", value={servicename=service}, option=actions, errtxt=errtxt })
end

function startstop_service(startstop, action)
	if not action then
		startstop.errtxt = "Invalid Action"
	else
		local reverseactions = {}
		for i,act in ipairs(startstop.option) do reverseactions[string.lower(act)] = i end
		if reverseactions[string.lower(action)] then
			local cmdresult, errtxt = processinfo.daemoncontrol(startstop.value.servicename.value, string.lower(action))
			startstop.descr = cmdresult
			startstop.errtxt = errtxt
		else
			startstop.errtxt = "Unknown command!"
		end
	end
	return startstop
end

function getstatus(processname, packagename, label, servicename)
	local status = {}
	
	local value, errtxt = processinfo.package_version(packagename)
	status.version = cfe({
		label="Program version",
		value=value,
		errtxt=errtxt,
		name=packagename
		})

	status.status = getenabled(processname)

	local autostart_value, autostart_errtxt = processinfo.process_autostart(servicename or processname)
	status.autostart = cfe({
		label="Autostart status",
		value=autostart_value,
		errtxt=autostart_errtxt,
		name=servicename or processname
		})
	
	return cfe({ type="group", value=status, label=label })
end

function getfiledetails(file, validatefilename, validatefiledetails)
	local filename = cfe({ value=file or "", label="File name" })
	local filecontent = cfe({ type="longtext", label="File content" })
	local filesize = cfe({ value="0", label="File size" })
	local mtime = cfe({ value="---", label="File date" })
	local filedetails = cfe({ type="group", value={filename=filename, filecontent=filecontent, filesize=filesize, mtime=mtime}, label="Config file details" })
	local success = true
	if type(validatefilename) == "function" then
		success = validatefilename(filedetails.value.filename.value)
		if not success then
			filedetails.value.filename.errtxt = "Invalid File"
		end
	elseif type(validatefilename) == "table" then
		success = false
		filedetails.value.filename.errtxt = "Invalid File"
		for i,f in ipairs(validatefilename) do	
			if f == filedetails.value.filename.value then
				success = true
				filedetails.value.filename.errtxt = nil
			end
		end
	end
	if success then
		if fs.is_file(file) then
			local filedetails = fs.stat(file)
			filecontent.value = fs.read_file(file) or ""
			filesize.value = filedetails.size
			mtime.value = filedetails.mtime
		else
			filename.errtxt = "File not found"
		end
		if validatefiledetails then
			success, filedetails = validatefiledetails(filedetails)
		end
	end
	return filedetails
end

function setfiledetails(filedetails, validatefilename, validatefiledetails)
	filedetails.value.filecontent.value = string.gsub(format.dostounix(filedetails.value.filecontent.value), "\n+$", "")
	local success = true
	if type(validatefilename) == "function" then
		success = validatefilename(filedetails.value.filename.value)
		if not success then
			filedetails.value.filename.errtxt = "Invalid File"
		end
	elseif type(validatefilename) == "table" then
		success = false
		filedetails.value.filename.errtxt = "Invalid File"
		for i,f in ipairs(validatefilename) do	
			if f == filedetails.value.filename.value then
				success = true
				filedetails.value.filename.errtxt = nil
			end
		end
	end
	if success and type(validatefiledetails) == "function" then
		success, filedetails = validatefiledetails(filedetails)
	end
	if success then
		--fs.write_file(filedetails.value.filename.value, filedetails.value.filecontent.value)
		write_file_with_audit(filedetails.value.filename.value, filedetails.value.filecontent.value)
		filedetails = getfiledetails(filedetails.value.filename.value)
	else
		filedetails.errtxt = "Failed to set file"
	end

	return filedetails
end

function validateselect(select)
	for i,option in ipairs(select.option) do
		if type(option) == "string" and option == select.value then
			return true
		elseif type(option) == "table" and option.value == select.value then
			return true
		end
	end
	select.errtxt = "Invalid selection"
	return false
end

function validatemulti(multi)
	local reverseoption = {}
	for i,option in ipairs(multi.option) do
		if type(option) == "string" then
			reverseoption[option] = i
		else
			reverseoption[option.value] = i
		end
	end
	for i,value in ipairs(multi.value) do
		if not reverseoption[value] then
			multi.errtxt = "Invalid selection"
			return false
		end
	end
	return true
end

function write_file_with_audit (self, path, str)
	-- if there are only two parameters, assume self was omitted
	if not str then
		str = path
		path = self
		self = nil
	end
	-- attempt to find self
	if not self then
		if SELF and #SELF > 0 then
			self = SELF[#SELF]
		elseif APP then
			self = APP
		end
	end

	if self then
		local pre = ""
		local post = ""

		local tmpfile = (self.conf.sessiondir or "/tmp/") .. 
			(self.sessiondata.userinfo.userid or "unknown") .. "-" ..
			 os.time() .. ".tmp"
	
		if type(self.conf) == "table" then
			-- we make temporary globals for expand_bash_syntax_vars
			local a,b,c = TEMPFILE,CONFFILE,_G.self
			TEMPFILE=tmpfile
			CONFFILE=path
			_G.self=self

			pre = self.conf.audit_precommit or ""
			post = self.conf.audit_postcommit or ""

			local m = self.conf.app_hooks[self.conf.controller] or {}
			if m.audit_precommit then pre = m.audit_precommit end
			if m.audit_postcommit then post = m.audit_postcommit end
			m=nil

			if (type(pre) == "string") then 
				pre = format.expand_bash_syntax_vars(pre)
			end
			if type (post) == "string" then
				post = format.expand_bash_syntax_vars(post)
			end
			TEMPFILE,CONFFILE,_G.self = a,b,c
		end

		fs.write_file(tmpfile,str)
		fs.copy_properties(path, tmpfile)

		if (type(pre) == "string" and #pre) then
			os.execute(pre)
		elseif (type(pre) == "function") then
			pre(self, path, tmpfile)
		end

		fs.move_file(tmpfile, path)

		if (type(post) == "string" and #post) then
			os.execute(post)
		elseif (type(post) == "function") then
			post(self, path, tmpfile)
		end
	else
		fs.write_file(path,str)
	end

	return
end