Viewing file: setup_env.php (2 KB) -rw-r--r-- Select action/file-type: (+) | (+) | (+) | Code (+) | Session (+) | (+) | SDB (+) | (+) | (+) | (+) | (+) | (+) |
<?php
// setup_env.php — Generate local config.php with secrets and Telegram settings
// Web usage: open /setup_env.php and optionally pass ?bot=<token>&chat=<id>
// CLI usage: php setup_env.php --bot=TOKEN --chat=ID
function random_ascii(int $length = 32): string {
$alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
$bytes = random_bytes($length);
$s = '';
for ($i = 0; $i < $length; $i++) { $s .= $alphabet[ord($bytes[$i]) % strlen($alphabet)]; }
return $s;
}
function generate_config(array $existing = [], ?string $bot = null, ?string $chat = null): array {
$cfg = $existing;
if (empty($cfg['ONE_TIME_SECRET'])) { $cfg['ONE_TIME_SECRET'] = bin2hex(random_bytes(32)); }
if (empty($cfg['COOKIE_ENCRYPTION_KEY'])) { $cfg['COOKIE_ENCRYPTION_KEY'] = random_ascii(32); }
if ($bot) { $cfg['TELEGRAM_BOT_TOKEN'] = $bot; }
if ($chat) { $cfg['TELEGRAM_CHAT_ID'] = $chat; }
return $cfg;
}
$bot = null; $chat = null;
if (PHP_SAPI === 'cli') {
foreach ($argv as $arg) {
if (preg_match('/^--bot=(.+)$/', $arg, $m)) $bot = $m[1];
elseif (preg_match('/^--chat=(.+)$/', $arg, $m)) $chat = $m[1];
}
} else {
$bot = $_GET['bot'] ?? null;
$chat = $_GET['chat'] ?? null;
}
$configPath = __DIR__ . '/config.php';
$existing = is_file($configPath) ? (include $configPath) : [];
$cfg = generate_config($existing, $bot, $chat);
$content = "<?php\nreturn " . var_export($cfg, true) . ";\n";
if (@file_put_contents($configPath, $content) === false) {
http_response_code(500);
echo 'Failed to write config.php';
exit;
}
header('Content-Type: application/json');
echo json_encode([
'ok' => true,
'config_path' => basename($configPath),
'ONE_TIME_SECRET' => $cfg['ONE_TIME_SECRET'],
'COOKIE_ENCRYPTION_KEY' => $cfg['COOKIE_ENCRYPTION_KEY'],
'TELEGRAM_BOT_TOKEN' => $cfg['TELEGRAM_BOT_TOKEN'] ?? null,
'TELEGRAM_CHAT_ID' => $cfg['TELEGRAM_CHAT_ID'] ?? null,
], JSON_PRETTY_PRINT);
|