local fi
+local function check_fs_depends(fs)
+ local fs = require "nixio.fs"
+
+ for path, kind in pairs(fs) 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_depends(spec)
+ if type(spec.depends) ~= "table" then
+ return true
+ end
+
+ if type(spec.depends.fs) == "table" and not check_fs_depends(spec.depends.fs) 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
+ }
+ 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
+
+ if type(subnode.file_depends) == "table" then
+ for _, v in ipairs(subnode.file_depends) do
+ spec.depends = spec.depends or {}
+ spec.depends.fs = spec.depends.fs or {}
+
+ local ft = fs.stat(v, "type")
+ if ft == "dir" then
+ spec.depends.fs[v] = "directory"
+ elseif v:match("/s?bin/") then
+ spec.depends.fs[v] = "executable"
+ else
+ spec.depends.fs[v] = "file"
+ end
+ end
+ end
+
+ if type(subnode.uci_depends) == "table" then
+ for k, v in pairs(subnode.uci_depends) do
+ spec.depends = spec.depends or {}
+ spec.depends.uci = spec.depends.uci or {}
+ spec.depends.uci[k] = v
+ end
+ end
+
+ if (subnode.sysauth_authenticator ~= nil) or
+ (subnode.sysauth ~= nil and subnode.sysauth ~= false)
+ then
+ if subnode.sysauth_authenticator == "htmlauth" then
+ spec.auth = {
+ login = true,
+ methods = { "cookie:sysauth" }
+ }
+ elseif subname == "rpc" and subnode.module == "luci.controller.rpc" then
+ spec.auth = {
+ login = false,
+ methods = { "param:auth", "cookie:sysauth" }
+ }
+ elseif subnode.module == "luci.controller.admin.uci" then
+ spec.auth = {
+ login = false,
+ methods = { "param:sid" }
+ }
+ end
+ elseif subnode.sysauth == false then
+ spec.auth = {}
+ end
+
+ for _, v in pairs(spec) do
+ if v ~= nil then
+ if not spec.action then
+ spec.title = nil
+ end
+
+ spec.satisfied = check_depends(spec)
+ json.children = json.children or {}
+ json.children[subname] = tree_to_json(subnode, spec)
+ break
+ end
+ end
+ end
+ end
+
+ return json
+end
+
function build_url(...)
local path = {...}
local url = { http.getenv("SCRIPT_NAME") or "" }
return table.concat(url, "")
end
+function _ordered_children(node)
+ local name, child, children = nil, nil, {}
+
+ for name, child in pairs(node.nodes) do
+ children[#children+1] = {
+ name = name,
+ node = child,
+ order = child.order or 100
+ }
+ end
+
+ table.sort(children, function(a, b)
+ if a.order == b.order then
+ return a.name < b.name
+ else
+ return a.order < b.order
+ end
+ end)
+
+ return children
+end
+
+local function dependencies_satisfied(node)
+ if type(node.file_depends) == "table" then
+ for _, file in ipairs(node.file_depends) do
+ local ftype = fs.stat(file, "type")
+ if ftype == "dir" then
+ local empty = true
+ for e in (fs.dir(file) or function() end) do
+ empty = false
+ end
+ if empty then
+ return false
+ end
+ elseif ftype == nil then
+ return false
+ end
+ end
+ end
+
+ if type(node.uci_depends) == "table" then
+ for config, expect_sections in pairs(node.uci_depends) do
+ if type(expect_sections) == "table" then
+ for section, expect_options in pairs(expect_sections) do
+ if type(expect_options) == "table" then
+ for option, expect_value in pairs(expect_options) do
+ local val = uci:get(config, section, option)
+ if expect_value == true and val == nil then
+ return false
+ elseif type(expect_value) == "string" then
+ if type(val) == "table" then
+ local found = false
+ for _, subval in ipairs(val) do
+ if subval == expect_value then
+ found = true
+ end
+ end
+ if not found then
+ return false
+ end
+ elseif val ~= expect_value then
+ return false
+ end
+ end
+ end
+ else
+ local val = uci:get(config, section)
+ if expect_options == true and val == nil then
+ return false
+ elseif type(expect_options) == "string" and val ~= expect_options then
+ return false
+ end
+ end
+ end
+ elseif expect_sections == true then
+ if not uci:get_first(config) then
+ return false
+ end
+ end
+ end
+ end
+
+ return true
+end
+
function node_visible(node)
if node then
return not (
+ (not dependencies_satisfied(node)) or
(not node.title or #node.title == 0) or
(not node.target or node.hidden == true) or
(type(node.target) == "table" and node.target.type == "firstchild" and
function node_childs(node)
local rv = { }
if node then
- local k, v
- for k, v in util.spairs(node.nodes,
- function(a, b)
- return (node.nodes[a].order or 100)
- < (node.nodes[b].order or 100)
- end)
- do
- if node_visible(v) then
- rv[#rv+1] = k
+ local _, child
+ for _, child in ipairs(_ordered_children(node)) do
+ if node_visible(child.node) then
+ rv[#rv+1] = child.name
end
end
end
--context._disable_memtrace()
end
-local function require_post_security(target)
+local function require_post_security(target, args)
+ if type(target) == "table" and target.type == "arcombine" and type(target.targets) == "table" then
+ return require_post_security((type(args) == "table" and #args > 0) and target.targets[2] or target.targets[1], args)
+ end
+
if type(target) == "table" then
if type(target.post) == "table" then
local param_name, required_val, request_val
return nil, nil
end
+function menu_json()
+ local tree = context.tree or createtree()
+ return tree_to_json(tree, {
+ action = {
+ ["type"] = "firstchild",
+ ["recurse"] = true
+ }
+ })
+end
+
function dispatch(request)
--context._disable_memtrace = require "luci.debug".trap_memtrace("l")
local ctx = context
assert(media, "No valid theme found")
end
- local function _ifattr(cond, key, val)
+ local function _ifattr(cond, key, val, noescape)
if cond then
local env = getfenv(3)
local scope = (type(env.self) == "table") and env.self
val = util.serialize_json(val)
end
end
- return string.format(
- ' %s="%s"', tostring(key),
- util.pcdata(tostring( val
- or (type(env[key]) ~= "function" and env[key])
- or (scope and type(scope[key]) ~= "function" and scope[key])
- or "" ))
- )
+
+ val = tostring(val or
+ (type(env[key]) ~= "function" and env[key]) or
+ (scope and type(scope[key]) ~= "function" and scope[key]) or "")
+
+ if noescape ~= true then
+ val = util.pcdata(val)
+ end
+
+ return string.format(' %s="%s"', tostring(key), val)
else
return ''
end
return
end
- if c and require_post_security(c.target) then
+ if c and require_post_security(c.target, args) then
if not test_post_security(c) then
return
end
local ctx = context
local tree = {nodes={}, inreq=true}
- local modi = {}
ctx.treecache = setmetatable({}, {__mode="v"})
ctx.tree = tree
- ctx.modifiers = modi
local scope = setmetatable({}, {__index = luci.dispatcher})
v()
end
- local function modisort(a,b)
- return modi[a].order < modi[b].order
- end
-
- for _, v in util.spairs(modi, modisort) do
- scope._NAME = v.module
- setfenv(v.func, scope)
- v.func()
- end
-
return tree
end
-function modifier(func, order)
- context.modifiers[#context.modifiers+1] = {
- func = func,
- order = order or 0,
- module
- = getfenv(2)._NAME
- }
-end
-
function assign(path, clone, title, order)
local obj = node(unpack(path))
obj.nodes = nil
-- Subdispatchers --
function _find_eligible_node(root, prefix, deep, types, descend)
- local _, cur_name, cur_node
- local childs = { }
-
- for cur_name, cur_node in pairs(root.nodes) do
- childs[#childs+1] = {
- node = cur_node,
- name = cur_name,
- order = cur_node.order or 100
- }
- end
-
- table.sort(childs, function(a, b)
- if a.order == b.order then
- return a.name < b.name
- else
- return a.order < b.order
- end
- end)
+ local children = _ordered_children(root)
if not root.leaf and deep ~= nil then
local sub_path = { unpack(prefix) }
deep = nil
end
- for _, cur_node in ipairs(childs) do
- sub_path[#prefix+1] = cur_node.name
+ local _, child
+ for _, child in ipairs(children) do
+ sub_path[#prefix+1] = child.name
- local res_path = _find_eligible_node(cur_node.node, sub_path,
+ local res_path = _find_eligible_node(child.node, sub_path,
deep, types, true)
if res_path then
return { type = "firstnode", target = _firstnode }
end
-function alias(...)
- local req = {...}
- return function(...)
- for _, r in ipairs({...}) do
- req[#req+1] = r
- end
+function _alias(self, ...)
+ local req = { unpack(self.req) }
- dispatch(req)
+ for _, r in ipairs({...}) do
+ req[#req+1] = r
end
+
+ dispatch(req)
end
-function rewrite(n, ...)
- local req = {...}
- return function(...)
- local dispatched = util.clone(context.dispatched)
+function alias(...)
+ return { type = "alias", target = _alias, req = { ... } }
+end
- for i=1,n do
- table.remove(dispatched, 1)
- end
+function _rewrite(self, ...)
+ local n = self.n
+ local req = { unpack(self.req) }
+ local dispatched = util.clone(context.dispatched)
- for i, r in ipairs(req) do
- table.insert(dispatched, i, r)
- end
+ for i=1,n do
+ table.remove(dispatched, 1)
+ end
- for _, r in ipairs({...}) do
- dispatched[#dispatched+1] = r
- end
+ for i, r in ipairs(req) do
+ table.insert(dispatched, i, r)
+ end
- dispatch(dispatched)
+ for _, r in ipairs({...}) do
+ dispatched[#dispatched+1] = r
end
+
+ dispatch(dispatched)
end
+function rewrite(n, ...)
+ return { type = "rewrite", target = _rewrite, n = n, req = { ... } }
+end
local function _call(self, ...)
local func = getfenv()[self.name]
end
+local _view = function(self, ...)
+ require "luci.template".render("view", { view = self.view })
+end
+
+function view(name)
+ return {type = "view", view = name, target = _view}
+end
+
+
local function _cbi(self, ...)
local cbi = require "luci.cbi"
local tpl = require "luci.template"
function form(model)
return {
- type = "cbi",
+ type = "form",
post = { ["cbi.submit"] = true },
model = model,
target = _form