<?php
/**
 * ioncuber server check / loader wizard
 * =====================================
 *
 * Upload this single file to the server that runs the protected project, open it
 * in a browser (or run `php ioncuber-check.php`) and it tells you exactly which
 * loader build the server needs and how to enable it.
 *
 * It reads nothing sensitive and changes nothing.
 */

const IONCUBER_SITE = 'https://ioncuber.com';   // <- set this to your ioncuber site

$phpVersion = PHP_VERSION;
$branch     = PHP_MAJOR_VERSION . '.' . PHP_MINOR_VERSION;
$zts        = (defined('ZEND_THREAD_SAFE') && ZEND_THREAD_SAFE) ? 'ts' : 'nts';
$bits       = PHP_INT_SIZE * 8;
$arch       = $bits === 64 ? 'x64' : 'x86';
$osFamily   = PHP_OS_FAMILY;
$isWindows  = stripos(PHP_OS_FAMILY, 'Windows') !== false;
$ext         = $isWindows ? 'dll' : 'so';
$osSlug      = $isWindows ? 'windows' : 'linux';
$extDir     = (string) ini_get('extension_dir');
$iniFile    = php_ini_loaded_file() ?: '(none)';
$sapi       = PHP_SAPI;

$loaded  = extension_loaded('ioncuber');
$info    = ($loaded && function_exists('ioncuber_loader_info')) ? ioncuber_loader_info() : null;
$keys    = $info['keys'] ?? [];
$host    = $_SERVER['HTTP_HOST'] ?? null;

// Loaders are selected per PHP *branch* (8.2.x): every patch release of a branch
// shares the same Zend API, so any 8.2 build runs on any 8.2.x.
$needed   = sprintf('PHP %s - %s - %s - %s', $branch, $osSlug, $arch, strtoupper($zts));
$download = IONCUBER_SITE . '/loaders';

$cli = (PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg');
if ($cli) {
    echo "ioncuber server check\n";
    echo str_repeat('=', 40), "\n";
    printf("PHP            : %s (%s)\n", $phpVersion, $sapi);
    printf("PHP branch     : %s\n", $branch);
    printf("OS / arch      : %s / %s (%d bit)\n", $osFamily, $arch, $bits);
    printf("thread safety  : %s\n", strtoupper($zts));
    printf("php.ini        : %s\n", $iniFile);
    printf("extension_dir  : %s\n", $extDir);
    printf("loader loaded  : %s\n", $loaded ? 'yes' : 'NO');
    if ($loaded) {
        printf("loader version : %s\n", $info['loader'] ?? '?');
        printf("loader keys    : %s\n", implode(', ', $keys));
    }
    printf("needed loader  : %s\n", $needed);
    printf("               (any %s.x build works - pick the %s/%s/%s one)\n", $branch, $osSlug, $arch, strtoupper($zts));
    printf("download page  : %s\n", $download);
    echo "\nini lines to add:\n";
    echo $isWindows
        ? "  extension_dir = \"$extDir\"\n  extension=ioncuber   ; after renaming the file to php_ioncuber.dll\n"
        : "  extension=ioncuber.so\n";
    exit($loaded ? 0 : 1);
}

if (!headers_sent()) {
    header('Content-Type: text/html; charset=utf-8');
    http_response_code($loaded ? 200 : 500);
}

$e = fn($v) => htmlspecialchars((string) $v, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
?>
<!doctype html>
<html lang="fa" dir="rtl">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>ioncuber server check</title>
<style>
  *{box-sizing:border-box;margin:0;padding:0}
  body{font-family:Segoe UI,Tahoma,Vazirmatn,Arial,sans-serif;background:linear-gradient(135deg,#0f172a,#312e81);
       min-height:100vh;display:flex;align-items:center;justify-content:center;padding:24px;line-height:2}
  .card{width:min(880px,100%);background:#fff;border-radius:18px;overflow:hidden;box-shadow:0 30px 90px rgba(0,0,0,.4)}
  header{padding:26px 28px;color:#fff;background:<?= $loaded ? 'linear-gradient(135deg,#16a34a,#22c55e)' : 'linear-gradient(135deg,#dc2626,#f97316)' ?>}
  header h1{font-size:26px;margin-bottom:6px}
  main{padding:24px 28px}
  table{width:100%;border-collapse:collapse;font-size:14px;margin-bottom:18px}
  th,td{padding:9px 12px;border-bottom:1px solid #e5e7eb;text-align:start}
  th{background:#f8fafc;width:220px;color:#374151}
  code{background:#0f172a;color:#34d399;padding:2px 7px;border-radius:6px;direction:ltr;display:inline-block;
       font-family:ui-monospace,Consolas,monospace;font-size:12.5px}
  .box{background:#f8fafc;border-inline-start:6px solid #4f46e5;border-radius:12px;padding:14px 16px;margin:14px 0}
  .box.warn{border-color:#f97316;background:#fff7ed}
  .box.ok{border-color:#16a34a;background:#f0fdf4}
  .btn{display:inline-block;padding:12px 20px;border-radius:999px;background:linear-gradient(135deg,#4f46e5,#7c3aed);
       color:#fff;text-decoration:none;font-weight:700;margin-top:6px}
  a{color:#4f46e5}
  footer{padding-top:16px;border-top:1px solid #e5e7eb;color:#6b7280;font-size:13px}
  .ltr{direction:ltr;text-align:left}
</style>
</head>
<body>
<div class="card">
  <header>
    <h1><?= $loaded ? '✅ ioncuber loader فعال است' : '❌ ioncuber loader نصب نیست' ?></h1>
    <p class="ltr"><?= $loaded ? 'The ioncuber loader is installed and working on this PHP build.' : 'This PHP build cannot run ioncuber protected files yet.' ?></p>
  </header>
  <main>
    <table>
      <tr><th>PHP version</th><td><code><?= $e($phpVersion) ?></code> (branch <code><?= $e($branch) ?></code>)</td></tr>
      <tr><th>OS / architecture</th><td><code><?= $e($osFamily) ?></code> / <code><?= $e($arch) ?></code> (<?= (int) $bits ?> bit)</td></tr>
      <tr><th>Thread safety</th><td><code><?= $e(strtoupper($zts)) ?></code> <?= $zts === 'ts' ? '(ZTS)' : '(NTS)' ?></td></tr>
      <tr><th>SAPI</th><td><code><?= $e($sapi) ?></code></td></tr>
      <tr><th>php.ini</th><td><code><?= $e($iniFile) ?></code></td></tr>
      <tr><th>extension_dir</th><td><code><?= $e($extDir) ?></code></td></tr>
      <tr><th>loader loaded</th><td><b><?= $loaded ? 'yes' : 'NO' ?></b><?= $loaded ? ' — version <code>' . $e($info['loader'] ?? '?') . '</code>' : '' ?></td></tr>
      <?php if ($loaded): ?>
      <tr><th>supported keys</th><td><code><?= $e(implode(', ', $keys)) ?></code></td></tr>
      <?php endif; ?>
      <?php if ($host): ?>
      <tr><th>request host</th><td><code><?= $e($host) ?></code></td></tr>
      <?php endif; ?>
    </table>

    <?php if ($loaded): ?>
      <div class="box ok">
        همه چیز درست است. فایل‌های محافظت‌شده روی همین PHP اجرا می‌شوند.
        <div class="ltr">Nothing to do. Protected files will run under this PHP build.</div>
      </div>
    <?php else: ?>
      <div class="box warn">
        <b>لودر مورد نیاز برای این سرور:</b> <code><?= $e($needed) ?></code><br>
        <span class="ltr">Any <?= $e($branch) ?>.x build works (same Zend API) - pick <code><?= $e($osSlug) ?></code> / <code><?= $e($arch) ?></code> / <code><?= $e(strtoupper($zts)) ?></code> on the loaders page.</span>
        <div class="ltr" style="margin-top:8px">
          Then add these lines to <code><?= $e($iniFile) ?></code>:
        </div>
        <div class="ltr" style="margin-top:8px">
          <?php if ($isWindows): ?>
            <code>extension_dir = "<?= $e($extDir) ?>"</code><br>
            <code>extension=ioncuber</code> &nbsp;<span style="color:#6b7280">(rename the file to <code>php_ioncuber.dll</code>)</span>
          <?php else: ?>
            <code>extension=ioncuber.so</code>
          <?php endif; ?>
        </div>
      </div>
      <a class="btn" href="<?= $e($download) ?>" target="_blank" rel="noopener">دانلود لودر / Download loader</a>
    <?php endif; ?>

    <footer>
      راهنما: <a href="<?= $e(IONCUBER_SITE) ?>/docs" target="_blank" rel="noopener"><?= $e(IONCUBER_SITE) ?>/docs</a>
      &middot; پشتیبانی: <?= $e(IONCUBER_SITE) ?>/loaders
      <div class="ltr">ioncuber &middot; <?= $loaded ? 'loader active' : 'loader missing' ?> &middot; <?= $e(date('Y-m-d H:i:s')) ?></div>
    </footer>
  </main>
</div>
</body>
</html>
