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