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