* Fixed last commit
[project/luci.git] / libs / web / luasrc / template.lua
index 369aa0a..a24c7ed 100644 (file)
@@ -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