<?php
include __DIR__ . '/auth.php';
admin_check();
include __DIR__ . '/layout.php';
include_once __DIR__ . '/../inc/static_cache.php';

function health_row($name, $ok, $message, $level = 'warn'){
    return array('name'=>$name, 'ok'=>(bool)$ok, 'message'=>$message, 'level'=>$level);
}

$root = realpath(__DIR__ . '/..');
$adminCfg = is_file(__DIR__ . '/../config/admin.php') ? include __DIR__ . '/../config/admin.php' : array();
$cacheCfg = is_file(__DIR__ . '/../config/cache.php') ? include __DIR__ . '/../config/cache.php' : array();
$account = admin_client_account();
$staticFiles = is_dir(__DIR__ . '/../cache/static') ? glob(__DIR__ . '/../cache/static/*.html') : array();
$backupFiles = is_dir(__DIR__ . '/../cache/backup') ? glob(__DIR__ . '/../cache/backup/*/*') : array();

$checks = array();
$checks[] = health_row('安装锁', is_file(__DIR__ . '/../install/install.lock'), 'install/install.lock 应存在，避免安装向导被再次访问。', 'danger');
$checks[] = health_row('后台目录', ($adminCfg['admin_dir'] ?? '') !== 'yanscms', '建议将 config/admin.php 的 admin_dir 改成非默认目录。');
$checks[] = health_row('默认密码风险', !admin_password_ok('admin123'), '默认密码 admin123 不应可用。', 'danger');
$checks[] = health_row('PHP 版本', version_compare(PHP_VERSION, '7.1.0', '>='), '当前 PHP：' . PHP_VERSION);
$checks[] = health_row('curl 扩展', function_exists('curl_init'), '授权同步和推送接口依赖 curl。');
$checks[] = health_row('openssl 扩展', extension_loaded('openssl'), '授权签名和安全通信依赖 openssl。');
$checks[] = health_row('json 扩展', function_exists('json_encode'), '主题注册和后台配置依赖 JSON。');

foreach(array('cache','cache/static','cache/backup','data','data/sites','data/content_center','assets/uploads','config') as $dir){
    $path = __DIR__ . '/../' . $dir;
    $checks[] = health_row('目录可写：' . $dir, is_dir($path) && is_writable($path), $dir . ' 需要 Web 进程写入权限。');
}

$checks[] = health_row('静态缓存', isset($cacheCfg['enabled']), '当前缓存文件：' . count($staticFiles) . '，TTL：' . intval($cacheCfg['ttl'] ?? 0) . ' 秒。', 'info');
$checks[] = health_row('配置备份', is_dir(__DIR__ . '/../cache/backup'), '配置写入前会备份到 cache/backup。当前备份文件约：' . count($backupFiles), 'info');
$checks[] = health_row('客户账号', !empty($account['account']) || !empty($account['password_hash']), '建议完成客户注册并使用哈希密码登录。');

$okCount = 0;
foreach($checks as $check){ if($check['ok']) $okCount++; }

admin_header('站点体检');
?>
<div class="grid">
    <div class="stat"><strong><?php echo $okCount; ?>/<?php echo count($checks); ?></strong><span>通过项</span></div>
    <div class="stat"><strong><?php echo count($staticFiles); ?></strong><span>静态缓存</span></div>
    <div class="stat"><strong><?php echo count($backupFiles); ?></strong><span>配置备份</span></div>
    <div class="stat"><strong><?php echo admin_h(PHP_VERSION); ?></strong><span>PHP 版本</span></div>
</div>

<div class="card">
    <h2>体检结果</h2>
    <table>
        <tr><th>项目</th><th>状态</th><th>说明</th></tr>
        <?php foreach($checks as $check): ?>
        <tr>
            <td><?php echo admin_h($check['name']); ?></td>
            <td><?php echo $check['ok'] ? '通过' : '需处理'; ?></td>
            <td><?php echo admin_h($check['message']); ?></td>
        </tr>
        <?php endforeach; ?>
    </table>
</div>

<div class="card">
    <h2>建议处理顺序</h2>
    <p>生产环境优先处理默认后台目录、默认密码、安装锁和目录权限。确认这些基础项后，再检查缓存策略、授权状态、内容备份和日志轮转。</p>
</div>
<?php admin_footer(); ?>

