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