template.lua 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. require("fwutils.webapi")
  2. -- CREATE TABLE `fw_template` (
  3. -- `id` int NOT NULL AUTO_INCREMENT,
  4. -- `role_id` int DEFAULT NULL,
  5. -- `key` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  6. -- `value` varchar(2048) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  7. -- `enable` tinyint NOT NULL COMMENT '是否启用',
  8. -- PRIMARY KEY (`id`),
  9. -- UNIQUE KEY `role_id` (`role_id`,`key`)
  10. -- ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='Fastweb-关键词模板';
  11. local M = {
  12. cfg = nil,
  13. template_engine_bc_this_role = nil
  14. }
  15. -- 允许的扩展名
  16. local allowed_extensions = {
  17. "shtml",
  18. "html",
  19. "js"
  20. }
  21. function M.get_by_id(id,conn)
  22. local select = conn:select()
  23. select:table("fw_template LEFT JOIN fw_role ON fw_template.role_id = fw_role.id")
  24. select:field({
  25. "fw_template.id",
  26. "fw_template.role_id",
  27. "fw_template.`key`",
  28. "fw_template.value",
  29. "fw_template.enable",
  30. "fw_role.title as role_title",
  31. })
  32. select:where_i32("fw_template.id","=",id)
  33. select:limit(0,1)
  34. local result = select:query()
  35. if result:row_count() == 0 then
  36. return nil
  37. end
  38. local d = result:table()[1]
  39. return d
  40. end
  41. function M.add(data,conn)
  42. local insert = conn:insert()
  43. insert:table("fw_template")
  44. if data.role_id ~= nil and data.role_id ~= cjson.null then
  45. insert:set_i32("role_id",data.role_id)
  46. end
  47. if data.key ~= nil then
  48. insert:set_str("`key`",data.key)
  49. end
  50. if data.value ~= nil then
  51. insert:set_str("value",data.value)
  52. end
  53. if data.enable ~= nil then
  54. insert:set_i32("enable",data.enable)
  55. end
  56. local d = insert:exec()
  57. return d == 1
  58. end
  59. function M.update(data,conn)
  60. local update = conn:update()
  61. update:table("fw_template")
  62. if data.role_id ~= nil then
  63. update:set_i32("role_id",data.role_id)
  64. end
  65. if data.key ~= nil then
  66. update:set_str("`key`",data.key)
  67. end
  68. if data.value ~= nil then
  69. update:set_str("value",data.value)
  70. end
  71. if data.enable ~= nil then
  72. update:set_i32("enable",data.enable)
  73. end
  74. update:where_i32("id","=",data.id)
  75. local d = update:exec()
  76. return d == 1
  77. end
  78. function M.delete(id,conn)
  79. local del = conn:delete()
  80. del:table("fw_template")
  81. del:where_i32("id","=",id)
  82. local d = del:exec()
  83. return d == 1
  84. end
  85. function M.list(search,limit,conn)
  86. return query_model_ex(conn,[=[
  87. fw_template LEFT JOIN fw_role ON fw_template.role_id = fw_role.id
  88. ]=],{
  89. "fw_template.id",
  90. "fw_template.role_id",
  91. "fw_template.`key`",
  92. "fw_template.value",
  93. "fw_template.enable",
  94. "fw_role.title as role_title",
  95. "fw_role.id as role_id",
  96. },limit.start,limit.length,function(sel)
  97. if search.role_id ~= nil and search.role_id ~= -1 then
  98. sel:where_i32("fw_template.role_id","=",search.role_id)
  99. end
  100. end,function(sel_data)
  101. sel_data:orderby("fw_template.enable DESC")
  102. end)
  103. end
  104. -- 更新
  105. M.make_bytecode = function(conn)
  106. -- 查询权限表
  107. local select = conn:select()
  108. select:table("fw_template")
  109. select:where_i32("enable","=",1)
  110. local result = select:query()
  111. local bc = {
  112. public = {}
  113. }
  114. while result:next() do
  115. local id = result:get("id")
  116. local role_id = string.format("%d",result:get("role_id"))
  117. local key = result:get("key")
  118. local value = result:get("value")
  119. if bc[role_id] == nil then
  120. bc[role_id] = {}
  121. end
  122. if role_id == "0" then
  123. bc["public"][key] = value
  124. else
  125. bc[role_id][key] = value
  126. end
  127. end
  128. local code = "return " .. require("serpent").serialize(bc, {comment = false})
  129. utils.save_file(fw.website_dir().."/"..(fwutils_config.path.luabytecode:gsub("%.", "/")).."/template_engine_bc.lua",code)
  130. return true
  131. end
  132. -- 处理
  133. -- @param static_content 静态内容
  134. -- @param cfg 配置
  135. -- @return 是否替换(TRUE则不需要继续处理,FALSE则继续处理)
  136. M.handle = function(__cfg,functions)
  137. local function has_ext(ext)
  138. for _, v in ipairs(allowed_extensions) do
  139. if v == ext then
  140. return true
  141. end
  142. end
  143. return false
  144. end
  145. M.cfg = __cfg
  146. M.template_engine_bc_this_role = {
  147. template = {
  148. private = {},
  149. public = {}
  150. },
  151. }
  152. local ext = utils.ext(M.cfg.filepath())
  153. local last_modified = fw.file_last_modified_time(fw.website_dir()..M.cfg.filepath())
  154. local static_content = nil
  155. if ext ~= nil then
  156. if not has_ext(ext) then
  157. return false
  158. end
  159. -- 读取资源文件
  160. static_content = utils.read_file(fw.website_dir()..M.cfg.filepath())
  161. if static_content == nil or static_content == "" then
  162. return false
  163. end
  164. else
  165. -- 无需替换的扩展名
  166. return false
  167. end
  168. -- 验证缓存
  169. local if_modified_since = request.header("If-Modified-Since")
  170. if if_modified_since ~= nil and if_modified_since ~= "" then
  171. if if_modified_since == last_modified then
  172. response.sendex("",304,"Not Modified")
  173. return true
  174. end
  175. end
  176. local template_engine_bc = require(fwutils_config.path.luabytecode..".template_engine_bc")
  177. M.template_engine_bc_this_role["template"]["private"] = template_engine_bc[string.format("%d",M.cfg.role_id())]
  178. M.template_engine_bc_this_role["template"]["public"] = template_engine_bc["public"]
  179. local replaced,content = M.replace(static_content)
  180. if replaced then
  181. static_content = content
  182. end
  183. -- 执行函数
  184. static_content, n = static_content:gsub("%${<<<%s*(.-)%s*>>>}", function(code)
  185. -- 尝试编译代码(Lua 5.2+ 使用 load;Lua 5.1 可用 loadstring)
  186. local env = {}
  187. _G["engine"] = M
  188. local env = require("fwutils.funs").regist(env)
  189. if functions ~= nil then
  190. env = functions.regist(env)
  191. end
  192. env["_G"] = _G
  193. local chunk, errmsg = load(code,nil,nil,env)
  194. if not chunk then
  195. fw.throw_string(errmsg)
  196. end
  197. -- 使用 pcall 安全执行代码块
  198. local status, result = pcall(chunk)
  199. if not status then
  200. fw.throw_string(result)
  201. end
  202. -- 如果代码没有返回值,则替换为空字符串,否则转换成字符串返回
  203. return tostring(result)
  204. end)
  205. if n > 0 then
  206. replaced = true
  207. end
  208. if replaced then
  209. if ext == "shtml" or ext == "html" then
  210. response.header("Content-Type","text/html")
  211. elseif ext == "js" then
  212. response.header("Content-Type","application/javascript")
  213. end
  214. -- print("Last-Modified:", last_modified)
  215. response.header("Last-Modified", last_modified)
  216. response.header("Cache-Control", "no-cache")
  217. response.send(static_content)
  218. return true
  219. end
  220. return false
  221. end
  222. M.replace = function (content)
  223. if content == nil or content == "" then
  224. return false
  225. end
  226. -- 支持多级kvs替换,如 ${people.age}
  227. local function flatten_kvs(tbl, prefix, out)
  228. if tbl == nil then
  229. return {}
  230. end
  231. out = out or {}
  232. prefix = prefix or ""
  233. for k, v in pairs(tbl) do
  234. local key = prefix ~= "" and (prefix .. "." .. k) or k
  235. if type(v) == "table" then
  236. flatten_kvs(v, key, out)
  237. elseif type(v) == "string" then
  238. out[key] = v
  239. elseif type(v) == "number" then
  240. if math.type and math.type(v) == "integer" then
  241. out[key] = string.format("%d", v)
  242. elseif math.floor(v) == v then
  243. out[key] = string.format("%d", v)
  244. else
  245. out[key] = tostring(v)
  246. end
  247. else
  248. out[key] = tostring(v)
  249. end
  250. end
  251. return out
  252. end
  253. -- 先提取 content 中所有需要替换的占位符
  254. -- 需要正确处理 ${<<<...>>>} 块:块内部的 ${...} 需要替换,但块本身不替换
  255. local placeholders = {}
  256. -- 第一步:提取所有 ${<<<...>>>} 块,临时替换它们,并收集所有占位符
  257. local blocks = {}
  258. local block_index = 0
  259. local temp_content = content:gsub("%${<<<%s*(.-)%s*>>>}", function(block_content)
  260. block_index = block_index + 1
  261. local placeholder = "${__TEMP_BLOCK_" .. block_index .. "__}"
  262. -- 收集块内部的 ${...} 占位符
  263. for ph in string.gmatch(block_content, "%${([^}]+)}") do
  264. placeholders[ph] = true
  265. end
  266. blocks[block_index] = {
  267. placeholder = placeholder,
  268. content = block_content
  269. }
  270. return placeholder
  271. end)
  272. -- 第二步:提取外部(不在 ${<<<...>>>} 块中)的 ${...} 占位符
  273. for placeholder in string.gmatch(temp_content, "%${([^}]+)}") do
  274. -- 忽略临时占位符
  275. if not placeholder:match("^__TEMP_BLOCK_%d+__$") then
  276. placeholders[placeholder] = true
  277. end
  278. end
  279. -- print("PLACEHOLDERS:",cjson.encode(placeholders))
  280. -- 如果没有任何占位符,直接返回
  281. if next(placeholders) == nil then
  282. return false
  283. end
  284. -- 合并所有数据源到一个查找表中(只 flatten 一次)
  285. local value_map = {}
  286. -- PUBLIC
  287. local flat_kvs = flatten_kvs(M.template_engine_bc_this_role["template"]["public"])
  288. for k, v in pairs(flat_kvs) do
  289. value_map[k] = v
  290. end
  291. -- PRIVATE
  292. flat_kvs = flatten_kvs(M.template_engine_bc_this_role["template"]["private"])
  293. for k, v in pairs(flat_kvs) do
  294. value_map[k] = v
  295. end
  296. -- REQUEST
  297. flat_kvs = flatten_kvs(request.gets(), "request")
  298. for k, v in pairs(flat_kvs) do
  299. value_map[k] = v
  300. end
  301. -- TOKEN
  302. flat_kvs = flatten_kvs(M.cfg.user_data(), "token")
  303. for k, v in pairs(flat_kvs) do
  304. value_map[k] = v
  305. end
  306. -- 转义函数:将 Lua 模式特殊字符转义为字面匹配
  307. local function escape_pattern(str)
  308. return str:gsub("([%^%$%(%)%%%.%[%]%*%+%-%?])", "%%%1")
  309. end
  310. -- 第三步:先处理 ${<<<...>>>} 块内部的占位符
  311. local replaced = false
  312. for i, block in ipairs(blocks) do
  313. local block_content = block.content
  314. for placeholder, _ in pairs(placeholders) do
  315. if value_map[placeholder] ~= nil then
  316. local escaped_placeholder = escape_pattern(placeholder)
  317. local new_content, count = string.gsub(block_content, "%${" .. escaped_placeholder .. "}", tostring(value_map[placeholder]))
  318. if count > 0 then
  319. block_content = new_content
  320. replaced = true
  321. end
  322. end
  323. end
  324. blocks[i].processed_content = block_content
  325. end
  326. -- 第四步:替换外部的 ${...} 占位符(在临时内容中,此时块已被替换为临时占位符)
  327. local n = 0
  328. for placeholder, _ in pairs(placeholders) do
  329. if value_map[placeholder] ~= nil then
  330. local escaped_placeholder = escape_pattern(placeholder)
  331. temp_content, n = string.gsub(temp_content, "%${" .. escaped_placeholder .. "}", tostring(value_map[placeholder]))
  332. if n > 0 then
  333. replaced = true
  334. end
  335. end
  336. end
  337. -- 第五步:恢复 ${<<<...>>>} 块(使用处理后的内容)
  338. for i, block in ipairs(blocks) do
  339. local processed_block = "${<<<" .. blocks[i].processed_content .. ">>>}"
  340. temp_content = temp_content:gsub(block.placeholder:gsub("([%^%$%(%)%%%.%[%]%*%+%-%?])", "%%%1"), processed_block)
  341. end
  342. content = temp_content
  343. if replaced then
  344. return true, content
  345. end
  346. return false
  347. end
  348. -- 检查内容中是否有TOKEN变量
  349. -- @return boolean
  350. M.hasToken = function()
  351. if M.static_content == nil or M.static_content == "" then
  352. return false
  353. end
  354. return M.static_content:match("%${token%.") ~= nil
  355. end
  356. return M