luci-base: Make default for FileUpload 'safe'
[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, "conloglevel", translate("Log output level"))
84 o:value(8, translate("Debug"))
85 o:value(7, translate("Info"))
86 o:value(6, translate("Notice"))
87 o:value(5, translate("Warning"))
88 o:value(4, translate("Error"))
89 o:value(3, translate("Critical"))
90 o:value(2, translate("Alert"))
91 o:value(1, translate("Emergency"))
92
93 o = s:taboption("logging", ListValue, "cronloglevel", translate("Cron Log Level"))
94 o.default = 8
95 o:value(5, translate("Debug"))
96 o:value(8, translate("Normal"))
97 o:value(9, translate("Warning"))
98
99
100 --
101 -- Langauge & Style
102 --
103
104 o = s:taboption("language", ListValue, "_lang", translate("Language"))
105 o:value("auto")
106
107 local i18ndir = luci.i18n.i18ndir .. "base."
108 for k, v in luci.util.kspairs(conf.languages) do
109         local file = i18ndir .. k:gsub("_", "-")
110         if k:sub(1, 1) ~= "." and fs.access(file .. ".lmo") then
111                 o:value(k, v)
112         end
113 end
114
115 function o.cfgvalue(...)
116         return m.uci:get("luci", "main", "lang")
117 end
118
119 function o.write(self, section, value)
120         m.uci:set("luci", "main", "lang", value)
121 end
122
123
124 o = s:taboption("language", ListValue, "_mediaurlbase", translate("Design"))
125 for k, v in pairs(conf.themes) do
126         if k:sub(1, 1) ~= "." then
127                 o:value(v, k)
128         end
129 end
130
131 function o.cfgvalue(...)
132         return m.uci:get("luci", "main", "mediaurlbase")
133 end
134
135 function o.write(self, section, value)
136         m.uci:set("luci", "main", "mediaurlbase", value)
137 end
138
139
140 --
141 -- NTP
142 --
143
144 if has_ntpd then
145
146         -- timeserver setup was requested, create section and reload page
147         if m:formvalue("cbid.system._timeserver._enable") then
148                 m.uci:section("system", "timeserver", "ntp",
149                         {
150                         server = { "0.openwrt.pool.ntp.org", "1.openwrt.pool.ntp.org", "2.openwrt.pool.ntp.org", "3.openwrt.pool.ntp.org" }
151                         }
152                 )
153
154                 m.uci:save("system")
155                 luci.http.redirect(luci.dispatcher.build_url("admin/system", arg[1]))
156                 return
157         end
158
159         local has_section = false
160         m.uci:foreach("system", "timeserver", 
161                 function(s) 
162                         has_section = true 
163                         return false
164         end)
165
166         if not has_section then
167
168                 s = m:section(TypedSection, "timeserver", translate("Time Synchronization"))
169                 s.anonymous   = true
170                 s.cfgsections = function() return { "_timeserver" } end
171
172                 x = s:option(Button, "_enable")
173                 x.title      = translate("Time Synchronization is not configured yet.")
174                 x.inputtitle = translate("Set up Time Synchronization")
175                 x.inputstyle = "apply"
176
177         else
178                 
179                 s = m:section(TypedSection, "timeserver", translate("Time Synchronization"))
180                 s.anonymous = true
181                 s.addremove = false
182
183                 o = s:option(Flag, "enable", translate("Enable NTP client"))
184                 o.rmempty = false
185
186                 function o.cfgvalue(self)
187                         return sys.init.enabled("sysntpd")
188                                 and self.enabled or self.disabled
189                 end
190
191                 function o.write(self, section, value)
192                         if value == self.enabled then
193                                 sys.init.enable("sysntpd")
194                                 sys.call("env -i /etc/init.d/sysntpd start >/dev/null")
195                         else
196                                 sys.call("env -i /etc/init.d/sysntpd stop >/dev/null")
197                                 sys.init.disable("sysntpd")
198                         end
199                 end
200
201
202                 o = s:option(Flag, "enable_server", translate("Provide NTP server"))
203                 o:depends("enable", "1")
204
205
206                 o = s:option(DynamicList, "server", translate("NTP server candidates"))
207                 o.datatype = "host"
208                 o:depends("enable", "1")
209
210                 -- retain server list even if disabled
211                 function o.remove() end
212
213         end
214 end
215
216 return m