cgi-bin/loona.lua
author Timm S. Mueller <tmueller@neoscientists.org>
Sun, 11 Mar 2007 02:24:24 +0100
changeset 137 45904cd06c29
parent 131 b8973657ca52
child 141 fc50232cf086
permissions -rw-r--r--
Invisible sections are no longer unrolled
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@137
    18
	local io = require "io"
tmueller@131
    19
tmueller@23
    20
tmueller@0
    21
local boxed_G = { 
tmueller@0
    22
	string = string, table = table,
tmueller@0
    23
	assert = assert, collectgarbage = collectgarbage, dofile = dofile,
tmueller@0
    24
	error = error, getfenv = getfenv, getmetatable = getmetatable,
tmueller@0
    25
	ipairs = ipairs, load = load, loadfile = loadfile, loadstring = loadstring,
tmueller@0
    26
	next = next, pairs = pairs, pcall = pcall, print = print,
tmueller@0
    27
	rawequal = rawequal, rawget = rawget, rawset = rawset, require = require,
tmueller@0
    28
	select = select, setfenv = setfenv, setmetatable = setmetatable,
tmueller@0
    29
	tonumber = tonumber, tostring = tostring, type = type, unpack = unpack,
tmueller@0
    30
	xpcall = xpcall
tmueller@0
    31
}
tmueller@0
    32
tmueller@68
    33
local table, string, assert, unpack, ipairs, pairs, type, require =
tmueller@68
    34
	table, string, assert, unpack, ipairs, pairs, type, require
tmueller@0
    35
local setmetatable, setfenv, getfenv = setmetatable, setfenv, getfenv
tmueller@0
    36
local open, remove, rename, getenv, time =
tmueller@0
    37
	io.open, os.remove, os.rename, os.getenv, os.time
tmueller@0
    38
tmueller@0
    39
tmueller@0
    40
module "loona"
tmueller@0
    41
tmueller@0
    42
tmueller@129
    43
_VERSION = 3
tmueller@129
    44
_REVISION = 0
tmueller@0
    45
tmueller@0
    46
tmueller@20
    47
local function lookupname(tab, val)
tmueller@0
    48
	if tab then
tmueller@0
    49
		for i, v in ipairs(tab) do
tmueller@20
    50
			if v.name == val then
tmueller@0
    51
				return i
tmueller@0
    52
			end
tmueller@0
    53
		end
tmueller@0
    54
	end
tmueller@0
    55
end
tmueller@0
    56
tmueller@0
    57
tmueller@129
    58
local atom = { }
tmueller@20
    59
tmueller@129
    60
function atom:new(o)
tmueller@129
    61
	o = o or { }
tmueller@129
    62
	setmetatable(o, self)
tmueller@129
    63
	self.__index = self
tmueller@129
    64
	return o
tmueller@129
    65
end
tmueller@129
    66
tmueller@129
    67
local loona = atom:new(getfenv())
tmueller@129
    68
tmueller@129
    69
tmueller@129
    70
function loona:dbmsg(msg, detail)
tmueller@129
    71
 	return (msg and detail and self.authuser) and
tmueller@129
    72
 		("%s : %s"):format(msg, detail) or msg
tmueller@129
    73
end
tmueller@129
    74
tmueller@129
    75
tmueller@129
    76
function loona:checkprofilename(n)
tmueller@129
    77
	assert(n:match("^%w+$") and n ~= "current",
tmueller@129
    78
		self:dbmsg("Invalid profile name", n))
tmueller@129
    79
	return n
tmueller@129
    80
end
tmueller@129
    81
tmueller@129
    82
tmueller@129
    83
function loona:checkbodyname(s)
tmueller@129
    84
	s = s or "main"
tmueller@129
    85
	assert(s:match("^[%w_]*%w+[%w_]*$"), self:dbmsg("Invalid body name", s))
tmueller@129
    86
	return s
tmueller@129
    87
end
tmueller@129
    88
tmueller@129
    89
tmueller@129
    90
function loona:deleteprofile(p, lang)
tmueller@129
    91
	p = self.config.contentdir .. "/" .. p .. "_" .. (lang or self.lang)
tmueller@129
    92
	for e in tek.util.readdir(p) do
tmueller@129
    93
 		local success, msg = remove(p .. "/" .. e)
tmueller@129
    94
		assert(success, self:dbmsg("Error removing entry in profile", msg))
tmueller@129
    95
	end
tmueller@129
    96
	return remove(p)
tmueller@129
    97
end
tmueller@129
    98
tmueller@129
    99
tmueller@129
   100
function loona:copyprofile(newprofile, srcprofile, lang)
tmueller@129
   101
	srcprofile = srcprofile or self.profile
tmueller@129
   102
	lang = lang or self.lang
tmueller@129
   103
	local contentdir = self.config.contentdir
tmueller@129
   104
	local src = ("%s/%s_%s"):format(contentdir, srcprofile or self.profile, lang)
tmueller@129
   105
	assert(posix.stat(src, "mode") == "directory",
tmueller@129
   106
		self:dbmsg("Not a directory", src))
tmueller@129
   107
	local dst = ("%s/%s_%s"):format(contentdir, newprofile, lang)
tmueller@129
   108
	local success, msg = posix.mkdir(dst)
tmueller@129
   109
	assert(success, self:dbmsg("Error creating profile directory", msg))
tmueller@129
   110
	for e in tek.util.readdir(src) do
tmueller@129
   111
		local ext = e:match("^[^.].*%.([^.]*)$")
tmueller@129
   112
		if ext ~= "LOCK" then
tmueller@129
   113
			local f = src .. "/" .. e
tmueller@129
   114
			if posix.stat(f, "mode") == "file" then
tmueller@129
   115
				success, msg = tek.copyfile(f, dst .. "/" .. e)
tmueller@129
   116
				assert(success, self:dbmsg("Error copying file", msg))
tmueller@129
   117
			end
tmueller@129
   118
		end
tmueller@129
   119
	end
tmueller@129
   120
end
tmueller@129
   121
tmueller@129
   122
tmueller@129
   123
function loona:publishprofile(profile, lang)
tmueller@131
   124
	
tmueller@129
   125
	lang = lang or self.lang
tmueller@129
   126
	local contentdir = self.config.contentdir
tmueller@129
   127
	local newpath = ("%s/current_%s"):format(contentdir, lang)
tmueller@129
   128
	local tmppath = newpath .. "." .. self.session.name
tmueller@129
   129
	local success, msg = posix.symlink(profile .. "_" .. lang, tmppath)
tmueller@129
   130
	assert(success, self:dbmsg("Cannot create symlink", msg))
tmueller@129
   131
	success, msg = rename(tmppath, newpath)
tmueller@129
   132
	assert(success, self:dbmsg("Cannot put symlink in place", msg))
tmueller@129
   133
	
tmueller@129
   134
	-- Unroll site to static HTML
tmueller@129
   135
	
tmueller@129
   136
	self:recursesections(self.sections, function(self, s, e, path)
tmueller@129
   137
		path = path and path .. "/" .. e.name or e.name
tmueller@137
   138
		if not e.notvisible then
tmueller@137
   139
			io.stderr:write("render...  " .. path .. "\n")
tmueller@137
   140
	 		loona:dumphtml { requestpath = path }
tmueller@137
   141
	 	end
tmueller@129
   142
		return path
tmueller@129
   143
	end)
tmueller@131
   144
	
tmueller@131
   145
	-- Update file cache
tmueller@131
   146
tmueller@131
   147
	local htdocs = self.config.htdocsdir
tmueller@131
   148
	local cache = self.config.htmlcachedir
tmueller@131
   149
tmueller@131
   150
	for e in tek.util.readdir(cache) do
tmueller@131
   151
		if e:match("^.*%.html$") then
tmueller@131
   152
			local success, msg = remove(htdocs .. "/" .. e)
tmueller@131
   153
			success, msg = remove(cache .. "/" .. e)
tmueller@131
   154
 			assert(success, self:dbmsg("Could not purge cached HTML file", msg))
tmueller@131
   155
		end
tmueller@131
   156
	end
tmueller@131
   157
tmueller@131
   158
	for e in tek.util.readdir(cache) do
tmueller@131
   159
		local f = e:match("^(.*%.html)%.tmp$")
tmueller@131
   160
		if f then
tmueller@131
   161
			local success, msg = rename(cache .. "/" .. e, cache .. "/" .. f)
tmueller@131
   162
			assert(success, self:dbmsg("Could not update cached HTML file", msg))
tmueller@131
   163
			success, msg = rename(htdocs .. "/" .. e, htdocs .. "/" .. f)
tmueller@131
   164
			assert(success, self:dbmsg("Could not update cached HTML file", msg))
tmueller@131
   165
		end
tmueller@131
   166
	end
tmueller@131
   167
tmueller@129
   168
end
tmueller@129
   169
tmueller@129
   170
tmueller@129
   171
function loona:recursesections(s, func, ...)
tmueller@0
   172
	for _, e in ipairs(s) do
tmueller@129
   173
		local udata = { func(self, s, e, unpack(arg)) }
tmueller@0
   174
		if e.subs then
tmueller@129
   175
			self:recursesections(e.subs, func, unpack(udata))
tmueller@0
   176
		end
tmueller@129
   177
	end
tmueller@129
   178
end
tmueller@129
   179
tmueller@129
   180
tmueller@129
   181
function loona:indexsections()
tmueller@129
   182
	self:recursesections(self.sections, function(self, s, e)
tmueller@129
   183
		e.notvalid = (not self.secure and e.secure) or 
tmueller@129
   184
			(not self.authuser and e.secret) or nil
tmueller@129
   185
		e.notvisible = e.notvalid or not self.authuser and e.hidden or nil
tmueller@16
   186
		s[e.name] = e
tmueller@129
   187
	end)
tmueller@0
   188
end
tmueller@0
   189
tmueller@0
   190
tmueller@20
   191
--	Decompose section path into a stack of sections, returning only up to
tmueller@0
   192
--	the last valid element in the path. additionally returns the table of
tmueller@0
   193
--	the last section path element (or the default section)
tmueller@0
   194
tmueller@129
   195
function loona:getsection(path)
tmueller@129
   196
	local default = not self.authuser and self.config.defname
tmueller@129
   197
	local tab = { { entries = self.sections, name = default } }
tmueller@129
   198
	local ss = self.sections
tmueller@0
   199
	local sectionpath
tmueller@129
   200
	(path or ""):gsub("(%w+)/?", function(a)
tmueller@27
   201
		if ss then
tmueller@27
   202
			local s = ss[a]
tmueller@20
   203
			if s and not s.notvalid then
tmueller@20
   204
				sectionpath = s
tmueller@0
   205
				tab[#tab].name = a
tmueller@27
   206
				ss = s.subs
tmueller@27
   207
				if ss then
tmueller@27
   208
					table.insert(tab, { entries = ss })
tmueller@0
   209
				end
tmueller@0
   210
			else
tmueller@27
   211
				ss = nil -- stop.
tmueller@0
   212
			end
tmueller@0
   213
		end
tmueller@0
   214
	end)
tmueller@129
   215
	if not self.section and not sectionpath then
tmueller@129
   216
		sectionpath = self.sections[default]
tmueller@0
   217
		if sectionpath then
tmueller@0
   218
			table.insert(tab, { entries = sectionpath.subs })
tmueller@0
   219
		end
tmueller@0
   220
	end
tmueller@0
   221
	return tab, sectionpath
tmueller@0
   222
end
tmueller@0
   223
tmueller@0
   224
tmueller@129
   225
function loona:getpath(delimiter)
tmueller@0
   226
	local t = { }
tmueller@129
   227
	for _, menu in ipairs(self.submenus) do
tmueller@0
   228
		if menu.name then
tmueller@0
   229
			table.insert(t, menu.name)
tmueller@0
   230
		end
tmueller@0
   231
	end
tmueller@0
   232
	return table.concat(t, delimiter or "/")
tmueller@0
   233
end
tmueller@0
   234
tmueller@0
   235
tmueller@129
   236
function loona:deletesection(fname, all_bodies)
tmueller@129
   237
	local fullname = self.contentdir .. "/" .. fname
tmueller@0
   238
	local success, msg = remove(fullname)
tmueller@129
   239
	if all_bodies then
tmueller@0
   240
		local pat = "^" .. 
tmueller@0
   241
			fname:gsub("%^%$%(%)%%%.%[%]%*%+%-%?", "%%%1") .. "%..*$"
tmueller@129
   242
		for e in tek.util.readdir(self.contentdir) do
tmueller@0
   243
			if e:match(pat) then
tmueller@129
   244
				remove(self.contentdir .. "/" .. e)
tmueller@0
   245
			end
tmueller@0
   246
		end
tmueller@0
   247
	end
tmueller@0
   248
	return success, msg
tmueller@0
   249
end
tmueller@0
   250
tmueller@0
   251
tmueller@129
   252
function loona:addpath(path, e)
tmueller@129
   253
	local tab = self.sections
tmueller@0
   254
	path:gsub("(%w+)/?", function(a)
tmueller@20
   255
		if tab then
tmueller@20
   256
			local s = tab[a]
tmueller@20
   257
			if s then
tmueller@20
   258
				if not s.subs then
tmueller@20
   259
					s.subs = { }
tmueller@0
   260
				end
tmueller@20
   261
				tab = s.subs
tmueller@0
   262
			else
tmueller@20
   263
				table.insert(tab, e)
tmueller@20
   264
				tab[a] = e
tmueller@20
   265
 				tab = nil -- stop
tmueller@0
   266
			end
tmueller@0
   267
		end
tmueller@0
   268
	end)
tmueller@129
   269
	return e
tmueller@0
   270
end
tmueller@0
   271
tmueller@0
   272
tmueller@129
   273
function loona:rmpath(path)
tmueller@20
   274
	local parent
tmueller@129
   275
	local tab = self.sections
tmueller@0
   276
	path:gsub("(%w+)/?", function(a)
tmueller@20
   277
		if tab then
tmueller@20
   278
			local idx = lookupname(tab, a)
tmueller@20
   279
			if idx then
tmueller@20
   280
				if tab[idx].subs then
tmueller@20
   281
					parent = tab[idx]
tmueller@20
   282
					tab = tab[idx].subs
tmueller@0
   283
				else
tmueller@20
   284
					table.remove(tab, idx)
tmueller@43
   285
					tab[a] = nil
tmueller@20
   286
					if #tab == 0 and parent then
tmueller@20
   287
						parent.subs = nil
tmueller@0
   288
					end
tmueller@20
   289
					tab = nil
tmueller@0
   290
				end
tmueller@0
   291
			end
tmueller@0
   292
		end
tmueller@0
   293
	end)
tmueller@0
   294
end
tmueller@0
   295
tmueller@0
   296
tmueller@129
   297
function loona:checkpath(path)
tmueller@129
   298
	if path ~= "index" then -- "index" is reserved
tmueller@129
   299
		local res, idx
tmueller@129
   300
		local tab = self.sections
tmueller@129
   301
		path:gsub("(%w+)/?", function(a)
tmueller@129
   302
			if tab then
tmueller@129
   303
				local i = lookupname(tab, a)
tmueller@129
   304
				if i then
tmueller@129
   305
					res, idx = tab, i
tmueller@129
   306
					tab = tab[i].subs
tmueller@129
   307
				else
tmueller@129
   308
					res, idx = nil, nil
tmueller@129
   309
				end
tmueller@129
   310
			end
tmueller@129
   311
		end)
tmueller@129
   312
		return res, idx
tmueller@129
   313
	end
tmueller@8
   314
end
tmueller@8
   315
tmueller@8
   316
tmueller@129
   317
function loona:title()
tmueller@129
   318
	return self.section and (self.section.title or self.section.label or
tmueller@129
   319
		self.section.name) or ""
tmueller@0
   320
end
tmueller@0
   321
tmueller@0
   322
tmueller@0
   323
--	Run a site function snippet, with full error recovery
tmueller@0
   324
--	(also recovers from errors in error handling function)
tmueller@0
   325
tmueller@129
   326
function loona:dosnippet(func, errfunc)
tmueller@0
   327
	local ret = { tek.catch(func) }
tmueller@0
   328
	if ret[1] == 0 or (errfunc and tek.catch(errfunc) == 0) then
tmueller@0
   329
		return unpack(ret)
tmueller@0
   330
	end
tmueller@129
   331
	self:out("<h2>Error</h2>")
tmueller@129
   332
	self:out("<h3>" .. self:encodeform(ret[2] or "") .. "</h3>")
tmueller@129
   333
	if self.authuser then
tmueller@0
   334
		if type(ret[3]) == "string" then
tmueller@129
   335
			self:out("<p>" .. self:encodeform(ret[3]) .. "</p>")
tmueller@0
   336
		end
tmueller@129
   337
		if ret[4] then
tmueller@129
   338
			self:out("<pre>" .. self:encodeform(ret[4]) .. "</pre>")
tmueller@0
   339
		end
tmueller@0
   340
	end
tmueller@0
   341
end	
tmueller@0
   342
tmueller@0
   343
tmueller@129
   344
function loona:lockfile(file)
tmueller@129
   345
	return not self.session and true or 
tmueller@129
   346
		posix.symlink(self.session.filename, file .. ".LOCK")
tmueller@0
   347
end
tmueller@0
   348
tmueller@0
   349
tmueller@129
   350
function loona:unlockfile(file)
tmueller@129
   351
	return not self.session and true or remove(file .. ".LOCK")
tmueller@0
   352
end
tmueller@0
   353
tmueller@0
   354
tmueller@129
   355
function loona:saveindex()
tmueller@129
   356
	local tempname = self.indexfname .. "." .. self.session.name
tmueller@124
   357
	local f, msg = open(tempname, "wb")
tmueller@129
   358
	assert(f, self:dbmsg("Error opening section file for writing", msg))
tmueller@129
   359
	tek.dump(self.sections, function(...)
tmueller@124
   360
		f:write(unpack(arg))
tmueller@124
   361
	end)
tmueller@124
   362
	f:close()
tmueller@129
   363
	local success, msg = rename(tempname, self.indexfname)
tmueller@129
   364
	assert(success, self:dbmsg("Error renaming section file", msg))
tmueller@124
   365
end
tmueller@124
   366
tmueller@124
   367
tmueller@129
   368
function loona:savebody(fname, content)
tmueller@129
   369
	fname = self.contentdir .. "/" .. fname
tmueller@0
   370
	local f, msg = open(fname, "wb")
tmueller@129
   371
	assert(f, self:dbmsg("Could not open file for writing", msg))
tmueller@0
   372
	f:write(content or "")
tmueller@0
   373
	f:close()
tmueller@0
   374
end
tmueller@0
   375
tmueller@23
   376
tmueller@129
   377
function loona:runboxed(func, envitems, ...)
tmueller@129
   378
	local fenv = {
tmueller@129
   379
 		arg = arg,
tmueller@129
   380
 		loona = self
tmueller@129
   381
 	}
tmueller@129
   382
 	if envitems then
tmueller@129
   383
	 	for k, v in pairs(envitems) do
tmueller@129
   384
 			fenv[k] = v
tmueller@129
   385
 		end
tmueller@129
   386
 	end
tmueller@129
   387
 	setmetatable(fenv, { __index = boxed_G }) -- boxed global environment
tmueller@129
   388
	setfenv(func, fenv)
tmueller@129
   389
	return func()
tmueller@129
   390
end
tmueller@0
   391
tmueller@129
   392
tmueller@129
   393
function loona:include(fname, ...)
tmueller@129
   394
	assert(not fname:match("%W"), self:dbmsg("Invalid include name", fname))
tmueller@129
   395
	local fname2 = ("%s/%s.lua"):format(self.config.extdir, fname)
tmueller@0
   396
	local f, msg = open(fname2)
tmueller@129
   397
	assert(f, self:dbmsg("Cannot open file", msg))
tmueller@129
   398
	local parsed, msg = self:loadhtml(f, "loona:out", fname2)
tmueller@129
   399
	assert(parsed, self:dbmsg("Syntax error", msg))
tmueller@129
   400
	return self:runboxed(parsed)
tmueller@0
   401
end
tmueller@0
   402
tmueller@0
   403
tmueller@68
   404
--	produce link target, propagate lang, profile, session
tmueller@68
   405
tmueller@129
   406
function loona:href(section, ...)
tmueller@129
   407
	local target = self:getdocname(section)
tmueller@129
   408
-- 	if self.session or self.profile ~= self.pubprofile then
tmueller@129
   409
	if self.session then
tmueller@68
   410
		return tek.web.gethref(target, { "profile", "session", "lang", 
tmueller@68
   411
			unpack(arg) })
tmueller@0
   412
	end
tmueller@68
   413
	return tek.web.gethref(target, { "lang", unpack(arg) })
tmueller@0
   414
end
tmueller@0
   415
tmueller@129
   416
function loona:ilink(target, text, extra)
tmueller@68
   417
	return ('<a href="%s"%s>%s</a>'):format(target, extra or "", text)
tmueller@0
   418
end
tmueller@0
   419
tmueller@68
   420
--	internal link, propagation of lang, profile, session
tmueller@0
   421
tmueller@129
   422
function loona:link(section, text, ...)
tmueller@129
   423
	return self:ilink(self:href(section, unpack(arg)), text or section)
tmueller@0
   424
end
tmueller@0
   425
tmueller@68
   426
--	external link (opens in a new window), no argument propagation
tmueller@0
   427
tmueller@129
   428
function loona:elink(target, text)
tmueller@129
   429
	return self:ilink(target, text or target, not self.config.extlinksamewindow and
tmueller@83
   430
		' class="extlink" onclick="void(window.open(this.href, \'\', \'\')); return false;"')
tmueller@0
   431
end
tmueller@0
   432
tmueller@68
   433
--	plain link, no argument propagation
tmueller@68
   434
tmueller@129
   435
function loona:plink(section, text, ...)
tmueller@129
   436
	return self:ilink(tek.web.gethref(section, arg), text or section)
tmueller@68
   437
end
tmueller@68
   438
tmueller@68
   439
--	user interface link, propagation of lang, profile, session
tmueller@68
   440
tmueller@129
   441
function loona:uilink(section, text, ...)
tmueller@129
   442
	return self:ilink(self:href(section, unpack(arg)), text or section)
tmueller@68
   443
end
tmueller@68
   444
tmueller@68
   445
--	produce a hidden input value in forms
tmueller@0
   446
tmueller@129
   447
function loona:hidden(name, value)
tmueller@68
   448
	return not value and "" or
tmueller@68
   449
		('<input type="hidden" name="%s" value="%s" />'):format(name, value)
tmueller@0
   450
end
tmueller@0
   451
tmueller@0
   452
tmueller@129
   453
function loona:getprofiles(lang)
tmueller@129
   454
	lang = lang or self.lang
tmueller@129
   455
	local dir = self.config.contentdir
tmueller@0
   456
	local t = { }
tmueller@129
   457
	for f in tek.util.readdir(dir) do
tmueller@129
   458
		if posix.lstat(dir .. "/" .. f, "mode") == "directory" then
tmueller@0
   459
			local e = f:match("^(%w+)_" .. lang .. "$")
tmueller@0
   460
 			if e then
tmueller@0
   461
	 			t[e] = e
tmueller@0
   462
 	 		end
tmueller@0
   463
		end
tmueller@0
   464
	end
tmueller@0
   465
	return t
tmueller@0
   466
end
tmueller@0
   467
tmueller@0
   468
tmueller@80
   469
--	Functions to produce a hierarchical navigation menu
tmueller@80
   470
tmueller@129
   471
local newent = { name = "new", label = "[+]", action="actionnew=true" }
tmueller@124
   472
tmueller@129
   473
function loona:rmenu(level, linkf, path)
tmueller@129
   474
	local sub = (self.authuser and level == #self.submenus + 1) and
tmueller@129
   475
		{ name = "new", entries = { }} or self.submenus[level]
tmueller@124
   476
 	if sub and sub.entries then
tmueller@80
   477
		local visible = { }
tmueller@80
   478
		for _, e in ipairs(sub.entries) do
tmueller@80
   479
			if not e.notvisible then
tmueller@80
   480
				table.insert(visible, e)
tmueller@80
   481
			end
tmueller@80
   482
		end
tmueller@129
   483
		if self.authuser then
tmueller@124
   484
			table.insert(visible, newent)
tmueller@124
   485
		end
tmueller@80
   486
		if #visible > 0 then
tmueller@129
   487
			self:out('<ul id="menulevel' .. level .. '">\n')
tmueller@80
   488
			for _, e in ipairs(visible) do
tmueller@129
   489
				local label = self:encodeform(e.label or e.name)
tmueller@80
   490
				local newpath = path and path .. "/" .. e.name or e.name
tmueller@80
   491
				local active = (e.name == sub.name)
tmueller@129
   492
				self:out('<li>\n')
tmueller@129
   493
				linkf(self, newpath, label, active, e.action)
tmueller@80
   494
				if active then
tmueller@129
   495
					self:rmenu(level + 1, linkf, newpath)
tmueller@80
   496
				end
tmueller@129
   497
				self:out('</li>\n')
tmueller@80
   498
			end
tmueller@129
   499
			self:out('</ul>\n')
tmueller@80
   500
		end
tmueller@80
   501
	end
tmueller@80
   502
end
tmueller@80
   503
tmueller@129
   504
function loona:menulink(path, label, active, ...)
tmueller@129
   505
	self:out(('<a %shref="%s">%s</a>\n'):format(active and 'class="active" ' or "",
tmueller@129
   506
		self:href(path, unpack(arg)), label))
tmueller@80
   507
end
tmueller@80
   508
tmueller@129
   509
function loona:menu(level, linkf)
tmueller@129
   510
	self:rmenu(level or 1, linkf or menulink)
tmueller@80
   511
end
tmueller@80
   512
tmueller@80
   513
tmueller@129
   514
function loona:init()
tmueller@0
   515
	
tmueller@0
   516
	-- get list of languages, in order of preference
tmueller@0
   517
	
tmueller@129
   518
	self.langs = { self.args.lang and self.args.lang:match("^%w+$") }
tmueller@129
   519
	if self.config.browserlang then
tmueller@0
   520
		local s = getenv("HTTP_ACCEPT_LANGUAGE")
tmueller@0
   521
		while s do
tmueller@0
   522
			local l, r = s:match("^([%w.=]+)[,;](.*)$")
tmueller@0
   523
			l = l or s
tmueller@0
   524
			s = r
tmueller@0
   525
			if l:match("^%w+$") then
tmueller@129
   526
				table.insert(self.langs, l)
tmueller@0
   527
			end
tmueller@0
   528
		end
tmueller@0
   529
	end
tmueller@129
   530
	table.insert(self.langs, self.config.deflang)
tmueller@0
   531
	
tmueller@0
   532
	-- get list of possible profiles
tmueller@0
   533
	
tmueller@0
   534
	local profiles = { }
tmueller@129
   535
	for e in tek.util.readdir(self.config.contentdir) do
tmueller@0
   536
		profiles[e] = e
tmueller@0
   537
	end
tmueller@0
   538
	
tmueller@0
   539
	-- get pubprofile
tmueller@0
   540
	
tmueller@129
   541
	for _, lang in ipairs(self.langs) do
tmueller@129
   542
		local p = posix.readlink(self.config.contentdir .. "/current_" .. lang)
tmueller@0
   543
		p = p and p:match("^(%w+)_" .. lang .. "$")
tmueller@0
   544
		if p then
tmueller@129
   545
			self.pubprofile = p
tmueller@0
   546
			break
tmueller@0
   547
		end
tmueller@0
   548
	end
tmueller@0
   549
	
tmueller@0
   550
	-- get profile
tmueller@0
   551
	
tmueller@129
   552
	local checkprofile = self.authuser and self.args.profile or self.pubprofile or "default"
tmueller@129
   553
	for _, lang in ipairs(self.langs) do
tmueller@129
   554
		if profiles[checkprofile .. "_" .. lang] then
tmueller@129
   555
			self.profile = checkprofile
tmueller@129
   556
			self.lang = lang
tmueller@0
   557
			break
tmueller@0
   558
		end
tmueller@0
   559
	end
tmueller@0
   560
	
tmueller@129
   561
	assert(self.profile and self.lang, "Invalid profile or language")
tmueller@0
   562
	
tmueller@47
   563
	
tmueller@129
   564
	-- write back language and profile
tmueller@0
   565
tmueller@129
   566
	self.args.lang = self.lang ~= self.config.deflang and self.lang or nil
tmueller@129
   567
	self.args.profile = self.profile
tmueller@0
   568
	
tmueller@47
   569
	
tmueller@0
   570
	-- determine content directory pathname and section filename
tmueller@0
   571
	
tmueller@129
   572
	self.contentdir = ("%s/%s_%s"):format(self.config.contentdir, self.profile, self.lang)
tmueller@129
   573
 	self.indexfname = self.contentdir .. "/.sections"
tmueller@0
   574
	
tmueller@0
   575
	-- load sections
tmueller@0
   576
	
tmueller@129
   577
 	self.sections = tek.source(self.indexfname)
tmueller@0
   578
	
tmueller@20
   579
	-- index sections, determine visibility in menu
tmueller@0
   580
	
tmueller@129
   581
	self:indexsections()
tmueller@0
   582
	
tmueller@129
   583
	-- decompose request path, produce a stack of sections
tmueller@0
   584
	
tmueller@129
   585
	self.submenus, self.section = self:getsection(self.requestpath)
tmueller@0
   586
tmueller@0
   587
	-- handle redirects if not logged on
tmueller@0
   588
	
tmueller@129
   589
	if not self.authuser and self.section and self.section.redirect then
tmueller@129
   590
		self.submenus, self.section = self:getsection(self.section.redirect)
tmueller@0
   591
	end
tmueller@0
   592
			
tmueller@0
   593
	-- section path and document name (refined)
tmueller@0
   594
	
tmueller@129
   595
	self.sectionpath = self:getpath()
tmueller@129
   596
	self.sectionname = self:getpath("_")
tmueller@0
   597
tmueller@0
   598
end
tmueller@0
   599
tmueller@0
   600
tmueller@129
   601
function loona:handlechanges()
tmueller@129
   602
	
tmueller@129
   603
	local save
tmueller@0
   604
tmueller@129
   605
	if self.args.editkey == "main" then
tmueller@23
   606
		
tmueller@23
   607
		-- In main editable section:
tmueller@23
   608
		
tmueller@129
   609
		if self.args.actioncreate then
tmueller@129
   610
			
tmueller@45
   611
			-- Create new section
tmueller@129
   612
			
tmueller@129
   613
			local editname = self.args.editname:lower()
tmueller@20
   614
			assert(not editname:match("%W"),
tmueller@47
   615
				dbmsg("Invalid section name", editname))
tmueller@20
   616
			if not (section and (section.subs or section)[editname]) then
tmueller@20
   617
				local newpath = 
tmueller@129
   618
					(self.sectionpath and (self.sectionpath .. "/")) .. editname
tmueller@129
   619
				local s = self:addpath(newpath, { name = editname,
tmueller@129
   620
					label = self.args.editlabel ~= "" and self.args.editlabel or nil,
tmueller@129
   621
					title = self.args.edittitle ~= "" and self.args.edittitle or nil,
tmueller@129
   622
					redirect = self.args.editredirect ~= "" and self.args.editredirect or nil,
tmueller@129
   623
					hidden = self.args.editvisibility and true,
tmueller@129
   624
					secret = self.args.editsecrecy and true,
tmueller@129
   625
					secure = self.args.editsecure and true,
tmueller@129
   626
					creator = self.authuser,
tmueller@20
   627
					creationdate = time() })
tmueller@23
   628
				save = true
tmueller@0
   629
			end
tmueller@20
   630
		
tmueller@129
   631
		elseif self.args.actionsave then
tmueller@129
   632
			
tmueller@45
   633
			-- Save section
tmueller@129
   634
			
tmueller@129
   635
			self.section.revisiondate = time()
tmueller@129
   636
			self.section.revisioner = self.authuser
tmueller@129
   637
			save = true
tmueller@129
   638
 		
tmueller@129
   639
		elseif self.args.actionsaveprops then
tmueller@129
   640
			
tmueller@129
   641
			-- Save properties
tmueller@129
   642
			
tmueller@129
   643
			self.section.hidden = self.args.editvisibility and true
tmueller@129
   644
			self.section.secret = self.args.editsecrecy and true
tmueller@129
   645
			self.section.secure = self.args.editsecure and true
tmueller@129
   646
			self.section.label = self.args.editlabel ~= "" and self.args.editlabel or nil
tmueller@129
   647
			self.section.title = self.args.edittitle ~= "" and self.args.edittitle or nil
tmueller@129
   648
			self.section.redirect =
tmueller@129
   649
				self.args.editredirect ~= "" and self.args.editredirect or nil
tmueller@23
   650
			save = true
tmueller@20
   651
		
tmueller@129
   652
		elseif self.args.actionup then
tmueller@129
   653
			
tmueller@45
   654
			-- Move section up
tmueller@129
   655
			
tmueller@129
   656
			local t, i = self:checkpath(self.sectionpath)
tmueller@0
   657
			if t and i > 1 then
tmueller@129
   658
				if self.profile == self.pubprofile and not self.args.actionconfirm then
tmueller@115
   659
					useralert = {
tmueller@129
   660
						text = self.locale.ALERT_MOVE_SECTION_IN_PUBLISHED_PROFILE,
tmueller@115
   661
						confirm =
tmueller@115
   662
							'<input type="submit" name="actionup" value="' .. 
tmueller@129
   663
							self.locale.MOVE .. '" /> ' ..
tmueller@129
   664
							self:hidden("actionconfirm", "true")
tmueller@115
   665
					}
tmueller@115
   666
				else
tmueller@115
   667
					local item = table.remove(t, i)
tmueller@115
   668
					table.insert(t, i - 1, item)
tmueller@115
   669
					save = true
tmueller@115
   670
				end
tmueller@0
   671
			end
tmueller@20
   672
		
tmueller@129
   673
		elseif self.args.actiondown then
tmueller@129
   674
			
tmueller@45
   675
			-- Move section down
tmueller@129
   676
			
tmueller@129
   677
			local t, i = self:checkpath(self.sectionpath)
tmueller@0
   678
			if t and i < #t then
tmueller@129
   679
				if self.profile == self.pubprofile and not self.args.actionconfirm then
tmueller@115
   680
					useralert = {
tmueller@129
   681
						text = self.locale.ALERT_MOVE_SECTION_IN_PUBLISHED_PROFILE,
tmueller@115
   682
						confirm =
tmueller@115
   683
							'<input type="submit" name="actiondown" value="' .. 
tmueller@129
   684
							self.locale.MOVE .. '" /> ' ..
tmueller@129
   685
							self:hidden("actionconfirm", "true")
tmueller@115
   686
					}
tmueller@115
   687
				else
tmueller@115
   688
					local item = table.remove(t, i)
tmueller@115
   689
					table.insert(t, i + 1, item)
tmueller@115
   690
					save = true
tmueller@115
   691
				end
tmueller@0
   692
			end
tmueller@20
   693
		
tmueller@129
   694
		elseif self.args.actioncreateprofile and self.args.createprofile then
tmueller@129
   695
			
tmueller@23
   696
			-- Create profile
tmueller@129
   697
			
tmueller@129
   698
			local c = self:checkprofilename(self.args.createprofile:lower())
tmueller@0
   699
			if c == profile then
tmueller@129
   700
				useralert = { text = self.locale.ALERT_CANNOT_COPY_PROFILE_TO_SELF }
tmueller@0
   701
			else
tmueller@129
   702
				local profiles = self:getprofiles()
tmueller@129
   703
				if profiles[c] and not self.args.actionconfirm then
tmueller@20
   704
					useralert = {
tmueller@129
   705
						text = c == self.pubprofile and 
tmueller@129
   706
							self.locale.ALERT_OVERWRITE_PUBLISHED_PROFILE or
tmueller@129
   707
							self.locale.ALERT_OVERWRITE_EXISTING_PROFILE,
tmueller@0
   708
						confirm =
tmueller@20
   709
							'<input type="submit" name="actioncreateprofile" value="' .. 
tmueller@129
   710
							self.locale.OVERWRITE .. '" /> ' ..
tmueller@129
   711
							self:hidden("actionconfirm", "true") .. 
tmueller@129
   712
							self:hidden("createprofile", c)
tmueller@20
   713
					}
tmueller@0
   714
				else
tmueller@0
   715
					if profiles[c] then
tmueller@129
   716
						self:deleteprofile(c)
tmueller@0
   717
					end
tmueller@129
   718
					self:copyprofile(c)
tmueller@0
   719
				end
tmueller@0
   720
			end
tmueller@20
   721
		
tmueller@129
   722
		elseif self.args.actiondeleteprofile and self.args.deleteprofile then
tmueller@129
   723
			
tmueller@23
   724
			-- Delete profile
tmueller@129
   725
			
tmueller@129
   726
			local c = self:checkprofilename(self.args.deleteprofile:lower())
tmueller@129
   727
			assert(c ~= self.pubprofile, self:dbmsg("Cannot delete published profile", c))
tmueller@129
   728
			if self.args.actionconfirm then
tmueller@129
   729
				self:deleteprofile(c)
tmueller@129
   730
				self.profile = nil
tmueller@129
   731
				self.args.profile = nil
tmueller@129
   732
				self:init()
tmueller@23
   733
				save = true
tmueller@0
   734
			else
tmueller@20
   735
				useralert = { 
tmueller@129
   736
					text = self.locale.ALERT_DELETE_PROFILE,
tmueller@20
   737
					confirm = 
tmueller@20
   738
						'<input type="submit" name="actiondeleteprofile" value="' .. 
tmueller@129
   739
						self.locale.DELETE .. '" /> ' ..
tmueller@129
   740
						self:hidden("actionconfirm", "true") ..
tmueller@129
   741
						self:hidden("deleteprofile", c)
tmueller@20
   742
				}
tmueller@0
   743
			end
tmueller@20
   744
		
tmueller@129
   745
		elseif self.args.actionchangeprofile and self.args.changeprofile then
tmueller@129
   746
			
tmueller@23
   747
			-- Change profile
tmueller@129
   748
			
tmueller@129
   749
			local c = self:checkprofilename(self.args.changeprofile:lower())
tmueller@129
   750
			self.profile = c
tmueller@129
   751
			self.args.profile = c
tmueller@23
   752
			save = true
tmueller@20
   753
		
tmueller@129
   754
		elseif self.args.actionpublishprofile and self.args.publishprofile then
tmueller@129
   755
			
tmueller@23
   756
			-- Publish profile
tmueller@129
   757
			
tmueller@129
   758
			local c = self:checkprofilename(self.args.publishprofile:lower())
tmueller@129
   759
			if c ~= self.publicprofile then
tmueller@129
   760
				if self.args.actionconfirm then
tmueller@129
   761
					self:publishprofile(c)
tmueller@23
   762
					save = true
tmueller@0
   763
				else
tmueller@20
   764
					useralert = {
tmueller@129
   765
						text = self.locale.ALERT_PUBLISH_PROFILE,
tmueller@20
   766
						confirm =
tmueller@20
   767
							'<input type="submit" name="actionpublishprofile" value="' ..
tmueller@129
   768
							self.locale.PUBLISH .. '" /> ' ..
tmueller@129
   769
							self:hidden("actionconfirm", "true") ..
tmueller@129
   770
							self:hidden("publishprofile", c)
tmueller@20
   771
					}
tmueller@20
   772
				end
tmueller@20
   773
			end
tmueller@0
   774
		end
tmueller@0
   775
		
tmueller@129
   776
	end
tmueller@0
   777
	
tmueller@129
   778
	if self.args.actiondelete then
tmueller@129
   779
		
tmueller@129
   780
		-- Delete section
tmueller@129
   781
		
tmueller@129
   782
		if not self.args.actionconfirm then
tmueller@129
   783
			useralert = {
tmueller@129
   784
				text = self.profile == self.pubprofile and
tmueller@129
   785
					self.locale.ALERT_DELETE_SECTION_IN_PUBLISHED_PROFILE or
tmueller@129
   786
					self.locale.ALERT_DELETE_SECTION,
tmueller@129
   787
				confirm =
tmueller@129
   788
					'<input type="submit" name="actiondelete" value="' .. 
tmueller@129
   789
					self.locale.DELETE .. '" /> ' ..
tmueller@129
   790
					self:hidden("actionconfirm", "true")
tmueller@129
   791
			}
tmueller@129
   792
		else
tmueller@129
   793
			local key = self.args.editkey
tmueller@129
   794
			if key == "main" and not self.section.subs then
tmueller@129
   795
				self:deletesection(self.sectionname, true) -- all bodies
tmueller@129
   796
				self:rmpath(self.sectionpath) -- and node
tmueller@129
   797
			else
tmueller@129
   798
				local ext = (key == "main" and "") or "." .. key
tmueller@129
   799
				self:deletesection(self.sectionname .. ext) -- only text
tmueller@129
   800
				if self.section.dynamic then
tmueller@129
   801
					self.section.dynamic[key] = nil
tmueller@131
   802
					local n = 0
tmueller@131
   803
					for _ in pairs(self.section.dynamic) do
tmueller@131
   804
						n = n + 1
tmueller@131
   805
					end
tmueller@131
   806
					if n == 0 then
tmueller@131
   807
						self.section.dynamic = nil
tmueller@131
   808
					end
tmueller@129
   809
				end
tmueller@129
   810
			end
tmueller@129
   811
			save = true
tmueller@0
   812
		end
tmueller@0
   813
	end
tmueller@129
   814
		
tmueller@129
   815
	if save then
tmueller@129
   816
		self:saveindex()
tmueller@129
   817
		self:init()
tmueller@129
   818
	end
tmueller@129
   819
	
tmueller@0
   820
end
tmueller@0
   821
tmueller@0
   822
tmueller@129
   823
function loona:encodeform(s)
tmueller@129
   824
	return cgi.encodeform(s)
tmueller@129
   825
end
tmueller@0
   826
tmueller@80
   827
tmueller@129
   828
function loona:loadhtml(src, outfunc, chunkname)
tmueller@129
   829
	return tek.web.include.load(src, outfunc, chunkname)
tmueller@129
   830
end
tmueller@80
   831
tmueller@80
   832
tmueller@129
   833
function loona:domarkup(s)
tmueller@129
   834
	return tek.web.markup.main(s)
tmueller@129
   835
end
tmueller@0
   836
tmueller@0
   837
tmueller@129
   838
function loona:expire(dir, pat, maxage)
tmueller@129
   839
	return tek.util.expire(dir, pat, maxage)
tmueller@129
   840
end
tmueller@129
   841
tmueller@129
   842
--
tmueller@129
   843
--	Get pathname of an existing content file that
tmueller@129
   844
--	the current path is determined by or defaults to
tmueller@129
   845
--
tmueller@129
   846
tmueller@129
   847
function loona:getsectionpath(bodyname, requestpath)
tmueller@129
   848
	local ext = (not bodyname or bodyname == "main") and "" or "." .. bodyname
tmueller@129
   849
	local t, path, section = { }
tmueller@129
   850
	for _, menu in ipairs(self.submenus) do
tmueller@129
   851
		if menu.entries and menu.entries[menu.name] then
tmueller@129
   852
			table.insert(t, menu.name)
tmueller@129
   853
			local fn = table.concat(t, "_")
tmueller@129
   854
			if posix.stat(self.contentdir .. "/" .. fn .. ext, "mode") == "file" then
tmueller@129
   855
				path, section = fn, menu
tmueller@129
   856
			end
tmueller@0
   857
		end
tmueller@0
   858
	end
tmueller@129
   859
	return path, ext, section
tmueller@0
   860
end
tmueller@0
   861
tmueller@20
   862
tmueller@129
   863
function loona:body(name)
tmueller@129
   864
	name = self:checkbodyname(name)
tmueller@129
   865
	local path, ext = self:getsectionpath(name)
tmueller@129
   866
	self:dosnippet(self.editable(name, path and path .. ext, self.sectionname .. ext))
tmueller@0
   867
end
tmueller@0
   868
tmueller@23
   869
tmueller@129
   870
function loona:new(o)
tmueller@0
   871
tmueller@129
   872
	local parsed, msg
tmueller@129
   873
	
tmueller@129
   874
	o = o or { }
tmueller@129
   875
	o = atom.new(self, o)
tmueller@129
   876
	
tmueller@129
   877
	o.out = o.out or function(self, s) tek.web.out(s) end
tmueller@129
   878
	o.setheader = o.setheader or function(self, s) tek.web.setheader(s) end
tmueller@129
   879
	
tmueller@129
   880
	-- Get configuration
tmueller@129
   881
	
tmueller@129
   882
	o.config = o.config or tek.source(o.conffile or "../etc/config.lua") or { }
tmueller@129
   883
	o.config.defname = o.config.defname or "home"
tmueller@129
   884
	o.config.deflang = o.config.deflang or "en"
tmueller@129
   885
	o.config.sessionmaxage = o.config.sessionmaxage or 600
tmueller@129
   886
	o.config.secureport = o.config.secureport or 443
tmueller@129
   887
	o.config.passwdfile = posix.abspath(o.config.passwdfile or "../etc/passwd.lua")
tmueller@129
   888
	o.config.sessiondir = posix.abspath(o.config.sessiondir or "../var/sessions")
tmueller@129
   889
	o.config.extdir = posix.abspath(o.config.extdir or "../extensions")
tmueller@129
   890
	o.config.contentdir = posix.abspath(o.config.contentdir or "../content")
tmueller@129
   891
	o.config.localedir = posix.abspath(o.config.localedir or "../locale")
tmueller@129
   892
	o.config.htdocsdir = posix.abspath(o.config.htdocsdir or "../htdocs")
tmueller@131
   893
	o.config.htmlcachedir = posix.abspath(o.config.htmlcachedir or "../var/htmlcache")
tmueller@129
   894
	
tmueller@129
   895
	-- Create proxy for on-demand loading of locales
tmueller@129
   896
	
tmueller@129
   897
	o.locale = { }
tmueller@129
   898
	local locmt = { }
tmueller@129
   899
	locmt.__index = function(_, key)
tmueller@129
   900
		for _, l in ipairs(o.langs) do
tmueller@129
   901
			locmt.__locale = tek.source(o.config.localedir .. "/" .. l)
tmueller@129
   902
			if locmt.__locale then
tmueller@129
   903
				break
tmueller@129
   904
			end
tmueller@129
   905
		end
tmueller@129
   906
		locmt.__index = function(tab, key)
tmueller@129
   907
			return locmt.__locale[key] or key
tmueller@129
   908
		end
tmueller@129
   909
		return locmt.__locale[key] or key
tmueller@129
   910
	end
tmueller@129
   911
	setmetatable(o.locale, locmt)
tmueller@129
   912
	
tmueller@129
   913
	-- Get request, args, document, script name, request path
tmueller@129
   914
	
tmueller@129
   915
	o.request = o.request or cgi.request
tmueller@129
   916
	o.args = o.args or cgi.request.args
tmueller@129
   917
	o.session = o.session or cgi.session
tmueller@129
   918
 	
tmueller@129
   919
 	o.scriptpath = o.scriptpath or cgi.document.Path
tmueller@129
   920
	o.requesthandler = o.requesthandler or cgi.document.Handler
tmueller@129
   921
 	o.requestdocument = o.requestdocument or cgi.document.Name
tmueller@129
   922
	o.requestpath = o.requestpath or cgi.document.VirtualPath
tmueller@129
   923
tmueller@129
   924
	-- Manage login and establish session
tmueller@129
   925
	
tmueller@129
   926
	o.session.init(o.config.sessiondir, o.args.session, o.config.sessionmaxage)
tmueller@129
   927
	if o.args.login then
tmueller@129
   928
		if o.args.login == "false" then
tmueller@129
   929
			o.session.delete()
tmueller@129
   930
			o.session = nil
tmueller@129
   931
		elseif o.args.password then
tmueller@129
   932
			o.loginfailed = true
tmueller@129
   933
			local pwddb = tek.source(o.config.passwdfile)
tmueller@129
   934
			local pwdentry = pwddb[o.args.login]
tmueller@129
   935
			if pwdentry and pwdentry.password == o.args.password then
tmueller@129
   936
				o.session.data.authuser = pwdentry.username
tmueller@129
   937
				o.session.data.id = o.session.id
tmueller@129
   938
				o.loginfailed = nil
tmueller@129
   939
			end
tmueller@129
   940
		end
tmueller@129
   941
	end
tmueller@129
   942
	
tmueller@129
   943
	o.secure = o.request.Port == o.config.secureport
tmueller@129
   944
	o.authuser = o.session and o.session.data.authuser
tmueller@129
   945
	
tmueller@129
   946
	if o.nologin or not o.authuser then
tmueller@129
   947
		o.authuser = nil
tmueller@129
   948
		o.session = nil
tmueller@129
   949
-- 		o.args.session = nil -- TODO?
tmueller@129
   950
	end
tmueller@129
   951
	
tmueller@129
   952
	-- Get lang, locale, profile, section
tmueller@129
   953
	
tmueller@129
   954
	o:init()
tmueller@129
   955
	if o.authuser then
tmueller@129
   956
		o:handlechanges()
tmueller@129
   957
	end
tmueller@129
   958
	
tmueller@129
   959
	-- Current document
tmueller@129
   960
	
tmueller@129
   961
	o.document = o.requestdocument .. "/" .. o.sectionpath
tmueller@129
   962
	if o.authuser then
tmueller@129
   963
		o.getdocname = function(self, path)
tmueller@129
   964
			return path and self.requestdocument .. "/" .. path or self.requestdocument
tmueller@129
   965
		end
tmueller@129
   966
	else
tmueller@129
   967
		o.getdocname = function(self, path)
tmueller@129
   968
			path = path or self.config.defname
tmueller@129
   969
			if self:isdynamic(path) then
tmueller@129
   970
				return self.requestdocument .. "/" .. path
tmueller@129
   971
			end
tmueller@129
   972
			path = path == self.config.defname and "index" or path
tmueller@129
   973
			return "/" .. path:gsub("/", "_") .. ".html"
tmueller@129
   974
		end
tmueller@129
   975
	end
tmueller@129
   976
	
tmueller@129
   977
	-- Create "editable section" function
tmueller@129
   978
	
tmueller@129
   979
	local func, msg = o:loadhtml(open("loona/editable.lua"),
tmueller@129
   980
		"loona:out", "loona/editable.lua")
tmueller@129
   981
 	assert(func, o:dbmsg("Syntax error", msg))
tmueller@129
   982
	o.editable = o:runboxed(func)
tmueller@129
   983
	
tmueller@129
   984
	-- Save session state
tmueller@129
   985
	
tmueller@129
   986
	if o.session then
tmueller@129
   987
		o.session.save()
tmueller@129
   988
	end
tmueller@129
   989
tmueller@129
   990
	return o
tmueller@0
   991
end
tmueller@0
   992
tmueller@23
   993
tmueller@129
   994
function loona:execute(fname)
tmueller@129
   995
	self:indexdynamic() -- TODO: this a solution?
tmueller@129
   996
	fname = fname or self.requesthandler
tmueller@129
   997
	local parsed, msg = self:loadhtml(open(fname), "loona:out", fname)
tmueller@129
   998
	assert(parsed, self:dbmsg("HTML/Lua parsing failed", msg))
tmueller@129
   999
	self:runboxed(parsed)
tmueller@129
  1000
	return self
tmueller@129
  1001
end
tmueller@80
  1002
tmueller@80
  1003
tmueller@129
  1004
function loona:indexdynamic()
tmueller@129
  1005
	self:recursesections(self.sections, function(self, s, e, path, dynamic)
tmueller@129
  1006
		path = path and path .. "_" .. e.name or e.name
tmueller@129
  1007
		dynamic = dynamic or { }
tmueller@129
  1008
		for k in pairs(e.dynamic or { }) do
tmueller@129
  1009
			dynamic[k] = true
tmueller@129
  1010
		end
tmueller@129
  1011
		for k in pairs(dynamic) do
tmueller@129
  1012
			local ext = (k == "main" and "") or "." .. k
tmueller@129
  1013
			if posix.stat(self.contentdir .. "/" .. path .. ext, "mode") == "file" then
tmueller@129
  1014
				dynamic[k] = e.dynamic and e.dynamic[k]
tmueller@129
  1015
			end
tmueller@129
  1016
		end
tmueller@129
  1017
		local n = 0
tmueller@129
  1018
		for k in pairs(dynamic) do
tmueller@129
  1019
			n = n + 1
tmueller@129
  1020
		end
tmueller@129
  1021
		if n > 0 then
tmueller@129
  1022
			e.dynamic = { }
tmueller@129
  1023
			for k in pairs(dynamic) do
tmueller@129
  1024
				e.dynamic[k] = true
tmueller@129
  1025
			end
tmueller@129
  1026
		else
tmueller@129
  1027
			e.dynamic = nil
tmueller@129
  1028
		end
tmueller@129
  1029
		return path, dynamic
tmueller@129
  1030
	end)
tmueller@129
  1031
end
tmueller@80
  1032
tmueller@0
  1033
tmueller@129
  1034
function loona:isdynamic(path)
tmueller@129
  1035
	path = path or self.sectionpath
tmueller@129
  1036
	local t, i = self:checkpath(path)
tmueller@129
  1037
	return t and t[i].dynamic
tmueller@129
  1038
end
tmueller@0
  1039
tmueller@0
  1040
tmueller@129
  1041
function loona:dumphtml(o)
tmueller@129
  1042
	local outbuf = { }
tmueller@129
  1043
	o = o or { }
tmueller@129
  1044
	o.nologin = true
tmueller@129
  1045
	o.out = function(self, s) table.insert(outbuf, s) end
tmueller@129
  1046
	o.setheader = function(self, s) end
tmueller@129
  1047
	o = self:new(o):execute()
tmueller@129
  1048
	if not o:isdynamic() then
tmueller@129
  1049
		local path = o.sectionname
tmueller@129
  1050
		path = path == o.config.defname and "index" or path
tmueller@131
  1051
		local srcname = o.config.htdocsdir .. "/" .. path .. ".html"
tmueller@131
  1052
		local dstname = o.config.htmlcachedir .. "/" .. path .. ".html.tmp"
tmueller@131
  1053
		local fh, msg = open(srcname .. ".tmp", "wb")
tmueller@131
  1054
		assert(fh, self:dbmsg("Could not write cached HTML", msg))
tmueller@129
  1055
		fh:write(unpack(outbuf))
tmueller@129
  1056
		fh:close()
tmueller@131
  1057
		local success, msg = posix.symlink(srcname, dstname)
tmueller@137
  1058
-- 		assert(success, self:dbmsg("Could not link to cached HTML", msg))
tmueller@0
  1059
	end
tmueller@0
  1060
end