X-Git-Url: https://git.archive.openwrt.org/?p=project%2Fluci.git;a=blobdiff_plain;f=libs%2Fweb%2Fluasrc%2Ftemplate.lua;h=a24c7ed5820501f77be18a01a5df6679d60f9eb7;hp=369aa0a30b4bac478aac61eb19151e0983e7e057;hb=92fc9cd62f680a36c02be7a9c1364cc215c7d10e;hpb=d35a620e9f5665a94967f4bd02c93581a1dd7e00 diff --git a/libs/web/luasrc/template.lua b/libs/web/luasrc/template.lua index 369aa0a30..a24c7ed58 100644 --- a/libs/web/luasrc/template.lua +++ b/libs/web/luasrc/template.lua @@ -30,22 +30,20 @@ require("luci.util") require("luci.fs") require("luci.http") -viewdir = luci.sys.libpath() .. "/view/" +luci.config.template = luci.config.template or {} + +viewdir = luci.config.template.viewdir or luci.sys.libpath() .. "/view" +compiledir = luci.config.template.compiledir or luci.sys.libpath() .. "/view" + +-- Enforce cache security +compiledir = compiledir .. "/" .. luci.sys.process.info("uid") -- Compile modes: -- none: Never compile, only use precompiled data from files -- memory: Always compile, do not save compiled files, ignore precompiled -- file: Compile on demand, save compiled files, update precompiled -compiler_mode = "memory" - - --- This applies to compiler modes "always" and "smart" --- --- Produce compiled lua code rather than lua sourcecode --- WARNING: Increases template size heavily!!! --- This produces the same bytecode as luac but does not have a strip option -compiler_enable_bytecode = false +compiler_mode = luci.config.template.compiler_mode or "memory" -- Define the namespace for template modules @@ -107,12 +105,7 @@ function compile(template) template = template:gsub("<%%"..tostring(k).."%%>", re) end - if compiler_enable_bytecode then - tf = loadstring(template) - template = string.dump(tf) - end - - return template + return loadstring(template) end -- Oldstyle render shortcut @@ -156,13 +149,18 @@ function Template.__init__(self, name) end -- Compile and build - local sourcefile = viewdir .. name .. ".htm" - local compiledfile = viewdir .. name .. ".lua" + local sourcefile = viewdir .. "/" .. name .. ".htm" + local compiledfile = compiledir .. "/" .. luci.http.urlencode(name) .. ".lua" local err if compiler_mode == "file" then local tplmt = luci.fs.mtime(sourcefile) local commt = luci.fs.mtime(compiledfile) + + if not luci.fs.mtime(compiledir) then + luci.fs.mkdir(compiledir, true) + luci.fs.chmod(luci.fs.dirname(compiledir), "a+rxw") + end -- Build if there is no compiled file or if compiled file is outdated if ((commt == nil) and not (tplmt == nil)) @@ -171,9 +169,10 @@ function Template.__init__(self, name) source, err = luci.fs.readfile(sourcefile) if source then - local compiled = compile(source) - luci.fs.writefile(compiledfile, compiled) - self.template, err = loadstring(compiled) + local compiled, err = compile(source) + + luci.fs.writefile(compiledfile, luci.util.dump(compiled)) + self.template = compiled end else self.template, err = loadfile(compiledfile) @@ -186,7 +185,7 @@ function Template.__init__(self, name) local source source, err = luci.fs.readfile(sourcefile) if source then - self.template, err = loadstring(compile(source)) + self.template, err = compile(source) end end