2 LuCI - Configuration Bind Interface
5 Offers an interface for binding configuration values to certain
6 data types. Supports value and range validation and basic dependencies.
12 Copyright 2008 Steven Barth <steven@midlink.org>
14 Licensed under the Apache License, Version 2.0 (the "License");
15 you may not use this file except in compliance with the License.
16 You may obtain a copy of the License at
18 http://www.apache.org/licenses/LICENSE-2.0
20 Unless required by applicable law or agreed to in writing, software
21 distributed under the License is distributed on an "AS IS" BASIS,
22 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23 See the License for the specific language governing permissions and
24 limitations under the License.
27 module("luci.cbi", package.seeall)
29 require("luci.template")
32 require("luci.model.uci")
34 local uci = luci.model.uci
35 local class = luci.util.class
36 local instanceof = luci.util.instanceof
42 CREATE_PREFIX = "cbi.cts."
43 REMOVE_PREFIX = "cbi.rts."
45 -- Loads a CBI map from given file, creating an environment and returns it
46 function load(cbimap, ...)
49 require("luci.config")
52 local cbidir = luci.util.libpath() .. "/model/cbi/"
53 local func, err = loadfile(cbidir..cbimap..".lua")
59 luci.i18n.loadc("cbi")
61 luci.util.resfenv(func)
62 luci.util.updfenv(func, luci.cbi)
63 luci.util.extfenv(func, "translate", luci.i18n.translate)
64 luci.util.extfenv(func, "translatef", luci.i18n.translatef)
65 luci.util.extfenv(func, "arg", {...})
69 for i, map in ipairs(maps) do
70 if not instanceof(map, Node) then
71 error("CBI map returns no valid map object!")
79 -- Node pseudo abstract class
82 function Node.__init__(self, title, description)
84 self.title = title or ""
85 self.description = description or ""
86 self.template = "cbi/node"
90 function Node._i18n(self, config, section, option, title, description)
93 if type(luci.i18n) == "table" then
95 local key = config:gsub("[^%w]+", "")
97 if section then key = key .. "_" .. section:lower():gsub("[^%w]+", "") end
98 if option then key = key .. "_" .. option:lower():gsub("[^%w]+", "") end
100 self.title = title or luci.i18n.translate( key, option or section or config )
101 self.description = description or luci.i18n.translate( key .. "_desc", "" )
105 -- Append child nodes
106 function Node.append(self, obj)
107 table.insert(self.children, obj)
110 -- Parse this node and its children
111 function Node.parse(self, ...)
112 for k, child in ipairs(self.children) do
118 function Node.render(self, scope)
122 luci.template.render(self.template, scope)
125 -- Render the children
126 function Node.render_children(self, ...)
127 for k, node in ipairs(self.children) do
134 A simple template element
136 Template = class(Node)
138 function Template.__init__(self, template)
140 self.template = template
143 function Template.render(self)
144 luci.template.render(self.template, {self=self})
149 Map - A map describing a configuration file
153 function Map.__init__(self, config, ...)
154 Node.__init__(self, ...)
155 Node._i18n(self, config, nil, nil, ...)
158 self.parsechain = {self.config}
159 self.template = "cbi/map"
160 if not uci.load(self.config) then
161 error("Unable to read UCI data: " .. self.config)
166 -- Chain foreign config
167 function Map.chain(self, config)
168 table.insert(self.parsechain, config)
171 -- Use optimized UCI writing
172 function Map.parse(self, ...)
173 Node.parse(self, ...)
174 for i, config in ipairs(self.parsechain) do
177 if luci.http.formvalue("cbi.apply") then
178 for i, config in ipairs(self.parsechain) do
180 if luci.config.uci_oncommit and luci.config.uci_oncommit[config] then
181 luci.util.exec(luci.config.uci_oncommit[config])
184 -- Refresh data because commit changes section names
190 Node.parse(self, ...)
193 for i, config in ipairs(self.parsechain) do
198 -- Creates a child section
199 function Map.section(self, class, ...)
200 if instanceof(class, AbstractSection) then
201 local obj = class(self, ...)
205 error("class must be a descendent of AbstractSection")
210 function Map.add(self, sectiontype)
211 return uci.add(self.config, sectiontype)
215 function Map.set(self, section, option, value)
217 return uci.set(self.config, section, option, value)
219 return uci.set(self.config, section, value)
224 function Map.del(self, section, option)
226 return uci.delete(self.config, section, option)
228 return uci.delete(self.config, section)
233 function Map.get(self, section, option)
235 return uci.get_all(self.config)
237 return uci.get(self.config, section, option)
239 return uci.get_all(self.config, section)
244 function Map.stateget(self, section, option)
245 return uci.get_statevalue(self.config, section, option)
254 Page.__init__ = Node.__init__
255 Page.parse = function() end
259 SimpleForm - A Simple non-UCI form
261 SimpleForm = class(Node)
263 function SimpleForm.__init__(self, config, title, description, data)
264 Node.__init__(self, title, description)
266 self.data = data or {}
267 self.template = "cbi/simpleform"
271 function SimpleForm.parse(self, ...)
272 if luci.http.formvalue("cbi.submit") then
273 Node.parse(self, 1, ...)
277 for k, j in ipairs(self.children) do
278 for i, v in ipairs(j.children) do
280 and (not v.tag_missing or not v.tag_missing[1])
281 and (not v.tag_invalid or not v.tag_invalid[1])
286 not luci.http.formvalue("cbi.submit") and 0
290 self.dorender = self:handle(state, self.data) ~= false
293 function SimpleForm.render(self, ...)
294 if self.dorender then
295 Node.render(self, ...)
299 function SimpleForm.section(self, class, ...)
300 if instanceof(class, AbstractSection) then
301 local obj = class(self, ...)
305 error("class must be a descendent of AbstractSection")
309 -- Creates a child field
310 function SimpleForm.field(self, class, ...)
312 for k, v in ipairs(self.children) do
313 if instanceof(v, SimpleSection) then
319 section = self:section(SimpleSection)
322 if instanceof(class, AbstractValue) then
323 local obj = class(self, ...)
324 obj.track_missing = true
328 error("class must be a descendent of AbstractValue")
332 function SimpleForm.set(self, section, option, value)
333 self.data[option] = value
337 function SimpleForm.del(self, section, option)
338 self.data[option] = nil
342 function SimpleForm.get(self, section, option)
343 return self.data[option]
351 AbstractSection = class(Node)
353 function AbstractSection.__init__(self, map, sectiontype, ...)
354 Node.__init__(self, ...)
355 self.sectiontype = sectiontype
357 self.config = map.config
362 self.addremove = false
366 -- Appends a new option
367 function AbstractSection.option(self, class, option, ...)
368 if instanceof(class, AbstractValue) then
369 local obj = class(self.map, option, ...)
371 Node._i18n(obj, self.config, self.section or self.sectiontype, option, ...)
376 error("class must be a descendent of AbstractValue")
380 -- Parse optional options
381 function AbstractSection.parse_optionals(self, section)
382 if not self.optional then
386 self.optionals[section] = {}
388 local field = luci.http.formvalue("cbi.opt."..self.config.."."..section)
389 for k,v in ipairs(self.children) do
390 if v.optional and not v:cfgvalue(section) then
391 if field == v.option then
394 table.insert(self.optionals[section], v)
399 if field and #field > 0 and self.dynamic then
400 self:add_dynamic(field)
404 -- Add a dynamic option
405 function AbstractSection.add_dynamic(self, field, optional)
406 local o = self:option(Value, field, field)
407 o.optional = optional
410 -- Parse all dynamic options
411 function AbstractSection.parse_dynamic(self, section)
412 if not self.dynamic then
416 local arr = luci.util.clone(self:cfgvalue(section))
417 local form = luci.http.formvaluetable("cbid."..self.config.."."..section)
418 for k, v in pairs(form) do
422 for key,val in pairs(arr) do
425 for i,c in ipairs(self.children) do
426 if c.option == key then
431 if create and key:sub(1, 1) ~= "." then
432 self:add_dynamic(key, true)
437 -- Returns the section's UCI table
438 function AbstractSection.cfgvalue(self, section)
439 return self.map:get(section)
442 -- Removes the section
443 function AbstractSection.remove(self, section)
444 return self.map:del(section)
447 -- Creates the section
448 function AbstractSection.create(self, section)
452 stat = self.map:set(section, nil, self.sectiontype)
454 section = self.map:add(self.sectiontype)
459 for k,v in pairs(self.children) do
461 self.map:set(section, v.option, v.default)
465 for k,v in pairs(self.defaults) do
466 self.map:set(section, k, v)
474 SimpleSection = class(AbstractSection)
476 function SimpleSection.__init__(self, form, ...)
477 AbstractSection.__init__(self, form, nil, ...)
478 self.template = "cbi/nullsection"
484 NamedSection - A fixed configuration section defined by its name
486 NamedSection = class(AbstractSection)
488 function NamedSection.__init__(self, map, section, type, ...)
489 AbstractSection.__init__(self, map, type, ...)
490 Node._i18n(self, map.config, section, nil, ...)
492 self.template = "cbi/nsection"
493 self.section = section
494 self.addremove = false
497 function NamedSection.parse(self)
498 local s = self.section
499 local active = self:cfgvalue(s)
502 if self.addremove then
503 local path = self.config.."."..s
504 if active then -- Remove the section
505 if luci.http.formvalue("cbi.rns."..path) and self:remove(s) then
508 else -- Create and apply default values
509 if luci.http.formvalue("cbi.cns."..path) then
517 AbstractSection.parse_dynamic(self, s)
518 if luci.http.formvalue("cbi.submit") then
521 AbstractSection.parse_optionals(self, s)
527 TypedSection - A (set of) configuration section(s) defined by the type
528 addremove: Defines whether the user can add/remove sections of this type
529 anonymous: Allow creating anonymous sections
530 validate: a validation function returning nil if the section is invalid
532 TypedSection = class(AbstractSection)
534 function TypedSection.__init__(self, map, type, ...)
535 AbstractSection.__init__(self, map, type, ...)
536 Node._i18n(self, map.config, type, nil, ...)
538 self.template = "cbi/tsection"
541 self.anonymous = false
544 -- Return all matching UCI sections for this TypedSection
545 function TypedSection.cfgsections(self)
547 uci.foreach(self.map.config, self.sectiontype,
549 if self:checkscope(section[".name"]) then
550 table.insert(sections, section[".name"])
557 -- Limits scope to sections that have certain option => value pairs
558 function TypedSection.depends(self, option, value)
559 table.insert(self.deps, {option=option, value=value})
562 function TypedSection.parse(self)
563 if self.addremove then
565 local crval = CREATE_PREFIX .. self.config .. "." .. self.sectiontype
566 local name = luci.http.formvalue(crval)
567 if self.anonymous then
573 -- Ignore if it already exists
574 if self:cfgvalue(name) then
578 name = self:checkscope(name)
581 self.err_invalid = true
584 if name and name:len() > 0 then
591 crval = REMOVE_PREFIX .. self.config
592 name = luci.http.formvaluetable(crval)
593 for k,v in pairs(name) do
594 if self:cfgvalue(k) and self:checkscope(k) then
600 for i, k in ipairs(self:cfgsections()) do
601 AbstractSection.parse_dynamic(self, k)
602 if luci.http.formvalue("cbi.submit") then
605 AbstractSection.parse_optionals(self, k)
609 -- Verifies scope of sections
610 function TypedSection.checkscope(self, section)
611 -- Check if we are not excluded
612 if self.filter and not self:filter(section) then
616 -- Check if at least one dependency is met
617 if #self.deps > 0 and self:cfgvalue(section) then
620 for k, v in ipairs(self.deps) do
621 if self:cfgvalue(section)[v.option] == v.value then
631 return self:validate(section)
635 -- Dummy validate function
636 function TypedSection.validate(self, section)
642 AbstractValue - An abstract Value Type
643 null: Value can be empty
644 valid: A function returning the value if it is valid otherwise nil
645 depends: A table of option => value pairs of which one must be true
646 default: The default value
647 size: The size of the input fields
648 rmempty: Unset value if empty
649 optional: This value is optional (see AbstractSection.optionals)
651 AbstractValue = class(Node)
653 function AbstractValue.__init__(self, map, option, ...)
654 Node.__init__(self, ...)
657 self.config = map.config
658 self.tag_invalid = {}
659 self.tag_missing = {}
662 self.track_missing = false
666 self.optional = false
667 self.stateful = false
670 -- Add a dependencie to another section field
671 function AbstractValue.depends(self, field, value)
672 table.insert(self.deps, {field=field, value=value})
675 -- Return whether this object should be created
676 function AbstractValue.formcreated(self, section)
677 local key = "cbi.opt."..self.config.."."..section
678 return (luci.http.formvalue(key) == self.option)
681 -- Returns the formvalue for this object
682 function AbstractValue.formvalue(self, section)
683 local key = "cbid."..self.map.config.."."..section.."."..self.option
684 return luci.http.formvalue(key)
687 function AbstractValue.additional(self, value)
688 self.optional = value
691 function AbstractValue.mandatory(self, value)
692 self.rmempty = not value
695 function AbstractValue.parse(self, section)
696 local fvalue = self:formvalue(section)
697 local cvalue = self:cfgvalue(section)
699 if fvalue and fvalue ~= "" then -- If we have a form value, write it to UCI
700 fvalue = self:transform(self:validate(fvalue, section))
702 self.tag_invalid[section] = true
704 if fvalue and not (fvalue == cvalue) then
705 self:write(section, fvalue)
707 else -- Unset the UCI or error
708 if self.rmempty or self.optional then
710 elseif self.track_missing and not fvalue or fvalue ~= cvalue then
711 self.tag_missing[section] = true
716 -- Render if this value exists or if it is mandatory
717 function AbstractValue.render(self, s, scope)
718 if not self.optional or self:cfgvalue(s) or self:formcreated(s) then
721 scope.cbid = "cbid." .. self.config ..
725 scope.ifattr = function(cond,key,val)
727 return string.format(
728 ' %s="%s"', tostring(key),
731 or (type(self[key]) ~= "function" and self[key])
739 scope.attr = function(...)
740 return scope.ifattr( true, ... )
743 Node.render(self, scope)
747 -- Return the UCI value of this object
748 function AbstractValue.cfgvalue(self, section)
750 and self.map:stateget(section, self.option)
751 or self.map:get(section, self.option)
754 -- Validate the form value
755 function AbstractValue.validate(self, value)
759 AbstractValue.transform = AbstractValue.validate
763 function AbstractValue.write(self, section, value)
764 return self.map:set(section, self.option, value)
768 function AbstractValue.remove(self, section)
769 return self.map:del(section, self.option)
776 Value - A one-line value
777 maxlength: The maximum length
779 Value = class(AbstractValue)
781 function Value.__init__(self, ...)
782 AbstractValue.__init__(self, ...)
783 self.template = "cbi/value"
788 function Value.value(self, key, val)
790 table.insert(self.keylist, tostring(key))
791 table.insert(self.vallist, tostring(val))
795 -- DummyValue - This does nothing except being there
796 DummyValue = class(AbstractValue)
798 function DummyValue.__init__(self, map, ...)
799 AbstractValue.__init__(self, map, ...)
800 self.template = "cbi/dvalue"
804 function DummyValue.parse(self)
810 Flag - A flag being enabled or disabled
812 Flag = class(AbstractValue)
814 function Flag.__init__(self, ...)
815 AbstractValue.__init__(self, ...)
816 self.template = "cbi/fvalue"
822 -- A flag can only have two states: set or unset
823 function Flag.parse(self, section)
824 local fvalue = self:formvalue(section)
827 fvalue = self.enabled
829 fvalue = self.disabled
832 if fvalue == self.enabled or (not self.optional and not self.rmempty) then
833 if not(fvalue == self:cfgvalue(section)) then
834 self:write(section, fvalue)
844 ListValue - A one-line value predefined in a list
845 widget: The widget that will be used (select, radio)
847 ListValue = class(AbstractValue)
849 function ListValue.__init__(self, ...)
850 AbstractValue.__init__(self, ...)
851 self.template = "cbi/lvalue"
856 self.widget = "select"
859 function ListValue.value(self, key, val)
861 table.insert(self.keylist, tostring(key))
862 table.insert(self.vallist, tostring(val))
865 function ListValue.validate(self, val)
866 if luci.util.contains(self.keylist, val) then
876 MultiValue - Multiple delimited values
877 widget: The widget that will be used (select, checkbox)
878 delimiter: The delimiter that will separate the values (default: " ")
880 MultiValue = class(AbstractValue)
882 function MultiValue.__init__(self, ...)
883 AbstractValue.__init__(self, ...)
884 self.template = "cbi/mvalue"
888 self.widget = "checkbox"
892 function MultiValue.render(self, ...)
893 if self.widget == "select" and not self.size then
894 self.size = #self.vallist
897 AbstractValue.render(self, ...)
900 function MultiValue.value(self, key, val)
902 table.insert(self.keylist, tostring(key))
903 table.insert(self.vallist, tostring(val))
906 function MultiValue.valuelist(self, section)
907 local val = self:cfgvalue(section)
909 if not(type(val) == "string") then
913 return luci.util.split(val, self.delimiter)
916 function MultiValue.validate(self, val)
917 val = (type(val) == "table") and val or {val}
921 for i, value in ipairs(val) do
922 if luci.util.contains(self.keylist, value) then
923 result = result and (result .. self.delimiter .. value) or value
931 TextValue - A multi-line value
934 TextValue = class(AbstractValue)
936 function TextValue.__init__(self, ...)
937 AbstractValue.__init__(self, ...)
938 self.template = "cbi/tvalue"