feat: Secure SMS verification with Redis (Predis)
- Added Predis library for Redis connection (no PHP extension required) - Server-side SMS code generation and storage in Redis - Rate limiting and brute-force protection - Integration with n8n webhook for SMS sending - Environment variables moved to .env file - Fixed policy verification endpoint - Added file-based fallback if Redis unavailable
This commit is contained in:
65
env_loader.php
Normal file
65
env_loader.php
Normal file
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
/**
|
||||
* Утилита для загрузки переменных из .env файла
|
||||
*/
|
||||
|
||||
function loadEnv($envFile = null) {
|
||||
if ($envFile === null) {
|
||||
$envFile = __DIR__ . '/.env';
|
||||
}
|
||||
|
||||
if (!file_exists($envFile)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$lines = file($envFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
|
||||
|
||||
foreach ($lines as $line) {
|
||||
// Пропускаем комментарии
|
||||
if (strpos(trim($line), '#') === 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Парсим строку KEY=VALUE
|
||||
if (strpos($line, '=') !== false) {
|
||||
list($key, $value) = explode('=', $line, 2);
|
||||
$key = trim($key);
|
||||
$value = trim($value);
|
||||
|
||||
// Убираем кавычки если есть
|
||||
if ((substr($value, 0, 1) === '"' && substr($value, -1) === '"') ||
|
||||
(substr($value, 0, 1) === "'" && substr($value, -1) === "'")) {
|
||||
$value = substr($value, 1, -1);
|
||||
}
|
||||
|
||||
// Устанавливаем переменную окружения, если еще не установлена
|
||||
if (!isset($_ENV[$key]) && !getenv($key)) {
|
||||
$_ENV[$key] = $value;
|
||||
putenv("$key=$value");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// Автоматическая загрузка при подключении файла
|
||||
loadEnv();
|
||||
|
||||
/**
|
||||
* Получить значение из .env
|
||||
*/
|
||||
function env($key, $default = null) {
|
||||
// Сначала проверяем $_ENV
|
||||
if (isset($_ENV[$key])) {
|
||||
return $_ENV[$key];
|
||||
}
|
||||
|
||||
// Затем getenv
|
||||
$value = getenv($key);
|
||||
if ($value !== false) {
|
||||
return $value;
|
||||
}
|
||||
|
||||
return $default;
|
||||
}
|
||||
Reference in New Issue
Block a user