未安装时自动跳转安装向导(修「打开首页不跳转」)

现象:全新部署后打开域名,看到的是一个空导航站(没有任何提示),
必须自己猜 /install.php 这个网址才能安装。

原因:index.php 一进来就 db() 建库并渲染前台,全站没有任何
「还没装」的引导逻辑;install.php 只能靠手输网址触达。

改动:
- functions.php 新增 app_installed():全站唯一的「已安装」判断源
  (有管理员账号 = 已安装),内部 try/catch 兜住建库失败
- index.php 开头:未安装且 install.php 存在 → 302 到安装页
  · 只在 install.php 真存在时跳,用户装完删掉后自动失效,不会 404
- install.php 复用 app_installed(),删掉自己那套 is_file(DB_PATH) 推断,
  避免「首页说没装、install 说装了」的口径分裂

测试(都在 _tooling/):
- _sn_install_redirect_e2e.py 19 项:未安装跳转 / 已安装不跳 /
  install.php 删了不跳 / 已装拒重装,四态全覆盖
- _sn_install_redirect_falsify.py 6 变体全精确命中(含「无条件跳转→死循环」)
- _sn_install_e2e.py 41 项回归全绿(顺手修 6 条因新逻辑过时的断言)
This commit is contained in:
2026-11-03 22:07:22 +08:00
parent 87bc11ceee
commit ae75058de1
3 changed files with 42 additions and 10 deletions
+14
View File
@@ -9,6 +9,20 @@ declare(strict_types=1);
require_once __DIR__ . '/functions.php';
send_no_cache();
/**
* 未安装就先去装:库里还没有任何一个管理员账号时,前台没有意义
* (打开只会看到一个空导航,既没有提示也不知道去哪)。
*
* ⚠️ 只在 install.php 真的存在时才跳。用户装完删掉 install.php 后,
* 这个分支自动失效,不会跳到 404。
* ⚠️ 必须放在 db() 之前判断的等价位置:app_installed() 内部会调 db(),
* 但那边有 try/catch 兜住建库失败的情况,不会把异常抛到前台。
*/
if (!app_installed() && is_file(__DIR__ . '/install.php')) {
header('Location: ' . url('install.php'));
exit;
}
$pdo = db();
// 前台数据