applications/luci-splash: merge splash rework to trunk
[project/luci.git] / applications / luci-splash / luasrc / view / admin_status / splash.htm
1 <%#
2 LuCI - Lua Configuration Interface
3 Copyright 2009 Jo-Philipp Wich <xm@leipzig.freifunk.net>
4
5 Licensed under the Apache License, Version 2.0 (the "License");
6 you may not use this file except in compliance with the License.
7 You may obtain a copy of the License at
8
9         http://www.apache.org/licenses/LICENSE-2.0
10
11 $Id$
12
13 -%>
14
15 <%-
16
17 local utl = require "luci.util"
18 local ipt = require "luci.sys.iptparser".IptParser()
19 local uci = require "luci.model.uci".cursor_state()
20 local wat = require "luci.tools.webadmin"
21 local clients = { }
22 local leasetime = tonumber(uci:get("luci_splash", "general", "leasetime") or 1) * 60 * 60
23 local leasefile = "/tmp/dhcp.leases"
24
25 uci:foreach("dhcp", "dnsmasq",
26         function(s)
27                 if s.leasefile then leasefile = s.leasefile end
28         end)
29
30
31 uci:foreach("luci_splash", "lease",
32         function(s)
33                 if s.start and s.mac then
34                         clients[s.mac:lower()] = {
35                                 start   = tonumber(s.start),
36                                 limit   = ( tonumber(s.start) + leasetime ),
37                                 mac     = s.mac:upper(),
38                                 policy  = "normal",
39                                 packets = 0,
40                                 bytes   = 0,
41                                 kicked  = s.kicked and true or false
42                         }
43                 end
44         end)
45
46 for _, r in ipairs(ipt:find({table="nat", chain="luci_splash_leases"})) do
47         if r.options and #r.options >= 2 and r.options[1] == "MAC" then
48                 if not clients[r.options[2]:lower()] then
49                         clients[r.options[2]:lower()] = {
50                                 start  = 0,
51                                 limit  = 0,
52                                 mac    = r.options[2]:upper(),
53                                 policy = ( r.target == "RETURN" ) and "whitelist" or "blacklist",
54                                 packets = 0,
55                                 bytes   = 0
56                         }
57                 end
58         end
59 end
60
61 for _, r in ipairs(ipt:find({table="filter", chain="luci_splash_counter"})) do
62         if r.options and #r.options >= 2 and r.options[1] == "MAC" then
63                 local c = clients[r.options[2]:lower()]
64                 if c and c.packets == 0 then
65                         c.bytes   = tonumber(r.bytes)
66                         c.packets = tonumber(r.packets)
67                 end
68         end
69 end
70
71 uci:foreach("luci_splash", "whitelist",
72         function(s)
73                 if s.mac and clients[s.mac:lower()] then
74                         clients[s.mac:lower()].policy="whitelist"
75                 end
76         end)
77
78 uci:foreach("luci_splash", "blacklist",
79         function(s)
80                 if s.mac and clients[s.mac:lower()] then
81                         clients[s.mac:lower()].policy=(s.kicked and "kicked" or "blacklist")
82                 end
83         end)            
84
85 if luci.fs.access(leasefile) then
86         for l in io.lines(leasefile) do
87                 local time, mac, ip, name = l:match("^(%d+) (%S+) (%S+) (%S+)")
88                 if time and mac and ip then
89                         local c = clients[mac:lower()]
90                         if c then
91                                 c.ip = ip
92                                 c.hostname = ( name ~= "*" ) and name or nil
93                         end
94                 end
95         end
96 end
97
98 for i, a in ipairs(luci.sys.net.arptable()) do
99         local c = clients[a["HW address"]:lower()]
100         if c and not c.ip then
101                 c.ip = a["IP address"]
102         end
103 end
104
105 local function showmac(mac)
106         if not is_admin then
107                 mac = mac:gsub("(%S%S:%S%S):%S%S:%S%S:(%S%S:%S%S)", "%1:XX:XX:%2")
108         end
109         return mac
110 end
111
112 -%>
113
114 <%+header%>
115
116 <div id="cbi-splash-leases" class="cbi-map">
117         <h2><a id="content" name="content"><%:ff_splash Client-Splash%></a></h2>
118         <fieldset id="cbi-table-table" class="cbi-section">
119                 <legend><%:ff_splash_clients Active Clients%></legend>
120                 <div class="cbi-section-node">
121                         <% if is_admin then %><form action="<%=REQUEST_URI%>" method="post"><% end %>
122                         <table class="cbi-section-table">
123                                 <tr class="cbi-section-table-titles">
124                                         <th class="cbi-section-table-cell"><%:ff_splash_hostname Hostname%></th>
125                                         <th class="cbi-section-table-cell"><%:ff_splash_ip IP Address%></th>
126                                         <th class="cbi-section-table-cell"><%:ff_splash_mac MAC Address%></th>
127                                         <th class="cbi-section-table-cell"><%:ff_splash_timeleft Time remaining%></th>
128                                         <th class="cbi-section-table-cell"><%:ff_splash_traffic Outgoing traffic%></th>
129                                         <th class="cbi-section-table-cell"><%:ff_splash_policy Policy%></th>
130                                 </tr>
131
132                                 <%-
133                                         local count = 0
134                                         for _, c in utl.spairs(clients,
135                                                 function(a,b)
136                                                         if clients[a].policy == clients[b].policy then
137                                                                 return (clients[a].start > clients[b].start)
138                                                         else
139                                                                 return (clients[a].policy > clients[b].policy)
140                                                         end
141                                                 end)
142                                         do
143                                                 if c.ip then
144                                                         count = count + 1
145                                 -%>
146                                         <tr class="cbi-section-table-row cbi-rowstyle-<%=2-(count%2)%>">
147                                                 <td class="cbi-section-table-cell"><%=c.hostname or "<em>" .. translate("ff_splash_unknown", "unknown") .. "</em>"%></td>
148                                                 <td class="cbi-section-table-cell"><%=c.ip or "<em>" .. translate("ff_splash_unknown", "unknown") .. "</em>"%></td>
149                                                 <td class="cbi-section-table-cell"><%=showmac(c.mac)%></td>
150                                                 <td class="cbi-section-table-cell"><%=
151                                                         (c.limit >= os.time()) and wat.date_format(c.limit-os.time()) or
152                                                                 (c.policy ~= "normal") and "-" or "<em>" .. translate("ff_splash_expired", "expired") .. "</em>"
153                                                 %></td>
154                                                 <td class="cbi-section-table-cell"><%=wat.byte_format(c.bytes)%></td>
155                                                 <td class="cbi-section-table-cell">
156                                                         <% if is_admin then %>
157                                                         <select name="policy.<%=c.mac:lower()%>" style="width:200px">
158                                                                 <option value="whitelist"<%=c.policy=="whitelist" and ' selected="selected"'%>><%:ff_splash_whitelisted whitelisted%></option>
159                                                                 <option value="normal"<%=c.policy=="normal" and not c.kicked and ' selected="selected"'%>><%:ff_splash_splashed splashed%></option>
160                                                                 <option value="blacklist"<%=c.policy=="blacklist" and ' selected="selected"'%>><%:ff_splash_blacklisted blacklisted%></option>
161                                                                 <% if c.policy == "normal" then -%>
162                                                                 <option value="kick"<%=c.kicked and ' selected="selected"'%>><%:ff_splash_tempblock temporarily blocked%> (<%=wat.date_format(c.limit-os.time())%>)</option>
163                                                                 <%- end %>
164                                                         </select>
165                                                         <input type="submit" class="cbi-button cbi-button-save" name="save.<%=c.mac:lower()%>" value="<%:save Save%>" />
166                                                         <% else %>
167                                                         <%=c.policy%>
168                                                         <% end %>
169                                                 </td>
170                                         </tr>
171                                 <%- 
172                                                 end
173                                         end
174
175                                         if count == 0 then
176                                 -%>
177                                         <tr class="cbi-section-table-row">
178                                                 <td colspan="7" class="cbi-section-table-cell">
179                                                         <br /><em><%:ff_splash_noclients No clients connected%></em><br />
180                                                 </td>
181                                         </tr>
182                                 <%- end -%>
183                         </table>
184                         <% if is_admin then %></form><% end %>
185                 </div>
186         </fieldset>
187 </div>
188
189 <%+footer%>