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