Merge pull request #278 from nmav/ocserv
[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 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
61 end)
62 if blacklisted then
63 luci.http.redirect(luci.dispatcher.build_url("splash" ,"blocked"))
64 else
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")
68 end
69 if not redirect_url then
70 redirect_url = luci.model.uci.cursor():get("freifunk", "community", "homepage") or 'http://www.freifunk.net'
71 end
72 remove_redirect(mac:gsub(':', ''):lower())
73 os.execute("luci-splash lease "..mac.." >/dev/null 2>&1")
74 luci.http.redirect(redirect_url)
75 end
76 else
77 luci.http.redirect(luci.dispatcher.build_url())
78 end
79 end
80
81 function action_status_admin()
82 local uci = luci.model.uci.cursor_state()
83 local macs = luci.http.formvaluetable("save")
84
85 local changes = {
86 whitelist = { },
87 blacklist = { },
88 lease = { },
89 remove = { }
90 }
91
92 for key, _ in pairs(macs) do
93 local policy = luci.http.formvalue("policy.%s" % key)
94 local mac = luci.http.protocol.urldecode(key)
95
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
102 end
103 end
104
105 if #changes.whitelist > 0 then
106 os.execute("luci-splash whitelist %s >/dev/null"
107 % table.concat(changes.whitelist))
108 end
109
110 if #changes.blacklist > 0 then
111 os.execute("luci-splash blacklist %s >/dev/null"
112 % table.concat(changes.blacklist))
113 end
114
115 if #changes.lease > 0 then
116 os.execute("luci-splash lease %s >/dev/null"
117 % table.concat(changes.lease))
118 end
119
120 if #changes.remove > 0 then
121 os.execute("luci-splash remove %s >/dev/null"
122 % table.concat(changes.remove))
123 end
124
125 luci.template.render("admin_status/splash", { is_admin = true })
126 end
127
128 function action_status_public()
129 luci.template.render("admin_status/splash", { is_admin = false })
130 end
131
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")
139 -- For all redirects
140 for k, v in pairs(redirects) do
141 if v[".type"] == "redirect" then
142 if v[".name"] ~= mac then
143 -- Rewrite state
144 uci:section("luci_splash_locations", "redirect", v[".name"], {
145 location = v.location
146 })
147 end
148 end
149 end
150 uci:save("luci_splash_redirects")
151 end