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.
5 module("luci.tools.webadmin", package.seeall)
7 local util = require "luci.util"
8 local uci = require "luci.model.uci"
9 local ip = require "luci.ip"
11 function byte_format(byte)
12 local suff = {"B", "KB", "MB", "GB", "TB"}
14 if byte > 1024 and i < 5 then
17 return string.format("%.2f %s", byte, suff[i])
22 function date_format(secs)
23 local suff = {"min", "h", "d"}
28 secs = math.floor(secs)
30 mins = math.floor(secs / 60)
35 hour = math.floor(mins / 60)
40 days = math.floor(hour / 24)
45 return string.format("%.0fd %02.0fh %02.0fmin %02.0fs", days, hour, mins, secs)
47 return string.format("%02.0fh %02.0fmin %02.0fs", hour, mins, secs)
51 function cbi_add_networks(field)
52 uci.cursor():foreach("network", "interface",
54 if section[".name"] ~= "loopback" then
55 field:value(section[".name"])
59 field.titleref = luci.dispatcher.build_url("admin", "network", "network")
62 function cbi_add_knownips(field)
64 for _, n in ipairs(ip.neighbors({ family = 4 })) do
66 field:value(n.dest:string())
71 function firewall_find_zone(name)
74 luci.model.uci.cursor():foreach("firewall", "zone",
76 if section.name == name then
77 find = section[".name"]
85 function iface_get_network(iface)
86 local link = ip.link(tostring(iface))
91 local cur = uci.cursor()
92 local dump = util.ubus("network.interface", "dump", { })
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