0f7612ff766542a6fb946071360522287aca3601
[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         if secs > 60 then
38                 mins = math.floor(secs / 60)
39                 secs = secs % 60
40         end
41         
42         if mins > 60 then
43                 hour = math.floor(mins / 60)
44                 mins = mins % 60
45         end
46         
47         if hour > 24 then
48                 days = math.floor(hours / 24)
49                 hour = hour % 24
50         end
51         
52         if days > 0 then
53                 return string.format("%dd %02dh %02dmin %02ds", days, hour, mins, secs)
54         else
55                 return string.format("%02dh %02dmin %02ds", hour, mins, secs)
56         end
57 end
58
59 function network_get_addresses(net)
60         local addr = {}
61         local ipv4 = luci.model.uci.get_statevalue("network", net, "ipaddr")
62         local mav4 = luci.model.uci.get_statevalue("network", net, "netmask")
63         local ipv6 = luci.model.uci.get_statevalue("network", net, "ip6addr")
64         
65         if ipv4 and mav4 then
66                 ipv4 = luci.ip.IPv4(ipv4, mav4)
67                 
68                 if ipv4 then 
69                         table.insert(addr, ipv4:string())
70                 end
71         end
72
73         if ipv6 then
74                 table.insert(addr, ipv6)
75         end
76         
77         luci.model.uci.foreach("network", "alias",
78                 function (section)
79                         if section.interface == net then
80                                 if section.ipaddr and section.netmask then
81                                         local ipv4 = luci.ip.IPv4(section.ipaddr, section.netmask)
82                                         
83                                         if ipv4 then
84                                                 table.insert(addr, ipv4:string())
85                                         end
86                                 end
87                                 
88                                 if section.ip6addr then
89                                         table.insert(addr, section.ip6addr)
90                                 end
91                         end
92                 end
93         )
94         
95         return addr
96 end
97
98 function cbi_add_networks(field)
99         luci.model.uci.foreach("network", "interface",
100                 function (section)
101                         if section[".name"] ~= "loopback" then
102                                 field:value(section[".name"])
103                         end
104                 end
105         )
106         field.titleref = luci.dispatcher.build_url("admin", "network", "network")
107 end
108
109 function cbi_add_knownips(field)
110         for i, dataset in ipairs(luci.sys.net.arptable()) do
111                 field:value(dataset["IP address"])
112         end
113 end
114
115 function network_get_zones(net)
116         if not luci.model.uci.load("firewall") then
117                 return nil
118         end
119         
120         local zones = {}
121         
122         luci.model.uci.foreach("firewall", "zone", 
123                 function (section)
124                         local znet = section.network or section.name
125                         if luci.util.contains(luci.util.split(znet, " "), net) then
126                                 table.insert(zones, section.name)
127                         end
128                 end
129         )
130         
131         return zones
132 end
133
134 function firewall_find_zone(name)
135         local find
136         
137         luci.model.uci.foreach("firewall", "zone", 
138                 function (section)
139                         if section.name == name then
140                                 find = section[".name"]
141                         end
142                 end
143         )
144         
145         return find
146 end
147
148 function iface_get_network(iface)
149         local net
150         
151         luci.model.uci.foreach("network", "interface",
152                 function (section)
153                         local ifname = luci.model.uci.get_statevalue(
154                                 "network", section[".name"], "ifname"
155                         )
156                         
157                         if iface == ifname then
158                                 net = section[".name"]
159                         end
160                 end
161         )
162         
163         return net
164 end