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