Skip to content

Commit

Permalink
Implement SSID with UTF chars. Issue #38.
Browse files Browse the repository at this point in the history
  • Loading branch information
martignoni committed Mar 19, 2018
1 parent 3e0077b commit a6c96f1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
12 changes: 7 additions & 5 deletions bin/changewifisettings.sh
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,15 @@ sed -i "/^channel=/c\channel=$NEWCHANNEL" "$CONFIGFILE"
#
# SSID setting.
# Validate new SSID. Replace it with 'MoodleBox' if invalid.
[[ $NEWSSID =~ ^[[:alnum:][:blank:]]{1,32}$ ]] || NEWSSID="MoodleBox"
# At this point, $NEWSSID is a string of hex values, e.g. "74657374" for "test"
# We want to check that it is valid, and between 8 and 32 bytes.
[[ $NEWSSID =~ ^([0-9a-fA-F]{2}){8,32}$ ]] || NEWSSID="4d6f6f646c65426f78" # "MoodleBox"
# New SSID is now valid; set it in config file.
if [[ -z $(grep "^utf8_ssid=1$" "$CONFIGFILE") ]]; then # Change ssid to ssid2, add utf8_ssid param.
sed -i "/^ssid=/c\ssid2=P\"$NEWSSID\"" "$CONFIGFILE"
# Change ssid to ssid2
sed -i "/^ssid=/c\ssid2=$NEWSSID" "$CONFIGFILE"
sed -i "/^ssid2=/c\ssid2=$NEWSSID" "$CONFIGFILE"
if [[ -z $(grep "^utf8_ssid=1$" "$CONFIGFILE") ]]; then # add utf8_ssid param.
sed -i "/ssid2/a utf8_ssid=1" "$CONFIGFILE"
else
sed -i "/^ssid2=/c\ssid2=P\"$NEWSSID\"" "$CONFIGFILE"
fi
#
# Password protection setting.
Expand Down
11 changes: 8 additions & 3 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,12 @@
$wifiinfo = \tool_moodlebox\local\utils::parse_config_file('/etc/hostapd/hostapd.conf', false, INI_SCANNER_RAW);

$currentwifichannel = $wifiinfo['channel'];
$currentwifissid = array_key_exists('ssid', $wifiinfo) ? $wifiinfo['ssid'] : $wifiinfo['ssid2'];
if ( preg_match_all('/"([^"]+)"/', $currentwifissid, $ssidmatch) ) {
$currentwifissid = $ssidmatch[1][0];
if ( array_key_exists('ssid', $wifiinfo) ) {
$currentwifissid = $wifiinfo['ssid'];
} else {
$currentwifissid = $wifiinfo['ssid2'];
// Convert $currentwifissid from hex. See https://stackoverflow.com/a/46344675.
$currentwifissid = pack("H*", $currentwifissid);
}
$currentwifipassword = array_key_exists('wpa_passphrase', $wifiinfo) ? $wifiinfo['wpa_passphrase'] : null;
$currentwificountry = $wifiinfo['country_code'];
Expand Down Expand Up @@ -406,6 +409,8 @@ public function definition() {
if (!isset($data->wifipassword)) {
$data->wifipassword = null;
}
// Convert $data->wifissid to hex. See https://stackoverflow.com/a/46344675.
$data->wifissid = implode(unpack("H*", $data->wifissid));
file_put_contents($wifipasswordtriggerfilename,
"channel=" . $data->wifichannel . "\n" .
"password=" . $data->wifipassword . "\n" .
Expand Down

0 comments on commit a6c96f1

Please sign in to comment.