Merge pull request #3789 from weblate/weblate-openwrt-luci
[project/luci.git] / applications / luci-app-adblock / luasrc / model / cbi / adblock / configuration_tab.lua
1 -- Copyright 2017-2018 Dirk Brenken (dev@brenken.org)
2 -- This is free software, licensed under the Apache License, Version 2.0
3
4 local fs = require("nixio.fs")
5 local util = require("luci.util")
6 local input = "/etc/config/adblock"
7
8 if not fs.access(input) then
9 m = SimpleForm("error", nil, translate("Input file not found, please check your configuration."))
10 m.reset = false
11 m.submit = false
12 return m
13 end
14
15 if fs.stat(input).size >= 102400 then
16 m = SimpleForm("error", nil,
17 translate("The file size is too large for online editing in LuCI (≥ 100 KB). ")
18 .. translate("Please edit this file directly in a terminal session."))
19 m.reset = false
20 m.submit = false
21 return m
22 end
23
24 m = SimpleForm("input", nil)
25 m:append(Template("adblock/adblock_css"))
26 m.submit = translate("Save")
27 m.reset = false
28
29 s = m:section(SimpleSection, nil,
30 translate("This form allows you to modify the content of the main adblock configuration file (/etc/config/adblock)."))
31
32 f = s:option(TextValue, "data")
33 f.rows = 20
34 f.rmempty = true
35
36 function f.cfgvalue()
37 return fs.readfile(input) or ""
38 end
39
40 function f.write(self, section, data)
41 return fs.writefile(input, "\n" .. util.trim(data:gsub("\r\n", "\n")) .. "\n")
42 end
43
44 function f.remove(self, section, value)
45 return fs.writefile(input, "")
46 end
47
48 function s.handle(self, state, data)
49 return true
50 end
51
52 return m