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