Skip to content

Instantly share code, notes, and snippets.

@billz
Last active September 20, 2023 16:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save billz/ca8003ae88f68e39e0dab31305f917e8 to your computer and use it in GitHub Desktop.
Save billz/ca8003ae88f68e39e0dab31305f917e8 to your computer and use it in GitHub Desktop.
<?php
$ssid = "my_ssid";
$network['passphrase'] = "my_passphrase";
$wpa_file = fopen('/tmp/wifidata', 'w');
if (strlen($network['passphrase']) >=8 && strlen($network['passphrase']) <= 63) {
unset($wpa_passphrase);
unset($line);
exec('wpa_passphrase '. ssid2utf8( escapeshellarg($ssid) ) . ' ' . escapeshellarg($network['passphrase']), $wpa_passphrase);
foreach ($wpa_passphrase as $line) {
if (preg_match('/^\s*}\s*$/', $line)) {
if (array_key_exists('priority', $network)) {
fwrite($wpa_file, "\tpriority=".$network['priority'].PHP_EOL);
}
fwrite($wpa_file, $line.PHP_EOL);
} else {
if ( preg_match('/\\\\x[0-9A-Fa-f]{2}/',$ssid) && strpos($line, "ssid=\"") !== false ) {
fwrite($wpa_file, "\tssid=P\"".$ssid."\"".PHP_EOL);
} else {
fwrite($wpa_file, $line.PHP_EOL);
}
}
}
} else {
echo 'WPA passphrase must be between 8 and 63 characters';
}
/*
* Replace escaped bytes (hex) by binary - assume UTF8 encoding
*
* @param string $ssid
*/
function ssid2utf8($ssid)
{
return evalHexSequence($ssid);
}
// Sanitizes a string for QR encoding
// @param string $str
// @return string
function qr_encode($str)
{
return preg_replace('/(?<!\\\)([\":;,])/', '\\\\\1', $str);
}
function evalHexSequence($string)
{
$evaluator = function ($input) {
return hex2bin($input[1]);
};
return preg_replace_callback('/\\\x(..)/', $evaluator, $string);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment