1 module("luci.controller.splash.splash", package.seeall)
3 local uci = luci.model.uci.cursor()
4 local util = require "luci.util"
5 local ipc = require "luci.ip"
8 entry({"admin", "services", "splash"}, cbi("splash/splash"), _("Client-Splash"), 90)
9 entry({"admin", "services", "splash", "splashtext" }, form("splash/splashtext"), _("Splashtext"), 10)
14 e.target = call("action_dispatch")
16 node("splash", "activate").target = call("action_activate")
17 node("splash", "splash").target = template("splash_splash/splash")
18 node("splash", "blocked").target = template("splash/blocked")
20 entry({"admin", "status", "splash"}, post("action_status_admin"), _("Client-Splash"))
22 local page = node("splash", "publicstatus")
23 page.target = call("action_status_public")
27 function ip_to_mac(ip)
29 for i, n in ipairs(ipc.neighbors({ dest = ip })) do
30 local mac = ipc.checkmac(n.mac)
31 if mac then return mac end
35 function action_dispatch()
36 local uci = luci.model.uci.cursor_state()
37 local mac = ip_to_mac(luci.http.getenv("REMOTE_ADDR"))
41 uci:foreach("luci_splash", "lease", function(s)
42 if ipc.checkmac(s.mac) == mac then
48 uci:foreach("luci_splash", "whitelist", function(s)
49 if ipc.checkmac(s.mac) == mac then
57 luci.http.redirect(luci.dispatcher.build_url())
59 luci.http.redirect(luci.dispatcher.build_url("splash", "splash"))
65 uci:foreach("luci_splash", "blacklist", function(s)
66 local m = ipc.checkmac(s.mac)
67 if m then leased_macs[m] = true end
72 function action_activate()
73 local ipc = require "luci.ip"
74 local mac = ip_to_mac(luci.http.getenv("REMOTE_ADDR") or "127.0.0.1")
75 local uci_state = require "luci.model.uci".cursor_state()
76 local blacklisted = false
77 if mac and luci.http.formvalue("accept") then
78 uci:foreach("luci_splash", "blacklist", function(s)
79 if ipc.checkmac(s.mac) == mac then
86 luci.http.redirect(luci.dispatcher.build_url("splash" ,"blocked"))
88 local id = tostring(mac):gsub(':', ''):lower()
89 local redirect_url = uci:get("luci_splash", "general", "redirect_url")
90 if not redirect_url then
91 redirect_url = uci_state:get("luci_splash_locations", id, "location")
93 if not redirect_url then
94 redirect_url = luci.model.uci.cursor():get("freifunk", "community", "homepage") or 'http://www.freifunk.net'
97 os.execute("luci-splash lease "..tostring(mac).." >/dev/null 2>&1")
98 luci.http.redirect(redirect_url)
101 luci.http.redirect(luci.dispatcher.build_url())
105 function action_status_admin()
106 local uci = luci.model.uci.cursor_state()
107 local macs = luci.http.formvaluetable("save")
117 for key, _ in pairs(macs) do
118 local policy = luci.http.formvalue("policy.%s" % key)
119 local mac = luci.http.protocol.urldecode(key)
121 if policy == "whitelist" or policy == "blacklist" then
122 changes[policy][#changes[policy]+1] = mac
123 elseif policy == "normal" then
124 changes["lease"][#changes["lease"]+1] = mac
125 elseif policy == "kicked" then
126 changes["remove"][#changes["remove"]+1] = mac
130 if #changes.whitelist > 0 then
131 os.execute("luci-splash whitelist %s >/dev/null"
132 % table.concat(changes.whitelist))
135 if #changes.blacklist > 0 then
136 os.execute("luci-splash blacklist %s >/dev/null"
137 % table.concat(changes.blacklist))
140 if #changes.lease > 0 then
141 os.execute("luci-splash lease %s >/dev/null"
142 % table.concat(changes.lease))
145 if #changes.remove > 0 then
146 os.execute("luci-splash remove %s >/dev/null"
147 % table.concat(changes.remove))
150 luci.template.render("admin_status/splash", { is_admin = true })
153 function action_status_public()
154 luci.template.render("admin_status/splash", { is_admin = false })
157 function remove_redirect(id)
158 local uci = require "luci.model.uci".cursor_state()
159 local redirects = uci:get_all("luci_splash_locations")
160 --uci:load("luci_splash_locations")
161 uci:revert("luci_splash_locations")
165 for k, v in pairs(redirects) do
166 if v[".type"] == "redirect" then
167 if v[".name"] ~= id then
169 uci:section("luci_splash_locations", "redirect", v[".name"], {
170 location = v.location
176 uci:save("luci_splash_redirects")