362ad9def413bb9614d950949329d0dbadf17b08
[project/luci.git] / applications / luci-splash / luasrc / controller / splash / splash.lua
1 module("luci.controller.splash.splash", package.seeall)
2
3 function index()
4         entry({"admin", "services", "splash"}, cbi("splash/splash"), "Client-Splash")
5
6         node("splash").target = call("action_dispatch")
7         node("splash", "activate").target = call("action_activate")
8         node("splash", "splash").target   = template("splash_splash/splash")
9
10         entry({"admin", "status", "splash"}, call("action_status_admin"), "Client-Splash")
11 end
12
13 function action_dispatch()
14         local uci = luci.model.uci.cursor_state()
15         local mac = luci.sys.net.ip4mac(luci.http.getenv("REMOTE_ADDR")) or ""
16         local access = false
17
18         uci:foreach("luci_splash", "lease", function(s)
19                 if s.mac and s.mac:lower() == mac then access = true end
20         end)
21         uci:foreach("luci_splash", "whitelist", function(s)
22                 if s.mac and s.mac:lower() == mac then access = true end
23         end)
24
25         if #mac > 0 and access then
26                 luci.http.redirect(luci.dispatcher.build_url())
27         else
28                 luci.http.redirect(luci.dispatcher.build_url("splash", "splash"))
29         end
30 end
31
32 function action_activate()
33         local ip = luci.http.getenv("REMOTE_ADDR") or "127.0.0.1"
34         local mac = luci.sys.net.ip4mac(ip:match("^[\[::ffff:]*(%d+.%d+%.%d+%.%d+)\]*$"))
35         if mac and luci.http.formvalue("accept") then
36                 os.execute("luci-splash lease "..mac.." >/dev/null 2>&1")
37                 luci.http.redirect(luci.model.uci.cursor():get("freifunk", "community", "homepage"))
38         else
39                 luci.http.redirect(luci.dispatcher.build_url())
40         end
41 end
42
43 function action_status_admin()
44         local uci = luci.model.uci.cursor_state()
45         local macs = luci.http.formvaluetable("save")
46
47         local changes = { 
48                 whitelist = { },
49                 blacklist = { },
50                 lease     = { },
51                 remove    = { }
52         }
53
54         for key, _ in pairs(macs) do
55                 local policy = luci.http.formvalue("policy.%s" % key)
56                 local mac    = luci.http.protocol.urldecode(key)
57
58                 if policy == "whitelist" or policy == "blacklist" then
59                         changes[policy][#changes[policy]+1] = mac
60                 elseif policy == "normal" then
61                         changes["lease"][#changes["lease"]+1] = mac
62                 elseif policy == "kicked" then
63                         changes["remove"][#changes["remove"]+1] = mac
64                 end
65         end
66
67         if #changes.whitelist > 0 then
68                 os.execute("luci-splash whitelist %s >/dev/null"
69                         % table.concat(changes.whitelist))
70         end
71
72         if #changes.blacklist > 0 then
73                 os.execute("luci-splash blacklist %s >/dev/null"
74                         % table.concat(changes.blacklist))
75         end
76
77         if #changes.lease > 0 then
78                 os.execute("luci-splash lease %s >/dev/null"
79                         % table.concat(changes.lease))
80         end
81
82         if #changes.remove > 0 then
83                 os.execute("luci-splash remove %s >/dev/null"
84                         % table.concat(changes.remove))
85         end
86
87         luci.template.render("admin_status/splash", { is_admin = true })
88 end