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