Translated using Weblate (Japanese)
[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 action_logout()
7         local dsp = require "luci.dispatcher"
8         local utl = require "luci.util"
9         local sid = dsp.context.authsession
10
11         if sid then
12                 utl.ubus("session", "destroy", { ubus_rpc_session = sid })
13
14                 luci.http.header("Set-Cookie", "sysauth=%s; expires=%s; path=%s" %{
15                         '', 'Thu, 01 Jan 1970 01:00:00 GMT', dsp.build_url()
16                 })
17         end
18
19         luci.http.redirect(dsp.build_url())
20 end
21
22 function action_translations(lang)
23         local i18n = require "luci.i18n"
24         local http = require "luci.http"
25         local fs = require "nixio".fs
26
27         if lang and #lang > 0 then
28                 lang = i18n.setlanguage(lang)
29                 if lang then
30                         local s = fs.stat("%s/base.%s.lmo" %{ i18n.i18ndir, lang })
31                         if s then
32                                 http.header("Cache-Control", "public, max-age=31536000")
33                                 http.header("ETag", "%x-%x-%x" %{ s["ino"], s["size"], s["mtime"] })
34                         end
35                 end
36         end
37
38         http.prepare_content("application/javascript; charset=utf-8")
39         http.write("window.TR=")
40         http.write_json(i18n.dump())
41 end
42
43 local function ubus_reply(id, data, code, errmsg)
44         local reply = { jsonrpc = "2.0", id = id }
45         if errmsg then
46                 reply.error = {
47                         code = code,
48                         message = errmsg
49                 }
50         elseif type(code) == "table" then
51                 reply.result = code
52         else
53                 reply.result = { code, data }
54         end
55
56         return reply
57 end
58
59 local ubus_types = {
60         nil,
61         "array",
62         "object",
63         "string",
64         nil, -- INT64
65         "number",
66         nil, -- INT16,
67         "boolean",
68         "double"
69 }
70
71 local function ubus_access(sid, obj, fun)
72         local res, code = luci.util.ubus("session", "access", {
73                 ubus_rpc_session = sid,
74                 scope            = "ubus",
75                 object           = obj,
76                 ["function"]     = fun
77         })
78
79         return (type(res) == "table" and res.access == true)
80 end
81
82 local function ubus_request(req)
83         if type(req) ~= "table" or type(req.method) ~= "string" or req.jsonrpc ~= "2.0" or req.id == nil then
84                 return ubus_reply(nil, nil, -32600, "Invalid request")
85
86         elseif req.method == "call" then
87                 if type(req.params) ~= "table" or #req.params < 3 then
88                         return ubus_reply(nil, nil, -32600, "Invalid parameters")
89                 end
90
91                 local sid, obj, fun, arg =
92                         req.params[1], req.params[2], req.params[3], req.params[4] or {}
93                 if type(arg) ~= "table" or arg.ubus_rpc_session ~= nil then
94                         return ubus_reply(req.id, nil, -32602, "Invalid parameters")
95                 end
96
97                 if sid == "00000000000000000000000000000000" and luci.dispatcher.context.authsession then
98                         sid = luci.dispatcher.context.authsession
99                 end
100
101                 if not ubus_access(sid, obj, fun) then
102                         return ubus_reply(req.id, nil, -32002, "Access denied")
103                 end
104
105                 arg.ubus_rpc_session = sid
106
107                 local res, code = luci.util.ubus(obj, fun, arg)
108                 return ubus_reply(req.id, res, code or 0)
109
110         elseif req.method == "list" then
111                 if req.params == nil or (type(req.params) == "table" and #req.params == 0) then
112                         local objs = luci.util.ubus()
113                         return ubus_reply(req.id, nil, objs)
114
115                 elseif type(req.params) == "table" then
116                         local n, rv = nil, {}
117                         for n = 1, #req.params do
118                                 if type(req.params[n]) ~= "string" then
119                                         return ubus_reply(req.id, nil, -32602, "Invalid parameters")
120                                 end
121
122                                 local sig = luci.util.ubus(req.params[n])
123                                 if sig and type(sig) == "table" then
124                                         rv[req.params[n]] = {}
125
126                                         local m, p
127                                         for m, p in pairs(sig) do
128                                                 if type(p) == "table" then
129                                                         rv[req.params[n]][m] = {}
130
131                                                         local pn, pt
132                                                         for pn, pt in pairs(p) do
133                                                                 rv[req.params[n]][m][pn] = ubus_types[pt] or "unknown"
134                                                         end
135                                                 end
136                                         end
137                                 end
138                         end
139                         return ubus_reply(req.id, nil, rv)
140
141                 else
142                         return ubus_reply(req.id, nil, -32602, "Invalid parameters")
143                 end
144         end
145
146         return ubus_reply(req.id, nil, -32601, "Method not found")
147 end
148
149 function action_ubus()
150         local parser = require "luci.jsonc".new()
151
152         luci.http.context.request:setfilehandler(function(_, s)
153                 if not s then
154                         return nil
155                 end
156
157                 local ok, err = parser:parse(s)
158                 return (not err or nil)
159         end)
160
161         luci.http.context.request:content()
162
163         local json = parser:get()
164         if json == nil or type(json) ~= "table" then
165                 luci.http.prepare_content("application/json")
166                 luci.http.write_json(ubus_reply(nil, nil, -32700, "Parse error"))
167                 return
168         end
169
170         local response
171         if #json == 0 then
172                 response = ubus_request(json)
173         else
174                 response = {}
175
176                 local _, request
177                 for _, request in ipairs(json) do
178                         response[_] = ubus_request(request)
179                 end
180         end
181
182         luci.http.prepare_content("application/json")
183         luci.http.write_json(response)
184 end
185
186 function action_menu()
187         local dsp = require "luci.dispatcher"
188         local utl = require "luci.util"
189         local http = require "luci.http"
190
191         local acls = utl.ubus("session", "access", { ubus_rpc_session = http.getcookie("sysauth") })
192         local menu = dsp.menu_json(acls or {}) or {}
193
194         http.prepare_content("application/json")
195         http.write_json(menu)
196 end