+ 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;
+ 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