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