-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglobals.lua
67 lines (59 loc) · 1.35 KB
/
globals.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
hex = function (str)
return string.gsub(str,".",function (c)
return string.format("%02x", string.byte(c))
end)
end
hash_pass = function (pass)
local digest = require 'openssl.digest'
local sha256 = digest.new('sha256')
return hex(sha256:final(pass))
end
shallow_clone = function (t)
local nt = {}
for k,v in pairs(t) do nt[k] = v end
return nt
end
view = function (name,p)
local txt = templates.index({
title = config.title,
session = p.session,
content = templates[name](p)
})
sessions[p.session_id].errors = nil
sessions[p.session_id].messages = nil
return {
txt = txt
}
end
redirect = function (path,s)
return {
status = s or 302,
redirect = path
}
end
dd = function (data)
return {
ctype = 'text/plain',
txt = serpent.block(data,{comment=false})
}
end
csrf_token = function (sid)
if sessions[sid].csrf == nil then sessions[sid].csrf = uuid() end
return string.format('<input type="hidden" name="csrf_token" value="%s">', sessions[sid].csrf)
end
gen_ct = function (path)
local s = string.match(path,"(%.%a+)$")
if s == nil or lt[s] == nil then return 'text/plain' end
return lt[s]
end
res_to_table = function (r)
local t = {}
for i=1, r:ntuples() do
local nt = {}
for j=1, r:nfields() do
nt[r:fname(j)] = r:getvalue(i, j)
end
table.insert(t,nt)
end
return t
end