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