Merge pull request #930 from cshore-firmware/pull-request-fix-webadmin
[project/luci.git] / modules / luci-base / luasrc / tools / webadmin.lua
1 -- Copyright 2008 Steven Barth <steven@midlink.org>
2 -- Copyright 2008-2015 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
7 local util = require "luci.util"
8 local uci  = require "luci.model.uci"
9 local ip   = require "luci.ip"
10
11 function byte_format(byte)
12         local suff = {"B", "KB", "MB", "GB", "TB"}
13         for i=1, 5 do
14                 if byte > 1024 and i < 5 then
15                         byte = byte / 1024
16                 else
17                         return string.format("%.2f %s", byte, suff[i]) 
18                 end 
19         end
20 end
21
22 function date_format(secs)
23         local suff = {"min", "h", "d"}
24         local mins = 0
25         local hour = 0
26         local days = 0
27         
28         secs = math.floor(secs)
29         if secs > 60 then
30                 mins = math.floor(secs / 60)
31                 secs = secs % 60
32         end
33         
34         if mins > 60 then
35                 hour = math.floor(mins / 60)
36                 mins = mins % 60
37         end
38         
39         if hour > 24 then
40                 days = math.floor(hour / 24)
41                 hour = hour % 24
42         end
43         
44         if days > 0 then
45                 return string.format("%.0fd %02.0fh %02.0fmin %02.0fs", days, hour, mins, secs)
46         else
47                 return string.format("%02.0fh %02.0fmin %02.0fs", hour, mins, secs)
48         end
49 end
50
51 function cbi_add_networks(field)
52         uci.cursor():foreach("network", "interface",
53                 function (section)
54                         if section[".name"] ~= "loopback" then
55                                 field:value(section[".name"])
56                         end
57                 end
58         )
59         field.titleref = luci.dispatcher.build_url("admin", "network", "network")
60 end
61
62 function cbi_add_knownips(field)
63         local _, n
64         for _, n in ipairs(ip.neighbors({ family = 4 })) do
65                 if n.dest then
66                         field:value(n.dest:string())
67                 end
68         end
69 end
70
71 function firewall_find_zone(name)
72         local find
73         
74         luci.model.uci.cursor():foreach("firewall", "zone", 
75                 function (section)
76                         if section.name == name then
77                                 find = section[".name"]
78                         end
79                 end
80         )
81         
82         return find
83 end
84
85 function iface_get_network(iface)
86         local link = ip.link(tostring(iface))
87         if link.master then
88                 iface = link.master
89         end
90
91         local cur = uci.cursor()
92         local dump = util.ubus("network.interface", "dump", { })
93         if dump then
94                 local _, net
95                 for _, net in ipairs(dump.interface) do
96                         if net.l3_device == iface or net.device == iface then
97                                 -- cross check with uci to filter out @name style aliases
98                                 local uciname = cur:get("network", net.interface, "ifname")
99                                 if type(uciname) == "string" and uciname:sub(1,1) ~= "@" or uciname then
100                                         return net.interface
101                                 end
102                         end
103                 end
104         end
105 end