2 if _lib_inc_init ~= nil then return end
6 dofile("lang_" .. CONF_LANG .. ".inc");
8 err_msg = nil -- show err_msg
9 -- archss_header output
10 -- title: outout the web title
11 -- fun_cb: callback function for private data
12 function as_head(fun_cb, title)
13 assert(type(title) == "string");
14 assert(type(fun_cb) == "function");
16 print('Content-type: text/html\n\n' ..
17 '<HTML><HEAD><TITLE>' .. title .. '</TITLE>\n' ..
18 '<META HTTP-EQUIV="Content-Type" CONTENT="text/html; CHARSET="UTF-8"/>\n' ..
19 '<LINK REL=STYLESHEET HREF="styles.css" TYPE="TEXT/CSS">');
24 -- onload: web init with javascript
25 function as_body(fun_cb, onload)
26 assert(type(onload) == "string");
27 assert(type(fun_cb) == "function");
29 print('<BODY ONLOAD="' .. onload .. '">');
31 print('</BODY></HTML>');
35 -- target: post this form to which cgi
36 -- datatype: used for binary or normal data
37 function as_form(fun_cb, target, datatype)
38 assert(type(fun_cb) == "function")
39 assert(type(target) == "string")
42 if datatype ~= nil then dt = datatype end;
43 print('<form action="' .. target .. CONF_EXT .. '" method="post"' .. dt .. '>');
48 function as_table(fun_cb, width, class)
49 assert(type(fun_cb) == "function")
50 assert(type(width) == "string")
52 print('<table width="' .. width .. '" style="' .. class .. '">');
57 -- parse STDIN and query_string
58 local _as_stdin_init; -- parsed flag
59 local _as_query_data = {}; -- query data store
60 --local _as_kv_token = "(%w+)=([%w_-%%]+)"
61 local _as_kv_token = "([^&=]+)=([^&=]+)"
62 -- FIXME this MUST work with multi-byte encoded data
64 if _as_stdin_init ~= nil then return _as_query_data end
65 local query_string = os.getenv("QUERY_STRING");
68 if query_string == nil then return _as_query_data end
69 for k, v in string.gfind(query_string, _as_kv_token) do
70 _as_query_data[unescape(k)] = unescape(v);
73 return _as_query_data;
77 local _as_post_data = {}; -- post data store
79 if _as_post_init ~= nil then return _as_post_data end
80 local read = io.read();
83 if read == nil then return _as_post_data end
84 for k, v in string.gfind(read, _as_kv_token) do
85 _as_post_data[unescape(k)] = unescape(v);
91 --[[ all key/value in one
94 return QUERY() .. POST();
98 function show_errmsg()
99 if err_msg ~= nil then print('<div class="err_msg">'..err_msg..'</div>') end
102 -- database deal with
105 require "luasql.mysql";
106 env = assert (luasql.mysql())
107 con = assert (env:connect(CONF_DB.DBN, CONF_DB.USR, CONF_DB.PWD, CONF_DB.SRV), "mysql connect bad")
110 function rows(connection, sql_statement)
111 local cursor = assert(connection:execute(sql_statement))
113 return cursor:fetch()
118 function unescape (s)
119 if s == nil then return s end
120 s = string.gsub(s, "+", " ")
121 s = string.gsub(s, "%%(%x%x)", function (h)
122 return string.char(tonumber(h, 16))
128 s = string.gsub(s, "([&=+%c])", function (c)
129 return string.format("%%%02X", string.byte(c))
131 s = string.gsub(s, " ", "+")
138 for k,v in pairs(t) do
139 s = s .. "&" .. escape(k) .. "=" .. escape(v)
141 return string.sub(s, 2) -- remove first `&'
145 -- private msg store with "XXX_ARCHSS", we need config it when install