Rebased from upstream / out of band repository.
[librecmc/librecmc.git] / package / luci / 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
84                 page = entry({"admin", "wireless_deauth"}, post("wifi_deauth"), nil)
85                 page.leaf = true
86         end
87
88         page = entry({"admin", "translations"}, call("action_translations"), nil)
89         page.leaf = true
90
91         -- Logout is last
92         entry({"admin", "logout"}, call("action_logout"), _("Logout"), 999)
93 end
94
95 function action_logout()
96         local dsp = require "luci.dispatcher"
97         local utl = require "luci.util"
98         local sid = dsp.context.authsession
99
100         if sid then
101                 utl.ubus("session", "destroy", { ubus_rpc_session = sid })
102
103                 luci.http.header("Set-Cookie", "sysauth=%s; expires=%s; path=%s" %{
104                         '', 'Thu, 01 Jan 1970 01:00:00 GMT', dsp.build_url()
105                 })
106         end
107
108         luci.http.redirect(dsp.build_url())
109 end
110
111 function action_translations(lang)
112         local i18n = require "luci.i18n"
113         local http = require "luci.http"
114         local fs = require "nixio".fs
115
116         if lang and #lang > 0 then
117                 lang = i18n.setlanguage(lang)
118                 if lang then
119                         local s = fs.stat("%s/base.%s.lmo" %{ i18n.i18ndir, lang })
120                         if s then
121                                 http.header("Cache-Control", "public, max-age=31536000")
122                                 http.header("ETag", "%x-%x-%x" %{ s["ino"], s["size"], s["mtime"] })
123                         end
124                 end
125         end
126
127         http.prepare_content("application/javascript; charset=utf-8")
128         http.write("window.TR=")
129         http.write_json(i18n.dump())
130 end
131
132
133 function lease_status()
134         local s = require "luci.tools.status"
135
136         luci.http.prepare_content("application/json")
137         luci.http.write('[')
138         luci.http.write_json(s.dhcp_leases())
139         luci.http.write(',')
140         luci.http.write_json(s.dhcp6_leases())
141         luci.http.write(']')
142 end
143
144 function wifi_assoclist()
145         local s = require "luci.tools.status"
146
147         luci.http.prepare_content("application/json")
148         luci.http.write_json(s.wifi_assoclist())
149 end
150
151 function wifi_deauth()
152         local iface = luci.http.formvalue("iface")
153         local bssid = luci.http.formvalue("bssid")
154
155         if iface and bssid then
156                 luci.util.ubus("hostapd.%s" % iface, "del_client", {
157                         addr = bssid,
158                         deauth = true,
159                         reason = 5,
160                         ban_time = 60000
161                 })
162         end
163         luci.http.status(200, "OK")
164 end