forked from MiczFlor/RPi-Jukebox-RFID
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinc.setWifi.php
executable file
·115 lines (108 loc) · 3.84 KB
/
inc.setWifi.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
<?php
/*
* read ssid and password from /etc/wpa_supplicant/wpa_supplicant.conf
*/
$wpaconf = file_get_contents("/etc/wpa_supplicant/wpa_supplicant.conf");
/*
* get the lines we need (in a rough way...)
*/
foreach(preg_split("/((\r?\n)|(\r\n?))/", $wpaconf) as $line){
unset($temp);
$temp = explode("=", $line);
if(trim($temp[0]) == "ssid") {
$CONFssid = trim(trim($temp[1]), '"');
}
if(trim($temp[0]) == "psk") {
$CONFWIFIpass = trim(trim($temp[1]), '"');
}
}
/*
* see if we got some values from the form
*/
if(trim($_POST['WIFIssid']) != "") {
$WIFIssid = trim($_POST['WIFIssid']);
} else {
$WIFIssid = $CONFssid;
}
if(trim($_POST['WIFIpass']) != "") {
$WIFIpass = trim($_POST['WIFIpass']);
} else {
$WIFIpass = $CONFWIFIpass;
}
/*
* Now we need to check if we need to create a new wpa_supplicant.conf
* This is the case if either value coming from the form is different from
* the current conf file
*/
unset($exec);
if($WIFIssid != $CONFssid || $WIFIpass != $CONFWIFIpass) {
// make multiline bash
$exec = "bash <<'END'
sudo cp /home/pi/RPi-Jukebox-RFID/misc/sampleconfigs//wpa_supplicant.conf.stretch-default2.sample /etc/wpa_supplicant/wpa_supplicant.conf
sudo sed -i 's/%WIFIssid%/'".$WIFIssid."'/' /etc/wpa_supplicant/wpa_supplicant.conf
sudo sed -i 's/%WIFIpass%/'".$WIFIpass."'/' /etc/wpa_supplicant/wpa_supplicant.conf
sudo chown root:netdev /etc/wpa_supplicant/wpa_supplicant.conf
sudo chmod 664 /etc/wpa_supplicant/wpa_supplicant.conf
# restart wlan0
# dunno how to do this on the command line. The following doesn't disconnect the WiFi:
# sudo ip link set wlan0 down && sudo ip link set wlan0 up
END";
passthru($exec);
}
if($debug == "true") {
print "<pre>";
print "\$wpaconf:\n";
print $wpaconf;
print "\$WIFIssid: ".$WIFIssid."\n";
print "\$WIFIpass: ".$WIFIpass."\n";
print "\$_POST:\n";
print_r($_POST);
print "</pre>";
}
?>
<form name='volume' method='post' action='<?php print $_SERVER['PHP_SELF']; ?>'>
<fieldset>
<!-- Form Name -->
<legend><?php print $lang['globalWifiNetwork']; ?></legend>
<?php
if(isset($exec)) {
print '
<div class="alert alert-info">
'.$lang['settingsWifiRestart'].'
</div>';
}
?>
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="WIFIssid"><?php print $lang['globalSSID']; ?></label>
<div class="col-md-6">
<input value="<?php
if(isset($WIFIssid) && $WIFIssid != "") {
print $WIFIssid;
}
?>" id="WIFIssid" name="WIFIssid" placeholder="<?php print $lang['settingsWifiSsidPlaceholder']; ?>" class="form-control input-md" type="text" required="required">
<span class="help-block"><?php print $lang['settingsWifiSsidHelp']; ?></span>
</div>
</div>
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="WIFIpass"><?php print $lang['globalPassword']; ?></label>
<div class="col-md-6">
<input value="<?php
if(isset($WIFIpass) && $WIFIpass != "") {
print $WIFIpass;
}
?>" id="WIFIpass" name="WIFIpass" placeholder="" class="form-control input-md" type="text" required="required">
<span class="help-block"></span>
</div>
</div>
</fieldset>
<!-- Button (Double) -->
<div class="form-group">
<label class="col-md-4 control-label" for="submit"></label>
<div class="col-md-8">
<button id="submitWifi" name="submitWifi" class="btn btn-success" value="submit"><?php print $lang['globalSubmit']; ?></button>
<br clear='all'><br>
</div>
</div>
</form>