#108
[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 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         luci.model.uci.load_state("network")
63         local addr = {}
64         local ipv4 = luci.model.uci.get("network", net, "ipaddr")
65         local mav4 = luci.model.uci.get("network", net, "netmask")
66         local ipv6 = luci.model.uci.get("network", net, "ip6addr")
67         
68         if ipv4 and mav4 then
69                 ipv4 = luci.ip.IPv4(ipv4, mav4)
70                 
71                 if ipv4 then 
72                         table.insert(addr, ipv4:string())
73                 end
74         end
75
76         if ipv6 then
77                 table.insert(addr, ipv6)
78         end
79         
80         luci.model.uci.foreach("network", "alias",
81                 function (section)
82                         if section.interface == net then
83                                 if section.ipaddr and section.netmask then
84                                         local ipv4 = luci.ip.IPv4(section.ipaddr, section.netmask)
85                                         
86                                         if ipv4 then
87                                                 table.insert(addr, ipv4:string())
88                                         end
89                                 end
90                                 
91                                 if section.ip6addr then
92                                         table.insert(addr, section.ip6addr)
93                                 end
94                         end
95                 end
96         )
97         
98         return addr
99 end
100
101 function cbi_add_networks(field)
102         luci.model.uci.foreach("network", "interface",
103                 function (section)
104                         if section[".name"] ~= "loopback" then
105                                 field:value(section[".name"])
106                         end
107                 end
108         )
109         field.titleref = luci.dispatcher.build_url("admin", "network", "network")
110 end
111
112 function cbi_add_knownips(field)
113         for i, dataset in ipairs(luci.sys.net.arptable()) do
114                 field:value(dataset["IP address"])
115         end
116 end
117
118 function network_get_zones(net)
119         if not luci.model.uci.load_state("firewall") then
120                 return nil
121         end
122         
123         local zones = {}
124         
125         luci.model.uci.foreach("firewall", "zone", 
126                 function (section)
127                         local znet = section.network or section.name
128                         if luci.util.contains(luci.util.split(znet, " "), net) then
129                                 table.insert(zones, section.name)
130                         end
131                 end
132         )
133         
134         return zones
135 end
136
137 function firewall_find_zone(name)
138         local find
139         
140         luci.model.uci.foreach("firewall", "zone", 
141                 function (section)
142                         if section.name == name then
143                                 find = section[".name"]
144                         end
145                 end
146         )
147         
148         return find
149 end
150
151 function iface_get_network(iface)
152         luci.model.uci.load_state("network")
153         local net
154         
155         luci.model.uci.foreach("network", "interface",
156                 function (section)
157                         local ifname = luci.model.uci.get(
158                                 "network", section[".name"], "ifname"
159                         )
160                         
161                         if iface == ifname then
162                                 net = section[".name"]
163                         end
164                 end
165         )
166         
167         return net
168 end