guestbook/admin-password.php
Zhang Pu 06d21f90db init: guestbook v1.0.0 首次发布
- 加固版留言系统 / 磁盘清理助手
- MIT License
2026-11-02 11:12:15 +08:00

52 lines
1.5 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/**
* 修改管理员密码(仅命令行可用)
* 用法:php admin-password.php <新密码> [用户名]
* php admin-password.php --random [用户名] # 生成随机密码并打印
*/
declare(strict_types=1);
if (PHP_SAPI !== 'cli') {
http_response_code(403);
exit("仅限命令行使用\n");
}
require_once __DIR__ . '/config.php';
$args = array_slice($argv, 1);
$random = in_array('--random', $args, true);
if ($random) { $args = array_values(array_diff($args, ['--random'])); }
$username = $args[1] ?? 'admin';
$pass = $args[0] ?? '';
if ($random || $pass === '') {
$pass = bin2hex(random_bytes(8));
}
if (mb_len($pass) < 8) {
fwrite(STDERR, "密码太短(至少 8 位)\n");
exit(1);
}
try {
$db = get_db();
$st = $db->prepare('SELECT id FROM admins WHERE username = ?');
$st->bindValue(1, $username, SQLITE3_TEXT);
$row = $st->execute()->fetchArray(SQLITE3_ASSOC);
if (!$row) {
fwrite(STDERR, "账号不存在: {$username}\n");
exit(1);
}
$u = $db->prepare('UPDATE admins SET password_hash = ? WHERE id = ?');
$u->bindValue(1, password_hash($pass, PASSWORD_DEFAULT), SQLITE3_TEXT);
$u->bindValue(2, (int)$row['id'], SQLITE3_INTEGER);
$u->execute();
echo "已更新账号 {$username} 的密码\n";
if ($random) {
echo "新密码: {$pass}\n";
echo "请立刻记下(此密码只在本次输出中显示)\n";
}
exit(0);
} catch (Throwable $e) {
fwrite(STDERR, '失败: ' . $e->getMessage() . "\n");
exit(1);
}