system.lua 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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, "log_proto", translate("External system log server protocol"))
  61. o:value("udp", "UDP")
  62. o:value("tcp", "TCP")
  63. o = s:taboption("logging", Value, "log_file", translate("Write system log to file"))
  64. o.optional = true
  65. o.placeholder = "/tmp/system.log"
  66. o = s:taboption("logging", ListValue, "conloglevel", translate("Log output level"))
  67. o:value(8, translate("Debug"))
  68. o:value(7, translate("Info"))
  69. o:value(6, translate("Notice"))
  70. o:value(5, translate("Warning"))
  71. o:value(4, translate("Error"))
  72. o:value(3, translate("Critical"))
  73. o:value(2, translate("Alert"))
  74. o:value(1, translate("Emergency"))
  75. o = s:taboption("logging", ListValue, "cronloglevel", translate("Cron Log Level"))
  76. o.default = 8
  77. o:value(5, translate("Debug"))
  78. o:value(8, translate("Normal"))
  79. o:value(9, translate("Warning"))
  80. --
  81. -- Langauge & Style
  82. --
  83. o = s:taboption("language", ListValue, "_lang", translate("Language"))
  84. o:value("auto")
  85. local i18ndir = luci.i18n.i18ndir .. "base."
  86. for k, v in luci.util.kspairs(conf.languages) do
  87. local file = i18ndir .. k:gsub("_", "-")
  88. if k:sub(1, 1) ~= "." and fs.access(file .. ".lmo") then
  89. o:value(k, v)
  90. end
  91. end
  92. function o.cfgvalue(...)
  93. return m.uci:get("luci", "main", "lang")
  94. end
  95. function o.write(self, section, value)
  96. m.uci:set("luci", "main", "lang", value)
  97. end
  98. o = s:taboption("language", ListValue, "_mediaurlbase", translate("Design"))
  99. for k, v in pairs(conf.themes) do
  100. if k:sub(1, 1) ~= "." then
  101. o:value(v, k)
  102. end
  103. end
  104. function o.cfgvalue(...)
  105. return m.uci:get("luci", "main", "mediaurlbase")
  106. end
  107. function o.write(self, section, value)
  108. m.uci:set("luci", "main", "mediaurlbase", value)
  109. end
  110. --
  111. -- NTP
  112. --
  113. if has_ntpd then
  114. -- timeserver setup was requested, create section and reload page
  115. if m:formvalue("cbid.system._timeserver._enable") then
  116. m.uci:section("system", "timeserver", "ntp",
  117. {
  118. server = { "0.openwrt.pool.ntp.org", "1.openwrt.pool.ntp.org", "2.openwrt.pool.ntp.org", "3.openwrt.pool.ntp.org" }
  119. }
  120. )
  121. m.uci:save("system")
  122. luci.http.redirect(luci.dispatcher.build_url("admin/system", arg[1]))
  123. return
  124. end
  125. local has_section = false
  126. m.uci:foreach("system", "timeserver",
  127. function(s)
  128. has_section = true
  129. return false
  130. end)
  131. if not has_section then
  132. s = m:section(TypedSection, "timeserver", translate("Time Synchronization"))
  133. s.anonymous = true
  134. s.cfgsections = function() return { "_timeserver" } end
  135. x = s:option(Button, "_enable")
  136. x.title = translate("Time Synchronization is not configured yet.")
  137. x.inputtitle = translate("Set up Time Synchronization")
  138. x.inputstyle = "apply"
  139. else
  140. s = m:section(TypedSection, "timeserver", translate("Time Synchronization"))
  141. s.anonymous = true
  142. s.addremove = false
  143. o = s:option(Flag, "enable", translate("Enable NTP client"))
  144. o.rmempty = false
  145. function o.cfgvalue(self)
  146. return sys.init.enabled("sysntpd")
  147. and self.enabled or self.disabled
  148. end
  149. function o.write(self, section, value)
  150. if value == self.enabled then
  151. sys.init.enable("sysntpd")
  152. sys.call("env -i /etc/init.d/sysntpd start >/dev/null")
  153. else
  154. sys.call("env -i /etc/init.d/sysntpd stop >/dev/null")
  155. sys.init.disable("sysntpd")
  156. end
  157. end
  158. o = s:option(Flag, "enable_server", translate("Provide NTP server"))
  159. o:depends("enable", "1")
  160. o = s:option(DynamicList, "server", translate("NTP server candidates"))
  161. o.datatype = "host(0)"
  162. o:depends("enable", "1")
  163. -- retain server list even if disabled
  164. function o.remove() end
  165. end
  166. end
  167. return m