From a6c96f126ca333fb10750165161084c7c7dd0b34 Mon Sep 17 00:00:00 2001 From: Nicolas Martignoni Date: Mon, 19 Mar 2018 17:28:36 +0100 Subject: [PATCH] Implement SSID with UTF chars. Issue #38. --- bin/changewifisettings.sh | 12 +++++++----- index.php | 11 ++++++++--- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/bin/changewifisettings.sh b/bin/changewifisettings.sh index ae90f1d..d69cb8a 100644 --- a/bin/changewifisettings.sh +++ b/bin/changewifisettings.sh @@ -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. diff --git a/index.php b/index.php index 760dafb..4c25363 100644 --- a/index.php +++ b/index.php @@ -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']; @@ -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" .