php: init

This commit is contained in:
Ludwig Behm 2024-04-20 22:23:29 +02:00
parent 1d8fb05acd
commit a1f1f278cf
3 changed files with 126 additions and 0 deletions

48
src/html/index.php Normal file
View file

@ -0,0 +1,48 @@
<?php
$access_list_path = '/etc/tuer3.0/door_access_hashs';
if (!hasCredentials())
render_default();
elseif (hasValidCredentials())
execute_cmd($_POST['cmd']);
else
render_failure();
function executeCmd($cmd) {
switch($cmd) {
case 'indoor_lock':
sendKeyBLE('lock');
break;
case 'indoor_open':
sendKeyBLE('open');
break;
default:
render_failure();
}
}
function hasCredentials() {
$secret = $_GET['secret'] || $_COOKIES['secret'];
return is_string($secret) && !empty($secret);
}
function hasValidCredentials() {
$secret = $_GET['secret'] || $_COOKIES['secret'];
$cipher = hash('sha512', $secret);
$tokens = hasAccessTokens($cipher, $access_list_path);
return false;
}
function hasAccessTokens($needle, $path) {
$lines = file($path, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
foreach($lines as $line) {
if (str_starts_with(ltrim($line), '#'))
continue;
$values = explode(';', $line);
if (count($values) != 3)
continue;
if ($needle == $values[2])
return true;
}
return false;
}