root/ff-luci/trunk/modules/admin-core/luasrc/tools/webadmin.lua @ 2862

Revision 2862, 3.3 KB (checked in by Cyrus, 5 years ago)

Add missing number conversion (fixed #108)

Line 
1--[[
2LuCI - Lua Configuration Interface
3
4Copyright 2008 Steven Barth <steven@midlink.org>
5Copyright 2008 Jo-Philipp Wich <xm@leipzig.freifunk.net>
6
7Licensed under the Apache License, Version 2.0 (the "License");
8you may not use this file except in compliance with the License.
9You may obtain a copy of the License at
10
11    http://www.apache.org/licenses/LICENSE-2.0
12
13$Id$
14]]--
15
16module("luci.tools.webadmin", package.seeall)
17require("luci.model.uci")
18require("luci.sys")
19require("luci.ip")
20
21function 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
30end
31
32function 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(tonumber(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("%dd %02dh %02dmin %02ds", days, hour, mins, secs)
56    else
57        return string.format("%02dh %02dmin %02ds", hour, mins, secs)
58    end
59end
60
61function 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
99end
100
101function 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")
110end
111
112function cbi_add_knownips(field)
113    for i, dataset in ipairs(luci.sys.net.arptable()) do
114        field:value(dataset["IP address"])
115    end
116end
117
118function 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
135end
136
137function 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
149end
150
151function 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
168end
Note: See TracBrowser for help on using the browser.