summaryrefslogtreecommitdiffstats
path: root/skins-model.lua
blob: d4f7c3455fb889008b6b4cc4a708d02730c511b3 (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
-- acf model for displaying logfiles recusivly 
module (..., package.seeall)

require("fs")
require("format")

local function set_skins(skin)
	local conffile = "/etc/acf/acf.conf"
	local content = "\n"..(fs.read_file(conffile) or "")
	local count
	content,count = string.gsub(content, "\n%s*skin%s*=[^\n]*", "\nskin="..format.escapespecialcharacters(skin))
	if count == 0 then
		content = "\nskin="..format.escapespecialcharacters(skin)..content
	end
	fs.write_file(conffile, string.sub(content,2))
	local cmdoutput = "New skin selected"
	return cmdoutput, errtxt
end

local function list_skins()
	local path = "/usr/share/acf/www/skins/"
	local skinarray = {}
	for i,file in ipairs(posix.dir(path) or {}) do
		-- Ignore files that begins with a '.' and 'cgi-bin' and only list folders
		if not ((string.match(file, "^%.")) or (string.match(file, "^cgi[-]bin")) or (string.match(file, "^static")) or (posix.stat(path .. file).type ~= "directory")) then
			local entry = cfe({ value=file, label="Skin name" })
			local current = conf.skin
			entry.inuse = (file == current)
			table.insert(skinarray, entry)
		end
	end
	return cfe({ type="list", value=skinarray, label="Skins" })
end


get = function ()
	return list_skins()
end

update = function (newskin)
	-- Make sure no one can inject code into the model.
	local availableskins = list_skins()
	local cmdoutput = "Failed to set skin"
	local errtxt = "Invalid selection"
	for i,skin in ipairs(availableskins.value) do
		if ( skin.value == newskin) then
			cmdoutput, errtxt = set_skins(newskin)
		end
	end
	return cfe({ value=cmdoutput, errtxt=errtxt, label="Set skin result" })
end