<?php

header('Content-Type: text/html; charset=utf-8');

$extension = 'phpenc_loader';
$isLoaded = extension_loaded($extension);
$loaderVersion = $isLoaded && function_exists('phpenc_loader_version') ? phpenc_loader_version() : null;
$loaderInfo = $isLoaded && function_exists('phpenc_loader_info') ? phpenc_loader_info() : [];
$detectedMachineId = $isLoaded && function_exists('phpenc_machine_id') ? phpenc_machine_id() : '';
$phpVersion = PHP_VERSION;
$phpVersionId = PHP_VERSION_ID;
$phpSapi = PHP_SAPI;
$phpIni = php_ini_loaded_file() ?: '未加载';
$extensionDir = ini_get('extension_dir');
$machineId = ini_get('phpenc_loader.machine_id');
$domainOverride = ini_get('phpenc_loader.domain');
$licensePath = ini_get('phpenc_loader.license_path');
$enforcePolicy = ini_get('phpenc_loader.enforce_policy');
$onlyEncrypted = ini_get('phpenc_loader.only_encrypted');
$currentHost = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '';
$serverName = isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : '';
$documentRoot = isset($_SERVER['DOCUMENT_ROOT']) ? $_SERVER['DOCUMENT_ROOT'] : getcwd();
$licenseCandidates = array_values(array_unique(array_filter([
    $licensePath,
    __DIR__ . DIRECTORY_SEPARATOR . 'license.lic',
    $documentRoot ? rtrim($documentRoot, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . 'license.lic' : '',
])));

$checks = [];
$checks[] = [
    'name' => '扩展已加载',
    'ok' => $isLoaded,
    'detail' => $isLoaded ? 'phpenc_loader 已加载' : '未加载；请在 php.ini 添加 extension=/path/phpenc_loader.so 并重启 PHP-FPM',
];
$checks[] = [
    'name' => '版本函数',
    'ok' => $loaderVersion !== null,
    'detail' => $loaderVersion !== null ? $loaderVersion : 'phpenc_loader_version() 不可用',
];
$checks[] = [
    'name' => 'PHP 版本',
    'ok' => $phpVersionId >= 70000,
    'detail' => $phpVersion . ' / ' . $phpSapi,
];
$checks[] = [
    'name' => 'license.lic 搜索',
    'ok' => hasExistingFile($licenseCandidates),
    'detail' => describeLicenseCandidates($licenseCandidates),
];
$checks[] = [
    'name' => '域名来源',
    'ok' => $domainOverride !== '' || $currentHost !== '' || $serverName !== '',
    'detail' => 'phpenc_loader.domain=' . printable($domainOverride) . '，HTTP_HOST=' . printable($currentHost) . '，SERVER_NAME=' . printable($serverName),
];
$checks[] = [
    'name' => '机器码',
    'ok' => $machineId !== '' || $detectedMachineId !== '',
    'detail' => $machineId !== '' ? '手动配置：' . $machineId : ($detectedMachineId !== '' ? '自动机器码：' . $detectedMachineId : '扩展未加载，无法读取'),
];

$allOk = true;
foreach ($checks as $check) {
    if (!$check['ok']) {
        $allOk = false;
        break;
    }
}

function hasExistingFile(array $paths)
{
    foreach ($paths as $path) {
        if ($path !== '' && is_file($path)) {
            return true;
        }
    }
    return false;
}

function describeLicenseCandidates(array $paths)
{
    if (!$paths) {
        return '没有候选路径';
    }

    $parts = [];
    foreach ($paths as $path) {
        $parts[] = $path . (is_file($path) ? ' 存在' : ' 缺失');
    }
    return implode('；', $parts);
}

function printable($value)
{
    return $value === '' || $value === false ? '空' : (string)$value;
}

function h($value)
{
    return htmlspecialchars((string)$value, ENT_QUOTES, 'UTF-8');
}

?><!doctype html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>phpenc_loader 扩展检查</title>
    <style>
        body { font-family: Arial, 'Microsoft YaHei', sans-serif; margin: 24px; color: #1f2937; background: #f8fafc; }
        main { max-width: 980px; margin: 0 auto; background: #fff; border: 1px solid #e5e7eb; border-radius: 8px; padding: 24px; }
        h1 { margin-top: 0; font-size: 24px; }
        table { width: 100%; border-collapse: collapse; margin-top: 16px; }
        th, td { border-bottom: 1px solid #e5e7eb; text-align: left; padding: 10px; vertical-align: top; }
        th { background: #f3f4f6; }
        .ok { color: #047857; font-weight: 700; }
        .bad { color: #b91c1c; font-weight: 700; }
        code { background: #f3f4f6; padding: 2px 5px; border-radius: 5px; }
        pre { background: #101828; color: #d9f99d; padding: 14px; border-radius: 8px; overflow: auto; }
        .summary { padding: 12px; margin: 14px 0; border: 1px solid #e5e7eb; border-radius: 8px; }
        .summary.okbox { border-color: #86efac; background: #f0fdf4; }
        .summary.badbox { border-color: #fecaca; background: #fef2f2; }
        ul { line-height: 1.8; }
    </style>
</head>
<body>
<main>
    <h1>phpenc_loader 扩展检查</h1>
    <div class="summary <?php echo $allOk ? 'okbox' : 'badbox'; ?>">
        当前状态：<strong><?php echo $allOk ? '基础检查通过' : '需要处理'; ?></strong>
    </div>

    <table>
        <thead><tr><th>项目</th><th>状态</th><th>详情</th></tr></thead>
        <tbody>
        <?php foreach ($checks as $check): ?>
            <tr>
                <td><?php echo h($check['name']); ?></td>
                <td class="<?php echo $check['ok'] ? 'ok' : 'bad'; ?>"><?php echo $check['ok'] ? 'OK' : 'FAIL'; ?></td>
                <td><?php echo h($check['detail']); ?></td>
            </tr>
        <?php endforeach; ?>
        </tbody>
    </table>

    <h2>当前 PHP 信息</h2>
    <ul>
        <li>PHP：<code><?php echo h($phpVersion); ?></code></li>
        <li>SAPI：<code><?php echo h($phpSapi); ?></code></li>
        <li>php.ini：<code><?php echo h($phpIni); ?></code></li>
        <li>extension_dir：<code><?php echo h($extensionDir); ?></code></li>
        <li>phpenc_loader.enforce_policy：<code><?php echo h(printable($enforcePolicy)); ?></code></li>
        <li>phpenc_loader.only_encrypted：<code><?php echo h(printable($onlyEncrypted)); ?></code></li>
        <li>phpenc_loader.license_path：<code><?php echo h(printable($licensePath)); ?></code></li>
        <li>phpenc_loader.machine_id：<code><?php echo h(printable($machineId)); ?></code></li>
        <li>phpenc_loader.domain：<code><?php echo h(printable($domainOverride)); ?></code></li>
        <?php if ($detectedMachineId !== ''): ?><li>自动机器码：<code><?php echo h($detectedMachineId); ?></code></li><?php endif; ?>
        <?php if (isset($loaderInfo['cache_entries'])): ?><li>运行时缓存条目：<code><?php echo (int)$loaderInfo['cache_entries']; ?></code></li><?php endif; ?>
    </ul>

    <h2>宝塔安装示例</h2>
    <p>把匹配当前 PHP 版本的 <code>phpenc_loader-phpXX.so</code> 放到服务器后，在对应 PHP 的 <code>php.ini</code> 加入：</p>
    <pre><code>extension=/www/wwwroot/phpenc/loaders/phpenc_loader-phpXX.so
phpenc_loader.enforce_policy=1
phpenc_loader.only_encrypted=0</code></pre>
    <p>如果授权启用了机器绑定，可以把上方“自动机器码”填入加密面板；也可以手动固定：</p>
    <pre><code>phpenc_loader.machine_id=你的机器码</code></pre>
    <p>如果 CLI 或计划任务没有域名环境，可以额外加入：</p>
    <pre><code>phpenc_loader.domain=example.com</code></pre>

    <h2>加密文件识别</h2>
    <p>加密后的 PHP 文件会保留 <code>.php</code> 后缀，文件前部是 Swoole/SG16 风格 PHP stub，真正容器位于 <code>__halt_compiler()</code> 之后。运行时必须能在项目目录或父目录找到 <code>license.lic</code>。</p>
</main>
</body>
</html>
