Files
f3-simple-blog/scripts/create-admin.php
2026-03-28 23:29:06 +01:00

37 lines
806 B
PHP

<?php
declare(strict_types=1);
$f3 = require __DIR__ . '/bootstrap.php';
$username = trim((string) ($argv[1] ?? ''));
if ($username === '') {
fwrite(STDERR, "Usage: php scripts/create-admin.php <username>\n");
exit(1);
}
fwrite(STDOUT, 'Mot de passe (10 caractères minimum): ');
if (stripos(PHP_OS, 'WIN') !== 0) {
system('stty -echo');
}
$password = trim((string) fgets(STDIN));
if (stripos(PHP_OS, 'WIN') !== 0) {
system('stty echo');
}
fwrite(STDOUT, PHP_EOL);
if ($password === '') {
fwrite(STDERR, "Mot de passe vide.\n");
exit(1);
}
User::bootstrap($f3->get('DB'));
try {
$id = (new User())->create($username, $password);
fwrite(STDOUT, "Admin créé (ID {$id}).\n");
} catch (RuntimeException $e) {
fwrite(STDERR, $e->getMessage() . "\n");
exit(1);
}