Merge pull request #656 from nlhintz/pull-request
[project/luci.git] / modules / luci-mod-admin-full / luasrc / model / cbi / admin_system / system.lua
1 -- Copyright 2008 Steven Barth <steven@midlink.org>
2 -- Copyright 2011 Jo-Philipp Wich <jow@openwrt.org>
3 -- Licensed to the public under the Apache License 2.0.
4
5 local sys   = require "luci.sys"
6 local zones = require "luci.sys.zoneinfo"
7 local fs    = require "nixio.fs"
8 local conf  = require "luci.config"
9
10 local m, s, o
11 local has_ntpd = fs.access("/usr/sbin/ntpd")
12
13 m = Map("system", translate("System"), translate("Here you can configure the basic aspects of your device like its hostname or the timezone."))
14 m:chain("luci")
15
16
17 s = m:section(TypedSection, "system", translate("System Properties"))
18 s.anonymous = true
19 s.addremove = false
20
21 s:tab("general",  translate("General Settings"))
22 s:tab("logging",  translate("Logging"))
23 s:tab("language", translate("Language and Style"))
24
25
26 --
27 -- System Properties
28 --
29
30 o = s:taboption("general", DummyValue, "_systime", translate("Local Time"))
31 o.template = "admin_system/clock_status"
32
33
34 o = s:taboption("general", Value, "hostname", translate("Hostname"))
35 o.datatype = "hostname"
36
37 function o.write(self, section, value)
38         Value.write(self, section, value)
39         sys.hostname(value)
40 end
41
42
43 o = s:taboption("general", ListValue, "zonename", translate("Timezone"))
44 o:value("UTC")
45
46 for i, zone in ipairs(zones.TZ) do
47         o:value(zone[1])
48 end
49
50 function o.write(self, section, value)
51         local function lookup_zone(title)
52                 for _, zone in ipairs(zones.TZ) do
53                         if zone[1] == title then return zone[2] end
54                 end
55         end
56
57         AbstractValue.write(self, section, value)
58         local timezone = lookup_zone(value) or "GMT0"
59         self.map.uci:set("system", section, "timezone", timezone)
60         fs.writefile("/etc/TZ", timezone .. "\n")
61 end
62
63
64 --
65 -- Logging
66 --
67
68 o = s:taboption("logging", Value, "log_size", translate("System log buffer size"), "kiB")
69 o.optional    = true
70 o.placeholder = 16
71 o.datatype    = "uinteger"
72
73 o = s:taboption("logging", Value, "log_ip", translate("External system log server"))
74 o.optional    = true
75 o.placeholder = "0.0.0.0"
76 o.datatype    = "ip4addr"
77
78 o = s:taboption("logging", Value, "log_port", translate("External system log server port"))
79 o.optional    = true
80 o.placeholder = 514
81 o.datatype    = "port"
82
83 o = s:taboption("logging", ListValue, "log_proto", translate("External system log server protocol"))
84 o:value("udp", "UDP")
85 o:value("tcp", "TCP")
86
87 o = s:taboption("logging", Value, "log_file", translate("Write system log to file"))
88 o.optional    = true
89 o.placeholder = "/tmp/system.log"
90
91 o = s:taboption("logging", ListValue, "conloglevel", translate("Log output level"))
92 o:value(8, translate("Debug"))
93 o:value(7, translate("Info"))
94 o:value(6, translate("Notice"))
95 o:value(5, translate("Warning"))
96 o:value(4, translate("Error"))
97 o:value(3, translate("Critical"))
98 o:value(2, translate("Alert"))
99 o:value(1, translate("Emergency"))
100
101 o = s:taboption("logging", ListValue, "cronloglevel", translate("Cron Log Level"))
102 o.default = 8
103 o:value(5, translate("Debug"))
104 o:value(8, translate("Normal"))
105 o:value(9, translate("Warning"))
106
107
108 --
109 -- Langauge & Style
110 --
111
112 o = s:taboption("language", ListValue, "_lang", translate("Language"))
113 o:value("auto")
114
115 local i18ndir = luci.i18n.i18ndir .. "base."
116 for k, v in luci.util.kspairs(conf.languages) do
117         local file = i18ndir .. k:gsub("_", "-")
118         if k:sub(1, 1) ~= "." and fs.access(file .. ".lmo") then
119                 o:value(k, v)
120         end
121 end
122
123 function o.cfgvalue(...)
124         return m.uci:get("luci", "main", "lang")
125 end
126
127 function o.write(self, section, value)
128         m.uci:set("luci", "main", "lang", value)
129 end
130
131
132 o = s:taboption("language", ListValue, "_mediaurlbase", translate("Design"))
133 for k, v in pairs(conf.themes) do
134         if k:sub(1, 1) ~= "." then
135                 o:value(v, k)
136         end
137 end
138
139 function o.cfgvalue(...)
140         return m.uci:get("luci", "main", "mediaurlbase")
141 end
142
143 function o.write(self, section, value)
144         m.uci:set("luci", "main", "mediaurlbase", value)
145 end
146
147
148 --
149 -- NTP
150 --
151
152 if has_ntpd then
153
154         -- timeserver setup was requested, create section and reload page
155         if m:formvalue("cbid.system._timeserver._enable") then
156                 m.uci:section("system", "timeserver", "ntp",
157                         {
158                         server = { "0.openwrt.pool.ntp.org", "1.openwrt.pool.ntp.org", "2.openwrt.pool.ntp.org", "3.openwrt.pool.ntp.org" }
159                         }
160                 )
161
162                 m.uci:save("system")
163                 luci.http.redirect(luci.dispatcher.build_url("admin/system", arg[1]))
164                 return
165         end
166
167         local has_section = false
168         m.uci:foreach("system", "timeserver", 
169                 function(s) 
170                         has_section = true 
171                         return false
172         end)
173
174         if not has_section then
175
176                 s = m:section(TypedSection, "timeserver", translate("Time Synchronization"))
177                 s.anonymous   = true
178                 s.cfgsections = function() return { "_timeserver" } end
179
180                 x = s:option(Button, "_enable")
181                 x.title      = translate("Time Synchronization is not configured yet.")
182                 x.inputtitle = translate("Set up Time Synchronization")
183                 x.inputstyle = "apply"
184
185         else
186                 
187                 s = m:section(TypedSection, "timeserver", translate("Time Synchronization"))
188                 s.anonymous = true
189                 s.addremove = false
190
191                 o = s:option(Flag, "enable", translate("Enable NTP client"))
192                 o.rmempty = false
193
194                 function o.cfgvalue(self)
195                         return sys.init.enabled("sysntpd")
196                                 and self.enabled or self.disabled
197                 end
198
199                 function o.write(self, section, value)
200                         if value == self.enabled then
201                                 sys.init.enable("sysntpd")
202                                 sys.call("env -i /etc/init.d/sysntpd start >/dev/null")
203                         else
204                                 sys.call("env -i /etc/init.d/sysntpd stop >/dev/null")
205                                 sys.init.disable("sysntpd")
206                         end
207                 end
208
209
210                 o = s:option(Flag, "enable_server", translate("Provide NTP server"))
211                 o:depends("enable", "1")
212
213
214                 o = s:option(DynamicList, "server", translate("NTP server candidates"))
215                 o.datatype = "host(0)"
216                 o:depends("enable", "1")
217
218                 -- retain server list even if disabled
219                 function o.remove() end
220
221         end
222 end
223
224 return m