cgi-bin/loona.lua
author Timm S. Mueller <tmueller@neoscientists.org>
Sun, 11 Mar 2007 15:01:41 +0100
changeset 143 8495a4fdc479
parent 141 fc50232cf086
child 144 531eca7acb52
permissions -rw-r--r--
Added new variable ispubprofile
tmueller@0
     1
tmueller@0
     2
--
tmueller@0
     3
--	loona - tiny CMS
tmueller@0
     4
--	Written by Timm S. Mueller <tmueller at neoscientists.org>
tmueller@0
     5
--	See copyright notice in COPYRIGHT
tmueller@0
     6
--
tmueller@0
     7
tmueller@68
     8
local tek = require "tek"
tmueller@68
     9
local cgi = require "tek.cgi"
tmueller@68
    10
local posix = require "tek.posix"
tmueller@0
    11
require "tek.cgi.request"
tmueller@0
    12
require "tek.cgi.request.args"
tmueller@0
    13
require "tek.cgi.session"
tmueller@0
    14
require "tek.web"
tmueller@0
    15
require "tek.web.markup"
tmueller@0
    16
require "tek.util"
tmueller@0
    17
tmueller@23
    18
tmueller@0
    19
local boxed_G = { 
tmueller@0
    20
	string = string, table = table,
tmueller@0
    21
	assert = assert, collectgarbage = collectgarbage, dofile = dofile,
tmueller@0
    22
	error = error, getfenv = getfenv, getmetatable = getmetatable,
tmueller@0
    23
	ipairs = ipairs, load = load, loadfile = loadfile, loadstring = loadstring,
tmueller@0
    24
	next = next, pairs = pairs, pcall = pcall, print = print,
tmueller@0
    25
	rawequal = rawequal, rawget = rawget, rawset = rawset, require = require,
tmueller@0
    26
	select = select, setfenv = setfenv, setmetatable = setmetatable,
tmueller@0
    27
	tonumber = tonumber, tostring = tostring, type = type, unpack = unpack,
tmueller@0
    28
	xpcall = xpcall
tmueller@0
    29
}
tmueller@0
    30
tmueller@68
    31
local table, string, assert, unpack, ipairs, pairs, type, require =
tmueller@68
    32
	table, string, assert, unpack, ipairs, pairs, type, require
tmueller@0
    33
local setmetatable, setfenv, getfenv = setmetatable, setfenv, getfenv
tmueller@0
    34
local open, remove, rename, getenv, time =
tmueller@0
    35
	io.open, os.remove, os.rename, os.getenv, os.time
tmueller@0
    36
tmueller@0
    37
tmueller@0
    38
module "loona"
tmueller@0
    39
tmueller@0
    40
tmueller@129
    41
_VERSION = 3
tmueller@141
    42
_REVISION = 1
tmueller@0
    43
tmueller@0
    44
tmueller@20
    45
local function lookupname(tab, val)
tmueller@0
    46
	if tab then
tmueller@0
    47
		for i, v in ipairs(tab) do
tmueller@20
    48
			if v.name == val then
tmueller@0
    49
				return i
tmueller@0
    50
			end
tmueller@0
    51
		end
tmueller@0
    52
	end
tmueller@0
    53
end
tmueller@0
    54
tmueller@0
    55
tmueller@129
    56
local atom = { }
tmueller@20
    57
tmueller@129
    58
function atom:new(o)
tmueller@129
    59
	o = o or { }
tmueller@129
    60
	setmetatable(o, self)
tmueller@129
    61
	self.__index = self
tmueller@129
    62
	return o
tmueller@129
    63
end
tmueller@129
    64
tmueller@129
    65
local loona = atom:new(getfenv())
tmueller@129
    66
tmueller@129
    67
tmueller@129
    68
function loona:dbmsg(msg, detail)
tmueller@129
    69
 	return (msg and detail and self.authuser) and
tmueller@129
    70
 		("%s : %s"):format(msg, detail) or msg
tmueller@129
    71
end
tmueller@129
    72
tmueller@129
    73
tmueller@129
    74
function loona:checkprofilename(n)
tmueller@129
    75
	assert(n:match("^%w+$") and n ~= "current",
tmueller@129
    76
		self:dbmsg("Invalid profile name", n))
tmueller@129
    77
	return n
tmueller@129
    78
end
tmueller@129
    79
tmueller@129
    80
tmueller@129
    81
function loona:checkbodyname(s)
tmueller@129
    82
	s = s or "main"
tmueller@129
    83
	assert(s:match("^[%w_]*%w+[%w_]*$"), self:dbmsg("Invalid body name", s))
tmueller@129
    84
	return s
tmueller@129
    85
end
tmueller@129
    86
tmueller@129
    87
tmueller@129
    88
function loona:deleteprofile(p, lang)
tmueller@129
    89
	p = self.config.contentdir .. "/" .. p .. "_" .. (lang or self.lang)
tmueller@129
    90
	for e in tek.util.readdir(p) do
tmueller@129
    91
 		local success, msg = remove(p .. "/" .. e)
tmueller@129
    92
		assert(success, self:dbmsg("Error removing entry in profile", msg))
tmueller@129
    93
	end
tmueller@129
    94
	return remove(p)
tmueller@129
    95
end
tmueller@129
    96
tmueller@129
    97
tmueller@129
    98
function loona:copyprofile(newprofile, srcprofile, lang)
tmueller@129
    99
	srcprofile = srcprofile or self.profile
tmueller@129
   100
	lang = lang or self.lang
tmueller@129
   101
	local contentdir = self.config.contentdir
tmueller@143
   102
	local src = ("%s/%s_%s"):format(contentdir, srcprofile or self.profile, 
tmueller@143
   103
		lang)
tmueller@129
   104
	assert(posix.stat(src, "mode") == "directory",
tmueller@129
   105
		self:dbmsg("Not a directory", src))
tmueller@129
   106
	local dst = ("%s/%s_%s"):format(contentdir, newprofile, lang)
tmueller@129
   107
	local success, msg = posix.mkdir(dst)
tmueller@129
   108
	assert(success, self:dbmsg("Error creating profile directory", msg))
tmueller@129
   109
	for e in tek.util.readdir(src) do
tmueller@129
   110
		local ext = e:match("^[^.].*%.([^.]*)$")
tmueller@129
   111
		if ext ~= "LOCK" then
tmueller@129
   112
			local f = src .. "/" .. e
tmueller@129
   113
			if posix.stat(f, "mode") == "file" then
tmueller@129
   114
				success, msg = tek.copyfile(f, dst .. "/" .. e)
tmueller@129
   115
				assert(success, self:dbmsg("Error copying file", msg))
tmueller@129
   116
			end
tmueller@129
   117
		end
tmueller@129
   118
	end
tmueller@129
   119
end
tmueller@129
   120
tmueller@129
   121
tmueller@129
   122
function loona:publishprofile(profile, lang)
tmueller@129
   123
	lang = lang or self.lang
tmueller@129
   124
	local contentdir = self.config.contentdir
tmueller@129
   125
	local newpath = ("%s/current_%s"):format(contentdir, lang)
tmueller@129
   126
	local tmppath = newpath .. "." .. self.session.name
tmueller@129
   127
	local success, msg = posix.symlink(profile .. "_" .. lang, tmppath)
tmueller@129
   128
	assert(success, self:dbmsg("Cannot create symlink", msg))
tmueller@129
   129
	success, msg = rename(tmppath, newpath)
tmueller@129
   130
	assert(success, self:dbmsg("Cannot put symlink in place", msg))
tmueller@129
   131
	
tmueller@141
   132
	-- get languages of the current profile
tmueller@141
   133
	
tmueller@141
   134
	local plangs = { }
tmueller@141
   135
	local lmatch = "^" .. self.profile .. "_(%w+)$"
tmueller@141
   136
	for e in tek.util.readdir(self.config.contentdir) do
tmueller@141
   137
		local l = e:match(lmatch)
tmueller@141
   138
		if l then
tmueller@141
   139
			table.insert(plangs, l)
tmueller@141
   140
		end
tmueller@141
   141
	end
tmueller@141
   142
	
tmueller@129
   143
	-- Unroll site to static HTML
tmueller@129
   144
	
tmueller@141
   145
	for _, lang in ipairs(plangs) do
tmueller@141
   146
		local ext = (#plangs == 1 and ".html") or (".html." .. lang)
tmueller@141
   147
		self:recursesections(self.sections, function(self, s, e, path)
tmueller@141
   148
			path = path and path .. "/" .. e.name or e.name
tmueller@141
   149
			if not e.notvisible then
tmueller@143
   150
				loona:dumphtml { requestpath = path, requestlang = lang,
tmueller@143
   151
					htmlext = ext }
tmueller@141
   152
			end
tmueller@141
   153
			return path
tmueller@141
   154
		end)
tmueller@141
   155
	end
tmueller@131
   156
	
tmueller@131
   157
	-- Update file cache
tmueller@131
   158
tmueller@131
   159
	local htdocs = self.config.htdocsdir
tmueller@131
   160
	local cache = self.config.htmlcachedir
tmueller@131
   161
tmueller@131
   162
	for e in tek.util.readdir(cache) do
tmueller@141
   163
		local f = e:match("^.*%.html%.?(%w*)$")
tmueller@141
   164
		if f and f ~= "tmp" then
tmueller@131
   165
			local success, msg = remove(htdocs .. "/" .. e)
tmueller@131
   166
			success, msg = remove(cache .. "/" .. e)
tmueller@143
   167
 			assert(success,
tmueller@143
   168
 				self:dbmsg("Could not purge cached HTML file", msg))
tmueller@131
   169
		end
tmueller@131
   170
	end
tmueller@131
   171
	for e in tek.util.readdir(cache) do
tmueller@141
   172
		local f = e:match("^(.*%.html%.?%w*)%.tmp$")
tmueller@131
   173
		if f then
tmueller@131
   174
			local success, msg = rename(cache .. "/" .. e, cache .. "/" .. f)
tmueller@143
   175
			assert(success,
tmueller@143
   176
				self:dbmsg("Could not update cached HTML file", msg))
tmueller@131
   177
			success, msg = rename(htdocs .. "/" .. e, htdocs .. "/" .. f)
tmueller@143
   178
			assert(success,
tmueller@143
   179
				self:dbmsg("Could not update cached HTML file", msg))
tmueller@131
   180
		end
tmueller@131
   181
	end
tmueller@129
   182
end
tmueller@129
   183
tmueller@129
   184
tmueller@129
   185
function loona:recursesections(s, func, ...)
tmueller@0
   186
	for _, e in ipairs(s) do
tmueller@129
   187
		local udata = { func(self, s, e, unpack(arg)) }
tmueller@0
   188
		if e.subs then
tmueller@129
   189
			self:recursesections(e.subs, func, unpack(udata))
tmueller@0
   190
		end
tmueller@129
   191
	end
tmueller@129
   192
end
tmueller@129
   193
tmueller@129
   194
tmueller@129
   195
function loona:indexsections()
tmueller@129
   196
	self:recursesections(self.sections, function(self, s, e)
tmueller@129
   197
		e.notvalid = (not self.secure and e.secure) or 
tmueller@129
   198
			(not self.authuser and e.secret) or nil
tmueller@129
   199
		e.notvisible = e.notvalid or not self.authuser and e.hidden or nil
tmueller@16
   200
		s[e.name] = e
tmueller@129
   201
	end)
tmueller@0
   202
end
tmueller@0
   203
tmueller@0
   204
tmueller@20
   205
--	Decompose section path into a stack of sections, returning only up to
tmueller@0
   206
--	the last valid element in the path. additionally returns the table of
tmueller@0
   207
--	the last section path element (or the default section)
tmueller@0
   208
tmueller@129
   209
function loona:getsection(path)
tmueller@129
   210
	local default = not self.authuser and self.config.defname
tmueller@129
   211
	local tab = { { entries = self.sections, name = default } }
tmueller@129
   212
	local ss = self.sections
tmueller@0
   213
	local sectionpath
tmueller@129
   214
	(path or ""):gsub("(%w+)/?", function(a)
tmueller@27
   215
		if ss then
tmueller@27
   216
			local s = ss[a]
tmueller@20
   217
			if s and not s.notvalid then
tmueller@20
   218
				sectionpath = s
tmueller@0
   219
				tab[#tab].name = a
tmueller@27
   220
				ss = s.subs
tmueller@27
   221
				if ss then
tmueller@27
   222
					table.insert(tab, { entries = ss })
tmueller@0
   223
				end
tmueller@0
   224
			else
tmueller@27
   225
				ss = nil -- stop.
tmueller@0
   226
			end
tmueller@0
   227
		end
tmueller@0
   228
	end)
tmueller@129
   229
	if not self.section and not sectionpath then
tmueller@129
   230
		sectionpath = self.sections[default]
tmueller@0
   231
		if sectionpath then
tmueller@0
   232
			table.insert(tab, { entries = sectionpath.subs })
tmueller@0
   233
		end
tmueller@0
   234
	end
tmueller@0
   235
	return tab, sectionpath
tmueller@0
   236
end
tmueller@0
   237
tmueller@0
   238
tmueller@129
   239
function loona:getpath(delimiter)
tmueller@0
   240
	local t = { }
tmueller@129
   241
	for _, menu in ipairs(self.submenus) do
tmueller@0
   242
		if menu.name then
tmueller@0
   243
			table.insert(t, menu.name)
tmueller@0
   244
		end
tmueller@0
   245
	end
tmueller@0
   246
	return table.concat(t, delimiter or "/")
tmueller@0
   247
end
tmueller@0
   248
tmueller@0
   249
tmueller@129
   250
function loona:deletesection(fname, all_bodies)
tmueller@129
   251
	local fullname = self.contentdir .. "/" .. fname
tmueller@0
   252
	local success, msg = remove(fullname)
tmueller@129
   253
	if all_bodies then
tmueller@0
   254
		local pat = "^" .. 
tmueller@0
   255
			fname:gsub("%^%$%(%)%%%.%[%]%*%+%-%?", "%%%1") .. "%..*$"
tmueller@129
   256
		for e in tek.util.readdir(self.contentdir) do
tmueller@0
   257
			if e:match(pat) then
tmueller@129
   258
				remove(self.contentdir .. "/" .. e)
tmueller@0
   259
			end
tmueller@0
   260
		end
tmueller@0
   261
	end
tmueller@0
   262
	return success, msg
tmueller@0
   263
end
tmueller@0
   264
tmueller@0
   265
tmueller@129
   266
function loona:addpath(path, e)
tmueller@129
   267
	local tab = self.sections
tmueller@0
   268
	path:gsub("(%w+)/?", function(a)
tmueller@20
   269
		if tab then
tmueller@20
   270
			local s = tab[a]
tmueller@20
   271
			if s then
tmueller@20
   272
				if not s.subs then
tmueller@20
   273
					s.subs = { }
tmueller@0
   274
				end
tmueller@20
   275
				tab = s.subs
tmueller@0
   276
			else
tmueller@20
   277
				table.insert(tab, e)
tmueller@20
   278
				tab[a] = e
tmueller@20
   279
 				tab = nil -- stop
tmueller@0
   280
			end
tmueller@0
   281
		end
tmueller@0
   282
	end)
tmueller@129
   283
	return e
tmueller@0
   284
end
tmueller@0
   285
tmueller@0
   286
tmueller@129
   287
function loona:rmpath(path)
tmueller@20
   288
	local parent
tmueller@129
   289
	local tab = self.sections
tmueller@0
   290
	path:gsub("(%w+)/?", function(a)
tmueller@20
   291
		if tab then
tmueller@20
   292
			local idx = lookupname(tab, a)
tmueller@20
   293
			if idx then
tmueller@20
   294
				if tab[idx].subs then
tmueller@20
   295
					parent = tab[idx]
tmueller@20
   296
					tab = tab[idx].subs
tmueller@0
   297
				else
tmueller@20
   298
					table.remove(tab, idx)
tmueller@43
   299
					tab[a] = nil
tmueller@20
   300
					if #tab == 0 and parent then
tmueller@20
   301
						parent.subs = nil
tmueller@0
   302
					end
tmueller@20
   303
					tab = nil
tmueller@0
   304
				end
tmueller@0
   305
			end
tmueller@0
   306
		end
tmueller@0
   307
	end)
tmueller@0
   308
end
tmueller@0
   309
tmueller@0
   310
tmueller@129
   311
function loona:checkpath(path)
tmueller@129
   312
	if path ~= "index" then -- "index" is reserved
tmueller@129
   313
		local res, idx
tmueller@129
   314
		local tab = self.sections
tmueller@129
   315
		path:gsub("(%w+)/?", function(a)
tmueller@129
   316
			if tab then
tmueller@129
   317
				local i = lookupname(tab, a)
tmueller@129
   318
				if i then
tmueller@129
   319
					res, idx = tab, i
tmueller@129
   320
					tab = tab[i].subs
tmueller@129
   321
				else
tmueller@129
   322
					res, idx = nil, nil
tmueller@129
   323
				end
tmueller@129
   324
			end
tmueller@129
   325
		end)
tmueller@129
   326
		return res, idx
tmueller@129
   327
	end
tmueller@8
   328
end
tmueller@8
   329
tmueller@8
   330
tmueller@129
   331
function loona:title()
tmueller@129
   332
	return self.section and (self.section.title or self.section.label or
tmueller@129
   333
		self.section.name) or ""
tmueller@0
   334
end
tmueller@0
   335
tmueller@0
   336
tmueller@0
   337
--	Run a site function snippet, with full error recovery
tmueller@0
   338
--	(also recovers from errors in error handling function)
tmueller@0
   339
tmueller@129
   340
function loona:dosnippet(func, errfunc)
tmueller@0
   341
	local ret = { tek.catch(func) }
tmueller@0
   342
	if ret[1] == 0 or (errfunc and tek.catch(errfunc) == 0) then
tmueller@0
   343
		return unpack(ret)
tmueller@0
   344
	end
tmueller@129
   345
	self:out("<h2>Error</h2>")
tmueller@129
   346
	self:out("<h3>" .. self:encodeform(ret[2] or "") .. "</h3>")
tmueller@129
   347
	if self.authuser then
tmueller@0
   348
		if type(ret[3]) == "string" then
tmueller@129
   349
			self:out("<p>" .. self:encodeform(ret[3]) .. "</p>")
tmueller@0
   350
		end
tmueller@129
   351
		if ret[4] then
tmueller@129
   352
			self:out("<pre>" .. self:encodeform(ret[4]) .. "</pre>")
tmueller@0
   353
		end
tmueller@0
   354
	end
tmueller@0
   355
end	
tmueller@0
   356
tmueller@0
   357
tmueller@129
   358
function loona:lockfile(file)
tmueller@129
   359
	return not self.session and true or 
tmueller@129
   360
		posix.symlink(self.session.filename, file .. ".LOCK")
tmueller@0
   361
end
tmueller@0
   362
tmueller@0
   363
tmueller@129
   364
function loona:unlockfile(file)
tmueller@129
   365
	return not self.session and true or remove(file .. ".LOCK")
tmueller@0
   366
end
tmueller@0
   367
tmueller@0
   368
tmueller@129
   369
function loona:saveindex()
tmueller@129
   370
	local tempname = self.indexfname .. "." .. self.session.name
tmueller@124
   371
	local f, msg = open(tempname, "wb")
tmueller@129
   372
	assert(f, self:dbmsg("Error opening section file for writing", msg))
tmueller@129
   373
	tek.dump(self.sections, function(...)
tmueller@124
   374
		f:write(unpack(arg))
tmueller@124
   375
	end)
tmueller@124
   376
	f:close()
tmueller@129
   377
	local success, msg = rename(tempname, self.indexfname)
tmueller@129
   378
	assert(success, self:dbmsg("Error renaming section file", msg))
tmueller@124
   379
end
tmueller@124
   380
tmueller@124
   381
tmueller@129
   382
function loona:savebody(fname, content)
tmueller@129
   383
	fname = self.contentdir .. "/" .. fname
tmueller@0
   384
	local f, msg = open(fname, "wb")
tmueller@129
   385
	assert(f, self:dbmsg("Could not open file for writing", msg))
tmueller@0
   386
	f:write(content or "")
tmueller@0
   387
	f:close()
tmueller@0
   388
end
tmueller@0
   389
tmueller@23
   390
tmueller@129
   391
function loona:runboxed(func, envitems, ...)
tmueller@129
   392
	local fenv = {
tmueller@129
   393
 		arg = arg,
tmueller@129
   394
 		loona = self
tmueller@129
   395
 	}
tmueller@129
   396
 	if envitems then
tmueller@129
   397
	 	for k, v in pairs(envitems) do
tmueller@129
   398
 			fenv[k] = v
tmueller@129
   399
 		end
tmueller@129
   400
 	end
tmueller@129
   401
 	setmetatable(fenv, { __index = boxed_G }) -- boxed global environment
tmueller@129
   402
	setfenv(func, fenv)
tmueller@129
   403
	return func()
tmueller@129
   404
end
tmueller@0
   405
tmueller@129
   406
tmueller@129
   407
function loona:include(fname, ...)
tmueller@129
   408
	assert(not fname:match("%W"), self:dbmsg("Invalid include name", fname))
tmueller@129
   409
	local fname2 = ("%s/%s.lua"):format(self.config.extdir, fname)
tmueller@0
   410
	local f, msg = open(fname2)
tmueller@129
   411
	assert(f, self:dbmsg("Cannot open file", msg))
tmueller@129
   412
	local parsed, msg = self:loadhtml(f, "loona:out", fname2)
tmueller@129
   413
	assert(parsed, self:dbmsg("Syntax error", msg))
tmueller@129
   414
	return self:runboxed(parsed)
tmueller@0
   415
end
tmueller@0
   416
tmueller@0
   417
tmueller@68
   418
--	produce link target, propagate lang, profile, session
tmueller@68
   419
tmueller@129
   420
function loona:href(section, ...)
tmueller@129
   421
	local target = self:getdocname(section)
tmueller@129
   422
-- 	if self.session or self.profile ~= self.pubprofile then
tmueller@129
   423
	if self.session then
tmueller@68
   424
		return tek.web.gethref(target, { "profile", "session", "lang", 
tmueller@68
   425
			unpack(arg) })
tmueller@0
   426
	end
tmueller@68
   427
	return tek.web.gethref(target, { "lang", unpack(arg) })
tmueller@0
   428
end
tmueller@0
   429
tmueller@129
   430
function loona:ilink(target, text, extra)
tmueller@68
   431
	return ('<a href="%s"%s>%s</a>'):format(target, extra or "", text)
tmueller@0
   432
end
tmueller@0
   433
tmueller@68
   434
--	internal link, propagation of lang, profile, session
tmueller@0
   435
tmueller@129
   436
function loona:link(section, text, ...)
tmueller@129
   437
	return self:ilink(self:href(section, unpack(arg)), text or section)
tmueller@0
   438
end
tmueller@0
   439
tmueller@68
   440
--	external link (opens in a new window), no argument propagation
tmueller@0
   441
tmueller@129
   442
function loona:elink(target, text)
tmueller@143
   443
	return self:ilink(target, text or target,
tmueller@143
   444
		not self.config.extlinksamewindow and
tmueller@83
   445
		' class="extlink" onclick="void(window.open(this.href, \'\', \'\')); return false;"')
tmueller@0
   446
end
tmueller@0
   447
tmueller@68
   448
--	plain link, no argument propagation
tmueller@68
   449
tmueller@129
   450
function loona:plink(section, text, ...)
tmueller@129
   451
	return self:ilink(tek.web.gethref(section, arg), text or section)
tmueller@68
   452
end
tmueller@68
   453
tmueller@68
   454
--	user interface link, propagation of lang, profile, session
tmueller@68
   455
tmueller@129
   456
function loona:uilink(section, text, ...)
tmueller@129
   457
	return self:ilink(self:href(section, unpack(arg)), text or section)
tmueller@68
   458
end
tmueller@68
   459
tmueller@68
   460
--	produce a hidden input value in forms
tmueller@0
   461
tmueller@129
   462
function loona:hidden(name, value)
tmueller@68
   463
	return not value and "" or
tmueller@68
   464
		('<input type="hidden" name="%s" value="%s" />'):format(name, value)
tmueller@0
   465
end
tmueller@0
   466
tmueller@0
   467
tmueller@129
   468
function loona:getprofiles(lang)
tmueller@129
   469
	lang = lang or self.lang
tmueller@129
   470
	local dir = self.config.contentdir
tmueller@0
   471
	local t = { }
tmueller@129
   472
	for f in tek.util.readdir(dir) do
tmueller@129
   473
		if posix.lstat(dir .. "/" .. f, "mode") == "directory" then
tmueller@0
   474
			local e = f:match("^(%w+)_" .. lang .. "$")
tmueller@0
   475
 			if e then
tmueller@0
   476
	 			t[e] = e
tmueller@0
   477
 	 		end
tmueller@0
   478
		end
tmueller@0
   479
	end
tmueller@0
   480
	return t
tmueller@0
   481
end
tmueller@0
   482
tmueller@0
   483
tmueller@80
   484
--	Functions to produce a hierarchical navigation menu
tmueller@80
   485
tmueller@129
   486
local newent = { name = "new", label = "[+]", action="actionnew=true" }
tmueller@124
   487
tmueller@143
   488
function loona:rmenu(level, linkf, path, addnew)
tmueller@143
   489
	local sub = (addnew and level == #self.submenus + 1) and
tmueller@129
   490
		{ name = "new", entries = { }} or self.submenus[level]
tmueller@124
   491
 	if sub and sub.entries then
tmueller@80
   492
		local visible = { }
tmueller@80
   493
		for _, e in ipairs(sub.entries) do
tmueller@80
   494
			if not e.notvisible then
tmueller@80
   495
				table.insert(visible, e)
tmueller@80
   496
			end
tmueller@80
   497
		end
tmueller@143
   498
		if addnew then
tmueller@124
   499
			table.insert(visible, newent)
tmueller@124
   500
		end
tmueller@80
   501
		if #visible > 0 then
tmueller@129
   502
			self:out('<ul id="menulevel' .. level .. '">\n')
tmueller@80
   503
			for _, e in ipairs(visible) do
tmueller@129
   504
				local label = self:encodeform(e.label or e.name)
tmueller@80
   505
				local newpath = path and path .. "/" .. e.name or e.name
tmueller@80
   506
				local active = (e.name == sub.name)
tmueller@129
   507
				self:out('<li>\n')
tmueller@129
   508
				linkf(self, newpath, label, active, e.action)
tmueller@80
   509
				if active then
tmueller@129
   510
					self:rmenu(level + 1, linkf, newpath)
tmueller@80
   511
				end
tmueller@129
   512
				self:out('</li>\n')
tmueller@80
   513
			end
tmueller@129
   514
			self:out('</ul>\n')
tmueller@80
   515
		end
tmueller@80
   516
	end
tmueller@80
   517
end
tmueller@80
   518
tmueller@129
   519
function loona:menulink(path, label, active, ...)
tmueller@143
   520
	self:out(('<a %shref="%s">%s</a>\n'):format(active and 
tmueller@143
   521
		'class="active" ' or "", self:href(path, unpack(arg)), label))
tmueller@80
   522
end
tmueller@80
   523
tmueller@129
   524
function loona:menu(level, linkf)
tmueller@143
   525
	local addnew = self.authuser and not self.ispubprofile
tmueller@143
   526
	self:rmenu(level or 1, linkf or menulink, nil, addnew)
tmueller@80
   527
end
tmueller@80
   528
tmueller@80
   529
tmueller@129
   530
function loona:init()
tmueller@0
   531
	
tmueller@0
   532
	-- get list of languages, in order of preference
tmueller@141
   533
	-- TODO: respect quality parameter, not just order
tmueller@0
   534
	
tmueller@141
   535
	local l = self.requestlang or self.args.lang
tmueller@141
   536
	self.langs = { l and l:match("^%w+$") }
tmueller@129
   537
	if self.config.browserlang then
tmueller@0
   538
		local s = getenv("HTTP_ACCEPT_LANGUAGE")
tmueller@0
   539
		while s do
tmueller@0
   540
			local l, r = s:match("^([%w.=]+)[,;](.*)$")
tmueller@0
   541
			l = l or s
tmueller@0
   542
			s = r
tmueller@0
   543
			if l:match("^%w+$") then
tmueller@129
   544
				table.insert(self.langs, l)
tmueller@0
   545
			end
tmueller@0
   546
		end
tmueller@0
   547
	end
tmueller@129
   548
	table.insert(self.langs, self.config.deflang)
tmueller@0
   549
	
tmueller@0
   550
	-- get list of possible profiles
tmueller@0
   551
	
tmueller@0
   552
	local profiles = { }
tmueller@129
   553
	for e in tek.util.readdir(self.config.contentdir) do
tmueller@0
   554
		profiles[e] = e
tmueller@0
   555
	end
tmueller@0
   556
	
tmueller@0
   557
	-- get pubprofile
tmueller@0
   558
	
tmueller@129
   559
	for _, lang in ipairs(self.langs) do
tmueller@129
   560
		local p = posix.readlink(self.config.contentdir .. "/current_" .. lang)
tmueller@0
   561
		p = p and p:match("^(%w+)_" .. lang .. "$")
tmueller@0
   562
		if p then
tmueller@129
   563
			self.pubprofile = p
tmueller@0
   564
			break
tmueller@0
   565
		end
tmueller@0
   566
	end
tmueller@0
   567
	
tmueller@0
   568
	-- get profile
tmueller@0
   569
	
tmueller@143
   570
	local checkprofile =
tmueller@143
   571
		self.authuser and self.args.profile or self.pubprofile or "default"
tmueller@129
   572
	for _, lang in ipairs(self.langs) do
tmueller@129
   573
		if profiles[checkprofile .. "_" .. lang] then
tmueller@129
   574
			self.profile = checkprofile
tmueller@129
   575
			self.lang = lang
tmueller@0
   576
			break
tmueller@0
   577
		end
tmueller@0
   578
	end
tmueller@0
   579
	
tmueller@129
   580
	assert(self.profile and self.lang, "Invalid profile or language")
tmueller@0
   581
	
tmueller@47
   582
	
tmueller@143
   583
	self.ispubprofile = self.profile == self.pubprofile
tmueller@143
   584
	
tmueller@129
   585
	-- write back language and profile
tmueller@0
   586
tmueller@129
   587
	self.args.lang = self.lang ~= self.config.deflang and self.lang or nil
tmueller@129
   588
	self.args.profile = self.profile
tmueller@0
   589
	
tmueller@47
   590
	
tmueller@0
   591
	-- determine content directory pathname and section filename
tmueller@0
   592
	
tmueller@143
   593
	self.contentdir =
tmueller@143
   594
		("%s/%s_%s"):format(self.config.contentdir, self.profile, self.lang)
tmueller@129
   595
 	self.indexfname = self.contentdir .. "/.sections"
tmueller@0
   596
	
tmueller@0
   597
	-- load sections
tmueller@0
   598
	
tmueller@129
   599
 	self.sections = tek.source(self.indexfname)
tmueller@0
   600
	
tmueller@20
   601
	-- index sections, determine visibility in menu
tmueller@0
   602
	
tmueller@129
   603
	self:indexsections()
tmueller@0
   604
	
tmueller@129
   605
	-- decompose request path, produce a stack of sections
tmueller@0
   606
	
tmueller@129
   607
	self.submenus, self.section = self:getsection(self.requestpath)
tmueller@0
   608
tmueller@0
   609
	-- handle redirects if not logged on
tmueller@0
   610
	
tmueller@129
   611
	if not self.authuser and self.section and self.section.redirect then
tmueller@129
   612
		self.submenus, self.section = self:getsection(self.section.redirect)
tmueller@0
   613
	end
tmueller@0
   614
			
tmueller@0
   615
	-- section path and document name (refined)
tmueller@0
   616
	
tmueller@129
   617
	self.sectionpath = self:getpath()
tmueller@129
   618
	self.sectionname = self:getpath("_")
tmueller@0
   619
tmueller@0
   620
end
tmueller@0
   621
tmueller@0
   622
tmueller@129
   623
function loona:handlechanges()
tmueller@129
   624
	
tmueller@129
   625
	local save
tmueller@0
   626
tmueller@129
   627
	if self.args.editkey == "main" then
tmueller@23
   628
		
tmueller@23
   629
		-- In main editable section:
tmueller@23
   630
		
tmueller@129
   631
		if self.args.actioncreate then
tmueller@129
   632
			
tmueller@45
   633
			-- Create new section
tmueller@129
   634
			
tmueller@129
   635
			local editname = self.args.editname:lower()
tmueller@20
   636
			assert(not editname:match("%W"),
tmueller@47
   637
				dbmsg("Invalid section name", editname))
tmueller@20
   638
			if not (section and (section.subs or section)[editname]) then
tmueller@143
   639
				local newpath = (self.sectionpath and 
tmueller@143
   640
					(self.sectionpath .. "/")) .. editname
tmueller@129
   641
				local s = self:addpath(newpath, { name = editname,
tmueller@143
   642
					label = self.args.editlabel ~= "" and
tmueller@143
   643
						self.args.editlabel or nil,
tmueller@143
   644
					title = self.args.edittitle ~= "" and
tmueller@143
   645
						self.args.edittitle or nil,
tmueller@143
   646
					redirect = self.args.editredirect ~= "" and
tmueller@143
   647
						self.args.editredirect or nil,
tmueller@129
   648
					hidden = self.args.editvisibility and true,
tmueller@129
   649
					secret = self.args.editsecrecy and true,
tmueller@129
   650
					secure = self.args.editsecure and true,
tmueller@129
   651
					creator = self.authuser,
tmueller@20
   652
					creationdate = time() })
tmueller@23
   653
				save = true
tmueller@0
   654
			end
tmueller@20
   655
		
tmueller@129
   656
		elseif self.args.actionsave then
tmueller@129
   657
			
tmueller@45
   658
			-- Save section
tmueller@129
   659
			
tmueller@129
   660
			self.section.revisiondate = time()
tmueller@129
   661
			self.section.revisioner = self.authuser
tmueller@129
   662
			save = true
tmueller@129
   663
 		
tmueller@129
   664
		elseif self.args.actionsaveprops then
tmueller@129
   665
			
tmueller@129
   666
			-- Save properties
tmueller@129
   667
			
tmueller@129
   668
			self.section.hidden = self.args.editvisibility and true
tmueller@129
   669
			self.section.secret = self.args.editsecrecy and true
tmueller@129
   670
			self.section.secure = self.args.editsecure and true
tmueller@143
   671
			self.section.label = self.args.editlabel ~= "" and
tmueller@143
   672
				self.args.editlabel or nil
tmueller@143
   673
			self.section.title = self.args.edittitle ~= "" and
tmueller@143
   674
				self.args.edittitle or nil
tmueller@129
   675
			self.section.redirect =
tmueller@129
   676
				self.args.editredirect ~= "" and self.args.editredirect or nil
tmueller@23
   677
			save = true
tmueller@20
   678
		
tmueller@129
   679
		elseif self.args.actionup then
tmueller@129
   680
			
tmueller@45
   681
			-- Move section up
tmueller@129
   682
			
tmueller@129
   683
			local t, i = self:checkpath(self.sectionpath)
tmueller@0
   684
			if t and i > 1 then
tmueller@143
   685
				if self.ispubprofile and not self.args.actionconfirm then
tmueller@115
   686
					useralert = {
tmueller@129
   687
						text = self.locale.ALERT_MOVE_SECTION_IN_PUBLISHED_PROFILE,
tmueller@115
   688
						confirm =
tmueller@115
   689
							'<input type="submit" name="actionup" value="' .. 
tmueller@129
   690
							self.locale.MOVE .. '" /> ' ..
tmueller@129
   691
							self:hidden("actionconfirm", "true")
tmueller@115
   692
					}
tmueller@115
   693
				else
tmueller@115
   694
					local item = table.remove(t, i)
tmueller@115
   695
					table.insert(t, i - 1, item)
tmueller@115
   696
					save = true
tmueller@115
   697
				end
tmueller@0
   698
			end
tmueller@20
   699
		
tmueller@129
   700
		elseif self.args.actiondown then
tmueller@129
   701
			
tmueller@45
   702
			-- Move section down
tmueller@129
   703
			
tmueller@129
   704
			local t, i = self:checkpath(self.sectionpath)
tmueller@0
   705
			if t and i < #t then
tmueller@143
   706
				if self.ispubprofile and not self.args.actionconfirm then
tmueller@115
   707
					useralert = {
tmueller@129
   708
						text = self.locale.ALERT_MOVE_SECTION_IN_PUBLISHED_PROFILE,
tmueller@115
   709
						confirm =
tmueller@115
   710
							'<input type="submit" name="actiondown" value="' .. 
tmueller@129
   711
							self.locale.MOVE .. '" /> ' ..
tmueller@129
   712
							self:hidden("actionconfirm", "true")
tmueller@115
   713
					}
tmueller@115
   714
				else
tmueller@115
   715
					local item = table.remove(t, i)
tmueller@115
   716
					table.insert(t, i + 1, item)
tmueller@115
   717
					save = true
tmueller@115
   718
				end
tmueller@0
   719
			end
tmueller@20
   720
		
tmueller@129
   721
		elseif self.args.actioncreateprofile and self.args.createprofile then
tmueller@129
   722
			
tmueller@23
   723
			-- Create profile
tmueller@129
   724
			
tmueller@129
   725
			local c = self:checkprofilename(self.args.createprofile:lower())
tmueller@0
   726
			if c == profile then
tmueller@129
   727
				useralert = { text = self.locale.ALERT_CANNOT_COPY_PROFILE_TO_SELF }
tmueller@0
   728
			else
tmueller@129
   729
				local profiles = self:getprofiles()
tmueller@129
   730
				if profiles[c] and not self.args.actionconfirm then
tmueller@20
   731
					useralert = {
tmueller@129
   732
						text = c == self.pubprofile and 
tmueller@129
   733
							self.locale.ALERT_OVERWRITE_PUBLISHED_PROFILE or
tmueller@129
   734
							self.locale.ALERT_OVERWRITE_EXISTING_PROFILE,
tmueller@0
   735
						confirm =
tmueller@20
   736
							'<input type="submit" name="actioncreateprofile" value="' .. 
tmueller@129
   737
							self.locale.OVERWRITE .. '" /> ' ..
tmueller@129
   738
							self:hidden("actionconfirm", "true") .. 
tmueller@129
   739
							self:hidden("createprofile", c)
tmueller@20
   740
					}
tmueller@0
   741
				else
tmueller@0
   742
					if profiles[c] then
tmueller@129
   743
						self:deleteprofile(c)
tmueller@0
   744
					end
tmueller@129
   745
					self:copyprofile(c)
tmueller@0
   746
				end
tmueller@0
   747
			end
tmueller@20
   748
		
tmueller@129
   749
		elseif self.args.actiondeleteprofile and self.args.deleteprofile then
tmueller@129
   750
			
tmueller@23
   751
			-- Delete profile
tmueller@129
   752
			
tmueller@129
   753
			local c = self:checkprofilename(self.args.deleteprofile:lower())
tmueller@129
   754
			assert(c ~= self.pubprofile, self:dbmsg("Cannot delete published profile", c))
tmueller@129
   755
			if self.args.actionconfirm then
tmueller@129
   756
				self:deleteprofile(c)
tmueller@129
   757
				self.profile = nil
tmueller@129
   758
				self.args.profile = nil
tmueller@129
   759
				self:init()
tmueller@23
   760
				save = true
tmueller@0
   761
			else
tmueller@20
   762
				useralert = { 
tmueller@129
   763
					text = self.locale.ALERT_DELETE_PROFILE,
tmueller@20
   764
					confirm = 
tmueller@20
   765
						'<input type="submit" name="actiondeleteprofile" value="' .. 
tmueller@129
   766
						self.locale.DELETE .. '" /> ' ..
tmueller@129
   767
						self:hidden("actionconfirm", "true") ..
tmueller@129
   768
						self:hidden("deleteprofile", c)
tmueller@20
   769
				}
tmueller@0
   770
			end
tmueller@20
   771
		
tmueller@129
   772
		elseif self.args.actionchangeprofile and self.args.changeprofile then
tmueller@129
   773
			
tmueller@23
   774
			-- Change profile
tmueller@129
   775
			
tmueller@129
   776
			local c = self:checkprofilename(self.args.changeprofile:lower())
tmueller@129
   777
			self.profile = c
tmueller@129
   778
			self.args.profile = c
tmueller@23
   779
			save = true
tmueller@20
   780
		
tmueller@129
   781
		elseif self.args.actionpublishprofile and self.args.publishprofile then
tmueller@129
   782
			
tmueller@23
   783
			-- Publish profile
tmueller@129
   784
			
tmueller@129
   785
			local c = self:checkprofilename(self.args.publishprofile:lower())
tmueller@129
   786
			if c ~= self.publicprofile then
tmueller@129
   787
				if self.args.actionconfirm then
tmueller@129
   788
					self:publishprofile(c)
tmueller@23
   789
					save = true
tmueller@0
   790
				else
tmueller@20
   791
					useralert = {
tmueller@129
   792
						text = self.locale.ALERT_PUBLISH_PROFILE,
tmueller@20
   793
						confirm =
tmueller@20
   794
							'<input type="submit" name="actionpublishprofile" value="' ..
tmueller@129
   795
							self.locale.PUBLISH .. '" /> ' ..
tmueller@129
   796
							self:hidden("actionconfirm", "true") ..
tmueller@129
   797
							self:hidden("publishprofile", c)
tmueller@20
   798
					}
tmueller@20
   799
				end
tmueller@20
   800
			end
tmueller@0
   801
		end
tmueller@0
   802
		
tmueller@129
   803
	end
tmueller@0
   804
	
tmueller@129
   805
	if self.args.actiondelete then
tmueller@129
   806
		
tmueller@129
   807
		-- Delete section
tmueller@129
   808
		
tmueller@129
   809
		if not self.args.actionconfirm then
tmueller@129
   810
			useralert = {
tmueller@143
   811
				text = self.ispubprofile and
tmueller@129
   812
					self.locale.ALERT_DELETE_SECTION_IN_PUBLISHED_PROFILE or
tmueller@129
   813
					self.locale.ALERT_DELETE_SECTION,
tmueller@129
   814
				confirm =
tmueller@129
   815
					'<input type="submit" name="actiondelete" value="' .. 
tmueller@129
   816
					self.locale.DELETE .. '" /> ' ..
tmueller@129
   817
					self:hidden("actionconfirm", "true")
tmueller@129
   818
			}
tmueller@129
   819
		else
tmueller@129
   820
			local key = self.args.editkey
tmueller@129
   821
			if key == "main" and not self.section.subs then
tmueller@129
   822
				self:deletesection(self.sectionname, true) -- all bodies
tmueller@129
   823
				self:rmpath(self.sectionpath) -- and node
tmueller@129
   824
			else
tmueller@129
   825
				local ext = (key == "main" and "") or "." .. key
tmueller@129
   826
				self:deletesection(self.sectionname .. ext) -- only text
tmueller@129
   827
				if self.section.dynamic then
tmueller@129
   828
					self.section.dynamic[key] = nil
tmueller@131
   829
					local n = 0
tmueller@131
   830
					for _ in pairs(self.section.dynamic) do
tmueller@131
   831
						n = n + 1
tmueller@131
   832
					end
tmueller@131
   833
					if n == 0 then
tmueller@131
   834
						self.section.dynamic = nil
tmueller@131
   835
					end
tmueller@129
   836
				end
tmueller@129
   837
			end
tmueller@129
   838
			save = true
tmueller@0
   839
		end
tmueller@0
   840
	end
tmueller@129
   841
		
tmueller@129
   842
	if save then
tmueller@129
   843
		self:saveindex()
tmueller@129
   844
		self:init()
tmueller@129
   845
	end
tmueller@129
   846
	
tmueller@0
   847
end
tmueller@0
   848
tmueller@0
   849
tmueller@129
   850
function loona:encodeform(s)
tmueller@129
   851
	return cgi.encodeform(s)
tmueller@129
   852
end
tmueller@0
   853
tmueller@80
   854
tmueller@129
   855
function loona:loadhtml(src, outfunc, chunkname)
tmueller@129
   856
	return tek.web.include.load(src, outfunc, chunkname)
tmueller@129
   857
end
tmueller@80
   858
tmueller@80
   859
tmueller@129
   860
function loona:domarkup(s)
tmueller@129
   861
	return tek.web.markup.main(s)
tmueller@129
   862
end
tmueller@0
   863
tmueller@0
   864
tmueller@129
   865
function loona:expire(dir, pat, maxage)
tmueller@129
   866
	return tek.util.expire(dir, pat, maxage)
tmueller@129
   867
end
tmueller@129
   868
tmueller@129
   869
--
tmueller@129
   870
--	Get pathname of an existing content file that
tmueller@129
   871
--	the current path is determined by or defaults to
tmueller@129
   872
--
tmueller@129
   873
tmueller@129
   874
function loona:getsectionpath(bodyname, requestpath)
tmueller@129
   875
	local ext = (not bodyname or bodyname == "main") and "" or "." .. bodyname
tmueller@129
   876
	local t, path, section = { }
tmueller@129
   877
	for _, menu in ipairs(self.submenus) do
tmueller@129
   878
		if menu.entries and menu.entries[menu.name] then
tmueller@129
   879
			table.insert(t, menu.name)
tmueller@129
   880
			local fn = table.concat(t, "_")
tmueller@129
   881
			if posix.stat(self.contentdir .. "/" .. fn .. ext, "mode") == "file" then
tmueller@129
   882
				path, section = fn, menu
tmueller@129
   883
			end
tmueller@0
   884
		end
tmueller@0
   885
	end
tmueller@129
   886
	return path, ext, section
tmueller@0
   887
end
tmueller@0
   888
tmueller@20
   889
tmueller@129
   890
function loona:body(name)
tmueller@129
   891
	name = self:checkbodyname(name)
tmueller@129
   892
	local path, ext = self:getsectionpath(name)
tmueller@129
   893
	self:dosnippet(self.editable(name, path and path .. ext, self.sectionname .. ext))
tmueller@0
   894
end
tmueller@0
   895
tmueller@23
   896
tmueller@129
   897
function loona:new(o)
tmueller@0
   898
tmueller@129
   899
	local parsed, msg
tmueller@129
   900
	
tmueller@129
   901
	o = o or { }
tmueller@129
   902
	o = atom.new(self, o)
tmueller@129
   903
	
tmueller@129
   904
	o.out = o.out or function(self, s) tek.web.out(s) end
tmueller@129
   905
	o.setheader = o.setheader or function(self, s) tek.web.setheader(s) end
tmueller@129
   906
	
tmueller@129
   907
	-- Get configuration
tmueller@129
   908
	
tmueller@129
   909
	o.config = o.config or tek.source(o.conffile or "../etc/config.lua") or { }
tmueller@129
   910
	o.config.defname = o.config.defname or "home"
tmueller@129
   911
	o.config.deflang = o.config.deflang or "en"
tmueller@129
   912
	o.config.sessionmaxage = o.config.sessionmaxage or 600
tmueller@129
   913
	o.config.secureport = o.config.secureport or 443
tmueller@129
   914
	o.config.passwdfile = posix.abspath(o.config.passwdfile or "../etc/passwd.lua")
tmueller@129
   915
	o.config.sessiondir = posix.abspath(o.config.sessiondir or "../var/sessions")
tmueller@129
   916
	o.config.extdir = posix.abspath(o.config.extdir or "../extensions")
tmueller@129
   917
	o.config.contentdir = posix.abspath(o.config.contentdir or "../content")
tmueller@129
   918
	o.config.localedir = posix.abspath(o.config.localedir or "../locale")
tmueller@129
   919
	o.config.htdocsdir = posix.abspath(o.config.htdocsdir or "../htdocs")
tmueller@131
   920
	o.config.htmlcachedir = posix.abspath(o.config.htmlcachedir or "../var/htmlcache")
tmueller@129
   921
	
tmueller@129
   922
	-- Create proxy for on-demand loading of locales
tmueller@129
   923
	
tmueller@129
   924
	o.locale = { }
tmueller@129
   925
	local locmt = { }
tmueller@129
   926
	locmt.__index = function(_, key)
tmueller@129
   927
		for _, l in ipairs(o.langs) do
tmueller@129
   928
			locmt.__locale = tek.source(o.config.localedir .. "/" .. l)
tmueller@129
   929
			if locmt.__locale then
tmueller@129
   930
				break
tmueller@129
   931
			end
tmueller@129
   932
		end
tmueller@129
   933
		locmt.__index = function(tab, key)
tmueller@129
   934
			return locmt.__locale[key] or key
tmueller@129
   935
		end
tmueller@129
   936
		return locmt.__locale[key] or key
tmueller@129
   937
	end
tmueller@129
   938
	setmetatable(o.locale, locmt)
tmueller@129
   939
	
tmueller@129
   940
	-- Get request, args, document, script name, request path
tmueller@129
   941
	
tmueller@129
   942
	o.request = o.request or cgi.request
tmueller@129
   943
	o.args = o.args or cgi.request.args
tmueller@129
   944
	o.session = o.session or cgi.session
tmueller@129
   945
 	
tmueller@129
   946
 	o.scriptpath = o.scriptpath or cgi.document.Path
tmueller@129
   947
	o.requesthandler = o.requesthandler or cgi.document.Handler
tmueller@129
   948
 	o.requestdocument = o.requestdocument or cgi.document.Name
tmueller@129
   949
	o.requestpath = o.requestpath or cgi.document.VirtualPath
tmueller@129
   950
tmueller@129
   951
	-- Manage login and establish session
tmueller@129
   952
	
tmueller@129
   953
	o.session.init(o.config.sessiondir, o.args.session, o.config.sessionmaxage)
tmueller@129
   954
	if o.args.login then
tmueller@129
   955
		if o.args.login == "false" then
tmueller@129
   956
			o.session.delete()
tmueller@129
   957
			o.session = nil
tmueller@129
   958
		elseif o.args.password then
tmueller@129
   959
			o.loginfailed = true
tmueller@129
   960
			local pwddb = tek.source(o.config.passwdfile)
tmueller@129
   961
			local pwdentry = pwddb[o.args.login]
tmueller@129
   962
			if pwdentry and pwdentry.password == o.args.password then
tmueller@129
   963
				o.session.data.authuser = pwdentry.username
tmueller@129
   964
				o.session.data.id = o.session.id
tmueller@129
   965
				o.loginfailed = nil
tmueller@129
   966
			end
tmueller@129
   967
		end
tmueller@129
   968
	end
tmueller@129
   969
	
tmueller@129
   970
	o.secure = o.request.Port == o.config.secureport
tmueller@129
   971
	o.authuser = o.session and o.session.data.authuser
tmueller@129
   972
	
tmueller@129
   973
	if o.nologin or not o.authuser then
tmueller@129
   974
		o.authuser = nil
tmueller@129
   975
		o.session = nil
tmueller@129
   976
-- 		o.args.session = nil -- TODO?
tmueller@129
   977
	end
tmueller@129
   978
	
tmueller@129
   979
	-- Get lang, locale, profile, section
tmueller@129
   980
	
tmueller@129
   981
	o:init()
tmueller@129
   982
	if o.authuser then
tmueller@129
   983
		o:handlechanges()
tmueller@129
   984
	end
tmueller@129
   985
	
tmueller@129
   986
	-- Current document
tmueller@129
   987
	
tmueller@129
   988
	o.document = o.requestdocument .. "/" .. o.sectionpath
tmueller@129
   989
	if o.authuser then
tmueller@129
   990
		o.getdocname = function(self, path)
tmueller@129
   991
			return path and self.requestdocument .. "/" .. path or self.requestdocument
tmueller@129
   992
		end
tmueller@129
   993
	else
tmueller@129
   994
		o.getdocname = function(self, path)
tmueller@129
   995
			path = path or self.config.defname
tmueller@129
   996
			if self:isdynamic(path) then
tmueller@129
   997
				return self.requestdocument .. "/" .. path
tmueller@129
   998
			end
tmueller@129
   999
			path = path == self.config.defname and "index" or path
tmueller@129
  1000
			return "/" .. path:gsub("/", "_") .. ".html"
tmueller@129
  1001
		end
tmueller@129
  1002
	end
tmueller@129
  1003
	
tmueller@129
  1004
	-- Create "editable section" function
tmueller@129
  1005
	
tmueller@129
  1006
	local func, msg = o:loadhtml(open("loona/editable.lua"),
tmueller@129
  1007
		"loona:out", "loona/editable.lua")
tmueller@129
  1008
 	assert(func, o:dbmsg("Syntax error", msg))
tmueller@129
  1009
	o.editable = o:runboxed(func)
tmueller@129
  1010
	
tmueller@129
  1011
	-- Save session state
tmueller@129
  1012
	
tmueller@129
  1013
	if o.session then
tmueller@129
  1014
		o.session.save()
tmueller@129
  1015
	end
tmueller@129
  1016
tmueller@129
  1017
	return o
tmueller@0
  1018
end
tmueller@0
  1019
tmueller@23
  1020
tmueller@129
  1021
function loona:execute(fname)
tmueller@141
  1022
	self:indexdynamic()
tmueller@129
  1023
	fname = fname or self.requesthandler
tmueller@129
  1024
	local parsed, msg = self:loadhtml(open(fname), "loona:out", fname)
tmueller@129
  1025
	assert(parsed, self:dbmsg("HTML/Lua parsing failed", msg))
tmueller@129
  1026
	self:runboxed(parsed)
tmueller@141
  1027
	
tmueller@141
  1028
	
tmueller@141
  1029
		local fh = open("/tmp/dump", "wb")
tmueller@141
  1030
		tek.dump(self, function(s) fh:write(s) end)
tmueller@141
  1031
	
tmueller@141
  1032
	
tmueller@129
  1033
	return self
tmueller@129
  1034
end
tmueller@80
  1035
tmueller@80
  1036
tmueller@129
  1037
function loona:indexdynamic()
tmueller@129
  1038
	self:recursesections(self.sections, function(self, s, e, path, dynamic)
tmueller@129
  1039
		path = path and path .. "_" .. e.name or e.name
tmueller@129
  1040
		dynamic = dynamic or { }
tmueller@129
  1041
		for k in pairs(e.dynamic or { }) do
tmueller@129
  1042
			dynamic[k] = true
tmueller@129
  1043
		end
tmueller@129
  1044
		for k in pairs(dynamic) do
tmueller@129
  1045
			local ext = (k == "main" and "") or "." .. k
tmueller@129
  1046
			if posix.stat(self.contentdir .. "/" .. path .. ext, "mode") == "file" then
tmueller@129
  1047
				dynamic[k] = e.dynamic and e.dynamic[k]
tmueller@129
  1048
			end
tmueller@129
  1049
		end
tmueller@129
  1050
		local n = 0
tmueller@129
  1051
		for k in pairs(dynamic) do
tmueller@129
  1052
			n = n + 1
tmueller@129
  1053
		end
tmueller@129
  1054
		if n > 0 then
tmueller@129
  1055
			e.dynamic = { }
tmueller@129
  1056
			for k in pairs(dynamic) do
tmueller@129
  1057
				e.dynamic[k] = true
tmueller@129
  1058
			end
tmueller@129
  1059
		else
tmueller@129
  1060
			e.dynamic = nil
tmueller@129
  1061
		end
tmueller@129
  1062
		return path, dynamic
tmueller@129
  1063
	end)
tmueller@129
  1064
end
tmueller@80
  1065
tmueller@0
  1066
tmueller@129
  1067
function loona:isdynamic(path)
tmueller@129
  1068
	path = path or self.sectionpath
tmueller@129
  1069
	local t, i = self:checkpath(path)
tmueller@129
  1070
	return t and t[i].dynamic
tmueller@129
  1071
end
tmueller@0
  1072
tmueller@0
  1073
tmueller@129
  1074
function loona:dumphtml(o)
tmueller@129
  1075
	local outbuf = { }
tmueller@129
  1076
	o = o or { }
tmueller@129
  1077
	o.nologin = true
tmueller@129
  1078
	o.out = function(self, s) table.insert(outbuf, s) end
tmueller@129
  1079
	o.setheader = function(self, s) end
tmueller@129
  1080
	o = self:new(o):execute()
tmueller@129
  1081
	if not o:isdynamic() then
tmueller@129
  1082
		local path = o.sectionname
tmueller@129
  1083
		path = path == o.config.defname and "index" or path
tmueller@141
  1084
		local srcname = o.config.htdocsdir .. "/" .. path .. o.htmlext
tmueller@141
  1085
		local dstname = o.config.htmlcachedir .. "/" .. path .. o.htmlext .. ".tmp"
tmueller@131
  1086
		local fh, msg = open(srcname .. ".tmp", "wb")
tmueller@131
  1087
		assert(fh, self:dbmsg("Could not write cached HTML", msg))
tmueller@129
  1088
		fh:write(unpack(outbuf))
tmueller@129
  1089
		fh:close()
tmueller@131
  1090
		local success, msg = posix.symlink(srcname, dstname)
tmueller@137
  1091
-- 		assert(success, self:dbmsg("Could not link to cached HTML", msg))
tmueller@0
  1092
	end
tmueller@0
  1093
end