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