6878275d78b0512e46ce19c3ec9363b1696237e3
[project/luci.git] / applications / luci-app-openvpn / luasrc / model / cbi / openvpn-file.lua
1 -- Licensed to the public under the Apache License 2.0.
2
3 local ip = require("luci.ip")
4 local fs = require("nixio.fs")
5 local util = require("luci.util")
6 local uci = require("luci.model.uci").cursor()
7 local cfg_file = uci:get("openvpn", arg[1], "config")
8
9 local m = Map("openvpn")
10
11 local p = m:section( SimpleSection )
12 p.template = "openvpn/pageswitch"
13 p.mode = "file"
14 p.instance = arg[1]
15
16 if not cfg_file or not fs.access(cfg_file) then
17 local f = SimpleForm("error", nil, translatef("The OVPN config file (%s) could not be found, please check your configuration.", cfg_file or "n/a"))
18 f:append(Template("openvpn/ovpn_css"))
19 f.reset = false
20 f.submit = false
21 return m, f
22 end
23
24 if fs.stat(cfg_file).size >= 102400 then
25 f = SimpleForm("error", nil,
26 translatef("The size of the OVPN config file (%s) is too large for online editing in LuCI (≥ 100 KB). ", cfg_file)
27 .. translate("Please edit this file directly in a terminal session."))
28 f:append(Template("openvpn/ovpn_css"))
29 f.reset = false
30 f.submit = false
31 return m, f
32 end
33
34 f = SimpleForm("cfg", nil)
35 f:append(Template("openvpn/ovpn_css"))
36 f.submit = translate("Save")
37 f.reset = false
38
39 s = f:section(SimpleSection, nil, translatef("This form allows you to modify the content of the OVPN config file (%s). ", cfg_file))
40 file = s:option(TextValue, "data")
41 file.datatype = "string"
42 file.rows = 20
43 file.rmempty = true
44
45 function file.cfgvalue()
46 return fs.readfile(cfg_file) or ""
47 end
48
49 function file.write(self, section, data)
50 return fs.writefile(cfg_file, "\n" .. util.trim(data:gsub("\r\n", "\n")) .. "\n")
51 end
52
53 function file.remove(self, section, value)
54 return fs.writefile(cfg_file, "")
55 end
56
57 function s.handle(self, state, data)
58 return true
59 end
60
61 return m, f