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