modules/admin-full: cope with missing wireless or dnsmasq config, hide menu points...
[project/luci.git] / modules / admin-full / luasrc / controller / admin / network.lua
1 --[[
2 LuCI - Lua Configuration Interface
3
4 Copyright 2008 Steven Barth <steven@midlink.org>
5
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 $Id$
13 ]]--
14 module("luci.controller.admin.network", package.seeall)
15
16 function index()
17 require("luci.i18n")
18 local uci = require("luci.model.uci").cursor()
19 local i18n = luci.i18n.translate
20 local has_wifi = nixio.fs.stat("/etc/config/wireless")
21
22 local page = node("admin", "network")
23 page.target = alias("admin", "network", "network")
24 page.title = i18n("Network")
25 page.order = 50
26 page.index = true
27
28 local page = node("admin", "network", "vlan")
29 page.target = cbi("admin_network/vlan")
30 page.title = i18n("Switch")
31 page.order = 20
32
33 if has_wifi and has_wifi.size > 0 then
34 local page = entry({"admin", "network", "wireless"}, arcombine(template("admin_network/wifi_overview"), cbi("admin_network/wifi")), i18n("Wifi"), 15)
35 page.leaf = true
36 page.subindex = true
37
38 local page = entry({"admin", "network", "wireless_join"}, call("wifi_join"), nil, 16)
39 page.leaf = true
40
41 local page = entry({"admin", "network", "wireless_delete"}, call("wifi_delete"), nil, 16)
42 page.leaf = true
43 end
44
45 local page = entry({"admin", "network", "network"}, arcombine(cbi("admin_network/network"), cbi("admin_network/ifaces")), i18n("Interfaces"), 10)
46 page.leaf = true
47 page.subindex = true
48
49 local page = entry({"admin", "network", "add"}, cbi("admin_network/iface_add"), nil)
50 page.leaf = true
51
52 uci:foreach("network", "interface",
53 function (section)
54 local ifc = section[".name"]
55 if ifc ~= "loopback" then
56 entry({"admin", "network", "network", ifc},
57 true,
58 ifc:upper())
59 end
60 end
61 )
62
63 if nixio.fs.access("/etc/config/dhcp") then
64 local page = node("admin", "network", "dhcpleases")
65 page.target = cbi("admin_network/dhcpleases")
66 page.title = i18n("DHCP Leases")
67 page.order = 30
68 end
69
70 local page = node("admin", "network", "hosts")
71 page.target = cbi("admin_network/hosts")
72 page.title = i18n("Hostnames")
73 page.order = 40
74
75 local page = node("admin", "network", "routes")
76 page.target = cbi("admin_network/routes")
77 page.title = i18n("Static Routes")
78 page.order = 50
79
80 end
81
82 function wifi_join()
83 local function param(x)
84 return luci.http.formvalue(x)
85 end
86
87 local function ptable(x)
88 x = param(x)
89 return x and (type(x) ~= "table" and { x } or x) or {}
90 end
91
92 local dev = param("device")
93 local ssid = param("join")
94
95 if dev and ssid then
96 local cancel = (param("cancel") or param("cbi.cancel")) and true or false
97
98 if cancel then
99 luci.http.redirect(luci.dispatcher.build_url("admin/network/wireless_join?device=" .. dev))
100 else
101 local cbi = require "luci.cbi"
102 local tpl = require "luci.template"
103 local map = luci.cbi.load("admin_network/wifi_add")[1]
104
105 if map:parse() ~= cbi.FORM_DONE then
106 tpl.render("header")
107 map:render()
108 tpl.render("footer")
109 end
110 end
111 else
112 luci.template.render("admin_network/wifi_join")
113 end
114 end
115
116 function wifi_delete(network)
117 local uci = require "luci.model.uci".cursor()
118 local wlm = require "luci.model.wireless"
119
120 wlm.init(uci)
121 wlm:del_network(network)
122
123 uci:save("wireless")
124 luci.http.redirect(luci.dispatcher.build_url("admin/network/wireless"))
125 end