luci-base: form.js: store pointer to parent map in modal overlay map
[project/luci.git] / modules / luci-mod-network / luasrc / model / cbi / admin_network / wifi_add.lua
1 -- Copyright 2009 Jo-Philipp Wich <jow@openwrt.org>
2 -- Licensed to the public under the Apache License 2.0.
3
4 local fs = require "nixio.fs"
5 local nw = require "luci.model.network"
6 local fw = require "luci.model.firewall"
7 local uci = require "luci.model.uci".cursor()
8 local http = require "luci.http"
9
10 local iw = luci.sys.wifi.getiwinfo(http.formvalue("device"))
11
12 local has_firewall = fs.access("/etc/config/firewall")
13
14 if not iw then
15 luci.http.redirect(luci.dispatcher.build_url("admin/network/wireless"))
16 return
17 end
18
19 m = SimpleForm("network", translatef("Joining Network: %q", http.formvalue("join")))
20 m.cancel = translate("Back to scan results")
21 m.reset = false
22
23 function m.on_cancel()
24 local dev = http.formvalue("device")
25 http.redirect(luci.dispatcher.build_url(
26 dev and "admin/network/wireless_join?device=" .. dev
27 or "admin/network/wireless"
28 ))
29 end
30
31 nw.init(uci)
32 fw.init(uci)
33
34 m.hidden = {
35 device = http.formvalue("device"),
36 join = http.formvalue("join"),
37 channel = http.formvalue("channel"),
38 mode = http.formvalue("mode"),
39 bssid = http.formvalue("bssid"),
40 wep = http.formvalue("wep"),
41 wpa_suites = http.formvalue("wpa_suites"),
42 wpa_version = http.formvalue("wpa_version")
43 }
44
45 if iw and iw.mbssid_support then
46 replace = m:field(Flag, "replace", translate("Replace wireless configuration"),
47 translate("Check this option to delete the existing networks from this radio."))
48
49 function replace.cfgvalue() return "0" end
50 else
51 replace = m:field(DummyValue, "replace", translate("Replace wireless configuration"))
52 replace.default = translate("The hardware is not multi-SSID capable and the existing " ..
53 "configuration will be replaced if you proceed.")
54
55 function replace.formvalue() return "1" end
56 end
57
58 if http.formvalue("wep") == "1" then
59 key = m:field(Value, "key", translate("WEP passphrase"),
60 translate("Specify the secret encryption key here."))
61
62 key.password = true
63 key.datatype = "wepkey"
64
65 elseif (tonumber(m.hidden.wpa_version) or 0) > 0 and
66 (m.hidden.wpa_suites == "PSK" or m.hidden.wpa_suites == "PSK2")
67 then
68 key = m:field(Value, "key", translate("WPA passphrase"),
69 translate("Specify the secret encryption key here."))
70
71 key.password = true
72 key.datatype = "wpakey"
73 --m.hidden.wpa_suite = (tonumber(http.formvalue("wpa_version")) or 0) >= 2 and "psk2" or "psk"
74 end
75
76 newnet = m:field(Value, "_netname_new", translate("Name of the new network"),
77 translate("The allowed characters are: <code>A-Z</code>, <code>a-z</code>, " ..
78 "<code>0-9</code> and <code>_</code>"
79 ))
80
81 newnet.default = m.hidden.mode == "Ad-Hoc" and "mesh" or "wwan"
82 newnet.datatype = "uciname"
83
84 if has_firewall then
85 fwzone = m:field(Value, "_fwzone",
86 translate("Create / Assign firewall-zone"),
87 translate("Choose the firewall zone you want to assign to this interface. Select <em>unspecified</em> to remove the interface from the associated zone or fill out the <em>create</em> field to define a new zone and attach the interface to it."))
88
89 fwzone.template = "cbi/firewall_zonelist"
90 fwzone.default = m.hidden.mode == "Ad-Hoc" and "mesh" or "wan"
91 end
92
93 function newnet.parse(self, section)
94 local net, zone
95
96 if has_firewall then
97 local value = fwzone:formvalue(section)
98 if value and #value > 0 then
99 zone = fw:get_zone(value) or fw:add_zone(value)
100 end
101 end
102
103 local wdev = nw:get_wifidev(m.hidden.device)
104
105 wdev:set("disabled", false)
106 wdev:set("channel", m.hidden.channel)
107
108 if replace:formvalue(section) then
109 local n
110 for _, n in ipairs(wdev:get_wifinets()) do
111 wdev:del_wifinet(n)
112 end
113 end
114
115 local wconf = {
116 device = m.hidden.device,
117 ssid = m.hidden.join,
118 mode = (m.hidden.mode == "Ad-Hoc" and "adhoc" or "sta")
119 }
120
121 if m.hidden.wep == "1" then
122 wconf.encryption = "wep-open"
123 wconf.key = "1"
124 wconf.key1 = key and key:formvalue(section) or ""
125 elseif (tonumber(m.hidden.wpa_version) or 0) > 0 then
126 wconf.encryption = (tonumber(m.hidden.wpa_version) or 0) >= 2 and "psk2" or "psk"
127 wconf.key = key and key:formvalue(section) or ""
128 else
129 wconf.encryption = "none"
130 end
131
132 if wconf.mode == "adhoc" or wconf.mode == "sta" then
133 wconf.bssid = m.hidden.bssid
134 end
135
136 local value = self:formvalue(section)
137 net = nw:add_network(value, { proto = "dhcp" })
138
139 if not net then
140 self.error = { [section] = "missing" }
141 else
142 wconf.network = net:name()
143
144 local wnet = wdev:add_wifinet(wconf)
145 if wnet then
146 if zone then
147 fw:del_network(net:name())
148 zone:add_network(net:name())
149 end
150
151 uci:save("wireless")
152 uci:save("network")
153 uci:save("firewall")
154
155 luci.http.redirect(wnet:adminlink())
156 end
157 end
158 end
159
160 if has_firewall then
161 function fwzone.cfgvalue(self, section)
162 self.iface = section
163 local z = fw:get_zone_by_network(section)
164 return z and z:name()
165 end
166 end
167
168 return m