+local function check_fs_depends(spec)
+ local fs = require "nixio.fs"
+
+ for path, kind in pairs(spec) do
+ if kind == "directory" then
+ local empty = true
+ for entry in (fs.dir(path) or function() end) do
+ empty = false
+ break
+ end
+ if empty then
+ return false
+ end
+ elseif kind == "executable" then
+ if fs.stat(path, "type") ~= "reg" or not fs.access(path, "x") then
+ return false
+ end
+ elseif kind == "file" then
+ if fs.stat(path, "type") ~= "reg" then
+ return false
+ end
+ end
+ end
+
+ return true
+end
+
+local function check_uci_depends_options(conf, s, opts)
+ local uci = require "luci.model.uci"
+
+ if type(opts) == "string" then
+ return (s[".type"] == opts)
+ elseif opts == true then
+ for option, value in pairs(s) do
+ if option:byte(1) ~= 46 then
+ return true
+ end
+ end
+ elseif type(opts) == "table" then
+ for option, value in pairs(opts) do
+ local sval = s[option]
+ if type(sval) == "table" then
+ local found = false
+ for _, v in ipairs(sval) do
+ if v == value then
+ found = true
+ break
+ end
+ end
+ if not found then
+ return false
+ end
+ elseif value == true then
+ if sval == nil then
+ return false
+ end
+ else
+ if sval ~= value then
+ return false
+ end
+ end
+ end
+ end
+
+ return true
+end
+
+local function check_uci_depends_section(conf, sect)
+ local uci = require "luci.model.uci"
+
+ for section, options in pairs(sect) do
+ local stype = section:match("^@([A-Za-z0-9_%-]+)$")
+ if stype then
+ local found = false
+ uci:foreach(conf, stype, function(s)
+ if check_uci_depends_options(conf, s, options) then
+ found = true
+ return false
+ end
+ end)
+ if not found then
+ return false
+ end
+ else
+ local s = uci:get_all(conf, section)
+ if not s or not check_uci_depends_options(conf, s, options) then
+ return false
+ end
+ end
+ end
+
+ return true
+end
+
+local function check_uci_depends(conf)
+ local uci = require "luci.model.uci"
+
+ for config, values in pairs(conf) do
+ if values == true then
+ local found = false
+ uci:foreach(config, nil, function(s)
+ found = true
+ return false
+ end)
+ if not found then
+ return false
+ end
+ elseif type(values) == "table" then
+ if not check_uci_depends_section(config, values) then
+ return false
+ end
+ end
+ end
+
+ return true
+end
+
+local function check_acl_depends(require_groups, groups)
+ if type(require_groups) == "table" and #require_groups > 0 then
+ local writable = false
+
+ for _, group in ipairs(require_groups) do
+ local read = false
+ local write = false
+ if type(groups) == "table" and type(groups[group]) == "table" then
+ for _, perm in ipairs(groups[group]) do
+ if perm == "read" then
+ read = true
+ elseif perm == "write" then
+ write = true
+ end
+ end
+ end
+ if not read and not write then
+ return nil
+ elseif write then
+ writable = true
+ end
+ end
+
+ return writable
+ end
+
+ return true
+end
+
+local function check_depends(spec)
+ if type(spec.depends) ~= "table" then
+ return true
+ end
+
+ if type(spec.depends.fs) == "table" then
+ local satisfied = false
+ local alternatives = (#spec.depends.fs > 0) and spec.depends.fs or { spec.depends.fs }
+ for _, alternative in ipairs(alternatives) do
+ if check_fs_depends(alternative) then
+ satisfied = true
+ break
+ end
+ end
+ if not satisfied then
+ return false
+ end
+ end
+
+ if type(spec.depends.uci) == "table" then
+ local satisfied = false
+ local alternatives = (#spec.depends.uci > 0) and spec.depends.uci or { spec.depends.uci }
+ for _, alternative in ipairs(alternatives) do
+ if check_uci_depends(alternative) then
+ satisfied = true
+ break
+ end
+ end
+ if not satisfied then
+ return false
+ end
+ end
+
+ return true
+end
+
+local function target_to_json(target, module)
+ local action
+
+ if target.type == "call" then
+ action = {
+ ["type"] = "call",
+ ["module"] = module,
+ ["function"] = target.name,
+ ["parameters"] = target.argv
+ }
+ elseif target.type == "view" then
+ action = {
+ ["type"] = "view",
+ ["path"] = target.view
+ }
+ elseif target.type == "template" then
+ action = {
+ ["type"] = "template",
+ ["path"] = target.view
+ }
+ elseif target.type == "cbi" then
+ action = {
+ ["type"] = "cbi",
+ ["path"] = target.model,
+ ["config"] = target.config
+ }
+ elseif target.type == "form" then
+ action = {
+ ["type"] = "form",
+ ["path"] = target.model
+ }
+ elseif target.type == "firstchild" then
+ action = {
+ ["type"] = "firstchild"
+ }
+ elseif target.type == "firstnode" then
+ action = {
+ ["type"] = "firstchild",
+ ["recurse"] = true
+ }
+ elseif target.type == "arcombine" then
+ if type(target.targets) == "table" then
+ action = {
+ ["type"] = "arcombine",
+ ["targets"] = {
+ target_to_json(target.targets[1], module),
+ target_to_json(target.targets[2], module)
+ }
+ }
+ end
+ elseif target.type == "alias" then
+ action = {
+ ["type"] = "alias",
+ ["path"] = table.concat(target.req, "/")
+ }
+ elseif target.type == "rewrite" then
+ action = {
+ ["type"] = "rewrite",
+ ["path"] = table.concat(target.req, "/"),
+ ["remove"] = target.n
+ }
+ end
+
+ if target.post and action then
+ action.post = target.post
+ end
+
+ return action
+end
+
+local function tree_to_json(node, json)
+ local fs = require "nixio.fs"
+ local util = require "luci.util"
+
+ if type(node.nodes) == "table" then
+ for subname, subnode in pairs(node.nodes) do
+ local spec = {
+ title = util.striptags(subnode.title),
+ order = subnode.order
+ }
+
+ if subnode.leaf then
+ spec.wildcard = true
+ end
+
+ if subnode.cors then
+ spec.cors = true
+ end
+
+ if subnode.setuser then
+ spec.setuser = subnode.setuser
+ end
+
+ if subnode.setgroup then
+ spec.setgroup = subnode.setgroup
+ end
+
+ if type(subnode.target) == "table" then
+ spec.action = target_to_json(subnode.target, subnode.module)
+ end