template.lua 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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 static_content = nil
  154. if ext ~= nil then
  155. if not has_ext(ext) then
  156. return false
  157. end
  158. -- 读取资源文件
  159. static_content = utils.read_file(fw.website_dir()..M.cfg.filepath())
  160. if static_content == nil or static_content == "" then
  161. return false
  162. end
  163. else
  164. -- 无需替换的扩展名
  165. return false
  166. end
  167. local template_engine_bc = require(fwutils_config.path.luabytecode..".template_engine_bc")
  168. M.template_engine_bc_this_role["template"]["private"] = template_engine_bc[string.format("%d",M.cfg.role_id())]
  169. M.template_engine_bc_this_role["template"]["public"] = template_engine_bc["public"]
  170. local replaced,content = M.replace(static_content)
  171. if replaced then
  172. static_content = content
  173. end
  174. -- 执行函数
  175. static_content, n = static_content:gsub("%${<<<%s*(.-)%s*>>>}", function(code)
  176. -- 尝试编译代码(Lua 5.2+ 使用 load;Lua 5.1 可用 loadstring)
  177. local env = {}
  178. _G["engine"] = M
  179. local env = require("fwutils.funs").regist(env)
  180. if functions ~= nil then
  181. env = functions.regist(env)
  182. end
  183. env["_G"] = _G
  184. local chunk, errmsg = load(code,nil,nil,env)
  185. if not chunk then
  186. fw.throw_string(errmsg)
  187. end
  188. -- 使用 pcall 安全执行代码块
  189. local status, result = pcall(chunk)
  190. if not status then
  191. fw.throw_string(result)
  192. end
  193. -- 如果代码没有返回值,则替换为空字符串,否则转换成字符串返回
  194. return tostring(result)
  195. end)
  196. if n > 0 then
  197. replaced = true
  198. end
  199. if replaced then
  200. if ext == "shtml" or ext == "html" then
  201. response.header("Content-Type","text/html")
  202. elseif ext == "js" then
  203. response.header("Content-Type","application/javascript")
  204. end
  205. response.send(static_content)
  206. return true
  207. end
  208. return false
  209. end
  210. M.replace = function (content)
  211. if content == nil or content == "" then
  212. return false
  213. end
  214. -- 支持多级kvs替换,如 ${people.age}
  215. local function flatten_kvs(tbl, prefix, out)
  216. if tbl == nil then
  217. return {}
  218. end
  219. out = out or {}
  220. prefix = prefix or ""
  221. for k, v in pairs(tbl) do
  222. local key = prefix ~= "" and (prefix .. "." .. k) or k
  223. if type(v) == "table" then
  224. flatten_kvs(v, key, out)
  225. elseif type(v) == "string" then
  226. out[key] = v
  227. elseif type(v) == "number" then
  228. if math.type and math.type(v) == "integer" then
  229. out[key] = string.format("%d", v)
  230. elseif math.floor(v) == v then
  231. out[key] = string.format("%d", v)
  232. else
  233. out[key] = tostring(v)
  234. end
  235. else
  236. out[key] = tostring(v)
  237. end
  238. end
  239. return out
  240. end
  241. -- 先提取 content 中所有需要替换的占位符
  242. -- 需要正确处理 ${<<<...>>>} 块:块内部的 ${...} 需要替换,但块本身不替换
  243. local placeholders = {}
  244. -- 第一步:提取所有 ${<<<...>>>} 块,临时替换它们,并收集所有占位符
  245. local blocks = {}
  246. local block_index = 0
  247. local temp_content = content:gsub("%${<<<%s*(.-)%s*>>>}", function(block_content)
  248. block_index = block_index + 1
  249. local placeholder = "${__TEMP_BLOCK_" .. block_index .. "__}"
  250. -- 收集块内部的 ${...} 占位符
  251. for ph in string.gmatch(block_content, "%${([^}]+)}") do
  252. placeholders[ph] = true
  253. end
  254. blocks[block_index] = {
  255. placeholder = placeholder,
  256. content = block_content
  257. }
  258. return placeholder
  259. end)
  260. -- 第二步:提取外部(不在 ${<<<...>>>} 块中)的 ${...} 占位符
  261. for placeholder in string.gmatch(temp_content, "%${([^}]+)}") do
  262. -- 忽略临时占位符
  263. if not placeholder:match("^__TEMP_BLOCK_%d+__$") then
  264. placeholders[placeholder] = true
  265. end
  266. end
  267. -- print("PLACEHOLDERS:",cjson.encode(placeholders))
  268. -- 如果没有任何占位符,直接返回
  269. if next(placeholders) == nil then
  270. return false
  271. end
  272. -- 合并所有数据源到一个查找表中(只 flatten 一次)
  273. local value_map = {}
  274. -- PUBLIC
  275. local flat_kvs = flatten_kvs(M.template_engine_bc_this_role["template"]["public"])
  276. for k, v in pairs(flat_kvs) do
  277. value_map[k] = v
  278. end
  279. -- PRIVATE
  280. flat_kvs = flatten_kvs(M.template_engine_bc_this_role["template"]["private"])
  281. for k, v in pairs(flat_kvs) do
  282. value_map[k] = v
  283. end
  284. -- REQUEST
  285. flat_kvs = flatten_kvs(request.gets(), "request")
  286. for k, v in pairs(flat_kvs) do
  287. value_map[k] = v
  288. end
  289. -- TOKEN
  290. flat_kvs = flatten_kvs(M.cfg.user_data(), "token")
  291. for k, v in pairs(flat_kvs) do
  292. value_map[k] = v
  293. end
  294. -- 转义函数:将 Lua 模式特殊字符转义为字面匹配
  295. local function escape_pattern(str)
  296. return str:gsub("([%^%$%(%)%%%.%[%]%*%+%-%?])", "%%%1")
  297. end
  298. -- 第三步:先处理 ${<<<...>>>} 块内部的占位符
  299. local replaced = false
  300. for i, block in ipairs(blocks) do
  301. local block_content = block.content
  302. for placeholder, _ in pairs(placeholders) do
  303. if value_map[placeholder] ~= nil then
  304. local escaped_placeholder = escape_pattern(placeholder)
  305. local new_content, count = string.gsub(block_content, "%${" .. escaped_placeholder .. "}", tostring(value_map[placeholder]))
  306. if count > 0 then
  307. block_content = new_content
  308. replaced = true
  309. end
  310. end
  311. end
  312. blocks[i].processed_content = block_content
  313. end
  314. -- 第四步:替换外部的 ${...} 占位符(在临时内容中,此时块已被替换为临时占位符)
  315. local n = 0
  316. for placeholder, _ in pairs(placeholders) do
  317. if value_map[placeholder] ~= nil then
  318. local escaped_placeholder = escape_pattern(placeholder)
  319. temp_content, n = string.gsub(temp_content, "%${" .. escaped_placeholder .. "}", tostring(value_map[placeholder]))
  320. if n > 0 then
  321. replaced = true
  322. end
  323. end
  324. end
  325. -- 第五步:恢复 ${<<<...>>>} 块(使用处理后的内容)
  326. for i, block in ipairs(blocks) do
  327. local processed_block = "${<<<" .. blocks[i].processed_content .. ">>>}"
  328. temp_content = temp_content:gsub(block.placeholder:gsub("([%^%$%(%)%%%.%[%]%*%+%-%?])", "%%%1"), processed_block)
  329. end
  330. content = temp_content
  331. if replaced then
  332. return true, content
  333. end
  334. return false
  335. end
  336. -- 检查内容中是否有TOKEN变量
  337. -- @return boolean
  338. M.hasToken = function()
  339. if M.static_content == nil or M.static_content == "" then
  340. return false
  341. end
  342. return M.static_content:match("%${token%.") ~= nil
  343. end
  344. return M