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