12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850 |
- -- Copyright 2008 Steven Barth <steven@midlink.org>
- -- Licensed to the public under the Apache License 2.0.
- module("luci.cbi", package.seeall)
- require("luci.template")
- local util = require("luci.util")
- require("luci.http")
- --local event = require "luci.sys.event"
- local fs = require("nixio.fs")
- local uci = require("luci.model.uci")
- local datatypes = require("luci.cbi.datatypes")
- local dispatcher = require("luci.dispatcher")
- local class = util.class
- local instanceof = util.instanceof
- FORM_NODATA = 0
- FORM_PROCEED = 0
- FORM_VALID = 1
- FORM_DONE = 1
- FORM_INVALID = -1
- FORM_CHANGED = 2
- FORM_SKIP = 4
- AUTO = true
- CREATE_PREFIX = "cbi.cts."
- REMOVE_PREFIX = "cbi.rts."
- RESORT_PREFIX = "cbi.sts."
- FEXIST_PREFIX = "cbi.cbe."
- -- Loads a CBI map from given file, creating an environment and returns it
- function load(cbimap, ...)
- local fs = require "nixio.fs"
- local i18n = require "luci.i18n"
- require("luci.config")
- require("luci.util")
- local upldir = "/lib/uci/upload/"
- local cbidir = luci.util.libpath() .. "/model/cbi/"
- local func, err
- if fs.access(cbidir..cbimap..".lua") then
- func, err = loadfile(cbidir..cbimap..".lua")
- elseif fs.access(cbimap) then
- func, err = loadfile(cbimap)
- else
- func, err = nil, "Model '" .. cbimap .. "' not found!"
- end
- assert(func, err)
- local env = {
- translate=i18n.translate,
- translatef=i18n.translatef,
- arg={...}
- }
- setfenv(func, setmetatable(env, {__index =
- function(tbl, key)
- return rawget(tbl, key) or _M[key] or _G[key]
- end}))
- local maps = { func() }
- local uploads = { }
- local has_upload = false
- for i, map in ipairs(maps) do
- if not instanceof(map, Node) then
- error("CBI map returns no valid map object!")
- return nil
- else
- map:prepare()
- if map.upload_fields then
- has_upload = true
- for _, field in ipairs(map.upload_fields) do
- uploads[
- field.config .. '.' ..
- (field.section.sectiontype or '1') .. '.' ..
- field.option
- ] = true
- end
- end
- end
- end
- if has_upload then
- local uci = luci.model.uci.cursor()
- local prm = luci.http.context.request.message.params
- local fd, cbid
- luci.http.setfilehandler(
- function( field, chunk, eof )
- if not field then return end
- if field.name and not cbid then
- local c, s, o = field.name:gmatch(
- "cbid%.([^%.]+)%.([^%.]+)%.([^%.]+)"
- )()
- if c and s and o then
- local t = uci:get( c, s ) or s
- if uploads[c.."."..t.."."..o] then
- local path = upldir .. field.name
- fd = io.open(path, "w")
- if fd then
- cbid = field.name
- prm[cbid] = path
- end
- end
- end
- end
- if field.name == cbid and fd then
- fd:write(chunk)
- end
- if eof and fd then
- fd:close()
- fd = nil
- cbid = nil
- end
- end
- )
- end
- return maps
- end
- --
- -- Compile a datatype specification into a parse tree for evaluation later on
- --
- local cdt_cache = { }
- function compile_datatype(code)
- local i
- local pos = 0
- local esc = false
- local depth = 0
- local stack = { }
- for i = 1, #code+1 do
- local byte = code:byte(i) or 44
- if esc then
- esc = false
- elseif byte == 92 then
- esc = true
- elseif byte == 40 or byte == 44 then
- if depth <= 0 then
- if pos < i then
- local label = code:sub(pos, i-1)
- :gsub("\\(.)", "%1")
- :gsub("^%s+", "")
- :gsub("%s+$", "")
- if #label > 0 and tonumber(label) then
- stack[#stack+1] = tonumber(label)
- elseif label:match("^'.*'$") or label:match('^".*"$') then
- stack[#stack+1] = label:gsub("[\"'](.*)[\"']", "%1")
- elseif type(datatypes[label]) == "function" then
- stack[#stack+1] = datatypes[label]
- stack[#stack+1] = { }
- else
- error("Datatype error, bad token %q" % label)
- end
- end
- pos = i + 1
- end
- depth = depth + (byte == 40 and 1 or 0)
- elseif byte == 41 then
- depth = depth - 1
- if depth <= 0 then
- if type(stack[#stack-1]) ~= "function" then
- error("Datatype error, argument list follows non-function")
- end
- stack[#stack] = compile_datatype(code:sub(pos, i-1))
- pos = i + 1
- end
- end
- end
- return stack
- end
- function verify_datatype(dt, value)
- if dt and #dt > 0 then
- if not cdt_cache[dt] then
- local c = compile_datatype(dt)
- if c and type(c[1]) == "function" then
- cdt_cache[dt] = c
- else
- error("Datatype error, not a function expression")
- end
- end
- if cdt_cache[dt] then
- return cdt_cache[dt][1](value, unpack(cdt_cache[dt][2]))
- end
- end
- return true
- end
- -- Node pseudo abstract class
- Node = class()
- function Node.__init__(self, title, description)
- self.children = {}
- self.title = title or ""
- self.description = description or ""
- self.template = "cbi/node"
- end
- -- hook helper
- function Node._run_hook(self, hook)
- if type(self[hook]) == "function" then
- return self[hook](self)
- end
- end
- function Node._run_hooks(self, ...)
- local f
- local r = false
- for _, f in ipairs(arg) do
- if type(self[f]) == "function" then
- self[f](self)
- r = true
- end
- end
- return r
- end
- -- Prepare nodes
- function Node.prepare(self, ...)
- for k, child in ipairs(self.children) do
- child:prepare(...)
- end
- end
- -- Append child nodes
- function Node.append(self, obj)
- table.insert(self.children, obj)
- end
- -- Parse this node and its children
- function Node.parse(self, ...)
- for k, child in ipairs(self.children) do
- child:parse(...)
- end
- end
- -- Render this node
- function Node.render(self, scope)
- scope = scope or {}
- scope.self = self
- luci.template.render(self.template, scope)
- end
- -- Render the children
- function Node.render_children(self, ...)
- local k, node
- for k, node in ipairs(self.children) do
- node.last_child = (k == #self.children)
- node:render(...)
- end
- end
- --[[
- A simple template element
- ]]--
- Template = class(Node)
- function Template.__init__(self, template)
- Node.__init__(self)
- self.template = template
- end
- function Template.render(self)
- luci.template.render(self.template, {self=self})
- end
- function Template.parse(self, readinput)
- self.readinput = (readinput ~= false)
- return Map.formvalue(self, "cbi.submit") and FORM_DONE or FORM_NODATA
- end
- --[[
- Map - A map describing a configuration file
- ]]--
- Map = class(Node)
- function Map.__init__(self, config, ...)
- Node.__init__(self, ...)
- self.config = config
- self.parsechain = {self.config}
- self.template = "cbi/map"
- self.apply_on_parse = nil
- self.readinput = true
- self.proceed = false
- self.flow = {}
- self.uci = uci.cursor()
- self.save = true
- self.changed = false
- local path = "%s/%s" %{ self.uci:get_confdir(), self.config }
- if fs.stat(path, "type") ~= "reg" then
- fs.writefile(path, "")
- end
- local ok, err = self.uci:load(self.config)
- if not ok then
- local url = dispatcher.build_url(unpack(dispatcher.context.request))
- local source = self:formvalue("cbi.source")
- if type(source) == "string" then
- fs.writefile(path, source:gsub("\r\n", "\n"))
- ok, err = self.uci:load(self.config)
- if ok then
- luci.http.redirect(url)
- end
- end
- self.save = false
- end
- if not ok then
- self.template = "cbi/error"
- self.error = err
- self.source = fs.readfile(path) or ""
- self.pageaction = false
- end
- end
- function Map.formvalue(self, key)
- return self.readinput and luci.http.formvalue(key)
- end
- function Map.formvaluetable(self, key)
- return self.readinput and luci.http.formvaluetable(key) or {}
- end
- function Map.get_scheme(self, sectiontype, option)
- if not option then
- return self.scheme and self.scheme.sections[sectiontype]
- else
- return self.scheme and self.scheme.variables[sectiontype]
- and self.scheme.variables[sectiontype][option]
- end
- end
- function Map.submitstate(self)
- return self:formvalue("cbi.submit")
- end
- -- Chain foreign config
- function Map.chain(self, config)
- table.insert(self.parsechain, config)
- end
- function Map.state_handler(self, state)
- return state
- end
- -- Use optimized UCI writing
- function Map.parse(self, readinput, ...)
- self.readinput = (readinput ~= false)
- self:_run_hooks("on_parse")
- if self:formvalue("cbi.skip") then
- self.state = FORM_SKIP
- return self:state_handler(self.state)
- end
- Node.parse(self, ...)
- if self.save then
- self:_run_hooks("on_save", "on_before_save")
- for i, config in ipairs(self.parsechain) do
- self.uci:save(config)
- end
- self:_run_hooks("on_after_save")
- if self:submitstate() and ((not self.proceed and self.flow.autoapply) or luci.http.formvalue("cbi.apply")) then
- self:_run_hooks("on_before_commit")
- for i, config in ipairs(self.parsechain) do
- self.uci:commit(config)
- -- Refresh data because commit changes section names
- self.uci:load(config)
- end
- self:_run_hooks("on_commit", "on_after_commit", "on_before_apply")
- if self.apply_on_parse then
- self.uci:apply(self.parsechain)
- self:_run_hooks("on_apply", "on_after_apply")
- else
- -- This is evaluated by the dispatcher and delegated to the
- -- template which in turn fires XHR to perform the actual
- -- apply actions.
- self.apply_needed = true
- end
- -- Reparse sections
- Node.parse(self, true)
- end
- for i, config in ipairs(self.parsechain) do
- self.uci:unload(config)
- end
- if type(self.commit_handler) == "function" then
- self:commit_handler(self:submitstate())
- end
- end
- if self:submitstate() then
- if not self.save then
- self.state = FORM_INVALID
- elseif self.proceed then
- self.state = FORM_PROCEED
- else
- self.state = self.changed and FORM_CHANGED or FORM_VALID
- end
- else
- self.state = FORM_NODATA
- end
- return self:state_handler(self.state)
- end
- function Map.render(self, ...)
- self:_run_hooks("on_init")
- Node.render(self, ...)
- end
- -- Creates a child section
- function Map.section(self, class, ...)
- if instanceof(class, AbstractSection) then
- local obj = class(self, ...)
- self:append(obj)
- return obj
- else
- error("class must be a descendent of AbstractSection")
- end
- end
- -- UCI add
- function Map.add(self, sectiontype)
- return self.uci:add(self.config, sectiontype)
- end
- -- UCI set
- function Map.set(self, section, option, value)
- if type(value) ~= "table" or #value > 0 then
- if option then
- return self.uci:set(self.config, section, option, value)
- else
- return self.uci:set(self.config, section, value)
- end
- else
- return Map.del(self, section, option)
- end
- end
- -- UCI del
- function Map.del(self, section, option)
- if option then
- return self.uci:delete(self.config, section, option)
- else
- return self.uci:delete(self.config, section)
- end
- end
- -- UCI get
- function Map.get(self, section, option)
- if not section then
- return self.uci:get_all(self.config)
- elseif option then
- return self.uci:get(self.config, section, option)
- else
- return self.uci:get_all(self.config, section)
- end
- end
- --[[
- Compound - Container
- ]]--
- Compound = class(Node)
- function Compound.__init__(self, ...)
- Node.__init__(self)
- self.template = "cbi/compound"
- self.children = {...}
- end
- function Compound.populate_delegator(self, delegator)
- for _, v in ipairs(self.children) do
- v.delegator = delegator
- end
- end
- function Compound.parse(self, ...)
- local cstate, state = 0
- for k, child in ipairs(self.children) do
- cstate = child:parse(...)
- state = (not state or cstate < state) and cstate or state
- end
- return state
- end
- --[[
- Delegator - Node controller
- ]]--
- Delegator = class(Node)
- function Delegator.__init__(self, ...)
- Node.__init__(self, ...)
- self.nodes = {}
- self.defaultpath = {}
- self.pageaction = false
- self.readinput = true
- self.allow_reset = false
- self.allow_cancel = false
- self.allow_back = false
- self.allow_finish = false
- self.template = "cbi/delegator"
- end
- function Delegator.set(self, name, node)
- assert(not self.nodes[name], "Duplicate entry")
- self.nodes[name] = node
- end
- function Delegator.add(self, name, node)
- node = self:set(name, node)
- self.defaultpath[#self.defaultpath+1] = name
- end
- function Delegator.insert_after(self, name, after)
- local n = #self.chain + 1
- for k, v in ipairs(self.chain) do
- if v == after then
- n = k + 1
- break
- end
- end
- table.insert(self.chain, n, name)
- end
- function Delegator.set_route(self, ...)
- local n, chain, route = 0, self.chain, {...}
- for i = 1, #chain do
- if chain[i] == self.current then
- n = i
- break
- end
- end
- for i = 1, #route do
- n = n + 1
- chain[n] = route[i]
- end
- for i = n + 1, #chain do
- chain[i] = nil
- end
- end
- function Delegator.get(self, name)
- local node = self.nodes[name]
- if type(node) == "string" then
- node = load(node, name)
- end
- if type(node) == "table" and getmetatable(node) == nil then
- node = Compound(unpack(node))
- end
- return node
- end
- function Delegator.parse(self, ...)
- if self.allow_cancel and Map.formvalue(self, "cbi.cancel") then
- if self:_run_hooks("on_cancel") then
- return FORM_DONE
- end
- end
- if not Map.formvalue(self, "cbi.delg.current") then
- self:_run_hooks("on_init")
- end
- local newcurrent
- self.chain = self.chain or self:get_chain()
- self.current = self.current or self:get_active()
- self.active = self.active or self:get(self.current)
- assert(self.active, "Invalid state")
- local stat = FORM_DONE
- if type(self.active) ~= "function" then
- self.active:populate_delegator(self)
- stat = self.active:parse()
- else
- self:active()
- end
- if stat > FORM_PROCEED then
- if Map.formvalue(self, "cbi.delg.back") then
- newcurrent = self:get_prev(self.current)
- else
- newcurrent = self:get_next(self.current)
- end
- elseif stat < FORM_PROCEED then
- return stat
- end
- if not Map.formvalue(self, "cbi.submit") then
- return FORM_NODATA
- elseif stat > FORM_PROCEED
- and (not newcurrent or not self:get(newcurrent)) then
- return self:_run_hook("on_done") or FORM_DONE
- else
- self.current = newcurrent or self.current
- self.active = self:get(self.current)
- if type(self.active) ~= "function" then
- self.active:populate_delegator(self)
- local stat = self.active:parse(false)
- if stat == FORM_SKIP then
- return self:parse(...)
- else
- return FORM_PROCEED
- end
- else
- return self:parse(...)
- end
- end
- end
- function Delegator.get_next(self, state)
- for k, v in ipairs(self.chain) do
- if v == state then
- return self.chain[k+1]
- end
- end
- end
- function Delegator.get_prev(self, state)
- for k, v in ipairs(self.chain) do
- if v == state then
- return self.chain[k-1]
- end
- end
- end
- function Delegator.get_chain(self)
- local x = Map.formvalue(self, "cbi.delg.path") or self.defaultpath
- return type(x) == "table" and x or {x}
- end
- function Delegator.get_active(self)
- return Map.formvalue(self, "cbi.delg.current") or self.chain[1]
- end
- --[[
- Page - A simple node
- ]]--
- Page = class(Node)
- Page.__init__ = Node.__init__
- Page.parse = function() end
- --[[
- SimpleForm - A Simple non-UCI form
- ]]--
- SimpleForm = class(Node)
- function SimpleForm.__init__(self, config, title, description, data)
- Node.__init__(self, title, description)
- self.config = config
- self.data = data or {}
- self.template = "cbi/simpleform"
- self.dorender = true
- self.pageaction = false
- self.readinput = true
- end
- SimpleForm.formvalue = Map.formvalue
- SimpleForm.formvaluetable = Map.formvaluetable
- function SimpleForm.parse(self, readinput, ...)
- self.readinput = (readinput ~= false)
- if self:formvalue("cbi.skip") then
- return FORM_SKIP
- end
- if self:formvalue("cbi.cancel") and self:_run_hooks("on_cancel") then
- return FORM_DONE
- end
- if self:submitstate() then
- Node.parse(self, 1, ...)
- end
- local valid = true
- for k, j in ipairs(self.children) do
- for i, v in ipairs(j.children) do
- valid = valid
- and (not v.tag_missing or not v.tag_missing[1])
- and (not v.tag_invalid or not v.tag_invalid[1])
- and (not v.error)
- end
- end
- local state =
- not self:submitstate() and FORM_NODATA
- or valid and FORM_VALID
- or FORM_INVALID
- self.dorender = not self.handle
- if self.handle then
- local nrender, nstate = self:handle(state, self.data)
- self.dorender = self.dorender or (nrender ~= false)
- state = nstate or state
- end
- return state
- end
- function SimpleForm.render(self, ...)
- if self.dorender then
- Node.render(self, ...)
- end
- end
- function SimpleForm.submitstate(self)
- return self:formvalue("cbi.submit")
- end
- function SimpleForm.section(self, class, ...)
- if instanceof(class, AbstractSection) then
- local obj = class(self, ...)
- self:append(obj)
- return obj
- else
- error("class must be a descendent of AbstractSection")
- end
- end
- -- Creates a child field
- function SimpleForm.field(self, class, ...)
- local section
- for k, v in ipairs(self.children) do
- if instanceof(v, SimpleSection) then
- section = v
- break
- end
- end
- if not section then
- section = self:section(SimpleSection)
- end
- if instanceof(class, AbstractValue) then
- local obj = class(self, section, ...)
- obj.track_missing = true
- section:append(obj)
- return obj
- else
- error("class must be a descendent of AbstractValue")
- end
- end
- function SimpleForm.set(self, section, option, value)
- self.data[option] = value
- end
- function SimpleForm.del(self, section, option)
- self.data[option] = nil
- end
- function SimpleForm.get(self, section, option)
- return self.data[option]
- end
- function SimpleForm.get_scheme()
- return nil
- end
- Form = class(SimpleForm)
- function Form.__init__(self, ...)
- SimpleForm.__init__(self, ...)
- self.embedded = true
- end
- --[[
- AbstractSection
- ]]--
- AbstractSection = class(Node)
- function AbstractSection.__init__(self, map, sectiontype, ...)
- Node.__init__(self, ...)
- self.sectiontype = sectiontype
- self.map = map
- self.config = map.config
- self.optionals = {}
- self.defaults = {}
- self.fields = {}
- self.tag_error = {}
- self.tag_invalid = {}
- self.tag_deperror = {}
- self.changed = false
- self.optional = true
- self.addremove = false
- self.dynamic = false
- end
- -- Define a tab for the section
- function AbstractSection.tab(self, tab, title, desc)
- self.tabs = self.tabs or { }
- self.tab_names = self.tab_names or { }
- self.tab_names[#self.tab_names+1] = tab
- self.tabs[tab] = {
- title = title,
- description = desc,
- childs = { }
- }
- end
- -- Check whether the section has tabs
- function AbstractSection.has_tabs(self)
- return (self.tabs ~= nil) and (next(self.tabs) ~= nil)
- end
- -- Appends a new option
- function AbstractSection.option(self, class, option, ...)
- if instanceof(class, AbstractValue) then
- local obj = class(self.map, self, option, ...)
- self:append(obj)
- self.fields[option] = obj
- return obj
- elseif class == true then
- error("No valid class was given and autodetection failed.")
- else
- error("class must be a descendant of AbstractValue")
- end
- end
- -- Appends a new tabbed option
- function AbstractSection.taboption(self, tab, ...)
- assert(tab and self.tabs and self.tabs[tab],
- "Cannot assign option to not existing tab %q" % tostring(tab))
- local l = self.tabs[tab].childs
- local o = AbstractSection.option(self, ...)
- if o then l[#l+1] = o end
- return o
- end
- -- Render a single tab
- function AbstractSection.render_tab(self, tab, ...)
- assert(tab and self.tabs and self.tabs[tab],
- "Cannot render not existing tab %q" % tostring(tab))
- local k, node
- for k, node in ipairs(self.tabs[tab].childs) do
- node.last_child = (k == #self.tabs[tab].childs)
- node:render(...)
- end
- end
- -- Parse optional options
- function AbstractSection.parse_optionals(self, section)
- if not self.optional then
- return
- end
- self.optionals[section] = {}
- local field = self.map:formvalue("cbi.opt."..self.config.."."..section)
- for k,v in ipairs(self.children) do
- if v.optional and not v:cfgvalue(section) and not self:has_tabs() then
- if field == v.option then
- field = nil
- self.map.proceed = true
- else
- table.insert(self.optionals[section], v)
- end
- end
- end
- if field and #field > 0 and self.dynamic then
- self:add_dynamic(field)
- end
- end
- -- Add a dynamic option
- function AbstractSection.add_dynamic(self, field, optional)
- local o = self:option(Value, field, field)
- o.optional = optional
- end
- -- Parse all dynamic options
- function AbstractSection.parse_dynamic(self, section)
- if not self.dynamic then
- return
- end
- local arr = luci.util.clone(self:cfgvalue(section))
- local form = self.map:formvaluetable("cbid."..self.config.."."..section)
- for k, v in pairs(form) do
- arr[k] = v
- end
- for key,val in pairs(arr) do
- local create = true
- for i,c in ipairs(self.children) do
- if c.option == key then
- create = false
- end
- end
- if create and key:sub(1, 1) ~= "." then
- self.map.proceed = true
- self:add_dynamic(key, true)
- end
- end
- end
- -- Returns the section's UCI table
- function AbstractSection.cfgvalue(self, section)
- return self.map:get(section)
- end
- -- Push events
- function AbstractSection.push_events(self)
- --luci.util.append(self.map.events, self.events)
- self.map.changed = true
- end
- -- Removes the section
- function AbstractSection.remove(self, section)
- self.map.proceed = true
- return self.map:del(section)
- end
- -- Creates the section
- function AbstractSection.create(self, section)
- local stat
- if section then
- stat = section:match("^[%w_]+$") and self.map:set(section, nil, self.sectiontype)
- else
- section = self.map:add(self.sectiontype)
- stat = section
- end
- if stat then
- for k,v in pairs(self.children) do
- if v.default then
- self.map:set(section, v.option, v.default)
- end
- end
- for k,v in pairs(self.defaults) do
- self.map:set(section, k, v)
- end
- end
- self.map.proceed = true
- return stat
- end
- SimpleSection = class(AbstractSection)
- function SimpleSection.__init__(self, form, ...)
- AbstractSection.__init__(self, form, nil, ...)
- self.template = "cbi/nullsection"
- end
- Table = class(AbstractSection)
- function Table.__init__(self, form, data, ...)
- local datasource = {}
- local tself = self
- datasource.config = "table"
- self.data = data or {}
- datasource.formvalue = Map.formvalue
- datasource.formvaluetable = Map.formvaluetable
- datasource.readinput = true
- function datasource.get(self, section, option)
- return tself.data[section] and tself.data[section][option]
- end
- function datasource.submitstate(self)
- return Map.formvalue(self, "cbi.submit")
- end
- function datasource.del(...)
- return true
- end
- function datasource.get_scheme()
- return nil
- end
- AbstractSection.__init__(self, datasource, "table", ...)
- self.template = "cbi/tblsection"
- self.rowcolors = true
- self.anonymous = true
- end
- function Table.parse(self, readinput)
- self.map.readinput = (readinput ~= false)
- for i, k in ipairs(self:cfgsections()) do
- if self.map:submitstate() then
- Node.parse(self, k)
- end
- end
- end
- function Table.cfgsections(self)
- local sections = {}
- for i, v in luci.util.kspairs(self.data) do
- table.insert(sections, i)
- end
- return sections
- end
- function Table.update(self, data)
- self.data = data
- end
- --[[
- NamedSection - A fixed configuration section defined by its name
- ]]--
- NamedSection = class(AbstractSection)
- function NamedSection.__init__(self, map, section, stype, ...)
- AbstractSection.__init__(self, map, stype, ...)
- -- Defaults
- self.addremove = false
- self.template = "cbi/nsection"
- self.section = section
- end
- function NamedSection.parse(self, novld)
- local s = self.section
- local active = self:cfgvalue(s)
- if self.addremove then
- local path = self.config.."."..s
- if active then -- Remove the section
- if self.map:formvalue("cbi.rns."..path) and self:remove(s) then
- self:push_events()
- return
- end
- else -- Create and apply default values
- if self.map:formvalue("cbi.cns."..path) then
- self:create(s)
- return
- end
- end
- end
- if active then
- AbstractSection.parse_dynamic(self, s)
- if self.map:submitstate() then
- Node.parse(self, s)
- end
- AbstractSection.parse_optionals(self, s)
- if self.changed then
- self:push_events()
- end
- end
- end
- --[[
- TypedSection - A (set of) configuration section(s) defined by the type
- addremove: Defines whether the user can add/remove sections of this type
- anonymous: Allow creating anonymous sections
- validate: a validation function returning nil if the section is invalid
- ]]--
- TypedSection = class(AbstractSection)
- function TypedSection.__init__(self, map, type, ...)
- AbstractSection.__init__(self, map, type, ...)
- self.template = "cbi/tsection"
- self.deps = {}
- self.anonymous = false
- end
- -- Return all matching UCI sections for this TypedSection
- function TypedSection.cfgsections(self)
- local sections = {}
- self.map.uci:foreach(self.map.config, self.sectiontype,
- function (section)
- if self:checkscope(section[".name"]) then
- table.insert(sections, section[".name"])
- end
- end)
- return sections
- end
- -- Limits scope to sections that have certain option => value pairs
- function TypedSection.depends(self, option, value)
- table.insert(self.deps, {option=option, value=value})
- end
- function TypedSection.parse(self, novld)
- if self.addremove then
- -- Remove
- local crval = REMOVE_PREFIX .. self.config
- local name = self.map:formvaluetable(crval)
- for k,v in pairs(name) do
- if k:sub(-2) == ".x" then
- k = k:sub(1, #k - 2)
- end
- if self:cfgvalue(k) and self:checkscope(k) then
- self:remove(k)
- end
- end
- end
- local co
- for i, k in ipairs(self:cfgsections()) do
- AbstractSection.parse_dynamic(self, k)
- if self.map:submitstate() then
- Node.parse(self, k, novld)
- end
- AbstractSection.parse_optionals(self, k)
- end
- if self.addremove then
- -- Create
- local created
- local crval = CREATE_PREFIX .. self.config .. "." .. self.sectiontype
- local origin, name = next(self.map:formvaluetable(crval))
- if self.anonymous then
- if name then
- created = self:create(nil, origin)
- end
- else
- if name then
- -- Ignore if it already exists
- if self:cfgvalue(name) then
- name = nil;
- end
- name = self:checkscope(name)
- if not name then
- self.err_invalid = true
- end
- if name and #name > 0 then
- created = self:create(name, origin) and name
- if not created then
- self.invalid_cts = true
- end
- end
- end
- end
- if created then
- AbstractSection.parse_optionals(self, created)
- end
- end
- if self.sortable then
- local stval = RESORT_PREFIX .. self.config .. "." .. self.sectiontype
- local order = self.map:formvalue(stval)
- if order and #order > 0 then
- local sid
- local num = 0
- for sid in util.imatch(order) do
- self.map.uci:reorder(self.config, sid, num)
- num = num + 1
- end
- self.changed = (num > 0)
- end
- end
- if created or self.changed then
- self:push_events()
- end
- end
- -- Verifies scope of sections
- function TypedSection.checkscope(self, section)
- -- Check if we are not excluded
- if self.filter and not self:filter(section) then
- return nil
- end
- -- Check if at least one dependency is met
- if #self.deps > 0 and self:cfgvalue(section) then
- local stat = false
- for k, v in ipairs(self.deps) do
- if self:cfgvalue(section)[v.option] == v.value then
- stat = true
- end
- end
- if not stat then
- return nil
- end
- end
- return self:validate(section)
- end
- -- Dummy validate function
- function TypedSection.validate(self, section)
- return section
- end
- --[[
- AbstractValue - An abstract Value Type
- null: Value can be empty
- valid: A function returning the value if it is valid otherwise nil
- depends: A table of option => value pairs of which one must be true
- default: The default value
- size: The size of the input fields
- rmempty: Unset value if empty
- optional: This value is optional (see AbstractSection.optionals)
- ]]--
- AbstractValue = class(Node)
- function AbstractValue.__init__(self, map, section, option, ...)
- Node.__init__(self, ...)
- self.section = section
- self.option = option
- self.map = map
- self.config = map.config
- self.tag_invalid = {}
- self.tag_missing = {}
- self.tag_reqerror = {}
- self.tag_error = {}
- self.deps = {}
- self.subdeps = {}
- --self.cast = "string"
- self.track_missing = false
- self.rmempty = true
- self.default = nil
- self.size = nil
- self.optional = false
- end
- function AbstractValue.prepare(self)
- self.cast = self.cast or "string"
- end
- -- Add a dependencie to another section field
- function AbstractValue.depends(self, field, value)
- local deps
- if type(field) == "string" then
- deps = {}
- deps[field] = value
- else
- deps = field
- end
- table.insert(self.deps, {deps=deps, add=""})
- end
- -- Generates the unique CBID
- function AbstractValue.cbid(self, section)
- return "cbid."..self.map.config.."."..section.."."..self.option
- end
- -- Return whether this object should be created
- function AbstractValue.formcreated(self, section)
- local key = "cbi.opt."..self.config.."."..section
- return (self.map:formvalue(key) == self.option)
- end
- -- Returns the formvalue for this object
- function AbstractValue.formvalue(self, section)
- return self.map:formvalue(self:cbid(section))
- end
- function AbstractValue.additional(self, value)
- self.optional = value
- end
- function AbstractValue.mandatory(self, value)
- self.rmempty = not value
- end
- function AbstractValue.add_error(self, section, type, msg)
- self.error = self.error or { }
- self.error[section] = msg or type
- self.section.error = self.section.error or { }
- self.section.error[section] = self.section.error[section] or { }
- table.insert(self.section.error[section], msg or type)
- if type == "invalid" then
- self.tag_invalid[section] = true
- elseif type == "missing" then
- self.tag_missing[section] = true
- end
- self.tag_error[section] = true
- self.map.save = false
- end
- function AbstractValue.parse(self, section, novld)
- local fvalue = self:formvalue(section)
- local cvalue = self:cfgvalue(section)
- -- If favlue and cvalue are both tables and have the same content
- -- make them identical
- if type(fvalue) == "table" and type(cvalue) == "table" then
- local equal = #fvalue == #cvalue
- if equal then
- for i=1, #fvalue do
- if cvalue[i] ~= fvalue[i] then
- equal = false
- end
- end
- end
- if equal then
- fvalue = cvalue
- end
- end
- if fvalue and #fvalue > 0 then -- If we have a form value, write it to UCI
- local val_err
- fvalue, val_err = self:validate(fvalue, section)
- fvalue = self:transform(fvalue)
- if not fvalue and not novld then
- self:add_error(section, "invalid", val_err)
- end
- if fvalue and (self.forcewrite or not (fvalue == cvalue)) then
- if self:write(section, fvalue) then
- -- Push events
- self.section.changed = true
- --luci.util.append(self.map.events, self.events)
- end
- end
- else -- Unset the UCI or error
- if self.rmempty or self.optional then
- if self:remove(section) then
- -- Push events
- self.section.changed = true
- --luci.util.append(self.map.events, self.events)
- end
- elseif cvalue ~= fvalue and not novld then
- -- trigger validator with nil value to get custom user error msg.
- local _, val_err = self:validate(nil, section)
- self:add_error(section, "missing", val_err)
- end
- end
- end
- -- Render if this value exists or if it is mandatory
- function AbstractValue.render(self, s, scope)
- if not self.optional or self.section:has_tabs() or self:cfgvalue(s) or self:formcreated(s) then
- scope = scope or {}
- scope.section = s
- scope.cbid = self:cbid(s)
- Node.render(self, scope)
- end
- end
- -- Return the UCI value of this object
- function AbstractValue.cfgvalue(self, section)
- local value
- if self.tag_error[section] then
- value = self:formvalue(section)
- else
- value = self.map:get(section, self.option)
- end
- if not value then
- return nil
- elseif not self.cast or self.cast == type(value) then
- return value
- elseif self.cast == "string" then
- if type(value) == "table" then
- return value[1]
- end
- elseif self.cast == "table" then
- return { value }
- end
- end
- -- Validate the form value
- function AbstractValue.validate(self, value)
- if self.datatype and value then
- if type(value) == "table" then
- local v
- for _, v in ipairs(value) do
- if v and #v > 0 and not verify_datatype(self.datatype, v) then
- return nil
- end
- end
- else
- if not verify_datatype(self.datatype, value) then
- return nil
- end
- end
- end
- return value
- end
- AbstractValue.transform = AbstractValue.validate
- -- Write to UCI
- function AbstractValue.write(self, section, value)
- return self.map:set(section, self.option, value)
- end
- -- Remove from UCI
- function AbstractValue.remove(self, section)
- return self.map:del(section, self.option)
- end
- --[[
- Value - A one-line value
- maxlength: The maximum length
- ]]--
- Value = class(AbstractValue)
- function Value.__init__(self, ...)
- AbstractValue.__init__(self, ...)
- self.template = "cbi/value"
- self.keylist = {}
- self.vallist = {}
- end
- function Value.reset_values(self)
- self.keylist = {}
- self.vallist = {}
- end
- function Value.value(self, key, val)
- val = val or key
- table.insert(self.keylist, tostring(key))
- table.insert(self.vallist, tostring(val))
- end
- -- DummyValue - This does nothing except being there
- DummyValue = class(AbstractValue)
- function DummyValue.__init__(self, ...)
- AbstractValue.__init__(self, ...)
- self.template = "cbi/dvalue"
- self.value = nil
- end
- function DummyValue.cfgvalue(self, section)
- local value
- if self.value then
- if type(self.value) == "function" then
- value = self:value(section)
- else
- value = self.value
- end
- else
- value = AbstractValue.cfgvalue(self, section)
- end
- return value
- end
- function DummyValue.parse(self)
- end
- --[[
- Flag - A flag being enabled or disabled
- ]]--
- Flag = class(AbstractValue)
- function Flag.__init__(self, ...)
- AbstractValue.__init__(self, ...)
- self.template = "cbi/fvalue"
- self.enabled = "1"
- self.disabled = "0"
- self.default = self.disabled
- end
- -- A flag can only have two states: set or unset
- function Flag.parse(self, section)
- local fexists = self.map:formvalue(
- FEXIST_PREFIX .. self.config .. "." .. section .. "." .. self.option)
- if fexists then
- local fvalue = self:formvalue(section) and self.enabled or self.disabled
- if fvalue ~= self.default or (not self.optional and not self.rmempty) then
- self:write(section, fvalue)
- else
- self:remove(section)
- end
- else
- self:remove(section)
- end
- end
- function Flag.cfgvalue(self, section)
- return AbstractValue.cfgvalue(self, section) or self.default
- end
- --[[
- ListValue - A one-line value predefined in a list
- widget: The widget that will be used (select, radio)
- ]]--
- ListValue = class(AbstractValue)
- function ListValue.__init__(self, ...)
- AbstractValue.__init__(self, ...)
- self.template = "cbi/lvalue"
- self.keylist = {}
- self.vallist = {}
- self.size = 1
- self.widget = "select"
- end
- function ListValue.reset_values(self)
- self.keylist = {}
- self.vallist = {}
- end
- function ListValue.value(self, key, val, ...)
- if luci.util.contains(self.keylist, key) then
- return
- end
- val = val or key
- table.insert(self.keylist, tostring(key))
- table.insert(self.vallist, tostring(val))
- for i, deps in ipairs({...}) do
- self.subdeps[#self.subdeps + 1] = {add = "-"..key, deps=deps}
- end
- end
- function ListValue.validate(self, val)
- if luci.util.contains(self.keylist, val) then
- return val
- else
- return nil
- end
- end
- --[[
- MultiValue - Multiple delimited values
- widget: The widget that will be used (select, checkbox)
- delimiter: The delimiter that will separate the values (default: " ")
- ]]--
- MultiValue = class(AbstractValue)
- function MultiValue.__init__(self, ...)
- AbstractValue.__init__(self, ...)
- self.template = "cbi/mvalue"
- self.keylist = {}
- self.vallist = {}
- self.widget = "checkbox"
- self.delimiter = " "
- end
- function MultiValue.render(self, ...)
- if self.widget == "select" and not self.size then
- self.size = #self.vallist
- end
- AbstractValue.render(self, ...)
- end
- function MultiValue.reset_values(self)
- self.keylist = {}
- self.vallist = {}
- end
- function MultiValue.value(self, key, val)
- if luci.util.contains(self.keylist, key) then
- return
- end
- val = val or key
- table.insert(self.keylist, tostring(key))
- table.insert(self.vallist, tostring(val))
- end
- function MultiValue.valuelist(self, section)
- local val = self:cfgvalue(section)
- if not(type(val) == "string") then
- return {}
- end
- return luci.util.split(val, self.delimiter)
- end
- function MultiValue.validate(self, val)
- val = (type(val) == "table") and val or {val}
- local result
- for i, value in ipairs(val) do
- if luci.util.contains(self.keylist, value) then
- result = result and (result .. self.delimiter .. value) or value
- end
- end
- return result
- end
- StaticList = class(MultiValue)
- function StaticList.__init__(self, ...)
- MultiValue.__init__(self, ...)
- self.cast = "table"
- self.valuelist = self.cfgvalue
- if not self.override_scheme
- and self.map:get_scheme(self.section.sectiontype, self.option) then
- local vs = self.map:get_scheme(self.section.sectiontype, self.option)
- if self.value and vs.values and not self.override_values then
- for k, v in pairs(vs.values) do
- self:value(k, v)
- end
- end
- end
- end
- function StaticList.validate(self, value)
- value = (type(value) == "table") and value or {value}
- local valid = {}
- for i, v in ipairs(value) do
- if luci.util.contains(self.keylist, v) then
- table.insert(valid, v)
- end
- end
- return valid
- end
- DynamicList = class(AbstractValue)
- function DynamicList.__init__(self, ...)
- AbstractValue.__init__(self, ...)
- self.template = "cbi/dynlist"
- self.cast = "table"
- self.keylist = {}
- self.vallist = {}
- end
- function DynamicList.reset_values(self)
- self.keylist = {}
- self.vallist = {}
- end
- function DynamicList.value(self, key, val)
- val = val or key
- table.insert(self.keylist, tostring(key))
- table.insert(self.vallist, tostring(val))
- end
- function DynamicList.write(self, section, value)
- local t = { }
- if type(value) == "table" then
- local x
- for _, x in ipairs(value) do
- if x and #x > 0 then
- t[#t+1] = x
- end
- end
- else
- t = { value }
- end
- if self.cast == "string" then
- value = table.concat(t, " ")
- else
- value = t
- end
- return AbstractValue.write(self, section, value)
- end
- function DynamicList.cfgvalue(self, section)
- local value = AbstractValue.cfgvalue(self, section)
- if type(value) == "string" then
- local x
- local t = { }
- for x in value:gmatch("%S+") do
- if #x > 0 then
- t[#t+1] = x
- end
- end
- value = t
- end
- return value
- end
- function DynamicList.formvalue(self, section)
- local value = AbstractValue.formvalue(self, section)
- if type(value) == "string" then
- if self.cast == "string" then
- local x
- local t = { }
- for x in value:gmatch("%S+") do
- t[#t+1] = x
- end
- value = t
- else
- value = { value }
- end
- end
- return value
- end
- --[[
- TextValue - A multi-line value
- rows: Rows
- ]]--
- TextValue = class(AbstractValue)
- function TextValue.__init__(self, ...)
- AbstractValue.__init__(self, ...)
- self.template = "cbi/tvalue"
- end
- --[[
- Button
- ]]--
- Button = class(AbstractValue)
- function Button.__init__(self, ...)
- AbstractValue.__init__(self, ...)
- self.template = "cbi/button"
- self.inputstyle = nil
- self.rmempty = true
- end
- FileUpload = class(AbstractValue)
- function FileUpload.__init__(self, ...)
- AbstractValue.__init__(self, ...)
- self.template = "cbi/upload"
- if not self.map.upload_fields then
- self.map.upload_fields = { self }
- else
- self.map.upload_fields[#self.map.upload_fields+1] = self
- end
- end
- function FileUpload.formcreated(self, section)
- return AbstractValue.formcreated(self, section) or
- self.map:formvalue("cbi.rlf."..section.."."..self.option) or
- self.map:formvalue("cbi.rlf."..section.."."..self.option..".x")
- end
- function FileUpload.cfgvalue(self, section)
- local val = AbstractValue.cfgvalue(self, section)
- if val and fs.access(val) then
- return val
- end
- return nil
- end
- function FileUpload.formvalue(self, section)
- local val = AbstractValue.formvalue(self, section)
- if val then
- if not self.map:formvalue("cbi.rlf."..section.."."..self.option) and
- not self.map:formvalue("cbi.rlf."..section.."."..self.option..".x")
- then
- return val
- end
- fs.unlink(val)
- self.value = nil
- end
- return nil
- end
- function FileUpload.remove(self, section)
- local val = AbstractValue.formvalue(self, section)
- if val and fs.access(val) then fs.unlink(val) end
- return AbstractValue.remove(self, section)
- end
- FileBrowser = class(AbstractValue)
- function FileBrowser.__init__(self, ...)
- AbstractValue.__init__(self, ...)
- self.template = "cbi/browser"
- end
|