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