Merge pull request #325 from chris5560/master-app-ddns
[project/luci.git] / applications / luci-app-ddns / luasrc / model / cbi / ddns / global.lua
1 -- Copyright 2014 Christian Schoenebeck <christian dot schoenebeck at gmail dot com>
2 -- Licensed to the public under the Apache License 2.0.
3
4 local NX   = require "nixio"
5 local NXFS = require "nixio.fs"
6 local DISP = require "luci.dispatcher"
7 local SYS  = require "luci.sys"
8 local DDNS = require "luci.tools.ddns"          -- ddns multiused functions
9
10 -- Bootstrap theme needs 2 or 3 additional linefeeds for tab description for better optic
11 local LFLF = (DDNS.get_theme() == "Bootstrap") and [[<br /><br />]] or [[]]
12
13 -- cbi-map definition -- #######################################################
14 local m = Map("ddns")
15
16 -- first need to close <a> from cbi map template our <a> closed by template
17 m.title = [[</a><a href="]] .. DISP.build_url("admin", "services", "ddns") .. [[">]] 
18         .. translate("Dynamic DNS")
19
20 m.description = translate("Dynamic DNS allows that your router can be reached with " ..
21                         "a fixed hostname while having a dynamically changing IP address.")
22
23 m.redirect = DISP.build_url("admin", "services", "ddns")
24
25 function m.commit_handler(self)
26         if self.changed then    -- changes ?
27                 os.execute("/etc/init.d/ddns reload &") -- reload configuration
28         end
29 end
30
31 -- cbi-section definition -- ###################################################
32 local ns = m:section( NamedSection, "global", "ddns",
33         translate("Global Settings"),
34         translate("Configure here the details for all Dynamic DNS services including this LuCI application.") 
35         .. [[<br /><strong>]]
36         .. translate("It is NOT recommended for casual users to change settings on this page.")
37         .. [[</strong><br />]]
38         .. [[<a href="http://wiki.openwrt.org/doc/uci/ddns#version_2x1" target="_blank">]]
39         .. translate("For detailed information about parameter settings look here.")
40         .. [[</a>]]
41         .. LFLF )
42 -- section might not exist
43 function ns.cfgvalue(self, section)
44         if not self.map:get(section) then
45                 self.map:set(section, nil, self.sectiontype)
46         end
47         return self.map:get(section)
48 end
49
50 -- allow_local_ip  -- ##########################################################
51 local ali       = ns:option(Flag, "allow_local_ip")
52 ali.title       = translate("Allow non-public IP's")
53 ali.description = translate("Non-public and by default blocked IP's") .. ":"
54                 .. [[<br /><strong>IPv4: </strong>]]
55                 .. "0/8, 10/8, 100.64/10, 127/8, 169.254/16, 172.16/12, 192.168/16"
56                 .. [[<br /><strong>IPv6: </strong>]]
57                 .. "::/32, f000::/4"
58 ali.reempty     = true
59 ali.default     = "0"
60 function ali.parse(self, section)
61         DDNS.flag_parse(self, section)
62 end
63 function ali.validate(self, value)
64         if value == self.default then
65                 return "" -- default = empty
66         end
67         return value
68 end
69
70 -- date_format  -- #############################################################
71 local df        = ns:option(Value, "date_format")
72 df.title        = translate("Date format")
73 df.description  = [[<a href="http://www.cplusplus.com/reference/ctime/strftime/" target="_blank">]]
74                 .. translate("For supported codes look here") 
75                 .. [[</a>]]
76 df.template     = "ddns/global_value"
77 df.rmempty      = true
78 df.default      = "%F %R"
79 df.date_string  = ""
80 function df.cfgvalue(self, section)
81         local value = AbstractValue.cfgvalue(self, section) or self.default
82         local epoch = os.time()
83         self.date_string = DDNS.epoch2date(epoch, value)
84         return value
85 end
86 function df.validate(self, value)
87         if value == self.default then
88                 return "" -- default = empty
89         end
90         return value
91 end
92
93 -- run_dir  -- #################################################################
94 local rd        = ns:option(Value, "run_dir")
95 rd.title        = translate("Status directory")
96 rd.description  = translate("Directory contains PID and other status information for each running section")
97 rd.rmempty      = true
98 rd.default      = "/var/run/ddns"
99 function rd.validate(self, value)
100         if value == self.default then
101                 return "" -- default = empty
102         end
103         return value
104 end
105
106 -- log_dir  -- #################################################################
107 local ld        = ns:option(Value, "log_dir")
108 ld.title        = translate("Log directory")
109 ld.description  = translate("Directory contains Log files for each running section")
110 ld.rmempty      = true
111 ld.default      = "/var/log/ddns"
112 function ld.validate(self, value)
113         if value == self.default then
114                 return "" -- default = empty
115         end
116         return value
117 end
118
119 -- log_lines  -- ###############################################################
120 local ll        = ns:option(Value, "log_lines")
121 ll.title        = translate("Log length")
122 ll.description  = translate("Number of last lines stored in log files")
123 ll.rmempty      = true
124 ll.default      = "250"
125 ll.datatype     = "and(uinteger,min(1))"
126 function ll.validate(self, value)
127         local n = tonumber(value)
128         if not n or math.floor(n) ~= n or n < 1 then
129                 return nil, self.title .. ": " .. translate("minimum value '1'")
130         end
131         if value == self.default then
132                 return "" -- default = empty
133         end
134         return value
135 end
136
137 -- use_curl  -- ################################################################
138 if (SYS.call([[ grep -i "\+ssl" /usr/bin/wget >/dev/null 2>&1 ]]) == 0) 
139 and NXFS.access("/usr/bin/curl") then
140         local pc        = ns:option(Flag, "use_curl")
141         pc.title        = translate("Use cURL")
142         pc.description  = translate("If both cURL and GNU Wget are installed, Wget is used by default.")
143                 .. [[<br />]]
144                 .. translate("To use cURL activate this option.")
145         pc.orientation  = "horizontal"
146         pc.rmempty      = true
147         pc.default      = "0"
148         function pc.parse(self, section)
149                 DDNS.flag_parse(self, section)
150         end
151         function pc.validate(self, value)
152                 if value == self.default then
153                         return "" -- default = empty
154                 end
155                 return value
156         end
157 end
158
159 return m