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