-
Notifications
You must be signed in to change notification settings - Fork 61
/
net-config2.php
252 lines (220 loc) · 8.35 KB
/
net-config2.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
<?php
/*
* PlayerUI Copyright (C) 2013 Andrea Coiutti & Simone De Gregori
* Tsunamp Team
* http://www.tsunamp.com
*
* This Program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3, or (at your option)
* any later version.
*
* This Program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with RaspyFi; see the file COPYING. If not, see
* <http://www.gnu.org/licenses/>.
*
*
* UI-design/JS code by: Andrea Coiutti (aka ACX)
* PHP/JS code by: Simone De Gregori (aka Orion)
*
* file: net-config.php
* version: 1.0
*
*/
// common include
include('inc/connection.php');
playerSession('open',$db,'','');
?>
<?php
// open player session
playerSession('open',$db,'','');
// handle POST (reset)
if (isset($_POST['reset']) && $_POST['reset'] == 1) {
$eth0 = "iface eth0 inet dhcp\n";
$value = array('ssid' => '', 'encryption' => '', 'password' => '');
$dbh = cfgdb_connect($db);
cfgdb_update('cfg_wifisec',$dbh,'',$value);
$wifisec = cfgdb_read('cfg_wifisec',$dbh);
$dbh = null;
$_POST['eth0']['dhcp'] = 'true';
$_POST['eth0']['ip'] = '';
$_POST['eth0']['netmask'] = '';
$_POST['eth0']['gw'] = '';
$_POST['eth0']['dns1'] = '';
$_POST['eth0']['dns2'] = '';
}
// handle POST
if (isset($_POST) && !empty($_POST)) {
$dbh = cfgdb_connect($db);
// eth0
if (isset($_POST['eth0']['dhcp']) && isset($_POST['eth0']['ip'])) {
if ($_POST['eth0']['dhcp'] == 'true') {
$_POST['eth0']['dhcp'] = 'true';
$_POST['eth0']['ip'] = '';
$_POST['eth0']['netmask'] = '';
$_POST['eth0']['gw'] = '';
$_POST['eth0']['dns1'] = '';
$_POST['eth0']['dns2'] = '';
} else {
$_POST['eth0']['dhcp'] = 'false';
}
$value = array( 'name' => 'eth0',
'dhcp' => $_POST['eth0']['dhcp'],
'ip' => $_POST['eth0']['ip'],
'netmask' => $_POST['eth0']['netmask'],
'gw' => $_POST['eth0']['gw'],
'dns1' => $_POST['eth0']['dns1'],
'dns2' => $_POST['eth0']['dns2'] );
cfgdb_update('cfg_lan',$dbh,'',$value);
$net = cfgdb_read('cfg_lan',$dbh);
// format new config string for eth0
if ($_POST['eth0']['dhcp'] == 'true' ) {
$eth0 = "\nauto eth0\nallow-hotplug eth0\niface eth0 inet dhcp\n";
}
else {
$eth0 = "\nauto eth0\nallow-hotplug eth0\niface eth0 inet static\n";
$eth0 .= "address ".$_POST['eth0']['ip']."\n";
$eth0 .= "netmask ".$_POST['eth0']['netmask']."\n";
$eth0 .= "gateway ".$_POST['eth0']['gw']."\n";
if (isset($_POST['eth0']['dns1']) && !empty($_POST['eth0']['dns1'])) {
$eth0 .= "nameserver ".$_POST['eth0']['dns1']."\n";
}
if (isset($_POST['eth0']['dns2']) && !empty($_POST['eth0']['dns2'])) {
$eth0 .= "nameserver ".$_POST['eth0']['dns2']."\n";
}
}//else eth0 static
// format new config string for wlan0
$wlan0 = "\n";
}//if etho dhcp is set
// wlan0
if (isset($_POST['wifisec']['ssid']) && !empty($_POST['wifisec']['ssid']) && isset($_POST['wifisec']['password']) && !empty($_POST['wifisec']['password']))
{
$value = array('ssid' => $_POST['wifisec']['ssid'], 'encryption' => $_POST['wifisec']['encryption'], 'password' => $_POST['wifisec']['password']);
cfgdb_update('cfg_wifisec',$dbh,'',$value);
$wifisec = cfgdb_read('cfg_wifisec',$dbh);
// format new config string for wlan0
if ($_POST['wifisec']['dhcp'] == 'true' ) {
$wlan0 = "\n";
$wlan0 .= "auto wlan0\n";
$wlan0 .= "iface wlan0 inet dhcp\n";
if ($_POST['wifisec']['encryption'] == 'wpa') {
$wlan0 .= "wpa-ssid ".$_POST['wifisec']['ssid']."\n";
$wlan0 .= "wpa-psk ".$_POST['wifisec']['password']."\n";
}
else {
$wlan0 .= "wireless-essid ".$_POST['wifisec']['ssid']."\n";
if ($_POST['wifisec']['encryption'] == 'wep') {
$wlan0 .= "wireless-key ".bin2hex($_POST['wifisec']['password'])."\n";
} else {
$wlan0 .= "wireless-mode managed\n";
}
}//else wep or open
}//if dhcp for wlan0
else {
$wlan0 = "\n";
$wlan0 .= "auto wlan0\n";
$wlan0 .= "iface wlan0 inet static\n";
$wlan0 .= "address ".$_POST['wifisec']['ip']."\n";
$wlan0 .= "netmask ".$_POST['wifisec']['netmask']."\n";
$wlan0 .= "gateway ".$_POST['wifisec']['gw']."\n";
if (isset($_POST['wifisec']['dns1']) && !empty($_POST['wifisec']['dns1'])) {
$wlan0 .= "nameserver ".$_POST['wifisec']['dns1']."\n";
}
if (isset($_POST['wifisec']['dns2']) && !empty($_POST['wifisec']['dns2'])) {
$wlan0 .= "nameserver ".$_POST['wifisec']['dns2']."\n";
}
if ($_POST['wifisec']['encryption'] == 'wpa') {
$wlan0 .= "wpa-ssid ".$_POST['wifisec']['ssid']."\n";
$wlan0 .= "wpa-psk ".$_POST['wifisec']['password']."\n";
} //wpa
else {
$wlan0 .= "wireless-essid ".$_POST['wifisec']['ssid']."\n";
if ($_POST['wifisec']['encryption'] == 'wep') {
$wlan0 .= "wireless-key ".bin2hex($_POST['wifisec']['password'])."\n";
} else {
$wlan0 .= "wireless-mode managed\n";
}
}//else wep or open
}//else wlan0 static
// format new config string for eth0
if ($_POST['eth0']['dhcp'] == 'true' ) {
$eth0 = "\nauto eth0\nallow-hotplug eth0\niface eth0 inet dhcp\n";
}
else {
$eth0 = "\nauto eth0\nallow-hotplug eth0\niface eth0 inet static\n";
$eth0 .= "address ".$_POST['eth0']['ip']."\n";
$eth0 .= "netmask ".$_POST['eth0']['netmask']."\n";
$eth0 .= "gateway ".$_POST['eth0']['gw']."\n";
if (isset($_POST['eth0']['dns1']) && !empty($_POST['eth0']['dns1'])) {
$eth0 .= "nameserver ".$_POST['eth0']['dns1']."\n";
}
if (isset($_POST['eth0']['dns2']) && !empty($_POST['eth0']['dns2'])) {
$eth0 .= "nameserver ".$_POST['eth0']['dns2']."\n";
}
}//else eth0 static
} // end wlan0
// handle manual config
if(isset($_POST['netconf']) && !empty($_POST['netconf'])) {
// tell worker to write new MPD config
if ($_SESSION['w_lock'] != 1 && $_SESSION['w_queue'] == '') {
session_start();
$_SESSION['w_queue'] = "netcfgman";
$_SESSION['w_queueargs'] = $_POST['netconf'];
$_SESSION['w_active'] = 1;
// set UI notify
$_SESSION['notify']['title'] = 'Network Config modified';
$_SESSION['notify']['msg'] = '';
session_write_close();
} else {
session_start();
$_SESSION['notify']['title'] = 'Job Failed';
$_SESSION['notify']['msg'] = 'background worker is busy.';
session_write_close();
}
}
// close DB handle
$dbh = null;
// create job for background worker
if ($_SESSION['w_lock'] != 1 && !isset($_POST['netconf'])) {
// start / respawn session
session_start();
$_SESSION['w_queue'] = 'netcfg';
$_SESSION['w_queueargs'] = $wlan0.$eth0;
$_SESSION['w_active'] = 1;
// set ui_notify
$_SESSION['notify']['title'] = '';
if (isset($_GET['reset']) && $_GET['reset'] == 1 ) {
$_SESSION['notify']['msg'] = 'NetConfig restored to default settings';
} else {
$_SESSION['notify']['msg'] = 'NetConfig modified';
}
} else {
$_SESSION['notify']['title'] = '';
$_SESSION['notify']['msg'] = 'Background worker busy';
}
// unlock session file
playerSession('unlock');
}
// wait for worker output if $_SESSION['w_active'] = 1
//waitWorker(1);
// unlock session files
playerSession('unlock',$db,'','');
?>
<?php
$sezione = basename(__FILE__, '.php');
include('_header.php');
?>
<!-- content --!>
<?php
eval("echoTemplate(\"".getTemplate("templates/$tpl")."\");");
?>
<!-- content -->
<?php
debug($_POST);
?>
<?php include('_footer.php'); ?>