refactor: use .env file for admin password instead of hardcoded 1234

This commit is contained in:
2026-07-05 11:48:40 +02:00
parent c78589f29f
commit c322107dd1
4 changed files with 64 additions and 23 deletions
+30 -3
View File
@@ -5,6 +5,24 @@ header('Content-Type: application/json');
$dataDir = __DIR__ . '/data/';
$dataFile = $dataDir . 'feedback.json';
function getAdminPassword() {
$envFile = __DIR__ . '/.env';
if (file_exists($envFile)) {
$lines = file($envFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
foreach ($lines as $line) {
if (strpos(trim($line), '#') === 0) continue;
$parts = explode('=', $line, 2);
if (count($parts) === 2) {
if (trim($parts[0]) === 'ADMIN_PASSWORD') {
return trim($parts[1]);
}
}
}
}
return '1234';
}
$adminPassword = getAdminPassword();
// Create data directory if not exists
if (!is_dir($dataDir)) {
@mkdir($dataDir, 0755, true);
@@ -114,12 +132,21 @@ switch ($_SERVER['REQUEST_METHOD']) {
echo json_encode(['error' => 'Feedback not found']);
}
}
elseif ($action === 'verify_password') {
$secret = $input['secret'] ?? '';
if ($secret === $adminPassword) {
echo json_encode(['success' => true]);
} else {
http_response_code(401);
echo json_encode(['error' => 'Unauthorized']);
}
exit;
}
elseif ($action === 'delete') {
$id = $input['id'] ?? '';
$secret = $input['secret'] ?? '';
// Simple authentication via secret "1234" (as defined in frontend)
if ($secret !== '1234') {
if ($secret !== $adminPassword) {
http_response_code(401);
echo json_encode(['error' => 'Unauthorized']);
exit;
@@ -139,7 +166,7 @@ switch ($_SERVER['REQUEST_METHOD']) {
$answer = $input['answer'] ?? '';
$secret = $input['secret'] ?? '';
if ($secret !== '1234') {
if ($secret !== $adminPassword) {
http_response_code(401);
echo json_encode(['error' => 'Unauthorized']);
exit;