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