GitGram — setup.php — GitGram
IndexGram / main / indexgram_v5.00 / setup.php6,711 B↓ Raw
<?php
// IndexGram — First-run setup wizard
require_once __DIR__ . '/config.php';

// Already installed?
if (IS_INSTALLED) {
    header('Location: ' . AUTO_BASE_URL . '/admin/'); exit;
}

require_once ROOT_PATH . '/includes/db.php';
require_once ROOT_PATH . '/includes/functions.php';

// Ensure data dir is writable
$errors = [];
if (!is_writable(DATA_PATH)) {
    $errors[] = 'The <code>data/</code> directory is not writable. Please run: <code>chmod 750 data/</code>';
}
if (!is_writable(UPLOADS_PATH)) {
    $errors[] = 'The <code>uploads/</code> directory is not writable. Please run: <code>chmod 755 uploads/</code>';
}

$step = (int)($_GET['step'] ?? 1);
$done = false;

if ($_SERVER['REQUEST_METHOD'] === 'POST' && empty($errors)) {
    $step = (int)($_POST['step'] ?? 1);

    if ($step === 1) {
        // Save basic settings
        $siteTitle = trim($_POST['site_title'] ?? 'IndexGram');
        $siteDesc  = trim($_POST['site_description'] ?? '');
        $baseUrl   = rtrim(trim($_POST['base_url'] ?? AUTO_BASE_URL), '/');

        set_setting('site_title',    $siteTitle ?: 'IndexGram');
        set_setting('og_title',      $siteTitle ?: 'IndexGram');
        set_setting('site_description', $siteDesc);
        set_setting('og_description',   $siteDesc);
        set_setting('base_url',      $baseUrl);
        set_setting('footer_text',   trim($_POST['footer_text'] ?? 'IndexGram by Amfile.org of Page Telegram Volunteer Services v1.00 2026'));

        header('Location: setup.php?step=2'); exit;

    } elseif ($step === 2) {
        // Create admin user
        $username = trim($_POST['username'] ?? '');
        $email    = trim($_POST['email'] ?? '');
        $password = $_POST['password'] ?? '';
        $confirm  = $_POST['confirm']  ?? '';

        if (strlen($username) < 3) $errors[] = 'Username must be at least 3 characters.';
        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) $errors[] = 'Invalid email address.';
        if (strlen($password) < 8) $errors[] = 'Password must be at least 8 characters.';
        if ($password !== $confirm) $errors[] = 'Passwords do not match.';

        if (empty($errors)) {
            $hash = password_hash($password, PASSWORD_DEFAULT);
            db()->prepare(
                "INSERT INTO users (username, email, password_hash, role) VALUES (?,?,?,'admin')"
            )->execute([$username, $email, $hash]);

            // Seed default OS/2 theme into DB now that CSS file may exist
            $themeCount = db()->query("SELECT COUNT(*) FROM themes")->fetchColumn();
            if ($themeCount == 0) {
                $cssPath = ROOT_PATH . '/assets/css/os2.css';
                $css = file_exists($cssPath) ? file_get_contents($cssPath) : '/* OS/2 theme */';
                db()->prepare("INSERT INTO themes (name, css_content, is_active) VALUES (?, ?, 1)")
                    ->execute(['OS/2 Warp 3.0 (Default)', $css]);
            }

            // Mark installed
            file_put_contents(DATA_PATH . '/installed', date('Y-m-d H:i:s'));
            header('Location: admin/'); exit;
        }
    }
}
?><!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>IndexGram Setup</title>
<link rel="stylesheet" href="assets/css/os2.css">
<style>
  body { background:#008080; font-family:Arial,Helvetica,sans-serif; }
  .setup-win { max-width:580px; margin:40px auto; }
  .setup-win .win-body { padding:20px; }
  label { display:block; margin:10px 0 3px; font-weight:bold; font-size:13px; }
  input[type=text],input[type=email],input[type=password],input[type=url],textarea {
    width:100%; padding:4px 6px; box-sizing:border-box;
    border:2px solid; border-color:#808080 #FFF #FFF #808080;
    font-size:13px; background:#FFF;
  }
  textarea { height:80px; resize:vertical; }
  .hint { font-size:11px; color:#555; margin:2px 0 8px; }
  .err { background:#FFFFCC; border:1px solid #CC0000; padding:8px; margin:10px 0; font-size:13px; }
  .steps { display:flex; gap:8px; margin-bottom:16px; }
  .step-badge { padding:3px 10px; font-size:12px; }
  .step-badge.active { background:#000080; color:#FFF; }
</style>
</head>
<body>
<div class="setup-win">
  <div class="window">
    <div class="win-titlebar">
      <span>&#9632; IndexGram Setup Wizard</span>
    </div>
    <div class="win-body">
      <div class="steps">
        <span class="step-badge button <?= $step===1?'active':'' ?>">1. Site Info</span>
        <span class="step-badge button <?= $step===2?'active':'' ?>">2. Admin Account</span>
      </div>

      <?php if ($errors): ?>
        <div class="err"><strong>Please fix the following:</strong><ul>
          <?php foreach ($errors as $e): ?><li><?= $e ?></li><?php endforeach; ?>
        </ul></div>
      <?php endif; ?>

      <?php if ($step === 1): ?>
      <h3 style="margin-top:0">Step 1 — Site Information</h3>
      <form method="post">
        <input type="hidden" name="step" value="1">
        <label>Site Title</label>
        <input type="text" name="site_title" value="IndexGram" required>
        <label>Site Description</label>
        <textarea name="site_description">A digital file cabinet for organized knowledge — accessible, printable, and aggregatable.</textarea>
        <label>Base URL <span class="hint">(no trailing slash)</span></label>
        <input type="url" name="base_url" value="<?= htmlspecialchars(AUTO_BASE_URL) ?>" required>
        <label>Footer Text</label>
        <input type="text" name="footer_text" value="IndexGram by Amfile.org of Page Telegram Volunteer Services v1.00 2026">
        <p style="margin-top:16px">
          <button type="submit" class="button">Next: Admin Account &rarr;</button>
        </p>
      </form>

      <?php elseif ($step === 2): ?>
      <h3 style="margin-top:0">Step 2 — Create Admin Account</h3>
      <form method="post">
        <input type="hidden" name="step" value="2">
        <label>Username</label>
        <input type="text" name="username" autocomplete="username" required minlength="3">
        <label>Email</label>
        <input type="email" name="email" autocomplete="email" required>
        <label>Password <span class="hint">(min 8 characters)</span></label>
        <input type="password" name="password" autocomplete="new-password" required minlength="8">
        <label>Confirm Password</label>
        <input type="password" name="confirm" autocomplete="new-password" required>
        <p style="margin-top:16px">
          <button type="submit" class="button">Finish Setup &amp; Enter Admin Panel &rarr;</button>
        </p>
      </form>
      <?php endif; ?>

    </div>
  </div>
</div>
</body>
</html>
Ready
GitGram