1 module("luci.controller.splash.splash", package.seeall)
3 local uci = luci.model.uci.cursor()
4 local util = require "luci.util"
7 entry({"admin", "services", "splash"}, cbi("splash/splash"), _("Client-Splash"), 90)
8 entry({"admin", "services", "splash", "splashtext" }, form("splash/splashtext"), _("Splashtext"), 10)
13 e.target = call("action_dispatch")
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")
19 entry({"admin", "status", "splash"}, call("action_status_admin"), _("Client-Splash"))
21 local page = node("splash", "publicstatus")
22 page.target = call("action_status_public")
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 ""
31 uci:foreach("luci_splash", "lease", function(s)
32 if s.mac and s.mac:lower() == mac then access = true end
34 uci:foreach("luci_splash", "whitelist", function(s)
35 if s.mac and s.mac:lower() == mac then access = true end
38 if #mac > 0 and access then
39 luci.http.redirect(luci.dispatcher.build_url())
41 luci.http.redirect(luci.dispatcher.build_url("splash", "splash"))
47 uci:foreach("luci_splash", "blacklist",
48 function(s) leased_macs[s.mac:lower()] = true
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 uci_state = require "luci.model.uci".cursor_state()
57 local blacklisted = false
58 if mac and luci.http.formvalue("accept") then
59 uci:foreach("luci_splash", "blacklist",
60 function(s) if s.mac:lower() == mac or s.mac == mac then blacklisted = true end
63 luci.http.redirect(luci.dispatcher.build_url("splash" ,"blocked"))
65 local redirect_url = uci:get("luci_splash", "general", "redirect_url")
66 if not redirect_url then
67 redirect_url = uci_state:get("luci_splash_locations", mac:gsub(':', ''):lower(), "location")
69 if not redirect_url then
70 redirect_url = luci.model.uci.cursor():get("freifunk", "community", "homepage") or 'http://www.freifunk.net'
72 remove_redirect(mac:gsub(':', ''):lower())
73 os.execute("luci-splash lease "..mac.." >/dev/null 2>&1")
74 luci.http.redirect(redirect_url)
77 luci.http.redirect(luci.dispatcher.build_url())
81 function action_status_admin()
82 local uci = luci.model.uci.cursor_state()
83 local macs = luci.http.formvaluetable("save")
92 for key, _ in pairs(macs) do
93 local policy = luci.http.formvalue("policy.%s" % key)
94 local mac = luci.http.protocol.urldecode(key)
96 if policy == "whitelist" or policy == "blacklist" then
97 changes[policy][#changes[policy]+1] = mac
98 elseif policy == "normal" then
99 changes["lease"][#changes["lease"]+1] = mac
100 elseif policy == "kicked" then
101 changes["remove"][#changes["remove"]+1] = mac
105 if #changes.whitelist > 0 then
106 os.execute("luci-splash whitelist %s >/dev/null"
107 % table.concat(changes.whitelist))
110 if #changes.blacklist > 0 then
111 os.execute("luci-splash blacklist %s >/dev/null"
112 % table.concat(changes.blacklist))
115 if #changes.lease > 0 then
116 os.execute("luci-splash lease %s >/dev/null"
117 % table.concat(changes.lease))
120 if #changes.remove > 0 then
121 os.execute("luci-splash remove %s >/dev/null"
122 % table.concat(changes.remove))
125 luci.template.render("admin_status/splash", { is_admin = true })
128 function action_status_public()
129 luci.template.render("admin_status/splash", { is_admin = false })
132 function remove_redirect(mac)
133 local mac = mac:lower()
134 mac = mac:gsub(":", "")
135 local uci = require "luci.model.uci".cursor_state()
136 local redirects = uci:get_all("luci_splash_locations")
137 --uci:load("luci_splash_locations")
138 uci:revert("luci_splash_locations")
140 for k, v in pairs(redirects) do
141 if v[".type"] == "redirect" then
142 if v[".name"] ~= mac then
144 uci:section("luci_splash_locations", "redirect", v[".name"], {
145 location = v.location
150 uci:save("luci_splash_redirects")