-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathnet-config.php
268 lines (239 loc) · 9.04 KB
/
net-config.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
<?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\niface eth0 inet dhcp\n";
} else {
$eth0 = "\nauto 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";
}
}
$wlan0 = "\n";
}
// wlan0
if (isset($_POST['wifisec']['ssid']) && !empty($_POST['wifisec']['ssid'])) {
$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
$wlan0 = "\n";
$wlan0 .= "auto wlan0\n";
$wlan0 .= "iface wlan0 inet dhcp\n";
$wlan0 .= "wireless-power off\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 ".$_POST['wifisec']['password']."\n";
} else {
if ($_POST['wifisec']['encryption'] == 'none') {
$wlan0 .= "wireless-mode managed\n";
}
}
}
$eth0 = "\nauto eth0\niface eth0 inet dhcp\n";
} // 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);
// check integrity of /etc/network/interfaces
if(!hashCFG('check_net',$db)) {
$_netconf = file_get_contents('/etc/network/interfaces');
// set manual config template
$tpl = "net-config-manual.html";
} else {
$dbh = cfgdb_connect($db);
$net = cfgdb_read('cfg_lan',$dbh);
$wifisec = cfgdb_read('cfg_wifisec',$dbh);
$dbh = null;
$ipe = "ip addr list eth0 |grep \"inet \" |cut -d' ' -f6|cut -d/ -f1";
$ipw = "ip addr list wlan0 |grep \"inet \" |cut -d' ' -f6|cut -d/ -f1";
$ipeth0 = exec($ipe);
$ipwlan0 = exec($ipw);
$spe = "ethtool eth0 | grep -i speed | tr -d 'Speed:'";
$speth0 = exec($spe);
//getting signal quality percentage
$quw = "iwconfig wlan0 | grep 'Link Quality' | awk '{print $2}' | tr -d 'Quality=' | cut -c 1-2";
$quwg = exec($quw);
$quwlan0 = round(($quwg / 70) * 100);
$bitr = "iwconfig wlan0 | grep 'Bit Rate' | awk '{print $2}' | tr -d 'Bit Rate='";
$bitrate = exec($bitr);
if (!empty($ipeth0)) {
$statuset = 'Connected <i class="fa fa-check green sx"></i>';
} else {
$statuset = 'Not Connected <i class="fa fa-remove red sx"></i>';
}
if (!empty($ipwlan0)) {
$statuswl = 'Connected <i class="fa fa-check green sx"></i>';
} else {
if (wrk_checkStrSysfile('/proc/net/wireless','wlan0')) {
$statuswl = 'Not Connected <i class="fa fa-remove red sx"></i>';
} else {
$statuswl = 'No Wireless Interface Present';
}
}
// eth0
if (isset($_SESSION['netconf']['eth0']) && !empty($_SESSION['netconf']['eth0'])) {
$_eth0 .= "<div class=\"alert alert-info\">\n";
$_eth0 .= "<div><b>Status:</b> ".$statuset."</div>\n";
$_eth0 .= "<div><b>IP address:</b> ".$ipeth0."</div>\n";
$_eth0 .= "<div><b>Speed:</b> ".$speth0."</div>\n";
$_eth0 .= "</div>\n";
$_int0name .= $net[0]['name'];
$_int0dhcp .= "<option value=\"true\" ".((isset($net[0]['dhcp']) && $net[0]['dhcp']=="true") ? "selected" : "")." >Automatic (DHCP)</option>\n";
$_int0dhcp .= "<option value=\"false\" ".((isset($net[0]['dhcp']) && $net[0]['dhcp']=="false") ? "selected" : "")." >Static</option>\n";
$_int0 = $net[0];
}
// wlan0
if (isset($_SESSION['netconf']['wlan0']) && !empty($_SESSION['netconf']['wlan0'])) {
$_wlan0 .= "<div class=\"alert alert-info\">\n";
$_wlan0 .= "<div><b>Status:</b> ".$statuswl."</div>\n";
$_wlan0 .= "<div><b>IP address:</b> ".$ipwlan0."</div>\n";
$_wlan0 .= "<div><b>Signal Strength:</b> ".$quwlan0."%</div>\n";
$_wlan0 .= "<div><b>BitRate:</b> ".$bitrate." Mb/s</div>\n";
$_wlan0 .= "</div>\n";
$_wlan0ssid = $wifisec[0]['ssid'];
}
$_wlan0security .= "<option value=\"none\"".(($wifisec[0]['security'] == 'none') ? "selected" : "").">No security</option>\n";
$_wlan0security .= "<option value=\"wep\"".(($wifisec[0]['security'] == 'wep') ? "selected" : "").">WEP</option>\n";
$_wlan0security .= "<option value=\"wpa\"".(($wifisec[0]['security'] == 'wpa') ? "selected" : "").">WPA/WPA2 - Personal</option>\n";
$tpl = "net-config.html";
}
// 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'); ?>