11430f0acc368736266b36d8bedb46e19380ee14
[project/luci.git] / applications / luci-splash / root / usr / lib / luci-splash / htdocs / cgi-bin / index.cgi
1 #!/usr/bin/haserl --shell=luac
2 package.path = "/usr/lib/lua/?.lua;/usr/lib/lua/?/init.lua;" .. package.path
3 package.cpath = "/usr/lib/lua/?.so;" .. package.cpath
4
5 require("ffluci.http")
6 require("ffluci.sys")
7 require("ffluci.model.uci")
8
9 local srv
10 local net
11 local ip = ffluci.http.env.REMOTE_ADDR
12 for k, v in pairs(ffluci.model.uci.sections("network")) do
13 if v[".type"] == "interface" and v.ipaddr then
14 local p = ffluci.sys.net.mask4prefix(v.netmask)
15 if ffluci.sys.net.belongs(ip, v.ipaddr, p) then
16 net = k
17 srv = v.ipaddr
18 break
19 end
20 end
21 end
22
23 local stat = false
24 for k, v in pairs(ffluci.model.uci.sections("luci_splash")) do
25 if v[".type"] == "iface" and v.network == net then
26 stat = true
27 end
28 end
29
30 if not srv then
31 ffluci.http.prepare_content("text/plain")
32 print("Unable to detect network settings!")
33 elseif not stat then
34 ffluci.http.redirect("http://" .. srv)
35 else
36 local action = "splash"
37
38 local mac = ffluci.sys.net.ip4mac(ip)
39 if not mac then
40 action = "unknown"
41 end
42
43 local status = ffluci.sys.execl("luci-splash status "..mac)[1]
44
45 if status == "whitelisted" or status == "lease" then
46 action = "allowed"
47 end
48
49 ffluci.http.redirect("http://" .. srv .. "/cgi-bin/luci-splash/" .. action)
50 end