cgi-bin/loona.lua
author Timm S. Mueller <tmueller@neoscientists.org>
Sat, 14 Apr 2007 17:44:49 +0200
changeset 170 5c3d82f27213
parent 169 76916d60d4c9
child 174 dae1c4153ed6
permissions -rw-r--r--
Listbegin menu generator always produced class menulevel1 - fixed
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@149
    34
local open, remove, rename, getenv, time, date =
tmueller@149
    35
	io.open, os.remove, os.rename, os.getenv, os.time, os.date
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@149
    58
tmueller@129
    59
function atom:new(o)
tmueller@129
    60
	o = o or { }
tmueller@129
    61
	setmetatable(o, self)
tmueller@129
    62
	self.__index = self
tmueller@129
    63
	return o
tmueller@129
    64
end
tmueller@129
    65
tmueller@149
    66
tmueller@129
    67
local loona = atom:new(getfenv())
tmueller@129
    68
tmueller@129
    69
tmueller@129
    70
function loona:dbmsg(msg, detail)
tmueller@129
    71
 	return (msg and detail and self.authuser) and
tmueller@129
    72
 		("%s : %s"):format(msg, detail) or msg
tmueller@129
    73
end
tmueller@129
    74
tmueller@129
    75
tmueller@129
    76
function loona:checkprofilename(n)
tmueller@129
    77
	assert(n:match("^%w+$") and n ~= "current",
tmueller@129
    78
		self:dbmsg("Invalid profile name", n))
tmueller@129
    79
	return n
tmueller@129
    80
end
tmueller@129
    81
tmueller@129
    82
tmueller@129
    83
function loona:checkbodyname(s)
tmueller@129
    84
	s = s or "main"
tmueller@129
    85
	assert(s:match("^[%w_]*%w+[%w_]*$"), self:dbmsg("Invalid body name", s))
tmueller@129
    86
	return s
tmueller@129
    87
end
tmueller@129
    88
tmueller@129
    89
tmueller@129
    90
function loona:deleteprofile(p, lang)
tmueller@129
    91
	p = self.config.contentdir .. "/" .. p .. "_" .. (lang or self.lang)
tmueller@129
    92
	for e in tek.util.readdir(p) do
tmueller@129
    93
 		local success, msg = remove(p .. "/" .. e)
tmueller@129
    94
		assert(success, self:dbmsg("Error removing entry in profile", msg))
tmueller@129
    95
	end
tmueller@129
    96
	return remove(p)
tmueller@129
    97
end
tmueller@129
    98
tmueller@129
    99
tmueller@129
   100
function loona:copyprofile(newprofile, srcprofile, lang)
tmueller@129
   101
	srcprofile = srcprofile or self.profile
tmueller@129
   102
	lang = lang or self.lang
tmueller@129
   103
	local contentdir = self.config.contentdir
tmueller@143
   104
	local src = ("%s/%s_%s"):format(contentdir, srcprofile or self.profile, 
tmueller@143
   105
		lang)
tmueller@129
   106
	assert(posix.stat(src, "mode") == "directory",
tmueller@129
   107
		self:dbmsg("Not a directory", src))
tmueller@129
   108
	local dst = ("%s/%s_%s"):format(contentdir, newprofile, lang)
tmueller@129
   109
	local success, msg = posix.mkdir(dst)
tmueller@129
   110
	assert(success, self:dbmsg("Error creating profile directory", msg))
tmueller@129
   111
	for e in tek.util.readdir(src) do
tmueller@129
   112
		local ext = e:match("^[^.].*%.([^.]*)$")
tmueller@129
   113
		if ext ~= "LOCK" then
tmueller@129
   114
			local f = src .. "/" .. e
tmueller@129
   115
			if posix.stat(f, "mode") == "file" then
tmueller@129
   116
				success, msg = tek.copyfile(f, dst .. "/" .. e)
tmueller@129
   117
				assert(success, self:dbmsg("Error copying file", msg))
tmueller@129
   118
			end
tmueller@129
   119
		end
tmueller@129
   120
	end
tmueller@129
   121
end
tmueller@129
   122
tmueller@129
   123
tmueller@129
   124
function loona:publishprofile(profile, lang)
tmueller@129
   125
	lang = lang or self.lang
tmueller@129
   126
	local contentdir = self.config.contentdir
tmueller@129
   127
	local newpath = ("%s/current_%s"):format(contentdir, lang)
tmueller@129
   128
	local tmppath = newpath .. "." .. self.session.name
tmueller@129
   129
	local success, msg = posix.symlink(profile .. "_" .. lang, tmppath)
tmueller@129
   130
	assert(success, self:dbmsg("Cannot create symlink", msg))
tmueller@129
   131
	success, msg = rename(tmppath, newpath)
tmueller@129
   132
	assert(success, self:dbmsg("Cannot put symlink in place", msg))
tmueller@129
   133
	
tmueller@147
   134
	-- Get languages in the current profile
tmueller@141
   135
	
tmueller@141
   136
	local plangs = { }
tmueller@141
   137
	local lmatch = "^" .. self.profile .. "_(%w+)$"
tmueller@141
   138
	for e in tek.util.readdir(self.config.contentdir) do
tmueller@141
   139
		local l = e:match(lmatch)
tmueller@141
   140
		if l then
tmueller@141
   141
			table.insert(plangs, l)
tmueller@141
   142
		end
tmueller@141
   143
	end
tmueller@141
   144
	
tmueller@147
   145
	-- For all languages, unroll site to static HTML
tmueller@129
   146
	
tmueller@141
   147
	for _, lang in ipairs(plangs) do
tmueller@141
   148
		local ext = (#plangs == 1 and ".html") or (".html." .. lang)
tmueller@141
   149
		self:recursesections(self.sections, function(self, s, e, path)
tmueller@141
   150
			path = path and path .. "/" .. e.name or e.name
tmueller@141
   151
			if not e.notvisible then
tmueller@143
   152
				loona:dumphtml { requestpath = path, requestlang = lang,
tmueller@150
   153
					htmlext = ext, insecure = true }
tmueller@141
   154
			end
tmueller@141
   155
			return path
tmueller@141
   156
		end)
tmueller@141
   157
	end
tmueller@131
   158
	
tmueller@131
   159
	-- Update file cache
tmueller@131
   160
tmueller@131
   161
	local htdocs = self.config.htdocsdir
tmueller@131
   162
	local cache = self.config.htmlcachedir
tmueller@131
   163
tmueller@131
   164
	for e in tek.util.readdir(cache) do
tmueller@141
   165
		local f = e:match("^.*%.html%.?(%w*)$")
tmueller@141
   166
		if f and f ~= "tmp" then
tmueller@131
   167
			local success, msg = remove(htdocs .. "/" .. e)
tmueller@131
   168
			success, msg = remove(cache .. "/" .. e)
tmueller@143
   169
 			assert(success,
tmueller@143
   170
 				self:dbmsg("Could not purge cached HTML file", msg))
tmueller@131
   171
		end
tmueller@131
   172
	end
tmueller@147
   173
	
tmueller@131
   174
	for e in tek.util.readdir(cache) do
tmueller@141
   175
		local f = e:match("^(.*%.html%.?%w*)%.tmp$")
tmueller@131
   176
		if f then
tmueller@131
   177
			local success, msg = rename(cache .. "/" .. e, cache .. "/" .. f)
tmueller@143
   178
			assert(success,
tmueller@143
   179
				self:dbmsg("Could not update cached HTML file", msg))
tmueller@131
   180
			success, msg = rename(htdocs .. "/" .. e, htdocs .. "/" .. f)
tmueller@143
   181
			assert(success,
tmueller@143
   182
				self:dbmsg("Could not update cached HTML file", msg))
tmueller@131
   183
		end
tmueller@131
   184
	end
tmueller@129
   185
end
tmueller@129
   186
tmueller@129
   187
tmueller@129
   188
function loona:recursesections(s, func, ...)
tmueller@0
   189
	for _, e in ipairs(s) do
tmueller@129
   190
		local udata = { func(self, s, e, unpack(arg)) }
tmueller@0
   191
		if e.subs then
tmueller@129
   192
			self:recursesections(e.subs, func, unpack(udata))
tmueller@0
   193
		end
tmueller@129
   194
	end
tmueller@129
   195
end
tmueller@129
   196
tmueller@129
   197
tmueller@129
   198
function loona:indexsections()
tmueller@129
   199
	self:recursesections(self.sections, function(self, s, e)
tmueller@129
   200
		e.notvalid = (not self.secure and e.secure) or 
tmueller@129
   201
			(not self.authuser and e.secret) or nil
tmueller@129
   202
		e.notvisible = e.notvalid or not self.authuser and e.hidden or nil
tmueller@16
   203
		s[e.name] = e
tmueller@129
   204
	end)
tmueller@0
   205
end
tmueller@0
   206
tmueller@0
   207
tmueller@20
   208
--	Decompose section path into a stack of sections, returning only up to
tmueller@0
   209
--	the last valid element in the path. additionally returns the table of
tmueller@0
   210
--	the last section path element (or the default section)
tmueller@0
   211
tmueller@129
   212
function loona:getsection(path)
tmueller@129
   213
	local default = not self.authuser and self.config.defname
tmueller@129
   214
	local tab = { { entries = self.sections, name = default } }
tmueller@129
   215
	local ss = self.sections
tmueller@0
   216
	local sectionpath
tmueller@129
   217
	(path or ""):gsub("(%w+)/?", function(a)
tmueller@27
   218
		if ss then
tmueller@27
   219
			local s = ss[a]
tmueller@20
   220
			if s and not s.notvalid then
tmueller@20
   221
				sectionpath = s
tmueller@0
   222
				tab[#tab].name = a
tmueller@27
   223
				ss = s.subs
tmueller@27
   224
				if ss then
tmueller@27
   225
					table.insert(tab, { entries = ss })
tmueller@0
   226
				end
tmueller@0
   227
			else
tmueller@27
   228
				ss = nil -- stop.
tmueller@0
   229
			end
tmueller@0
   230
		end
tmueller@0
   231
	end)
tmueller@129
   232
	if not self.section and not sectionpath then
tmueller@129
   233
		sectionpath = self.sections[default]
tmueller@0
   234
		if sectionpath then
tmueller@0
   235
			table.insert(tab, { entries = sectionpath.subs })
tmueller@0
   236
		end
tmueller@0
   237
	end
tmueller@0
   238
	return tab, sectionpath
tmueller@0
   239
end
tmueller@0
   240
tmueller@0
   241
tmueller@169
   242
function loona:getpath(delimiter, maxdepth)
tmueller@0
   243
	local t = { }
tmueller@169
   244
	local d = 0
tmueller@169
   245
	maxdepth = maxdepth or #self.submenus
tmueller@129
   246
	for _, menu in ipairs(self.submenus) do
tmueller@0
   247
		if menu.name then
tmueller@0
   248
			table.insert(t, menu.name)
tmueller@0
   249
		end
tmueller@169
   250
		d = d + 1
tmueller@169
   251
		if d == maxdepth then
tmueller@169
   252
			break
tmueller@169
   253
		end
tmueller@0
   254
	end
tmueller@0
   255
	return table.concat(t, delimiter or "/")
tmueller@0
   256
end
tmueller@0
   257
tmueller@0
   258
tmueller@129
   259
function loona:deletesection(fname, all_bodies)
tmueller@129
   260
	local fullname = self.contentdir .. "/" .. fname
tmueller@0
   261
	local success, msg = remove(fullname)
tmueller@129
   262
	if all_bodies then
tmueller@0
   263
		local pat = "^" .. 
tmueller@0
   264
			fname:gsub("%^%$%(%)%%%.%[%]%*%+%-%?", "%%%1") .. "%..*$"
tmueller@129
   265
		for e in tek.util.readdir(self.contentdir) do
tmueller@0
   266
			if e:match(pat) then
tmueller@129
   267
				remove(self.contentdir .. "/" .. e)
tmueller@0
   268
			end
tmueller@0
   269
		end
tmueller@0
   270
	end
tmueller@0
   271
	return success, msg
tmueller@0
   272
end
tmueller@0
   273
tmueller@0
   274
tmueller@129
   275
function loona:addpath(path, e)
tmueller@129
   276
	local tab = self.sections
tmueller@0
   277
	path:gsub("(%w+)/?", function(a)
tmueller@20
   278
		if tab then
tmueller@20
   279
			local s = tab[a]
tmueller@20
   280
			if s then
tmueller@20
   281
				if not s.subs then
tmueller@20
   282
					s.subs = { }
tmueller@0
   283
				end
tmueller@20
   284
				tab = s.subs
tmueller@0
   285
			else
tmueller@20
   286
				table.insert(tab, e)
tmueller@20
   287
				tab[a] = e
tmueller@20
   288
 				tab = nil -- stop
tmueller@0
   289
			end
tmueller@0
   290
		end
tmueller@0
   291
	end)
tmueller@129
   292
	return e
tmueller@0
   293
end
tmueller@0
   294
tmueller@0
   295
tmueller@129
   296
function loona:rmpath(path)
tmueller@20
   297
	local parent
tmueller@129
   298
	local tab = self.sections
tmueller@0
   299
	path:gsub("(%w+)/?", function(a)
tmueller@20
   300
		if tab then
tmueller@20
   301
			local idx = lookupname(tab, a)
tmueller@20
   302
			if idx then
tmueller@20
   303
				if tab[idx].subs then
tmueller@20
   304
					parent = tab[idx]
tmueller@20
   305
					tab = tab[idx].subs
tmueller@0
   306
				else
tmueller@20
   307
					table.remove(tab, idx)
tmueller@43
   308
					tab[a] = nil
tmueller@20
   309
					if #tab == 0 and parent then
tmueller@20
   310
						parent.subs = nil
tmueller@0
   311
					end
tmueller@20
   312
					tab = nil
tmueller@0
   313
				end
tmueller@0
   314
			end
tmueller@0
   315
		end
tmueller@0
   316
	end)
tmueller@0
   317
end
tmueller@0
   318
tmueller@0
   319
tmueller@129
   320
function loona:checkpath(path)
tmueller@129
   321
	if path ~= "index" then -- "index" is reserved
tmueller@129
   322
		local res, idx
tmueller@129
   323
		local tab = self.sections
tmueller@129
   324
		path:gsub("(%w+)/?", function(a)
tmueller@129
   325
			if tab then
tmueller@129
   326
				local i = lookupname(tab, a)
tmueller@129
   327
				if i then
tmueller@129
   328
					res, idx = tab, i
tmueller@129
   329
					tab = tab[i].subs
tmueller@129
   330
				else
tmueller@129
   331
					res, idx = nil, nil
tmueller@129
   332
				end
tmueller@129
   333
			end
tmueller@129
   334
		end)
tmueller@129
   335
		return res, idx
tmueller@129
   336
	end
tmueller@8
   337
end
tmueller@8
   338
tmueller@8
   339
tmueller@129
   340
function loona:title()
tmueller@129
   341
	return self.section and (self.section.title or self.section.label or
tmueller@129
   342
		self.section.name) or ""
tmueller@0
   343
end
tmueller@0
   344
tmueller@0
   345
tmueller@0
   346
--	Run a site function snippet, with full error recovery
tmueller@0
   347
--	(also recovers from errors in error handling function)
tmueller@0
   348
tmueller@129
   349
function loona:dosnippet(func, errfunc)
tmueller@0
   350
	local ret = { tek.catch(func) }
tmueller@0
   351
	if ret[1] == 0 or (errfunc and tek.catch(errfunc) == 0) then
tmueller@0
   352
		return unpack(ret)
tmueller@0
   353
	end
tmueller@129
   354
	self:out("<h2>Error</h2>")
tmueller@129
   355
	self:out("<h3>" .. self:encodeform(ret[2] or "") .. "</h3>")
tmueller@129
   356
	if self.authuser then
tmueller@0
   357
		if type(ret[3]) == "string" then
tmueller@129
   358
			self:out("<p>" .. self:encodeform(ret[3]) .. "</p>")
tmueller@0
   359
		end
tmueller@129
   360
		if ret[4] then
tmueller@129
   361
			self:out("<pre>" .. self:encodeform(ret[4]) .. "</pre>")
tmueller@0
   362
		end
tmueller@0
   363
	end
tmueller@0
   364
end	
tmueller@0
   365
tmueller@0
   366
tmueller@129
   367
function loona:lockfile(file)
tmueller@129
   368
	return not self.session and true or 
tmueller@129
   369
		posix.symlink(self.session.filename, file .. ".LOCK")
tmueller@0
   370
end
tmueller@0
   371
tmueller@0
   372
tmueller@129
   373
function loona:unlockfile(file)
tmueller@129
   374
	return not self.session and true or remove(file .. ".LOCK")
tmueller@0
   375
end
tmueller@0
   376
tmueller@0
   377
tmueller@129
   378
function loona:saveindex()
tmueller@129
   379
	local tempname = self.indexfname .. "." .. self.session.name
tmueller@124
   380
	local f, msg = open(tempname, "wb")
tmueller@129
   381
	assert(f, self:dbmsg("Error opening section file for writing", msg))
tmueller@129
   382
	tek.dump(self.sections, function(...)
tmueller@124
   383
		f:write(unpack(arg))
tmueller@124
   384
	end)
tmueller@124
   385
	f:close()
tmueller@129
   386
	local success, msg = rename(tempname, self.indexfname)
tmueller@129
   387
	assert(success, self:dbmsg("Error renaming section file", msg))
tmueller@124
   388
end
tmueller@124
   389
tmueller@124
   390
tmueller@129
   391
function loona:savebody(fname, content)
tmueller@129
   392
	fname = self.contentdir .. "/" .. fname
tmueller@0
   393
	local f, msg = open(fname, "wb")
tmueller@129
   394
	assert(f, self:dbmsg("Could not open file for writing", msg))
tmueller@0
   395
	f:write(content or "")
tmueller@0
   396
	f:close()
tmueller@0
   397
end
tmueller@0
   398
tmueller@23
   399
tmueller@129
   400
function loona:runboxed(func, envitems, ...)
tmueller@129
   401
	local fenv = {
tmueller@129
   402
 		arg = arg,
tmueller@129
   403
 		loona = self
tmueller@129
   404
 	}
tmueller@129
   405
 	if envitems then
tmueller@129
   406
	 	for k, v in pairs(envitems) do
tmueller@129
   407
 			fenv[k] = v
tmueller@129
   408
 		end
tmueller@129
   409
 	end
tmueller@129
   410
 	setmetatable(fenv, { __index = boxed_G }) -- boxed global environment
tmueller@129
   411
	setfenv(func, fenv)
tmueller@129
   412
	return func()
tmueller@129
   413
end
tmueller@0
   414
tmueller@129
   415
tmueller@129
   416
function loona:include(fname, ...)
tmueller@129
   417
	assert(not fname:match("%W"), self:dbmsg("Invalid include name", fname))
tmueller@129
   418
	local fname2 = ("%s/%s.lua"):format(self.config.extdir, fname)
tmueller@0
   419
	local f, msg = open(fname2)
tmueller@129
   420
	assert(f, self:dbmsg("Cannot open file", msg))
tmueller@129
   421
	local parsed, msg = self:loadhtml(f, "loona:out", fname2)
tmueller@129
   422
	assert(parsed, self:dbmsg("Syntax error", msg))
tmueller@151
   423
	return self:runboxed(parsed, nil, unpack(arg))
tmueller@0
   424
end
tmueller@0
   425
tmueller@0
   426
tmueller@68
   427
--	produce link target, propagate lang, profile, session
tmueller@68
   428
tmueller@129
   429
function loona:href(section, ...)
tmueller@129
   430
	local target = self:getdocname(section)
tmueller@129
   431
	if self.session then
tmueller@68
   432
		return tek.web.gethref(target, { "profile", "session", "lang", 
tmueller@68
   433
			unpack(arg) })
tmueller@0
   434
	end
tmueller@68
   435
	return tek.web.gethref(target, { "lang", unpack(arg) })
tmueller@0
   436
end
tmueller@0
   437
tmueller@149
   438
tmueller@129
   439
function loona:ilink(target, text, extra)
tmueller@68
   440
	return ('<a href="%s"%s>%s</a>'):format(target, extra or "", text)
tmueller@0
   441
end
tmueller@0
   442
tmueller@149
   443
tmueller@68
   444
--	internal link, propagation of lang, profile, session
tmueller@0
   445
tmueller@129
   446
function loona:link(section, text, ...)
tmueller@162
   447
	return self:ilink(self:href(section, unpack(arg)), text or section,
tmueller@162
   448
	' class="intlink"')
tmueller@0
   449
end
tmueller@0
   450
tmueller@149
   451
tmueller@68
   452
--	external link (opens in a new window), no argument propagation
tmueller@0
   453
tmueller@129
   454
function loona:elink(target, text)
tmueller@162
   455
	return self:ilink(target, text or target, self.config.extlinkextra)
tmueller@0
   456
end
tmueller@0
   457
tmueller@149
   458
tmueller@68
   459
--	plain link, no argument propagation
tmueller@68
   460
tmueller@129
   461
function loona:plink(section, text, ...)
tmueller@129
   462
	return self:ilink(tek.web.gethref(section, arg), text or section)
tmueller@68
   463
end
tmueller@68
   464
tmueller@149
   465
tmueller@68
   466
--	user interface link, propagation of lang, profile, session
tmueller@68
   467
tmueller@129
   468
function loona:uilink(section, text, ...)
tmueller@129
   469
	return self:ilink(self:href(section, unpack(arg)), text or section)
tmueller@68
   470
end
tmueller@68
   471
tmueller@149
   472
tmueller@68
   473
--	produce a hidden input value in forms
tmueller@0
   474
tmueller@129
   475
function loona:hidden(name, value)
tmueller@68
   476
	return not value and "" or
tmueller@68
   477
		('<input type="hidden" name="%s" value="%s" />'):format(name, value)
tmueller@0
   478
end
tmueller@0
   479
tmueller@0
   480
tmueller@129
   481
function loona:getprofiles(lang)
tmueller@129
   482
	lang = lang or self.lang
tmueller@129
   483
	local dir = self.config.contentdir
tmueller@0
   484
	local t = { }
tmueller@129
   485
	for f in tek.util.readdir(dir) do
tmueller@129
   486
		if posix.lstat(dir .. "/" .. f, "mode") == "directory" then
tmueller@0
   487
			local e = f:match("^(%w+)_" .. lang .. "$")
tmueller@0
   488
 			if e then
tmueller@0
   489
	 			t[e] = e
tmueller@0
   490
 	 		end
tmueller@0
   491
		end
tmueller@0
   492
	end
tmueller@0
   493
	return t
tmueller@0
   494
end
tmueller@0
   495
tmueller@0
   496
tmueller@169
   497
--	Functions to produce a navigation menu
tmueller@80
   498
tmueller@129
   499
local newent = { name = "new", label = "[+]", action="actionnew=true" }
tmueller@124
   500
tmueller@169
   501
function loona:rmenu(level, render, path, addnew, recurse)
tmueller@143
   502
	local sub = (addnew and level == #self.submenus + 1) and
tmueller@129
   503
		{ name = "new", entries = { }} or self.submenus[level]
tmueller@124
   504
 	if sub and sub.entries then
tmueller@80
   505
		local visible = { }
tmueller@80
   506
		for _, e in ipairs(sub.entries) do
tmueller@80
   507
			if not e.notvisible then
tmueller@80
   508
				table.insert(visible, e)
tmueller@80
   509
			end
tmueller@80
   510
		end
tmueller@143
   511
		if addnew then
tmueller@124
   512
			table.insert(visible, newent)
tmueller@124
   513
		end
tmueller@169
   514
		local numvis = #visible
tmueller@169
   515
		if numvis > 0 then
tmueller@169
   516
			render.listbegin(self, level, numvis, path)
tmueller@169
   517
			for idx, e in ipairs(visible) do
tmueller@129
   518
				local label = self:encodeform(e.label or e.name)
tmueller@80
   519
				local newpath = path and path .. "/" .. e.name or e.name
tmueller@80
   520
				local active = (e.name == sub.name)
tmueller@169
   521
				render.itembegin(self, level, idx)
tmueller@169
   522
				render.link(self, level, newpath, label, active, e.action)
tmueller@169
   523
				if recurse and active then
tmueller@169
   524
					self:rmenu(level + 1, render, newpath, addnew, recurse)
tmueller@80
   525
				end
tmueller@169
   526
				render.itemend(self)
tmueller@80
   527
			end
tmueller@169
   528
			render.listend(self)
tmueller@80
   529
		end
tmueller@80
   530
	end
tmueller@80
   531
end
tmueller@80
   532
tmueller@149
   533
tmueller@169
   534
function loona:menu(level, recurse, render)
tmueller@169
   535
	level = level or 1
tmueller@169
   536
	render = render or { }
tmueller@169
   537
	render.link = render.link or 
tmueller@169
   538
		function(self, level, path, label, active, ...)
tmueller@169
   539
			self:out(('<a %shref="%s">%s</a>\n'):format(active and 
tmueller@169
   540
				'class="active" ' or "", self:href(path, unpack(arg)), label))
tmueller@169
   541
		end
tmueller@169
   542
	render.listbegin = render.listbegin or
tmueller@170
   543
		function(self, level) -- , numvis, path
tmueller@169
   544
			self:out('<ul id="menulevel' .. level .. '">\n')
tmueller@169
   545
		end
tmueller@169
   546
	render.listend = render.listend or
tmueller@169
   547
		function(self)
tmueller@169
   548
			self:out('</ul>\n')
tmueller@169
   549
		end
tmueller@169
   550
	render.itembegin = render.itembegin or
tmueller@169
   551
		function(self) -- , level, idx
tmueller@169
   552
			self:out('<li>\n')
tmueller@169
   553
		end
tmueller@169
   554
	render.itemend = render.itemend or
tmueller@169
   555
		function(self)
tmueller@169
   556
			self:out('</li>\n')
tmueller@169
   557
		end
tmueller@169
   558
	recurse = recurse == nil and true or recurse
tmueller@169
   559
	local path = level > 1 and self:getpath("/", level - 1) or nil
tmueller@143
   560
	local addnew = self.authuser and not self.ispubprofile
tmueller@169
   561
	self:rmenu(level, render, path, addnew, recurse)
tmueller@80
   562
end
tmueller@80
   563
tmueller@80
   564
tmueller@147
   565
function loona:loadcontent(fname)
tmueller@147
   566
	if fname then
tmueller@147
   567
		local f = open(self.contentdir .. "/" .. fname)
tmueller@147
   568
		local c = f:read("*a")
tmueller@147
   569
		f:close()
tmueller@147
   570
		return c
tmueller@147
   571
	end
tmueller@147
   572
	return ""
tmueller@147
   573
end
tmueller@147
   574
tmueller@147
   575
tmueller@147
   576
function loona:loadmarkup(fname)
tmueller@147
   577
	return (fname and fname ~= "") and
tmueller@147
   578
		self:domarkup(self:loadcontent(fname)) or ""
tmueller@147
   579
end
tmueller@147
   580
tmueller@147
   581
tmueller@147
   582
function loona:editable(editkey, fname, savename)
tmueller@147
   583
	
tmueller@147
   584
	local contentdir = self.contentdir
tmueller@147
   585
	local edit, show, hidden, extramsg, changed
tmueller@147
   586
	
tmueller@147
   587
	if self.authuser then
tmueller@149
   588
		
tmueller@149
   589
		local hiddenvars = table.concat( {
tmueller@149
   590
			self:hidden("lang", self.args.lang),
tmueller@149
   591
			self:hidden("profile", self.profile),
tmueller@149
   592
			self:hidden("session", self.session.id),
tmueller@149
   593
			self:hidden("editkey", editkey) }, " ")
tmueller@149
   594
	
tmueller@147
   595
		local lockfname = fname and (contentdir .. "/" .. fname)
tmueller@147
   596
		
tmueller@147
   597
		if self.useralert and editkey == self.args.editkey then
tmueller@149
   598
			
tmueller@147
   599
			--	display user alert/request/confirmation
tmueller@149
   600
			
tmueller@147
   601
			hidden = true
tmueller@149
   602
			self:out([[
tmueller@149
   603
			<form action="]] .. self.document .. [[" method="post" accept-charset="utf-8">
tmueller@149
   604
				<fieldset>
tmueller@149
   605
					<legend>]] .. self.useralert.text ..[[</legend>
tmueller@149
   606
					]] .. (self.useralert.confirm or "") .. [[
tmueller@149
   607
					<input type="submit" name="actioncancel" value="]] .. self.locale.CANCEL  ..[[" />
tmueller@149
   608
					]] .. hiddenvars .. [[
tmueller@149
   609
				</fieldset>
tmueller@149
   610
			</form>
tmueller@147
   611
			]])
tmueller@147
   612
			
tmueller@147
   613
		elseif self.args.actionnew and editkey == "main" then
tmueller@149
   614
			
tmueller@147
   615
			--	form for creating a new section
tmueller@149
   616
			
tmueller@147
   617
			hidden = true
tmueller@149
   618
			if self.ispubprofile then
tmueller@149
   619
				self:out([[<h2><span class="warn">]] .. self.locale.WARNING_YOU_ARE_IN_PUBLISHED_PROFILE ..[[</span></h2>]])
tmueller@149
   620
			end
tmueller@149
   621
			self:out([[
tmueller@149
   622
			<form action="]] .. self.document .. [[" method="post" accept-charset="utf-8">
tmueller@149
   623
				<fieldset>
tmueller@149
   624
					<legend>
tmueller@149
   625
						]] .. self.locale.CREATE_NEW_SECTION_UNDER .. " " .. self.sectionpath .. [[
tmueller@149
   626
					</legend>
tmueller@149
   627
					<table>
tmueller@149
   628
						<tr>
tmueller@149
   629
							<td align="right">
tmueller@149
   630
								]] .. self.locale.PATHNAME .. [[
tmueller@149
   631
							</td>
tmueller@149
   632
							<td>
tmueller@149
   633
								<input size="30" maxlength="30" name="editname" />
tmueller@149
   634
							</td>
tmueller@149
   635
						</tr>
tmueller@149
   636
						<tr>
tmueller@149
   637
							<td align="right">
tmueller@149
   638
								]] .. self.locale.MENULABEL .. [[
tmueller@149
   639
							</td>
tmueller@149
   640
							<td>
tmueller@149
   641
								<input size="30" maxlength="50" name="editlabel" />
tmueller@149
   642
							</td>
tmueller@149
   643
						</tr>
tmueller@149
   644
						<tr>
tmueller@149
   645
							<td align="right">
tmueller@149
   646
								]] .. self.locale.WINDOWTITLE .. [[
tmueller@149
   647
							</td>
tmueller@149
   648
							<td>
tmueller@149
   649
								<input size="30" maxlength="50" name="edittitle" />
tmueller@149
   650
							</td>
tmueller@149
   651
						</tr>
tmueller@149
   652
						<tr>
tmueller@149
   653
							<td align="right">
tmueller@149
   654
								]] .. self.locale.INVISIBLE .. [[
tmueller@149
   655
							</td>
tmueller@149
   656
							<td>
tmueller@149
   657
								<input type="checkbox" name="editvisibility" />
tmueller@149
   658
							</td>
tmueller@149
   659
						</tr>
tmueller@149
   660
						<tr>
tmueller@149
   661
							<td align="right">
tmueller@149
   662
								]] .. self.locale.SECRET .. [[
tmueller@149
   663
							</td>
tmueller@149
   664
							<td>
tmueller@149
   665
								<input type="checkbox" name="editsecrecy" />
tmueller@149
   666
							</td>
tmueller@149
   667
						</tr>
tmueller@149
   668
						<tr>
tmueller@149
   669
							<td align="right">
tmueller@149
   670
								]] .. self.locale.SECURE_CONNECTION .. [[
tmueller@149
   671
							</td>
tmueller@149
   672
							<td>
tmueller@149
   673
								<input type="checkbox" name="editsecure" />
tmueller@149
   674
							</td>
tmueller@149
   675
						</tr>
tmueller@149
   676
						<tr>
tmueller@149
   677
							<td align="right">
tmueller@149
   678
								]] .. self.locale.REDIRECT .. [[
tmueller@149
   679
							</td>
tmueller@149
   680
							<td>
tmueller@149
   681
								<input size="30" maxlength="50" name="editredirect" />
tmueller@149
   682
							</td>
tmueller@149
   683
						</tr>
tmueller@149
   684
					</table>					
tmueller@149
   685
					<input type="submit" name="actioncreate" value="]] .. self.locale.CREATE .. [[" />
tmueller@149
   686
					]] .. hiddenvars .. [[
tmueller@149
   687
				</fieldset>
tmueller@149
   688
			</form>
tmueller@149
   689
			<hr />
tmueller@147
   690
			]])
tmueller@147
   691
		
tmueller@147
   692
		elseif self.args.actioneditprops and editkey == "main" then
tmueller@147
   693
			hidden = true
tmueller@149
   694
			if self.ispubprofile then
tmueller@149
   695
				self:out([[<h2><span class="warn">]] .. self.locale.WARNING_YOU_ARE_IN_PUBLISHED_PROFILE ..[[</span></h2>]])
tmueller@149
   696
			end
tmueller@149
   697
			self:out([[
tmueller@149
   698
			<form action="]] ..self.document .. [[" method="post" accept-charset="utf-8">
tmueller@149
   699
				<fieldset>
tmueller@149
   700
					<legend>
tmueller@149
   701
						]] .. self.locale.MODIFY_PROPERTIES_OF_SECTION .. " " .. self.sectionpath .. [[
tmueller@149
   702
					</legend>
tmueller@149
   703
					<table>
tmueller@149
   704
						<tr>
tmueller@149
   705
							<td align="right">
tmueller@149
   706
								]] .. self.locale.MENULABEL .. [[
tmueller@149
   707
							</td>
tmueller@149
   708
							<td>
tmueller@149
   709
								<input size="30" maxlength="50" name="editlabel" value="]] .. (self.section.label or "") .. [[" />
tmueller@149
   710
							</td>
tmueller@149
   711
						</tr>
tmueller@149
   712
						<tr>
tmueller@149
   713
							<td align="right">
tmueller@149
   714
								]] .. self.locale.WINDOWTITLE .. [[
tmueller@149
   715
							</td>
tmueller@149
   716
							<td>
tmueller@149
   717
								<input size="30" maxlength="50" name="edittitle" value="]] .. (self.section.title or "") .. [[" />
tmueller@149
   718
							</td>
tmueller@149
   719
						</tr>
tmueller@149
   720
						<tr>
tmueller@149
   721
							<td align="right">
tmueller@149
   722
								]] .. self.locale.INVISIBLE .. [[
tmueller@149
   723
							</td>
tmueller@149
   724
							<td>
tmueller@149
   725
								<input type="checkbox" name="editvisibility" ]] .. (self.section.hidden and 'checked="checked"' or "") .. [[/>
tmueller@149
   726
							</td>
tmueller@149
   727
						</tr>
tmueller@149
   728
						<tr>
tmueller@149
   729
							<td align="right">
tmueller@149
   730
								]] .. self.locale.SECRET .. [[
tmueller@149
   731
							</td>
tmueller@149
   732
							<td>
tmueller@149
   733
								<input type="checkbox" name="editsecrecy" ]] .. (self.section.secret and 'checked="checked"' or "") .. [[/>
tmueller@149
   734
							</td>
tmueller@149
   735
						</tr>
tmueller@149
   736
						<tr>
tmueller@149
   737
							<td align="right">
tmueller@149
   738
								]] .. self.locale.SECURE_CONNECTION .. [[
tmueller@149
   739
							</td>
tmueller@149
   740
							<td>
tmueller@149
   741
								<input type="checkbox" name="editsecure" ]] .. (self.section.secure and 'checked="checked"' or "") .. [[/>
tmueller@149
   742
							</td>
tmueller@149
   743
						</tr>
tmueller@149
   744
						<tr>
tmueller@149
   745
							<td align="right">
tmueller@149
   746
								]] .. self.locale.REDIRECT .. [[
tmueller@149
   747
							</td>
tmueller@149
   748
							<td>
tmueller@149
   749
								<input size="30" maxlength="50" name="editredirect" value="]] .. (self.section.redirect or "") .. [[" />
tmueller@149
   750
							</td>
tmueller@149
   751
						</tr>
tmueller@149
   752
					</table>
tmueller@149
   753
					<input type="submit" name="actionsaveprops" value="]] .. self.locale.SAVE .. [[" />
tmueller@149
   754
					<input type="submit" name="actioncancel" value="]] .. self.locale.CANCEL .. [[" />
tmueller@149
   755
					]] .. hiddenvars .. [[
tmueller@149
   756
				</fieldset>
tmueller@149
   757
			</form>
tmueller@147
   758
			]])
tmueller@149
   759
		
tmueller@147
   760
		elseif (self.args.actioneditprofiles or
tmueller@147
   761
			self.args.actioncreateprofile or 
tmueller@147
   762
			self.args.actionchangeprofile or 
tmueller@147
   763
			self.args.actionpublishprofile) and editkey == "main" then
tmueller@147
   764
			hidden = true
tmueller@147
   765
			local profiles = { }
tmueller@147
   766
			for p in pairs(self:getprofiles()) do
tmueller@147
   767
				table.insert(profiles, p)
tmueller@147
   768
			end
tmueller@147
   769
			table.sort(profiles)
tmueller@149
   770
			self:out([[
tmueller@149
   771
			<form action="]] .. self.document .. [[" method="post" accept-charset="utf-8">
tmueller@149
   772
				<fieldset>
tmueller@149
   773
					<legend>
tmueller@149
   774
						]] .. self.locale.CHANGEPROFILE .. [[
tmueller@149
   775
					</legend>
tmueller@149
   776
					<select name="changeprofile" size="1">]])
tmueller@149
   777
						for _, val in ipairs(profiles) do
tmueller@156
   778
							self:out('<option' .. (val == self.profile and ' selected="selected"' or '') .. '>')
tmueller@149
   779
							self:out(val)
tmueller@149
   780
							self:out('</option>')
tmueller@149
   781
						end
tmueller@149
   782
					self:out([[
tmueller@149
   783
					</select>							
tmueller@149
   784
					<input type="submit" name="actionchangeprofile" value="]] .. self.locale.CHANGE ..[[" />
tmueller@149
   785
					]] .. hiddenvars .. [[
tmueller@149
   786
				</fieldset>
tmueller@149
   787
			</form>
tmueller@149
   788
			<form action="]] .. self.document ..[[" method="post" accept-charset="utf-8">
tmueller@149
   789
				<fieldset>
tmueller@149
   790
					<legend>
tmueller@149
   791
						]] .. self.locale.CREATEPROFILE .. [[
tmueller@149
   792
					</legend>
tmueller@149
   793
					<input size="20" maxlength="20" name="createprofile" />
tmueller@149
   794
					<input type="submit" name="actioncreateprofile" value="]] .. self.locale.CREATE .. [[" />
tmueller@149
   795
					]] .. hiddenvars .. [[
tmueller@149
   796
				</fieldset>
tmueller@149
   797
			</form>
tmueller@149
   798
			]])
tmueller@149
   799
			if not self.ispubprofile then
tmueller@147
   800
				self:out([[
tmueller@147
   801
				<form action="]] .. self.document .. [[" method="post" accept-charset="utf-8">
tmueller@147
   802
					<fieldset>
tmueller@147
   803
						<legend>
tmueller@149
   804
							]] .. self.locale.PUBLISHPROFILE .. [[
tmueller@147
   805
						</legend>
tmueller@149
   806
						]] .. self:hidden("publishprofile", self.profile) .. [[
tmueller@149
   807
						<input type="submit" name="actionpublishprofile" value="]] .. self.locale.PUBLISH .. [[" />
tmueller@147
   808
						]] .. hiddenvars .. [[
tmueller@147
   809
					</fieldset>
tmueller@147
   810
				</form>
tmueller@149
   811
				<form action="]] .. self.document .. [[" method="post" accept-charset="utf-8">
tmueller@147
   812
					<fieldset>
tmueller@147
   813
						<legend>
tmueller@149
   814
							]] .. self.locale.DELETEPROFILE .. [[
tmueller@147
   815
						</legend>
tmueller@149
   816
						]] .. self:hidden("deleteprofile", self.profile) .. [[
tmueller@149
   817
						<input type="submit" name="actiondeleteprofile" value="]] .. self.locale.DELETE .. [[" />
tmueller@147
   818
						]] .. hiddenvars .. [[
tmueller@147
   819
					</fieldset>
tmueller@147
   820
				</form>
tmueller@147
   821
				]])
tmueller@149
   822
			end
tmueller@149
   823
			
tmueller@147
   824
		elseif self.args.actionedit and editkey == self.args.editkey then
tmueller@147
   825
			if not self.section.redirect then
tmueller@147
   826
				extramsg = self.ispubprofile and
tmueller@147
   827
					self.locale.WARNING_YOU_ARE_IN_PUBLISHED_PROFILE
tmueller@147
   828
				edit = self:loadcontent(fname):gsub("\194\160", "&nbsp;") -- TODO
tmueller@147
   829
				changed = self.section and (self.section.revisiondate or self.section.creationdate)
tmueller@147
   830
			end
tmueller@149
   831
		
tmueller@147
   832
		elseif self.args.actionpreview and editkey == self.args.editkey then
tmueller@147
   833
			edit = self.args.editform
tmueller@147
   834
			show = self:domarkup(edit:gsub("&nbsp;", "\194\160")) -- TODO
tmueller@149
   835
		
tmueller@147
   836
		elseif self.args.actionsave and editkey == self.args.editkey then
tmueller@147
   837
			local c = self.args.editform
tmueller@147
   838
			local dynamic
tmueller@147
   839
			
tmueller@147
   840
			if lockfname then
tmueller@147
   841
				self:expire(contentdir, "[^.]%S+.LOCK")
tmueller@147
   842
				if self:lockfile(lockfname) then
tmueller@147
   843
					-- lock was expired, aquired a new one
tmueller@147
   844
					extramsg = self.locale.SECTION_COULD_HAVE_CHANGED
tmueller@147
   845
					edit = c
tmueller@147
   846
				else
tmueller@147
   847
					local tab = tek.source(lockfname .. ".LOCK")
tmueller@147
   848
					if tab and tab.id == self.session.id then
tmueller@147
   849
						-- lock already held and is mine - try to save:
tmueller@147
   850
						local savec = c:gsub("&nbsp;", "\194\160") -- TODO
tmueller@149
   851
						remove(contentdir .. "/" .. savename .. ".html")
tmueller@147
   852
						self:savebody(savename, savec)
tmueller@147
   853
						-- TODO: error handling
tmueller@147
   854
						self:unlockfile(lockfname)
tmueller@147
   855
						show, dynamic = self:domarkup(savec)
tmueller@149
   856
						changed = time()
tmueller@147
   857
					else
tmueller@147
   858
						-- lock was expired and someone else has it now
tmueller@147
   859
						extramsg = self.locale.SECTION_IN_USE
tmueller@147
   860
						edit = c
tmueller@147
   861
					end
tmueller@147
   862
				end
tmueller@147
   863
			else
tmueller@147
   864
				-- new sidefile
tmueller@147
   865
				local savec = c:gsub("&nbsp;", "\194\160") -- TODO
tmueller@147
   866
				self:savebody(savename, savec)
tmueller@147
   867
				-- TODO: error handling
tmueller@147
   868
				show, dynamic = self:domarkup(savec)
tmueller@147
   869
			end
tmueller@147
   870
			
tmueller@147
   871
			-- mark dynamic text bodies
tmueller@147
   872
			if not self.section.dynamic then
tmueller@147
   873
				self.section.dynamic = { }
tmueller@147
   874
			end
tmueller@147
   875
			self.section.dynamic[editkey] = dynamic
tmueller@147
   876
			local n = 0
tmueller@147
   877
			for _ in pairs(self.section.dynamic) do
tmueller@147
   878
				n = n + 1
tmueller@147
   879
			end
tmueller@147
   880
			if n == 0 then
tmueller@147
   881
				self.section.dynamic = nil
tmueller@147
   882
			end
tmueller@147
   883
			
tmueller@147
   884
			self:saveindex()
tmueller@147
   885
			
tmueller@147
   886
		elseif self.args.actioncancel and editkey == self.args.editkey then
tmueller@147
   887
			if lockfname then
tmueller@147
   888
				self:unlockfile(lockfname) -- remove lock
tmueller@147
   889
			end
tmueller@147
   890
		end
tmueller@147
   891
		
tmueller@147
   892
		if editkey == "main" and self.section and self.section.redirect then
tmueller@149
   893
			self:out('<h2>' .. self.locale.SECTION_IS_REDIRECT ..'</h2>')
tmueller@149
   894
			self:out(self:link(self.section.redirect))
tmueller@149
   895
			self:out('<hr />')
tmueller@147
   896
		end
tmueller@147
   897
	
tmueller@147
   898
		if edit then
tmueller@147
   899
			self:expire(contentdir, "[^.]%S+.LOCK")
tmueller@147
   900
			if fname and not self:lockfile(contentdir .. "/" .. fname) then
tmueller@147
   901
				local tab = tek.source(contentdir .. "/" .. fname .. ".LOCK")
tmueller@147
   902
				if tab and tab.id ~= self.session.id then
tmueller@147
   903
					extramsg = self.locale.SECTION_IN_USE
tmueller@147
   904
				end
tmueller@147
   905
				-- else already owner
tmueller@147
   906
			end
tmueller@149
   907
			if extramsg then
tmueller@149
   908
				self:out('<h2><span class="warn">' .. extramsg .. '</span></h2>')
tmueller@149
   909
			end
tmueller@149
   910
			self:out([[
tmueller@149
   911
			<form action="]] .. self.document .. [[#preview" method="post" accept-charset="utf-8">
tmueller@149
   912
				<fieldset>
tmueller@149
   913
					<legend>
tmueller@149
   914
						]] .. self.locale.EDIT_SECTION .. [[
tmueller@149
   915
					</legend>
tmueller@168
   916
					<textarea cols="80" rows="25" name="editform">
tmueller@168
   917
]] .. self:encodeform(edit) .. [[</textarea>
tmueller@149
   918
					<br />
tmueller@149
   919
					<input type="submit" name="actionsave" value="]] .. self.locale.SAVE .. [[" />
tmueller@149
   920
					<input type="submit" name="actionpreview" value="]] .. self.locale.PREVIEW .. [[" />
tmueller@149
   921
					<input type="submit" name="actioncancel" value="]] .. self.locale.CANCEL .. [[" />
tmueller@149
   922
					]] .. hiddenvars .. [[
tmueller@149
   923
				</fieldset>
tmueller@149
   924
			</form>
tmueller@147
   925
			]])
tmueller@147
   926
		end
tmueller@147
   927
	end	
tmueller@147
   928
	
tmueller@147
   929
	if not hidden then
tmueller@149
   930
		self:dosnippet(function()
tmueller@149
   931
			if not show then
tmueller@149
   932
				show = self:loadmarkup(fname)
tmueller@149
   933
				changed = self.section and (self.section.revisiondate or self.section.creationdate)
tmueller@149
   934
			end
tmueller@149
   935
			local parsed, msg = self:loadhtml(show, "loona:out", "<parsed html>")
tmueller@149
   936
			assert(parsed, msg and "Syntax error : " .. msg)
tmueller@149
   937
			self:runboxed(parsed)
tmueller@147
   938
		end)
tmueller@147
   939
	end
tmueller@147
   940
	
tmueller@147
   941
	if self.authuser then
tmueller@149
   942
		self:out([[
tmueller@149
   943
		<hr />
tmueller@149
   944
		<div class="edit">]])
tmueller@149
   945
			if editkey == "main" then
tmueller@149
   946
				self:out([[
tmueller@149
   947
				<a name="preview"></a>
tmueller@149
   948
				]] .. self.authuser .. [[ : 
tmueller@149
   949
				]] .. self:uilink(self.sectionpath, "[" .. self.locale.PROFILE .. "]", "actioneditprofiles=true", "editkey=" .. editkey) .. [[ :
tmueller@149
   950
				]] .. self.profile .. "_" .. self.lang)
tmueller@149
   951
				if self.ispubprofile then
tmueller@149
   952
					self:out([[
tmueller@149
   953
						<span class="warn">[]] .. self.locale.PUBLIC .. [[]</span>]])
tmueller@149
   954
				end
tmueller@149
   955
				self:out(' : ' .. self.sectionpath .. ' ')
tmueller@149
   956
			end
tmueller@149
   957
			if self.section and not self.ispubprofile then
tmueller@149
   958
				self:out(self:uilink(self.sectionpath, "[" .. self.locale.EDIT .. "]", "actionedit=true", "editkey=" .. editkey) .. " ")
tmueller@147
   959
				if editkey == "main" then
tmueller@149
   960
					self:out('- ' .. self:uilink(self.sectionpath, "[" .. self.locale.PROPERTIES .. "]", "actioneditprops=true", "editkey=" .. editkey) .. " ")
tmueller@147
   961
				end
tmueller@149
   962
				if fname == savename or not self.section.subs then
tmueller@149
   963
					self:out('- ' .. self:uilink(self.sectionpath, "[" .. self.locale.DELETE .. "]", "actiondelete=true", "editkey=" .. editkey) .. " ")
tmueller@147
   964
				end
tmueller@149
   965
				if editkey == "main" then
tmueller@149
   966
					self:out('- ' .. self:uilink(self.sectionpath, "[" .. self.locale.MOVEUP .. "]", "actionup=true", "editkey=" .. editkey) .. " ")
tmueller@149
   967
					self:out('- ' .. self:uilink(self.sectionpath, "[" .. self.locale.MOVEDOWN .. "]", "actiondown=true", "editkey=" .. editkey) .. " ")
tmueller@149
   968
				end
tmueller@149
   969
				if changed and editkey == "main" then
tmueller@149
   970
					self:out('- ' .. self.locale.CHANGED .. ': ' .. date("%d-%b-%Y %T", changed))
tmueller@149
   971
				end
tmueller@149
   972
			end
tmueller@149
   973
		self:out('</div>')
tmueller@147
   974
	end
tmueller@147
   975
tmueller@147
   976
end
tmueller@147
   977
tmueller@147
   978
tmueller@147
   979
--	Get pathname of an existing content file that
tmueller@147
   980
--	the current path is determined by (or defaults to)
tmueller@147
   981
tmueller@147
   982
function loona:getsectionpath(bodyname, requestpath)
tmueller@147
   983
	local ext = (not bodyname or bodyname == "main") and "" or "." .. bodyname
tmueller@147
   984
	local t, path, section = { }
tmueller@147
   985
	for _, menu in ipairs(self.submenus) do
tmueller@147
   986
		if menu.entries and menu.entries[menu.name] then
tmueller@147
   987
			table.insert(t, menu.name)
tmueller@147
   988
			local fn = table.concat(t, "_")
tmueller@147
   989
			if posix.stat(self.contentdir .. "/" .. fn .. ext, 
tmueller@147
   990
				"mode") == "file" then
tmueller@147
   991
				path, section = fn, menu
tmueller@147
   992
			end
tmueller@147
   993
		end
tmueller@147
   994
	end
tmueller@147
   995
	return path, ext, section
tmueller@147
   996
end
tmueller@147
   997
tmueller@147
   998
tmueller@147
   999
function loona:body(name)
tmueller@147
  1000
	name = self:checkbodyname(name)
tmueller@147
  1001
	local path, ext = self:getsectionpath(name)
tmueller@149
  1002
	self:dosnippet(function()
tmueller@149
  1003
		self:editable(name, path and path .. ext, self.sectionname .. ext)
tmueller@149
  1004
	end)
tmueller@147
  1005
end
tmueller@147
  1006
tmueller@147
  1007
tmueller@129
  1008
function loona:init()
tmueller@0
  1009
	
tmueller@0
  1010
	-- get list of languages, in order of preference
tmueller@141
  1011
	-- TODO: respect quality parameter, not just order
tmueller@0
  1012
	
tmueller@141
  1013
	local l = self.requestlang or self.args.lang
tmueller@141
  1014
	self.langs = { l and l:match("^%w+$") }
tmueller@129
  1015
	if self.config.browserlang then
tmueller@0
  1016
		local s = getenv("HTTP_ACCEPT_LANGUAGE")
tmueller@0
  1017
		while s do
tmueller@0
  1018
			local l, r = s:match("^([%w.=]+)[,;](.*)$")
tmueller@0
  1019
			l = l or s
tmueller@0
  1020
			s = r
tmueller@0
  1021
			if l:match("^%w+$") then
tmueller@129
  1022
				table.insert(self.langs, l)
tmueller@0
  1023
			end
tmueller@0
  1024
		end
tmueller@0
  1025
	end
tmueller@129
  1026
	table.insert(self.langs, self.config.deflang)
tmueller@0
  1027
	
tmueller@0
  1028
	-- get list of possible profiles
tmueller@0
  1029
	
tmueller@0
  1030
	local profiles = { }
tmueller@129
  1031
	for e in tek.util.readdir(self.config.contentdir) do
tmueller@0
  1032
		profiles[e] = e
tmueller@0
  1033
	end
tmueller@0
  1034
	
tmueller@0
  1035
	-- get pubprofile
tmueller@0
  1036
	
tmueller@129
  1037
	for _, lang in ipairs(self.langs) do
tmueller@129
  1038
		local p = posix.readlink(self.config.contentdir .. "/current_" .. lang)
tmueller@0
  1039
		p = p and p:match("^(%w+)_" .. lang .. "$")
tmueller@0
  1040
		if p then
tmueller@129
  1041
			self.pubprofile = p
tmueller@0
  1042
			break
tmueller@0
  1043
		end
tmueller@0
  1044
	end
tmueller@0
  1045
	
tmueller@0
  1046
	-- get profile
tmueller@0
  1047
	
tmueller@143
  1048
	local checkprofile =
tmueller@149
  1049
		self.authuser and self.args.profile or self.pubprofile or "work"
tmueller@129
  1050
	for _, lang in ipairs(self.langs) do
tmueller@129
  1051
		if profiles[checkprofile .. "_" .. lang] then
tmueller@129
  1052
			self.profile = checkprofile
tmueller@129
  1053
			self.lang = lang
tmueller@0
  1054
			break
tmueller@0
  1055
		end
tmueller@0
  1056
	end
tmueller@0
  1057
	
tmueller@129
  1058
	assert(self.profile and self.lang, "Invalid profile or language")
tmueller@0
  1059
	
tmueller@47
  1060
	
tmueller@143
  1061
	self.ispubprofile = self.profile == self.pubprofile
tmueller@143
  1062
	
tmueller@129
  1063
	-- write back language and profile
tmueller@0
  1064
tmueller@129
  1065
	self.args.lang = self.lang ~= self.config.deflang and self.lang or nil
tmueller@129
  1066
	self.args.profile = self.profile
tmueller@0
  1067
	
tmueller@47
  1068
	
tmueller@0
  1069
	-- determine content directory pathname and section filename
tmueller@0
  1070
	
tmueller@143
  1071
	self.contentdir =
tmueller@143
  1072
		("%s/%s_%s"):format(self.config.contentdir, self.profile, self.lang)
tmueller@129
  1073
 	self.indexfname = self.contentdir .. "/.sections"
tmueller@0
  1074
	
tmueller@0
  1075
	-- load sections
tmueller@0
  1076
	
tmueller@129
  1077
 	self.sections = tek.source(self.indexfname)
tmueller@0
  1078
	
tmueller@20
  1079
	-- index sections, determine visibility in menu
tmueller@0
  1080
	
tmueller@129
  1081
	self:indexsections()
tmueller@0
  1082
	
tmueller@129
  1083
	-- decompose request path, produce a stack of sections
tmueller@0
  1084
	
tmueller@129
  1085
	self.submenus, self.section = self:getsection(self.requestpath)
tmueller@0
  1086
tmueller@0
  1087
	-- handle redirects if not logged on
tmueller@0
  1088
	
tmueller@129
  1089
	if not self.authuser and self.section and self.section.redirect then
tmueller@129
  1090
		self.submenus, self.section = self:getsection(self.section.redirect)
tmueller@0
  1091
	end
tmueller@0
  1092
			
tmueller@0
  1093
	-- section path and document name (refined)
tmueller@0
  1094
	
tmueller@129
  1095
	self.sectionpath = self:getpath()
tmueller@129
  1096
	self.sectionname = self:getpath("_")
tmueller@0
  1097
tmueller@0
  1098
end
tmueller@0
  1099
tmueller@0
  1100
tmueller@129
  1101
function loona:handlechanges()
tmueller@129
  1102
	
tmueller@129
  1103
	local save
tmueller@0
  1104
tmueller@129
  1105
	if self.args.editkey == "main" then
tmueller@23
  1106
		
tmueller@23
  1107
		-- In main editable section:
tmueller@23
  1108
		
tmueller@129
  1109
		if self.args.actioncreate then
tmueller@129
  1110
			
tmueller@45
  1111
			-- Create new section
tmueller@129
  1112
			
tmueller@129
  1113
			local editname = self.args.editname:lower()
tmueller@20
  1114
			assert(not editname:match("%W"),
tmueller@158
  1115
				self:dbmsg("Invalid section name", editname))
tmueller@20
  1116
			if not (section and (section.subs or section)[editname]) then
tmueller@143
  1117
				local newpath = (self.sectionpath and 
tmueller@143
  1118
					(self.sectionpath .. "/")) .. editname
tmueller@129
  1119
				local s = self:addpath(newpath, { name = editname,
tmueller@143
  1120
					label = self.args.editlabel ~= "" and
tmueller@143
  1121
						self.args.editlabel or nil,
tmueller@143
  1122
					title = self.args.edittitle ~= "" and
tmueller@143
  1123
						self.args.edittitle or nil,
tmueller@143
  1124
					redirect = self.args.editredirect ~= "" and
tmueller@143
  1125
						self.args.editredirect or nil,
tmueller@129
  1126
					hidden = self.args.editvisibility and true,
tmueller@129
  1127
					secret = self.args.editsecrecy and true,
tmueller@129
  1128
					secure = self.args.editsecure and true,
tmueller@129
  1129
					creator = self.authuser,
tmueller@20
  1130
					creationdate = time() })
tmueller@23
  1131
				save = true
tmueller@0
  1132
			end
tmueller@20
  1133
		
tmueller@129
  1134
		elseif self.args.actionsave then
tmueller@129
  1135
			
tmueller@45
  1136
			-- Save section
tmueller@129
  1137
			
tmueller@129
  1138
			self.section.revisiondate = time()
tmueller@129
  1139
			self.section.revisioner = self.authuser
tmueller@129
  1140
			save = true
tmueller@129
  1141
 		
tmueller@129
  1142
		elseif self.args.actionsaveprops then
tmueller@129
  1143
			
tmueller@129
  1144
			-- Save properties
tmueller@129
  1145
			
tmueller@129
  1146
			self.section.hidden = self.args.editvisibility and true
tmueller@129
  1147
			self.section.secret = self.args.editsecrecy and true
tmueller@129
  1148
			self.section.secure = self.args.editsecure and true
tmueller@143
  1149
			self.section.label = self.args.editlabel ~= "" and
tmueller@143
  1150
				self.args.editlabel or nil
tmueller@143
  1151
			self.section.title = self.args.edittitle ~= "" and
tmueller@143
  1152
				self.args.edittitle or nil
tmueller@129
  1153
			self.section.redirect =
tmueller@129
  1154
				self.args.editredirect ~= "" and self.args.editredirect or nil
tmueller@23
  1155
			save = true
tmueller@20
  1156
		
tmueller@129
  1157
		elseif self.args.actionup then
tmueller@129
  1158
			
tmueller@45
  1159
			-- Move section up
tmueller@129
  1160
			
tmueller@129
  1161
			local t, i = self:checkpath(self.sectionpath)
tmueller@0
  1162
			if t and i > 1 then
tmueller@143
  1163
				if self.ispubprofile and not self.args.actionconfirm then
tmueller@115
  1164
					useralert = {
tmueller@144
  1165
						text = self.locale.ALERT_MOVE_IN_PUBLISHED_PROFILE,
tmueller@115
  1166
						confirm =
tmueller@144
  1167
							'<input type="submit" name="actionup" value="' ..
tmueller@129
  1168
							self.locale.MOVE .. '" /> ' ..
tmueller@129
  1169
							self:hidden("actionconfirm", "true")
tmueller@115
  1170
					}
tmueller@115
  1171
				else
tmueller@115
  1172
					local item = table.remove(t, i)
tmueller@115
  1173
					table.insert(t, i - 1, item)
tmueller@115
  1174
					save = true
tmueller@115
  1175
				end
tmueller@0
  1176
			end
tmueller@20
  1177
		
tmueller@129
  1178
		elseif self.args.actiondown then
tmueller@129
  1179
			
tmueller@45
  1180
			-- Move section down
tmueller@129
  1181
			
tmueller@129
  1182
			local t, i = self:checkpath(self.sectionpath)
tmueller@0
  1183
			if t and i < #t then
tmueller@143
  1184
				if self.ispubprofile and not self.args.actionconfirm then
tmueller@115
  1185
					useralert = {
tmueller@144
  1186
						text = self.locale.ALERT_MOVE_IN_PUBLISHED_PROFILE,
tmueller@115
  1187
						confirm =
tmueller@144
  1188
							'<input type="submit" name="actiondown" value="' ..
tmueller@129
  1189
							self.locale.MOVE .. '" /> ' ..
tmueller@129
  1190
							self:hidden("actionconfirm", "true")
tmueller@115
  1191
					}
tmueller@115
  1192
				else
tmueller@115
  1193
					local item = table.remove(t, i)
tmueller@115
  1194
					table.insert(t, i + 1, item)
tmueller@115
  1195
					save = true
tmueller@115
  1196
				end
tmueller@0
  1197
			end
tmueller@20
  1198
		
tmueller@129
  1199
		elseif self.args.actioncreateprofile and self.args.createprofile then
tmueller@129
  1200
			
tmueller@23
  1201
			-- Create profile
tmueller@129
  1202
			
tmueller@129
  1203
			local c = self:checkprofilename(self.args.createprofile:lower())
tmueller@0
  1204
			if c == profile then
tmueller@144
  1205
				useralert = { 
tmueller@144
  1206
					text = self.locale.ALERT_CANNOT_COPY_PROFILE_TO_SELF
tmueller@144
  1207
				}
tmueller@0
  1208
			else
tmueller@129
  1209
				local profiles = self:getprofiles()
tmueller@129
  1210
				if profiles[c] and not self.args.actionconfirm then
tmueller@20
  1211
					useralert = {
tmueller@129
  1212
						text = c == self.pubprofile and 
tmueller@129
  1213
							self.locale.ALERT_OVERWRITE_PUBLISHED_PROFILE or
tmueller@129
  1214
							self.locale.ALERT_OVERWRITE_EXISTING_PROFILE,
tmueller@144
  1215
						confirm = '<input type="submit" ' ..
tmueller@144
  1216
							'name="actioncreateprofile" value="' ..
tmueller@129
  1217
							self.locale.OVERWRITE .. '" /> ' ..
tmueller@144
  1218
							self:hidden("actionconfirm", "true") ..
tmueller@129
  1219
							self:hidden("createprofile", c)
tmueller@20
  1220
					}
tmueller@0
  1221
				else
tmueller@0
  1222
					if profiles[c] then
tmueller@129
  1223
						self:deleteprofile(c)
tmueller@0
  1224
					end
tmueller@129
  1225
					self:copyprofile(c)
tmueller@0
  1226
				end
tmueller@0
  1227
			end
tmueller@20
  1228
		
tmueller@129
  1229
		elseif self.args.actiondeleteprofile and self.args.deleteprofile then
tmueller@129
  1230
			
tmueller@23
  1231
			-- Delete profile
tmueller@129
  1232
			
tmueller@129
  1233
			local c = self:checkprofilename(self.args.deleteprofile:lower())
tmueller@144
  1234
			assert(c ~= self.pubprofile,
tmueller@144
  1235
				self:dbmsg("Cannot delete published profile", c))
tmueller@129
  1236
			if self.args.actionconfirm then
tmueller@129
  1237
				self:deleteprofile(c)
tmueller@129
  1238
				self.profile = nil
tmueller@129
  1239
				self.args.profile = nil
tmueller@129
  1240
				self:init()
tmueller@23
  1241
				save = true
tmueller@0
  1242
			else
tmueller@20
  1243
				useralert = { 
tmueller@129
  1244
					text = self.locale.ALERT_DELETE_PROFILE,
tmueller@144
  1245
					confirm = '<input type="submit" ' ..
tmueller@144
  1246
						'name="actiondeleteprofile" value="' .. 
tmueller@129
  1247
						self.locale.DELETE .. '" /> ' ..
tmueller@129
  1248
						self:hidden("actionconfirm", "true") ..
tmueller@129
  1249
						self:hidden("deleteprofile", c)
tmueller@20
  1250
				}
tmueller@0
  1251
			end
tmueller@20
  1252
		
tmueller@129
  1253
		elseif self.args.actionchangeprofile and self.args.changeprofile then
tmueller@129
  1254
			
tmueller@23
  1255
			-- Change profile
tmueller@129
  1256
			
tmueller@129
  1257
			local c = self:checkprofilename(self.args.changeprofile:lower())
tmueller@129
  1258
			self.profile = c
tmueller@129
  1259
			self.args.profile = c
tmueller@23
  1260
			save = true
tmueller@20
  1261
		
tmueller@129
  1262
		elseif self.args.actionpublishprofile and self.args.publishprofile then
tmueller@129
  1263
			
tmueller@23
  1264
			-- Publish profile
tmueller@129
  1265
			
tmueller@129
  1266
			local c = self:checkprofilename(self.args.publishprofile:lower())
tmueller@129
  1267
			if c ~= self.publicprofile then
tmueller@129
  1268
				if self.args.actionconfirm then
tmueller@129
  1269
					self:publishprofile(c)
tmueller@23
  1270
					save = true
tmueller@0
  1271
				else
tmueller@20
  1272
					useralert = {
tmueller@129
  1273
						text = self.locale.ALERT_PUBLISH_PROFILE,
tmueller@144
  1274
						confirm = '<input type="submit" ' ..
tmueller@144
  1275
							'name="actionpublishprofile" value="' ..
tmueller@129
  1276
							self.locale.PUBLISH .. '" /> ' ..
tmueller@129
  1277
							self:hidden("actionconfirm", "true") ..
tmueller@129
  1278
							self:hidden("publishprofile", c)
tmueller@20
  1279
					}
tmueller@20
  1280
				end
tmueller@20
  1281
			end
tmueller@0
  1282
		end
tmueller@0
  1283
		
tmueller@129
  1284
	end
tmueller@0
  1285
	
tmueller@129
  1286
	if self.args.actiondelete then
tmueller@129
  1287
		
tmueller@129
  1288
		-- Delete section
tmueller@129
  1289
		
tmueller@129
  1290
		if not self.args.actionconfirm then
tmueller@129
  1291
			useralert = {
tmueller@143
  1292
				text = self.ispubprofile and
tmueller@144
  1293
					self.locale.ALERT_DELETE_IN_PUBLISHED_PROFILE or
tmueller@129
  1294
					self.locale.ALERT_DELETE_SECTION,
tmueller@129
  1295
				confirm =
tmueller@129
  1296
					'<input type="submit" name="actiondelete" value="' .. 
tmueller@129
  1297
					self.locale.DELETE .. '" /> ' ..
tmueller@129
  1298
					self:hidden("actionconfirm", "true")
tmueller@129
  1299
			}
tmueller@129
  1300
		else
tmueller@129
  1301
			local key = self.args.editkey
tmueller@129
  1302
			if key == "main" and not self.section.subs then
tmueller@129
  1303
				self:deletesection(self.sectionname, true) -- all bodies
tmueller@129
  1304
				self:rmpath(self.sectionpath) -- and node
tmueller@129
  1305
			else
tmueller@129
  1306
				local ext = (key == "main" and "") or "." .. key
tmueller@129
  1307
				self:deletesection(self.sectionname .. ext) -- only text
tmueller@129
  1308
				if self.section.dynamic then
tmueller@129
  1309
					self.section.dynamic[key] = nil
tmueller@131
  1310
					local n = 0
tmueller@131
  1311
					for _ in pairs(self.section.dynamic) do
tmueller@131
  1312
						n = n + 1
tmueller@131
  1313
					end
tmueller@131
  1314
					if n == 0 then
tmueller@131
  1315
						self.section.dynamic = nil
tmueller@131
  1316
					end
tmueller@129
  1317
				end
tmueller@129
  1318
			end
tmueller@129
  1319
			save = true
tmueller@0
  1320
		end
tmueller@0
  1321
	end
tmueller@129
  1322
		
tmueller@129
  1323
	if save then
tmueller@129
  1324
		self:saveindex()
tmueller@129
  1325
		self:init()
tmueller@129
  1326
	end
tmueller@129
  1327
	
tmueller@0
  1328
end
tmueller@0
  1329
tmueller@0
  1330
tmueller@129
  1331
function loona:encodeform(s)
tmueller@129
  1332
	return cgi.encodeform(s)
tmueller@129
  1333
end
tmueller@0
  1334
tmueller@80
  1335
tmueller@129
  1336
function loona:loadhtml(src, outfunc, chunkname)
tmueller@129
  1337
	return tek.web.include.load(src, outfunc, chunkname)
tmueller@129
  1338
end
tmueller@80
  1339
tmueller@80
  1340
tmueller@129
  1341
function loona:domarkup(s)
tmueller@129
  1342
	return tek.web.markup.main(s)
tmueller@129
  1343
end
tmueller@0
  1344
tmueller@0
  1345
tmueller@129
  1346
function loona:expire(dir, pat, maxage)
tmueller@129
  1347
	return tek.util.expire(dir, pat, maxage)
tmueller@129
  1348
end
tmueller@129
  1349
tmueller@23
  1350
tmueller@129
  1351
function loona:new(o)
tmueller@0
  1352
tmueller@129
  1353
	local parsed, msg
tmueller@129
  1354
	
tmueller@129
  1355
	o = o or { }
tmueller@129
  1356
	o = atom.new(self, o)
tmueller@129
  1357
	
tmueller@129
  1358
	o.out = o.out or function(self, s) tek.web.out(s) end
tmueller@129
  1359
	o.setheader = o.setheader or function(self, s) tek.web.setheader(s) end
tmueller@129
  1360
	
tmueller@129
  1361
	-- Get configuration
tmueller@129
  1362
	
tmueller@129
  1363
	o.config = o.config or tek.source(o.conffile or "../etc/config.lua") or { }
tmueller@129
  1364
	o.config.defname = o.config.defname or "home"
tmueller@129
  1365
	o.config.deflang = o.config.deflang or "en"
tmueller@129
  1366
	o.config.sessionmaxage = o.config.sessionmaxage or 600
tmueller@129
  1367
	o.config.secureport = o.config.secureport or 443
tmueller@144
  1368
	o.config.passwdfile =
tmueller@144
  1369
		posix.abspath(o.config.passwdfile or "../etc/passwd.lua")
tmueller@144
  1370
	o.config.sessiondir =
tmueller@144
  1371
		posix.abspath(o.config.sessiondir or "../var/sessions")
tmueller@129
  1372
	o.config.extdir = posix.abspath(o.config.extdir or "../extensions")
tmueller@129
  1373
	o.config.contentdir = posix.abspath(o.config.contentdir or "../content")
tmueller@129
  1374
	o.config.localedir = posix.abspath(o.config.localedir or "../locale")
tmueller@129
  1375
	o.config.htdocsdir = posix.abspath(o.config.htdocsdir or "../htdocs")
tmueller@144
  1376
	o.config.htmlcachedir = 
tmueller@144
  1377
		posix.abspath(o.config.htmlcachedir or "../var/htmlcache")
tmueller@162
  1378
	o.config.extlinkextra = o.config.extlinksamewindow and ' class="extlink"'
tmueller@162
  1379
		or ' class="extlink" onclick="void(window.open(this.href, \'\', \'\')); return false;"'
tmueller@129
  1380
	
tmueller@129
  1381
	-- Create proxy for on-demand loading of locales
tmueller@129
  1382
	
tmueller@129
  1383
	o.locale = { }
tmueller@129
  1384
	local locmt = { }
tmueller@129
  1385
	locmt.__index = function(_, key)
tmueller@129
  1386
		for _, l in ipairs(o.langs) do
tmueller@129
  1387
			locmt.__locale = tek.source(o.config.localedir .. "/" .. l)
tmueller@129
  1388
			if locmt.__locale then
tmueller@129
  1389
				break
tmueller@129
  1390
			end
tmueller@129
  1391
		end
tmueller@129
  1392
		locmt.__index = function(tab, key)
tmueller@129
  1393
			return locmt.__locale[key] or key
tmueller@129
  1394
		end
tmueller@129
  1395
		return locmt.__locale[key] or key
tmueller@129
  1396
	end
tmueller@129
  1397
	setmetatable(o.locale, locmt)
tmueller@129
  1398
	
tmueller@129
  1399
	-- Get request, args, document, script name, request path
tmueller@129
  1400
	
tmueller@129
  1401
	o.request = o.request or cgi.request
tmueller@129
  1402
	o.args = o.args or cgi.request.args
tmueller@129
  1403
	o.session = o.session or cgi.session
tmueller@129
  1404
 	
tmueller@129
  1405
 	o.scriptpath = o.scriptpath or cgi.document.Path
tmueller@129
  1406
	o.requesthandler = o.requesthandler or cgi.document.Handler
tmueller@129
  1407
 	o.requestdocument = o.requestdocument or cgi.document.Name
tmueller@129
  1408
	o.requestpath = o.requestpath or cgi.document.VirtualPath
tmueller@129
  1409
tmueller@129
  1410
	-- Manage login and establish session
tmueller@129
  1411
	
tmueller@129
  1412
	o.session.init(o.config.sessiondir, o.args.session, o.config.sessionmaxage)
tmueller@129
  1413
	if o.args.login then
tmueller@129
  1414
		if o.args.login == "false" then
tmueller@129
  1415
			o.session.delete()
tmueller@129
  1416
			o.session = nil
tmueller@129
  1417
		elseif o.args.password then
tmueller@129
  1418
			o.loginfailed = true
tmueller@129
  1419
			local pwddb = tek.source(o.config.passwdfile)
tmueller@129
  1420
			local pwdentry = pwddb[o.args.login]
tmueller@129
  1421
			if pwdentry and pwdentry.password == o.args.password then
tmueller@129
  1422
				o.session.data.authuser = pwdentry.username
tmueller@129
  1423
				o.session.data.id = o.session.id
tmueller@129
  1424
				o.loginfailed = nil
tmueller@129
  1425
			end
tmueller@129
  1426
		end
tmueller@129
  1427
	end
tmueller@129
  1428
	
tmueller@150
  1429
	o.secure = not o.insecure and (o.request.Port == o.config.secureport)
tmueller@129
  1430
	o.authuser = o.session and o.session.data.authuser
tmueller@129
  1431
	
tmueller@129
  1432
	if o.nologin or not o.authuser then
tmueller@129
  1433
		o.authuser = nil
tmueller@129
  1434
		o.session = nil
tmueller@129
  1435
	end
tmueller@129
  1436
	
tmueller@129
  1437
	-- Get lang, locale, profile, section
tmueller@129
  1438
	
tmueller@129
  1439
	o:init()
tmueller@129
  1440
	if o.authuser then
tmueller@129
  1441
		o:handlechanges()
tmueller@129
  1442
	end
tmueller@129
  1443
	
tmueller@129
  1444
	-- Current document
tmueller@129
  1445
	
tmueller@129
  1446
	o.document = o.requestdocument .. "/" .. o.sectionpath
tmueller@129
  1447
	if o.authuser then
tmueller@129
  1448
		o.getdocname = function(self, path)
tmueller@144
  1449
			return path and self.requestdocument .. "/" .. path or
tmueller@144
  1450
				self.requestdocument
tmueller@129
  1451
		end
tmueller@129
  1452
	else
tmueller@129
  1453
		o.getdocname = function(self, path)
tmueller@146
  1454
			local dyn
tmueller@129
  1455
			path = path or self.config.defname
tmueller@146
  1456
			dyn, path = self:isdynamic(path)
tmueller@146
  1457
			if dyn then
tmueller@129
  1458
				return self.requestdocument .. "/" .. path
tmueller@129
  1459
			end
tmueller@129
  1460
			path = path == self.config.defname and "index" or path
tmueller@129
  1461
			return "/" .. path:gsub("/", "_") .. ".html"
tmueller@129
  1462
		end
tmueller@129
  1463
	end
tmueller@129
  1464
	
tmueller@129
  1465
	-- Save session state
tmueller@129
  1466
	
tmueller@129
  1467
	if o.session then
tmueller@129
  1468
		o.session.save()
tmueller@129
  1469
	end
tmueller@129
  1470
tmueller@129
  1471
	return o
tmueller@0
  1472
end
tmueller@0
  1473
tmueller@23
  1474
tmueller@129
  1475
function loona:execute(fname)
tmueller@141
  1476
	self:indexdynamic()
tmueller@129
  1477
	fname = fname or self.requesthandler
tmueller@129
  1478
	local parsed, msg = self:loadhtml(open(fname), "loona:out", fname)
tmueller@129
  1479
	assert(parsed, self:dbmsg("HTML/Lua parsing failed", msg))
tmueller@129
  1480
	self:runboxed(parsed)
tmueller@129
  1481
	return self
tmueller@129
  1482
end
tmueller@80
  1483
tmueller@80
  1484
tmueller@129
  1485
function loona:indexdynamic()
tmueller@129
  1486
	self:recursesections(self.sections, function(self, s, e, path, dynamic)
tmueller@129
  1487
		path = path and path .. "_" .. e.name or e.name
tmueller@129
  1488
		dynamic = dynamic or { }
tmueller@129
  1489
		for k in pairs(e.dynamic or { }) do
tmueller@129
  1490
			dynamic[k] = true
tmueller@129
  1491
		end
tmueller@129
  1492
		for k in pairs(dynamic) do
tmueller@129
  1493
			local ext = (k == "main" and "") or "." .. k
tmueller@144
  1494
			if posix.stat(self.contentdir .. "/" .. path .. ext,
tmueller@144
  1495
				"mode") == "file" then
tmueller@129
  1496
				dynamic[k] = e.dynamic and e.dynamic[k]
tmueller@129
  1497
			end
tmueller@129
  1498
		end
tmueller@129
  1499
		local n = 0
tmueller@129
  1500
		for k in pairs(dynamic) do
tmueller@129
  1501
			n = n + 1
tmueller@129
  1502
		end
tmueller@129
  1503
		if n > 0 then
tmueller@129
  1504
			e.dynamic = { }
tmueller@129
  1505
			for k in pairs(dynamic) do
tmueller@129
  1506
				e.dynamic[k] = true
tmueller@129
  1507
			end
tmueller@129
  1508
		else
tmueller@129
  1509
			e.dynamic = nil
tmueller@129
  1510
		end
tmueller@129
  1511
		return path, dynamic
tmueller@129
  1512
	end)
tmueller@129
  1513
end
tmueller@80
  1514
tmueller@0
  1515
tmueller@129
  1516
function loona:isdynamic(path)
tmueller@129
  1517
	path = path or self.sectionpath
tmueller@129
  1518
	local t, i = self:checkpath(path)
tmueller@146
  1519
	if t and t[i].redirect then
tmueller@146
  1520
		path = t[i].redirect
tmueller@146
  1521
		t, i = self:isdynamic(path) -- TODO: prohibit endless recursion
tmueller@146
  1522
	end
tmueller@146
  1523
	return t and t[i].dynamic, path
tmueller@129
  1524
end
tmueller@0
  1525
tmueller@0
  1526
tmueller@129
  1527
function loona:dumphtml(o)
tmueller@129
  1528
	local outbuf = { }
tmueller@129
  1529
	o = o or { }
tmueller@129
  1530
	o.nologin = true
tmueller@129
  1531
	o.out = function(self, s) table.insert(outbuf, s) end
tmueller@129
  1532
	o.setheader = function(self, s) end
tmueller@129
  1533
	o = self:new(o):execute()
tmueller@129
  1534
	if not o:isdynamic() then
tmueller@129
  1535
		local path = o.sectionname
tmueller@129
  1536
		path = path == o.config.defname and "index" or path
tmueller@141
  1537
		local srcname = o.config.htdocsdir .. "/" .. path .. o.htmlext
tmueller@131
  1538
		local fh, msg = open(srcname .. ".tmp", "wb")
tmueller@131
  1539
		assert(fh, self:dbmsg("Could not write cached HTML", msg))
tmueller@129
  1540
		fh:write(unpack(outbuf))
tmueller@129
  1541
		fh:close()
tmueller@144
  1542
		local dstname = o.config.htmlcachedir .. "/" .. path .. o.htmlext
tmueller@144
  1543
		local success, msg = posix.symlink(srcname, dstname .. ".tmp")
tmueller@137
  1544
-- 		assert(success, self:dbmsg("Could not link to cached HTML", msg))
tmueller@0
  1545
	end
tmueller@0
  1546
end