system.lua 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. local sys = require "luci.sys"
  5. local zones = require "luci.sys.zoneinfo"
  6. local fs = require "nixio.fs"
  7. local conf = require "luci.config"
  8. local m, s, o
  9. local has_ntpd = fs.access("/usr/sbin/ntpd")
  10. m = Map("system", translate("System"), translate("Here you can configure the basic aspects of your device like its hostname or the timezone."))
  11. m:chain("luci")
  12. s = m:section(TypedSection, "system", translate("System Properties"))
  13. s.anonymous = true
  14. s.addremove = false
  15. s:tab("general", translate("General Settings"))
  16. s:tab("logging", translate("Logging"))
  17. s:tab("language", translate("Language and Style"))
  18. --
  19. -- System Properties
  20. --
  21. o = s:taboption("general", DummyValue, "_systime", translate("Local Time"))
  22. o.template = "admin_system/clock_status"
  23. o = s:taboption("general", Value, "hostname", translate("Hostname"))
  24. o.datatype = "hostname"
  25. function o.write(self, section, value)
  26. Value.write(self, section, value)
  27. sys.hostname(value)
  28. end
  29. o = s:taboption("general", ListValue, "zonename", translate("Timezone"))
  30. o:value("UTC")
  31. for i, zone in ipairs(zones.TZ) do
  32. o:value(zone[1])
  33. end
  34. function o.write(self, section, value)
  35. local function lookup_zone(title)
  36. for _, zone in ipairs(zones.TZ) do
  37. if zone[1] == title then return zone[2] end
  38. end
  39. end
  40. AbstractValue.write(self, section, value)
  41. local timezone = lookup_zone(value) or "GMT0"
  42. self.map.uci:set("system", section, "timezone", timezone)
  43. fs.writefile("/etc/TZ", timezone .. "\n")
  44. end
  45. --
  46. -- Logging
  47. --
  48. o = s:taboption("logging", Value, "log_size", translate("System log buffer size"), "kiB")
  49. o.optional = true
  50. o.placeholder = 16
  51. o.datatype = "uinteger"
  52. o = s:taboption("logging", Value, "log_ip", translate("External system log server"))
  53. o.optional = true
  54. o.placeholder = "0.0.0.0"
  55. o.datatype = "ip4addr"
  56. o = s:taboption("logging", Value, "log_port", translate("External system log server port"))
  57. o.optional = true
  58. o.placeholder = 514
  59. o.datatype = "port"
  60. o = s:taboption("logging", ListValue, "conloglevel", translate("Log output level"))
  61. o:value(8, translate("Debug"))
  62. o:value(7, translate("Info"))
  63. o:value(6, translate("Notice"))
  64. o:value(5, translate("Warning"))
  65. o:value(4, translate("Error"))
  66. o:value(3, translate("Critical"))
  67. o:value(2, translate("Alert"))
  68. o:value(1, translate("Emergency"))
  69. o = s:taboption("logging", ListValue, "cronloglevel", translate("Cron Log Level"))
  70. o.default = 8
  71. o:value(5, translate("Debug"))
  72. o:value(8, translate("Normal"))
  73. o:value(9, translate("Warning"))
  74. --
  75. -- Langauge & Style
  76. --
  77. o = s:taboption("language", ListValue, "_lang", translate("Language"))
  78. o:value("auto")
  79. local i18ndir = luci.i18n.i18ndir .. "base."
  80. for k, v in luci.util.kspairs(conf.languages) do
  81. local file = i18ndir .. k:gsub("_", "-")
  82. if k:sub(1, 1) ~= "." and fs.access(file .. ".lmo") then
  83. o:value(k, v)
  84. end
  85. end
  86. function o.cfgvalue(...)
  87. return m.uci:get("luci", "main", "lang")
  88. end
  89. function o.write(self, section, value)
  90. m.uci:set("luci", "main", "lang", value)
  91. end
  92. o = s:taboption("language", ListValue, "_mediaurlbase", translate("Design"))
  93. for k, v in pairs(conf.themes) do
  94. if k:sub(1, 1) ~= "." then
  95. o:value(v, k)
  96. end
  97. end
  98. function o.cfgvalue(...)
  99. return m.uci:get("luci", "main", "mediaurlbase")
  100. end
  101. function o.write(self, section, value)
  102. m.uci:set("luci", "main", "mediaurlbase", value)
  103. end
  104. --
  105. -- NTP
  106. --
  107. if has_ntpd then
  108. -- timeserver setup was requested, create section and reload page
  109. if m:formvalue("cbid.system._timeserver._enable") then
  110. m.uci:section("system", "timeserver", "ntp",
  111. {
  112. server = { "0.openwrt.pool.ntp.org", "1.openwrt.pool.ntp.org", "2.openwrt.pool.ntp.org", "3.openwrt.pool.ntp.org" }
  113. }
  114. )
  115. m.uci:save("system")
  116. luci.http.redirect(luci.dispatcher.build_url("admin/system", arg[1]))
  117. return
  118. end
  119. local has_section = false
  120. m.uci:foreach("system", "timeserver",
  121. function(s)
  122. has_section = true
  123. return false
  124. end)
  125. if not has_section then
  126. s = m:section(TypedSection, "timeserver", translate("Time Synchronization"))
  127. s.anonymous = true
  128. s.cfgsections = function() return { "_timeserver" } end
  129. x = s:option(Button, "_enable")
  130. x.title = translate("Time Synchronization is not configured yet.")
  131. x.inputtitle = translate("Set up Time Synchronization")
  132. x.inputstyle = "apply"
  133. else
  134. s = m:section(TypedSection, "timeserver", translate("Time Synchronization"))
  135. s.anonymous = true
  136. s.addremove = false
  137. o = s:option(Flag, "enable", translate("Enable NTP client"))
  138. o.rmempty = false
  139. function o.cfgvalue(self)
  140. return sys.init.enabled("sysntpd")
  141. and self.enabled or self.disabled
  142. end
  143. function o.write(self, section, value)
  144. if value == self.enabled then
  145. sys.init.enable("sysntpd")
  146. sys.call("env -i /etc/init.d/sysntpd start >/dev/null")
  147. else
  148. sys.call("env -i /etc/init.d/sysntpd stop >/dev/null")
  149. sys.init.disable("sysntpd")
  150. end
  151. end
  152. o = s:option(Flag, "enable_server", translate("Provide NTP server"))
  153. o:depends("enable", "1")
  154. o = s:option(DynamicList, "server", translate("NTP server candidates"))
  155. o.datatype = "host"
  156. o:depends("enable", "1")
  157. -- retain server list even if disabled
  158. function o.remove() end
  159. end
  160. end
  161. return m