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