6155c28f1a7a123268bab92a5f1c6e0fc8c0bd20
[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_add"}, call("wifi_add"), nil, 16)
42                 page.leaf = true
43
44                 local page = entry({"admin", "network", "wireless_delete"}, call("wifi_delete"), nil, 16)
45                 page.leaf = true
46         end
47
48         local page = entry({"admin", "network", "network"}, arcombine(cbi("admin_network/network"), cbi("admin_network/ifaces")), i18n("Interfaces"), 10)
49         page.leaf   = true
50         page.subindex = true
51
52         local page = entry({"admin", "network", "add"}, cbi("admin_network/iface_add"), nil)
53         page.leaf = true
54
55         uci:foreach("network", "interface",
56                 function (section)
57                         local ifc = section[".name"]
58                         if ifc ~= "loopback" then
59                                 entry({"admin", "network", "network", ifc},
60                                  true,
61                                  ifc:upper())
62                         end
63                 end
64         )
65
66         if nixio.fs.access("/etc/config/dhcp") then
67                 local page  = node("admin", "network", "dhcpleases")
68                 page.target = cbi("admin_network/dhcpleases")
69                 page.title  = i18n("DHCP Leases")
70                 page.order  = 30
71         end
72
73         local page  = node("admin", "network", "hosts")
74         page.target = cbi("admin_network/hosts")
75         page.title  = i18n("Hostnames")
76         page.order  = 40
77
78         local page  = node("admin", "network", "routes")
79         page.target = cbi("admin_network/routes")
80         page.title  = i18n("Static Routes")
81         page.order  = 50
82
83 end
84
85 function wifi_join()
86         local function param(x)
87                 return luci.http.formvalue(x)
88         end
89
90         local function ptable(x)
91                 x = param(x)
92                 return x and (type(x) ~= "table" and { x } or x) or {}
93         end
94
95         local dev  = param("device")
96         local ssid = param("join")
97
98         if dev and ssid then
99                 local cancel  = (param("cancel") or param("cbi.cancel")) and true or false
100
101                 if cancel then
102                         luci.http.redirect(luci.dispatcher.build_url("admin/network/wireless_join?device=" .. dev))
103                 else
104                         local cbi = require "luci.cbi"
105                         local tpl = require "luci.template"
106                         local map = luci.cbi.load("admin_network/wifi_add")[1]
107
108                         if map:parse() ~= cbi.FORM_DONE then
109                                 tpl.render("header")
110                                 map:render()
111                                 tpl.render("footer")
112                         end
113                 end
114         else
115                 luci.template.render("admin_network/wifi_join")
116         end
117 end
118
119 function wifi_add()
120         local dev = luci.http.formvalue("device") 
121         local uci = require "luci.model.uci".cursor()
122         local wlm = require "luci.model.wireless"
123
124         if dev then
125                 wlm.init(uci)
126
127                 local net = wlm:add_network({
128                         device     = dev,
129                         mode       = "ap",
130                         ssid       = "OpenWrt",
131                         encryption = "none"
132                 })
133
134                 uci:save("wireless")
135                 luci.http.redirect(luci.dispatcher.build_url("admin/network/wireless", dev, net:name()))
136         end
137 end
138
139 function wifi_delete(network)
140         local uci = require "luci.model.uci".cursor()
141         local wlm = require "luci.model.wireless"
142
143         wlm.init(uci)
144         wlm:del_network(network)
145
146         uci:save("wireless")
147         luci.http.redirect(luci.dispatcher.build_url("admin/network/wireless"))
148 end