modules/admin-core: implement a more elegant solution for #46; treat empty or zero...
[project/luci.git] / modules / admin-core / luasrc / tools / webadmin.lua
1 --[[
2 LuCI - Lua Configuration Interface
3
4 Copyright 2008 Steven Barth <steven@midlink.org>
5 Copyright 2008 Jo-Philipp Wich <xm@leipzig.freifunk.net>
6
7 Licensed under the Apache License, Version 2.0 (the "License");
8 you may not use this file except in compliance with the License.
9 You may obtain a copy of the License at
10
11         http://www.apache.org/licenses/LICENSE-2.0
12
13 $Id$
14 ]]--
15
16 module("luci.tools.webadmin", package.seeall)
17 local uci = require("luci.model.uci")
18 require("luci.sys")
19 require("luci.ip")
20
21 function byte_format(byte)
22         local suff = {"B", "KB", "MB", "GB", "TB"}
23         for i=1, 5 do
24                 if byte > 1024 and i < 5 then
25                         byte = byte / 1024
26                 else
27                         return string.format("%.2f %s", byte, suff[i]) 
28                 end 
29         end
30 end
31
32 function date_format(secs)
33         local suff = {"min", "h", "d"}
34         local mins = 0
35         local hour = 0
36         local days = 0
37         
38         secs = math.floor(secs)
39         if secs > 60 then
40                 mins = math.floor(secs / 60)
41                 secs = secs % 60
42         end
43         
44         if mins > 60 then
45                 hour = math.floor(mins / 60)
46                 mins = mins % 60
47         end
48         
49         if hour > 24 then
50                 days = math.floor(hour / 24)
51                 hour = hour % 24
52         end
53         
54         if days > 0 then
55                 return string.format("%.0fd %02.0fh %02.0fmin %02.0fs", days, hour, mins, secs)
56         else
57                 return string.format("%02.0fh %02.0fmin %02.0fs", hour, mins, secs)
58         end
59 end
60
61 function network_get_addresses(net)
62         local state = uci.cursor_state()
63         state:load("network")
64         local addr = {}
65         local ipv4 = state:get("network", net, "ipaddr")
66         local mav4 = state:get("network", net, "netmask")
67         local ipv6 = state:get("network", net, "ip6addr")
68         
69         if ipv4 and #ipv4 > 0 then
70                 if mav4 and #mav4 == 0 then mav4 = nil end
71
72                 ipv4 = luci.ip.IPv4(ipv4, mav4)
73                 
74                 if ipv4 then 
75                         table.insert(addr, ipv4:string())
76                 end
77         end
78
79         if ipv6 then
80                 table.insert(addr, ipv6)
81         end
82         
83         state:foreach("network", "alias",
84                 function (section)
85                         if section.interface == net then
86                                 if section.ipaddr and section.netmask then
87                                         local ipv4 = luci.ip.IPv4(section.ipaddr, section.netmask)
88                                         
89                                         if ipv4 then
90                                                 table.insert(addr, ipv4:string())
91                                         end
92                                 end
93                                 
94                                 if section.ip6addr then
95                                         table.insert(addr, section.ip6addr)
96                                 end
97                         end
98                 end
99         )
100         
101         return addr
102 end
103
104 function cbi_add_networks(field)
105         uci.cursor():foreach("network", "interface",
106                 function (section)
107                         if section[".name"] ~= "loopback" then
108                                 field:value(section[".name"])
109                         end
110                 end
111         )
112         field.titleref = luci.dispatcher.build_url("admin", "network", "network")
113 end
114
115 function cbi_add_knownips(field)
116         for i, dataset in ipairs(luci.sys.net.arptable()) do
117                 field:value(dataset["IP address"])
118         end
119 end
120
121 function network_get_zones(net)
122         local state = uci.cursor_state()
123         if not state:load("firewall") then
124                 return nil
125         end
126         
127         local zones = {}
128         
129         state:foreach("firewall", "zone", 
130                 function (section)
131                         local znet = section.network or section.name
132                         if luci.util.contains(luci.util.split(znet, " "), net) then
133                                 table.insert(zones, section.name)
134                         end
135                 end
136         )
137         
138         return zones
139 end
140
141 function firewall_find_zone(name)
142         local find
143         
144         luci.model.uci.cursor():foreach("firewall", "zone", 
145                 function (section)
146                         if section.name == name then
147                                 find = section[".name"]
148                         end
149                 end
150         )
151         
152         return find
153 end
154
155 function iface_get_network(iface)
156         local state = uci.cursor_state()
157         state:load("network")
158         local net
159         
160         state:foreach("network", "interface",
161                 function (section)
162                         local ifname = state:get(
163                                 "network", section[".name"], "ifname"
164                         )
165                         
166                         if iface == ifname then
167                                 net = section[".name"]
168                         end
169                 end
170         )
171         
172         return net
173 end