Merge pull request #1801 from dibdot/adblock
[project/luci.git] / modules / luci-mod-admin-full / luasrc / controller / admin / uci.lua
1 -- Copyright 2008 Steven Barth <steven@midlink.org>
2 -- Copyright 2010-2015 Jo-Philipp Wich <jow@openwrt.org>
3 -- Licensed to the public under the Apache License 2.0.
4
5 module("luci.controller.admin.uci", package.seeall)
6
7 function index()
8         local redir = luci.http.formvalue("redir", true)
9                 or table.concat(luci.dispatcher.context.request, "/")
10
11         entry({"admin", "uci"}, nil, _("Configuration"))
12         entry({"admin", "uci", "changes"}, call("action_changes"), _("Changes"), 40).query = {redir=redir}
13         entry({"admin", "uci", "revert"}, post("action_revert"), _("Revert"), 30).query = {redir=redir}
14
15         local node
16         local authen = function(checkpass, allowed_users)
17                 return "root", luci.http.formvalue("sid")
18         end
19
20         node = entry({"admin", "uci", "apply_rollback"}, post("action_apply_rollback"), nil)
21         node.cors = true
22         node.sysauth_authenticator = authen
23
24         node = entry({"admin", "uci", "apply_unchecked"}, post("action_apply_unchecked"), nil)
25         node.cors = true
26         node.sysauth_authenticator = authen
27
28         node = entry({"admin", "uci", "confirm"}, post("action_confirm"), nil)
29         node.cors = true
30         node.sysauth_authenticator = authen
31 end
32
33
34 function action_changes()
35         local uci  = require "luci.model.uci"
36         local changes = uci:changes()
37
38         luci.template.render("admin_uci/changes", {
39                 changes  = next(changes) and changes,
40                 timeout  = timeout
41         })
42 end
43
44 function action_revert()
45         local uci = require "luci.model.uci"
46         local changes = uci:changes()
47
48         -- Collect files to be reverted
49         local r, tbl
50         for r, tbl in pairs(changes) do
51                 uci:revert(r)
52         end
53
54         luci.template.render("admin_uci/revert", {
55                 changes = next(changes) and changes
56         })
57 end
58
59
60 local function ubus_state_to_http(errstr)
61         local map = {
62                 ["Invalid command"]   = 400,
63                 ["Invalid argument"]  = 400,
64                 ["Method not found"]  = 404,
65                 ["Entry not found"]   = 404,
66                 ["No data"]           = 204,
67                 ["Permission denied"] = 403,
68                 ["Timeout"]           = 504,
69                 ["Not supported"]     = 500,
70                 ["Unknown error"]     = 500,
71                 ["Connection failed"] = 503
72         }
73
74         local code = map[errstr] or 200
75         local msg  = errstr      or "OK"
76
77         luci.http.status(code, msg)
78
79         if code ~= 204 then
80                 luci.http.prepare_content("text/plain")
81                 luci.http.write(msg)
82         end
83 end
84
85 function action_apply_rollback()
86         local uci = require "luci.model.uci"
87         local _, errstr = uci:apply(true)
88         ubus_state_to_http(errstr)
89 end
90
91 function action_apply_unchecked()
92         local uci = require "luci.model.uci"
93         local _, errstr = uci:apply(false)
94         ubus_state_to_http(errstr)
95 end
96
97 function action_confirm()
98         local uci = require "luci.model.uci"
99         local _, errstr = uci:confirm()
100         ubus_state_to_http(errstr)
101 end