* Merged Luci to use native UCI-library
[project/luci.git] / applications / luci-splash / root / usr / lib / luci-splash / htdocs / cgi-bin / index.cgi
1 #!/usr/bin/haserl --shell=luac
2
3 require("luci.http")
4 require("luci.sys")
5 require("luci.model.uci")
6
7 luci.model.uci.set_savedir(luci.model.uci.savedir_state)
8
9 local srv
10 local net
11 local ip = luci.http.env.REMOTE_ADDR
12 luci.model.uci.foreach("network", "interface",
13         function (section)
14                 if section.ipaddr then
15                         local p = luci.sys.net.mask4prefix(section.netmask)
16                         if luci.sys.net.belongs(ip, section.ipaddr, p) then
17                                 net = section[".name"]
18                                 srv = section.ipaddr
19                                 return
20                         end
21                 end
22         end)
23
24 local stat = false
25 luci.model.uci.foreach("luci_splash", "iface",
26         function (section)
27                 if section.network == net then
28                         stat = true
29                 end
30         end)
31
32 if not srv then
33         luci.http.prepare_content("text/plain")
34         print("Unable to detect network settings!")
35 elseif not stat then
36         luci.http.redirect("http://" .. srv)
37 else
38         local action = "splash"
39         
40         local mac = luci.sys.net.ip4mac(ip)
41         if not mac then
42                 action = "unknown"
43         end
44         
45         local status = luci.sys.execl("luci-splash status "..mac)[1]
46         
47         if status == "whitelisted" or status == "lease" then
48                 action = "allowed"
49         end
50         
51         luci.http.redirect("http://" .. srv .. "/cgi-bin/luci-splash/" .. action)
52 end