forked from laysense/laysense.cn
Rebuild Laysense site with Webman and SQLite CMS
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
require dirname(__DIR__) . '/vendor/autoload.php';
|
||||
|
||||
use App\Content\Database;
|
||||
|
||||
$email = strtolower(trim($argv[1] ?? ''));
|
||||
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
|
||||
fwrite(STDERR, "用法:php bin/admin.php admin@example.com\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
fwrite(STDERR, "请输入至少 12 字符的管理员密码:");
|
||||
$tty = function_exists('stream_isatty') && stream_isatty(STDIN);
|
||||
if ($tty) {
|
||||
exec('stty -echo');
|
||||
}
|
||||
try {
|
||||
$password = rtrim((string) fgets(STDIN), "\r\n");
|
||||
} finally {
|
||||
if ($tty) {
|
||||
exec('stty echo');
|
||||
}
|
||||
fwrite(STDERR, "\n");
|
||||
}
|
||||
if (mb_strlen($password) < 12 || strlen($password) > 72 || str_contains($password, "\0")) {
|
||||
fwrite(STDERR, "密码需至少 12 字符,并且最多 72 字节。\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
Database::initialize();
|
||||
$hash = password_hash($password, PASSWORD_DEFAULT);
|
||||
$query = Database::connect()->prepare('INSERT INTO users (email, password_hash) VALUES (?, ?)
|
||||
ON CONFLICT (email) DO UPDATE SET password_hash = excluded.password_hash');
|
||||
$query->execute([$email, $hash]);
|
||||
fwrite(STDOUT, "管理员账户已就绪。\n");
|
||||
Reference in New Issue
Block a user