Globally reduce copyright headers
[project/luci.git] / applications / luci-app-ddns / luasrc / model / cbi / ddns / overview.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 NXFS = require "nixio.fs"
5 local CTRL = require "luci.controller.ddns"     -- this application's controller
6 local DISP = require "luci.dispatcher"
7 local HTTP = require "luci.http"
8 local SYS  = require "luci.sys"
9 local DDNS = require "luci.tools.ddns"          -- ddns multiused functions
10
11 -- show hints ?
12 show_hints = not (DDNS.check_ipv6()             -- IPv6 support
13                 and DDNS.check_ssl()            -- HTTPS support
14                 and DDNS.check_proxy()          -- Proxy support
15                 and DDNS.check_bind_host()      -- DNS TCP support
16                 )
17 need_update = CTRL.update_needed()              -- correct ddns-scripts version
18
19 -- html constants
20 font_red = [[<font color="red">]]
21 font_off = [[</font>]]
22 bold_on  = [[<strong>]]
23 bold_off = [[</strong>]]
24
25 -- cbi-map definition -- #######################################################
26 m = Map("ddns")
27
28 -- first need to close <a> from cbi map template our <a> closed by template
29 --m.title = [[</a><a href="javascript:alert(']] .. CTRL.show_versions() ..[[')">]] ..
30 --              translate("Dynamic DNS")
31 m.title = [[</a><a href="#" onclick="onclick_maptitle();">]] ..
32                 translate("Dynamic DNS")
33
34 m.description = translate("Dynamic DNS allows that your router can be reached with " ..
35                         "a fixed hostname while having a dynamically changing " ..
36                         "IP address.")
37
38 m.on_after_commit = function(self)
39         if self.changed then    -- changes ?
40                 if SYS.init.enabled("ddns") then        -- ddns service enabled, restart all
41                         os.execute("/etc/init.d/ddns restart")
42                 else    -- ddns service disabled, send SIGHUP to running
43                         os.execute("killall -1 dynamic_dns_updater.sh")
44                 end
45         end
46 end
47
48 -- SimpleSection definiton -- ##################################################
49 -- with all the JavaScripts we need for "a good Show"
50 a = m:section( SimpleSection )
51 a.template = "ddns/overview_status"
52
53 -- SimpleSection definition -- #################################################
54 -- show Hints to optimize installation and script usage
55 -- only show if         service not enabled
56 --              or      no IPv6 support
57 --              or      not GNU Wget and not cURL       (for https support)
58 --              or      not GNU Wget but cURL without proxy support
59 --              or      not BIND's host
60 --              or      ddns-scripts package need update
61 if show_hints or need_update or not SYS.init.enabled("ddns") then
62         s = m:section( SimpleSection, translate("Hints") )
63
64         -- ddns_scripts needs to be updated for full functionality
65         if need_update then
66                 local dv = s:option(DummyValue, "_update_needed")
67                 dv.titleref = DISP.build_url("admin", "system", "packages")
68                 dv.rawhtml  = true
69                 dv.title = font_red .. bold_on ..
70                         translate("Software update required") .. bold_off .. font_off
71                 dv.value = translate("The currently installed 'ddns-scripts' package did not support all available settings.") ..
72                                 "<br />" ..
73                                 translate("Please update to the current version!")
74         end
75
76         -- DDNS Service disabled
77         if not SYS.init.enabled("ddns") then
78                 local dv = s:option(DummyValue, "_not_enabled")
79                 dv.titleref = DISP.build_url("admin", "system", "startup")
80                 dv.rawhtml  = true
81                 dv.title = bold_on ..
82                         translate("DDNS Autostart disabled") .. bold_off
83                 dv.value = translate("Currently DDNS updates are not started at boot or on interface events." .. "<br />" ..
84                                 "You can start/stop each configuration here. It will run until next reboot.")
85         end
86
87         -- Show more hints on a separate page
88         if show_hints then
89                 local dv = s:option(DummyValue, "_separate")
90                 dv.titleref = DISP.build_url("admin", "services", "ddns", "hints")
91                 dv.rawhtml  = true
92                 dv.title = bold_on ..
93                         translate("Show more") .. bold_off
94                 dv.value = translate("Follow this link" .. "<br />" ..
95                                 "You will find more hints to optimize your system to run DDNS scripts with all options")
96         end
97 end
98
99 -- TableSection definition -- ##################################################
100 ts = m:section( TypedSection, "service",
101         translate("Overview"),
102         translate("Below is a list of configured DDNS configurations and their current state." .. "<br />" ..
103                 "If you want to send updates for IPv4 and IPv6 you need to define two separate Configurations " ..
104                 "i.e. 'myddns_ipv4' and 'myddns_ipv6'") )
105 ts.sectionhead = translate("Configuration")
106 ts.template = "cbi/tblsection"
107 ts.addremove = true
108 ts.extedit = DISP.build_url("admin", "services", "ddns", "detail", "%s")
109 function ts.create(self, name)
110         AbstractSection.create(self, name)
111         HTTP.redirect( self.extedit:format(name) )
112 end
113
114 -- Domain and registered IP -- #################################################
115 dom = ts:option(DummyValue, "_domainIP",
116         translate("Hostname/Domain") .. "<br />" .. translate("Registered IP") )
117 dom.template = "ddns/overview_doubleline"
118 function dom.set_one(self, section)
119         local domain = self.map:get(section, "domain") or ""
120         if domain ~= "" then
121                 return domain
122         else
123                 return [[<em>]] .. translate("config error") .. [[</em>]]
124         end
125 end
126 function dom.set_two(self, section)
127         local domain = self.map:get(section, "domain") or ""
128         if domain == "" then return "" end
129         local dnsserver = self.map:get(section, "dnsserver") or ""
130         local use_ipv6 = tonumber(self.map:get(section, "use_ipv6") or 0)
131         local force_ipversion = tonumber(self.map:get(section, "force_ipversion") or 0)
132         local force_dnstcp = tonumber(self.map:get(section, "force_dnstcp") or 0)
133         local command = [[/usr/lib/ddns/dynamic_dns_lucihelper.sh]]
134         if not NXFS.access(command, "rwx", "rx", "rx") then
135                 NXFS.chmod(command, 755)
136         end
137         command = command .. [[ get_registered_ip ]] .. domain .. [[ ]] .. use_ipv6 ..
138                 [[ ]] .. force_ipversion .. [[ ]] .. force_dnstcp .. [[ ]] .. dnsserver
139         local ip = SYS.exec(command)
140         if ip == "" then ip = translate("no data") end
141         return ip
142 end
143
144 -- enabled
145 ena = ts:option( Flag, "enabled",
146         translate("Enabled"))
147 ena.template = "ddns/overview_enabled"
148 ena.rmempty = false
149 function ena.parse(self, section)
150         DDNS.flag_parse(self, section)
151 end
152
153 -- show PID and next update
154 upd = ts:option( DummyValue, "_update",
155         translate("Last Update") .. "<br />" .. translate("Next Update"))
156 upd.template = "ddns/overview_doubleline"
157 function upd.set_one(self, section)     -- fill Last Update
158         -- get/validate last update
159         local uptime   = SYS.uptime()
160         local lasttime = DDNS.get_lastupd(section)
161         if lasttime > uptime then       -- /var might not be linked to /tmp and cleared on reboot
162                 lasttime = 0
163         end
164
165         -- no last update happen
166         if lasttime == 0 then
167                 return translate("never")
168
169         -- we read last update
170         else
171                 -- calc last update
172                 --            os.epoch  - sys.uptime + lastupdate(uptime)
173                 local epoch = os.time() - uptime + lasttime
174                 -- use linux date to convert epoch
175                 return DDNS.epoch2date(epoch)
176         end
177 end
178 function upd.set_two(self, section)     -- fill Next Update
179         -- get enabled state
180         local enabled   = tonumber(self.map:get(section, "enabled") or 0)
181         local datenext  = translate("unknown error")    -- formatted date of next update
182
183         -- get force seconds
184         local force_interval = tonumber(self.map:get(section, "force_interval") or 72)
185         local force_unit = self.map:get(section, "force_unit") or "hours"
186         local force_seconds = DDNS.calc_seconds(force_interval, force_unit)
187
188         -- get last update and get/validate PID
189         local uptime   = SYS.uptime()
190         local lasttime = DDNS.get_lastupd(section)
191         if lasttime > uptime then       -- /var might not be linked to /tmp and cleared on reboot
192                 lasttime = 0
193         end
194         local pid      = DDNS.get_pid(section)
195
196         -- calc next update
197         if lasttime > 0 then
198                 local epoch = os.time() - uptime + lasttime + force_seconds
199                 -- use linux date to convert epoch
200                 datelast = DDNS.epoch2date(epoch)
201         end
202
203         -- process running but update needs to happen
204         if pid > 0 and ( lasttime + force_seconds - uptime ) < 0 then
205                 datenext = translate("Verify")
206
207         -- run once
208         elseif force_seconds == 0 then
209                 datenext = translate("Run once")
210
211         -- no process running and NOT enabled
212         elseif pid == 0 and enabled == 0 then
213                 datenext  = translate("Disabled")
214
215         -- no process running and NOT
216         elseif pid == 0 and enabled ~= 0 then
217                 datenext = translate("Stopped")
218         end
219
220         return datenext
221 end
222
223 -- start/stop button
224 btn = ts:option( Button, "_startstop",
225         translate("Process ID") .. "<br />" .. translate("Start / Stop") )
226 btn.template = "ddns/overview_startstop"
227 function btn.cfgvalue(self, section)
228         local pid = DDNS.get_pid(section)
229         if pid > 0 then
230                 btn.inputtitle  = "PID: " .. pid
231                 btn.inputstyle  = "reset"
232                 btn.disabled    = false
233         elseif (self.map:get(section, "enabled") or "0") ~= "0" then
234                 btn.inputtitle  = translate("Start")
235                 btn.inputstyle  = "apply"
236                 btn.disabled    = false
237         else
238                 btn.inputtitle  = "----------"
239                 btn.inputstyle  = "button"
240                 btn.disabled    = true
241         end
242         return true
243 end
244
245 return m