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