* Fixed broken commit
[project/luci.git] / contrib / package / ffluci-splash / src / 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 ip = ffluci.http.remote_addr()
11 for k, v in pairs(ffluci.model.uci.show("network").network) do
12         if v[".type"] == "interface" and v.ipaddr then
13                 local p = ffluci.sys.net.mask4prefix(v.netmask)
14                 if ffluci.sys.net.belongs(ip, v.ipaddr, p) then
15                         srv = v.ipaddr
16                         break
17                 end
18         end
19 end
20
21 if not srv then
22         ffluci.http.textheader()
23         return print("Unable to detect network settings!")
24 end
25
26 local action = "splash"
27
28 local mac = ffluci.sys.net.ip4mac(ip)
29 if not mac then
30         action = "unknown"
31 end
32
33 local status = ffluci.sys.exec("luci-splash status "..mac)
34
35 if status == "whitelisted" or status == "lease" then
36         action = "allowed"
37 end
38
39 ffluci.http.redirect("http://" .. srv .. "/cgi-bin/luci-splash/" .. action)