cgi-bin/loona.lua
author Timm S. Mueller <tmueller@neoscientists.org>
Thu, 22 Feb 2007 14:52:21 +0100
changeset 80 cbb6e85e04b4
parent 77 77254b00ba67
child 83 17d62210a4fc
permissions -rw-r--r--
Added menu function
tmueller@0
     1
tmueller@0
     2
--
tmueller@0
     3
--	loona - tiny CMS
tmueller@0
     4
--	Written by Timm S. Mueller <tmueller at neoscientists.org>
tmueller@0
     5
--	See copyright notice in COPYRIGHT
tmueller@0
     6
--
tmueller@0
     7
tmueller@68
     8
local tek = require "tek"
tmueller@68
     9
local cgi = require "tek.cgi"
tmueller@68
    10
local posix = require "tek.posix"
tmueller@0
    11
require "tek.cgi.request"
tmueller@0
    12
require "tek.cgi.request.args"
tmueller@0
    13
require "tek.cgi.session"
tmueller@0
    14
require "tek.web"
tmueller@0
    15
require "tek.web.markup"
tmueller@0
    16
require "tek.util"
tmueller@0
    17
tmueller@23
    18
tmueller@0
    19
local boxed_G = { 
tmueller@0
    20
	string = string, table = table,
tmueller@0
    21
	assert = assert, collectgarbage = collectgarbage, dofile = dofile,
tmueller@0
    22
	error = error, getfenv = getfenv, getmetatable = getmetatable,
tmueller@0
    23
	ipairs = ipairs, load = load, loadfile = loadfile, loadstring = loadstring,
tmueller@0
    24
	next = next, pairs = pairs, pcall = pcall, print = print,
tmueller@0
    25
	rawequal = rawequal, rawget = rawget, rawset = rawset, require = require,
tmueller@0
    26
	select = select, setfenv = setfenv, setmetatable = setmetatable,
tmueller@0
    27
	tonumber = tonumber, tostring = tostring, type = type, unpack = unpack,
tmueller@0
    28
	xpcall = xpcall
tmueller@0
    29
}
tmueller@0
    30
tmueller@68
    31
local table, string, assert, unpack, ipairs, pairs, type, require =
tmueller@68
    32
	table, string, assert, unpack, ipairs, pairs, type, require
tmueller@0
    33
local setmetatable, setfenv, getfenv = setmetatable, setfenv, getfenv
tmueller@0
    34
local open, remove, rename, getenv, time =
tmueller@0
    35
	io.open, os.remove, os.rename, os.getenv, os.time
tmueller@0
    36
tmueller@68
    37
local sectionfname, langs, docname
tmueller@0
    38
tmueller@0
    39
tmueller@0
    40
module "loona"
tmueller@0
    41
tmueller@0
    42
tmueller@0
    43
_VERSION = 2
tmueller@80
    44
_REVISION = 2
tmueller@0
    45
tmueller@0
    46
tmueller@0
    47
out = tek.web.out
tmueller@0
    48
setheader = tek.web.setheader
tmueller@0
    49
session = cgi.session
tmueller@0
    50
request = cgi.request
tmueller@0
    51
args = request.args
tmueller@0
    52
encodeform = cgi.encodeform
tmueller@0
    53
loadhtml = tek.web.include.load
tmueller@0
    54
source = tek.source
tmueller@0
    55
domarkup = tek.web.markup.main
tmueller@0
    56
expire = tek.util.expire
tmueller@0
    57
tmueller@0
    58
tmueller@20
    59
local function dbmsg(msg, detail)
tmueller@80
    60
	return (msg and detail and config.debug) and
tmueller@47
    61
		("%s : %s"):format(msg, detail) or msg
tmueller@20
    62
end
tmueller@20
    63
tmueller@20
    64
tmueller@0
    65
local function checkprofilename(c)
tmueller@47
    66
	assert(c:match("^%w+$") and c ~= "current",
tmueller@47
    67
		dbmsg("Invalid profile name", c))
tmueller@0
    68
	return c
tmueller@0
    69
end
tmueller@0
    70
tmueller@0
    71
tmueller@47
    72
local function checkbodyname(s)
tmueller@0
    73
	s = s or "main"
tmueller@47
    74
	assert(s:match("^[%w_]*%w+[%w_]*$"), dbmsg("Invalid body name", s))
tmueller@0
    75
	return s
tmueller@0
    76
end
tmueller@0
    77
tmueller@0
    78
tmueller@0
    79
local function deletedir(dst)
tmueller@0
    80
	for e in tek.util.readdir(dst) do
tmueller@47
    81
 		local success, msg = remove(dst .. "/" .. e)
tmueller@20
    82
		assert(success, dbmsg("Error removing entry in profile", msg))
tmueller@0
    83
	end
tmueller@0
    84
	return remove(dst)
tmueller@0
    85
end
tmueller@0
    86
tmueller@0
    87
tmueller@0
    88
local function copyprofile(contentdir, lang, srcprofile, newprofile)
tmueller@68
    89
	local src = ("%s/%s_%s"):format(contentdir, srcprofile, lang)
tmueller@20
    90
	assert(posix.stat(src, "mode") == "directory",
tmueller@20
    91
		dbmsg("Not a directory", src))
tmueller@68
    92
	local dst = ("%s/%s_%s"):format(contentdir, newprofile, lang)
tmueller@0
    93
	local success, msg = posix.mkdir(dst)
tmueller@20
    94
	assert(success, dbmsg("Error creating profile directory", msg))
tmueller@0
    95
	for e in tek.util.readdir(src) do
tmueller@0
    96
		local ext = e:match("^[^.].*%.([^.]*)$")
tmueller@0
    97
		if ext ~= "LOCK" then
tmueller@0
    98
			local f = src .. "/" .. e
tmueller@0
    99
			if posix.stat(f, "mode") == "file" then
tmueller@0
   100
				success, msg = tek.copyfile(f, dst .. "/" .. e)
tmueller@20
   101
				assert(success, dbmsg("Error copying file", msg))
tmueller@0
   102
			end
tmueller@0
   103
		end
tmueller@0
   104
	end
tmueller@0
   105
end
tmueller@0
   106
tmueller@0
   107
tmueller@0
   108
local function publishprofile(contentdir, lang, profile)
tmueller@68
   109
	local newpath = ("%s/current_%s"):format(contentdir, lang)
tmueller@68
   110
	local tmppath = newpath .. "." .. session.name
tmueller@23
   111
	local success, msg = posix.symlink(profile .. "_" .. lang, tmppath)
tmueller@20
   112
	assert(success, dbmsg("Cannot create symlink", msg))
tmueller@23
   113
	success, msg = rename(tmppath, newpath)
tmueller@23
   114
	assert(success, dbmsg("Cannot put symlink in place", msg))
tmueller@0
   115
end
tmueller@0
   116
tmueller@0
   117
tmueller@20
   118
local function lookupname(tab, val)
tmueller@0
   119
	if tab then
tmueller@0
   120
		for i, v in ipairs(tab) do
tmueller@20
   121
			if v.name == val then
tmueller@0
   122
				return i
tmueller@0
   123
			end
tmueller@0
   124
		end
tmueller@0
   125
	end
tmueller@0
   126
end
tmueller@0
   127
tmueller@0
   128
tmueller@20
   129
--	Index sections, determine accessibility and visibility in menu
tmueller@20
   130
tmueller@20
   131
local function indexsections(s)
tmueller@0
   132
	for _, e in ipairs(s) do
tmueller@0
   133
		if e.subs then
tmueller@20
   134
			indexsections(e.subs)
tmueller@0
   135
		end
tmueller@0
   136
		e.notvalid = (not secure and e.secure) or 
tmueller@0
   137
			(not authuser and e.secret) or nil
tmueller@0
   138
		e.notvisible = e.notvalid or not authuser and e.hidden or nil
tmueller@16
   139
		s[e.name] = e
tmueller@0
   140
	end
tmueller@0
   141
end
tmueller@0
   142
tmueller@0
   143
tmueller@20
   144
--	Decompose section path into a stack of sections, returning only up to
tmueller@0
   145
--	the last valid element in the path. additionally returns the table of
tmueller@0
   146
--	the last section path element (or the default section)
tmueller@0
   147
tmueller@27
   148
local function getsection(section, authuser, path, default)
tmueller@27
   149
	local tab = { { entries = sections, name = default } }
tmueller@27
   150
	local ss = sections
tmueller@0
   151
	local sectionpath
tmueller@0
   152
	(path or default):gsub("(%w+)/?", function(a)
tmueller@27
   153
		if ss then
tmueller@27
   154
			local s = ss[a]
tmueller@20
   155
			if s and not s.notvalid then
tmueller@20
   156
				sectionpath = s
tmueller@0
   157
				tab[#tab].name = a
tmueller@27
   158
				ss = s.subs
tmueller@27
   159
				if ss then
tmueller@27
   160
					table.insert(tab, { entries = ss })
tmueller@0
   161
				end
tmueller@0
   162
			else
tmueller@27
   163
				ss = nil -- stop.
tmueller@0
   164
			end
tmueller@0
   165
		end
tmueller@0
   166
	end)
tmueller@0
   167
	if not section and not sectionpath then
tmueller@27
   168
		sectionpath = sections[default]
tmueller@0
   169
		if sectionpath then
tmueller@0
   170
			table.insert(tab, { entries = sectionpath.subs })
tmueller@0
   171
		end
tmueller@0
   172
	end
tmueller@0
   173
	return tab, sectionpath
tmueller@0
   174
end
tmueller@0
   175
tmueller@0
   176
tmueller@27
   177
local function getpath(menus, delimiter)
tmueller@0
   178
	local t = { }
tmueller@27
   179
	for _, menu in ipairs(menus) do
tmueller@0
   180
		if menu.name then
tmueller@0
   181
			table.insert(t, menu.name)
tmueller@0
   182
		end
tmueller@0
   183
	end
tmueller@0
   184
	return table.concat(t, delimiter or "/")
tmueller@0
   185
end
tmueller@0
   186
tmueller@0
   187
tmueller@20
   188
--	Descending into the sections table alongside the current path,
tmueller@20
   189
--	return the filename to include, defaulting to its parent value
tmueller@20
   190
--	(or the default specified)
tmueller@0
   191
tmueller@27
   192
local function getsectionfile(menus, path, ext, default)
tmueller@20
   193
	local t, val = { }
tmueller@27
   194
	for _, menu in ipairs(menus) do
tmueller@20
   195
		if menu.entries and menu.entries[menu.name] then
tmueller@20
   196
			table.insert(t, menu.name)
tmueller@20
   197
			local fn = table.concat(t, "_") .. ext
tmueller@0
   198
			if posix.stat(path .. "/" .. fn, "mode") == "file" then
tmueller@0
   199
				val = fn
tmueller@0
   200
			end
tmueller@0
   201
		end
tmueller@0
   202
	end
tmueller@0
   203
	return val or default
tmueller@0
   204
end
tmueller@0
   205
tmueller@0
   206
tmueller@45
   207
local function deletesection(dir, fname)
tmueller@0
   208
	local fullname = dir .. "/" .. fname
tmueller@0
   209
	local success, msg = remove(fullname)
tmueller@0
   210
	if success then
tmueller@0
   211
		local pat = "^" .. 
tmueller@0
   212
			fname:gsub("%^%$%(%)%%%.%[%]%*%+%-%?", "%%%1") .. "%..*$"
tmueller@0
   213
		for e in tek.util.readdir(dir) do
tmueller@0
   214
			if e:match(pat) then
tmueller@0
   215
				remove(dir .. "/" .. e)
tmueller@0
   216
			end
tmueller@0
   217
		end
tmueller@0
   218
	end
tmueller@0
   219
	return success, msg
tmueller@0
   220
end
tmueller@0
   221
tmueller@0
   222
tmueller@20
   223
--	Add element to path
tmueller@0
   224
tmueller@20
   225
local function addtopath(tab, path, e)
tmueller@0
   226
	path:gsub("(%w+)/?", function(a)
tmueller@20
   227
		if tab then
tmueller@20
   228
			local s = tab[a]
tmueller@20
   229
			if s then
tmueller@20
   230
				if not s.subs then
tmueller@20
   231
					s.subs = { }
tmueller@0
   232
				end
tmueller@20
   233
				tab = s.subs
tmueller@0
   234
			else
tmueller@20
   235
				table.insert(tab, e)
tmueller@20
   236
				tab[a] = e
tmueller@20
   237
 				tab = nil -- stop
tmueller@0
   238
			end
tmueller@0
   239
		end
tmueller@0
   240
	end)
tmueller@0
   241
end
tmueller@0
   242
tmueller@0
   243
tmueller@20
   244
--	Remove element from path
tmueller@0
   245
tmueller@20
   246
local function rmpath(tab, path)
tmueller@20
   247
	local parent
tmueller@0
   248
	path:gsub("(%w+)/?", function(a)
tmueller@20
   249
		if tab then
tmueller@20
   250
			local idx = lookupname(tab, a)
tmueller@20
   251
			if idx then
tmueller@20
   252
				if tab[idx].subs then
tmueller@20
   253
					parent = tab[idx]
tmueller@20
   254
					tab = tab[idx].subs
tmueller@0
   255
				else
tmueller@20
   256
					table.remove(tab, idx)
tmueller@43
   257
					tab[a] = nil
tmueller@20
   258
					if #tab == 0 and parent then
tmueller@20
   259
						parent.subs = nil
tmueller@0
   260
					end
tmueller@20
   261
					tab = nil
tmueller@0
   262
				end
tmueller@0
   263
			end
tmueller@0
   264
		end
tmueller@0
   265
	end)
tmueller@0
   266
end
tmueller@0
   267
tmueller@0
   268
tmueller@0
   269
-------------------------------------------------------------------------------
tmueller@0
   270
tmueller@0
   271
tmueller@8
   272
--	Produce page title
tmueller@8
   273
tmueller@8
   274
function title()
tmueller@8
   275
	return section and (section.title or section.label or section.name) or ""
tmueller@8
   276
end
tmueller@8
   277
tmueller@8
   278
tmueller@47
   279
--	Create proxy for on-demand loading of locale strings
tmueller@0
   280
tmueller@47
   281
locale = { }
tmueller@47
   282
local locmt = { }
tmueller@47
   283
locmt.__index = function(_, key)
tmueller@47
   284
	for _, l in ipairs(langs) do
tmueller@47
   285
		locmt.__locale = source(config.localedir .. "/" .. l)
tmueller@47
   286
		if locmt.__locale then
tmueller@47
   287
			break
tmueller@0
   288
		end
tmueller@0
   289
	end
tmueller@47
   290
	locmt.__index = locmt.__locale
tmueller@47
   291
	return locmt.__locale[key]
tmueller@0
   292
end
tmueller@47
   293
setmetatable(locale, locmt)
tmueller@0
   294
tmueller@0
   295
tmueller@0
   296
--	Find element in path
tmueller@0
   297
tmueller@0
   298
function checkpath(tab, path)
tmueller@0
   299
	local res, idx
tmueller@0
   300
	path:gsub("(%w+)/?", function(a)
tmueller@0
   301
		if tab then
tmueller@20
   302
			local i = lookupname(tab, a)
tmueller@0
   303
			if i then
tmueller@0
   304
				res, idx = tab, i
tmueller@0
   305
				tab = tab[i].subs
tmueller@0
   306
			else
tmueller@0
   307
				res, idx = nil, nil
tmueller@0
   308
			end
tmueller@0
   309
		end
tmueller@0
   310
	end)
tmueller@0
   311
	return res, idx
tmueller@0
   312
end
tmueller@0
   313
tmueller@0
   314
tmueller@0
   315
--	Run a site function snippet, with full error recovery
tmueller@0
   316
--	(also recovers from errors in error handling function)
tmueller@0
   317
tmueller@23
   318
function dosnippet(config, func, errfunc)
tmueller@0
   319
	local ret = { tek.catch(func) }
tmueller@0
   320
	if ret[1] == 0 or (errfunc and tek.catch(errfunc) == 0) then
tmueller@0
   321
		return unpack(ret)
tmueller@0
   322
	end
tmueller@0
   323
	out("<h2>Error</h2>")
tmueller@68
   324
	out("<h3>" .. cgi.encodeform(ret[2] or "") .. "</h3>")
tmueller@80
   325
	if config.debug then
tmueller@0
   326
		if type(ret[3]) == "string" then
tmueller@68
   327
			out("<p>" .. cgi.encodeform(ret[3]) .. "</p>")
tmueller@0
   328
		end
tmueller@80
   329
		if ret[4] and config.debug then
tmueller@68
   330
			out("<pre>" .. cgi.encodeform(ret[4]) .. "</pre>")
tmueller@0
   331
		end
tmueller@0
   332
	end
tmueller@0
   333
end	
tmueller@0
   334
tmueller@0
   335
tmueller@0
   336
function lockfile(newfile)
tmueller@0
   337
	return not session and true or 
tmueller@0
   338
		posix.symlink(session.filename, newfile .. ".LOCK")
tmueller@0
   339
end
tmueller@0
   340
tmueller@0
   341
tmueller@0
   342
function unlockfile(dstfile)
tmueller@0
   343
	return not session and true or remove(dstfile .. ".LOCK")
tmueller@0
   344
end
tmueller@0
   345
tmueller@0
   346
tmueller@45
   347
function savesection(dir, fname, content)
tmueller@0
   348
	fname = dir .. "/" .. fname
tmueller@0
   349
	local f, msg = open(fname, "wb")
tmueller@20
   350
	assert(f, dbmsg("Could not open file for writing", msg))
tmueller@0
   351
	f:write(content or "")
tmueller@0
   352
	f:close()
tmueller@0
   353
end
tmueller@0
   354
tmueller@23
   355
tmueller@20
   356
--	Run extension in a controlled environment
tmueller@0
   357
tmueller@0
   358
function include(fname, ...)
tmueller@47
   359
	assert(not fname:match("%W"), dbmsg("Invalid include name", fname))
tmueller@47
   360
	local fname2 = ("%s/%s.lua"):format(config.extdir, fname)
tmueller@0
   361
	local f, msg = open(fname2)
tmueller@20
   362
	assert(f, dbmsg("Cannot open file", msg))
tmueller@0
   363
	local parsed, msg = loadhtml(f, "loona.out", fname2)
tmueller@20
   364
	assert(parsed, dbmsg("Syntax error", msg))
tmueller@0
   365
 	local fenv = {
tmueller@0
   366
 		arg = arg,
tmueller@0
   367
 		loona = {
tmueller@0
   368
 			out = out,
tmueller@0
   369
 			setheader = setheader,
tmueller@0
   370
			hidden = hidden,
tmueller@15
   371
			link = link,
tmueller@0
   372
			elink = elink,
tmueller@68
   373
			plink = plink,
tmueller@0
   374
			href = href,
tmueller@0
   375
			checkpath = checkpath,
tmueller@0
   376
			authuser = authuser,
tmueller@0
   377
			document = document,
tmueller@0
   378
			contentdir = contentdir,
tmueller@0
   379
			profile = profile,
tmueller@0
   380
			pubprofile = pubprofile,
tmueller@0
   381
			lang = lang,
tmueller@47
   382
			locale = locale,
tmueller@0
   383
			secure = secure,
tmueller@0
   384
			config = config,
tmueller@27
   385
			sectionpath = sectionpath,
tmueller@27
   386
			sections = sections,
tmueller@0
   387
			session = session,
tmueller@0
   388
		}
tmueller@0
   389
	}
tmueller@0
   390
 	setmetatable(fenv, { __index = boxed_G }) -- boxed global environment
tmueller@0
   391
	setfenv(parsed, fenv)
tmueller@0
   392
	return parsed()
tmueller@0
   393
end
tmueller@0
   394
tmueller@0
   395
tmueller@68
   396
--	produce link target, propagate lang, profile, session
tmueller@68
   397
tmueller@0
   398
function href(section, ...)
tmueller@68
   399
	local target = section and docname .. "/" .. section or docname
tmueller@68
   400
	if session or profile ~= pubprofile then
tmueller@68
   401
		return tek.web.gethref(target, { "profile", "session", "lang", 
tmueller@68
   402
			unpack(arg) })
tmueller@0
   403
	end
tmueller@68
   404
	return tek.web.gethref(target, { "lang", unpack(arg) })
tmueller@0
   405
end
tmueller@0
   406
tmueller@68
   407
local function ilink(target, text, extra)
tmueller@68
   408
	return ('<a href="%s"%s>%s</a>'):format(target, extra or "", text)
tmueller@0
   409
end
tmueller@0
   410
tmueller@68
   411
--	internal link, propagation of lang, profile, session
tmueller@0
   412
tmueller@68
   413
function link(section, text, ...)
tmueller@68
   414
	return ilink(href(section, unpack(arg)), text or section,
tmueller@68
   415
		' class="loonaIntLink"')
tmueller@0
   416
end
tmueller@0
   417
tmueller@68
   418
--	external link (opens in a new window), no argument propagation
tmueller@0
   419
tmueller@68
   420
function elink(target, text)
tmueller@68
   421
	return ilink(target, text or target,
tmueller@68
   422
		' class="loonaExtLink" onclick="void(window.open(this.href, \'\', \'\')); return false;"')
tmueller@0
   423
end
tmueller@0
   424
tmueller@68
   425
--	plain link, no argument propagation
tmueller@68
   426
tmueller@68
   427
function plink(section, text, ...)
tmueller@68
   428
	return ilink(tek.web.gethref(section, arg), text or section)
tmueller@68
   429
end
tmueller@68
   430
tmueller@68
   431
--	user interface link, propagation of lang, profile, session
tmueller@68
   432
tmueller@68
   433
function uilink(section, text, ...)
tmueller@68
   434
	return ilink(href(section, unpack(arg)), text or section,
tmueller@68
   435
		' class="loonaUILink"')
tmueller@68
   436
end
tmueller@68
   437
tmueller@68
   438
--	produce a hidden input value in forms
tmueller@0
   439
tmueller@0
   440
function hidden(name, value)
tmueller@68
   441
	return not value and "" or
tmueller@68
   442
		('<input type="hidden" name="%s" value="%s" />'):format(name, value)
tmueller@0
   443
end
tmueller@0
   444
tmueller@0
   445
tmueller@0
   446
function getprofiles(contentdir, lang)
tmueller@0
   447
	local t = { }
tmueller@0
   448
	for f in tek.util.readdir(contentdir) do
tmueller@0
   449
		if posix.lstat(contentdir .. "/" .. f, "mode") == "directory" then
tmueller@0
   450
			local e = f:match("^(%w+)_" .. lang .. "$")
tmueller@0
   451
 			if e then
tmueller@0
   452
	 			t[e] = e
tmueller@0
   453
 	 		end
tmueller@0
   454
		end
tmueller@0
   455
	end
tmueller@0
   456
	return t
tmueller@0
   457
end
tmueller@0
   458
tmueller@0
   459
tmueller@80
   460
--	Functions to produce a hierarchical navigation menu
tmueller@80
   461
tmueller@80
   462
local function rmenu(level, linkf, path)
tmueller@80
   463
	local sub = submenus[level]
tmueller@80
   464
	if sub and sub.entries then
tmueller@80
   465
		local visible = { }
tmueller@80
   466
		for _, e in ipairs(sub.entries) do
tmueller@80
   467
			if not e.notvisible then
tmueller@80
   468
				table.insert(visible, e)
tmueller@80
   469
			end
tmueller@80
   470
		end
tmueller@80
   471
		if #visible > 0 then
tmueller@80
   472
			out('<ul id="loonaMenu' .. level .. '">')
tmueller@80
   473
			for _, e in ipairs(visible) do
tmueller@80
   474
				local label = encodeform(e.label or e.name)
tmueller@80
   475
				local newpath = path and path .. "/" .. e.name or e.name
tmueller@80
   476
				local active = (e.name == sub.name)
tmueller@80
   477
				out('<li>')
tmueller@80
   478
				linkf(newpath, label, active, e.action)
tmueller@80
   479
				if active then
tmueller@80
   480
					rmenu(level + 1, linkf, newpath)
tmueller@80
   481
				end
tmueller@80
   482
				out('</li>')
tmueller@80
   483
			end
tmueller@80
   484
			out('</ul>')
tmueller@80
   485
		end
tmueller@80
   486
	end
tmueller@80
   487
end
tmueller@80
   488
tmueller@80
   489
local function mlink(path, label, active, ...)
tmueller@80
   490
	local href = href(path, unpack(arg))
tmueller@80
   491
	if active then
tmueller@80
   492
		out('<a class="loonaActiveMenu" href="' .. href .. '">' .. label .. '</a>')
tmueller@80
   493
	else
tmueller@80
   494
		out('<a href="' .. href .. '">' .. label .. '</a>')
tmueller@80
   495
	end	
tmueller@80
   496
end
tmueller@80
   497
tmueller@80
   498
function menu(level, linkf)
tmueller@80
   499
	rmenu(level or 1, linkf or mlink)
tmueller@80
   500
end
tmueller@80
   501
tmueller@80
   502
tmueller@0
   503
-- Init
tmueller@0
   504
tmueller@0
   505
local function init()
tmueller@0
   506
	
tmueller@0
   507
	-- get list of languages, in order of preference
tmueller@0
   508
	
tmueller@0
   509
	langs = { args.lang and args.lang:match("^%w+$") }
tmueller@80
   510
	if config.browserlang then
tmueller@0
   511
		local s = getenv("HTTP_ACCEPT_LANGUAGE")
tmueller@0
   512
		while s do
tmueller@0
   513
			local l, r = s:match("^([%w.=]+)[,;](.*)$")
tmueller@0
   514
			l = l or s
tmueller@0
   515
			s = r
tmueller@0
   516
			if l:match("^%w+$") then
tmueller@0
   517
				table.insert(langs, l)
tmueller@0
   518
			end
tmueller@0
   519
		end
tmueller@0
   520
	end
tmueller@0
   521
	table.insert(langs, config.deflang)
tmueller@0
   522
	
tmueller@0
   523
	-- get list of possible profiles
tmueller@0
   524
	
tmueller@0
   525
	local profiles = { }
tmueller@0
   526
	for e in tek.util.readdir(config.contentdir) do
tmueller@0
   527
		profiles[e] = e
tmueller@0
   528
	end
tmueller@0
   529
	
tmueller@0
   530
	-- get pubprofile
tmueller@0
   531
	
tmueller@0
   532
	for _, lang in ipairs(langs) do
tmueller@0
   533
		local p = posix.readlink(config.contentdir .. "/current_" .. lang)
tmueller@0
   534
		p = p and p:match("^(%w+)_" .. lang .. "$")
tmueller@0
   535
		if p then
tmueller@0
   536
			pubprofile = p
tmueller@0
   537
			break
tmueller@0
   538
		end
tmueller@0
   539
	end
tmueller@0
   540
	
tmueller@0
   541
	-- get profile
tmueller@0
   542
	
tmueller@0
   543
	local checkprofile = authuser and args.profile or pubprofile or "default"
tmueller@0
   544
	for _, l in ipairs(langs) do
tmueller@0
   545
		if profiles[checkprofile .. "_" .. l] then
tmueller@0
   546
			profile = checkprofile
tmueller@0
   547
			lang = l
tmueller@0
   548
			break
tmueller@0
   549
		end
tmueller@0
   550
	end
tmueller@0
   551
	
tmueller@20
   552
	assert(profile and lang, "Invalid profile or language")
tmueller@0
   553
	
tmueller@47
   554
	
tmueller@47
   555
	-- Write back language and profile
tmueller@0
   556
tmueller@0
   557
	args.lang = lang ~= config.deflang and lang or nil
tmueller@0
   558
	args.profile = profile
tmueller@0
   559
	
tmueller@47
   560
	
tmueller@0
   561
	-- determine content directory pathname and section filename
tmueller@0
   562
	
tmueller@68
   563
	contentdir = ("%s/%s_%s"):format(config.contentdir, profile, lang)
tmueller@68
   564
 	sectionfname = contentdir .. "/.sections"
tmueller@0
   565
	
tmueller@0
   566
	-- load sections
tmueller@0
   567
	
tmueller@68
   568
 	sections = source(sectionfname)
tmueller@0
   569
	
tmueller@20
   570
	-- index sections, determine visibility in menu
tmueller@0
   571
	
tmueller@27
   572
	indexsections(sections)
tmueller@0
   573
	
tmueller@0
   574
	-- decompose section path, produce a stack of sections
tmueller@0
   575
	
tmueller@27
   576
	submenus, section = getsection(section, authuser, 
tmueller@0
   577
		cgi.document.VirtualPath or "", not authuser and config.defname)
tmueller@0
   578
tmueller@0
   579
	-- handle redirects if not logged on
tmueller@0
   580
	
tmueller@0
   581
	if not authuser and section and section.redirect then
tmueller@27
   582
		submenus, section = getsection(section, authuser, 
tmueller@0
   583
			section.redirect, not authuser and config.defname)
tmueller@0
   584
	end
tmueller@0
   585
			
tmueller@0
   586
	-- section path and document name (refined)
tmueller@0
   587
	
tmueller@0
   588
	sectionpath = getpath(submenus)
tmueller@77
   589
	sectionname = getpath(submenus, "_")
tmueller@0
   590
tmueller@0
   591
end
tmueller@0
   592
tmueller@0
   593
tmueller@20
   594
--	Handle state modifications (create/save/delete, profile management)
tmueller@0
   595
tmueller@0
   596
local function handlestate()
tmueller@23
   597
tmueller@0
   598
	if args.editkey == "main" then
tmueller@23
   599
		
tmueller@23
   600
		-- In main editable section:
tmueller@23
   601
		
tmueller@23
   602
		local save
tmueller@0
   603
		
tmueller@0
   604
		if args.actioncreate then
tmueller@45
   605
			-- Create new section
tmueller@0
   606
			local editname = args.editname:lower()
tmueller@20
   607
			assert(not editname:match("%W"),
tmueller@47
   608
				dbmsg("Invalid section name", editname))
tmueller@20
   609
			if not (section and (section.subs or section)[editname]) then
tmueller@20
   610
				local newpath = 
tmueller@20
   611
					(sectionpath and (sectionpath .. "/")) .. editname
tmueller@27
   612
				addtopath(sections, newpath, { name = editname,
tmueller@0
   613
					label = args.editlabel ~= "" and args.editlabel or nil,
tmueller@0
   614
					title = args.edittitle ~= "" and args.edittitle or nil,
tmueller@43
   615
					redirect = args.editredirect ~= "" and args.editredirect or nil,
tmueller@43
   616
					hidden = args.editvisibility and true,
tmueller@43
   617
					secret = args.editsecrecy and true,
tmueller@43
   618
					secure = args.editsecure and true,
tmueller@0
   619
					creator = authuser,
tmueller@20
   620
					creationdate = time() })
tmueller@23
   621
				save = true
tmueller@0
   622
			end
tmueller@20
   623
		
tmueller@0
   624
		elseif args.actionsave then
tmueller@45
   625
			-- Save section
tmueller@0
   626
			section.revisiondate = time()
tmueller@0
   627
			section.revisioner = authuser
tmueller@23
   628
			save = true
tmueller@20
   629
		
tmueller@0
   630
		elseif args.actiondelete then
tmueller@45
   631
			-- Delete section
tmueller@0
   632
			if not args.actionconfirm then
tmueller@20
   633
				useralert = {
tmueller@47
   634
					text = locale.ALERT_DELETE_SECTION,
tmueller@20
   635
					confirm =
tmueller@20
   636
						'<input type="submit" name="actiondelete" value="' .. 
tmueller@47
   637
						locale.DELETE .. '" /> ' ..
tmueller@20
   638
						hidden("actionconfirm", "true")
tmueller@20
   639
				}
tmueller@0
   640
			else
tmueller@77
   641
				deletesection(contentdir, sectionname)
tmueller@27
   642
				rmpath(sections, sectionpath)
tmueller@23
   643
				save = true
tmueller@0
   644
			end
tmueller@20
   645
		
tmueller@0
   646
		elseif args.actionsaveprops then
tmueller@23
   647
			-- Save properties
tmueller@0
   648
			section.hidden = args.editvisibility and true
tmueller@0
   649
			section.secret = args.editsecrecy and true
tmueller@0
   650
			section.secure = args.editsecure and true
tmueller@0
   651
			section.label = args.editlabel ~= "" and args.editlabel or nil
tmueller@0
   652
			section.title = args.edittitle ~= "" and args.edittitle or nil
tmueller@20
   653
			section.redirect =
tmueller@20
   654
				args.editredirect ~= "" and args.editredirect or nil
tmueller@23
   655
			save = true
tmueller@20
   656
		
tmueller@0
   657
		elseif args.actionup then
tmueller@45
   658
			-- Move section up
tmueller@27
   659
			local t, i = checkpath(sections, sectionpath)
tmueller@0
   660
			if t and i > 1 then
tmueller@0
   661
				local item = table.remove(t, i)
tmueller@0
   662
				table.insert(t, i - 1, item)
tmueller@23
   663
				save = true
tmueller@0
   664
			end
tmueller@20
   665
		
tmueller@0
   666
		elseif args.actiondown then
tmueller@45
   667
			-- Move section down
tmueller@27
   668
			local t, i = checkpath(sections, sectionpath)
tmueller@0
   669
			if t and i < #t then
tmueller@0
   670
				local item = table.remove(t, i)
tmueller@0
   671
				table.insert(t, i + 1, item)
tmueller@23
   672
				save = true
tmueller@0
   673
			end
tmueller@20
   674
		
tmueller@0
   675
		elseif args.actioncreateprofile and args.createprofile then
tmueller@23
   676
			-- Create profile
tmueller@0
   677
			local c = checkprofilename(args.createprofile:lower())
tmueller@0
   678
			if c == profile then
tmueller@47
   679
				useralert = { text = locale.ALERT_CANNOT_COPY_PROFILE_TO_SELF }
tmueller@0
   680
			else
tmueller@0
   681
				local profiles = getprofiles(config.contentdir, lang)
tmueller@0
   682
				if profiles[c] and not args.actionconfirm then
tmueller@20
   683
					useralert = {
tmueller@20
   684
						text = c == pubprofile and 
tmueller@47
   685
							locale.ALERT_OVERWRITE_PUBLISHED_PROFILE or
tmueller@47
   686
							locale.ALERT_OVERWRITE_EXISTING_PROFILE,
tmueller@0
   687
						confirm =
tmueller@20
   688
							'<input type="submit" name="actioncreateprofile" value="' .. 
tmueller@47
   689
							locale.OVERWRITE .. '" /> ' ..
tmueller@20
   690
							hidden("actionconfirm", "true") .. 
tmueller@20
   691
							hidden("createprofile", c)
tmueller@20
   692
					}
tmueller@0
   693
				else
tmueller@0
   694
					if profiles[c] then
tmueller@0
   695
						deletedir(config.contentdir .. "/" .. c .. "_" .. lang)
tmueller@0
   696
					end
tmueller@0
   697
					copyprofile(config.contentdir, lang, profile, c)
tmueller@0
   698
				end
tmueller@0
   699
			end
tmueller@20
   700
		
tmueller@0
   701
		elseif args.actiondeleteprofile and args.deleteprofile then
tmueller@23
   702
			-- Delete profile
tmueller@0
   703
			local c = checkprofilename(args.deleteprofile:lower())
tmueller@47
   704
			assert(c ~= pubprofile, dbmsg("Cannot delete published profile", c))
tmueller@0
   705
			if args.actionconfirm then
tmueller@0
   706
				deletedir(config.contentdir .. "/" .. c .. "_" .. lang)
tmueller@0
   707
				profile = nil
tmueller@0
   708
				args.profile = nil
tmueller@68
   709
				init()
tmueller@23
   710
				save = true
tmueller@0
   711
			else
tmueller@20
   712
				useralert = { 
tmueller@47
   713
					text = locale.ALERT_DELETE_PROFILE,
tmueller@20
   714
					confirm = 
tmueller@20
   715
						'<input type="submit" name="actiondeleteprofile" value="' .. 
tmueller@47
   716
						locale.DELETE .. '" /> ' ..
tmueller@20
   717
						hidden("actionconfirm", "true") ..
tmueller@20
   718
						hidden("deleteprofile", c)
tmueller@20
   719
				}
tmueller@0
   720
			end
tmueller@20
   721
		
tmueller@0
   722
		elseif args.actionchangeprofile and args.changeprofile then
tmueller@23
   723
			-- Change profile
tmueller@0
   724
			local c = checkprofilename(args.changeprofile:lower())
tmueller@0
   725
			profile = c
tmueller@0
   726
			args.profile = c
tmueller@23
   727
			save = true
tmueller@20
   728
		
tmueller@0
   729
		elseif args.actionpublishprofile and args.publishprofile then
tmueller@23
   730
			-- Publish profile
tmueller@0
   731
			local c = checkprofilename(args.publishprofile:lower())
tmueller@0
   732
			if c ~= _publicprofile then
tmueller@0
   733
				if args.actionconfirm then
tmueller@0
   734
					publishprofile(config.contentdir, lang, c)
tmueller@23
   735
					save = true
tmueller@0
   736
				else
tmueller@20
   737
					useralert = {
tmueller@47
   738
						text = locale.ALERT_PUBLISH_PROFILE,
tmueller@20
   739
						confirm =
tmueller@20
   740
							'<input type="submit" name="actionpublishprofile" value="' ..
tmueller@47
   741
							locale.PUBLISH .. '" /> ' ..
tmueller@20
   742
							hidden("actionconfirm", "true") ..
tmueller@20
   743
							hidden("publishprofile", c)
tmueller@20
   744
					}
tmueller@20
   745
				end
tmueller@20
   746
			end
tmueller@0
   747
		end
tmueller@0
   748
		
tmueller@23
   749
		if save then
tmueller@23
   750
			-- Write sections atomically, reload
tmueller@68
   751
			local tempname = sectionfname .. "." .. session.name
tmueller@0
   752
			local f, msg = open(tempname, "wb")
tmueller@20
   753
			assert(f, dbmsg("Error opening section file for writing", msg))
tmueller@27
   754
			tek.dump(sections, function(...)
tmueller@0
   755
				f:write(unpack(arg))
tmueller@0
   756
			end)
tmueller@0
   757
			f:close()
tmueller@0
   758
			local success, msg = rename(tempname, sectionfname)
tmueller@20
   759
			assert(success, dbmsg("Error renaming section file", msg))
tmueller@0
   760
			init()
tmueller@0
   761
		end
tmueller@0
   762
	
tmueller@47
   763
	elseif args.editkey and checkbodyname(args.editkey) then
tmueller@0
   764
		if args.actiondelete then
tmueller@45
   765
			-- Delete section in secondary editable body:
tmueller@77
   766
			deletesection(contentdir, sectionname .. "." .. args.editkey)
tmueller@0
   767
		end
tmueller@0
   768
	end
tmueller@0
   769
end
tmueller@0
   770
tmueller@0
   771
tmueller@80
   772
--	Load/create configuration
tmueller@0
   773
tmueller@0
   774
config = source("../etc/config.lua") or { }
tmueller@80
   775
tmueller@80
   776
config.defname = config.defname or "home"
tmueller@80
   777
config.deflang = config.deflang or "en"
tmueller@80
   778
config.sessionmaxage = config.sessionmaxage or 600
tmueller@80
   779
config.secureport = config.secureport or 443
tmueller@80
   780
tmueller@80
   781
--	Check paths and make them absolute
tmueller@80
   782
tmueller@20
   783
config.passwdfile = posix.abspath(config.passwdfile or "../etc/passwd.lua")
tmueller@0
   784
config.sessiondir = posix.abspath(config.sessiondir or "../var/sessions")
tmueller@0
   785
config.extdir = posix.abspath(config.extdir or "../extensions")
tmueller@20
   786
config.contentdir = posix.abspath(config.contentdir or "../content")
tmueller@20
   787
config.localedir = posix.abspath(config.localedir or "../locale")
tmueller@0
   788
tmueller@80
   789
--	Manage login and establish session
tmueller@0
   790
tmueller@0
   791
session.init(config.sessiondir, args.session, config.sessionmaxage)
tmueller@0
   792
if args.login then
tmueller@0
   793
	if args.login == "false" then
tmueller@0
   794
		session.delete()
tmueller@0
   795
		session = nil
tmueller@0
   796
	elseif args.password then
tmueller@0
   797
		local pwddb = source(config.passwdfile)
tmueller@0
   798
		local pwdentry = pwddb[args.login]
tmueller@0
   799
		if pwdentry and pwdentry.password == args.password then
tmueller@0
   800
			session.data.authuser = pwdentry.username
tmueller@0
   801
			session.data.id = session.id
tmueller@0
   802
		end
tmueller@0
   803
	end
tmueller@0
   804
end
tmueller@0
   805
tmueller@68
   806
secure = request.Port == config.secureport
tmueller@20
   807
authuser = session and session.data.authuser
tmueller@20
   808
tmueller@20
   809
if not authuser then
tmueller@0
   810
	session = nil
tmueller@0
   811
	args.session = nil
tmueller@0
   812
end
tmueller@0
   813
tmueller@23
   814
tmueller@80
   815
--	get lang, locale, profile, section
tmueller@0
   816
tmueller@0
   817
init()
tmueller@0
   818
if authuser then
tmueller@80
   819
	-- handle state modifications
tmueller@0
   820
	handlestate()
tmueller@80
   821
	assert(authuser) -- TODO?
tmueller@0
   822
end
tmueller@0
   823
tmueller@23
   824
tmueller@80
   825
--	add links for creating new sections
tmueller@20
   826
tmueller@0
   827
if authuser then
tmueller@20
   828
	local newent = { name = "new", 
tmueller@47
   829
		label = "[" .. locale.NEW .. "]",
tmueller@20
   830
		action = "actionnew=true" }
tmueller@0
   831
	for _, s in ipairs(submenus) do
tmueller@20
   832
		table.insert(s.entries, newent)
tmueller@0
   833
	end
tmueller@0
   834
	if submenus[#submenus].name then
tmueller@20
   835
		table.insert(submenus, {
tmueller@20
   836
			name = "new", 
tmueller@20
   837
			entries = { [1] = newent }
tmueller@20
   838
		})
tmueller@0
   839
	end
tmueller@0
   840
end
tmueller@0
   841
tmueller@23
   842
tmueller@80
   843
--	current document
tmueller@80
   844
tmueller@80
   845
docname = cgi.document.Name
tmueller@80
   846
assert(sectionpath) -- TODO?
tmueller@80
   847
document = docname .. "/" .. sectionpath
tmueller@80
   848
tmueller@80
   849
tmueller@0
   850
--	create section function
tmueller@0
   851
tmueller@0
   852
local func, msg = loadhtml(open("loona/editable.lua"),
tmueller@0
   853
	"tek.web.out", "loona/editable.lua")
tmueller@0
   854
tmueller@20
   855
assert(func, dbmsg("Syntax error", msg))
tmueller@0
   856
tmueller@0
   857
local editable = func()
tmueller@0
   858
tmueller@0
   859
function body(name)
tmueller@47
   860
	name = checkbodyname(name)
tmueller@0
   861
	if name == "main" then
tmueller@80
   862
		dosnippet(config, editable("main", contentdir, sectionname, sectionname))
tmueller@0
   863
	else
tmueller@0
   864
		local ext = "." .. name
tmueller@0
   865
		dosnippet(config, editable(name, contentdir,
tmueller@77
   866
			getsectionfile(submenus, contentdir, ext), sectionname .. ext))
tmueller@0
   867
	end
tmueller@0
   868
end
tmueller@0
   869
tmueller@23
   870
tmueller@0
   871
--	write back session state
tmueller@0
   872
tmueller@0
   873
if session then
tmueller@0
   874
	session.save()
tmueller@0
   875
end