cgi-bin/tek/class/loona.lua
author Timm S. Mueller <tmueller@neoscientists.org>
Fri, 05 Oct 2007 01:41:59 +0200
changeset 198 87a4de7c7457
parent 189 efb1bd425c56
child 199 8b5fc485edf4
permissions -rw-r--r--
Editing and re-publishing is now supported in public profile (configurable via
new option); checkpath() fixed, did not properly take into account the full
path; isdynamic() takes into account existance of the addressed link now; if a
link target does not exist, it is no longer referenced as static HTML in an
unrolled site (allows unknown link targets to be processed by other webserver
handlers); an userdata argument can now be passed to LOona; variables fixed
that were misplaced in global namespace; tek.os.posix.abspath() now handles
paths that were absolute already; added fastcgi external server launcher;
inheritance is now based on tek.class.atom; added lua.cgi cgi wrapper; added
fine-grained permissive rights; request, document and post classes overhauled;
underscore now permitted in cgi arguments; quotation marks now handled in
serialization; added checkpw method that can be used as a login hook; improved
buffer logic, setheader renamed to addheader; site template is more flexible
and uses config.defname as the logo's link target now
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@198
   486
	assert(parsed, self:dbmsg("Syntax error", msg and msg.text))
tmueller@151
   487
	return self:runboxed(parsed, nil, unpack(arg))
tmueller@0
   488
end
tmueller@0
   489
tmueller@0
   490
tmueller@174
   491
--	produce link target (simple)
tmueller@174
   492
tmueller@188
   493
function Loona:shref(section, arg)
tmueller@174
   494
	local args2 = { } -- propagated or new arguments
tmueller@174
   495
	for _, a in ipairs(arg) do
tmueller@198
   496
		local key, val = a:match("^([%w_]+)=(.*)$")
tmueller@174
   497
		if key and val then -- "arg=val" sets/overrides argument
tmueller@174
   498
			table.insert(args2, { name = key, value = val })
tmueller@174
   499
		elseif self.args[a] then -- just "arg" propagates argument
tmueller@174
   500
			table.insert(args2, { name = a, value = self.args[a] })
tmueller@174
   501
		end
tmueller@174
   502
	end
tmueller@174
   503
	local doc = self:getdocname(section, #args2 > 0)
tmueller@174
   504
	local url, anch = doc:match("^(.+)(#.+)$")
tmueller@174
   505
	local notfirst = doc:match("%?")
tmueller@174
   506
	local href = { anch and url or doc }
tmueller@174
   507
	for i, arg in ipairs(args2) do
tmueller@174
   508
		if i > 1 or notfirst then
tmueller@174
   509
			table.insert(href, "&amp;")
tmueller@174
   510
		else
tmueller@174
   511
			table.insert(href, "?")
tmueller@174
   512
		end
tmueller@189
   513
		table.insert(href, arg.name .. "=" .. cgi.encodeurl(arg.value))
tmueller@174
   514
	end
tmueller@174
   515
	if anch then
tmueller@174
   516
		insert(href, anch)
tmueller@174
   517
	end
tmueller@174
   518
	return table.concat(href)
tmueller@174
   519
end
tmueller@174
   520
tmueller@174
   521
tmueller@174
   522
--	produce link target, implicit propagation of lang, profile, session
tmueller@68
   523
tmueller@188
   524
function Loona:href(section, ...)
tmueller@129
   525
	if self.session then
tmueller@174
   526
		table.insert(arg, 1, "profile")
tmueller@174
   527
		table.insert(arg, 1, "session")
tmueller@0
   528
	end
tmueller@174
   529
	if self.explicitlang then
tmueller@174
   530
		table.insert(arg, 1, "lang")
tmueller@174
   531
	end
tmueller@174
   532
	return self:shref(section, arg)
tmueller@0
   533
end
tmueller@0
   534
tmueller@149
   535
tmueller@188
   536
function Loona:ilink(target, text, extra)
tmueller@68
   537
	return ('<a href="%s"%s>%s</a>'):format(target, extra or "", text)
tmueller@0
   538
end
tmueller@0
   539
tmueller@149
   540
tmueller@174
   541
--	internal link, implicit propagation of lang, profile, session
tmueller@0
   542
tmueller@188
   543
function Loona:link(section, text, ...)
tmueller@162
   544
	return self:ilink(self:href(section, unpack(arg)), text or section,
tmueller@162
   545
	' class="intlink"')
tmueller@0
   546
end
tmueller@0
   547
tmueller@149
   548
tmueller@68
   549
--	external link (opens in a new window), no argument propagation
tmueller@0
   550
tmueller@188
   551
function Loona:elink(target, text)
tmueller@162
   552
	return self:ilink(target, text or target, self.config.extlinkextra)
tmueller@0
   553
end
tmueller@0
   554
tmueller@149
   555
tmueller@174
   556
--	plain link, no implicit argument propagation
tmueller@68
   557
tmueller@188
   558
function Loona:plink(section, text, ...)
tmueller@174
   559
	return self:ilink(self:shref(section, arg), text or section)
tmueller@68
   560
end
tmueller@68
   561
tmueller@149
   562
tmueller@174
   563
--	user interface link, implicit propagation of lang, profile, session
tmueller@68
   564
tmueller@188
   565
function Loona:uilink(section, text, ...)
tmueller@174
   566
	return self:ilink(self:href(section, unpack(arg)), text or section,
tmueller@174
   567
	' class="uilink"')
tmueller@68
   568
end
tmueller@68
   569
tmueller@149
   570
tmueller@68
   571
--	produce a hidden input value in forms
tmueller@0
   572
tmueller@188
   573
function Loona:hidden(name, value)
tmueller@68
   574
	return not value and "" or
tmueller@68
   575
		('<input type="hidden" name="%s" value="%s" />'):format(name, value)
tmueller@0
   576
end
tmueller@0
   577
tmueller@0
   578
tmueller@188
   579
function Loona:scanprofiles(func)
tmueller@175
   580
	local tab = { }
tmueller@175
   581
	local dir = self.config.contentdir
tmueller@188
   582
	for f in util.readdir(dir) do
tmueller@175
   583
		if posix.lstat(dir .. "/" .. f, "mode") == "directory" then
tmueller@175
   584
			f = func(f)
tmueller@175
   585
			if f then
tmueller@175
   586
				table.insert(tab, f)
tmueller@175
   587
			end
tmueller@175
   588
		end
tmueller@175
   589
	end
tmueller@175
   590
	table.sort(tab)
tmueller@175
   591
	for _, v in ipairs(tab) do
tmueller@198
   592
		tab[v] = v
tmueller@175
   593
	end
tmueller@175
   594
	return tab
tmueller@175
   595
end
tmueller@175
   596
tmueller@175
   597
tmueller@188
   598
function Loona:getprofiles(lang)
tmueller@129
   599
	lang = lang or self.lang
tmueller@175
   600
	return self:scanprofiles(function(f)
tmueller@175
   601
		return f:match("^(%w+)_" .. lang .. "$")
tmueller@175
   602
	end)
tmueller@175
   603
end
tmueller@175
   604
tmueller@175
   605
tmueller@188
   606
function Loona:getlanguages(prof)
tmueller@175
   607
	prof = prof or self.profile
tmueller@175
   608
	return self:scanprofiles(function(f)
tmueller@175
   609
		return f:match("^" .. prof .. "_(%l%l)$")
tmueller@175
   610
	end)
tmueller@0
   611
end
tmueller@0
   612
tmueller@0
   613
tmueller@169
   614
--	Functions to produce a navigation menu
tmueller@80
   615
tmueller@129
   616
local newent = { name = "new", label = "[+]", action="actionnew=true" }
tmueller@124
   617
tmueller@188
   618
function Loona:rmenu(level, render, path, addnew, recurse)
tmueller@143
   619
	local sub = (addnew and level == #self.submenus + 1) and
tmueller@129
   620
		{ name = "new", entries = { }} or self.submenus[level]
tmueller@124
   621
 	if sub and sub.entries then
tmueller@80
   622
		local visible = { }
tmueller@80
   623
		for _, e in ipairs(sub.entries) do
tmueller@80
   624
			if not e.notvisible then
tmueller@80
   625
				table.insert(visible, e)
tmueller@80
   626
			end
tmueller@80
   627
		end
tmueller@143
   628
		if addnew then
tmueller@124
   629
			table.insert(visible, newent)
tmueller@124
   630
		end
tmueller@169
   631
		local numvis = #visible
tmueller@169
   632
		if numvis > 0 then
tmueller@169
   633
			render.listbegin(self, level, numvis, path)
tmueller@169
   634
			for idx, e in ipairs(visible) do
tmueller@129
   635
				local label = self:encodeform(e.label or e.name)
tmueller@80
   636
				local newpath = path and path .. "/" .. e.name or e.name
tmueller@80
   637
				local active = (e.name == sub.name)
tmueller@169
   638
				render.itembegin(self, level, idx)
tmueller@169
   639
				render.link(self, level, newpath, label, active, e.action)
tmueller@169
   640
				if recurse and active then
tmueller@169
   641
					self:rmenu(level + 1, render, newpath, addnew, recurse)
tmueller@80
   642
				end
tmueller@169
   643
				render.itemend(self)
tmueller@80
   644
			end
tmueller@169
   645
			render.listend(self)
tmueller@80
   646
		end
tmueller@80
   647
	end
tmueller@80
   648
end
tmueller@80
   649
tmueller@149
   650
tmueller@188
   651
function Loona:menu(level, recurse, render)
tmueller@169
   652
	level = level or 1
tmueller@169
   653
	render = render or { }
tmueller@198
   654
	render.link = render.link or
tmueller@169
   655
		function(self, level, path, label, active, ...)
tmueller@198
   656
			self:out(('<a %shref="%s">%s</a>\n'):format(active and
tmueller@169
   657
				'class="active" ' or "", self:href(path, unpack(arg)), label))
tmueller@169
   658
		end
tmueller@169
   659
	render.listbegin = render.listbegin or
tmueller@170
   660
		function(self, level) -- , numvis, path
tmueller@169
   661
			self:out('<ul id="menulevel' .. level .. '">\n')
tmueller@169
   662
		end
tmueller@169
   663
	render.listend = render.listend or
tmueller@169
   664
		function(self)
tmueller@169
   665
			self:out('</ul>\n')
tmueller@169
   666
		end
tmueller@169
   667
	render.itembegin = render.itembegin or
tmueller@169
   668
		function(self) -- , level, idx
tmueller@169
   669
			self:out('<li>\n')
tmueller@169
   670
		end
tmueller@169
   671
	render.itemend = render.itemend or
tmueller@169
   672
		function(self)
tmueller@169
   673
			self:out('</li>\n')
tmueller@169
   674
		end
tmueller@169
   675
	recurse = recurse == nil and true or recurse
tmueller@169
   676
	local path = level > 1 and self:getpath("/", level - 1) or nil
tmueller@198
   677
	local addnew = self.authuser_menu and
tmueller@198
   678
		(not self.ispubprofile or self.config.editablepubprofile)
tmueller@169
   679
	self:rmenu(level, render, path, addnew, recurse)
tmueller@80
   680
end
tmueller@80
   681
tmueller@80
   682
tmueller@188
   683
function Loona:loadcontent(fname)
tmueller@147
   684
	if fname then
tmueller@147
   685
		local f = open(self.contentdir .. "/" .. fname)
tmueller@147
   686
		local c = f:read("*a")
tmueller@147
   687
		f:close()
tmueller@147
   688
		return c
tmueller@147
   689
	end
tmueller@147
   690
	return ""
tmueller@147
   691
end
tmueller@147
   692
tmueller@147
   693
tmueller@188
   694
function Loona:loadmarkup(fname)
tmueller@147
   695
	return (fname and fname ~= "") and
tmueller@147
   696
		self:domarkup(self:loadcontent(fname)) or ""
tmueller@147
   697
end
tmueller@147
   698
tmueller@147
   699
tmueller@188
   700
function Loona:editable(editkey, fname, savename)
tmueller@198
   701
tmueller@147
   702
	local contentdir = self.contentdir
tmueller@147
   703
	local edit, show, hidden, extramsg, changed
tmueller@198
   704
tmueller@198
   705
	if self.authuser_edit or self.authuser_profile or self.authuser_modifyprofile or self_authuser_attr
tmueller@198
   706
		or self.authuser_menu then
tmueller@198
   707
tmueller@149
   708
		local hiddenvars = table.concat( {
tmueller@149
   709
			self:hidden("lang", self.args.lang),
tmueller@149
   710
			self:hidden("profile", self.profile),
tmueller@149
   711
			self:hidden("session", self.session.id),
tmueller@149
   712
			self:hidden("editkey", editkey) }, " ")
tmueller@198
   713
tmueller@147
   714
		local lockfname = fname and (contentdir .. "/" .. fname)
tmueller@198
   715
tmueller@147
   716
		if self.useralert and editkey == self.args.editkey then
tmueller@198
   717
tmueller@147
   718
			--	display user alert/request/confirmation
tmueller@198
   719
tmueller@147
   720
			hidden = true
tmueller@149
   721
			self:out([[
tmueller@149
   722
			<form action="]] .. self.document .. [[" method="post" accept-charset="utf-8">
tmueller@149
   723
				<fieldset>
tmueller@149
   724
					<legend>]] .. self.useralert.text ..[[</legend>
tmueller@149
   725
					]] .. (self.useralert.confirm or "") .. [[
tmueller@177
   726
					]] .. (self.useralert.returnto or "") .. [[
tmueller@149
   727
					<input type="submit" name="actioncancel" value="]] .. self.locale.CANCEL  ..[[" />
tmueller@149
   728
					]] .. hiddenvars .. [[
tmueller@149
   729
				</fieldset>
tmueller@149
   730
			</form>
tmueller@147
   731
			]])
tmueller@198
   732
tmueller@198
   733
		elseif self.args.actionnew and editkey == "main" and self.authuser_menu then
tmueller@198
   734
tmueller@147
   735
			--	form for creating a new section
tmueller@198
   736
tmueller@147
   737
			hidden = true
tmueller@149
   738
			if self.ispubprofile then
tmueller@149
   739
				self:out([[<h2><span class="warn">]] .. self.locale.WARNING_YOU_ARE_IN_PUBLISHED_PROFILE ..[[</span></h2>]])
tmueller@149
   740
			end
tmueller@149
   741
			self:out([[
tmueller@149
   742
			<form action="]] .. self.document .. [[" method="post" accept-charset="utf-8">
tmueller@149
   743
				<fieldset>
tmueller@149
   744
					<legend>
tmueller@149
   745
						]] .. self.locale.CREATE_NEW_SECTION_UNDER .. " " .. self.sectionpath .. [[
tmueller@149
   746
					</legend>
tmueller@149
   747
					<table>
tmueller@149
   748
						<tr>
tmueller@149
   749
							<td align="right">
tmueller@149
   750
								]] .. self.locale.PATHNAME .. [[
tmueller@149
   751
							</td>
tmueller@149
   752
							<td>
tmueller@149
   753
								<input size="30" maxlength="30" name="editname" />
tmueller@149
   754
							</td>
tmueller@149
   755
						</tr>
tmueller@149
   756
						<tr>
tmueller@149
   757
							<td align="right">
tmueller@149
   758
								]] .. self.locale.MENULABEL .. [[
tmueller@149
   759
							</td>
tmueller@149
   760
							<td>
tmueller@149
   761
								<input size="30" maxlength="50" name="editlabel" />
tmueller@149
   762
							</td>
tmueller@149
   763
						</tr>
tmueller@149
   764
						<tr>
tmueller@149
   765
							<td align="right">
tmueller@149
   766
								]] .. self.locale.WINDOWTITLE .. [[
tmueller@149
   767
							</td>
tmueller@149
   768
							<td>
tmueller@149
   769
								<input size="30" maxlength="50" name="edittitle" />
tmueller@149
   770
							</td>
tmueller@149
   771
						</tr>
tmueller@149
   772
						<tr>
tmueller@149
   773
							<td align="right">
tmueller@149
   774
								]] .. self.locale.INVISIBLE .. [[
tmueller@149
   775
							</td>
tmueller@149
   776
							<td>
tmueller@149
   777
								<input type="checkbox" name="editvisibility" />
tmueller@149
   778
							</td>
tmueller@149
   779
						</tr>
tmueller@149
   780
						<tr>
tmueller@149
   781
							<td align="right">
tmueller@149
   782
								]] .. self.locale.SECRET .. [[
tmueller@149
   783
							</td>
tmueller@149
   784
							<td>
tmueller@149
   785
								<input type="checkbox" name="editsecrecy" />
tmueller@149
   786
							</td>
tmueller@149
   787
						</tr>
tmueller@149
   788
						<tr>
tmueller@149
   789
							<td align="right">
tmueller@149
   790
								]] .. self.locale.SECURE_CONNECTION .. [[
tmueller@149
   791
							</td>
tmueller@149
   792
							<td>
tmueller@149
   793
								<input type="checkbox" name="editsecure" />
tmueller@149
   794
							</td>
tmueller@149
   795
						</tr>
tmueller@149
   796
						<tr>
tmueller@149
   797
							<td align="right">
tmueller@149
   798
								]] .. self.locale.REDIRECT .. [[
tmueller@149
   799
							</td>
tmueller@149
   800
							<td>
tmueller@149
   801
								<input size="30" maxlength="50" name="editredirect" />
tmueller@149
   802
							</td>
tmueller@149
   803
						</tr>
tmueller@198
   804
					</table>
tmueller@149
   805
					<input type="submit" name="actioncreate" value="]] .. self.locale.CREATE .. [[" />
tmueller@149
   806
					]] .. hiddenvars .. [[
tmueller@149
   807
				</fieldset>
tmueller@149
   808
			</form>
tmueller@149
   809
			<hr />
tmueller@147
   810
			]])
tmueller@198
   811
tmueller@198
   812
		elseif self.args.actioneditprops and editkey == "main" and self.authuser_attr then
tmueller@147
   813
			hidden = true
tmueller@149
   814
			if self.ispubprofile then
tmueller@149
   815
				self:out([[<h2><span class="warn">]] .. self.locale.WARNING_YOU_ARE_IN_PUBLISHED_PROFILE ..[[</span></h2>]])
tmueller@149
   816
			end
tmueller@149
   817
			self:out([[
tmueller@149
   818
			<form action="]] ..self.document .. [[" method="post" accept-charset="utf-8">
tmueller@149
   819
				<fieldset>
tmueller@149
   820
					<legend>
tmueller@149
   821
						]] .. self.locale.MODIFY_PROPERTIES_OF_SECTION .. " " .. self.sectionpath .. [[
tmueller@149
   822
					</legend>
tmueller@149
   823
					<table>
tmueller@149
   824
						<tr>
tmueller@149
   825
							<td align="right">
tmueller@149
   826
								]] .. self.locale.MENULABEL .. [[
tmueller@149
   827
							</td>
tmueller@149
   828
							<td>
tmueller@149
   829
								<input size="30" maxlength="50" name="editlabel" value="]] .. (self.section.label or "") .. [[" />
tmueller@149
   830
							</td>
tmueller@149
   831
						</tr>
tmueller@149
   832
						<tr>
tmueller@149
   833
							<td align="right">
tmueller@149
   834
								]] .. self.locale.WINDOWTITLE .. [[
tmueller@149
   835
							</td>
tmueller@149
   836
							<td>
tmueller@149
   837
								<input size="30" maxlength="50" name="edittitle" value="]] .. (self.section.title or "") .. [[" />
tmueller@149
   838
							</td>
tmueller@149
   839
						</tr>
tmueller@149
   840
						<tr>
tmueller@149
   841
							<td align="right">
tmueller@149
   842
								]] .. self.locale.INVISIBLE .. [[
tmueller@149
   843
							</td>
tmueller@149
   844
							<td>
tmueller@149
   845
								<input type="checkbox" name="editvisibility" ]] .. (self.section.hidden and 'checked="checked"' or "") .. [[/>
tmueller@149
   846
							</td>
tmueller@149
   847
						</tr>
tmueller@149
   848
						<tr>
tmueller@149
   849
							<td align="right">
tmueller@149
   850
								]] .. self.locale.SECRET .. [[
tmueller@149
   851
							</td>
tmueller@149
   852
							<td>
tmueller@149
   853
								<input type="checkbox" name="editsecrecy" ]] .. (self.section.secret and 'checked="checked"' or "") .. [[/>
tmueller@149
   854
							</td>
tmueller@149
   855
						</tr>
tmueller@149
   856
						<tr>
tmueller@149
   857
							<td align="right">
tmueller@149
   858
								]] .. self.locale.SECURE_CONNECTION .. [[
tmueller@149
   859
							</td>
tmueller@149
   860
							<td>
tmueller@149
   861
								<input type="checkbox" name="editsecure" ]] .. (self.section.secure and 'checked="checked"' or "") .. [[/>
tmueller@149
   862
							</td>
tmueller@149
   863
						</tr>
tmueller@149
   864
						<tr>
tmueller@149
   865
							<td align="right">
tmueller@149
   866
								]] .. self.locale.REDIRECT .. [[
tmueller@149
   867
							</td>
tmueller@149
   868
							<td>
tmueller@149
   869
								<input size="30" maxlength="50" name="editredirect" value="]] .. (self.section.redirect or "") .. [[" />
tmueller@149
   870
							</td>
tmueller@149
   871
						</tr>
tmueller@149
   872
					</table>
tmueller@149
   873
					<input type="submit" name="actionsaveprops" value="]] .. self.locale.SAVE .. [[" />
tmueller@149
   874
					<input type="submit" name="actioncancel" value="]] .. self.locale.CANCEL .. [[" />
tmueller@149
   875
					]] .. hiddenvars .. [[
tmueller@149
   876
				</fieldset>
tmueller@149
   877
			</form>
tmueller@147
   878
			]])
tmueller@198
   879
tmueller@147
   880
		elseif (self.args.actioneditprofiles or
tmueller@198
   881
			self.args.actioncreateprofile or
tmueller@198
   882
			self.args.actionchangeprofile or
tmueller@177
   883
			self.args.actionchangelanguage or
tmueller@198
   884
			self.args.actionpublishprofile) and editkey == "main" and
tmueller@198
   885
			(self.authuser_profile or self.authuser_modifyprofile) then
tmueller@147
   886
			hidden = true
tmueller@198
   887
			if self.authuser_profile then
tmueller@198
   888
				self:out([[
tmueller@198
   889
				<form action="]] .. self.document .. [[" method="post" accept-charset="utf-8">
tmueller@198
   890
					<fieldset>
tmueller@198
   891
						<legend>
tmueller@198
   892
							]] .. self.locale.CHANGEPROFILE .. [[
tmueller@198
   893
						</legend>
tmueller@198
   894
						<select name="changeprofile" size="1">]])
tmueller@198
   895
							for _, val in ipairs(self:getprofiles()) do
tmueller@198
   896
								self:out('<option' .. (val == self.profile and ' selected="selected"' or '') .. '>')
tmueller@198
   897
								self:out(val)
tmueller@198
   898
								self:out('</option>')
tmueller@198
   899
							end
tmueller@198
   900
						self:out([[
tmueller@198
   901
						</select>
tmueller@198
   902
						<input type="submit" name="actionchangeprofile" value="]] .. self.locale.CHANGE ..[[" />
tmueller@198
   903
						]] .. hiddenvars .. [[
tmueller@198
   904
					</fieldset>
tmueller@198
   905
				</form>
tmueller@198
   906
				<form action="]] .. self.document .. [[" method="post" accept-charset="utf-8">
tmueller@198
   907
					<fieldset>
tmueller@198
   908
						<legend>
tmueller@198
   909
							]] .. self.locale.CHANGELANGUAGE .. [[
tmueller@198
   910
						</legend>
tmueller@198
   911
						<select name="changelanguage" size="1">]])
tmueller@198
   912
							for _, val in ipairs(self:getlanguages()) do
tmueller@198
   913
								self:out('<option' .. (val == self.lang and ' selected="selected"' or '') .. '>')
tmueller@198
   914
								self:out(val)
tmueller@198
   915
								self:out('</option>')
tmueller@198
   916
							end
tmueller@198
   917
						self:out([[
tmueller@198
   918
						</select>
tmueller@198
   919
						<input type="submit" name="actionchangelanguage" value="]] .. self.locale.CHANGE ..[[" />
tmueller@198
   920
						]] .. hiddenvars .. [[
tmueller@198
   921
					</fieldset>
tmueller@198
   922
				</form>
tmueller@198
   923
				]])
tmueller@198
   924
			end
tmueller@198
   925
			if self.authuser_modifyprofile then
tmueller@198
   926
				self:out([[
tmueller@198
   927
				<form action="]] .. self.document ..[[" method="post" accept-charset="utf-8">
tmueller@198
   928
					<fieldset>
tmueller@198
   929
						<legend>
tmueller@198
   930
							]] .. self.locale.CREATEPROFILE .. [[
tmueller@198
   931
						</legend>
tmueller@198
   932
						<input size="20" maxlength="20" name="createprofile" />
tmueller@198
   933
						]] .. self.locale.LANGUAGE ..[[
tmueller@198
   934
						<input size="2" maxlength="2" name="createlanguage" value="]] .. self.lang ..[[" />
tmueller@198
   935
						<input type="submit" name="actioncreateprofile" value="]] .. self.locale.CREATE .. [[" />
tmueller@198
   936
						]] .. hiddenvars .. [[
tmueller@198
   937
					</fieldset>
tmueller@198
   938
				</form>
tmueller@198
   939
				]])
tmueller@198
   940
			end
tmueller@198
   941
			if not self.ispubprofile or self.config.editablepubprofile and
tmueller@198
   942
				self.authuser_modifyprofile then
tmueller@147
   943
				self:out([[
tmueller@147
   944
				<form action="]] .. self.document .. [[" method="post" accept-charset="utf-8">
tmueller@147
   945
					<fieldset>
tmueller@147
   946
						<legend>
tmueller@149
   947
							]] .. self.locale.PUBLISHPROFILE .. [[
tmueller@147
   948
						</legend>
tmueller@149
   949
						]] .. self:hidden("publishprofile", self.profile) .. [[
tmueller@149
   950
						<input type="submit" name="actionpublishprofile" value="]] .. self.locale.PUBLISH .. [[" />
tmueller@147
   951
						]] .. hiddenvars .. [[
tmueller@147
   952
					</fieldset>
tmueller@147
   953
				</form>
tmueller@149
   954
				<form action="]] .. self.document .. [[" method="post" accept-charset="utf-8">
tmueller@147
   955
					<fieldset>
tmueller@147
   956
						<legend>
tmueller@149
   957
							]] .. self.locale.DELETEPROFILE .. [[
tmueller@147
   958
						</legend>
tmueller@149
   959
						]] .. self:hidden("deleteprofile", self.profile) .. [[
tmueller@149
   960
						<input type="submit" name="actiondeleteprofile" value="]] .. self.locale.DELETE .. [[" />
tmueller@147
   961
						]] .. hiddenvars .. [[
tmueller@147
   962
					</fieldset>
tmueller@147
   963
				</form>
tmueller@147
   964
				]])
tmueller@149
   965
			end
tmueller@198
   966
tmueller@147
   967
		elseif self.args.actionedit and editkey == self.args.editkey then
tmueller@198
   968
			if not self.section.redirect and self.authuser_edit then
tmueller@147
   969
				extramsg = self.ispubprofile and
tmueller@147
   970
					self.locale.WARNING_YOU_ARE_IN_PUBLISHED_PROFILE
tmueller@147
   971
				edit = self:loadcontent(fname):gsub("\194\160", "&nbsp;") -- TODO
tmueller@147
   972
				changed = self.section and (self.section.revisiondate or self.section.creationdate)
tmueller@147
   973
			end
tmueller@198
   974
tmueller@198
   975
		elseif self.args.actionpreview and editkey == self.args.editkey and
tmueller@198
   976
			self.authuser_edit then
tmueller@147
   977
			edit = self.args.editform
tmueller@147
   978
			show = self:domarkup(edit:gsub("&nbsp;", "\194\160")) -- TODO
tmueller@198
   979
tmueller@198
   980
		elseif self.args.actionsave and editkey == self.args.editkey
tmueller@198
   981
			and self.authuser_edit then
tmueller@147
   982
			local c = self.args.editform
tmueller@147
   983
			local dynamic
tmueller@198
   984
tmueller@147
   985
			if lockfname then
tmueller@147
   986
				self:expire(contentdir, "[^.]%S+.LOCK")
tmueller@147
   987
				if self:lockfile(lockfname) then
tmueller@147
   988
					-- lock was expired, aquired a new one
tmueller@147
   989
					extramsg = self.locale.SECTION_COULD_HAVE_CHANGED
tmueller@147
   990
					edit = c
tmueller@147
   991
				else
tmueller@188
   992
					local tab = lib.source(lockfname .. ".LOCK")
tmueller@147
   993
					if tab and tab.id == self.session.id then
tmueller@147
   994
						-- lock already held and is mine - try to save:
tmueller@147
   995
						local savec = c:gsub("&nbsp;", "\194\160") -- TODO
tmueller@149
   996
						remove(contentdir .. "/" .. savename .. ".html")
tmueller@147
   997
						self:savebody(savename, savec)
tmueller@147
   998
						-- TODO: error handling
tmueller@147
   999
						self:unlockfile(lockfname)
tmueller@147
  1000
						show, dynamic = self:domarkup(savec)
tmueller@149
  1001
						changed = time()
tmueller@147
  1002
					else
tmueller@147
  1003
						-- lock was expired and someone else has it now
tmueller@147
  1004
						extramsg = self.locale.SECTION_IN_USE
tmueller@147
  1005
						edit = c
tmueller@147
  1006
					end
tmueller@147
  1007
				end
tmueller@147
  1008
			else
tmueller@147
  1009
				-- new sidefile
tmueller@147
  1010
				local savec = c:gsub("&nbsp;", "\194\160") -- TODO
tmueller@147
  1011
				self:savebody(savename, savec)
tmueller@147
  1012
				-- TODO: error handling
tmueller@147
  1013
				show, dynamic = self:domarkup(savec)
tmueller@147
  1014
			end
tmueller@198
  1015
tmueller@147
  1016
			-- mark dynamic text bodies
tmueller@147
  1017
			if not self.section.dynamic then
tmueller@147
  1018
				self.section.dynamic = { }
tmueller@147
  1019
			end
tmueller@147
  1020
			self.section.dynamic[editkey] = dynamic
tmueller@147
  1021
			local n = 0
tmueller@147
  1022
			for _ in pairs(self.section.dynamic) do
tmueller@147
  1023
				n = n + 1
tmueller@147
  1024
			end
tmueller@147
  1025
			if n == 0 then
tmueller@147
  1026
				self.section.dynamic = nil
tmueller@147
  1027
			end
tmueller@198
  1028
tmueller@147
  1029
			self:saveindex()
tmueller@198
  1030
tmueller@147
  1031
		elseif self.args.actioncancel and editkey == self.args.editkey then
tmueller@147
  1032
			if lockfname then
tmueller@147
  1033
				self:unlockfile(lockfname) -- remove lock
tmueller@147
  1034
			end
tmueller@147
  1035
		end
tmueller@198
  1036
tmueller@147
  1037
		if editkey == "main" and self.section and self.section.redirect then
tmueller@149
  1038
			self:out('<h2>' .. self.locale.SECTION_IS_REDIRECT ..'</h2>')
tmueller@149
  1039
			self:out(self:link(self.section.redirect))
tmueller@149
  1040
			self:out('<hr />')
tmueller@147
  1041
		end
tmueller@198
  1042
tmueller@147
  1043
		if edit then
tmueller@147
  1044
			self:expire(contentdir, "[^.]%S+.LOCK")
tmueller@147
  1045
			if fname and not self:lockfile(contentdir .. "/" .. fname) then
tmueller@188
  1046
				local tab = lib.source(contentdir .. "/" .. fname .. ".LOCK")
tmueller@147
  1047
				if tab and tab.id ~= self.session.id then
tmueller@147
  1048
					extramsg = self.locale.SECTION_IN_USE
tmueller@147
  1049
				end
tmueller@147
  1050
				-- else already owner
tmueller@147
  1051
			end
tmueller@149
  1052
			if extramsg then
tmueller@149
  1053
				self:out('<h2><span class="warn">' .. extramsg .. '</span></h2>')
tmueller@149
  1054
			end
tmueller@149
  1055
			self:out([[
tmueller@149
  1056
			<form action="]] .. self.document .. [[#preview" method="post" accept-charset="utf-8">
tmueller@149
  1057
				<fieldset>
tmueller@149
  1058
					<legend>
tmueller@149
  1059
						]] .. self.locale.EDIT_SECTION .. [[
tmueller@149
  1060
					</legend>
tmueller@168
  1061
					<textarea cols="80" rows="25" name="editform">
tmueller@168
  1062
]] .. self:encodeform(edit) .. [[</textarea>
tmueller@149
  1063
					<br />
tmueller@149
  1064
					<input type="submit" name="actionsave" value="]] .. self.locale.SAVE .. [[" />
tmueller@149
  1065
					<input type="submit" name="actionpreview" value="]] .. self.locale.PREVIEW .. [[" />
tmueller@149
  1066
					<input type="submit" name="actioncancel" value="]] .. self.locale.CANCEL .. [[" />
tmueller@149
  1067
					]] .. hiddenvars .. [[
tmueller@149
  1068
				</fieldset>
tmueller@149
  1069
			</form>
tmueller@147
  1070
			]])
tmueller@147
  1071
		end
tmueller@198
  1072
	end
tmueller@198
  1073
tmueller@147
  1074
	if not hidden then
tmueller@149
  1075
		self:dosnippet(function()
tmueller@149
  1076
			if not show then
tmueller@149
  1077
				show = self:loadmarkup(fname)
tmueller@149
  1078
				changed = self.section and (self.section.revisiondate or self.section.creationdate)
tmueller@149
  1079
			end
tmueller@149
  1080
			local parsed, msg = self:loadhtml(show, "loona:out", "<parsed html>")
tmueller@149
  1081
			assert(parsed, msg and "Syntax error : " .. msg)
tmueller@149
  1082
			self:runboxed(parsed)
tmueller@147
  1083
		end)
tmueller@147
  1084
	end
tmueller@198
  1085
tmueller@198
  1086
	if self.authuser_profile or self.authuser_edit or self.authuser_menu or self.authuser_attr then
tmueller@149
  1087
		self:out([[
tmueller@149
  1088
		<hr />
tmueller@149
  1089
		<div class="edit">]])
tmueller@149
  1090
			if editkey == "main" then
tmueller@149
  1091
				self:out([[
tmueller@149
  1092
				<a name="preview"></a>
tmueller@198
  1093
				]] .. self.authuser .. [[ : ]])
tmueller@198
  1094
				if self.authuser_profile then
tmueller@198
  1095
					self:out(self:uilink(self.sectionpath, "[" .. self.locale.PROFILE .. "]", "actioneditprofiles=true", "editkey=" .. editkey) .. [[ :
tmueller@198
  1096
						]] .. self.profile .. "_" .. self.lang)
tmueller@198
  1097
					if self.ispubprofile then
tmueller@198
  1098
						self:out([[
tmueller@198
  1099
							<span class="warn">[]] .. self.locale.PUBLIC .. [[]</span>]])
tmueller@198
  1100
					end
tmueller@198
  1101
					self:out(" : ")
tmueller@149
  1102
				end
tmueller@198
  1103
				self:out(self.sectionpath .. ' ')
tmueller@149
  1104
			end
tmueller@198
  1105
			if self.section and (not self.ispubprofile or self.config.editablepubprofile) then
tmueller@198
  1106
				if self.authuser_edit then
tmueller@198
  1107
					self:out('- ' .. self:uilink(self.sectionpath, "[" .. self.locale.EDIT .. "]", "actionedit=true", "editkey=" .. editkey) .. " ")
tmueller@198
  1108
				end
tmueller@198
  1109
				if editkey == "main" and self.authuser_attr then
tmueller@149
  1110
					self:out('- ' .. self:uilink(self.sectionpath, "[" .. self.locale.PROPERTIES .. "]", "actioneditprops=true", "editkey=" .. editkey) .. " ")
tmueller@147
  1111
				end
tmueller@198
  1112
				if (fname == savename or not self.section.subs) and (self.authuser_edit and self.authuser_menu) then
tmueller@149
  1113
					self:out('- ' .. self:uilink(self.sectionpath, "[" .. self.locale.DELETE .. "]", "actiondelete=true", "editkey=" .. editkey) .. " ")
tmueller@147
  1114
				end
tmueller@198
  1115
				if editkey == "main" and self.authuser_menu then
tmueller@149
  1116
					self:out('- ' .. self:uilink(self.sectionpath, "[" .. self.locale.MOVEUP .. "]", "actionup=true", "editkey=" .. editkey) .. " ")
tmueller@149
  1117
					self:out('- ' .. self:uilink(self.sectionpath, "[" .. self.locale.MOVEDOWN .. "]", "actiondown=true", "editkey=" .. editkey) .. " ")
tmueller@149
  1118
				end
tmueller@149
  1119
				if changed and editkey == "main" then
tmueller@149
  1120
					self:out('- ' .. self.locale.CHANGED .. ': ' .. date("%d-%b-%Y %T", changed))
tmueller@149
  1121
				end
tmueller@149
  1122
			end
tmueller@149
  1123
		self:out('</div>')
tmueller@147
  1124
	end
tmueller@147
  1125
tmueller@147
  1126
end
tmueller@147
  1127
tmueller@147
  1128
tmueller@147
  1129
--	Get pathname of an existing content file that
tmueller@147
  1130
--	the current path is determined by (or defaults to)
tmueller@147
  1131
tmueller@188
  1132
function Loona:getsectionpath(bodyname, requestpath)
tmueller@147
  1133
	local ext = (not bodyname or bodyname == "main") and "" or "." .. bodyname
tmueller@147
  1134
	local t, path, section = { }
tmueller@147
  1135
	for _, menu in ipairs(self.submenus) do
tmueller@147
  1136
		if menu.entries and menu.entries[menu.name] then
tmueller@147
  1137
			table.insert(t, menu.name)
tmueller@147
  1138
			local fn = table.concat(t, "_")
tmueller@198
  1139
			if posix.stat(self.contentdir .. "/" .. fn .. ext,
tmueller@147
  1140
				"mode") == "file" then
tmueller@147
  1141
				path, section = fn, menu
tmueller@147
  1142
			end
tmueller@147
  1143
		end
tmueller@147
  1144
	end
tmueller@147
  1145
	return path, ext, section
tmueller@147
  1146
end
tmueller@147
  1147
tmueller@147
  1148
tmueller@188
  1149
function Loona:body(name)
tmueller@147
  1150
	name = self:checkbodyname(name)
tmueller@147
  1151
	local path, ext = self:getsectionpath(name)
tmueller@149
  1152
	self:dosnippet(function()
tmueller@149
  1153
		self:editable(name, path and path .. ext, self.sectionname .. ext)
tmueller@149
  1154
	end)
tmueller@147
  1155
end
tmueller@147
  1156
tmueller@147
  1157
tmueller@188
  1158
function Loona:init()
tmueller@198
  1159
tmueller@0
  1160
	-- get list of languages, in order of preference
tmueller@141
  1161
	-- TODO: respect quality parameter, not just order
tmueller@198
  1162
tmueller@141
  1163
	local l = self.requestlang or self.args.lang
tmueller@141
  1164
	self.langs = { l and l:match("^%w+$") }
tmueller@180
  1165
	local s = getenv("HTTP_ACCEPT_LANGUAGE")
tmueller@180
  1166
	while s do
tmueller@180
  1167
		local l, r = s:match("^([%w.=]+)[,;](.*)$")
tmueller@180
  1168
		l = l or s
tmueller@180
  1169
		s = r
tmueller@180
  1170
		if l:match("^%w+$") then
tmueller@180
  1171
			table.insert(self.langs, l)
tmueller@0
  1172
		end
tmueller@180
  1173
	end
tmueller@129
  1174
	table.insert(self.langs, self.config.deflang)
tmueller@198
  1175
tmueller@0
  1176
	-- get list of possible profiles
tmueller@198
  1177
tmueller@0
  1178
	local profiles = { }
tmueller@188
  1179
	for e in util.readdir(self.config.contentdir) do
tmueller@0
  1180
		profiles[e] = e
tmueller@0
  1181
	end
tmueller@198
  1182
tmueller@0
  1183
	-- get pubprofile
tmueller@198
  1184
tmueller@129
  1185
	for _, lang in ipairs(self.langs) do
tmueller@129
  1186
		local p = posix.readlink(self.config.contentdir .. "/current_" .. lang)
tmueller@0
  1187
		p = p and p:match("^(%w+)_" .. lang .. "$")
tmueller@0
  1188
		if p then
tmueller@129
  1189
			self.pubprofile = p
tmueller@0
  1190
			break
tmueller@0
  1191
		end
tmueller@0
  1192
	end
tmueller@198
  1193
tmueller@0
  1194
	-- get profile
tmueller@198
  1195
tmueller@143
  1196
	local checkprofile =
tmueller@198
  1197
		self.authuser_profile and self.args.profile or self.pubprofile or "work"
tmueller@129
  1198
	for _, lang in ipairs(self.langs) do
tmueller@129
  1199
		if profiles[checkprofile .. "_" .. lang] then
tmueller@129
  1200
			self.profile = checkprofile
tmueller@129
  1201
			self.lang = lang
tmueller@0
  1202
			break
tmueller@0
  1203
		end
tmueller@0
  1204
	end
tmueller@198
  1205
tmueller@129
  1206
	assert(self.profile and self.lang, "Invalid profile or language")
tmueller@198
  1207
tmueller@198
  1208
tmueller@143
  1209
	self.ispubprofile = self.profile == self.pubprofile
tmueller@198
  1210
tmueller@129
  1211
	-- write back language and profile
tmueller@198
  1212
tmueller@180
  1213
	self.args.lang = (self.explicitlang or self.lang ~= self.config.deflang)
tmueller@180
  1214
		and self.lang or nil
tmueller@129
  1215
	self.args.profile = self.profile
tmueller@198
  1216
tmueller@0
  1217
	-- determine content directory pathname and section filename
tmueller@198
  1218
tmueller@143
  1219
	self.contentdir =
tmueller@143
  1220
		("%s/%s_%s"):format(self.config.contentdir, self.profile, self.lang)
tmueller@129
  1221
 	self.indexfname = self.contentdir .. "/.sections"
tmueller@198
  1222
tmueller@0
  1223
	-- load sections
tmueller@198
  1224
tmueller@188
  1225
 	self.sections = lib.source(self.indexfname)
tmueller@198
  1226
tmueller@20
  1227
	-- index sections, determine visibility in menu
tmueller@198
  1228
tmueller@129
  1229
	self:indexsections()
tmueller@198
  1230
tmueller@129
  1231
	-- decompose request path, produce a stack of sections
tmueller@198
  1232
tmueller@129
  1233
	self.submenus, self.section = self:getsection(self.requestpath)
tmueller@0
  1234
tmueller@0
  1235
	-- handle redirects if not logged on
tmueller@198
  1236
tmueller@198
  1237
	if not self.authuser_edit and self.section and self.section.redirect then
tmueller@129
  1238
		self.submenus, self.section = self:getsection(self.section.redirect)
tmueller@0
  1239
	end
tmueller@198
  1240
tmueller@0
  1241
	-- section path and document name (refined)
tmueller@198
  1242
tmueller@129
  1243
	self.sectionpath = self:getpath()
tmueller@129
  1244
	self.sectionname = self:getpath("_")
tmueller@0
  1245
tmueller@0
  1246
end
tmueller@0
  1247
tmueller@0
  1248
tmueller@188
  1249
function Loona:handlechanges()
tmueller@198
  1250
tmueller@129
  1251
	local save
tmueller@0
  1252
tmueller@129
  1253
	if self.args.editkey == "main" then
tmueller@198
  1254
tmueller@23
  1255
		-- In main editable section:
tmueller@198
  1256
tmueller@129
  1257
		if self.args.actioncreate then
tmueller@198
  1258
tmueller@45
  1259
			-- Create new section
tmueller@198
  1260
tmueller@129
  1261
			local editname = self.args.editname:lower()
tmueller@20
  1262
			assert(not editname:match("%W"),
tmueller@158
  1263
				self:dbmsg("Invalid section name", editname))
tmueller@20
  1264
			if not (section and (section.subs or section)[editname]) then
tmueller@198
  1265
				local newpath = (self.sectionpath and
tmueller@143
  1266
					(self.sectionpath .. "/")) .. editname
tmueller@129
  1267
				local s = self:addpath(newpath, { name = editname,
tmueller@143
  1268
					label = self.args.editlabel ~= "" and
tmueller@143
  1269
						self.args.editlabel or nil,
tmueller@143
  1270
					title = self.args.edittitle ~= "" and
tmueller@143
  1271
						self.args.edittitle or nil,
tmueller@143
  1272
					redirect = self.args.editredirect ~= "" and
tmueller@143
  1273
						self.args.editredirect or nil,
tmueller@129
  1274
					hidden = self.args.editvisibility and true,
tmueller@129
  1275
					secret = self.args.editsecrecy and true,
tmueller@129
  1276
					secure = self.args.editsecure and true,
tmueller@129
  1277
					creator = self.authuser,
tmueller@20
  1278
					creationdate = time() })
tmueller@23
  1279
				save = true
tmueller@0
  1280
			end
tmueller@198
  1281
tmueller@129
  1282
		elseif self.args.actionsave then
tmueller@198
  1283
tmueller@45
  1284
			-- Save section
tmueller@198
  1285
tmueller@129
  1286
			self.section.revisiondate = time()
tmueller@129
  1287
			self.section.revisioner = self.authuser
tmueller@129
  1288
			save = true
tmueller@198
  1289
tmueller@129
  1290
		elseif self.args.actionsaveprops then
tmueller@198
  1291
tmueller@129
  1292
			-- Save properties
tmueller@198
  1293
tmueller@129
  1294
			self.section.hidden = self.args.editvisibility and true
tmueller@129
  1295
			self.section.secret = self.args.editsecrecy and true
tmueller@129
  1296
			self.section.secure = self.args.editsecure and true
tmueller@143
  1297
			self.section.label = self.args.editlabel ~= "" and
tmueller@143
  1298
				self.args.editlabel or nil
tmueller@143
  1299
			self.section.title = self.args.edittitle ~= "" and
tmueller@143
  1300
				self.args.edittitle or nil
tmueller@129
  1301
			self.section.redirect =
tmueller@129
  1302
				self.args.editredirect ~= "" and self.args.editredirect or nil
tmueller@23
  1303
			save = true
tmueller@198
  1304
tmueller@129
  1305
		elseif self.args.actionup then
tmueller@198
  1306
tmueller@45
  1307
			-- Move section up
tmueller@198
  1308
tmueller@129
  1309
			local t, i = self:checkpath(self.sectionpath)
tmueller@0
  1310
			if t and i > 1 then
tmueller@143
  1311
				if self.ispubprofile and not self.args.actionconfirm then
tmueller@198
  1312
					self.useralert = {
tmueller@144
  1313
						text = self.locale.ALERT_MOVE_IN_PUBLISHED_PROFILE,
tmueller@115
  1314
						confirm =
tmueller@144
  1315
							'<input type="submit" name="actionup" value="' ..
tmueller@129
  1316
							self.locale.MOVE .. '" /> ' ..
tmueller@129
  1317
							self:hidden("actionconfirm", "true")
tmueller@115
  1318
					}
tmueller@115
  1319
				else
tmueller@115
  1320
					local item = table.remove(t, i)
tmueller@115
  1321
					table.insert(t, i - 1, item)
tmueller@115
  1322
					save = true
tmueller@115
  1323
				end
tmueller@0
  1324
			end
tmueller@198
  1325
tmueller@129
  1326
		elseif self.args.actiondown then
tmueller@198
  1327
tmueller@45
  1328
			-- Move section down
tmueller@198
  1329
tmueller@129
  1330
			local t, i = self:checkpath(self.sectionpath)
tmueller@0
  1331
			if t and i < #t then
tmueller@143
  1332
				if self.ispubprofile and not self.args.actionconfirm then
tmueller@198
  1333
					self.useralert = {
tmueller@144
  1334
						text = self.locale.ALERT_MOVE_IN_PUBLISHED_PROFILE,
tmueller@115
  1335
						confirm =
tmueller@144
  1336
							'<input type="submit" name="actiondown" value="' ..
tmueller@129
  1337
							self.locale.MOVE .. '" /> ' ..
tmueller@129
  1338
							self:hidden("actionconfirm", "true")
tmueller@115
  1339
					}
tmueller@115
  1340
				else
tmueller@115
  1341
					local item = table.remove(t, i)
tmueller@115
  1342
					table.insert(t, i + 1, item)
tmueller@115
  1343
					save = true
tmueller@115
  1344
				end
tmueller@0
  1345
			end
tmueller@198
  1346
tmueller@129
  1347
		elseif self.args.actioncreateprofile and self.args.createprofile then
tmueller@198
  1348
tmueller@23
  1349
			-- Create profile
tmueller@198
  1350
tmueller@175
  1351
			local c = self.args.createprofile
tmueller@175
  1352
			if c == "" then
tmueller@175
  1353
				c = self.profile
tmueller@175
  1354
			end
tmueller@175
  1355
			c = self:checkprofilename(c:lower())
tmueller@175
  1356
			local l = self:checklanguage((self.args.createlanguage or self.lang):lower())
tmueller@175
  1357
			if c == self.profile and l == self.lang then
tmueller@198
  1358
				self.useralert = {
tmueller@177
  1359
					text = self.locale.ALERT_CANNOT_COPY_PROFILE_TO_SELF,
tmueller@177
  1360
					returnto = self:hidden("actioneditprofiles", "true")
tmueller@144
  1361
				}
tmueller@0
  1362
			else
tmueller@175
  1363
				local profiles = self:getprofiles(l)
tmueller@177
  1364
				local text
tmueller@177
  1365
				if c == self.pubprofile then
tmueller@177
  1366
					text = self.locale.ALERT_OVERWRITE_PUBLISHED_PROFILE
tmueller@177
  1367
				elseif profiles[c] and l == self.lang then
tmueller@177
  1368
					text = self.locale.ALERT_OVERWRITE_EXISTING_PROFILE
tmueller@177
  1369
				end
tmueller@177
  1370
				if text and not self.args.actionconfirm then
tmueller@198
  1371
					self.useralert = {
tmueller@177
  1372
						text = text,
tmueller@177
  1373
						returnto = self:hidden("actioneditprofiles", "true"),
tmueller@144
  1374
						confirm = '<input type="submit" ' ..
tmueller@144
  1375
							'name="actioncreateprofile" value="' ..
tmueller@129
  1376
							self.locale.OVERWRITE .. '" /> ' ..
tmueller@144
  1377
							self:hidden("actionconfirm", "true") ..
tmueller@175
  1378
							self:hidden("createlanguage", l) ..
tmueller@129
  1379
							self:hidden("createprofile", c)
tmueller@20
  1380
					}
tmueller@0
  1381
				else
tmueller@0
  1382
					if profiles[c] then
tmueller@175
  1383
						self:deleteprofile(c, l)
tmueller@0
  1384
					end
tmueller@175
  1385
					self:copyprofile(c, self.profile, l, self.lang)
tmueller@0
  1386
				end
tmueller@0
  1387
			end
tmueller@198
  1388
tmueller@129
  1389
		elseif self.args.actiondeleteprofile and self.args.deleteprofile then
tmueller@198
  1390
tmueller@23
  1391
			-- Delete profile
tmueller@198
  1392
tmueller@129
  1393
			local c = self:checkprofilename(self.args.deleteprofile:lower())
tmueller@144
  1394
			assert(c ~= self.pubprofile,
tmueller@144
  1395
				self:dbmsg("Cannot delete published profile", c))
tmueller@129
  1396
			if self.args.actionconfirm then
tmueller@129
  1397
				self:deleteprofile(c)
tmueller@129
  1398
				self.profile = nil
tmueller@129
  1399
				self.args.profile = nil
tmueller@129
  1400
				self:init()
tmueller@23
  1401
				save = true
tmueller@0
  1402
			else
tmueller@198
  1403
				self.useralert = {
tmueller@129
  1404
					text = self.locale.ALERT_DELETE_PROFILE,
tmueller@179
  1405
					returnto = self:hidden("actioneditprofiles", "true"),
tmueller@144
  1406
					confirm = '<input type="submit" ' ..
tmueller@198
  1407
						'name="actiondeleteprofile" value="' ..
tmueller@129
  1408
						self.locale.DELETE .. '" /> ' ..
tmueller@129
  1409
						self:hidden("actionconfirm", "true") ..
tmueller@129
  1410
						self:hidden("deleteprofile", c)
tmueller@20
  1411
				}
tmueller@0
  1412
			end
tmueller@198
  1413
tmueller@129
  1414
		elseif self.args.actionchangeprofile and self.args.changeprofile then
tmueller@198
  1415
tmueller@23
  1416
			-- Change profile
tmueller@198
  1417
tmueller@129
  1418
			local c = self:checkprofilename(self.args.changeprofile:lower())
tmueller@129
  1419
			self.profile = c
tmueller@129
  1420
			self.args.profile = c
tmueller@23
  1421
			save = true
tmueller@198
  1422
tmueller@175
  1423
		elseif self.args.actionchangelanguage and self.args.changelanguage then
tmueller@198
  1424
tmueller@175
  1425
			-- Change language
tmueller@198
  1426
tmueller@175
  1427
			local l = self:checklanguage(self.args.changelanguage:lower())
tmueller@175
  1428
			self.lang = l
tmueller@175
  1429
			self.args.lang = l
tmueller@175
  1430
 			self.explicitlang = l
tmueller@175
  1431
			save = true
tmueller@198
  1432
tmueller@129
  1433
		elseif self.args.actionpublishprofile and self.args.publishprofile then
tmueller@198
  1434
tmueller@23
  1435
			-- Publish profile
tmueller@198
  1436
tmueller@129
  1437
			local c = self:checkprofilename(self.args.publishprofile:lower())
tmueller@129
  1438
			if c ~= self.publicprofile then
tmueller@129
  1439
				if self.args.actionconfirm then
tmueller@129
  1440
					self:publishprofile(c)
tmueller@23
  1441
					save = true
tmueller@0
  1442
				else
tmueller@198
  1443
					self.useralert = {
tmueller@129
  1444
						text = self.locale.ALERT_PUBLISH_PROFILE,
tmueller@179
  1445
						returnto = self:hidden("actioneditprofiles", "true"),
tmueller@144
  1446
						confirm = '<input type="submit" ' ..
tmueller@144
  1447
							'name="actionpublishprofile" value="' ..
tmueller@129
  1448
							self.locale.PUBLISH .. '" /> ' ..
tmueller@129
  1449
							self:hidden("actionconfirm", "true") ..
tmueller@129
  1450
							self:hidden("publishprofile", c)
tmueller@20
  1451
					}
tmueller@20
  1452
				end
tmueller@20
  1453
			end
tmueller@0
  1454
		end
tmueller@198
  1455
tmueller@129
  1456
	end
tmueller@198
  1457
tmueller@129
  1458
	if self.args.actiondelete then
tmueller@198
  1459
tmueller@129
  1460
		-- Delete section
tmueller@198
  1461
tmueller@129
  1462
		if not self.args.actionconfirm then
tmueller@198
  1463
			self.useralert = {
tmueller@143
  1464
				text = self.ispubprofile and
tmueller@144
  1465
					self.locale.ALERT_DELETE_IN_PUBLISHED_PROFILE or
tmueller@129
  1466
					self.locale.ALERT_DELETE_SECTION,
tmueller@129
  1467
				confirm =
tmueller@198
  1468
					'<input type="submit" name="actiondelete" value="' ..
tmueller@129
  1469
					self.locale.DELETE .. '" /> ' ..
tmueller@129
  1470
					self:hidden("actionconfirm", "true")
tmueller@129
  1471
			}
tmueller@129
  1472
		else
tmueller@129
  1473
			local key = self.args.editkey
tmueller@129
  1474
			if key == "main" and not self.section.subs then
tmueller@129
  1475
				self:deletesection(self.sectionname, true) -- all bodies
tmueller@129
  1476
				self:rmpath(self.sectionpath) -- and node
tmueller@129
  1477
			else
tmueller@129
  1478
				local ext = (key == "main" and "") or "." .. key
tmueller@129
  1479
				self:deletesection(self.sectionname .. ext) -- only text
tmueller@129
  1480
				if self.section.dynamic then
tmueller@129
  1481
					self.section.dynamic[key] = nil
tmueller@131
  1482
					local n = 0
tmueller@131
  1483
					for _ in pairs(self.section.dynamic) do
tmueller@131
  1484
						n = n + 1
tmueller@131
  1485
					end
tmueller@131
  1486
					if n == 0 then
tmueller@131
  1487
						self.section.dynamic = nil
tmueller@131
  1488
					end
tmueller@129
  1489
				end
tmueller@129
  1490
			end
tmueller@129
  1491
			save = true
tmueller@0
  1492
		end
tmueller@0
  1493
	end
tmueller@198
  1494
tmueller@129
  1495
	if save then
tmueller@129
  1496
		self:saveindex()
tmueller@129
  1497
		self:init()
tmueller@129
  1498
	end
tmueller@198
  1499
tmueller@0
  1500
end
tmueller@0
  1501
tmueller@0
  1502
tmueller@188
  1503
function Loona:encodeform(s)
tmueller@189
  1504
	return util.encodeform(s)
tmueller@129
  1505
end
tmueller@0
  1506
tmueller@80
  1507
tmueller@188
  1508
function Loona:loadhtml(src, outfunc, chunkname)
tmueller@188
  1509
 	return luahtml.load(src, outfunc, chunkname)
tmueller@129
  1510
end
tmueller@80
  1511
tmueller@80
  1512
tmueller@188
  1513
function Loona:domarkup(s)
tmueller@188
  1514
	return markup.load(s)
tmueller@129
  1515
end
tmueller@0
  1516
tmueller@0
  1517
tmueller@188
  1518
function Loona:expire(dir, pat, maxage)
tmueller@188
  1519
	return util.expire(dir, pat, maxage or self.config.sessionmaxage)
tmueller@129
  1520
end
tmueller@129
  1521
tmueller@23
  1522
tmueller@198
  1523
function Loona.new(class, self)
tmueller@198
  1524
tmueller@198
  1525
	self = Atom.new(class, self or { })
tmueller@0
  1526
tmueller@129
  1527
	local parsed, msg
tmueller@198
  1528
tmueller@188
  1529
	-- Buffer
tmueller@198
  1530
tmueller@198
  1531
	self.out = self.out or function(self, s)
tmueller@188
  1532
		self.buf:out(s)
tmueller@188
  1533
	end
tmueller@198
  1534
	self.addheader = self.addheader or function(self, s)
tmueller@198
  1535
		self.buf:addheader(s)
tmueller@188
  1536
	end
tmueller@198
  1537
tmueller@129
  1538
	-- Get configuration
tmueller@198
  1539
tmueller@198
  1540
	self.config = self.config or lib.source(self.conffile or "../etc/config.lua") or { }
tmueller@198
  1541
	self.config.defname = self.config.defname or "home"
tmueller@198
  1542
	self.config.deflang = self.config.deflang or "en"
tmueller@198
  1543
	self.config.sessionmaxage = self.config.sessionmaxage or 6000
tmueller@198
  1544
	self.config.secureport = self.config.secureport or 443
tmueller@198
  1545
	self.config.passwdfile =
tmueller@198
  1546
		posix.abspath(self.config.passwdfile or "../etc/passwd.lua")
tmueller@198
  1547
	self.config.sessiondir =
tmueller@198
  1548
		posix.abspath(self.config.sessiondir or "../var/sessions")
tmueller@198
  1549
	self.config.extdir = posix.abspath(self.config.extdir or "../extensions")
tmueller@198
  1550
	self.config.contentdir = posix.abspath(self.config.contentdir or "../content")
tmueller@198
  1551
	self.config.localedir = posix.abspath(self.config.localedir or "../locale")
tmueller@198
  1552
	self.config.htdocsdir = posix.abspath(self.config.htdocsdir or "../htdocs")
tmueller@198
  1553
	self.config.htmlcachedir =
tmueller@198
  1554
		posix.abspath(self.config.htmlcachedir or "../var/htmlcache")
tmueller@198
  1555
	self.config.extlinkextra = self.config.extlinksamewindow and ' class="extlink"'
tmueller@162
  1556
		or ' class="extlink" onclick="void(window.open(this.href, \'\', \'\')); return false;"'
tmueller@198
  1557
tmueller@129
  1558
	-- Create proxy for on-demand loading of locales
tmueller@198
  1559
tmueller@198
  1560
	self.locale = { }
tmueller@129
  1561
	local locmt = { }
tmueller@129
  1562
	locmt.__index = function(_, key)
tmueller@198
  1563
		for _, l in ipairs(self.langs) do
tmueller@198
  1564
			locmt.__locale = lib.source(self.config.localedir .. "/" .. l)
tmueller@129
  1565
			if locmt.__locale then
tmueller@129
  1566
				break
tmueller@129
  1567
			end
tmueller@129
  1568
		end
tmueller@129
  1569
		locmt.__index = function(tab, key)
tmueller@129
  1570
			return locmt.__locale[key] or key
tmueller@129
  1571
		end
tmueller@129
  1572
		return locmt.__locale[key] or key
tmueller@129
  1573
	end
tmueller@198
  1574
	setmetatable(self.locale, locmt)
tmueller@198
  1575
tmueller@129
  1576
	-- Get request, args, document, script name, request path
tmueller@198
  1577
tmueller@198
  1578
	self.request = self.request or Request:new()
tmueller@198
  1579
	self.args = self.request:getargs()
tmueller@198
  1580
	self.cgi_document = self.request:getdocument()
tmueller@198
  1581
tmueller@198
  1582
--  	self.scriptpath = self.scriptpath or self.cgi_document.Path
tmueller@198
  1583
	self.requesthandler = self.requesthandler or self.cgi_document.Handler
tmueller@198
  1584
 	self.requestdocument = self.requestdocument or self.cgi_document.Name
tmueller@198
  1585
	self.requestpath = self.requestpath or self.cgi_document.VirtualPath
tmueller@198
  1586
	self.explicitlang = not self.requestlang and self.args.lang
tmueller@198
  1587
	self.secure = not self.insecure and (self.request.SERVER_PORT == self.config.secureport)
tmueller@129
  1588
tmueller@129
  1589
	-- Manage login and establish session
tmueller@198
  1590
tmueller@198
  1591
	if not self.nologin then
tmueller@198
  1592
		local sid = self.args.session or self.request.UNIQUE_ID
tmueller@198
  1593
		self.session = self.session or Session:new {
tmueller@188
  1594
			id = sid,
tmueller@198
  1595
			sessiondir = self.config.sessiondir,
tmueller@198
  1596
			maxage = self.config.sessionmaxage
tmueller@188
  1597
		}
tmueller@198
  1598
		if self.args.login then
tmueller@188
  1599
			-- write back session ID into request args:
tmueller@198
  1600
			self.args.session = sid -- !
tmueller@198
  1601
			if self.args.login == "false" then
tmueller@198
  1602
				self.session:delete()
tmueller@198
  1603
				self.session = nil
tmueller@198
  1604
			elseif self.args.password then
tmueller@198
  1605
				self.loginfailed = true
tmueller@198
  1606
				local match, username, perm =
tmueller@198
  1607
					self:checkpw(self.args.login, self.args.password)
tmueller@198
  1608
				if match then
tmueller@198
  1609
					self.session.data.authuser = self.args.login
tmueller@198
  1610
					self.session.data.username = username
tmueller@198
  1611
					self.session.data.permissions = perm
tmueller@198
  1612
					self.session.data.id = self.session.id
tmueller@198
  1613
					self.loginfailed = nil
tmueller@183
  1614
				end
tmueller@129
  1615
			end
tmueller@129
  1616
		end
tmueller@198
  1617
		self.authuser = self.session and self.session.data.authuser
tmueller@129
  1618
	end
tmueller@183
  1619
tmueller@198
  1620
	if self.nologin or not self.authuser then
tmueller@198
  1621
		self.authuser = nil
tmueller@198
  1622
		self.session = nil
tmueller@198
  1623
		self.args.session = nil
tmueller@198
  1624
	else
tmueller@198
  1625
		self.authuser_edit = self.session.data.permissions:find("e") and true
tmueller@198
  1626
		self.authuser_menu = self.session.data.permissions:find("m") and true
tmueller@198
  1627
		self.authuser_attr = self.session.data.permissions:find("a") and true
tmueller@198
  1628
		self.authuser_profile = self.session.data.permissions:find("p") and true
tmueller@198
  1629
		self.authuser_modifyprofile = self.session.data.permissions:find("c") and true
tmueller@198
  1630
		self.authuser_visible = self.session.data.permissions:find("v") and true
tmueller@198
  1631
		self.authuser_debug = self.session.data.permissions:find("d") and true
tmueller@129
  1632
	end
tmueller@183
  1633
tmueller@129
  1634
	-- Get lang, locale, profile, section
tmueller@183
  1635
tmueller@198
  1636
	self:init()
tmueller@198
  1637
	if self.authuser then -- TODO?
tmueller@198
  1638
		self:handlechanges()
tmueller@174
  1639
	else
tmueller@198
  1640
		self.args.profile = nil
tmueller@129
  1641
	end
tmueller@198
  1642
tmueller@129
  1643
	-- Current document
tmueller@198
  1644
tmueller@198
  1645
	self.document = self.requestdocument .. "/" .. self.sectionpath
tmueller@198
  1646
	if self.authuser then
tmueller@198
  1647
		self.getdocname = function(self, path)
tmueller@174
  1648
			return self.requestdocument .. "/" .. (path or self.sectionpath)
tmueller@129
  1649
		end
tmueller@129
  1650
	else
tmueller@198
  1651
		self.getdocname = function(self, path, haveargs)
tmueller@198
  1652
			local dyn, exists
tmueller@198
  1653
			dyn, path, exists = self:isdynamic(path or self.sectionpath)
tmueller@198
  1654
			if dyn or haveargs or not exists then
tmueller@129
  1655
				return self.requestdocument .. "/" .. path
tmueller@129
  1656
			end
tmueller@129
  1657
			path = path == self.config.defname and "index" or path
tmueller@129
  1658
			return "/" .. path:gsub("/", "_") .. ".html"
tmueller@129
  1659
		end
tmueller@129
  1660
	end
tmueller@198
  1661
tmueller@129
  1662
	-- Save session state
tmueller@198
  1663
tmueller@198
  1664
	if self.session then
tmueller@198
  1665
		self.session:save()
tmueller@129
  1666
	end
tmueller@198
  1667
tmueller@198
  1668
	return self
tmueller@198
  1669
end
tmueller@198
  1670
tmueller@198
  1671
tmueller@198
  1672
function Loona:checkpw(login, passwd)
tmueller@198
  1673
	local pwddb = lib.source(self.config.passwdfile)
tmueller@198
  1674
	local pwdentry = pwddb[login]
tmueller@198
  1675
	if pwdentry and pwdentry.password == passwd then
tmueller@198
  1676
		return true, pwdentry.username, pwdentry.permissions or ""
tmueller@198
  1677
	end
tmueller@0
  1678
end
tmueller@0
  1679
tmueller@23
  1680
tmueller@188
  1681
function Loona:run(fname)
tmueller@141
  1682
	self:indexdynamic()
tmueller@129
  1683
	fname = fname or self.requesthandler
tmueller@129
  1684
	local parsed, msg = self:loadhtml(open(fname), "loona:out", fname)
tmueller@129
  1685
	assert(parsed, self:dbmsg("HTML/Lua parsing failed", msg))
tmueller@129
  1686
	self:runboxed(parsed)
tmueller@129
  1687
	return self
tmueller@129
  1688
end
tmueller@80
  1689
tmueller@80
  1690
tmueller@188
  1691
function Loona:indexdynamic()
tmueller@129
  1692
	self:recursesections(self.sections, function(self, s, e, path, dynamic)
tmueller@129
  1693
		path = path and path .. "_" .. e.name or e.name
tmueller@129
  1694
		dynamic = dynamic or { }
tmueller@129
  1695
		for k in pairs(e.dynamic or { }) do
tmueller@129
  1696
			dynamic[k] = true
tmueller@129
  1697
		end
tmueller@129
  1698
		for k in pairs(dynamic) do
tmueller@129
  1699
			local ext = (k == "main" and "") or "." .. k
tmueller@144
  1700
			if posix.stat(self.contentdir .. "/" .. path .. ext,
tmueller@144
  1701
				"mode") == "file" then
tmueller@129
  1702
				dynamic[k] = e.dynamic and e.dynamic[k]
tmueller@129
  1703
			end
tmueller@129
  1704
		end
tmueller@129
  1705
		local n = 0
tmueller@129
  1706
		for k in pairs(dynamic) do
tmueller@129
  1707
			n = n + 1
tmueller@129
  1708
		end
tmueller@129
  1709
		if n > 0 then
tmueller@129
  1710
			e.dynamic = { }
tmueller@129
  1711
			for k in pairs(dynamic) do
tmueller@129
  1712
				e.dynamic[k] = true
tmueller@129
  1713
			end
tmueller@129
  1714
		else
tmueller@129
  1715
			e.dynamic = nil
tmueller@129
  1716
		end
tmueller@129
  1717
		return path, dynamic
tmueller@129
  1718
	end)
tmueller@129
  1719
end
tmueller@80
  1720
tmueller@0
  1721
tmueller@188
  1722
function Loona:isdynamic(path)
tmueller@129
  1723
	path = path or self.sectionpath
tmueller@198
  1724
	local exists
tmueller@129
  1725
	local t, i = self:checkpath(path)
tmueller@198
  1726
	if t then
tmueller@198
  1727
		exists = true
tmueller@198
  1728
		if t[i].redirect then
tmueller@198
  1729
			path = t[i].redirect
tmueller@198
  1730
			t, i, exists = self:isdynamic(path) -- TODO: prohibit endless recursion
tmueller@198
  1731
		end
tmueller@146
  1732
	end
tmueller@198
  1733
	return t and t[i].dynamic, path, exists
tmueller@129
  1734
end
tmueller@0
  1735
tmueller@0
  1736
tmueller@188
  1737
function Loona:dumphtml(o)
tmueller@129
  1738
	local outbuf = { }
tmueller@129
  1739
	o = o or { }
tmueller@129
  1740
	o.nologin = true
tmueller@129
  1741
	o.out = function(self, s) table.insert(outbuf, s) end
tmueller@198
  1742
	o.addheader = function(self, s) end
tmueller@198
  1743
tmueller@188
  1744
	o = self:new(o):run()
tmueller@129
  1745
	if not o:isdynamic() then
tmueller@129
  1746
		local path = o.sectionname
tmueller@129
  1747
		path = path == o.config.defname and "index" or path
tmueller@141
  1748
		local srcname = o.config.htdocsdir .. "/" .. path .. o.htmlext
tmueller@131
  1749
		local fh, msg = open(srcname .. ".tmp", "wb")
tmueller@131
  1750
		assert(fh, self:dbmsg("Could not write cached HTML", msg))
tmueller@198
  1751
		for _, line in ipairs(outbuf) do
tmueller@198
  1752
			fh:write(line)
tmueller@198
  1753
		end
tmueller@129
  1754
		fh:close()
tmueller@144
  1755
		local dstname = o.config.htmlcachedir .. "/" .. path .. o.htmlext
tmueller@144
  1756
		local success, msg = posix.symlink(srcname, dstname .. ".tmp")
tmueller@198
  1757
		-- assert(success, self:dbmsg("Could not link to cached HTML", msg))
tmueller@0
  1758
	end
tmueller@0
  1759
end