diff options
Diffstat (limited to 'libs/web')
82 files changed, 0 insertions, 9507 deletions
diff --git a/libs/web/Makefile b/libs/web/Makefile deleted file mode 100644 index ad16ec737..000000000 --- a/libs/web/Makefile +++ /dev/null @@ -1,45 +0,0 @@ -ifneq (,$(wildcard ../../build/config.mk)) -include ../../build/config.mk -include ../../build/module.mk -include ../../build/gccconfig.mk -else -include standalone.mk -endif - -TPL_LDFLAGS = -TPL_CFLAGS = -TPL_SO = parser.so -TPL_PO2LMO = po2lmo -TPL_PO2LMO_OBJ = src/po2lmo.o -TPL_LMO_OBJ = src/template_lmo.o -TPL_COMMON_OBJ = src/template_parser.o src/template_utils.o -TPL_LUALIB_OBJ = src/template_lualib.o - -%.o: %.c - $(COMPILE) $(TPL_CFLAGS) $(LUA_CFLAGS) $(FPIC) -c -o $@ $< - -compile: build-clean $(TPL_COMMON_OBJ) $(TPL_LUALIB_OBJ) $(TPL_LMO_OBJ) $(TPL_PO2LMO_OBJ) - $(LINK) $(SHLIB_FLAGS) $(TPL_LDFLAGS) -o src/$(TPL_SO) \ - $(TPL_COMMON_OBJ) $(TPL_LMO_OBJ) $(TPL_LUALIB_OBJ) - $(LINK) -o src/$(TPL_PO2LMO) \ - $(TPL_LMO_OBJ) $(TPL_PO2LMO_OBJ) - mkdir -p dist$(LUCI_LIBRARYDIR)/template - cp src/$(TPL_SO) dist$(LUCI_LIBRARYDIR)/template/$(TPL_SO) - -install: build - cp -pR dist$(LUA_LIBRARYDIR)/* $(LUA_LIBRARYDIR) - -clean: build-clean - -build-clean: - rm -f src/*.o src/$(TPL_SO) - -host-compile: build-clean host-clean $(TPL_LMO_OBJ) $(TPL_PO2LMO_OBJ) - $(LINK) -o src/$(TPL_PO2LMO) $(TPL_LMO_OBJ) $(TPL_PO2LMO_OBJ) - -host-install: host-compile - cp src/$(TPL_PO2LMO) ../../build/$(TPL_PO2LMO) - -host-clean: - rm -f src/$(TPL_PO2LMO) - rm -f ../../build/$(TPL_PO2LMO) diff --git a/libs/web/htdocs/luci-static/resources/cbi.js b/libs/web/htdocs/luci-static/resources/cbi.js deleted file mode 100644 index 02814a542..000000000 --- a/libs/web/htdocs/luci-static/resources/cbi.js +++ /dev/null @@ -1,1339 +0,0 @@ -/* - LuCI - Lua Configuration Interface - - Copyright 2008 Steven Barth <steven@midlink.org> - Copyright 2008-2012 Jo-Philipp Wich <xm@subsignal.org> - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 -*/ - -var cbi_d = []; -var cbi_t = []; -var cbi_c = []; - -var cbi_validators = { - - 'integer': function() - { - return (this.match(/^-?[0-9]+$/) != null); - }, - - 'uinteger': function() - { - return (cbi_validators.integer.apply(this) && (this >= 0)); - }, - - 'float': function() - { - return !isNaN(parseFloat(this)); - }, - - 'ufloat': function() - { - return (cbi_validators['float'].apply(this) && (this >= 0)); - }, - - 'ipaddr': function() - { - return cbi_validators.ip4addr.apply(this) || - cbi_validators.ip6addr.apply(this); - }, - - 'ip4addr': function() - { - if (this.match(/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})(\/(\S+))?$/)) - { - return (RegExp.$1 >= 0) && (RegExp.$1 <= 255) && - (RegExp.$2 >= 0) && (RegExp.$2 <= 255) && - (RegExp.$3 >= 0) && (RegExp.$3 <= 255) && - (RegExp.$4 >= 0) && (RegExp.$4 <= 255) && - ((RegExp.$6.indexOf('.') < 0) - ? ((RegExp.$6 >= 0) && (RegExp.$6 <= 32)) - : (cbi_validators.ip4addr.apply(RegExp.$6))) - ; - } - - return false; - }, - - 'ip6addr': function() - { - if( this.match(/^([a-fA-F0-9:.]+)(\/(\d+))?$/) ) - { - if( !RegExp.$2 || ((RegExp.$3 >= 0) && (RegExp.$3 <= 128)) ) - { - var addr = RegExp.$1; - - if( addr == '::' ) - { - return true; - } - - if( addr.indexOf('.') > 0 ) - { - var off = addr.lastIndexOf(':'); - - if( !(off && cbi_validators.ip4addr.apply(addr.substr(off+1))) ) - return false; - - addr = addr.substr(0, off) + ':0:0'; - } - - if( addr.indexOf('::') >= 0 ) - { - var colons = 0; - var fill = '0'; - - for( var i = 1; i < (addr.length-1); i++ ) - if( addr.charAt(i) == ':' ) - colons++; - - if( colons > 7 ) - return false; - - for( var i = 0; i < (7 - colons); i++ ) - fill += ':0'; - - if (addr.match(/^(.*?)::(.*?)$/)) - addr = (RegExp.$1 ? RegExp.$1 + ':' : '') + fill + - (RegExp.$2 ? ':' + RegExp.$2 : ''); - } - - return (addr.match(/^(?:[a-fA-F0-9]{1,4}:){7}[a-fA-F0-9]{1,4}$/) != null); - } - } - - return false; - }, - - 'port': function() - { - return cbi_validators.integer.apply(this) && - (this >= 0) && (this <= 65535); - }, - - 'portrange': function() - { - if (this.match(/^(\d+)-(\d+)$/)) - { - var p1 = RegExp.$1; - var p2 = RegExp.$2; - - return cbi_validators.port.apply(p1) && - cbi_validators.port.apply(p2) && - (parseInt(p1) <= parseInt(p2)) - ; - } - else - { - return cbi_validators.port.apply(this); - } - }, - - 'macaddr': function() - { - return (this.match(/^([a-fA-F0-9]{2}:){5}[a-fA-F0-9]{2}$/) != null); - }, - - 'host': function() - { - return cbi_validators.hostname.apply(this) || - cbi_validators.ipaddr.apply(this); - }, - - 'hostname': function() - { - if (this.length <= 253) - return (this.match(/^[a-zA-Z0-9]+$/) != null || - (this.match(/^[a-zA-Z0-9_][a-zA-Z0-9_\-.]*[a-zA-Z0-9]$/) && - this.match(/[^0-9.]/))); - - return false; - }, - - 'network': function() - { - return cbi_validators.uciname.apply(this) || - cbi_validators.host.apply(this); - }, - - 'wpakey': function() - { - var v = this; - - if( v.length == 64 ) - return (v.match(/^[a-fA-F0-9]{64}$/) != null); - else - return (v.length >= 8) && (v.length <= 63); - }, - - 'wepkey': function() - { - var v = this; - - if ( v.substr(0,2) == 's:' ) - v = v.substr(2); - - if( (v.length == 10) || (v.length == 26) ) - return (v.match(/^[a-fA-F0-9]{10,26}$/) != null); - else - return (v.length == 5) || (v.length == 13); - }, - - 'uciname': function() - { - return (this.match(/^[a-zA-Z0-9_]+$/) != null); - }, - - 'range': function(min, max) - { - var val = parseFloat(this); - if (!isNaN(min) && !isNaN(max) && !isNaN(val)) - return ((val >= min) && (val <= max)); - - return false; - }, - - 'min': function(min) - { - var val = parseFloat(this); - if (!isNaN(min) && !isNaN(val)) - return (val >= min); - - return false; - }, - - 'max': function(max) - { - var val = parseFloat(this); - if (!isNaN(max) && !isNaN(val)) - return (val <= max); - - return false; - }, - - 'rangelength': function(min, max) - { - var val = '' + this; - if (!isNaN(min) && !isNaN(max)) - return ((val.length >= min) && (val.length <= max)); - - return false; - }, - - 'minlength': function(min) - { - var val = '' + this; - if (!isNaN(min)) - return (val.length >= min); - - return false; - }, - - 'maxlength': function(max) - { - var val = '' + this; - if (!isNaN(max)) - return (val.length <= max); - - return false; - }, - - 'or': function() - { - for (var i = 0; i < arguments.length; i += 2) - { - if (typeof arguments[i] != 'function') - { - if (arguments[i] == this) - return true; - i--; - } - else if (arguments[i].apply(this, arguments[i+1])) - { - return true; - } - } - return false; - }, - - 'and': function() - { - for (var i = 0; i < arguments.length; i += 2) - { - if (typeof arguments[i] != 'function') - { - if (arguments[i] != this) - return false; - i--; - } - else if (!arguments[i].apply(this, arguments[i+1])) - { - return false; - } - } - return true; - }, - - 'neg': function() - { - return cbi_validators.or.apply( - this.replace(/^[ \t]*![ \t]*/, ''), arguments); - }, - - 'list': function(subvalidator, subargs) - { - if (typeof subvalidator != 'function') - return false; - - var tokens = this.match(/[^ \t]+/g); - for (var i = 0; i < tokens.length; i++) - if (!subvalidator.apply(tokens[i], subargs)) - return false; - - return true; - }, - 'phonedigit': function() - { - return (this.match(/^[0-9\*#!\.]+$/) != null); - } -}; - - -function cbi_d_add(field, dep, next) { - var obj = document.getElementById(field); - if (obj) { - var entry - for (var i=0; i<cbi_d.length; i++) { - if (cbi_d[i].id == field) { - entry = cbi_d[i]; - break; - } - } - if (!entry) { - entry = { - "node": obj, - "id": field, - "parent": obj.parentNode.id, - "next": next, - "deps": [] - }; - cbi_d.unshift(entry); - } - entry.deps.push(dep) - } -} - -function cbi_d_checkvalue(target, ref) { - var t = document.getElementById(target); - var value; - - if (!t) { - var tl = document.getElementsByName(target); - - if( tl.length > 0 && (tl[0].type == 'radio' || tl[0].type == 'checkbox')) - for( var i = 0; i < tl.length; i++ ) - if( tl[i].checked ) { - value = tl[i].value; - break; - } - - value = value ? value : ""; - } else if (!t.value) { - value = ""; - } else { - value = t.value; - - if (t.type == "checkbox") { - value = t.checked ? value : ""; - } - } - - return (value == ref) -} - -function cbi_d_check(deps) { - var reverse; - var def = false; - for (var i=0; i<deps.length; i++) { - var istat = true; - reverse = false; - for (var j in deps[i]) { - if (j == "!reverse") { - reverse = true; - } else if (j == "!default") { - def = true; - istat = false; - } else { - istat = (istat && cbi_d_checkvalue(j, deps[i][j])) - } - } - if (istat) { - return !reverse; - } - } - return def; -} - -function cbi_d_update() { - var state = false; - for (var i=0; i<cbi_d.length; i++) { - var entry = cbi_d[i]; - var next = document.getElementById(entry.next) - var node = document.getElementById(entry.id) - var parent = document.getElementById(entry.parent) - - if (node && node.parentNode && !cbi_d_check(entry.deps)) { - node.parentNode.removeChild(node); - state = true; - if( entry.parent ) - cbi_c[entry.parent]--; - } else if ((!node || !node.parentNode) && cbi_d_check(entry.deps)) { - if (!next) { - parent.appendChild(entry.node); - } else { - next.parentNode.insertBefore(entry.node, next); - } - state = true; - if( entry.parent ) - cbi_c[entry.parent]++; - } - } - - if (entry && entry.parent) { - if (!cbi_t_update()) - cbi_tag_last(parent); - } - - if (state) { - cbi_d_update(); - } -} - -function cbi_bind(obj, type, callback, mode) { - if (!obj.addEventListener) { - obj.attachEvent('on' + type, - function(){ - var e = window.event; - - if (!e.target && e.srcElement) - e.target = e.srcElement; - - return !!callback(e); - } - ); - } else { - obj.addEventListener(type, callback, !!mode); - } - return obj; -} - -function cbi_combobox(id, values, def, man) { - var selid = "cbi.combobox." + id; - if (document.getElementById(selid)) { - return - } - - var obj = document.getElementById(id) - var sel = document.createElement("select"); - sel.id = selid; - sel.className = obj.className.replace(/cbi-input-text/, 'cbi-input-select'); - - if (obj.nextSibling) { - obj.parentNode.insertBefore(sel, obj.nextSibling); - } else { - obj.parentNode.appendChild(sel); - } - - var dt = obj.getAttribute('cbi_datatype'); - var op = obj.getAttribute('cbi_optional'); - - if (dt) - cbi_validate_field(sel, op == 'true', dt); - - if (!values[obj.value]) { - if (obj.value == "") { - var optdef = document.createElement("option"); - optdef.value = ""; - optdef.appendChild(document.createTextNode(def)); - sel.appendChild(optdef); - } else { - var opt = document.createElement("option"); - opt.value = obj.value; - opt.selected = "selected"; - opt.appendChild(document.createTextNode(obj.value)); - sel.appendChild(opt); - } - } - - for (var i in values) { - var opt = document.createElement("option"); - opt.value = i; - - if (obj.value == i) { - opt.selected = "selected"; - } - - opt.appendChild(document.createTextNode(values[i])); - sel.appendChild(opt); - } - - var optman = document.createElement("option"); - optman.value = ""; - optman.appendChild(document.createTextNode(man)); - sel.appendChild(optman); - - obj.style.display = "none"; - - cbi_bind(sel, "change", function() { - if (sel.selectedIndex == sel.options.length - 1) { - obj.style.display = "inline"; - sel.parentNode.removeChild(sel); - obj.focus(); - } else { - obj.value = sel.options[sel.selectedIndex].value; - } - - try { - cbi_d_update(); - } catch (e) { - //Do nothing - } - }) - - // Retrigger validation in select - sel.focus(); - sel.blur(); -} - -function cbi_combobox_init(id, values, def, man) { - var obj = document.getElementById(id); - cbi_bind(obj, "blur", function() { - cbi_combobox(id, values, def, man) - }); - cbi_combobox(id, values, def, man); -} - -function cbi_filebrowser(id, url, defpath) { - var field = document.getElementById(id); - var browser = window.open( - url + ( field.value || defpath || '' ) + '?field=' + id, - "luci_filebrowser", "width=300,height=400,left=100,top=200,scrollbars=yes" - ); - - browser.focus(); -} - -function cbi_browser_init(id, respath, url, defpath) -{ - function cbi_browser_btnclick(e) { - cbi_filebrowser(id, url, defpath); - return false; - } - - var field = document.getElementById(id); - - var btn = document.createElement('img'); - btn.className = 'cbi-image-button'; - btn.src = respath + '/cbi/folder.gif'; - field.parentNode.insertBefore(btn, field.nextSibling); - - cbi_bind(btn, 'click', cbi_browser_btnclick); -} - -function cbi_dynlist_init(name, respath, datatype, optional, choices) -{ - var input0 = document.getElementsByName(name)[0]; - var prefix = input0.name; - var parent = input0.parentNode; - var holder = input0.placeholder; - - var values; - - function cbi_dynlist_redraw(focus, add, del) - { - values = [ ]; - - while (parent.firstChild) - { - var n = parent.firstChild; - var i = parseInt(n.index); - - if (i != del) - { - if (n.nodeName.toLowerCase() == 'input') - values.push(n.value || ''); - else if (n.nodeName.toLowerCase() == 'select') - values[values.length-1] = n.options[n.selectedIndex].value; - } - - parent.removeChild(n); - } - - if (add >= 0) - { - focus = add+1; - values.splice(focus, 0, ''); - } - else if (values.length == 0) - { - focus = 0; - values.push(''); - } - - for (var i = 0; i < values.length; i++) - { - var t = document.createElement('input'); - t.id = prefix + '.' + (i+1); - t.name = prefix; - t.value = values[i]; - t.type = 'text'; - t.index = i; - t.className = 'cbi-input-text'; - - if (i == 0 && holder) - { - t.placeholder = holder; - } - - var b = document.createElement('img'); - b.src = respath + ((i+1) < values.length ? '/cbi/remove.gif' : '/cbi/add.gif'); - b.className = 'cbi-image-button'; - - parent.appendChild(t); - parent.appendChild(b); - parent.appendChild(document.createElement('br')); - - if (datatype) - { - cbi_validate_field(t.id, ((i+1) == values.length) || optional, datatype); - } - - if (choices) - { - cbi_combobox_init(t.id, choices[0], '', choices[1]); - t.nextSibling.index = i; - - cbi_bind(t.nextSibling, 'keydown', cbi_dynlist_keydown); - cbi_bind(t.nextSibling, 'keypress', cbi_dynlist_keypress); - - if (i == focus || -i == focus) - t.nextSibling.focus(); - } - else - { - cbi_bind(t, 'keydown', cbi_dynlist_keydown); - cbi_bind(t, 'keypress', cbi_dynlist_keypress); - - if (i == focus) - { - t.focus(); - } - else if (-i == focus) - { - t.focus(); - - /* force cursor to end */ - var v = t.value; - t.value = ' ' - t.value = v; - } - } - - cbi_bind(b, 'click', cbi_dynlist_btnclick); - } - } - - function cbi_dynlist_keypress(ev) - { - ev = ev ? ev : window.event; - - var se = ev.target ? ev.target : ev.srcElement; - - if (se.nodeType == 3) - se = se.parentNode; - - switch (ev.keyCode) - { - /* backspace, delete */ - case 8: - case 46: - if (se.value.length == 0) - { - if (ev.preventDefault) - ev.preventDefault(); - - return false; - } - - return true; - - /* enter, arrow up, arrow down */ - case 13: - case 38: - case 40: - if (ev.preventDefault) - ev.preventDefault(); - - return false; - } - - return true; - } - - function cbi_dynlist_keydown(ev) - { - ev = ev ? ev : window.event; - - var se = ev.target ? ev.target : ev.srcElement; - - if (se.nodeType == 3) - se = se.parentNode; - - var prev = se.previousSibling; - while (prev && prev.name != name) - prev = prev.previousSibling; - - var next = se.nextSibling; - while (next && next.name != name) - next = next.nextSibling; - - /* advance one further in combobox case */ - if (next && next.nextSibling.name == name) - next = next.nextSibling; - - switch (ev.keyCode) - { - /* backspace, delete */ - case 8: - case 46: - var del = (se.nodeName.toLowerCase() == 'select') - ? true : (se.value.length == 0); - - if (del) - { - if (ev.preventDefault) - ev.preventDefault(); - - var focus = se.index; - if (ev.keyCode == 8) - focus = -focus+1; - - cbi_dynlist_redraw(focus, -1, se.index); - - return false; - } - - break; - - /* enter */ - case 13: - cbi_dynlist_redraw(-1, se.index, -1); - break; - - /* arrow up */ - case 38: - if (prev) - prev.focus(); - - break; - - /* arrow down */ - case 40: - if (next) - next.focus(); - - break; - } - - return true; - } - - function cbi_dynlist_btnclick(ev) - { - ev = ev ? ev : window.event; - - var se = ev.target ? ev.target : ev.srcElement; - - if (se.src.indexOf('remove') > -1) - { - se.previousSibling.value = ''; - - cbi_dynlist_keydown({ - target: se.previousSibling, - keyCode: 8 - }); - } - else - { - cbi_dynlist_keydown({ - target: se.previousSibling, - keyCode: 13 - }); - } - - return false; - } - - cbi_dynlist_redraw(NaN, -1, -1); -} - -//Hijacks the CBI form to send via XHR (requires Prototype) -function cbi_hijack_forms(layer, win, fail, load) { - var forms = layer.getElementsByTagName('form'); - for (var i=0; i<forms.length; i++) { - $(forms[i]).observe('submit', function(event) { - // Prevent the form from also submitting the regular way - event.stop(); - - // Submit via XHR - event.element().request({ - onSuccess: win, - onFailure: fail - }); - - if (load) { - load(); - } - }); - } -} - - -function cbi_t_add(section, tab) { - var t = document.getElementById('tab.' + section + '.' + tab); - var c = document.getElementById('container.' + section + '.' + tab); - - if( t && c ) { - cbi_t[section] = (cbi_t[section] || [ ]); - cbi_t[section][tab] = { 'tab': t, 'container': c, 'cid': c.id }; - } -} - -function cbi_t_switch(section, tab) { - if( cbi_t[section] && cbi_t[section][tab] ) { - var o = cbi_t[section][tab]; - var h = document.getElementById('tab.' + section); - for( var tid in cbi_t[section] ) { - var o2 = cbi_t[section][tid]; - if( o.tab.id != o2.tab.id ) { - o2.tab.className = o2.tab.className.replace(/(^| )cbi-tab( |$)/, " cbi-tab-disabled "); - o2.container.style.display = 'none'; - } - else { - if(h) h.value = tab; - o2.tab.className = o2.tab.className.replace(/(^| )cbi-tab-disabled( |$)/, " cbi-tab "); - o2.container.style.display = 'block'; - } - } - } - return false -} - -function cbi_t_update() { - var hl_tabs = [ ]; - var updated = false; - - for( var sid in cbi_t ) - for( var tid in cbi_t[sid] ) - { - if( cbi_c[cbi_t[sid][tid].cid] == 0 ) { - cbi_t[sid][tid].tab.style.display = 'none'; - } - else if( cbi_t[sid][tid].tab && cbi_t[sid][tid].tab.style.display == 'none' ) { - cbi_t[sid][tid].tab.style.display = ''; - - var t = cbi_t[sid][tid].tab; - t.className += ' cbi-tab-highlighted'; - hl_tabs.push(t); - } - - cbi_tag_last(cbi_t[sid][tid].container); - updated = true; - } - - if( hl_tabs.length > 0 ) - window.setTimeout(function() { - for( var i = 0; i < hl_tabs.length; i++ ) - hl_tabs[i].className = hl_tabs[i].className.replace(/ cbi-tab-highlighted/g, ''); - }, 750); - - return updated; -} - - -function cbi_validate_form(form, errmsg) -{ - /* if triggered by a section removal or addition, don't validate */ - if( form.cbi_state == 'add-section' || form.cbi_state == 'del-section' ) - return true; - - if( form.cbi_validators ) - { - for( var i = 0; i < form.cbi_validators.length; i++ ) - { - var validator = form.cbi_validators[i]; - if( !validator() && errmsg ) - { - alert(errmsg); - return false; - } - } - } - - return true; -} - -function cbi_validate_reset(form) -{ - window.setTimeout( - function() { cbi_validate_form(form, null) }, 100 - ); - - return true; -} - -function cbi_validate_compile(code) -{ - var pos = 0; - var esc = false; - var depth = 0; - var stack = [ ]; - - code += ','; - - for (var i = 0; i < code.length; i++) - { - if (esc) - { - esc = false; - continue; - } - - switch (code.charCodeAt(i)) - { - case 92: - esc = true; - break; - - case 40: - case 44: - if (depth <= 0) - { - if (pos < i) - { - var label = code.substring(pos, i); - label = label.replace(/\\(.)/g, '$1'); - label = label.replace(/^[ \t]+/g, ''); - label = label.replace(/[ \t]+$/g, ''); - - if (label && !isNaN(label)) - { - stack.push(parseFloat(label)); - } - else if (label.match(/^(['"]).*\1$/)) - { - stack.push(label.replace(/^(['"])(.*)\1$/, '$2')); - } - else if (typeof cbi_validators[label] == 'function') - { - stack.push(cbi_validators[label]); - stack.push(null); - } - else - { - throw "Syntax error, unhandled token '"+label+"'"; - } - } - pos = i+1; - } - depth += (code.charCodeAt(i) == 40); - break; - - case 41: - if (--depth <= 0) - { - if (typeof stack[stack.length-2] != 'function') - throw "Syntax error, argument list follows non-function"; - - stack[stack.length-1] = - arguments.callee(code.substring(pos, i)); - - pos = i+1; - } - break; - } - } - - return stack; -} - -function cbi_validate_field(cbid, optional, type) -{ - var field = (typeof cbid == "string") ? document.getElementById(cbid) : cbid; - var vstack; try { vstack = cbi_validate_compile(type); } catch(e) { }; - - if (field && vstack && typeof vstack[0] == "function") - { - var validator = function() - { - // is not detached - if( field.form ) - { - field.className = field.className.replace(/ cbi-input-invalid/g, ''); - - // validate value - var value = (field.options && field.options.selectedIndex > -1) - ? field.options[field.options.selectedIndex].value : field.value; - - if (!(((value.length == 0) && optional) || vstack[0].apply(value, vstack[1]))) - { - // invalid - field.className += ' cbi-input-invalid'; - return false; - } - } - - return true; - }; - - if( ! field.form.cbi_validators ) - field.form.cbi_validators = [ ]; - - field.form.cbi_validators.push(validator); - - cbi_bind(field, "blur", validator); - cbi_bind(field, "keyup", validator); - - if (field.nodeName == 'SELECT') - { - cbi_bind(field, "change", validator); - cbi_bind(field, "click", validator); - } - - field.setAttribute("cbi_validate", validator); - field.setAttribute("cbi_datatype", type); - field.setAttribute("cbi_optional", (!!optional).toString()); - - validator(); - - var fcbox = document.getElementById('cbi.combobox.' + field.id); - if (fcbox) - cbi_validate_field(fcbox, optional, type); - } -} - -function cbi_row_swap(elem, up, store) -{ - var tr = elem.parentNode; - while (tr && tr.nodeName.toLowerCase() != 'tr') - tr = tr.parentNode; - - if (!tr) - return false; - - var table = tr.parentNode; - while (table && table.nodeName.toLowerCase() != 'table') - table = table.parentNode; - - if (!table) - return false; - - var s = up ? 3 : 2; - var e = up ? table.rows.length : table.rows.length - 1; - - for (var idx = s; idx < e; idx++) - { - if (table.rows[idx] == tr) - { - if (up) - tr.parentNode.insertBefore(table.rows[idx], table.rows[idx-1]); - else - tr.parentNode.insertBefore(table.rows[idx+1], table.rows[idx]); - - break; - } - } - - var ids = [ ]; - for (idx = 2; idx < table.rows.length; idx++) - { - table.rows[idx].className = table.rows[idx].className.replace( - /cbi-rowstyle-[12]/, 'cbi-rowstyle-' + (1 + (idx % 2)) - ); - - if (table.rows[idx].id && table.rows[idx].id.match(/-([^\-]+)$/) ) - ids.push(RegExp.$1); - } - - var input = document.getElementById(store); - if (input) - input.value = ids.join(' '); - - return false; -} - -function cbi_tag_last(container) -{ - var last; - - for (var i = 0; i < container.childNodes.length; i++) - { - var c = container.childNodes[i]; - if (c.nodeType == 1 && c.nodeName.toLowerCase() == 'div') - { - c.className = c.className.replace(/ cbi-value-last$/, ''); - last = c; - } - } - - if (last) - { - last.className += ' cbi-value-last'; - } -} - -String.prototype.serialize = function() -{ - var o = this; - switch(typeof(o)) - { - case 'object': - // null - if( o == null ) - { - return 'null'; - } - - // array - else if( o.length ) - { - var i, s = ''; - - for( var i = 0; i < o.length; i++ ) - s += (s ? ', ' : '') + String.serialize(o[i]); - - return '[ ' + s + ' ]'; - } - - // object - else - { - var k, s = ''; - - for( k in o ) - s += (s ? ', ' : '') + k + ': ' + String.serialize(o[k]); - - return '{ ' + s + ' }'; - } - - break; - - case 'string': - // complex string - if( o.match(/[^a-zA-Z0-9_,.: -]/) ) - return 'decodeURIComponent("' + encodeURIComponent(o) + '")'; - - // simple string - else - return '"' + o + '"'; - - break; - - default: - return o.toString(); - } -} - -String.prototype.format = function() -{ - if (!RegExp) - return; - - var html_esc = [/&/g, '&', /"/g, '"', /'/g, ''', /</g, '<', />/g, '>']; - var quot_esc = [/"/g, '"', /'/g, ''']; - - function esc(s, r) { - for( var i = 0; i < r.length; i += 2 ) - s = s.replace(r[i], r[i+1]); - return s; - } - - var str = this; - var out = ''; - var re = /^(([^%]*)%('.|0|\x20)?(-)?(\d+)?(\.\d+)?(%|b|c|d|u|f|o|s|x|X|q|h|j|t|m))/; - var a = b = [], numSubstitutions = 0, numMatches = 0; - - while( a = re.exec(str) ) - { - var m = a[1]; - var leftpart = a[2], pPad = a[3], pJustify = a[4], pMinLength = a[5]; - var pPrecision = a[6], pType = a[7]; - - numMatches++; - - if (pType == '%') - { - subst = '%'; - } - else - { - if (numSubstitutions < arguments.length) - { - var param = arguments[numSubstitutions++]; - - var pad = ''; - if (pPad && pPad.substr(0,1) == "'") - pad = leftpart.substr(1,1); - else if (pPad) - pad = pPad; - - var justifyRight = true; - if (pJustify && pJustify === "-") - justifyRight = false; - - var minLength = -1; - if (pMinLength) - minLength = parseInt(pMinLength); - - var precision = -1; - if (pPrecision && pType == 'f') - precision = parseInt(pPrecision.substring(1)); - - var subst = param; - - switch(pType) - { - case 'b': - subst = (parseInt(param) || 0).toString(2); - break; - - case 'c': - subst = String.fromCharCode(parseInt(param) || 0); - break; - - case 'd': - subst = (parseInt(param) || 0); - break; - - case 'u': - subst = Math.abs(parseInt(param) || 0); - break; - - case 'f': - subst = (precision > -1) - ? ((parseFloat(param) || 0.0)).toFixed(precision) - : (parseFloat(param) || 0.0); - break; - - case 'o': - subst = (parseInt(param) || 0).toString(8); - break; - - case 's': - subst = param; - break; - - case 'x': - subst = ('' + (parseInt(param) || 0).toString(16)).toLowerCase(); - break; - - case 'X': - subst = ('' + (parseInt(param) || 0).toString(16)).toUpperCase(); - break; - - case 'h': - subst = esc(param, html_esc); - break; - - case 'q': - subst = esc(param, quot_esc); - break; - - case 'j': - subst = String.serialize(param); - break; - - case 't': - var td = 0; - var th = 0; - var tm = 0; - var ts = (param || 0); - - if (ts > 60) { - tm = Math.floor(ts / 60); - ts = (ts % 60); - } - - if (tm > 60) { - th = Math.floor(tm / 60); - tm = (tm % 60); - } - - if (th > 24) { - td = Math.floor(th / 24); - th = (th % 24); - } - - subst = (td > 0) - ? String.format('%dd %dh %dm %ds', td, th, tm, ts) - : String.format('%dh %dm %ds', th, tm, ts); - - break; - - case 'm': - var mf = pMinLength ? parseInt(pMinLength) : 1000; - var pr = pPrecision ? Math.floor(10*parseFloat('0'+pPrecision)) : 2; - - var i = 0; - var val = parseFloat(param || 0); - var units = [ '', 'K', 'M', 'G', 'T', 'P', 'E' ]; - - for (i = 0; (i < units.length) && (val > mf); i++) - val /= mf; - - subst = val.toFixed(pr) + ' ' + units[i]; - break; - } - } - } - - out += leftpart + subst; - str = str.substr(m.length); - } - - return out + str; -} - -String.prototype.nobr = function() -{ - return this.replace(/[\s\n]+/g, ' '); -} - -String.serialize = function() -{ - var a = [ ]; - for (var i = 1; i < arguments.length; i++) - a.push(arguments[i]); - return ''.serialize.apply(arguments[0], a); -} - -String.format = function() -{ - var a = [ ]; - for (var i = 1; i < arguments.length; i++) - a.push(arguments[i]); - return ''.format.apply(arguments[0], a); -} - -String.nobr = function() -{ - var a = [ ]; - for (var i = 1; i < arguments.length; i++) - a.push(arguments[i]); - return ''.nobr.apply(arguments[0], a); -} diff --git a/libs/web/htdocs/luci-static/resources/cbi/add.gif b/libs/web/htdocs/luci-static/resources/cbi/add.gif Binary files differdeleted file mode 100644 index 0888abf85..000000000 --- a/libs/web/htdocs/luci-static/resources/cbi/add.gif +++ /dev/null diff --git a/libs/web/htdocs/luci-static/resources/cbi/apply.gif b/libs/web/htdocs/luci-static/resources/cbi/apply.gif Binary files differdeleted file mode 100644 index 82ae7ed82..000000000 --- a/libs/web/htdocs/luci-static/resources/cbi/apply.gif +++ /dev/null diff --git a/libs/web/htdocs/luci-static/resources/cbi/arrow.gif b/libs/web/htdocs/luci-static/resources/cbi/arrow.gif Binary files differdeleted file mode 100644 index 10d797e9b..000000000 --- a/libs/web/htdocs/luci-static/resources/cbi/arrow.gif +++ /dev/null diff --git a/libs/web/htdocs/luci-static/resources/cbi/down.gif b/libs/web/htdocs/luci-static/resources/cbi/down.gif Binary files differdeleted file mode 100644 index f0bb6a4ea..000000000 --- a/libs/web/htdocs/luci-static/resources/cbi/down.gif +++ /dev/null diff --git a/libs/web/htdocs/luci-static/resources/cbi/download.gif b/libs/web/htdocs/luci-static/resources/cbi/download.gif Binary files differdeleted file mode 100644 index f99a5383b..000000000 --- a/libs/web/htdocs/luci-static/resources/cbi/download.gif +++ /dev/null diff --git a/libs/web/htdocs/luci-static/resources/cbi/edit.gif b/libs/web/htdocs/luci-static/resources/cbi/edit.gif Binary files differdeleted file mode 100644 index 7b02b6e72..000000000 --- a/libs/web/htdocs/luci-static/resources/cbi/edit.gif +++ /dev/null diff --git a/libs/web/htdocs/luci-static/resources/cbi/fieldadd.gif b/libs/web/htdocs/luci-static/resources/cbi/fieldadd.gif Binary files differdeleted file mode 100644 index 431ff64d1..000000000 --- a/libs/web/htdocs/luci-static/resources/cbi/fieldadd.gif +++ /dev/null diff --git a/libs/web/htdocs/luci-static/resources/cbi/file.gif b/libs/web/htdocs/luci-static/resources/cbi/file.gif Binary files differdeleted file mode 100644 index 3b1217dd6..000000000 --- a/libs/web/htdocs/luci-static/resources/cbi/file.gif +++ /dev/null diff --git a/libs/web/htdocs/luci-static/resources/cbi/find.gif b/libs/web/htdocs/luci-static/resources/cbi/find.gif Binary files differdeleted file mode 100644 index 9ae5e3489..000000000 --- a/libs/web/htdocs/luci-static/resources/cbi/find.gif +++ /dev/null diff --git a/libs/web/htdocs/luci-static/resources/cbi/folder.gif b/libs/web/htdocs/luci-static/resources/cbi/folder.gif Binary files differdeleted file mode 100644 index 22b583bb5..000000000 --- a/libs/web/htdocs/luci-static/resources/cbi/folder.gif +++ /dev/null diff --git a/libs/web/htdocs/luci-static/resources/cbi/help.gif b/libs/web/htdocs/luci-static/resources/cbi/help.gif Binary files differdeleted file mode 100644 index 9dfa0e196..000000000 --- a/libs/web/htdocs/luci-static/resources/cbi/help.gif +++ /dev/null diff --git a/libs/web/htdocs/luci-static/resources/cbi/key.gif b/libs/web/htdocs/luci-static/resources/cbi/key.gif Binary files differdeleted file mode 100644 index e3853e5af..000000000 --- a/libs/web/htdocs/luci-static/resources/cbi/key.gif +++ /dev/null diff --git a/libs/web/htdocs/luci-static/resources/cbi/link.gif b/libs/web/htdocs/luci-static/resources/cbi/link.gif Binary files differdeleted file mode 100644 index f0bb78da6..000000000 --- a/libs/web/htdocs/luci-static/resources/cbi/link.gif +++ /dev/null diff --git a/libs/web/htdocs/luci-static/resources/cbi/reload.gif b/libs/web/htdocs/luci-static/resources/cbi/reload.gif Binary files differdeleted file mode 100644 index 8268958a1..000000000 --- a/libs/web/htdocs/luci-static/resources/cbi/reload.gif +++ /dev/null diff --git a/libs/web/htdocs/luci-static/resources/cbi/remove.gif b/libs/web/htdocs/luci-static/resources/cbi/remove.gif Binary files differdeleted file mode 100644 index bf43a0a0b..000000000 --- a/libs/web/htdocs/luci-static/resources/cbi/remove.gif +++ /dev/null diff --git a/libs/web/htdocs/luci-static/resources/cbi/reset.gif b/libs/web/htdocs/luci-static/resources/cbi/reset.gif Binary files differdeleted file mode 100644 index c941c1902..000000000 --- a/libs/web/htdocs/luci-static/resources/cbi/reset.gif +++ /dev/null diff --git a/libs/web/htdocs/luci-static/resources/cbi/save.gif b/libs/web/htdocs/luci-static/resources/cbi/save.gif Binary files differdeleted file mode 100644 index 35e949963..000000000 --- a/libs/web/htdocs/luci-static/resources/cbi/save.gif +++ /dev/null diff --git a/libs/web/htdocs/luci-static/resources/cbi/up.gif b/libs/web/htdocs/luci-static/resources/cbi/up.gif Binary files differdeleted file mode 100644 index e8234178e..000000000 --- a/libs/web/htdocs/luci-static/resources/cbi/up.gif +++ /dev/null diff --git a/libs/web/htdocs/luci-static/resources/cbi/user.gif b/libs/web/htdocs/luci-static/resources/cbi/user.gif Binary files differdeleted file mode 100644 index dcb5c2a89..000000000 --- a/libs/web/htdocs/luci-static/resources/cbi/user.gif +++ /dev/null diff --git a/libs/web/luasrc/cacheloader.lua b/libs/web/luasrc/cacheloader.lua deleted file mode 100644 index 942c4b7b4..000000000 --- a/libs/web/luasrc/cacheloader.lua +++ /dev/null @@ -1,23 +0,0 @@ ---[[ -LuCI - Lua Configuration Interface - -Copyright 2008 Steven Barth <steven@midlink.org> -Copyright 2008 Jo-Philipp Wich <xm@leipzig.freifunk.net> - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - -http://www.apache.org/licenses/LICENSE-2.0 - -$Id$ -]]-- - -local config = require "luci.config" -local ccache = require "luci.ccache" - -module "luci.cacheloader" - -if config.ccache and config.ccache.enable == "1" then - ccache.cache_ondemand() -end
\ No newline at end of file diff --git a/libs/web/luasrc/cbi.lua b/libs/web/luasrc/cbi.lua deleted file mode 100644 index ae570b155..000000000 --- a/libs/web/luasrc/cbi.lua +++ /dev/null @@ -1,1850 +0,0 @@ ---[[ -LuCI - Configuration Bind Interface - -Description: -Offers an interface for binding configuration values to certain -data types. Supports value and range validation and basic dependencies. - -FileId: -$Id$ - -License: -Copyright 2008 Steven Barth <steven@midlink.org> - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. - -]]-- -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 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 - - if not self.uci:load(self.config) then - error("Unable to read UCI data: " .. self.config) - 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 diff --git a/libs/web/luasrc/cbi/datatypes.lua b/libs/web/luasrc/cbi/datatypes.lua deleted file mode 100644 index c5f4ec0f0..000000000 --- a/libs/web/luasrc/cbi/datatypes.lua +++ /dev/null @@ -1,345 +0,0 @@ ---[[ - -LuCI - Configuration Bind Interface - Datatype Tests -(c) 2010 Jo-Philipp Wich <xm@subsignal.org> - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -$Id$ - -]]-- - -local fs = require "nixio.fs" -local ip = require "luci.ip" -local math = require "math" -local util = require "luci.util" -local tonumber, tostring, type, unpack, select = tonumber, tostring, type, unpack, select - - -module "luci.cbi.datatypes" - - -_M['or'] = function(v, ...) - local i - for i = 1, select('#', ...), 2 do - local f = select(i, ...) - local a = select(i+1, ...) - if type(f) ~= "function" then - if f == v then - return true - end - i = i - 1 - elseif f(v, unpack(a)) then - return true - end - end - return false -end - -_M['and'] = function(v, ...) - local i - for i = 1, select('#', ...), 2 do - local f = select(i, ...) - local a = select(i+1, ...) - if type(f) ~= "function" then - if f ~= v then - return false - end - i = i - 1 - elseif not f(v, unpack(a)) then - return false - end - end - return true -end - -function neg(v, ...) - return _M['or'](v:gsub("^%s*!%s*", ""), ...) -end - -function list(v, subvalidator, subargs) - if type(subvalidator) ~= "function" then - return false - end - local token - for token in v:gmatch("%S+") do - if not subvalidator(token, unpack(subargs)) then - return false - end - end - return true -end - -function bool(val) - if val == "1" or val == "yes" or val == "on" or val == "true" then - return true - elseif val == "0" or val == "no" or val == "off" or val == "false" then - return true - elseif val == "" or val == nil then - return true - end - - return false -end - -function uinteger(val) - local n = tonumber(val) - if n ~= nil and math.floor(n) == n and n >= 0 then - return true - end - - return false -end - -function integer(val) - local n = tonumber(val) - if n ~= nil and math.floor(n) == n then - return true - end - - return false -end - -function ufloat(val) - local n = tonumber(val) - return ( n ~= nil and n >= 0 ) -end - -function float(val) - return ( tonumber(val) ~= nil ) -end - -function ipaddr(val) - return ip4addr(val) or ip6addr(val) -end - -function ip4addr(val) - if val then - return ip.IPv4(val) and true or false - end - - return false -end - -function ip4prefix(val) - val = tonumber(val) - return ( val and val >= 0 and val <= 32 ) -end - -function ip6addr(val) - if val then - return ip.IPv6(val) and true or false - end - - return false -end - -function ip6prefix(val) - val = tonumber(val) - return ( val and val >= 0 and val <= 128 ) -end - -function port(val) - val = tonumber(val) - return ( val and val >= 0 and val <= 65535 ) -end - -function portrange(val) - local p1, p2 = val:match("^(%d+)%-(%d+)$") - if p1 and p2 and port(p1) and port(p2) then - return true - else - return port(val) - end -end - -function macaddr(val) - if val and val:match( - "^[a-fA-F0-9]+:[a-fA-F0-9]+:[a-fA-F0-9]+:" .. - "[a-fA-F0-9]+:[a-fA-F0-9]+:[a-fA-F0-9]+$" - ) then - local parts = util.split( val, ":" ) - - for i = 1,6 do - parts[i] = tonumber( parts[i], 16 ) - if parts[i] < 0 or parts[i] > 255 then - return false - end - end - - return true - end - - return false -end - -function hostname(val) - if val and (#val < 254) and ( - val:match("^[a-zA-Z_]+$") or - (val:match("^[a-zA-Z0-9_][a-zA-Z0-9_%-%.]*[a-zA-Z0-9]$") and - val:match("[^0-9%.]")) - ) then - return true - end - return false -end - -function host(val) - return hostname(val) or ipaddr(val) -end - -function network(val) - return uciname(val) or host(val) -end - -function wpakey(val) - if #val == 64 then - return (val:match("^[a-fA-F0-9]+$") ~= nil) - else - return (#val >= 8) and (#val <= 63) - end -end - -function wepkey(val) - if val:sub(1, 2) == "s:" then - val = val:sub(3) - end - - if (#val == 10) or (#val == 26) then - return (val:match("^[a-fA-F0-9]+$") ~= nil) - else - return (#val == 5) or (#val == 13) - end -end - -function string(val) - return true -- Everything qualifies as valid string -end - -function directory( val, seen ) - local s = fs.stat(val) - seen = seen or { } - - if s and not seen[s.ino] then - seen[s.ino] = true - if s.type == "dir" then - return true - elseif s.type == "lnk" then - return directory( fs.readlink(val), seen ) - end - end - - return false -end - -function file( val, seen ) - local s = fs.stat(val) - seen = seen or { } - - if s and not seen[s.ino] then - seen[s.ino] = true - if s.type == "reg" then - return true - elseif s.type == "lnk" then - return file( fs.readlink(val), seen ) - end - end - - return false -end - -function device( val, seen ) - local s = fs.stat(val) - seen = seen or { } - - if s and not seen[s.ino] then - seen[s.ino] = true - if s.type == "chr" or s.type == "blk" then - return true - elseif s.type == "lnk" then - return device( fs.readlink(val), seen ) - end - end - - return false -end - -function uciname(val) - return (val:match("^[a-zA-Z0-9_]+$") ~= nil) -end - -function range(val, min, max) - val = tonumber(val) - min = tonumber(min) - max = tonumber(max) - - if val ~= nil and min ~= nil and max ~= nil then - return ((val >= min) and (val <= max)) - end - - return false -end - -function min(val, min) - val = tonumber(val) - min = tonumber(min) - - if val ~= nil and min ~= nil then - return (val >= min) - end - - return false -end - -function max(val, max) - val = tonumber(val) - max = tonumber(max) - - if val ~= nil and max ~= nil then - return (val <= max) - end - - return false -end - -function rangelength(val, min, max) - val = tostring(val) - min = tonumber(min) - max = tonumber(max) - - if val ~= nil and min ~= nil and max ~= nil then - return ((#val >= min) and (#val <= max)) - end - - return false -end - -function minlength(val, min) - val = tostring(val) - min = tonumber(min) - - if val ~= nil and min ~= nil then - return (#val >= min) - end - - return false -end - -function maxlength(val, max) - val = tostring(val) - max = tonumber(max) - - if val ~= nil and max ~= nil then - return (#val <= max) - end - - return false -end - -function phonedigit(val) - return (val:match("^[0-9\*#!%.]+$") ~= nil) -end diff --git a/libs/web/luasrc/config.lua b/libs/web/luasrc/config.lua deleted file mode 100644 index 53db82b32..000000000 --- a/libs/web/luasrc/config.lua +++ /dev/null @@ -1,42 +0,0 @@ ---[[ -LuCI - Configuration - -Description: -Some LuCI configuration values read from uci file "luci" - - -FileId: -$Id$ - -License: -Copyright 2008 Steven Barth <steven@midlink.org> - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. - -]]-- - -local util = require "luci.util" -module("luci.config", - function(m) - if pcall(require, "luci.model.uci") then - local config = util.threadlocal() - setmetatable(m, { - __index = function(tbl, key) - if not config[key] then - config[key] = luci.model.uci.cursor():get_all("luci", key) - end - return config[key] - end - }) - end - end) diff --git a/libs/web/luasrc/dispatcher.lua b/libs/web/luasrc/dispatcher.lua deleted file mode 100644 index 9e5b78d5e..000000000 --- a/libs/web/luasrc/dispatcher.lua +++ /dev/null @@ -1,959 +0,0 @@ ---[[ -LuCI - Dispatcher - -Description: -The request dispatcher and module dispatcher generators - -FileId: -$Id$ - -License: -Copyright 2008 Steven Barth <steven@midlink.org> - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. - -]]-- - ---- LuCI web dispatcher. -local fs = require "nixio.fs" -local sys = require "luci.sys" -local init = require "luci.init" -local util = require "luci.util" -local http = require "luci.http" -local nixio = require "nixio", require "nixio.util" - -module("luci.dispatcher", package.seeall) -context = util.threadlocal() -uci = require "luci.model.uci" -i18n = require "luci.i18n" -_M.fs = fs - -authenticator = {} - --- Index table -local index = nil - --- Fastindex -local fi - - ---- Build the URL relative to the server webroot from given virtual path. --- @param ... Virtual path --- @return Relative URL -function build_url(...) - local path = {...} - local url = { http.getenv("SCRIPT_NAME") or "" } - - local k, v - for k, v in pairs(context.urltoken) do - url[#url+1] = "/;" - url[#url+1] = http.urlencode(k) - url[#url+1] = "=" - url[#url+1] = http.urlencode(v) - end - - local p - for _, p in ipairs(path) do - if p:match("^[a-zA-Z0-9_%-%.%%/,;]+$") then - url[#url+1] = "/" - url[#url+1] = p - end - end - - return table.concat(url, "") -end - ---- Check whether a dispatch node shall be visible --- @param node Dispatch node --- @return Boolean indicating whether the node should be visible -function node_visible(node) - if node then - return not ( - (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 - (type(node.nodes) ~= "table" or not next(node.nodes))) - ) - end - return false -end - ---- Return a sorted table of visible childs within a given node --- @param node Dispatch node --- @return Ordered table of child node names -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 - end - end - end - return rv -end - - ---- Send a 404 error code and render the "error404" template if available. --- @param message Custom error message (optional) --- @return false -function error404(message) - luci.http.status(404, "Not Found") - message = message or "Not Found" - - require("luci.template") - if not luci.util.copcall(luci.template.render, "error404") then - luci.http.prepare_content("text/plain") - luci.http.write(message) - end - return false -end - ---- Send a 500 error code and render the "error500" template if available. --- @param message Custom error message (optional)# --- @return false -function error500(message) - luci.util.perror(message) - if not context.template_header_sent then - luci.http.status(500, "Internal Server Error") - luci.http.prepare_content("text/plain") - luci.http.write(message) - else - require("luci.template") - if not luci.util.copcall(luci.template.render, "error500", {message=message}) then - luci.http.prepare_content("text/plain") - luci.http.write(message) - end - end - return false -end - -function authenticator.htmlauth(validator, accs, default) - local user = luci.http.formvalue("username") - local pass = luci.http.formvalue("password") - - if user and validator(user, pass) then - return user - end - - require("luci.i18n") - require("luci.template") - context.path = {} - luci.template.render("sysauth", {duser=default, fuser=user}) - return false - -end - ---- Dispatch an HTTP request. --- @param request LuCI HTTP Request object -function httpdispatch(request, prefix) - luci.http.context.request = request - - local r = {} - context.request = r - context.urltoken = {} - - local pathinfo = http.urldecode(request:getenv("PATH_INFO") or "", true) - - if prefix then - for _, node in ipairs(prefix) do - r[#r+1] = node - end - end - - local tokensok = true - for node in pathinfo:gmatch("[^/]+") do - local tkey, tval - if tokensok then - tkey, tval = node:match(";(%w+)=([a-fA-F0-9]*)") - end - if tkey then - context.urltoken[tkey] = tval - else - tokensok = false - r[#r+1] = node - end - end - - local stat, err = util.coxpcall(function() - dispatch(context.request) - end, error500) - - luci.http.close() - - --context._disable_memtrace() -end - ---- Dispatches a LuCI virtual path. --- @param request Virtual path -function dispatch(request) - --context._disable_memtrace = require "luci.debug".trap_memtrace("l") - local ctx = context - ctx.path = request - - local conf = require "luci.config" - assert(conf.main, - "/etc/config/luci seems to be corrupt, unable to find section 'main'") - - local lang = conf.main.lang or "auto" - if lang == "auto" then - local aclang = http.getenv("HTTP_ACCEPT_LANGUAGE") or "" - for lpat in aclang:gmatch("[%w-]+") do - lpat = lpat and lpat:gsub("-", "_") - if conf.languages[lpat] then - lang = lpat - break - end - end - end - require "luci.i18n".setlanguage(lang) - - local c = ctx.tree - local stat - if not c then - c = createtree() - end - - local track = {} - local args = {} - ctx.args = args - ctx.requestargs = ctx.requestargs or args - local n - local token = ctx.urltoken - local preq = {} - local freq = {} - - for i, s in ipairs(request) do - preq[#preq+1] = s - freq[#freq+1] = s - c = c.nodes[s] - n = i - if not c then - break - end - - util.update(track, c) - - if c.leaf then - break - end - end - - if c and c.leaf then - for j=n+1, #request do - args[#args+1] = request[j] - freq[#freq+1] = request[j] - end - end - - ctx.requestpath = ctx.requestpath or freq - ctx.path = preq - - if track.i18n then - i18n.loadc(track.i18n) - end - - -- Init template engine - if (c and c.index) or not track.notemplate then - local tpl = require("luci.template") - local media = track.mediaurlbase or luci.config.main.mediaurlbase - if not pcall(tpl.Template, "themes/%s/header" % fs.basename(media)) then - media = nil - for name, theme in pairs(luci.config.themes) do - if name:sub(1,1) ~= "." and pcall(tpl.Template, - "themes/%s/header" % fs.basename(theme)) then - media = theme - end - end - assert(media, "No valid theme found") - end - - local function _ifattr(cond, key, val) - if cond then - local env = getfenv(3) - local scope = (type(env.self) == "table") and env.self - return string.format( - ' %s="%s"', tostring(key), - luci.util.pcdata(tostring( val - or (type(env[key]) ~= "function" and env[key]) - or (scope and type(scope[key]) ~= "function" and scope[key]) - or "" )) - ) - else - return '' - end - end - - tpl.context.viewns = setmetatable({ - write = luci.http.write; - include = function(name) tpl.Template(name):render(getfenv(2)) end; - translate = i18n.translate; - translatef = i18n.translatef; - export = function(k, v) if tpl.context.viewns[k] == nil then tpl.context.viewns[k] = v end end; - striptags = util.striptags; - pcdata = util.pcdata; - media = media; - theme = fs.basename(media); - resource = luci.config.main.resourcebase; - ifattr = function(...) return _ifattr(...) end; - attr = function(...) return _ifattr(true, ...) end; - }, {__index=function(table, key) - if key == "controller" then - return build_url() - elseif key == "REQUEST_URI" then - return build_url(unpack(ctx.requestpath)) - else - return rawget(table, key) or _G[key] - end - end}) - end - - track.dependent = (track.dependent ~= false) - assert(not track.dependent or not track.auto, - "Access Violation\nThe page at '" .. table.concat(request, "/") .. "/' " .. - "has no parent node so the access to this location has been denied.\n" .. - "This is a software bug, please report this message at " .. - "http://luci.subsignal.org/trac/newticket" - ) - - if track.sysauth then - local sauth = require "luci.sauth" - - local authen = type(track.sysauth_authenticator) == "function" - and track.sysauth_authenticator - or authenticator[track.sysauth_authenticator] - - local def = (type(track.sysauth) == "string") and track.sysauth - local accs = def and {track.sysauth} or track.sysauth - local sess = ctx.authsession - local verifytoken = false - if not sess then - sess = luci.http.getcookie("sysauth") - sess = sess and sess:match("^[a-f0-9]*$") - verifytoken = true - end - - local sdat = sauth.read(sess) - local user - - if sdat then - if not verifytoken or ctx.urltoken.stok == sdat.token then - user = sdat.user - end - else - local eu = http.getenv("HTTP_AUTH_USER") - local ep = http.getenv("HTTP_AUTH_PASS") - if eu and ep and luci.sys.user.checkpasswd(eu, ep) then - authen = function() return eu end - end - end - - if not util.contains(accs, user) then - if authen then - ctx.urltoken.stok = nil - local user, sess = authen(luci.sys.user.checkpasswd, accs, def) - if not user or not util.contains(accs, user) then - return - else - local sid = sess or luci.sys.uniqueid(16) - if not sess then - local token = luci.sys.uniqueid(16) - sauth.reap() - sauth.write(sid, { - user=user, - token=token, - secret=luci.sys.uniqueid(16) - }) - ctx.urltoken.stok = token - end - luci.http.header("Set-Cookie", "sysauth=" .. sid.."; path="..build_url()) - ctx.authsession = sid - ctx.authuser = user - end - else - luci.http.status(403, "Forbidden") - return - end - else - ctx.authsession = sess - ctx.authuser = user - end - end - - if track.setgroup then - luci.sys.process.setgroup(track.setgroup) - end - - if track.setuser then - luci.sys.process.setuser(track.setuser) - end - - local target = nil - if c then - if type(c.target) == "function" then - target = c.target - elseif type(c.target) == "table" then - target = c.target.target - end - end - - if c and (c.index or type(target) == "function") then - ctx.dispatched = c - ctx.requested = ctx.requested or ctx.dispatched - end - - if c and c.index then - local tpl = require "luci.template" - - if util.copcall(tpl.render, "indexer", {}) then - return true - end - end - - if type(target) == "function" then - util.copcall(function() - local oldenv = getfenv(target) - local module = require(c.module) - local env = setmetatable({}, {__index= - - function(tbl, key) - return rawget(tbl, key) or module[key] or oldenv[key] - end}) - - setfenv(target, env) - end) - - local ok, err - if type(c.target) == "table" then - ok, err = util.copcall(target, c.target, unpack(args)) - else - ok, err = util.copcall(target, unpack(args)) - end - assert(ok, - "Failed to execute " .. (type(c.target) == "function" and "function" or c.target.type or "unknown") .. - " dispatcher target for entry '/" .. table.concat(request, "/") .. "'.\n" .. - "The called action terminated with an exception:\n" .. tostring(err or "(unknown)")) - else - local root = node() - if not root or not root.target then - error404("No root node was registered, this usually happens if no module was installed.\n" .. - "Install luci-mod-admin-full and retry. " .. - "If the module is already installed, try removing the /tmp/luci-indexcache file.") - else - error404("No page is registered at '/" .. table.concat(request, "/") .. "'.\n" .. - "If this url belongs to an extension, make sure it is properly installed.\n" .. - "If the extension was recently installed, try removing the /tmp/luci-indexcache file.") - end - end -end - ---- Generate the dispatching index using the best possible strategy. -function createindex() - local path = luci.util.libpath() .. "/controller/" - local suff = { ".lua", ".lua.gz" } - - if luci.util.copcall(require, "luci.fastindex") then - createindex_fastindex(path, suff) - else - createindex_plain(path, suff) - end -end - ---- Generate the dispatching index using the fastindex C-indexer. --- @param path Controller base directory --- @param suffixes Controller file suffixes -function createindex_fastindex(path, suffixes) - index = {} - - if not fi then - fi = luci.fastindex.new("index") - for _, suffix in ipairs(suffixes) do - fi.add(path .. "*" .. suffix) - fi.add(path .. "*/*" .. suffix) - end - end - fi.scan() - - for k, v in pairs(fi.indexes) do - index[v[2]] = v[1] - end -end - ---- Generate the dispatching index using the native file-cache based strategy. --- @param path Controller base directory --- @param suffixes Controller file suffixes -function createindex_plain(path, suffixes) - local controllers = { } - for _, suffix in ipairs(suffixes) do - nixio.util.consume((fs.glob(path .. "*" .. suffix)), controllers) - nixio.util.consume((fs.glob(path .. "*/*" .. suffix)), controllers) - end - - if indexcache then - local cachedate = fs.stat(indexcache, "mtime") - if cachedate then - local realdate = 0 - for _, obj in ipairs(controllers) do - local omtime = fs.stat(obj, "mtime") - realdate = (omtime and omtime > realdate) and omtime or realdate - end - - if cachedate > realdate then - assert( - sys.process.info("uid") == fs.stat(indexcache, "uid") - and fs.stat(indexcache, "modestr") == "rw-------", - "Fatal: Indexcache is not sane!" - ) - - index = loadfile(indexcache)() - return index - end - end - end - - index = {} - - for i,c in ipairs(controllers) do - local modname = "luci.controller." .. c:sub(#path+1, #c):gsub("/", ".") - for _, suffix in ipairs(suffixes) do - modname = modname:gsub(suffix.."$", "") - end - - local mod = require(modname) - assert(mod ~= true, - "Invalid controller file found\n" .. - "The file '" .. c .. "' contains an invalid module line.\n" .. - "Please verify whether the module name is set to '" .. modname .. - "' - It must correspond to the file path!") - - local idx = mod.index - assert(type(idx) == "function", - "Invalid controller file found\n" .. - "The file '" .. c .. "' contains no index() function.\n" .. - "Please make sure that the controller contains a valid " .. - "index function and verify the spelling!") - - index[modname] = idx - end - - if indexcache then - local f = nixio.open(indexcache, "w", 600) - f:writeall(util.get_bytecode(index)) - f:close() - end -end - ---- Create the dispatching tree from the index. --- Build the index before if it does not exist yet. -function createtree() - if not index then - createindex() - end - - local ctx = context - local tree = {nodes={}, inreq=true} - local modi = {} - - ctx.treecache = setmetatable({}, {__mode="v"}) - ctx.tree = tree - ctx.modifiers = modi - - -- Load default translation - require "luci.i18n".loadc("base") - - local scope = setmetatable({}, {__index = luci.dispatcher}) - - for k, v in pairs(index) do - scope._NAME = k - setfenv(v, scope) - 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 - ---- Register a tree modifier. --- @param func Modifier function --- @param order Modifier order value (optional) -function modifier(func, order) - context.modifiers[#context.modifiers+1] = { - func = func, - order = order or 0, - module - = getfenv(2)._NAME - } -end - ---- Clone a node of the dispatching tree to another position. --- @param path Virtual path destination --- @param clone Virtual path source --- @param title Destination node title (optional) --- @param order Destination node order value (optional) --- @return Dispatching tree node -function assign(path, clone, title, order) - local obj = node(unpack(path)) - obj.nodes = nil - obj.module = nil - - obj.title = title - obj.order = order - - setmetatable(obj, {__index = _create_node(clone)}) - - return obj -end - ---- Create a new dispatching node and define common parameters. --- @param path Virtual path --- @param target Target function to call when dispatched. --- @param title Destination node title --- @param order Destination node order value (optional) --- @return Dispatching tree node -function entry(path, target, title, order) - local c = node(unpack(path)) - - c.target = target - c.title = title - c.order = order - c.module = getfenv(2)._NAME - - return c -end - ---- Fetch or create a dispatching node without setting the target module or --- enabling the node. --- @param ... Virtual path --- @return Dispatching tree node -function get(...) - return _create_node({...}) -end - ---- Fetch or create a new dispatching node. --- @param ... Virtual path --- @return Dispatching tree node -function node(...) - local c = _create_node({...}) - - c.module = getfenv(2)._NAME - c.auto = nil - - return c -end - -function _create_node(path) - if #path == 0 then - return context.tree - end - - local name = table.concat(path, ".") - local c = context.treecache[name] - - if not c then - local last = table.remove(path) - local parent = _create_node(path) - - c = {nodes={}, auto=true} - -- the node is "in request" if the request path matches - -- at least up to the length of the node path - if parent.inreq and context.path[#path+1] == last then - c.inreq = true - end - parent.nodes[last] = c - context.treecache[name] = c - end - return c -end - --- Subdispatchers -- - -function _firstchild() - local path = { unpack(context.path) } - local name = table.concat(path, ".") - local node = context.treecache[name] - - local lowest - if node and node.nodes and next(node.nodes) then - local k, v - for k, v in pairs(node.nodes) do - if not lowest or - (v.order or 100) < (node.nodes[lowest].order or 100) - then - lowest = k - end - end - end - - assert(lowest ~= nil, - "The requested node contains no childs, unable to redispatch") - - path[#path+1] = lowest - dispatch(path) -end - ---- Alias the first (lowest order) page automatically -function firstchild() - return { type = "firstchild", target = _firstchild } -end - ---- Create a redirect to another dispatching node. --- @param ... Virtual path destination -function alias(...) - local req = {...} - return function(...) - for _, r in ipairs({...}) do - req[#req+1] = r - end - - dispatch(req) - end -end - ---- Rewrite the first x path values of the request. --- @param n Number of path values to replace --- @param ... Virtual path to replace removed path values with -function rewrite(n, ...) - local req = {...} - return function(...) - local dispatched = util.clone(context.dispatched) - - for i=1,n do - table.remove(dispatched, 1) - end - - for i, r in ipairs(req) do - table.insert(dispatched, i, r) - end - - for _, r in ipairs({...}) do - dispatched[#dispatched+1] = r - end - - dispatch(dispatched) - end -end - - -local function _call(self, ...) - local func = getfenv()[self.name] - assert(func ~= nil, - 'Cannot resolve function "' .. self.name .. '". Is it misspelled or local?') - - assert(type(func) == "function", - 'The symbol "' .. self.name .. '" does not refer to a function but data ' .. - 'of type "' .. type(func) .. '".') - - if #self.argv > 0 then - return func(unpack(self.argv), ...) - else - return func(...) - end -end - ---- Create a function-call dispatching target. --- @param name Target function of local controller --- @param ... Additional parameters passed to the function -function call(name, ...) - return {type = "call", argv = {...}, name = name, target = _call} -end - - -local _template = function(self, ...) - require "luci.template".render(self.view) -end - ---- Create a template render dispatching target. --- @param name Template to be rendered -function template(name) - return {type = "template", view = name, target = _template} -end - - -local function _cbi(self, ...) - local cbi = require "luci.cbi" - local tpl = require "luci.template" - local http = require "luci.http" - - local config = self.config or {} - local maps = cbi.load(self.model, ...) - - local state = nil - - for i, res in ipairs(maps) do - res.flow = config - local cstate = res:parse() - if cstate and (not state or cstate < state) then - state = cstate - end - end - - local function _resolve_path(path) - return type(path) == "table" and build_url(unpack(path)) or path - end - - if config.on_valid_to and state and state > 0 and state < 2 then - http.redirect(_resolve_path(config.on_valid_to)) - return - end - - if config.on_changed_to and state and state > 1 then - http.redirect(_resolve_path(config.on_changed_to)) - return - end - - if config.on_success_to and state and state > 0 then - http.redirect(_resolve_path(config.on_success_to)) - return - end - - if config.state_handler then - if not config.state_handler(state, maps) then - return - end - end - - http.header("X-CBI-State", state or 0) - - if not config.noheader then - tpl.render("cbi/header", {state = state}) - end - - local redirect - local messages - local applymap = false - local pageaction = true - local parsechain = { } - - for i, res in ipairs(maps) do - if res.apply_needed and res.parsechain then - local c - for _, c in ipairs(res.parsechain) do - parsechain[#parsechain+1] = c - end - applymap = true - end - - if res.redirect then - redirect = redirect or res.redirect - end - - if res.pageaction == false then - pageaction = false - end - - if res.message then - messages = messages or { } - messages[#messages+1] = res.message - end - end - - for i, res in ipairs(maps) do - res:render({ - firstmap = (i == 1), - applymap = applymap, - redirect = redirect, - messages = messages, - pageaction = pageaction, - parsechain = parsechain - }) - end - - if not config.nofooter then - tpl.render("cbi/footer", { - flow = config, - pageaction = pageaction, - redirect = redirect, - state = state, - autoapply = config.autoapply - }) - end -end - ---- Create a CBI model dispatching target. --- @param model CBI model to be rendered -function cbi(model, config) - return {type = "cbi", config = config, model = model, target = _cbi} -end - - -local function _arcombine(self, ...) - local argv = {...} - local target = #argv > 0 and self.targets[2] or self.targets[1] - setfenv(target.target, self.env) - target:target(unpack(argv)) -end - ---- Create a combined dispatching target for non argv and argv requests. --- @param trg1 Overview Target --- @param trg2 Detail Target -function arcombine(trg1, trg2) - return {type = "arcombine", env = getfenv(), target = _arcombine, targets = {trg1, trg2}} -end - - -local function _form(self, ...) - local cbi = require "luci.cbi" - local tpl = require "luci.template" - local http = require "luci.http" - - local maps = luci.cbi.load(self.model, ...) - local state = nil - - for i, res in ipairs(maps) do - local cstate = res:parse() - if cstate and (not state or cstate < state) then - state = cstate - end - end - - http.header("X-CBI-State", state or 0) - tpl.render("header") - for i, res in ipairs(maps) do - res:render() - end - tpl.render("footer") -end - ---- Create a CBI form model dispatching target. --- @param model CBI form model tpo be rendered -function form(model) - return {type = "cbi", model = model, target = _form} -end - ---- Access the luci.i18n translate() api. --- @class function --- @name translate --- @param text Text to translate -translate = i18n.translate - ---- No-op function used to mark translation entries for menu labels. --- This function does not actually translate the given argument but --- is used by build/i18n-scan.pl to find translatable entries. -function _(text) - return text -end diff --git a/libs/web/luasrc/http.lua b/libs/web/luasrc/http.lua deleted file mode 100644 index c53307a5a..000000000 --- a/libs/web/luasrc/http.lua +++ /dev/null @@ -1,344 +0,0 @@ ---[[ -LuCI - HTTP-Interaction - -Description: -HTTP-Header manipulator and form variable preprocessor - -License: -Copyright 2008 Steven Barth <steven@midlink.org> - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. - -]]-- - -local ltn12 = require "luci.ltn12" -local protocol = require "luci.http.protocol" -local util = require "luci.util" -local string = require "string" -local coroutine = require "coroutine" -local table = require "table" - -local ipairs, pairs, next, type, tostring, error = - ipairs, pairs, next, type, tostring, error - ---- LuCI Web Framework high-level HTTP functions. -module "luci.http" - -context = util.threadlocal() - -Request = util.class() -function Request.__init__(self, env, sourcein, sinkerr) - self.input = sourcein - self.error = sinkerr - - - -- File handler - self.filehandler = function() end - - -- HTTP-Message table - self.message = { - env = env, - headers = {}, - params = protocol.urldecode_params(env.QUERY_STRING or ""), - } - - self.parsed_input = false -end - -function Request.formvalue(self, name, noparse) - if not noparse and not self.parsed_input then - self:_parse_input() - end - - if name then - return self.message.params[name] - else - return self.message.params - end -end - -function Request.formvaluetable(self, prefix) - local vals = {} - prefix = prefix and prefix .. "." or "." - - if not self.parsed_input then - self:_parse_input() - end - - local void = self.message.params[nil] - for k, v in pairs(self.message.params) do - if k:find(prefix, 1, true) == 1 then - vals[k:sub(#prefix + 1)] = tostring(v) - end - end - - return vals -end - -function Request.content(self) - if not self.parsed_input then - self:_parse_input() - end - - return self.message.content, self.message.content_length -end - -function Request.getcookie(self, name) - local c = string.gsub(";" .. (self:getenv("HTTP_COOKIE") or "") .. ";", "%s*;%s*", ";") - local p = ";" .. name .. "=(.-);" - local i, j, value = c:find(p) - return value and urldecode(value) -end - -function Request.getenv(self, name) - if name then - return self.message.env[name] - else - return self.message.env - end -end - -function Request.setfilehandler(self, callback) - self.filehandler = callback -end - -function Request._parse_input(self) - protocol.parse_message_body( - self.input, - self.message, - self.filehandler - ) - self.parsed_input = true -end - ---- Close the HTTP-Connection. -function close() - if not context.eoh then - context.eoh = true - coroutine.yield(3) - end - - if not context.closed then - context.closed = true - coroutine.yield(5) - end -end - ---- Return the request content if the request was of unknown type. --- @return HTTP request body --- @return HTTP request body length -function content() - return context.request:content() -end - ---- Get a certain HTTP input value or a table of all input values. --- @param name Name of the GET or POST variable to fetch --- @param noparse Don't parse POST data before getting the value --- @return HTTP input value or table of all input value -function formvalue(name, noparse) - return context.request:formvalue(name, noparse) -end - ---- Get a table of all HTTP input values with a certain prefix. --- @param prefix Prefix --- @return Table of all HTTP input values with given prefix -function formvaluetable(prefix) - return context.request:formvaluetable(prefix) -end - ---- Get the value of a certain HTTP-Cookie. --- @param name Cookie Name --- @return String containing cookie data -function getcookie(name) - return context.request:getcookie(name) -end - ---- Get the value of a certain HTTP environment variable --- or the environment table itself. --- @param name Environment variable --- @return HTTP environment value or environment table -function getenv(name) - return context.request:getenv(name) -end - ---- Set a handler function for incoming user file uploads. --- @param callback Handler function -function setfilehandler(callback) - return context.request:setfilehandler(callback) -end - ---- Send a HTTP-Header. --- @param key Header key --- @param value Header value -function header(key, value) - if not context.headers then - context.headers = {} - end - context.headers[key:lower()] = value - coroutine.yield(2, key, value) -end - ---- Set the mime type of following content data. --- @param mime Mimetype of following content -function prepare_content(mime) - if not context.headers or not context.headers["content-type"] then - if mime == "application/xhtml+xml" then - if not getenv("HTTP_ACCEPT") or - not getenv("HTTP_ACCEPT"):find("application/xhtml+xml", nil, true) then - mime = "text/html; charset=UTF-8" - end - header("Vary", "Accept") - end - header("Content-Type", mime) - end -end - ---- Get the RAW HTTP input source --- @return HTTP LTN12 source -function source() - return context.request.input -end - ---- Set the HTTP status code and status message. --- @param code Status code --- @param message Status message -function status(code, message) - code = code or 200 - message = message or "OK" - context.status = code - coroutine.yield(1, code, message) -end - ---- Send a chunk of content data to the client. --- This function is as a valid LTN12 sink. --- If the content chunk is nil this function will automatically invoke close. --- @param content Content chunk --- @param src_err Error object from source (optional) --- @see close -function write(content, src_err) - if not content then - if src_err then - error(src_err) - else - close() - end - return true - elseif #content == 0 then - return true - else - if not context.eoh then - if not context.status then - status() - end - if not context.headers or not context.headers["content-type"] then - header("Content-Type", "text/html; charset=utf-8") - end - if not context.headers["cache-control"] then - header("Cache-Control", "no-cache") - header("Expires", "0") - end - - - context.eoh = true - coroutine.yield(3) - end - coroutine.yield(4, content) - return true - end -end - ---- Splice data from a filedescriptor to the client. --- @param fp File descriptor --- @param size Bytes to splice (optional) -function splice(fd, size) - coroutine.yield(6, fd, size) -end - ---- Redirects the client to a new URL and closes the connection. --- @param url Target URL -function redirect(url) - status(302, "Found") - header("Location", url) - close() -end - ---- Create a querystring out of a table of key - value pairs. --- @param table Query string source table --- @return Encoded HTTP query string -function build_querystring(q) - local s = { "?" } - - for k, v in pairs(q) do - if #s > 1 then s[#s+1] = "&" end - - s[#s+1] = urldecode(k) - s[#s+1] = "=" - s[#s+1] = urldecode(v) - end - - return table.concat(s, "") -end - ---- Return the URL-decoded equivalent of a string. --- @param str URL-encoded string --- @param no_plus Don't decode + to " " --- @return URL-decoded string --- @see urlencode -urldecode = protocol.urldecode - ---- Return the URL-encoded equivalent of a string. --- @param str Source string --- @return URL-encoded string --- @see urldecode -urlencode = protocol.urlencode - ---- Send the given data as JSON encoded string. --- @param data Data to send -function write_json(x) - if x == nil then - write("null") - elseif type(x) == "table" then - local k, v - if type(next(x)) == "number" then - write("[ ") - for k, v in ipairs(x) do - write_json(v) - if next(x, k) then - write(", ") - end - end - write(" ]") - else - write("{ ") - for k, v in pairs(x) do - write("%q: " % k) - write_json(v) - if next(x, k) then - write(", ") - end - end - write(" }") - end - elseif type(x) == "number" or type(x) == "boolean" then - if (x ~= x) then - -- NaN is the only value that doesn't equal to itself. - write("Number.NaN") - else - write(tostring(x)) - end - else - write('"%s"' % tostring(x):gsub('["%z\1-\31]', function(c) - return '\\u%04x' % c:byte(1) - end)) - end -end diff --git a/libs/web/luasrc/http/protocol.lua b/libs/web/luasrc/http/protocol.lua deleted file mode 100644 index 0d41550b2..000000000 --- a/libs/web/luasrc/http/protocol.lua +++ /dev/null @@ -1,688 +0,0 @@ ---[[ - -HTTP protocol implementation for LuCI -(c) 2008 Freifunk Leipzig / Jo-Philipp Wich <xm@leipzig.freifunk.net> - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -$Id$ - -]]-- - ---- LuCI http protocol class. --- This class contains several functions useful for http message- and content --- decoding and to retrive form data from raw http messages. -module("luci.http.protocol", package.seeall) - -local ltn12 = require("luci.ltn12") - -HTTP_MAX_CONTENT = 1024*8 -- 8 kB maximum content size - ---- Decode an urlencoded string - optionally without decoding --- the "+" sign to " " - and return the decoded string. --- @param str Input string in x-www-urlencoded format --- @param no_plus Don't decode "+" signs to spaces --- @return The decoded string --- @see urlencode -function urldecode( str, no_plus ) - - local function __chrdec( hex ) - return string.char( tonumber( hex, 16 ) ) - end - - if type(str) == "string" then - if not no_plus then - str = str:gsub( "+", " " ) - end - - str = str:gsub( "%%([a-fA-F0-9][a-fA-F0-9])", __chrdec ) - end - - return str -end - ---- Extract and split urlencoded data pairs, separated bei either "&" or ";" --- from given url or string. Returns a table with urldecoded values. --- Simple parameters are stored as string values associated with the parameter --- name within the table. Parameters with multiple values are stored as array --- containing the corresponding values. --- @param url The url or string which contains x-www-urlencoded form data --- @param tbl Use the given table for storing values (optional) --- @return Table containing the urldecoded parameters --- @see urlencode_params -function urldecode_params( url, tbl ) - - local params = tbl or { } - - if url:find("?") then - url = url:gsub( "^.+%?([^?]+)", "%1" ) - end - - for pair in url:gmatch( "[^&;]+" ) do - - -- find key and value - local key = urldecode( pair:match("^([^=]+)") ) - local val = urldecode( pair:match("^[^=]+=(.+)$") ) - - -- store - if type(key) == "string" and key:len() > 0 then - if type(val) ~= "string" then val = "" end - - if not params[key] then - params[key] = val - elseif type(params[key]) ~= "table" then - params[key] = { params[key], val } - else - table.insert( params[key], val ) - end - end - end - - return params -end - ---- Encode given string to x-www-urlencoded format. --- @param str String to encode --- @return String containing the encoded data --- @see urldecode -function urlencode( str ) - - local function __chrenc( chr ) - return string.format( - "%%%02x", string.byte( chr ) - ) - end - - if type(str) == "string" then - str = str:gsub( - "([^a-zA-Z0-9$_%-%.%+!*'(),])", - __chrenc - ) - end - - return str -end - ---- Encode each key-value-pair in given table to x-www-urlencoded format, --- separated by "&". Tables are encoded as parameters with multiple values by --- repeating the parameter name with each value. --- @param tbl Table with the values --- @return String containing encoded values --- @see urldecode_params -function urlencode_params( tbl ) - local enc = "" - - for k, v in pairs(tbl) do - if type(v) == "table" then - for i, v2 in ipairs(v) do - enc = enc .. ( #enc > 0 and "&" or "" ) .. - urlencode(k) .. "=" .. urlencode(v2) - end - else - enc = enc .. ( #enc > 0 and "&" or "" ) .. - urlencode(k) .. "=" .. urlencode(v) - end - end - - return enc -end - --- (Internal function) --- Initialize given parameter and coerce string into table when the parameter --- already exists. --- @param tbl Table where parameter should be created --- @param key Parameter name --- @return Always nil -local function __initval( tbl, key ) - if tbl[key] == nil then - tbl[key] = "" - elseif type(tbl[key]) == "string" then - tbl[key] = { tbl[key], "" } - else - table.insert( tbl[key], "" ) - end -end - --- (Internal function) --- Append given data to given parameter, either by extending the string value --- or by appending it to the last string in the parameter's value table. --- @param tbl Table containing the previously initialized parameter value --- @param key Parameter name --- @param chunk String containing the data to append --- @return Always nil --- @see __initval -local function __appendval( tbl, key, chunk ) - if type(tbl[key]) == "table" then - tbl[key][#tbl[key]] = tbl[key][#tbl[key]] .. chunk - else - tbl[key] = tbl[key] .. chunk - end -end - --- (Internal function) --- Finish the value of given parameter, either by transforming the string value --- or - in the case of multi value parameters - the last element in the --- associated values table. --- @param tbl Table containing the previously initialized parameter value --- @param key Parameter name --- @param handler Function which transforms the parameter value --- @return Always nil --- @see __initval --- @see __appendval -local function __finishval( tbl, key, handler ) - if handler then - if type(tbl[key]) == "table" then - tbl[key][#tbl[key]] = handler( tbl[key][#tbl[key]] ) - else - tbl[key] = handler( tbl[key] ) - end - end -end - - --- Table of our process states -local process_states = { } - --- Extract "magic", the first line of a http message. --- Extracts the message type ("get", "post" or "response"), the requested uri --- or the status code if the line descripes a http response. -process_states['magic'] = function( msg, chunk, err ) - - if chunk ~= nil then - -- ignore empty lines before request - if #chunk == 0 then - return true, nil - end - - -- Is it a request? - local method, uri, http_ver = chunk:match("^([A-Z]+) ([^ ]+) HTTP/([01]%.[019])$") - - -- Yup, it is - if method then - - msg.type = "request" - msg.request_method = method:lower() - msg.request_uri = uri - msg.http_version = tonumber( http_ver ) - msg.headers = { } - - -- We're done, next state is header parsing - return true, function( chunk ) - return process_states['headers']( msg, chunk ) - end - - -- Is it a response? - else - - local http_ver, code, message = chunk:match("^HTTP/([01]%.[019]) ([0-9]+) ([^\r\n]+)$") - - -- Is a response - if code then - - msg.type = "response" - msg.status_code = code - msg.status_message = message - msg.http_version = tonumber( http_ver ) - msg.headers = { } - - -- We're done, next state is header parsing - return true, function( chunk ) - return process_states['headers']( msg, chunk ) - end - end - end - end - - -- Can't handle it - return nil, "Invalid HTTP message magic" -end - - --- Extract headers from given string. -process_states['headers'] = function( msg, chunk ) - - if chunk ~= nil then - - -- Look for a valid header format - local hdr, val = chunk:match( "^([A-Za-z][A-Za-z0-9%-_]+): +(.+)$" ) - - if type(hdr) == "string" and hdr:len() > 0 and - type(val) == "string" and val:len() > 0 - then - msg.headers[hdr] = val - - -- Valid header line, proceed - return true, nil - - elseif #chunk == 0 then - -- Empty line, we won't accept data anymore - return false, nil - else - -- Junk data - return nil, "Invalid HTTP header received" - end - else - return nil, "Unexpected EOF" - end -end - - ---- Creates a ltn12 source from the given socket. The source will return it's --- data line by line with the trailing \r\n stripped of. --- @param sock Readable network socket --- @return Ltn12 source function -function header_source( sock ) - return ltn12.source.simplify( function() - - local chunk, err, part = sock:receive("*l") - - -- Line too long - if chunk == nil then - if err ~= "timeout" then - return nil, part - and "Line exceeds maximum allowed length" - or "Unexpected EOF" - else - return nil, err - end - - -- Line ok - elseif chunk ~= nil then - - -- Strip trailing CR - chunk = chunk:gsub("\r$","") - - return chunk, nil - end - end ) -end - ---- Decode a mime encoded http message body with multipart/form-data --- Content-Type. Stores all extracted data associated with its parameter name --- in the params table withing the given message object. Multiple parameter --- values are stored as tables, ordinary ones as strings. --- If an optional file callback function is given then it is feeded with the --- file contents chunk by chunk and only the extracted file name is stored --- within the params table. The callback function will be called subsequently --- with three arguments: --- o Table containing decoded (name, file) and raw (headers) mime header data --- o String value containing a chunk of the file data --- o Boolean which indicates wheather the current chunk is the last one (eof) --- @param src Ltn12 source function --- @param msg HTTP message object --- @param filecb File callback function (optional) --- @return Value indicating successful operation (not nil means "ok") --- @return String containing the error if unsuccessful --- @see parse_message_header -function mimedecode_message_body( src, msg, filecb ) - - if msg and msg.env.CONTENT_TYPE then - msg.mime_boundary = msg.env.CONTENT_TYPE:match("^multipart/form%-data; boundary=(.+)$") - end - - if not msg.mime_boundary then - return nil, "Invalid Content-Type found" - end - - - local tlen = 0 - local inhdr = false - local field = nil - local store = nil - local lchunk = nil - - local function parse_headers( chunk, field ) - - local stat - repeat - chunk, stat = chunk:gsub( - "^([A-Z][A-Za-z0-9%-_]+): +([^\r\n]+)\r\n", - function(k,v) - field.headers[k] = v - return "" - end - ) - until stat == 0 - - chunk, stat = chunk:gsub("^\r\n","") - - -- End of headers - if stat > 0 then - if field.headers["Content-Disposition"] then - if field.headers["Content-Disposition"]:match("^form%-data; ") then - field.name = field.headers["Content-Disposition"]:match('name="(.-)"') - field.file = field.headers["Content-Disposition"]:match('filename="(.+)"$') - end - end - - if not field.headers["Content-Type"] then - field.headers["Content-Type"] = "text/plain" - end - - if field.name and field.file and filecb then - __initval( msg.params, field.name ) - __appendval( msg.params, field.name, field.file ) - - store = filecb - elseif field.name then - __initval( msg.params, field.name ) - - store = function( hdr, buf, eof ) - __appendval( msg.params, field.name, buf ) - end - else - store = nil - end - - return chunk, true - end - - return chunk, false - end - - local function snk( chunk ) - - tlen = tlen + ( chunk and #chunk or 0 ) - - if msg.env.CONTENT_LENGTH and tlen > tonumber(msg.env.CONTENT_LENGTH) + 2 then - return nil, "Message body size exceeds Content-Length" - end - - if chunk and not lchunk then - lchunk = "\r\n" .. chunk - - elseif lchunk then - local data = lchunk .. ( chunk or "" ) - local spos, epos, found - - repeat - spos, epos = data:find( "\r\n--" .. msg.mime_boundary .. "\r\n", 1, true ) - - if not spos then - spos, epos = data:find( "\r\n--" .. msg.mime_boundary .. "--\r\n", 1, true ) - end - - - if spos then - local predata = data:sub( 1, spos - 1 ) - - if inhdr then - predata, eof = parse_headers( predata, field ) - - if not eof then - return nil, "Invalid MIME section header" - elseif not field.name then - return nil, "Invalid Content-Disposition header" - end - end - - if store then - store( field, predata, true ) - end - - - field = { headers = { } } - found = found or true - - data, eof = parse_headers( data:sub( epos + 1, #data ), field ) - inhdr = not eof - end - until not spos - - if found then - -- We found at least some boundary. Save - -- the unparsed remaining data for the - -- next chunk. - lchunk, data = data, nil - else - -- There was a complete chunk without a boundary. Parse it as headers or - -- append it as data, depending on our current state. - if inhdr then - lchunk, eof = parse_headers( data, field ) - inhdr = not eof - else - -- We're inside data, so append the data. Note that we only append - -- lchunk, not all of data, since there is a chance that chunk - -- contains half a boundary. Assuming that each chunk is at least the - -- boundary in size, this should prevent problems - store( field, lchunk, false ) - lchunk, chunk = chunk, nil - end - end - end - - return true - end - - return ltn12.pump.all( src, snk ) -end - ---- Decode an urlencoded http message body with application/x-www-urlencoded --- Content-Type. Stores all extracted data associated with its parameter name --- in the params table withing the given message object. Multiple parameter --- values are stored as tables, ordinary ones as strings. --- @param src Ltn12 source function --- @param msg HTTP message object --- @return Value indicating successful operation (not nil means "ok") --- @return String containing the error if unsuccessful --- @see parse_message_header -function urldecode_message_body( src, msg ) - - local tlen = 0 - local lchunk = nil - - local function snk( chunk ) - - tlen = tlen + ( chunk and #chunk or 0 ) - - if msg.env.CONTENT_LENGTH and tlen > tonumber(msg.env.CONTENT_LENGTH) + 2 then - return nil, "Message body size exceeds Content-Length" - elseif tlen > HTTP_MAX_CONTENT then - return nil, "Message body size exceeds maximum allowed length" - end - - if not lchunk and chunk then - lchunk = chunk - - elseif lchunk then - local data = lchunk .. ( chunk or "&" ) - local spos, epos - - repeat - spos, epos = data:find("^.-[;&]") - - if spos then - local pair = data:sub( spos, epos - 1 ) - local key = pair:match("^(.-)=") - local val = pair:match("=([^%s]*)%s*$") - - if key and #key > 0 then - __initval( msg.params, key ) - __appendval( msg.params, key, val ) - __finishval( msg.params, key, urldecode ) - end - - data = data:sub( epos + 1, #data ) - end - until not spos - - lchunk = data - end - - return true - end - - return ltn12.pump.all( src, snk ) -end - ---- Try to extract an http message header including information like protocol --- version, message headers and resulting CGI environment variables from the --- given ltn12 source. --- @param src Ltn12 source function --- @return HTTP message object --- @see parse_message_body -function parse_message_header( src ) - - local ok = true - local msg = { } - - local sink = ltn12.sink.simplify( - function( chunk ) - return process_states['magic']( msg, chunk ) - end - ) - - -- Pump input data... - while ok do - - -- get data - ok, err = ltn12.pump.step( src, sink ) - - -- error - if not ok and err then - return nil, err - - -- eof - elseif not ok then - - -- Process get parameters - if ( msg.request_method == "get" or msg.request_method == "post" ) and - msg.request_uri:match("?") - then - msg.params = urldecode_params( msg.request_uri ) - else - msg.params = { } - end - - -- Populate common environment variables - msg.env = { - CONTENT_LENGTH = msg.headers['Content-Length']; - CONTENT_TYPE = msg.headers['Content-Type'] or msg.headers['Content-type']; - REQUEST_METHOD = msg.request_method:upper(); - REQUEST_URI = msg.request_uri; - SCRIPT_NAME = msg.request_uri:gsub("?.+$",""); - SCRIPT_FILENAME = ""; -- XXX implement me - SERVER_PROTOCOL = "HTTP/" .. string.format("%.1f", msg.http_version); - QUERY_STRING = msg.request_uri:match("?") - and msg.request_uri:gsub("^.+?","") or "" - } - - -- Populate HTTP_* environment variables - for i, hdr in ipairs( { - 'Accept', - 'Accept-Charset', - 'Accept-Encoding', - 'Accept-Language', - 'Connection', - 'Cookie', - 'Host', - 'Referer', - 'User-Agent', - } ) do - local var = 'HTTP_' .. hdr:upper():gsub("%-","_") - local val = msg.headers[hdr] - - msg.env[var] = val - end - end - end - - return msg -end - ---- Try to extract and decode a http message body from the given ltn12 source. --- This function will examine the Content-Type within the given message object --- to select the appropriate content decoder. --- Currently the application/x-www-urlencoded and application/form-data --- mime types are supported. If the encountered content encoding can't be --- handled then the whole message body will be stored unaltered as "content" --- property within the given message object. --- @param src Ltn12 source function --- @param msg HTTP message object --- @param filecb File data callback (optional, see mimedecode_message_body()) --- @return Value indicating successful operation (not nil means "ok") --- @return String containing the error if unsuccessful --- @see parse_message_header -function parse_message_body( src, msg, filecb ) - -- Is it multipart/mime ? - if msg.env.REQUEST_METHOD == "POST" and msg.env.CONTENT_TYPE and - msg.env.CONTENT_TYPE:match("^multipart/form%-data") - then - - return mimedecode_message_body( src, msg, filecb ) - - -- Is it application/x-www-form-urlencoded ? - elseif msg.env.REQUEST_METHOD == "POST" and msg.env.CONTENT_TYPE and - msg.env.CONTENT_TYPE:match("^application/x%-www%-form%-urlencoded") - then - return urldecode_message_body( src, msg, filecb ) - - - -- Unhandled encoding - -- If a file callback is given then feed it chunk by chunk, else - -- store whole buffer in message.content - else - - local sink - - -- If we have a file callback then feed it - if type(filecb) == "function" then - sink = filecb - - -- ... else append to .content - else - msg.content = "" - msg.content_length = 0 - - sink = function( chunk, err ) - if chunk then - if ( msg.content_length + #chunk ) <= HTTP_MAX_CONTENT then - msg.content = msg.content .. chunk - msg.content_length = msg.content_length + #chunk - return true - else - return nil, "POST data exceeds maximum allowed length" - end - end - return true - end - end - - -- Pump data... - while true do - local ok, err = ltn12.pump.step( src, sink ) - - if not ok and err then - return nil, err - elseif not err then - return true - end - end - - return true - end -end - ---- Table containing human readable messages for several http status codes. --- @class table -statusmsg = { - [200] = "OK", - [206] = "Partial Content", - [301] = "Moved Permanently", - [302] = "Found", - [304] = "Not Modified", - [400] = "Bad Request", - [403] = "Forbidden", - [404] = "Not Found", - [405] = "Method Not Allowed", - [408] = "Request Time-out", - [411] = "Length Required", - [412] = "Precondition Failed", - [416] = "Requested range not satisfiable", - [500] = "Internal Server Error", - [503] = "Server Unavailable", -} diff --git a/libs/web/luasrc/http/protocol/conditionals.lua b/libs/web/luasrc/http/protocol/conditionals.lua deleted file mode 100644 index 75e1f7b37..000000000 --- a/libs/web/luasrc/http/protocol/conditionals.lua +++ /dev/null @@ -1,153 +0,0 @@ ---[[ - -HTTP protocol implementation for LuCI - RFC2616 / 14.19, 14.24 - 14.28 -(c) 2008 Freifunk Leipzig / Jo-Philipp Wich <xm@leipzig.freifunk.net> - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -$Id$ - -]]-- - ---- LuCI http protocol implementation - HTTP/1.1 bits. --- This class provides basic ETag handling and implements most of the --- conditional HTTP/1.1 headers specified in RFC2616 Sct. 14.24 - 14.28 . -module("luci.http.protocol.conditionals", package.seeall) - -local date = require("luci.http.protocol.date") - - ---- Implement 14.19 / ETag. --- @param stat A file.stat structure --- @return String containing the generated tag suitable for ETag headers -function mk_etag( stat ) - if stat ~= nil then - return string.format( '"%x-%x-%x"', stat.ino, stat.size, stat.mtime ) - end -end - ---- 14.24 / If-Match --- Test whether the given message object contains an "If-Match" header and --- compare it against the given stat object. --- @param req HTTP request message object --- @param stat A file.stat object --- @return Boolean indicating whether the precondition is ok --- @return Alternative status code if the precondition failed -function if_match( req, stat ) - local h = req.headers - local etag = mk_etag( stat ) - - -- Check for matching resource - if type(h['If-Match']) == "string" then - for ent in h['If-Match']:gmatch("([^, ]+)") do - if ( ent == '*' or ent == etag ) and stat ~= nil then - return true - end - end - - return false, 412 - end - - return true -end - ---- 14.25 / If-Modified-Since --- Test whether the given message object contains an "If-Modified-Since" header --- and compare it against the given stat object. --- @param req HTTP request message object --- @param stat A file.stat object --- @return Boolean indicating whether the precondition is ok --- @return Alternative status code if the precondition failed --- @return Table containing extra HTTP headers if the precondition failed -function if_modified_since( req, stat ) - local h = req.headers - - -- Compare mtimes - if type(h['If-Modified-Since']) == "string" then - local since = date.to_unix( h['If-Modified-Since'] ) - - if stat == nil or since < stat.mtime then - return true - end - - return false, 304, { - ["ETag"] = mk_etag( stat ); - ["Date"] = date.to_http( os.time() ); - ["Last-Modified"] = date.to_http( stat.mtime ) - } - end - - return true -end - ---- 14.26 / If-None-Match --- Test whether the given message object contains an "If-None-Match" header and --- compare it against the given stat object. --- @param req HTTP request message object --- @param stat A file.stat object --- @return Boolean indicating whether the precondition is ok --- @return Alternative status code if the precondition failed --- @return Table containing extra HTTP headers if the precondition failed -function if_none_match( req, stat ) - local h = req.headers - local etag = mk_etag( stat ) - local method = req.env and req.env.REQUEST_METHOD or "GET" - - -- Check for matching resource - if type(h['If-None-Match']) == "string" then - for ent in h['If-None-Match']:gmatch("([^, ]+)") do - if ( ent == '*' or ent == etag ) and stat ~= nil then - if method == "GET" or method == "HEAD" then - return false, 304, { - ["ETag"] = etag; - ["Date"] = date.to_http( os.time() ); - ["Last-Modified"] = date.to_http( stat.mtime ) - } - else - return false, 412 - end - end - end - end - - return true -end - ---- 14.27 / If-Range --- The If-Range header is currently not implemented due to the lack of general --- byte range stuff in luci.http.protocol . This function will always return --- false, 412 to indicate a failed precondition. --- @param req HTTP request message object --- @param stat A file.stat object --- @return Boolean indicating whether the precondition is ok --- @return Alternative status code if the precondition failed -function if_range( req, stat ) - -- Sorry, no subranges (yet) - return false, 412 -end - ---- 14.28 / If-Unmodified-Since --- Test whether the given message object contains an "If-Unmodified-Since" --- header and compare it against the given stat object. --- @param req HTTP request message object --- @param stat A file.stat object --- @return Boolean indicating whether the precondition is ok --- @return Alternative status code if the precondition failed -function if_unmodified_since( req, stat ) - local h = req.headers - - -- Compare mtimes - if type(h['If-Unmodified-Since']) == "string" then - local since = date.to_unix( h['If-Unmodified-Since'] ) - - if stat ~= nil and since <= stat.mtime then - return false, 412 - end - end - - return true -end diff --git a/libs/web/luasrc/http/protocol/date.lua b/libs/web/luasrc/http/protocol/date.lua deleted file mode 100644 index 83d11e2c2..000000000 --- a/libs/web/luasrc/http/protocol/date.lua +++ /dev/null @@ -1,115 +0,0 @@ ---[[ - -HTTP protocol implementation for LuCI - date handling -(c) 2008 Freifunk Leipzig / Jo-Philipp Wich <xm@leipzig.freifunk.net> - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -$Id$ - -]]-- - ---- LuCI http protocol implementation - date helper class. --- This class contains functions to parse, compare and format http dates. -module("luci.http.protocol.date", package.seeall) - -require("luci.sys.zoneinfo") - - -MONTHS = { - "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", - "Sep", "Oct", "Nov", "Dec" -} - ---- Return the time offset in seconds between the UTC and given time zone. --- @param tz Symbolic or numeric timezone specifier --- @return Time offset to UTC in seconds -function tz_offset(tz) - - if type(tz) == "string" then - - -- check for a numeric identifier - local s, v = tz:match("([%+%-])([0-9]+)") - if s == '+' then s = 1 else s = -1 end - if v then v = tonumber(v) end - - if s and v then - return s * 60 * ( math.floor( v / 100 ) * 60 + ( v % 100 ) ) - - -- lookup symbolic tz - elseif luci.sys.zoneinfo.OFFSET[tz:lower()] then - return luci.sys.zoneinfo.OFFSET[tz:lower()] - end - - end - - -- bad luck - return 0 -end - ---- Parse given HTTP date string and convert it to unix epoch time. --- @param data String containing the date --- @return Unix epoch time -function to_unix(date) - - local wd, day, mon, yr, hr, min, sec, tz = date:match( - "([A-Z][a-z][a-z]), ([0-9]+) " .. - "([A-Z][a-z][a-z]) ([0-9]+) " .. - "([0-9]+):([0-9]+):([0-9]+) " .. - "([A-Z0-9%+%-]+)" - ) - - if day and mon and yr and hr and min and sec then - -- find month - local month = 1 - for i = 1, 12 do - if MONTHS[i] == mon then - month = i - break - end - end - - -- convert to epoch time - return tz_offset(tz) + os.time( { - year = yr, - month = month, - day = day, - hour = hr, - min = min, - sec = sec - } ) - end - - return 0 -end - ---- Convert the given unix epoch time to valid HTTP date string. --- @param time Unix epoch time --- @return String containing the formatted date -function to_http(time) - return os.date( "%a, %d %b %Y %H:%M:%S GMT", time ) -end - ---- Compare two dates which can either be unix epoch times or HTTP date strings. --- @param d1 The first date or epoch time to compare --- @param d2 The first date or epoch time to compare --- @return -1 - if d1 is lower then d2 --- @return 0 - if both dates are equal --- @return 1 - if d1 is higher then d2 -function compare(d1, d2) - - if d1:match("[^0-9]") then d1 = to_unix(d1) end - if d2:match("[^0-9]") then d2 = to_unix(d2) end - - if d1 == d2 then - return 0 - elseif d1 < d2 then - return -1 - else - return 1 - end -end diff --git a/libs/web/luasrc/http/protocol/mime.lua b/libs/web/luasrc/http/protocol/mime.lua deleted file mode 100644 index c87816066..000000000 --- a/libs/web/luasrc/http/protocol/mime.lua +++ /dev/null @@ -1,99 +0,0 @@ ---[[ - -HTTP protocol implementation for LuCI - mime handling -(c) 2008 Freifunk Leipzig / Jo-Philipp Wich <xm@leipzig.freifunk.net> - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -$Id$ - -]]-- - ---- LuCI http protocol implementation - mime helper class. --- This class provides functions to guess mime types from file extensions and --- vice versa. -module("luci.http.protocol.mime", package.seeall) - -require("luci.util") - ---- MIME mapping table containg extension - mimetype relations. --- @class table -MIME_TYPES = { - ["txt"] = "text/plain"; - ["js"] = "text/javascript"; - ["css"] = "text/css"; - ["htm"] = "text/html"; - ["html"] = "text/html"; - ["patch"] = "text/x-patch"; - ["c"] = "text/x-csrc"; - ["h"] = "text/x-chdr"; - ["o"] = "text/x-object"; - ["ko"] = "text/x-object"; - - ["bmp"] = "image/bmp"; - ["gif"] = "image/gif"; - ["png"] = "image/png"; - ["jpg"] = "image/jpeg"; - ["jpeg"] = "image/jpeg"; - ["svg"] = "image/svg+xml"; - - ["zip"] = "application/zip"; - ["pdf"] = "application/pdf"; - ["xml"] = "application/xml"; - ["xsl"] = "application/xml"; - ["doc"] = "application/msword"; - ["ppt"] = "application/vnd.ms-powerpoint"; - ["xls"] = "application/vnd.ms-excel"; - ["odt"] = "application/vnd.oasis.opendocument.text"; - ["odp"] = "application/vnd.oasis.opendocument.presentation"; - ["pl"] = "application/x-perl"; - ["sh"] = "application/x-shellscript"; - ["php"] = "application/x-php"; - ["deb"] = "application/x-deb"; - ["iso"] = "application/x-cd-image"; - ["tgz"] = "application/x-compressed-tar"; - - ["mp3"] = "audio/mpeg"; - ["ogg"] = "audio/x-vorbis+ogg"; - ["wav"] = "audio/x-wav"; - - ["mpg"] = "video/mpeg"; - ["mpeg"] = "video/mpeg"; - ["avi"] = "video/x-msvideo"; -} - ---- Extract extension from a filename and return corresponding mime-type or --- "application/octet-stream" if the extension is unknown. --- @param filename The filename for which the mime type is guessed --- @return String containign the determined mime type -function to_mime(filename) - if type(filename) == "string" then - local ext = filename:match("[^%.]+$") - - if ext and MIME_TYPES[ext:lower()] then - return MIME_TYPES[ext:lower()] - end - end - - return "application/octet-stream" -end - ---- Return corresponding extension for a given mime type or nil if the --- given mime-type is unknown. --- @param mimetype The mimetype to retrieve the extension from --- @return String with the extension or nil for unknown type -function to_ext(mimetype) - if type(mimetype) == "string" then - for ext, type in luci.util.kspairs( MIME_TYPES ) do - if type == mimetype then - return ext - end - end - end - - return nil -end diff --git a/libs/web/luasrc/i18n.lua b/libs/web/luasrc/i18n.lua deleted file mode 100644 index 545a8aed9..000000000 --- a/libs/web/luasrc/i18n.lua +++ /dev/null @@ -1,104 +0,0 @@ ---[[ -LuCI - Internationalisation - -Description: -A very minimalistic but yet effective internationalisation module - -FileId: -$Id$ - -License: -Copyright 2008 Steven Barth <steven@midlink.org> - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. - -]]-- - ---- LuCI translation library. -module("luci.i18n", package.seeall) -require("luci.util") - -local tparser = require "luci.template.parser" - -table = {} -i18ndir = luci.util.libpath() .. "/i18n/" -loaded = {} -context = luci.util.threadlocal() -default = "en" - ---- Clear the translation table. -function clear() -end - ---- Load a translation and copy its data into the translation table. --- @param file Language file --- @param lang Two-letter language code --- @param force Force reload even if already loaded (optional) --- @return Success status -function load(file, lang, force) -end - ---- Load a translation file using the default translation language. --- Alternatively load the translation of the fallback language. --- @param file Language file --- @param force Force reload even if already loaded (optional) -function loadc(file, force) -end - ---- Set the context default translation language. --- @param lang Two-letter language code -function setlanguage(lang) - context.lang = lang:gsub("_", "-") - context.parent = (context.lang:match("^([a-z][a-z])_")) - if not tparser.load_catalog(context.lang, i18ndir) then - if context.parent then - tparser.load_catalog(context.parent, i18ndir) - return context.parent - end - end - return context.lang -end - ---- Return the translated value for a specific translation key. --- @param key Default translation text --- @return Translated string -function translate(key) - return tparser.translate(key) or key -end - ---- Return the translated value for a specific translation key and use it as sprintf pattern. --- @param key Default translation text --- @param ... Format parameters --- @return Translated and formatted string -function translatef(key, ...) - return tostring(translate(key)):format(...) -end - ---- Return the translated value for a specific translation key --- and ensure that the returned value is a Lua string value. --- This is the same as calling <code>tostring(translate(...))</code> --- @param key Default translation text --- @return Translated string -function string(key) - return tostring(translate(key)) -end - ---- Return the translated value for a specific translation key and use it as sprintf pattern. --- Ensure that the returned value is a Lua string value. --- This is the same as calling <code>tostring(translatef(...))</code> --- @param key Default translation text --- @param ... Format parameters --- @return Translated and formatted string -function stringf(key, ...) - return tostring(translate(key)):format(...) -end diff --git a/libs/web/luasrc/sauth.lua b/libs/web/luasrc/sauth.lua deleted file mode 100644 index 32f172dcd..000000000 --- a/libs/web/luasrc/sauth.lua +++ /dev/null @@ -1,127 +0,0 @@ ---[[ - -Session authentication -(c) 2008 Steven Barth <steven@midlink.org> - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -$Id$ - -]]-- - ---- LuCI session library. -module("luci.sauth", package.seeall) -require("luci.util") -require("luci.sys") -require("luci.config") -local nixio = require "nixio", require "nixio.util" -local fs = require "nixio.fs" - - -luci.config.sauth = luci.config.sauth or {} -sessionpath = luci.config.sauth.sessionpath -sessiontime = tonumber(luci.config.sauth.sessiontime) or 15 * 60 - ---- Prepare session storage by creating the session directory. -function prepare() - fs.mkdir(sessionpath, 700) - if not sane() then - error("Security Exception: Session path is not sane!") - end -end - -local function _read(id) - local blob = fs.readfile(sessionpath .. "/" .. id) - return blob -end - -local function _write(id, data) - local f = nixio.open(sessionpath .. "/" .. id, "w", 600) - f:writeall(data) - f:close() -end - -local function _checkid(id) - return not not (id and #id == 32 and id:match("^[a-fA-F0-9]+$")) -end - ---- Write session data to a session file. --- @param id Session identifier --- @param data Session data table -function write(id, data) - if not sane() then - prepare() - end - - assert(_checkid(id), "Security Exception: Session ID is invalid!") - assert(type(data) == "table", "Security Exception: Session data invalid!") - - data.atime = luci.sys.uptime() - - _write(id, luci.util.get_bytecode(data)) -end - ---- Read a session and return its content. --- @param id Session identifier --- @return Session data table or nil if the given id is not found -function read(id) - if not id or #id == 0 then - return nil - end - - assert(_checkid(id), "Security Exception: Session ID is invalid!") - - if not sane(sessionpath .. "/" .. id) then - return nil - end - - local blob = _read(id) - local func = loadstring(blob) - setfenv(func, {}) - - local sess = func() - assert(type(sess) == "table", "Session data invalid!") - - if sess.atime and sess.atime + sessiontime < luci.sys.uptime() then - kill(id) - return nil - end - - -- refresh atime in session - write(id, sess) - - return sess -end - ---- Check whether Session environment is sane. --- @return Boolean status -function sane(file) - return luci.sys.process.info("uid") - == fs.stat(file or sessionpath, "uid") - and fs.stat(file or sessionpath, "modestr") - == (file and "rw-------" or "rwx------") -end - ---- Kills a session --- @param id Session identifier -function kill(id) - assert(_checkid(id), "Security Exception: Session ID is invalid!") - fs.unlink(sessionpath .. "/" .. id) -end - ---- Remove all expired session data files -function reap() - if sane() then - local id - for id in nixio.fs.dir(sessionpath) do - if _checkid(id) then - -- reading the session will kill it if it is expired - read(id) - end - end - end -end diff --git a/libs/web/luasrc/template.lua b/libs/web/luasrc/template.lua deleted file mode 100644 index 72127d1df..000000000 --- a/libs/web/luasrc/template.lua +++ /dev/null @@ -1,107 +0,0 @@ ---[[ -LuCI - Template Parser - -Description: -A template parser supporting includes, translations, Lua code blocks -and more. It can be used either as a compiler or as an interpreter. - -FileId: $Id$ - -License: -Copyright 2008 Steven Barth <steven@midlink.org> - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. - -]]-- - -local util = require "luci.util" -local config = require "luci.config" -local tparser = require "luci.template.parser" - -local tostring, pairs, loadstring = tostring, pairs, loadstring -local setmetatable, loadfile = setmetatable, loadfile -local getfenv, setfenv, rawget = getfenv, setfenv, rawget -local assert, type, error = assert, type, error - ---- LuCI template library. -module "luci.template" - -config.template = config.template or {} -viewdir = config.template.viewdir or util.libpath() .. "/view" - - --- Define the namespace for template modules -context = util.threadlocal() - ---- Render a certain template. --- @param name Template name --- @param scope Scope to assign to template (optional) -function render(name, scope) - return Template(name):render(scope or getfenv(2)) -end - - --- Template class -Template = util.class() - --- Shared template cache to store templates in to avoid unnecessary reloading -Template.cache = setmetatable({}, {__mode = "v"}) - - --- Constructor - Reads and compiles the template on-demand -function Template.__init__(self, name) - - self.template = self.cache[name] - self.name = name - - -- Create a new namespace for this template - self.viewns = context.viewns - - -- If we have a cached template, skip compiling and loading - if not self.template then - - -- Compile template - local err - local sourcefile = viewdir .. "/" .. name .. ".htm" - - self.template, _, err = tparser.parse(sourcefile) - - -- If we have no valid template throw error, otherwise cache the template - if not self.template then - error("Failed to load template '" .. name .. "'.\n" .. - "Error while parsing template '" .. sourcefile .. "':\n" .. - (err or "Unknown syntax error")) - else - self.cache[name] = self.template - end - end -end - - --- Renders a template -function Template.render(self, scope) - scope = scope or getfenv(2) - - -- Put our predefined objects in the scope of the template - setfenv(self.template, setmetatable({}, {__index = - function(tbl, key) - return rawget(tbl, key) or self.viewns[key] or scope[key] - end})) - - -- Now finally render the thing - local stat, err = util.copcall(self.template) - if not stat then - error("Failed to execute template '" .. self.name .. "'.\n" .. - "A runtime error occured: " .. tostring(err or "(nil)")) - end -end diff --git a/libs/web/luasrc/view/cbi/apply_xhr.htm b/libs/web/luasrc/view/cbi/apply_xhr.htm deleted file mode 100644 index 1814c9393..000000000 --- a/libs/web/luasrc/view/cbi/apply_xhr.htm +++ /dev/null @@ -1,43 +0,0 @@ -<% export("cbi_apply_xhr", function(id, configs, redirect) -%> -<fieldset class="cbi-section" id="cbi-apply-<%=id%>"> - <legend><%:Applying changes%></legend> - <script type="text/javascript">//<![CDATA[ - var apply_xhr = new XHR(); - - apply_xhr.get('<%=luci.dispatcher.build_url("servicectl", "restart", table.concat(configs, ","))%>', null, - function() { - var checkfinish = function() { - apply_xhr.get('<%=luci.dispatcher.build_url("servicectl", "status")%>', null, - function(x) { - if( x.responseText == 'finish' ) - { - var e = document.getElementById('cbi-apply-<%=id%>-status'); - if( e ) - { - e.innerHTML = '<%:Configuration applied.%>'; - window.setTimeout(function() { - e.parentNode.style.display = 'none'; - <% if redirect then %>location.href='<%=redirect%>';<% end %> - }, 1000); - } - } - else - { - var e = document.getElementById('cbi-apply-<%=id%>-status'); - if( e && x.responseText ) e.innerHTML = x.responseText; - - window.setTimeout(checkfinish, 1000); - } - } - ); - } - - window.setTimeout(checkfinish, 1000); - } - ); - //]]></script> - - <img src="<%=resource%>/icons/loading.gif" alt="<%:Loading%>" style="vertical-align:middle" /> - <span id="cbi-apply-<%=id%>-status"><%:Waiting for changes to be applied...%></span> -</fieldset> -<%- end) %> diff --git a/libs/web/luasrc/view/cbi/browser.htm b/libs/web/luasrc/view/cbi/browser.htm deleted file mode 100644 index e4a4077d5..000000000 --- a/libs/web/luasrc/view/cbi/browser.htm +++ /dev/null @@ -1,7 +0,0 @@ -<% local v = self:cfgvalue(section) -%> -<%+cbi/valueheader%> - <input class="cbi-input-text" type="text"<%= attr("value", v) .. attr("name", cbid) .. attr("id", cbid) %> /> - <script type="text/javascript"> -cbi_browser_init('<%=cbid%>', '<%=resource%>', '<%=luci.dispatcher.build_url("admin", "filebrowser")%>'<%=self.default_path and ", '"..self.default_path.."'"%>); - </script> -<%+cbi/valuefooter%> diff --git a/libs/web/luasrc/view/cbi/button.htm b/libs/web/luasrc/view/cbi/button.htm deleted file mode 100644 index 30f8ddfda..000000000 --- a/libs/web/luasrc/view/cbi/button.htm +++ /dev/null @@ -1,7 +0,0 @@ -<%+cbi/valueheader%> - <% if self:cfgvalue(section) ~= false then %> - <input class="cbi-button cbi-input-<%=self.inputstyle or "button" %>" type="submit"<%= attr("name", cbid) .. attr("id", cbid) .. attr("value", self.inputtitle or self.title)%> /> - <% else %> - - - <% end %> -<%+cbi/valuefooter%> diff --git a/libs/web/luasrc/view/cbi/cell_valuefooter.htm b/libs/web/luasrc/view/cbi/cell_valuefooter.htm deleted file mode 100644 index 220ebd42b..000000000 --- a/libs/web/luasrc/view/cbi/cell_valuefooter.htm +++ /dev/null @@ -1,20 +0,0 @@ -</div> -<div id="cbip-<%=self.config.."-"..section.."-"..self.option%>"></div> -</td> - -<% if #self.deps > 0 then -%> - <script type="text/javascript"> - <% for j, d in ipairs(self.deps) do -%> - cbi_d_add("cbi-<%=self.config.."-"..section.."-"..self.option..d.add%>", { - <%- - for k,v in pairs(d.deps) do - -%> - <%-=string.format('"cbid.%s.%s.%s"', self.config, section, k) .. ":" .. string.format("%q", v)-%> - <%-if next(d.deps, k) then-%>,<%-end-%> - <%- - end - -%> - }, "cbip-<%=self.config.."-"..section.."-"..self.option%>"); - <%- end %> - </script> -<%- end %> diff --git a/libs/web/luasrc/view/cbi/cell_valueheader.htm b/libs/web/luasrc/view/cbi/cell_valueheader.htm deleted file mode 100644 index 9e2e145dd..000000000 --- a/libs/web/luasrc/view/cbi/cell_valueheader.htm +++ /dev/null @@ -1,2 +0,0 @@ -<td class="cbi-value-field<% if self.error and self.error[section] then %> cbi-value-error<% end %>"> -<div id="cbi-<%=self.config.."-"..section.."-"..self.option%>"> diff --git a/libs/web/luasrc/view/cbi/compound.htm b/libs/web/luasrc/view/cbi/compound.htm deleted file mode 100644 index 12d02bb1d..000000000 --- a/libs/web/luasrc/view/cbi/compound.htm +++ /dev/null @@ -1 +0,0 @@ -<%- self:render_children() %> diff --git a/libs/web/luasrc/view/cbi/delegator.htm b/libs/web/luasrc/view/cbi/delegator.htm deleted file mode 100644 index 4fd19265d..000000000 --- a/libs/web/luasrc/view/cbi/delegator.htm +++ /dev/null @@ -1,24 +0,0 @@ -<%- self.active:render() %> - <div class="cbi-page-actions"> - <input type="hidden" name="cbi.delg.current" value="<%=self.current%>" /> -<% for _, x in ipairs(self.chain) do %> - <input type="hidden" name="cbi.delg.path" value="<%=x%>" /> -<% end %> -<% if not self.disallow_pageactions then %> -<% if self.allow_finish and not self:get_next(self.current) then %> - <input class="cbi-button cbi-button-finish" type="submit" value="<%:Finish%>" /> -<% elseif self:get_next(self.current) then %> - <input class="cbi-button cbi-button-next" type="submit" value="<%:Next »%>" /> -<% end %> -<% if self.allow_cancel then %> - <input class="cbi-button cbi-button-cancel" type="submit" name="cbi.cancel" value="<%:Cancel%>" /> -<% end %> -<% if self.allow_reset then %> - <input class="cbi-button cbi-button-reset" type="reset" value="<%:Reset%>" /> -<% end %> -<% if self.allow_back and self:get_prev(self.current) then %> - <input class="cbi-button cbi-button-back" type="submit" name="cbi.delg.back" value="<%:« Back%>" /> -<% end %> -<% end %> - <script type="text/javascript">cbi_d_update();</script> - </div> diff --git a/libs/web/luasrc/view/cbi/dvalue.htm b/libs/web/luasrc/view/cbi/dvalue.htm deleted file mode 100644 index 78e6f323d..000000000 --- a/libs/web/luasrc/view/cbi/dvalue.htm +++ /dev/null @@ -1,13 +0,0 @@ -<%+cbi/valueheader%> -<% if self.href then %><a href="<%=self.href%>"><% end -%> - <% - local val = self:cfgvalue(section) or self.default or "" - if not self.rawhtml then - write(pcdata(val)) - else - write(val) - end - %> -<%- if self.href then %></a><%end%> -<input type="hidden" id="<%=cbid%>" value="<%=pcdata(self:cfgvalue(section) or self.default or "")%>" /> -<%+cbi/valuefooter%> diff --git a/libs/web/luasrc/view/cbi/dynlist.htm b/libs/web/luasrc/view/cbi/dynlist.htm deleted file mode 100644 index fd626a4ec..000000000 --- a/libs/web/luasrc/view/cbi/dynlist.htm +++ /dev/null @@ -1,26 +0,0 @@ -<%+cbi/valueheader%> -<div> -<% - local vals = self:cfgvalue(section) or {} - for i=1, #vals + 1 do - local val = vals[i] - if (val and #val > 0) or (i == 1) then -%> - <input class="cbi-input-text" value="<%=pcdata(val)%>" onchange="cbi_d_update(this.id)" type="text"<%= - attr("id", cbid .. "." .. i) .. attr("name", cbid) .. ifattr(self.size, "size") .. - ifattr(i == 1 and self.placeholder, "placeholder", self.placeholder) - %> /><br /> -<% end end %> -</div> -<script type="text/javascript"> -cbi_dynlist_init( - '<%=cbid%>', '<%=resource%>', '<%=self.datatype%>', - <%=tostring(self.optional or self.rmempty)%> - <%- if #self.keylist > 0 then -%>, [{ - <%- for i, k in ipairs(self.keylist) do -%> - <%-=string.format("%q", k) .. ":" .. string.format("%q", self.vallist[i])-%> - <%-if i<#self.keylist then-%>,<%-end-%> - <%- end -%> - }, '<%: -- custom -- %>']<% end -%>); -</script> -<%+cbi/valuefooter%> diff --git a/libs/web/luasrc/view/cbi/filebrowser.htm b/libs/web/luasrc/view/cbi/filebrowser.htm deleted file mode 100644 index a79beebba..000000000 --- a/libs/web/luasrc/view/cbi/filebrowser.htm +++ /dev/null @@ -1,108 +0,0 @@ -<?xml version="1.0" encoding="UTF-8"?> -<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> -<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> -<head> - <title>Filebrowser - LuCI</title> - <style type="text/css"> - #path, #listing { - font-size: 85%; - } - - ul { - padding-left: 0; - list-style-type: none; - } - - li img { - vertical-align: bottom; - margin-right: 0.2em; - } - </style> - - <script type="text/javascript"> - function callback(path) { - if( window.opener ) { - var input = window.opener.document.getElementById('<%=luci.http.formvalue('field')%>'); - if( input ) { - input.value = path; - window.close(); - } - } - } - </script> -</head> -<body> - <% - require("nixio.fs") - require("nixio.util") - require("luci.http") - require("luci.dispatcher") - - local field = luci.http.formvalue('field') - local request = luci.dispatcher.context.args - local path = { '' } - - for i = 1, #request do - if request[i] ~= '..' and #request[i] > 0 then - path[#path+1] = request[i] - end - end - - local filepath = table.concat( path, '/' ) - local filestat = nixio.fs.stat( filepath ) - local baseurl = luci.dispatcher.build_url('admin', 'filebrowser') - - if filestat and filestat.type == "reg" then - table.remove( path, #path ) - filepath = table.concat( path, '/' ) .. '/' - elseif not ( filestat and filestat.type == "dir" ) then - path = { '' } - filepath = '/' - else - filepath = filepath .. '/' - end - - local entries = nixio.util.consume((nixio.fs.dir(filepath))) - -%> - <div id="path"> - Location: - <% for i, dir in ipairs(path) do %> - <% if i == 1 then %> - <a href="<%=baseurl%>?field=<%=field%>">(root)</a> - <% elseif next(path, i) then %> - <% baseurl = baseurl .. '/' .. dir %> - / <a href="<%=baseurl%>?field=<%=field%>"><%=dir%></a> - <% else %> - <% baseurl = baseurl .. '/' .. dir %> - / <%=dir%> - <% end %> - <% end %> - </div> - - <hr /> - - <div id="listing"> - <ul> - <% for _, e in luci.util.vspairs(entries) do - local stat = nixio.fs.stat(filepath..e) - if stat and stat.type == 'dir' then - -%> - <li class="dir"> - <img src="<%=resource%>/cbi/folder.gif" alt="<%:Directory%>" /> - <a href="<%=baseurl%>/<%=e%>?field=<%=field%>"><%=e%>/</a> - </li> - <% end end -%> - - <% for _, e in luci.util.vspairs(entries) do - local stat = nixio.fs.stat(filepath..e) - if stat and stat.type ~= 'dir' then - -%> - <li class="file"> - <img src="<%=resource%>/cbi/file.gif" alt="<%:File%>" /> - <a href="#" onclick="callback('<%=filepath..e%>')"><%=e%></a> - </li> - <% end end -%> - </ul> - </div> -</body> -</html> diff --git a/libs/web/luasrc/view/cbi/firewall_zoneforwards.htm b/libs/web/luasrc/view/cbi/firewall_zoneforwards.htm deleted file mode 100644 index 2a433b569..000000000 --- a/libs/web/luasrc/view/cbi/firewall_zoneforwards.htm +++ /dev/null @@ -1,59 +0,0 @@ -<%+cbi/valueheader%> - -<%- - local utl = require "luci.util" - local fwm = require "luci.model.firewall".init() - local nwm = require "luci.model.network".init() - - local zone, fwd, fz - local value = self:formvalue(section) - if not value or value == "-" then - value = self:cfgvalue(section) or self.default - end - - local def = fwm:get_defaults() - local zone = fwm:get_zone(value) - local empty = true --%> - -<% if zone then %> -<div style="white-space:nowrap"> - <label class="zonebadge" style="background-color:<%=zone:get_color()%>"> - <strong><%=zone:name()%>:</strong> - <%- - local zempty = true - for _, net in ipairs(zone:get_networks()) do - net = nwm:get_network(net) - if net then - zempty = false - -%> - <span class="ifacebadge<% if net:name() == self.network then %> ifacebadge-active<% end %>"><%=net:name()%>: - <% - local nempty = true - for _, iface in ipairs(net:is_bridge() and net:get_interfaces() or { net:get_interface() }) do - nempty = false - %> - <img<%=attr("title", iface:get_i18n())%> style="width:16px; height:16px; vertical-align:middle" src="<%=resource%>/icons/<%=iface:type()%><%=iface:is_up() and "" or "_disabled"%>.png" /> - <% end %> - <% if nempty then %><em><%:(empty)%></em><% end %> - </span> - <%- end end -%> - <%- if zempty then %><em><%:(empty)%></em><% end -%> - </label> -  ⇒  - <% for _, fwd in ipairs(zone:get_forwardings_by("src")) do - fz = fwd:dest_zone() - empty = false %> - <label class="zonebadge" style="background-color:<%=fz:get_color()%>"> - <strong><%=fz:name()%></strong> - </label>  - <% end %> - <% if empty then %> - <label class="zonebadge zonebadge-empty"> - <strong><%=zone:forward():upper()%></strong> - </label> - <% end %> -</div> -<% end %> - -<%+cbi/valuefooter%> diff --git a/libs/web/luasrc/view/cbi/firewall_zonelist.htm b/libs/web/luasrc/view/cbi/firewall_zonelist.htm deleted file mode 100644 index 7973437f4..000000000 --- a/libs/web/luasrc/view/cbi/firewall_zonelist.htm +++ /dev/null @@ -1,89 +0,0 @@ -<%+cbi/valueheader%> - -<%- - local utl = require "luci.util" - local fwm = require "luci.model.firewall".init() - local nwm = require "luci.model.network".init() - - local zone, net, iface - local zones = fwm:get_zones() - local value = self:formvalue(section) - if not value or value == "-" then - value = self:cfgvalue(section) or self.default - end - - local selected = false - local checked = { } - - for value in utl.imatch(value) do - checked[value] = true - end - - if not next(checked) then - checked[""] = true - end --%> - -<ul style="margin:0; list-style-type:none; text-align:left"> - <% if self.allowlocal then %> - <li style="padding:0.5em"> - <input class="cbi-input-radio" onclick="cbi_d_update(this.id)" onchange="cbi_d_update(this.id)"<%=attr("type", self.widget or "radio") .. attr("id", cbid .. "_empty") .. attr("name", cbid) .. attr("value", "") .. ifattr(checked[""], "checked", "checked")%> />   - <label<%=attr("for", cbid .. "_empty")%> style="background-color:<%=fwm.zone.get_color()%>" class="zonebadge"> - <strong><%:Device%></strong> - <% if self.allowany and self.allowlocal then %>(<%:input%>)<% end %> - </label> - </li> - <% end %> - <% if self.allowany then %> - <li style="padding:0.5em"> - <input class="cbi-input-radio" onclick="cbi_d_update(this.id)" onchange="cbi_d_update(this.id)"<%=attr("type", self.widget or "radio") .. attr("id", cbid .. "_any") .. attr("name", cbid) .. attr("value", "*") .. ifattr(checked["*"], "checked", "checked")%> />   - <label<%=attr("for", cbid .. "_any")%> style="background-color:<%=fwm.zone.get_color()%>" class="zonebadge"> - <strong><%:Any zone%></strong> - <% if self.allowany and self.allowlocal then %>(<%:forward%>)<% end %> - </label> - </li> - <% end %> - <% - for _, zone in utl.spairs(zones, function(a,b) return (zones[a]:name() < zones[b]:name()) end) do - if zone:name() ~= self.exclude then - selected = selected or (value == zone:name()) - %> - <li style="padding:0.5em"> - <input class="cbi-input-radio" onclick="cbi_d_update(this.id)" onchange="cbi_d_update(this.id)"<%=attr("type", self.widget or "radio") .. attr("id", cbid .. "." .. zone:name()) .. attr("name", cbid) .. attr("value", zone:name()) .. ifattr(checked[zone:name()], "checked", "checked")%> />   - <label<%=attr("for", cbid .. "." .. zone:name())%> style="background-color:<%=zone:get_color()%>" class="zonebadge"> - <strong><%=zone:name()%>:</strong> - <% - local zempty = true - for _, net in ipairs(zone:get_networks()) do - net = nwm:get_network(net) - if net then - zempty = false - %> - <span class="ifacebadge<% if net:name() == self.network then %> ifacebadge-active<% end %>"><%=net:name()%>: - <% - local nempty = true - for _, iface in ipairs(net:is_bridge() and net:get_interfaces() or { net:get_interface() }) do - nempty = false - %> - <img<%=attr("title", iface:get_i18n())%> style="width:16px; height:16px; vertical-align:middle" src="<%=resource%>/icons/<%=iface:type()%><%=iface:is_up() and "" or "_disabled"%>.png" /> - <% end %> - <% if nempty then %><em><%:(empty)%></em><% end %> - </span> - <% end end %> - <% if zempty then %><em><%:(empty)%></em><% end %> - </label> - </li> - <% end end %> - - <% if self.widget ~= "checkbox" and not self.nocreate then %> - <li style="padding:0.5em"> - <input class="cbi-input-radio" onclick="cbi_d_update(this.id)" onchange="cbi_d_update(this.id)" type="radio"<%=attr("id", cbid .. "_new") .. attr("name", cbid) .. attr("value", "-") .. ifattr(not selected, "checked", "checked")%> />   - <div onclick="document.getElementById('<%=cbid%>_new').checked=true" class="zonebadge" style="background-color:<%=fwm.zone.get_color()%>"> - <em><%:unspecified -or- create:%> </em> - <input type="text"<%=attr("name", cbid .. ".newzone") .. ifattr(not selected, "value", luci.http.formvalue(cbid .. ".newzone") or self.default)%> onfocus="document.getElementById('<%=cbid%>_new').checked=true" /> - </div> - </li> - <% end %> -</ul> - -<%+cbi/valuefooter%> diff --git a/libs/web/luasrc/view/cbi/footer.htm b/libs/web/luasrc/view/cbi/footer.htm deleted file mode 100644 index 2c34028e5..000000000 --- a/libs/web/luasrc/view/cbi/footer.htm +++ /dev/null @@ -1,26 +0,0 @@ - <%- if pageaction then -%> - <div class="cbi-page-actions"> - <% if redirect then %> - <div style="float:left"> - <input class="cbi-button cbi-button-link" type="button" value="<%:Back to Overview%>" onclick="location.href='<%=pcdata(redirect)%>'" /> - </div> - <% end %> - - <% if flow.skip then %> - <input class="cbi-button cbi-button-skip" type="submit" name="cbi.skip" value="<%:Skip%>" /> - <% end %> - <% if not autoapply and not flow.hideapplybtn then %> - <input class="cbi-button cbi-button-apply" type="submit" name="cbi.apply" value="<%:Save & Apply%>" /> - <% end %> - <% if not flow.hidesavebtn then %> - <input class="cbi-button cbi-button-save" type="submit" value="<%:Save%>" /> - <% end %> - <% if not flow.hideresetbtn then %> - <input class="cbi-button cbi-button-reset" type="reset" value="<%:Reset%>" /> - <% end %> - - <script type="text/javascript">cbi_d_update();</script> - </div> - <%- end -%> -</form> -<%+footer%> diff --git a/libs/web/luasrc/view/cbi/full_valuefooter.htm b/libs/web/luasrc/view/cbi/full_valuefooter.htm deleted file mode 100644 index 4876fbcc9..000000000 --- a/libs/web/luasrc/view/cbi/full_valuefooter.htm +++ /dev/null @@ -1,59 +0,0 @@ - <% if self.description and #self.description > 0 then -%> - <% if not luci.util.instanceof(self, luci.cbi.DynamicList) and (not luci.util.instanceof(self, luci.cbi.Flag) or self.orientation == "horizontal") then -%> - <br /> - <%- end %> - <div class="cbi-value-description"> - <span class="cbi-value-helpicon"><img src="<%=resource%>/cbi/help.gif" alt="<%:help%>" /></span> - <%=self.description%> - </div> - <%- end %> - <%- if self.title and #self.title > 0 then -%> - </div> - <%- end -%> -</div> - - -<% if #self.deps > 0 or #self.subdeps > 0 then -%> - <script type="text/javascript" id="cbip-<%=self.config.."-"..section.."-"..self.option%>"> - <% for j, d in ipairs(self.subdeps) do -%> - cbi_d_add("cbi-<%=self.config.."-"..section.."-"..self.option..d.add%>", { - <%- - for k,v in pairs(d.deps) do - local depk - if k:find("!", 1, true) then - depk = string.format('"%s"', k) - elseif k:find(".", 1, true) then - depk = string.format('"cbid.%s"', k) - else - depk = string.format('"cbid.%s.%s.%s"', self.config, section, k) - end - -%> - <%-= depk .. ":" .. string.format("%q", v)-%> - <%-if next(d.deps, k) then-%>,<%-end-%> - <%- - end - -%> - }, "cbip-<%=self.config.."-"..section.."-"..self.option..d.add%>"); - <%- end %> - <% for j, d in ipairs(self.deps) do -%> - cbi_d_add("cbi-<%=self.config.."-"..section.."-"..self.option..d.add%>", { - <%- - for k,v in pairs(d.deps) do - local depk - if k:find("!", 1, true) then - depk = string.format('"%s"', k) - elseif k:find(".", 1, true) then - depk = string.format('"cbid.%s"', k) - else - depk = string.format('"cbid.%s.%s.%s"', self.config, section, k) - end - -%> - <%-= depk .. ":" .. string.format("%q", v)-%> - <%-if next(d.deps, k) then-%>,<%-end-%> - <%- - end - -%> - }, "cbip-<%=self.config.."-"..section.."-"..self.option..d.add%>"); - <%- end %> - </script> -<%- end %> diff --git a/libs/web/luasrc/view/cbi/full_valueheader.htm b/libs/web/luasrc/view/cbi/full_valueheader.htm deleted file mode 100644 index aaf085473..000000000 --- a/libs/web/luasrc/view/cbi/full_valueheader.htm +++ /dev/null @@ -1,9 +0,0 @@ -<div class="cbi-value<% if self.error and self.error[section] then %> cbi-value-error<% end %><% if self.last_child then %> cbi-value-last<% end %>" id="cbi-<%=self.config.."-"..section.."-"..self.option%>"> - <%- if self.title and #self.title > 0 then -%> - <label class="cbi-value-title"<%= attr("for", cbid) %>> - <%- if self.titleref then -%><a title="<%=self.titledesc or translate('Go to relevant configuration page')%>" class="cbi-title-ref" href="<%=self.titleref%>"><%- end -%> - <%-=self.title-%> - <%- if self.titleref then -%></a><%- end -%> - </label> - <div class="cbi-value-field"> - <%- end -%> diff --git a/libs/web/luasrc/view/cbi/fvalue.htm b/libs/web/luasrc/view/cbi/fvalue.htm deleted file mode 100644 index a1e0808e8..000000000 --- a/libs/web/luasrc/view/cbi/fvalue.htm +++ /dev/null @@ -1,9 +0,0 @@ -<%+cbi/valueheader%> - <input type="hidden" value="1"<%= - attr("name", "cbi.cbe." .. self.config .. "." .. section .. "." .. self.option) - %> /> - <input class="cbi-input-checkbox" onclick="cbi_d_update(this.id)" onchange="cbi_d_update(this.id)" type="checkbox"<%= - attr("id", cbid) .. attr("name", cbid) .. attr("value", self.enabled or 1) .. - ifattr((self:cfgvalue(section) or self.default) == self.enabled, "checked", "checked") - %> /> -<%+cbi/valuefooter%> diff --git a/libs/web/luasrc/view/cbi/header.htm b/libs/web/luasrc/view/cbi/header.htm deleted file mode 100644 index 2bddaba61..000000000 --- a/libs/web/luasrc/view/cbi/header.htm +++ /dev/null @@ -1,7 +0,0 @@ -<%+header%> -<form method="post" name="cbi" action="<%=REQUEST_URI%>" enctype="multipart/form-data" onreset="return cbi_validate_reset(this)" onsubmit="return cbi_validate_form(this, '<%:Some fields are invalid, cannot save values!%>')"> - <div> - <script type="text/javascript" src="<%=resource%>/cbi.js"></script> - <input type="hidden" name="cbi.submit" value="1" /> - <input type="submit" value="<%:Save%>" class="hidden" /> - </div> diff --git a/libs/web/luasrc/view/cbi/lvalue.htm b/libs/web/luasrc/view/cbi/lvalue.htm deleted file mode 100644 index 8cc086db4..000000000 --- a/libs/web/luasrc/view/cbi/lvalue.htm +++ /dev/null @@ -1,18 +0,0 @@ -<%+cbi/valueheader%> -<% if self.widget == "select" then %> - <select class="cbi-input-select" onchange="cbi_d_update(this.id)"<%= attr("id", cbid) .. attr("name", cbid) .. ifattr(self.size, "size") %>> - <% for i, key in pairs(self.keylist) do -%> - <option id="cbi-<%=self.config.."-"..section.."-"..self.option.."-"..key%>"<%= attr("value", key) .. ifattr(tostring(self:cfgvalue(section) or self.default) == key, "selected", "selected") %>><%=striptags(self.vallist[i])%></option> - <%- end %> - </select> -<% elseif self.widget == "radio" then - local c = 0 - for i, key in pairs(self.keylist) do - c = c + 1 -%> - <input class="cbi-input-radio" onclick="cbi_d_update(this.id)" onchange="cbi_d_update(this.id)" type="radio"<%= attr("id", cbid..c) .. attr("name", cbid) .. attr("value", key) .. ifattr((self:cfgvalue(section) or self.default) == key, "checked", "checked") %> /> - <label<%= attr("for", cbid..c) %>><%=self.vallist[i]%></label> -<% if c == self.size then c = 0 %><% if self.orientation == "horizontal" then %> <% else %><br /><% end %> -<% end end %> -<% end %> -<%+cbi/valuefooter%> diff --git a/libs/web/luasrc/view/cbi/map.htm b/libs/web/luasrc/view/cbi/map.htm deleted file mode 100644 index 053220d18..000000000 --- a/libs/web/luasrc/view/cbi/map.htm +++ /dev/null @@ -1,13 +0,0 @@ -<%- if firstmap and messages then local msg; for _, msg in ipairs(messages) do -%> - <div class="errorbox"><%=pcdata(msg)%></div> -<%- end end -%> - -<%-+cbi/apply_xhr-%> - -<div class="cbi-map" id="cbi-<%=self.config%>"> - <% if self.title and #self.title > 0 then %><h2><a id="content" name="content"><%=self.title%></a></h2><% end %> - <% if self.description and #self.description > 0 then %><div class="cbi-map-descr"><%=self.description%></div><% end %> - <%- if firstmap and applymap then cbi_apply_xhr(self.config, parsechain, redirect) end -%> - <%- self:render_children() %> - <br /> -</div> diff --git a/libs/web/luasrc/view/cbi/mvalue.htm b/libs/web/luasrc/view/cbi/mvalue.htm deleted file mode 100644 index 6a0b3881d..000000000 --- a/libs/web/luasrc/view/cbi/mvalue.htm +++ /dev/null @@ -1,19 +0,0 @@ -<% local v = self:valuelist(section) or {} -%> -<%+cbi/valueheader%> -<% if self.widget == "select" then %> - <select class="cbi-input-select" multiple="multiple" onclick="cbi_d_update(this.id)" onchange="cbi_d_update(this.id)"<%= attr("name", cbid) .. ifattr(self.size, "size") %>> - <% for i, key in pairs(self.keylist) do -%> - <option<%= attr("value", key) .. ifattr(luci.util.contains(v, key), "selected", "selected") %>><%=striptags(self.vallist[i])%></option> - <%- end %> - </select> -<% elseif self.widget == "checkbox" then - local c = 0; - for i, key in pairs(self.keylist) do - c = c + 1 -%> - <input class="cbi-input-checkbox" type="checkbox" onclick="cbi_d_update(this.id)" onchange="cbi_d_update(this.id)"<%= attr("id", cbid..c) .. attr("name", cbid) .. attr("value", key) .. ifattr(luci.util.contains(v, key), "checked", "checked") %> /> - <label<%= attr("for", cbid..c) %>><%=self.vallist[i]%></label><br /> -<% if c == self.size then c = 0 %><br /> -<% end end %> -<% end %> -<%+cbi/valuefooter%> diff --git a/libs/web/luasrc/view/cbi/network_ifacelist.htm b/libs/web/luasrc/view/cbi/network_ifacelist.htm deleted file mode 100644 index 643d849a5..000000000 --- a/libs/web/luasrc/view/cbi/network_ifacelist.htm +++ /dev/null @@ -1,81 +0,0 @@ -<%+cbi/valueheader%> - -<%- - local utl = require "luci.util" - local net = require "luci.model.network".init() - local cbeid = luci.cbi.FEXIST_PREFIX .. self.config .. "." .. section .. "." .. self.option - - local iface - local ifaces = net:get_interfaces() - local value - - if self.map:formvalue(cbeid) == "1" then - value = self:formvalue(section) or self.default or "" - else - value = self:cfgvalue(section) or self.default - end - - local checked = { } - - if value then - for value in utl.imatch(value) do - checked[value] = true - end - else - local n = self.network and net:get_network(self.network) - if n then - local i - for _, i in ipairs(n:get_interfaces() or { n:get_interface() }) do - checked[i:name()] = true - end - end - end --%> - -<input type="hidden" name="<%=cbeid%>" value="1" /> -<ul style="margin:0; list-style-type:none"> - <% for _, iface in ipairs(ifaces) do - local link = iface:adminlink() - if (not self.nobridges or not iface:is_bridge()) and - (not self.noinactive or iface:is_up()) and - iface:name() ~= self.exclude - then %> - <li> - <input class="cbi-input-<%=self.widget or "radio"%>" onclick="cbi_d_update(this.id)" onchange="cbi_d_update(this.id)"<%= - attr("type", self.widget or "radio") .. - attr("id", cbid .. "." .. iface:name()) .. - attr("name", cbid) .. attr("value", iface:name()) .. - ifattr(checked[iface:name()], "checked", "checked") - %> />   - <label<%=attr("for", cbid .. "." .. iface:name())%>> - <% if link then -%><a href="<%=link%>"><% end -%> - <img<%=attr("title", iface:get_i18n())%> style="width:16px; height:16px; vertical-align:middle" src="<%=resource%>/icons/<%=iface:type()%><%=iface:is_up() and "" or "_disabled"%>.png" /> - <% if link then -%></a><% end -%> - <%=pcdata(iface:get_i18n())%> - <% local ns = iface:get_networks(); if #ns > 0 then %>( - <%- local i, n; for i, n in ipairs(ns) do -%> - <%-= (i>1) and ', ' -%> - <a href="<%=n:adminlink()%>"><%=n:name()%></a> - <%- end -%> - )<% end %> - </label> - </li> - <% end end %> - <% if not self.nocreate then %> - <li> - <input class="cbi-input-<%=self.widget or "radio"%>" onclick="cbi_d_update(this.id)" onchange="cbi_d_update(this.id)"<%= - attr("type", self.widget or "radio") .. - attr("id", cbid .. "_custom") .. - attr("name", cbid) .. - attr("value", " ") - %> />   - <label<%=attr("for", cbid .. "_custom")%>> - <img title="<%:Custom Interface%>" style="width:16px; height:16px; vertical-align:middle" src="<%=resource%>/icons/ethernet_disabled.png" /> - <%:Custom Interface%>: - </label> - <input type="text" style="width:50px" onfocus="document.getElementById('<%=cbid%>_custom').checked=true" onblur="var x=document.getElementById('<%=cbid%>_custom'); x.value=this.value; x.checked=true" /> - </li> - <% end %> -</ul> - -<%+cbi/valuefooter%> diff --git a/libs/web/luasrc/view/cbi/network_netinfo.htm b/libs/web/luasrc/view/cbi/network_netinfo.htm deleted file mode 100644 index 4fd84112a..000000000 --- a/libs/web/luasrc/view/cbi/network_netinfo.htm +++ /dev/null @@ -1,27 +0,0 @@ -<%+cbi/valueheader%> - -<%- - local value = self:formvalue(section) - if not value or value == "-" then - value = self:cfgvalue(section) or self.default - end - - local nwm = require "luci.model.network".init() - local net = nwm:get_network(value) --%> - -<% if net then %> -<span class="ifacebadge"><%=net:name()%>: - <% - local empty = true - for _, iface in ipairs(net:get_interfaces() or { net:get_interface() }) do - if not iface:is_bridge() then - empty = false - %> - <img<%=attr("title", iface:get_i18n())%> style="width:16px; height:16px; vertical-align:middle" src="<%=resource%>/icons/<%=iface:type()%><%=iface:is_up() and "" or "_disabled"%>.png" /> - <% end end %> - <% if empty then %><em><%:(no interfaces attached)%></em><% end %> -</span> -<% end %> - -<%+cbi/valuefooter%> diff --git a/libs/web/luasrc/view/cbi/network_netlist.htm b/libs/web/luasrc/view/cbi/network_netlist.htm deleted file mode 100644 index 7e23d149a..000000000 --- a/libs/web/luasrc/view/cbi/network_netlist.htm +++ /dev/null @@ -1,81 +0,0 @@ -<%+cbi/valueheader%> - -<%- - local utl = require "luci.util" - local nwm = require "luci.model.network".init() - - local net, iface - local networks = nwm:get_networks() - local value = self:formvalue(section) - - self.cast = nil - - if not value or value == "-" then - value = self:cfgvalue(section) or self.default - end - - local checked = { } - for value in utl.imatch(value) do - checked[value] = true - end --%> - -<ul style="margin:0; list-style-type:none; text-align:left"> - <% for _, net in ipairs(networks) do - if (net:name() ~= "loopback") and - (net:name() ~= self.exclude) and - (not self.novirtual or not net:is_virtual()) - then %> - <li style="padding:0.25em 0"> - <input class="cbi-input-<%=self.widget or "radio"%>" onclick="cbi_d_update(this.id)" onchange="cbi_d_update(this.id)"<%= - attr("type", self.widget or "radio") .. - attr("id", cbid .. "." .. net:name()) .. - attr("name", cbid) .. attr("value", net:name()) .. - ifattr(checked[net:name()], "checked", "checked") - %> />   - <label<%=attr("for", cbid .. "." .. net:name())%>> - <span class="ifacebadge"><%=net:name()%>: - <% - local empty = true - for _, iface in ipairs(net:is_bridge() and net:get_interfaces() or { net:get_interface() }) do - if not iface:is_bridge() then - empty = false - %> - <img<%=attr("title", iface:get_i18n())%> style="width:16px; height:16px; vertical-align:middle" src="<%=resource%>/icons/<%=iface:type()%><%=iface:is_up() and "" or "_disabled"%>.png" /> - <% end end %> - <% if empty then %><em><%:(no interfaces attached)%></em><% end %> - </span> - </label> - </li> - <% end end %> - - <% if not self.nocreate then %> - <li style="padding:0.25em 0"> - <input class="cbi-input-<%=self.widget or "radio"%>" onclick="cbi_d_update(this.id)" onchange="cbi_d_update(this.id)"<%=attr("type", self.widget or "radio") .. attr("id", cbid .. "_new") .. attr("name", cbid) .. attr("value", "-") .. ifattr(not value and self.widget ~= "checkbox", "checked", "checked")%> />   - <div style="padding:0.5em; display:inline"> - <label<%=attr("for", cbid .. "_new")%>><em> - <%- if self.widget == "checkbox" then -%> - <%:create:%> - <%- else -%> - <%:unspecified -or- create:%> - <%- end -%> </em></label> - <input style="width:6em" type="text"<%=attr("name", cbid .. ".newnet")%> onfocus="document.getElementById('<%=cbid%>_new').checked=true" /> - </div> - </li> - <% elseif self.widget ~= "checkbox" and self.unspecified then %> - <li style="padding:0.25em 0"> - <input class="cbi-input-<%=self.widget or "radio"%>" onclick="cbi_d_update(this.id)" onchange="cbi_d_update(this.id)"<%= - attr("type", self.widget or "radio") .. - attr("id", cbid .. "_uns") .. - attr("name", cbid) .. - attr("value", "") .. - ifattr(not value or #value == 0, "checked", "checked") - %> />   - <div style="padding:0.5em; display:inline"> - <label<%=attr("for", cbid .. "_uns")%>><em><%:unspecified%></em></label> - </div> - </li> - <% end %> -</ul> - -<%+cbi/valuefooter%> diff --git a/libs/web/luasrc/view/cbi/nsection.htm b/libs/web/luasrc/view/cbi/nsection.htm deleted file mode 100644 index 95e765882..000000000 --- a/libs/web/luasrc/view/cbi/nsection.htm +++ /dev/null @@ -1,31 +0,0 @@ -<% if self:cfgvalue(self.section) then section = self.section %> - <fieldset class="cbi-section" id="cbi-<%=self.config%>-<%=section%>"> - <% if self.title and #self.title > 0 then -%> - <legend><%=self.title%></legend> - <%- end %> - <% if self.description and #self.description > 0 then -%> - <div class="cbi-section-descr"><%=self.description%></div> - <%- end %> - <% if self.addremove then -%> - <div class="cbi-section-remove right"> - <input type="submit" name="cbi.rns.<%=self.config%>.<%=section%>" value="<%:Delete%>" /> - </div> - <%- end %> - <%+cbi/tabmenu%> - <div class="cbi-section-node<% if self.tabs then %> cbi-section-node-tabbed<% end %>" id="cbi-<%=self.config%>-<%=section%>"> - <%+cbi/ucisection%> - </div> - <br /> - </fieldset> -<% elseif self.addremove then %> - <% if self.template_addremove then include(self.template_addremove) else -%> - <fieldset class="cbi-section" id="cbi-<%=self.config%>-<%=self.section%>"> - <% if self.title and #self.title > 0 then -%> - <legend><%=self.title%></legend> - <%- end %> - <div class="cbi-section-descr"><%=self.description%></div> - <input type="submit" class="cbi-button-add" name="cbi.cns.<%=self.config%>.<%=self.section%>" value="<%:Add%>" /> - </fieldset> - <%- end %> -<% end %> -<!-- /nsection --> diff --git a/libs/web/luasrc/view/cbi/nullsection.htm b/libs/web/luasrc/view/cbi/nullsection.htm deleted file mode 100644 index bd4895095..000000000 --- a/libs/web/luasrc/view/cbi/nullsection.htm +++ /dev/null @@ -1,38 +0,0 @@ -<fieldset class="cbi-section"> - <% if self.title and #self.title > 0 then -%> - <legend><%=self.title%></legend> - <%- end %> - <% if self.description and #self.description > 0 then -%> - <div class="cbi-section-descr"><%=self.description%></div> - <%- end %> - <div class="cbi-section-node" id="cbi-<%=self.config%>-<%=tostring(self):sub(8)%>"> - <div> - <% self:render_children(1, scope or {}) %> - </div> - <% if self.error and self.error[1] then -%> - <div class="cbi-section-error"> - <ul><% for _, e in ipairs(self.error[1]) do -%> - <li> - <%- if e == "invalid" then -%> - <%:One or more fields contain invalid values!%> - <%- elseif e == "missing" then -%> - <%:One or more required fields have no value!%> - <%- else -%> - <%=pcdata(e)%> - <%- end -%> - </li> - <%- end %></ul> - </div> - <%- end %> - </div> - <br /> -</fieldset> -<%- - if type(self.hidden) == "table" then - for k, v in pairs(self.hidden) do --%> - <input type="hidden" id="<%=k%>" name="<%=k%>" value="<%=pcdata(v)%>" /> -<%- - end - end -%> diff --git a/libs/web/luasrc/view/cbi/simpleform.htm b/libs/web/luasrc/view/cbi/simpleform.htm deleted file mode 100644 index 5216cd50f..000000000 --- a/libs/web/luasrc/view/cbi/simpleform.htm +++ /dev/null @@ -1,57 +0,0 @@ -<% if not self.embedded then %> -<form method="post" enctype="multipart/form-data" action="<%=REQUEST_URI%>"> - <div> - <script type="text/javascript" src="<%=resource%>/cbi.js"></script> - <input type="hidden" name="cbi.submit" value="1" /> - </div> -<% end %> - <div class="cbi-map" id="cbi-<%=self.config%>"> - <% if self.title and #self.title > 0 then %><h2><a id="content" name="content"><%=self.title%></a></h2><% end %> - <% if self.description and #self.description > 0 then %><div class="cbi-map-descr"><%=self.description%></div><% end %> - <% self:render_children() %> - <br /> - </div> -<%- if self.message then %> - <div><%=self.message%></div> -<%- end %> -<%- if self.errmessage then %> - <div class="error"><%=self.errmessage%></div> -<%- end %> -<% if not self.embedded then %> - <div class="cbi-page-actions"> -<%- - if type(self.hidden) == "table" then - for k, v in pairs(self.hidden) do --%> - <input type="hidden" id="<%=k%>" name="<%=k%>" value="<%=pcdata(v)%>" /> -<%- - end - end -%> -<% if redirect then %> - <div style="float:left"> - <input class="cbi-button cbi-button-link" type="button" value="<%:Back to Overview%>" onclick="location.href='<%=pcdata(redirect)%>'" /> - </div> -<% end %> -<%- if self.flow and self.flow.skip then %> - <input class="cbi-button cbi-button-skip" type="submit" name="cbi.skip" value="<%:Skip%>" /> -<% end %> -<%- if self.submit ~= false then %> - <input class="cbi-button cbi-button-save" type="submit" value=" - <%- if not self.submit then -%><%-:Submit-%><%-else-%><%=self.submit%><%end-%> - " /> -<% end %> -<%- if self.reset ~= false then %> - <input class="cbi-button cbi-button-reset" type="reset" value=" - <%- if not self.reset then -%><%-:Reset-%><%-else-%><%=self.reset%><%end-%> - " /> -<% end %> -<%- if self.cancel ~= false and self.on_cancel then %> - <input class="cbi-button cbi-button-reset" type="submit" name="cbi.cancel" value=" - <%- if not self.cancel then -%><%-:Cancel-%><%-else-%><%=self.cancel%><%end-%> - " /> -<% end %> - <script type="text/javascript">cbi_d_update();</script> - </div> -</form> -<% end %> diff --git a/libs/web/luasrc/view/cbi/tabcontainer.htm b/libs/web/luasrc/view/cbi/tabcontainer.htm deleted file mode 100644 index 38c435d6a..000000000 --- a/libs/web/luasrc/view/cbi/tabcontainer.htm +++ /dev/null @@ -1,7 +0,0 @@ -<% for tab, data in pairs(self.tabs) do %> - <div class="cbi-tabcontainer" id="container.<%=self.config%>.<%=section%>.<%=tab%>"<% if tab ~= self.selected_tab then %> style="display:none"<% end %>> - <% if data.description then %><div class="cbi-tab-descr"><%=data.description%></div><% end %> - <% self:render_tab(tab, section, scope or {}) %> - </div> - <script type="text/javascript">cbi_t_add('<%=self.config%>.<%=section%>', '<%=tab%>')</script> -<% end %> diff --git a/libs/web/luasrc/view/cbi/tabmenu.htm b/libs/web/luasrc/view/cbi/tabmenu.htm deleted file mode 100644 index b96ac9ce4..000000000 --- a/libs/web/luasrc/view/cbi/tabmenu.htm +++ /dev/null @@ -1,13 +0,0 @@ -<%- if self.tabs then %> - <ul class="cbi-tabmenu"> - <%- self.selected_tab = luci.http.formvalue("tab." .. self.config .. "." .. section) %> - <%- for _, tab in ipairs(self.tab_names) do if #self.tabs[tab].childs > 0 then %> - <script type="text/javascript">cbi_c['container.<%=self.config%>.<%=section%>.<%=tab%>'] = <%=#self.tabs[tab].childs%>;</script> - <%- if not self.selected_tab then self.selected_tab = tab end %> - <li id="tab.<%=self.config%>.<%=section%>.<%=tab%>" class="cbi-tab<%=(tab == self.selected_tab) and '' or '-disabled'%>"> - <a onclick="this.blur(); return cbi_t_switch('<%=self.config%>.<%=section%>', '<%=tab%>')" href="<%=REQUEST_URI%>?tab.<%=self.config%>.<%=section%>=<%=tab%>"><%=self.tabs[tab].title%></a> - <% if tab == self.selected_tab then %><input type="hidden" id="tab.<%=self.config%>.<%=section%>" name="tab.<%=self.config%>.<%=section%>" value="<%=tab%>" /><% end %> - </li> - <% end end -%> - </ul> -<% end -%> diff --git a/libs/web/luasrc/view/cbi/tblsection.htm b/libs/web/luasrc/view/cbi/tblsection.htm deleted file mode 100644 index d92879116..000000000 --- a/libs/web/luasrc/view/cbi/tblsection.htm +++ /dev/null @@ -1,146 +0,0 @@ -<%- -local rowcnt = 1 -function rowstyle() - rowcnt = rowcnt + 1 - return (rowcnt % 2) + 1 -end - -function width(o) - if o.width then - if type(o.width) == 'number' then - return ' style="width:%dpx"' % o.width - end - return ' style="width:%s"' % o.width - end - return '' -end --%> - -<!-- tblsection --> -<fieldset class="cbi-section" id="cbi-<%=self.config%>-<%=self.sectiontype%>"> - <% if self.title and #self.title > 0 then -%> - <legend><%=self.title%></legend> - <%- end %> - <%- if self.sortable then -%> - <input type="hidden" id="cbi.sts.<%=self.config%>.<%=self.sectiontype%>" name="cbi.sts.<%=self.config%>.<%=self.sectiontype%>" value="" /> - <%- end -%> - <div class="cbi-section-descr"><%=self.description%></div> - <div class="cbi-section-node"> - <%- local count = 0 -%> - <table class="cbi-section-table"> - <tr class="cbi-section-table-titles"> - <%- if not self.anonymous then -%> - <%- if self.sectionhead then -%> - <th class="cbi-section-table-cell"><%=self.sectionhead%></th> - <%- else -%> - <th> </th> - <%- end -%> - <%- end -%> - <%- for i, k in pairs(self.children) do if not k.optional then -%> - <th class="cbi-section-table-cell"<%=width(k)%>> - <%- if k.titleref then -%><a title="<%=self.titledesc or translate('Go to relevant configuration page')%>" class="cbi-title-ref" href="<%=k.titleref%>"><%- end -%> - <%-=k.title-%> - <%- if k.titleref then -%></a><%- end -%> - </th> - <%- count = count + 1; end; end; if self.sortable then -%> - <th class="cbi-section-table-cell"><%:Sort%></th> - <%- end; if self.extedit or self.addremove then -%> - <th class="cbi-section-table-cell"> </th> - <%- count = count + 1; end -%> - </tr> - <tr class="cbi-section-table-descr"> - <%- if not self.anonymous then -%> - <%- if self.sectiondesc then -%> - <th class="cbi-section-table-cell"><%=self.sectiondesc%></th> - <%- else -%> - <th></th> - <%- end -%> - <%- end -%> - <%- for i, k in pairs(self.children) do if not k.optional then -%> - <th class="cbi-section-table-cell"<%=width(k)%>><%=k.description%></th> - <%- end; end; if self.sortable then -%> - <th class="cbi-section-table-cell"></th> - <%- end; if self.extedit or self.addremove then -%> - <th class="cbi-section-table-cell"></th> - <%- end -%> - </tr> - <%- local isempty = true - for i, k in ipairs(self:cfgsections()) do - section = k - isempty = false - scope = { valueheader = "cbi/cell_valueheader", valuefooter = "cbi/cell_valuefooter" } - -%> - <tr class="cbi-section-table-row<% if self.extedit or self.rowcolors then %> cbi-rowstyle-<%=rowstyle()%><% end %>" id="cbi-<%=self.config%>-<%=section%>"> - <% if not self.anonymous then -%> - <th><h3><%=(type(self.sectiontitle) == "function") and self:sectiontitle(section) or k%></h3></th> - <%- end %> - - - <%- - for k, node in ipairs(self.children) do - if not node.optional then - node:render(section, scope or {}) - end - end - -%> - - <%- if self.sortable then -%> - <td class="cbi-section-table-cell"> - <input class="cbi-button cbi-button-up" type="button" value="" onclick="return cbi_row_swap(this, true, 'cbi.sts.<%=self.config%>.<%=self.sectiontype%>')" alt="<%:Move up%>" title="<%:Move up%>" /> - <input class="cbi-button cbi-button-down" type="button" value="" onclick="return cbi_row_swap(this, false, 'cbi.sts.<%=self.config%>.<%=self.sectiontype%>')" alt="<%:Move down%>" title="<%:Move down%>" /> - </td> - <%- end -%> - - <%- if self.extedit or self.addremove then -%> - <td class="cbi-section-table-cell"> - <%- if self.extedit then -%> - <input class="cbi-button cbi-button-edit" type="button" value="<%:Edit%>" - <%- if type(self.extedit) == "string" then - %> onclick="location.href='<%=self.extedit:format(section)%>'" - <%- elseif type(self.extedit) == "function" then - %> onclick="location.href='<%=self:extedit(section)%>'" - <%- end - %> alt="<%:Edit%>" title="<%:Edit%>" /> - <%- end; if self.addremove then %> - <input class="cbi-button cbi-button-remove" type="submit" value="<%:Delete%>" onclick="this.form.cbi_state='del-section'; return true" name="cbi.rts.<%=self.config%>.<%=k%>" alt="<%:Delete%>" title="<%:Delete%>" /> - <%- end -%> - </td> - <%- end -%> - </tr> - <%- end -%> - - <%- if isempty then -%> - <tr class="cbi-section-table-row"> - <td colspan="<%=count%>"><em><br /><%:This section contains no values yet%></em></td> - </tr> - <%- end -%> - </table> - - <% if self.error then %> - <div class="cbi-section-error"> - <ul><% for _, c in pairs(self.error) do for _, e in ipairs(c) do -%> - <li><%=pcdata(e):gsub("\n","<br />")%></li> - <%- end end %></ul> - </div> - <% end %> - - <%- if self.addremove then -%> - <% if self.template_addremove then include(self.template_addremove) else -%> - <div class="cbi-section-create cbi-tblsection-create"> - <% if self.anonymous then %> - <input class="cbi-button cbi-button-add" type="submit" value="<%:Add%>" name="cbi.cts.<%=self.config%>.<%=self.sectiontype%>.<%=section%>" title="<%:Add%>" /> - <% else %> - <% if self.invalid_cts then -%><div class="cbi-section-error"><% end %> - <input type="text" class="cbi-section-create-name" id="cbi.cts.<%=self.config%>.<%=self.sectiontype%>.<%=section%>" name="cbi.cts.<%=self.config%>.<%=self.sectiontype%>.<%=section%>" /> - <script type="text/javascript">cbi_validate_field('cbi.cts.<%=self.config%>.<%=self.sectiontype%>.<%=section%>', true, 'uciname');</script> - <input class="cbi-button cbi-button-add" type="submit" onclick="this.form.cbi_state='add-section'; return true" value="<%:Add%>" title="<%:Add%>" /> - <% if self.invalid_cts then -%> - <br /><%:Invalid%></div> - <%- end %> - <% end %> - </div> - <%- end %> - <%- end -%> - </div> -</fieldset> -<!-- /tblsection --> diff --git a/libs/web/luasrc/view/cbi/tsection.htm b/libs/web/luasrc/view/cbi/tsection.htm deleted file mode 100644 index 087548bf2..000000000 --- a/libs/web/luasrc/view/cbi/tsection.htm +++ /dev/null @@ -1,48 +0,0 @@ -<fieldset class="cbi-section" id="cbi-<%=self.config%>-<%=self.sectiontype%>"> - <% if self.title and #self.title > 0 then -%> - <legend><%=self.title%></legend> - <%- end %> - <div class="cbi-section-descr"><%=self.description%></div> - <% local isempty = true for i, k in ipairs(self:cfgsections()) do -%> - <% if self.addremove then -%> - <div class="cbi-section-remove right"> - <input type="submit" name="cbi.rts.<%=self.config%>.<%=k%>" onclick="this.form.cbi_state='del-section'; return true" value="<%:Delete%>" class="cbi-button" /> - </div> - <%- end %> - - <%- section = k; isempty = false -%> - - <% if not self.anonymous then -%> - <h3><%=section:upper()%></h3> - <%- end %> - - <%+cbi/tabmenu%> - - <fieldset class="cbi-section-node<% if self.tabs then %> cbi-section-node-tabbed<% end %>" id="cbi-<%=self.config%>-<%=section%>"> - <%+cbi/ucisection%> - </fieldset> - <br /> - <%- end %> - - <% if isempty then -%> - <em><%:This section contains no values yet%><br /><br /></em> - <%- end %> - - <% if self.addremove then -%> - <% if self.template_addremove then include(self.template_addremove) else -%> - <div class="cbi-section-create"> - <% if self.anonymous then -%> - <input type="submit" class="cbi-button cbi-button-add" name="cbi.cts.<%=self.config%>.<%=self.sectiontype%>.<%=section%>" value="<%:Add%>" /> - <%- else -%> - <% if self.invalid_cts then -%><div class="cbi-section-error"><% end %> - <input type="text" class="cbi-section-create-name" id="cbi.cts.<%=self.config%>.<%=self.sectiontype%>.<%=section%>" name="cbi.cts.<%=self.config%>.<%=self.sectiontype%>.<%=section%>" /> - <script type="text/javascript">cbi_validate_field('cbi.cts.<%=self.config%>.<%=self.sectiontype%>.<%=section%>', true, 'uciname');</script> - <input type="submit" class="cbi-button cbi-button-add" onclick="this.form.cbi_state='add-section'; return true" value="<%:Add%>" /> - <% if self.invalid_cts then -%> - <br /><%:Invalid%></div> - <%- end %> - <%- end %> - </div> - <%- end %> - <%- end %> -</fieldset> diff --git a/libs/web/luasrc/view/cbi/tvalue.htm b/libs/web/luasrc/view/cbi/tvalue.htm deleted file mode 100644 index fcf7a6c94..000000000 --- a/libs/web/luasrc/view/cbi/tvalue.htm +++ /dev/null @@ -1,5 +0,0 @@ -<%+cbi/valueheader%> - <textarea class="cbi-input-textarea" <% if not self.size then %> style="width: 100%"<% else %> cols="<%=self.size%>"<% end %> onchange="cbi_d_update(this.id)"<%= attr("name", cbid) .. attr("id", cbid) .. ifattr(self.rows, "rows") .. ifattr(self.wrap, "wrap") %>> - <%-=pcdata(self:cfgvalue(section))-%> - </textarea> -<%+cbi/valuefooter%> diff --git a/libs/web/luasrc/view/cbi/ucisection.htm b/libs/web/luasrc/view/cbi/ucisection.htm deleted file mode 100644 index 3b69f12f2..000000000 --- a/libs/web/luasrc/view/cbi/ucisection.htm +++ /dev/null @@ -1,75 +0,0 @@ -<%- - if type(self.hidden) == "table" then - for k, v in pairs(self.hidden) do --%> - <input type="hidden" id="<%=k%>" name="<%=k%>" value="<%=pcdata(v)%>" /> -<%- - end - end -%> - -<% if self.tabs then %> - <%+cbi/tabcontainer%> -<% else %> - <% self:render_children(section, scope or {}) %> -<% end %> - -<% if self.error and self.error[section] then -%> - <div class="cbi-section-error"> - <ul><% for _, e in ipairs(self.error[section]) do -%> - <li> - <%- if e == "invalid" then -%> - <%:One or more fields contain invalid values!%> - <%- elseif e == "missing" then -%> - <%:One or more required fields have no value!%> - <%- else -%> - <%=pcdata(e)%> - <%- end -%> - </li> - <%- end %></ul> - </div> -<%- end %> - -<% if self.optionals[section] and #self.optionals[section] > 0 or self.dynamic then %> - <div class="cbi-optionals"> - <% if self.dynamic then %> - <input type="text" id="cbi.opt.<%=self.config%>.<%=section%>" name="cbi.opt.<%=self.config%>.<%=section%>" /> - <% if self.optionals[section] and #self.optionals[section] > 0 then %> - <script type="text/javascript"> - cbi_combobox_init('cbi.opt.<%=self.config%>.<%=section%>', { - <%- - for i, val in pairs(self.optionals[section]) do - -%> - <%-=string.format("%q", val.option) .. ":" .. string.format("%q", striptags(val.title))-%> - <%-if next(self.optionals[section], i) then-%>,<%-end-%> - <%- - end - -%> - }, '', '<%-: -- custom -- -%>'); - </script> - <% end %> - <% else %> - <select id="cbi.opt.<%=self.config%>.<%=section%>" name="cbi.opt.<%=self.config%>.<%=section%>"> - <option><%: -- Additional Field -- %></option> - <% for key, val in pairs(self.optionals[section]) do -%> - <option id="cbi-<%=self.config.."-"..section.."-"..val.option%>" value="<%=val.option%>"><%=striptags(val.title)%></option> - <%- end %> - </select> - <script type="text/javascript"><% for key, val in pairs(self.optionals[section]) do %> - <% if #val.deps > 0 then %><% for j, d in ipairs(val.deps) do -%> - cbi_d_add("cbi-<%=self.config.."-"..section.."-"..val.option..d.add%>", { - <%- - for k,v in pairs(d.deps) do - -%> - <%-=string.format('"cbid.%s.%s.%s"', self.config, section, k) .. ":" .. string.format("%q", v)-%> - <%-if next(d.deps, k) then-%>,<%-end-%> - <%- - end - -%> - }); - <%- end %><% end %> - <% end %></script> - <% end %> - <input type="submit" class="cbi-button cbi-button-fieldadd" value="<%:Add%>" /> - </div> -<% end %> diff --git a/libs/web/luasrc/view/cbi/upload.htm b/libs/web/luasrc/view/cbi/upload.htm deleted file mode 100644 index 777093411..000000000 --- a/libs/web/luasrc/view/cbi/upload.htm +++ /dev/null @@ -1,14 +0,0 @@ -<% - local t = require("luci.tools.webadmin") - local v = self:cfgvalue(section) - local s = v and nixio.fs.stat(v) --%> -<%+cbi/valueheader%> - <% if s then %> - <%:Uploaded File%> (<%=t.byte_format(s.size)%>) - <input type="hidden"<%= attr("value", v) .. attr("name", cbid) .. attr("id", cbid) %> /> - <input class="cbi-button cbi-input-image" type="image" value="<%:Replace entry%>" name="cbi.rlf.<%=section .. "." .. self.option%>" alt="<%:Replace entry%>" title="<%:Replace entry%>" src="<%=resource%>/cbi/reload.gif" /> - <% else %> - <input class="cbi-input-file" type="file"<%= attr("name", cbid) .. attr("id", cbid) %> /> - <% end %> -<%+cbi/valuefooter%> diff --git a/libs/web/luasrc/view/cbi/value.htm b/libs/web/luasrc/view/cbi/value.htm deleted file mode 100644 index d1a7bea5c..000000000 --- a/libs/web/luasrc/view/cbi/value.htm +++ /dev/null @@ -1,35 +0,0 @@ -<%+cbi/valueheader%> - <input type="<%=self.password and 'password" class="cbi-input-password' or 'text" class="cbi-input-text' %>" onchange="cbi_d_update(this.id)"<%= - attr("name", cbid) .. attr("id", cbid) .. attr("value", self:cfgvalue(section) or self.default) .. - ifattr(self.size, "size") .. ifattr(self.placeholder, "placeholder") - %> /> - <% if self.password then %><img src="<%=resource%>/cbi/reload.gif" style="vertical-align:middle" title="<%:Reveal/hide password%>" onclick="var e = document.getElementById('<%=cbid%>'); e.type = (e.type=='password') ? 'text' : 'password';" /><% end %> - <% if #self.keylist > 0 or self.datatype then -%> - <script type="text/javascript">//<![CDATA[ - <% if #self.keylist > 0 then -%> - cbi_combobox_init('<%=cbid%>', { - <%- - for i, k in ipairs(self.keylist) do - -%> - <%-=string.format("%q", k) .. ":" .. string.format("%q", self.vallist[i])-%> - <%-if i<#self.keylist then-%>,<%-end-%> - <%- - end - -%> - }, '<%- if not self.rmempty and not self.optional then -%> - <%-: -- Please choose -- -%> - <%- elseif self.placeholder then -%> - <%-= pcdata(self.placeholder) -%> - <%- end -%>', ' - <%- if self.combobox_manual then -%> - <%-=self.combobox_manual-%> - <%- else -%> - <%-: -- custom -- -%> - <%- end -%>'); - <%- end %> - <% if self.datatype then -%> - cbi_validate_field('<%=cbid%>', <%=tostring((self.optional or self.rmempty) == true)%>, '<%=self.datatype:gsub("'", "\\'")%>'); - <%- end %> - //]]></script> - <% end -%> -<%+cbi/valuefooter%> diff --git a/libs/web/luasrc/view/cbi/valuefooter.htm b/libs/web/luasrc/view/cbi/valuefooter.htm deleted file mode 100644 index 805312e45..000000000 --- a/libs/web/luasrc/view/cbi/valuefooter.htm +++ /dev/null @@ -1 +0,0 @@ -<% include( valuefooter or "cbi/full_valuefooter" ) %> diff --git a/libs/web/luasrc/view/cbi/valueheader.htm b/libs/web/luasrc/view/cbi/valueheader.htm deleted file mode 100644 index 761a54aed..000000000 --- a/libs/web/luasrc/view/cbi/valueheader.htm +++ /dev/null @@ -1 +0,0 @@ -<% include( valueheader or "cbi/full_valueheader" ) %> diff --git a/libs/web/root/etc/config/luci b/libs/web/root/etc/config/luci deleted file mode 100644 index c503a8f1e..000000000 --- a/libs/web/root/etc/config/luci +++ /dev/null @@ -1,24 +0,0 @@ -config core main - option lang auto - option mediaurlbase /luci-static/openwrt.org - option resourcebase /luci-static/resources - -config extern flash_keep - option uci "/etc/config/" - option dropbear "/etc/dropbear/" - option openvpn "/etc/openvpn/" - option passwd "/etc/passwd" - option opkg "/etc/opkg.conf" - option firewall "/etc/firewall.user" - option uploads "/lib/uci/upload/" - -config internal languages - -config internal sauth - option sessionpath "/tmp/luci-sessions" - option sessiontime 3600 - -config internal ccache - option enable 1 - -config internal themes diff --git a/libs/web/root/lib/uci/upload/.gitignore b/libs/web/root/lib/uci/upload/.gitignore deleted file mode 100644 index e69de29bb..000000000 --- a/libs/web/root/lib/uci/upload/.gitignore +++ /dev/null diff --git a/libs/web/src/po2lmo.c b/libs/web/src/po2lmo.c deleted file mode 100644 index 0da792b68..000000000 --- a/libs/web/src/po2lmo.c +++ /dev/null @@ -1,247 +0,0 @@ -/* - * lmo - Lua Machine Objects - PO to LMO conversion tool - * - * Copyright (C) 2009-2012 Jo-Philipp Wich <xm@subsignal.org> - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "template_lmo.h" - -static void die(const char *msg) -{ - fprintf(stderr, "Error: %s\n", msg); - exit(1); -} - -static void usage(const char *name) -{ - fprintf(stderr, "Usage: %s input.po output.lmo\n", name); - exit(1); -} - -static void print(const void *ptr, size_t size, size_t nmemb, FILE *stream) -{ - if( fwrite(ptr, size, nmemb, stream) == 0 ) - die("Failed to write stdout"); -} - -static int extract_string(const char *src, char *dest, int len) -{ - int pos = 0; - int esc = 0; - int off = -1; - - for( pos = 0; (pos < strlen(src)) && (pos < len); pos++ ) - { - if( (off == -1) && (src[pos] == '"') ) - { - off = pos + 1; - } - else if( off >= 0 ) - { - if( esc == 1 ) - { - switch (src[pos]) - { - case '"': - case '\\': - off++; - break; - } - dest[pos-off] = src[pos]; - esc = 0; - } - else if( src[pos] == '\\' ) - { - dest[pos-off] = src[pos]; - esc = 1; - } - else if( src[pos] != '"' ) - { - dest[pos-off] = src[pos]; - } - else - { - dest[pos-off] = '\0'; - break; - } - } - } - - return (off > -1) ? strlen(dest) : -1; -} - -static int cmp_index(const void *a, const void *b) -{ - uint32_t x = ((const lmo_entry_t *)a)->key_id; - uint32_t y = ((const lmo_entry_t *)b)->key_id; - - if (x < y) - return -1; - else if (x > y) - return 1; - - return 0; -} - -static void print_uint32(uint32_t x, FILE *out) -{ - uint32_t y = htonl(x); - print(&y, sizeof(uint32_t), 1, out); -} - -static void print_index(void *array, int n, FILE *out) -{ - lmo_entry_t *e; - - qsort(array, n, sizeof(*e), cmp_index); - - for (e = array; n > 0; n--, e++) - { - print_uint32(e->key_id, out); - print_uint32(e->val_id, out); - print_uint32(e->offset, out); - print_uint32(e->length, out); - } -} - -int main(int argc, char *argv[]) -{ - char line[4096]; - char key[4096]; - char val[4096]; - char tmp[4096]; - int state = 0; - int offset = 0; - int length = 0; - int n_entries = 0; - void *array = NULL; - lmo_entry_t *entry = NULL; - uint32_t key_id, val_id; - - FILE *in; - FILE *out; - - if( (argc != 3) || ((in = fopen(argv[1], "r")) == NULL) || ((out = fopen(argv[2], "w")) == NULL) ) - usage(argv[0]); - - memset(line, 0, sizeof(key)); - memset(key, 0, sizeof(val)); - memset(val, 0, sizeof(val)); - - while( (NULL != fgets(line, sizeof(line), in)) || (state >= 2 && feof(in)) ) - { - if( state == 0 && strstr(line, "msgid \"") == line ) - { - switch(extract_string(line, key, sizeof(key))) - { - case -1: - die("Syntax error in msgid"); - case 0: - state = 1; - break; - default: - state = 2; - } - } - else if( state == 1 || state == 2 ) - { - if( strstr(line, "msgstr \"") == line || state == 2 ) - { - switch(extract_string(line, val, sizeof(val))) - { - case -1: - state = 4; - break; - default: - state = 3; - } - } - else - { - switch(extract_string(line, tmp, sizeof(tmp))) - { - case -1: - state = 2; - break; - default: - strcat(key, tmp); - } - } - } - else if( state == 3 ) - { - switch(extract_string(line, tmp, sizeof(tmp))) - { - case -1: - state = 4; - break; - default: - strcat(val, tmp); - } - } - - if( state == 4 ) - { - if( strlen(key) > 0 && strlen(val) > 0 ) - { - key_id = sfh_hash(key, strlen(key)); - val_id = sfh_hash(val, strlen(val)); - - if( key_id != val_id ) - { - n_entries++; - array = realloc(array, n_entries * sizeof(lmo_entry_t)); - entry = (lmo_entry_t *)array + n_entries - 1; - - if (!array) - die("Out of memory"); - - entry->key_id = key_id; - entry->val_id = val_id; - entry->offset = offset; - entry->length = strlen(val); - - length = strlen(val) + ((4 - (strlen(val) % 4)) % 4); - - print(val, length, 1, out); - offset += length; - } - } - - state = 0; - memset(key, 0, sizeof(key)); - memset(val, 0, sizeof(val)); - } - - memset(line, 0, sizeof(line)); - } - - print_index(array, n_entries, out); - - if( offset > 0 ) - { - print_uint32(offset, out); - fsync(fileno(out)); - fclose(out); - } - else - { - fclose(out); - unlink(argv[2]); - } - - fclose(in); - return(0); -} diff --git a/libs/web/src/template_lmo.c b/libs/web/src/template_lmo.c deleted file mode 100644 index 27205a722..000000000 --- a/libs/web/src/template_lmo.c +++ /dev/null @@ -1,328 +0,0 @@ -/* - * lmo - Lua Machine Objects - Base functions - * - * Copyright (C) 2009-2010 Jo-Philipp Wich <xm@subsignal.org> - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "template_lmo.h" - -/* - * Hash function from http://www.azillionmonkeys.com/qed/hash.html - * Copyright (C) 2004-2008 by Paul Hsieh - */ - -uint32_t sfh_hash(const char *data, int len) -{ - uint32_t hash = len, tmp; - int rem; - - if (len <= 0 || data == NULL) return 0; - - rem = len & 3; - len >>= 2; - - /* Main loop */ - for (;len > 0; len--) { - hash += sfh_get16(data); - tmp = (sfh_get16(data+2) << 11) ^ hash; - hash = (hash << 16) ^ tmp; - data += 2*sizeof(uint16_t); - hash += hash >> 11; - } - - /* Handle end cases */ - switch (rem) { - case 3: hash += sfh_get16(data); - hash ^= hash << 16; - hash ^= data[sizeof(uint16_t)] << 18; - hash += hash >> 11; - break; - case 2: hash += sfh_get16(data); - hash ^= hash << 11; - hash += hash >> 17; - break; - case 1: hash += *data; - hash ^= hash << 10; - hash += hash >> 1; - } - - /* Force "avalanching" of final 127 bits */ - hash ^= hash << 3; - hash += hash >> 5; - hash ^= hash << 4; - hash += hash >> 17; - hash ^= hash << 25; - hash += hash >> 6; - - return hash; -} - -uint32_t lmo_canon_hash(const char *str, int len) -{ - char res[4096]; - char *ptr, prev; - int off; - - if (!str || len >= sizeof(res)) - return 0; - - for (prev = ' ', ptr = res, off = 0; off < len; prev = *str, off++, str++) - { - if (isspace(*str)) - { - if (!isspace(prev)) - *ptr++ = ' '; - } - else - { - *ptr++ = *str; - } - } - - if ((ptr > res) && isspace(*(ptr-1))) - ptr--; - - return sfh_hash(res, ptr - res); -} - -lmo_archive_t * lmo_open(const char *file) -{ - int in = -1; - uint32_t idx_offset = 0; - struct stat s; - - lmo_archive_t *ar = NULL; - - if (stat(file, &s) == -1) - goto err; - - if ((in = open(file, O_RDONLY)) == -1) - goto err; - - if ((ar = (lmo_archive_t *)malloc(sizeof(*ar))) != NULL) - { - memset(ar, 0, sizeof(*ar)); - - ar->fd = in; - ar->size = s.st_size; - - fcntl(ar->fd, F_SETFD, fcntl(ar->fd, F_GETFD) | FD_CLOEXEC); - - if ((ar->mmap = mmap(NULL, ar->size, PROT_READ, MAP_SHARED, ar->fd, 0)) == MAP_FAILED) - goto err; - - idx_offset = ntohl(*((const uint32_t *) - (ar->mmap + ar->size - sizeof(uint32_t)))); - - if (idx_offset >= ar->size) - goto err; - - ar->index = (lmo_entry_t *)(ar->mmap + idx_offset); - ar->length = (ar->size - idx_offset - sizeof(uint32_t)) / sizeof(lmo_entry_t); - ar->end = ar->mmap + ar->size; - - return ar; - } - -err: - if (in > -1) - close(in); - - if (ar != NULL) - { - if ((ar->mmap != NULL) && (ar->mmap != MAP_FAILED)) - munmap(ar->mmap, ar->size); - - free(ar); - } - - return NULL; -} - -void lmo_close(lmo_archive_t *ar) -{ - if (ar != NULL) - { - if ((ar->mmap != NULL) && (ar->mmap != MAP_FAILED)) - munmap(ar->mmap, ar->size); - - close(ar->fd); - free(ar); - - ar = NULL; - } -} - - -lmo_catalog_t *_lmo_catalogs = NULL; -lmo_catalog_t *_lmo_active_catalog = NULL; - -int lmo_load_catalog(const char *lang, const char *dir) -{ - DIR *dh = NULL; - char pattern[16]; - char path[PATH_MAX]; - struct dirent *de = NULL; - - lmo_archive_t *ar = NULL; - lmo_catalog_t *cat = NULL; - - if (!lmo_change_catalog(lang)) - return 0; - - if (!dir || !(dh = opendir(dir))) - goto err; - - if (!(cat = malloc(sizeof(*cat)))) - goto err; - - memset(cat, 0, sizeof(*cat)); - - snprintf(cat->lang, sizeof(cat->lang), "%s", lang); - snprintf(pattern, sizeof(pattern), "*.%s.lmo", lang); - - while ((de = readdir(dh)) != NULL) - { - if (!fnmatch(pattern, de->d_name, 0)) - { - snprintf(path, sizeof(path), "%s/%s", dir, de->d_name); - ar = lmo_open(path); - - if (ar) - { - ar->next = cat->archives; - cat->archives = ar; - } - } - } - - closedir(dh); - - cat->next = _lmo_catalogs; - _lmo_catalogs = cat; - - if (!_lmo_active_catalog) - _lmo_active_catalog = cat; - - return 0; - -err: - if (dh) closedir(dh); - if (cat) free(cat); - - return -1; -} - -int lmo_change_catalog(const char *lang) -{ - lmo_catalog_t *cat; - - for (cat = _lmo_catalogs; cat; cat = cat->next) - { - if (!strncmp(cat->lang, lang, sizeof(cat->lang))) - { - _lmo_active_catalog = cat; - return 0; - } - } - - return -1; -} - -static lmo_entry_t * lmo_find_entry(lmo_archive_t *ar, uint32_t hash) -{ - unsigned int m, l, r; - uint32_t k; - - l = 0; - r = ar->length - 1; - - while (1) - { - m = l + ((r - l) / 2); - - if (r < l) - break; - - k = ntohl(ar->index[m].key_id); - - if (k == hash) - return &ar->index[m]; - - if (k > hash) - { - if (!m) - break; - - r = m - 1; - } - else - { - l = m + 1; - } - } - - return NULL; -} - -int lmo_translate(const char *key, int keylen, char **out, int *outlen) -{ - uint32_t hash; - lmo_entry_t *e; - lmo_archive_t *ar; - - if (!key || !_lmo_active_catalog) - return -2; - - hash = lmo_canon_hash(key, keylen); - - for (ar = _lmo_active_catalog->archives; ar; ar = ar->next) - { - if ((e = lmo_find_entry(ar, hash)) != NULL) - { - *out = ar->mmap + ntohl(e->offset); - *outlen = ntohl(e->length); - return 0; - } - } - - return -1; -} - -void lmo_close_catalog(const char *lang) -{ - lmo_archive_t *ar, *next; - lmo_catalog_t *cat, *prev; - - for (prev = NULL, cat = _lmo_catalogs; cat; prev = cat, cat = cat->next) - { - if (!strncmp(cat->lang, lang, sizeof(cat->lang))) - { - if (prev) - prev->next = cat->next; - else - _lmo_catalogs = cat->next; - - for (ar = cat->archives; ar; ar = next) - { - next = ar->next; - lmo_close(ar); - } - - free(cat); - break; - } - } -} diff --git a/libs/web/src/template_lmo.h b/libs/web/src/template_lmo.h deleted file mode 100644 index 57f59aa56..000000000 --- a/libs/web/src/template_lmo.h +++ /dev/null @@ -1,92 +0,0 @@ -/* - * lmo - Lua Machine Objects - General header - * - * Copyright (C) 2009-2012 Jo-Philipp Wich <xm@subsignal.org> - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef _TEMPLATE_LMO_H_ -#define _TEMPLATE_LMO_H_ - -#include <stdlib.h> -#include <stdio.h> -#include <stdint.h> -#include <string.h> -#include <fcntl.h> -#include <sys/stat.h> -#include <sys/mman.h> -#include <arpa/inet.h> -#include <unistd.h> -#include <errno.h> -#include <fnmatch.h> -#include <dirent.h> -#include <ctype.h> -#include <limits.h> - -#if (defined(__GNUC__) && defined(__i386__)) -#define sfh_get16(d) (*((const uint16_t *) (d))) -#else -#define sfh_get16(d) ((((uint32_t)(((const uint8_t *)(d))[1])) << 8)\ - +(uint32_t)(((const uint8_t *)(d))[0]) ) -#endif - - -struct lmo_entry { - uint32_t key_id; - uint32_t val_id; - uint32_t offset; - uint32_t length; -} __attribute__((packed)); - -typedef struct lmo_entry lmo_entry_t; - - -struct lmo_archive { - int fd; - int length; - uint32_t size; - lmo_entry_t *index; - char *mmap; - char *end; - struct lmo_archive *next; -}; - -typedef struct lmo_archive lmo_archive_t; - - -struct lmo_catalog { - char lang[6]; - struct lmo_archive *archives; - struct lmo_catalog *next; -}; - -typedef struct lmo_catalog lmo_catalog_t; - - -uint32_t sfh_hash(const char *data, int len); -uint32_t lmo_canon_hash(const char *data, int len); - -lmo_archive_t * lmo_open(const char *file); -void lmo_close(lmo_archive_t *ar); - - -extern lmo_catalog_t *_lmo_catalogs; -extern lmo_catalog_t *_lmo_active_catalog; - -int lmo_load_catalog(const char *lang, const char *dir); -int lmo_change_catalog(const char *lang); -int lmo_translate(const char *key, int keylen, char **out, int *outlen); -void lmo_close_catalog(const char *lang); - -#endif diff --git a/libs/web/src/template_lualib.c b/libs/web/src/template_lualib.c deleted file mode 100644 index 0d4364104..000000000 --- a/libs/web/src/template_lualib.c +++ /dev/null @@ -1,163 +0,0 @@ -/* - * LuCI Template - Lua binding - * - * Copyright (C) 2009 Jo-Philipp Wich <xm@subsignal.org> - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "template_lualib.h" - -int template_L_parse(lua_State *L) -{ - const char *file = luaL_checkstring(L, 1); - struct template_parser *parser = template_open(file); - int lua_status, rv; - - if (!parser) - { - lua_pushnil(L); - lua_pushinteger(L, errno); - lua_pushstring(L, strerror(errno)); - return 3; - } - - lua_status = lua_load(L, template_reader, parser, file); - - if (lua_status == 0) - rv = 1; - else - rv = template_error(L, parser); - - template_close(parser); - - return rv; -} - -int template_L_utf8(lua_State *L) -{ - size_t len = 0; - const char *str = luaL_checklstring(L, 1, &len); - char *res = utf8(str, len); - - if (res != NULL) - { - lua_pushstring(L, res); - free(res); - - return 1; - } - - return 0; -} - -int template_L_pcdata(lua_State *L) -{ - size_t len = 0; - const char *str = luaL_checklstring(L, 1, &len); - char *res = pcdata(str, len); - - if (res != NULL) - { - lua_pushstring(L, res); - free(res); - - return 1; - } - - return 0; -} - -int template_L_striptags(lua_State *L) -{ - size_t len = 0; - const char *str = luaL_checklstring(L, 1, &len); - char *res = striptags(str, len); - - if (res != NULL) - { - lua_pushstring(L, res); - free(res); - - return 1; - } - - return 0; -} - -static int template_L_load_catalog(lua_State *L) { - const char *lang = luaL_optstring(L, 1, "en"); - const char *dir = luaL_optstring(L, 2, NULL); - lua_pushboolean(L, !lmo_load_catalog(lang, dir)); - return 1; -} - -static int template_L_close_catalog(lua_State *L) { - const char *lang = luaL_optstring(L, 1, "en"); - lmo_close_catalog(lang); - return 0; -} - -static int template_L_change_catalog(lua_State *L) { - const char *lang = luaL_optstring(L, 1, "en"); - lua_pushboolean(L, !lmo_change_catalog(lang)); - return 1; -} - -static int template_L_translate(lua_State *L) { - size_t len; - char *tr; - int trlen; - const char *key = luaL_checklstring(L, 1, &len); - - switch (lmo_translate(key, len, &tr, &trlen)) - { - case 0: - lua_pushlstring(L, tr, trlen); - return 1; - - case -1: - return 0; - } - - lua_pushnil(L); - lua_pushstring(L, "no catalog loaded"); - return 2; -} - -static int template_L_hash(lua_State *L) { - size_t len; - const char *key = luaL_checklstring(L, 1, &len); - lua_pushinteger(L, sfh_hash(key, len)); - return 1; -} - - -/* module table */ -static const luaL_reg R[] = { - { "parse", template_L_parse }, - { "utf8", template_L_utf8 }, - { "pcdata", template_L_pcdata }, - { "striptags", template_L_striptags }, - { "load_catalog", template_L_load_catalog }, - { "close_catalog", template_L_close_catalog }, - { "change_catalog", template_L_change_catalog }, - { "translate", template_L_translate }, - { "hash", template_L_hash }, - { NULL, NULL } -}; - -LUALIB_API int luaopen_luci_template_parser(lua_State *L) { - luaL_register(L, TEMPLATE_LUALIB_META, R); - return 1; -} diff --git a/libs/web/src/template_lualib.h b/libs/web/src/template_lualib.h deleted file mode 100644 index 1b659be12..000000000 --- a/libs/web/src/template_lualib.h +++ /dev/null @@ -1,30 +0,0 @@ -/* - * LuCI Template - Lua library header - * - * Copyright (C) 2009 Jo-Philipp Wich <xm@subsignal.org> - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef _TEMPLATE_LUALIB_H_ -#define _TEMPLATE_LUALIB_H_ - -#include "template_parser.h" -#include "template_utils.h" -#include "template_lmo.h" - -#define TEMPLATE_LUALIB_META "template.parser" - -LUALIB_API int luaopen_luci_template_parser(lua_State *L); - -#endif diff --git a/libs/web/src/template_parser.c b/libs/web/src/template_parser.c deleted file mode 100644 index 605445131..000000000 --- a/libs/web/src/template_parser.c +++ /dev/null @@ -1,386 +0,0 @@ -/* - * LuCI Template - Parser implementation - * - * Copyright (C) 2009-2012 Jo-Philipp Wich <xm@subsignal.org> - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "template_parser.h" -#include "template_utils.h" -#include "template_lmo.h" - - -/* leading and trailing code for different types */ -const char *gen_code[9][2] = { - { NULL, NULL }, - { "write(\"", "\")" }, - { NULL, NULL }, - { "write(tostring(", " or \"\"))" }, - { "include(\"", "\")" }, - { "write(\"", "\")" }, - { "write(\"", "\")" }, - { NULL, " " }, - { NULL, NULL }, -}; - -/* Simple strstr() like function that takes len arguments for both haystack and needle. */ -static char *strfind(char *haystack, int hslen, const char *needle, int ndlen) -{ - int match = 0; - int i, j; - - for( i = 0; i < hslen; i++ ) - { - if( haystack[i] == needle[0] ) - { - match = ((ndlen == 1) || ((i + ndlen) <= hslen)); - - for( j = 1; (j < ndlen) && ((i + j) < hslen); j++ ) - { - if( haystack[i+j] != needle[j] ) - { - match = 0; - break; - } - } - - if( match ) - return &haystack[i]; - } - } - - return NULL; -} - -struct template_parser * template_open(const char *file) -{ - struct stat s; - static struct template_parser *parser; - - if (!(parser = malloc(sizeof(*parser)))) - goto err; - - memset(parser, 0, sizeof(*parser)); - parser->fd = -1; - parser->file = file; - - if (stat(file, &s)) - goto err; - - if ((parser->fd = open(file, O_RDONLY)) < 0) - goto err; - - parser->size = s.st_size; - parser->mmap = mmap(NULL, parser->size, PROT_READ, MAP_PRIVATE, - parser->fd, 0); - - if (parser->mmap != MAP_FAILED) - { - parser->off = parser->mmap; - parser->cur_chunk.type = T_TYPE_INIT; - parser->cur_chunk.s = parser->mmap; - parser->cur_chunk.e = parser->mmap; - - return parser; - } - -err: - template_close(parser); - return NULL; -} - -void template_close(struct template_parser *parser) -{ - if (!parser) - return; - - if (parser->gc != NULL) - free(parser->gc); - - if ((parser->mmap != NULL) && (parser->mmap != MAP_FAILED)) - munmap(parser->mmap, parser->size); - - if (parser->fd >= 0) - close(parser->fd); - - free(parser); -} - -void template_text(struct template_parser *parser, const char *e) -{ - const char *s = parser->off; - - if (s < (parser->mmap + parser->size)) - { - if (parser->strip_after) - { - while ((s <= e) && isspace(*s)) - s++; - } - - parser->cur_chunk.type = T_TYPE_TEXT; - } - else - { - parser->cur_chunk.type = T_TYPE_EOF; - } - - parser->cur_chunk.line = parser->line; - parser->cur_chunk.s = s; - parser->cur_chunk.e = e; -} - -void template_code(struct template_parser *parser, const char *e) -{ - const char *s = parser->off; - - parser->strip_before = 0; - parser->strip_after = 0; - - if (*s == '-') - { - parser->strip_before = 1; - for (s++; (s <= e) && (*s == ' ' || *s == '\t'); s++); - } - - if (*(e-1) == '-') - { - parser->strip_after = 1; - for (e--; (e >= s) && (*e == ' ' || *e == '\t'); e--); - } - - switch (*s) - { - /* comment */ - case '#': - s++; - parser->cur_chunk.type = T_TYPE_COMMENT; - break; - - /* include */ - case '+': - s++; - parser->cur_chunk.type = T_TYPE_INCLUDE; - break; - - /* translate */ - case ':': - s++; - parser->cur_chunk.type = T_TYPE_I18N; - break; - - /* translate raw */ - case '_': - s++; - parser->cur_chunk.type = T_TYPE_I18N_RAW; - break; - - /* expr */ - case '=': - s++; - parser->cur_chunk.type = T_TYPE_EXPR; - break; - - /* code */ - default: - parser->cur_chunk.type = T_TYPE_CODE; - break; - } - - parser->cur_chunk.line = parser->line; - parser->cur_chunk.s = s; - parser->cur_chunk.e = e; -} - -static const char * -template_format_chunk(struct template_parser *parser, size_t *sz) -{ - const char *s, *p; - const char *head, *tail; - struct template_chunk *c = &parser->prv_chunk; - struct template_buffer *buf; - - *sz = 0; - s = parser->gc = NULL; - - if (parser->strip_before && c->type == T_TYPE_TEXT) - { - while ((c->e > c->s) && isspace(*(c->e - 1))) - c->e--; - } - - /* empty chunk */ - if (c->s == c->e) - { - if (c->type == T_TYPE_EOF) - { - *sz = 0; - s = NULL; - } - else - { - *sz = 1; - s = " "; - } - } - - /* format chunk */ - else if ((buf = buf_init(c->e - c->s)) != NULL) - { - if ((head = gen_code[c->type][0]) != NULL) - buf_append(buf, head, strlen(head)); - - switch (c->type) - { - case T_TYPE_TEXT: - luastr_escape(buf, c->s, c->e - c->s, 0); - break; - - case T_TYPE_EXPR: - buf_append(buf, c->s, c->e - c->s); - for (p = c->s; p < c->e; p++) - parser->line += (*p == '\n'); - break; - - case T_TYPE_INCLUDE: - luastr_escape(buf, c->s, c->e - c->s, 0); - break; - - case T_TYPE_I18N: - luastr_translate(buf, c->s, c->e - c->s, 1); - break; - - case T_TYPE_I18N_RAW: - luastr_translate(buf, c->s, c->e - c->s, 0); - break; - - case T_TYPE_CODE: - buf_append(buf, c->s, c->e - c->s); - for (p = c->s; p < c->e; p++) - parser->line += (*p == '\n'); - break; - } - - if ((tail = gen_code[c->type][1]) != NULL) - buf_append(buf, tail, strlen(tail)); - - *sz = buf_length(buf); - s = parser->gc = buf_destroy(buf); - - if (!*sz) - { - *sz = 1; - s = " "; - } - } - - return s; -} - -const char *template_reader(lua_State *L, void *ud, size_t *sz) -{ - struct template_parser *parser = ud; - int rem = parser->size - (parser->off - parser->mmap); - char *tag; - - parser->prv_chunk = parser->cur_chunk; - - /* free previous string */ - if (parser->gc) - { - free(parser->gc); - parser->gc = NULL; - } - - /* before tag */ - if (!parser->in_expr) - { - if ((tag = strfind(parser->off, rem, "<%", 2)) != NULL) - { - template_text(parser, tag); - parser->off = tag + 2; - parser->in_expr = 1; - } - else - { - template_text(parser, parser->mmap + parser->size); - parser->off = parser->mmap + parser->size; - } - } - - /* inside tag */ - else - { - if ((tag = strfind(parser->off, rem, "%>", 2)) != NULL) - { - template_code(parser, tag); - parser->off = tag + 2; - parser->in_expr = 0; - } - else - { - /* unexpected EOF */ - template_code(parser, parser->mmap + parser->size); - - *sz = 1; - return "\033"; - } - } - - return template_format_chunk(parser, sz); -} - -int template_error(lua_State *L, struct template_parser *parser) -{ - const char *err = luaL_checkstring(L, -1); - const char *off = parser->prv_chunk.s; - const char *ptr; - char msg[1024]; - int line = 0; - int chunkline = 0; - - if ((ptr = strfind((char *)err, strlen(err), "]:", 2)) != NULL) - { - chunkline = atoi(ptr + 2) - parser->prv_chunk.line; - - while (*ptr) - { - if (*ptr++ == ' ') - { - err = ptr; - break; - } - } - } - - if (strfind((char *)err, strlen(err), "'char(27)'", 10) != NULL) - { - off = parser->mmap + parser->size; - err = "'%>' expected before end of file"; - chunkline = 0; - } - - for (ptr = parser->mmap; ptr < off; ptr++) - if (*ptr == '\n') - line++; - - snprintf(msg, sizeof(msg), "Syntax error in %s:%d: %s", - parser->file, line + chunkline, err ? err : "(unknown error)"); - - lua_pushnil(L); - lua_pushinteger(L, line + chunkline); - lua_pushstring(L, msg); - - return 3; -} diff --git a/libs/web/src/template_parser.h b/libs/web/src/template_parser.h deleted file mode 100644 index d1c606272..000000000 --- a/libs/web/src/template_parser.h +++ /dev/null @@ -1,79 +0,0 @@ -/* - * LuCI Template - Parser header - * - * Copyright (C) 2009 Jo-Philipp Wich <xm@subsignal.org> - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef _TEMPLATE_PARSER_H_ -#define _TEMPLATE_PARSER_H_ - -#include <stdlib.h> -#include <stdio.h> -#include <stdint.h> -#include <unistd.h> -#include <fcntl.h> -#include <sys/stat.h> -#include <sys/mman.h> -#include <string.h> -#include <ctype.h> -#include <errno.h> - -#include <lua.h> -#include <lualib.h> -#include <lauxlib.h> - - -/* code types */ -#define T_TYPE_INIT 0 -#define T_TYPE_TEXT 1 -#define T_TYPE_COMMENT 2 -#define T_TYPE_EXPR 3 -#define T_TYPE_INCLUDE 4 -#define T_TYPE_I18N 5 -#define T_TYPE_I18N_RAW 6 -#define T_TYPE_CODE 7 -#define T_TYPE_EOF 8 - - -struct template_chunk { - const char *s; - const char *e; - int type; - int line; -}; - -/* parser state */ -struct template_parser { - int fd; - uint32_t size; - char *mmap; - char *off; - char *gc; - int line; - int in_expr; - int strip_before; - int strip_after; - struct template_chunk prv_chunk; - struct template_chunk cur_chunk; - const char *file; -}; - -struct template_parser * template_open(const char *file); -void template_close(struct template_parser *parser); - -const char *template_reader(lua_State *L, void *ud, size_t *sz); -int template_error(lua_State *L, struct template_parser *parser); - -#endif diff --git a/libs/web/src/template_utils.c b/libs/web/src/template_utils.c deleted file mode 100644 index 80542bd4f..000000000 --- a/libs/web/src/template_utils.c +++ /dev/null @@ -1,494 +0,0 @@ -/* - * LuCI Template - Utility functions - * - * Copyright (C) 2010 Jo-Philipp Wich <xm@subsignal.org> - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "template_utils.h" -#include "template_lmo.h" - -/* initialize a buffer object */ -struct template_buffer * buf_init(int size) -{ - struct template_buffer *buf; - - if (size <= 0) - size = 1024; - - buf = (struct template_buffer *)malloc(sizeof(struct template_buffer)); - - if (buf != NULL) - { - buf->fill = 0; - buf->size = size; - buf->data = malloc(buf->size); - - if (buf->data != NULL) - { - buf->dptr = buf->data; - buf->data[0] = 0; - - return buf; - } - - free(buf); - } - - return NULL; -} - -/* grow buffer */ -int buf_grow(struct template_buffer *buf, int size) -{ - unsigned int off = (buf->dptr - buf->data); - char *data; - - if (size <= 0) - size = 1024; - - data = realloc(buf->data, buf->size + size); - - if (data != NULL) - { - buf->data = data; - buf->dptr = data + off; - buf->size += size; - - return buf->size; - } - - return 0; -} - -/* put one char into buffer object */ -int buf_putchar(struct template_buffer *buf, char c) -{ - if( ((buf->fill + 1) >= buf->size) && !buf_grow(buf, 0) ) - return 0; - - *(buf->dptr++) = c; - *(buf->dptr) = 0; - - buf->fill++; - return 1; -} - -/* append data to buffer */ -int buf_append(struct template_buffer *buf, const char *s, int len) -{ - if ((buf->fill + len + 1) >= buf->size) - { - if (!buf_grow(buf, len + 1)) - return 0; - } - - memcpy(buf->dptr, s, len); - buf->fill += len; - buf->dptr += len; - - *(buf->dptr) = 0; - - return len; -} - -/* read buffer length */ -int buf_length(struct template_buffer *buf) -{ - return buf->fill; -} - -/* destroy buffer object and return pointer to data */ -char * buf_destroy(struct template_buffer *buf) -{ - char *data = buf->data; - - free(buf); - return data; -} - - -/* calculate the number of expected continuation chars */ -static inline int mb_num_chars(unsigned char c) -{ - if ((c & 0xE0) == 0xC0) - return 2; - else if ((c & 0xF0) == 0xE0) - return 3; - else if ((c & 0xF8) == 0xF0) - return 4; - else if ((c & 0xFC) == 0xF8) - return 5; - else if ((c & 0xFE) == 0xFC) - return 6; - - return 1; -} - -/* test whether the given byte is a valid continuation char */ -static inline int mb_is_cont(unsigned char c) -{ - return ((c >= 0x80) && (c <= 0xBF)); -} - -/* test whether the byte sequence at the given pointer with the given - * length is the shortest possible representation of the code point */ -static inline int mb_is_shortest(unsigned char *s, int n) -{ - switch (n) - { - case 2: - /* 1100000x (10xxxxxx) */ - return !(((*s >> 1) == 0x60) && - ((*(s+1) >> 6) == 0x02)); - - case 3: - /* 11100000 100xxxxx (10xxxxxx) */ - return !((*s == 0xE0) && - ((*(s+1) >> 5) == 0x04) && - ((*(s+2) >> 6) == 0x02)); - - case 4: - /* 11110000 1000xxxx (10xxxxxx 10xxxxxx) */ - return !((*s == 0xF0) && - ((*(s+1) >> 4) == 0x08) && - ((*(s+2) >> 6) == 0x02) && - ((*(s+3) >> 6) == 0x02)); - - case 5: - /* 11111000 10000xxx (10xxxxxx 10xxxxxx 10xxxxxx) */ - return !((*s == 0xF8) && - ((*(s+1) >> 3) == 0x10) && - ((*(s+2) >> 6) == 0x02) && - ((*(s+3) >> 6) == 0x02) && - ((*(s+4) >> 6) == 0x02)); - - case 6: - /* 11111100 100000xx (10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx) */ - return !((*s == 0xF8) && - ((*(s+1) >> 2) == 0x20) && - ((*(s+2) >> 6) == 0x02) && - ((*(s+3) >> 6) == 0x02) && - ((*(s+4) >> 6) == 0x02) && - ((*(s+5) >> 6) == 0x02)); - } - - return 1; -} - -/* test whether the byte sequence at the given pointer with the given - * length is an UTF-16 surrogate */ -static inline int mb_is_surrogate(unsigned char *s, int n) -{ - return ((n == 3) && (*s == 0xED) && (*(s+1) >= 0xA0) && (*(s+1) <= 0xBF)); -} - -/* test whether the byte sequence at the given pointer with the given - * length is an illegal UTF-8 code point */ -static inline int mb_is_illegal(unsigned char *s, int n) -{ - return ((n == 3) && (*s == 0xEF) && (*(s+1) == 0xBF) && - (*(s+2) >= 0xBE) && (*(s+2) <= 0xBF)); -} - - -/* scan given source string, validate UTF-8 sequence and store result - * in given buffer object */ -static int _validate_utf8(unsigned char **s, int l, struct template_buffer *buf) -{ - unsigned char *ptr = *s; - unsigned int o = 0, v, n; - - /* ascii byte without null */ - if ((*(ptr+0) >= 0x01) && (*(ptr+0) <= 0x7F)) - { - if (!buf_putchar(buf, *ptr++)) - return 0; - - o = 1; - } - - /* multi byte sequence */ - else if ((n = mb_num_chars(*ptr)) > 1) - { - /* count valid chars */ - for (v = 1; (v <= n) && ((o+v) < l) && mb_is_cont(*(ptr+v)); v++); - - switch (n) - { - case 6: - case 5: - /* five and six byte sequences are always invalid */ - if (!buf_putchar(buf, '?')) - return 0; - - break; - - default: - /* if the number of valid continuation bytes matches the - * expected number and if the sequence is legal, copy - * the bytes to the destination buffer */ - if ((v == n) && mb_is_shortest(ptr, n) && - !mb_is_surrogate(ptr, n) && !mb_is_illegal(ptr, n)) - { - /* copy sequence */ - if (!buf_append(buf, (char *)ptr, n)) - return 0; - } - - /* the found sequence is illegal, skip it */ - else - { - /* invalid sequence */ - if (!buf_putchar(buf, '?')) - return 0; - } - - break; - } - - /* advance beyound the last found valid continuation char */ - o = v; - ptr += v; - } - - /* invalid byte (0x00) */ - else - { - if (!buf_putchar(buf, '?')) /* or 0xEF, 0xBF, 0xBD */ - return 0; - - o = 1; - ptr++; - } - - *s = ptr; - return o; -} - -/* sanitize given string and replace all invalid UTF-8 sequences with "?" */ -char * utf8(const char *s, unsigned int l) -{ - struct template_buffer *buf = buf_init(l); - unsigned char *ptr = (unsigned char *)s; - unsigned int v, o; - - if (!buf) - return NULL; - - for (o = 0; o < l; o++) - { - /* ascii char */ - if ((*ptr >= 0x01) && (*ptr <= 0x7F)) - { - if (!buf_putchar(buf, (char)*ptr++)) - break; - } - - /* invalid byte or multi byte sequence */ - else - { - if (!(v = _validate_utf8(&ptr, l - o, buf))) - break; - - o += (v - 1); - } - } - - return buf_destroy(buf); -} - -/* Sanitize given string and strip all invalid XML bytes - * Validate UTF-8 sequences - * Escape XML control chars */ -char * pcdata(const char *s, unsigned int l) -{ - struct template_buffer *buf = buf_init(l); - unsigned char *ptr = (unsigned char *)s; - unsigned int o, v; - char esq[8]; - int esl; - - if (!buf) - return NULL; - - for (o = 0; o < l; o++) - { - /* Invalid XML bytes */ - if (((*ptr >= 0x00) && (*ptr <= 0x08)) || - ((*ptr >= 0x0B) && (*ptr <= 0x0C)) || - ((*ptr >= 0x0E) && (*ptr <= 0x1F)) || - (*ptr == 0x7F)) - { - ptr++; - } - - /* Escapes */ - else if ((*ptr == 0x26) || - (*ptr == 0x27) || - (*ptr == 0x22) || - (*ptr == 0x3C) || - (*ptr == 0x3E)) - { - esl = snprintf(esq, sizeof(esq), "&#%i;", *ptr); - - if (!buf_append(buf, esq, esl)) - break; - - ptr++; - } - - /* ascii char */ - else if (*ptr <= 0x7F) - { - buf_putchar(buf, (char)*ptr++); - } - - /* multi byte sequence */ - else - { - if (!(v = _validate_utf8(&ptr, l - o, buf))) - break; - - o += (v - 1); - } - } - - return buf_destroy(buf); -} - -char * striptags(const char *s, unsigned int l) -{ - struct template_buffer *buf = buf_init(l); - unsigned char *ptr = (unsigned char *)s; - unsigned char *end = ptr + l; - unsigned char *tag; - unsigned char prev; - char esq[8]; - int esl; - - for (prev = ' '; ptr < end; ptr++) - { - if ((*ptr == '<') && ((ptr + 2) < end) && - ((*(ptr + 1) == '/') || isalpha(*(ptr + 1)))) - { - for (tag = ptr; tag < end; tag++) - { - if (*tag == '>') - { - if (!isspace(prev)) - buf_putchar(buf, ' '); - - ptr = tag; - prev = ' '; - break; - } - } - } - else if (isspace(*ptr)) - { - if (!isspace(prev)) - buf_putchar(buf, *ptr); - - prev = *ptr; - } - else - { - switch(*ptr) - { - case '"': - case '\'': - case '<': - case '>': - case '&': - esl = snprintf(esq, sizeof(esq), "&#%i;", *ptr); - buf_append(buf, esq, esl); - break; - - default: - buf_putchar(buf, *ptr); - break; - } - - prev = *ptr; - } - } - - return buf_destroy(buf); -} - -void luastr_escape(struct template_buffer *out, const char *s, unsigned int l, - int escape_xml) -{ - int esl; - char esq[8]; - char *ptr; - - for (ptr = (char *)s; ptr < (s + l); ptr++) - { - switch (*ptr) - { - case '\\': - buf_append(out, "\\\\", 2); - break; - - case '"': - if (escape_xml) - buf_append(out, """, 5); - else - buf_append(out, "\\\"", 2); - break; - - case '\n': - buf_append(out, "\\n", 2); - break; - - case '\'': - case '&': - case '<': - case '>': - if (escape_xml) - { - esl = snprintf(esq, sizeof(esq), "&#%i;", *ptr); - buf_append(out, esq, esl); - break; - } - - default: - buf_putchar(out, *ptr); - } - } -} - -void luastr_translate(struct template_buffer *out, const char *s, unsigned int l, - int escape_xml) -{ - char *tr; - int trlen; - - switch (lmo_translate(s, l, &tr, &trlen)) - { - case 0: - luastr_escape(out, tr, trlen, escape_xml); - break; - - case -1: - luastr_escape(out, s, l, escape_xml); - break; - - default: - /* no catalog loaded */ - break; - } -} diff --git a/libs/web/src/template_utils.h b/libs/web/src/template_utils.h deleted file mode 100644 index c54af757c..000000000 --- a/libs/web/src/template_utils.h +++ /dev/null @@ -1,49 +0,0 @@ -/* - * LuCI Template - Utility header - * - * Copyright (C) 2010-2012 Jo-Philipp Wich <xm@subsignal.org> - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef _TEMPLATE_UTILS_H_ -#define _TEMPLATE_UTILS_H_ - -#include <stdlib.h> -#include <stdio.h> -#include <string.h> - - -/* buffer object */ -struct template_buffer { - char *data; - char *dptr; - unsigned int size; - unsigned int fill; -}; - -struct template_buffer * buf_init(int size); -int buf_grow(struct template_buffer *buf, int size); -int buf_putchar(struct template_buffer *buf, char c); -int buf_append(struct template_buffer *buf, const char *s, int len); -int buf_length(struct template_buffer *buf); -char * buf_destroy(struct template_buffer *buf); - -char * utf8(const char *s, unsigned int l); -char * pcdata(const char *s, unsigned int l); -char * striptags(const char *s, unsigned int l); - -void luastr_escape(struct template_buffer *out, const char *s, unsigned int l, int escape_xml); -void luastr_translate(struct template_buffer *out, const char *s, unsigned int l, int escape_xml); - -#endif diff --git a/libs/web/standalone.mk b/libs/web/standalone.mk deleted file mode 100644 index 66a0e5a2e..000000000 --- a/libs/web/standalone.mk +++ /dev/null @@ -1,56 +0,0 @@ -LUAC = luac -LUAC_OPTIONS = -s -LUA_TARGET ?= source - -LUA_MODULEDIR = /usr/local/share/lua/5.1 -LUA_LIBRARYDIR = /usr/local/lib/lua/5.1 - -OS ?= $(shell uname) - -LUA_SHLIBS = $(shell pkg-config --silence-errors --libs lua5.1 || pkg-config --silence-errors --libs lua-5.1 || pkg-config --silence-errors --libs lua) -LUA_LIBS = $(if $(LUA_SHLIBS),$(LUA_SHLIBS),$(firstword $(wildcard /usr/lib/liblua.a /usr/local/lib/liblua.a /opt/local/lib/liblua.a))) -LUA_CFLAGS = $(shell pkg-config --silence-errors --cflags lua5.1 || pkg-config --silence-errors --cflags lua-5.1 || pkg-config --silence-errors --cflags lua) - -CC = gcc -AR = ar -RANLIB = ranlib -CFLAGS = -O2 -FPIC = -fPIC -EXTRA_CFLAGS = --std=gnu99 -WFLAGS = -Wall -Werror -pedantic -CPPFLAGS = -COMPILE = $(CC) $(CPPFLAGS) $(CFLAGS) $(EXTRA_CFLAGS) $(WFLAGS) -ifeq ($(OS),Darwin) - SHLIB_FLAGS = -bundle -undefined dynamic_lookup -else - SHLIB_FLAGS = -shared -endif -LINK = $(CC) $(LDFLAGS) - -.PHONY: all build compile luacompile luasource clean luaclean - -all: build - -build: luabuild gccbuild - -luabuild: lua$(LUA_TARGET) - -gccbuild: compile -compile: - -clean: luaclean - -luasource: - mkdir -p dist$(LUA_MODULEDIR) - cp -pR root/* dist 2>/dev/null || true - cp -pR lua/* dist$(LUA_MODULEDIR) 2>/dev/null || true - for i in $$(find dist -name .svn); do rm -rf $$i || true; done - -luastrip: luasource - for i in $$(find dist -type f -name '*.lua'); do perl -e 'undef $$/; open( F, "< $$ARGV[0]" ) || die $$!; $$src = <F>; close F; $$src =~ s/--\[\[.*?\]\](--)?//gs; $$src =~ s/^\s*--.*?\n//gm; open( F, "> $$ARGV[0]" ) || die $$!; print F $$src; close F' $$i; done - -luacompile: luasource - for i in $$(find dist -name *.lua -not -name debug.lua); do $(LUAC) $(LUAC_OPTIONS) -o $$i $$i; done - -luaclean: - rm -rf dist |