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