Merge pull request #2245 from Ansuel/fstab
[oweals/luci.git] / modules / luci-base / luasrc / controller / admin / index.lua
1 -- Copyright 2008 Steven Barth <steven@midlink.org>
2 -- Licensed to the public under the Apache License 2.0.
3
4 module("luci.controller.admin.index", package.seeall)
5
6 function index()
7         function toplevel_page(page, preflookup, preftarget)
8                 if preflookup and preftarget then
9                         if lookup(preflookup) then
10                                 page.target = preftarget
11                         end
12                 end
13
14                 if not page.target then
15                         page.target = firstchild()
16                 end
17         end
18
19         local uci = require("luci.model.uci").cursor()
20
21         local root = node()
22         if not root.target then
23                 root.target = alias("admin")
24                 root.index = true
25         end
26
27         local page   = node("admin")
28
29         page.title   = _("Administration")
30         page.order   = 10
31         page.sysauth = "root"
32         page.sysauth_authenticator = "htmlauth"
33         page.ucidata = true
34         page.index = true
35         page.target = firstnode()
36
37         -- Empty menu tree to be populated by addons and modules
38
39         page = node("admin", "status")
40         page.title = _("Status")
41         page.order = 10
42         page.index = true
43         -- overview is from mod-admin-full
44         toplevel_page(page, "admin/status/overview", alias("admin", "status", "overview"))
45
46         page = node("admin", "system")
47         page.title = _("System")
48         page.order = 20
49         page.index = true
50         -- system/system is from mod-admin-full
51         toplevel_page(page, "admin/system/system", alias("admin", "system", "system"))
52
53         -- Only used if applications add items
54         page = node("admin", "services")
55         page.title = _("Services")
56         page.order = 40
57         page.index = true
58         toplevel_page(page, false, false)
59
60         -- Even for mod-admin-full network just uses first submenu item as landing
61         page = node("admin", "network")
62         page.title = _("Network")
63         page.order = 50
64         page.index = true
65         toplevel_page(page, false, false)
66
67         if nixio.fs.access("/etc/config/dhcp") then
68                 page = entry({"admin", "dhcplease_status"}, call("lease_status"), nil)
69                 page.leaf = true
70         end
71
72         local has_wifi = false
73
74         uci:foreach("wireless", "wifi-device",
75                 function(s)
76                         has_wifi = true
77                         return false
78                 end)
79
80         if has_wifi then
81                 page = entry({"admin", "wireless_assoclist"}, call("wifi_assoclist"), nil)
82                 page.leaf = true
83         end
84
85         page = entry({"admin", "translations"}, call("action_translations"), nil)
86         page.leaf = true
87
88         -- Logout is last
89         entry({"admin", "logout"}, call("action_logout"), _("Logout"), 999)
90 end
91
92 function action_logout()
93         local dsp = require "luci.dispatcher"
94         local utl = require "luci.util"
95         local sid = dsp.context.authsession
96
97         if sid then
98                 utl.ubus("session", "destroy", { ubus_rpc_session = sid })
99
100                 luci.http.header("Set-Cookie", "sysauth=%s; expires=%s; path=%s" %{
101                         '', 'Thu, 01 Jan 1970 01:00:00 GMT', dsp.build_url()
102                 })
103         end
104
105         luci.http.redirect(dsp.build_url())
106 end
107
108 function action_translations(lang)
109         local i18n = require "luci.i18n"
110         local http = require "luci.http"
111         local fs = require "nixio".fs
112
113         if lang and #lang > 0 then
114                 lang = i18n.setlanguage(lang)
115                 if lang then
116                         local s = fs.stat("%s/base.%s.lmo" %{ i18n.i18ndir, lang })
117                         if s then
118                                 http.header("Cache-Control", "public, max-age=31536000")
119                                 http.header("ETag", "%x-%x-%x" %{ s["ino"], s["size"], s["mtime"] })
120                         end
121                 end
122         end
123
124         http.prepare_content("application/javascript; charset=utf-8")
125         http.write("window.TR=")
126         http.write_json(i18n.dump())
127 end
128
129
130 function lease_status()
131         local s = require "luci.tools.status"
132
133         luci.http.prepare_content("application/json")
134         luci.http.write('[')
135         luci.http.write_json(s.dhcp_leases())
136         luci.http.write(',')
137         luci.http.write_json(s.dhcp6_leases())
138         luci.http.write(']')
139 end
140
141 function wifi_assoclist()
142         local s = require "luci.tools.status"
143
144         luci.http.prepare_content("application/json")
145         luci.http.write_json(s.wifi_assoclist())
146 end