Your IP : 216.73.217.78


Current Path : /home/seto/testhive.pm/admin/
Upload File :
Current File : /home/seto/testhive.pm/admin/settings.php

<?php
require_once __DIR__ . '/../includes/auth.php';
require_once __DIR__ . '/../includes/db.php';
require_once __DIR__ . '/_layout.php';

$msg = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // Regenerate global API key
    if (isset($_POST['regen_api_key'])) {
        $newKey = bin2hex(random_bytes(16));
        db()->prepare("INSERT OR REPLACE INTO config (key,value) VALUES ('global_api_key',?)")->execute([$newKey]);
        // clear Redis cache
        require_once __DIR__ . '/../includes/redis.php';
        cache_del('global_api_key');
        $msg = 'ok:API key обновлён';
    }

    // Change password
    if (!empty($_POST['new_password'])) {
        $new  = $_POST['new_password'];
        $conf = $_POST['confirm_password'] ?? '';
        if ($new !== $conf) {
            $msg = 'error:Пароли не совпадают';
        } elseif (strlen($new) < 6) {
            $msg = 'error:Пароль должен быть не менее 6 символов';
        } else {
            $hash = password_hash($new, PASSWORD_DEFAULT);
            db()->prepare("INSERT OR REPLACE INTO config (key,value) VALUES ('admin_password_hash',?)")->execute([$hash]);
            $msg = 'ok:Пароль изменён';
        }
    }

    // Global show_percent
    if (isset($_POST['global_show_percent'])) {
        $pct = max(0, min(100, (int)$_POST['global_show_percent']));
        db()->prepare("INSERT OR REPLACE INTO config (key,value) VALUES ('global_show_percent',?)")->execute([$pct]);
        if (!$msg) $msg = 'ok:Настройки сохранены';
    }

    // Blacklist: replace all
    if (isset($_POST['blacklist'])) {
        $patterns = array_filter(array_map('trim', explode("\n", $_POST['blacklist'])));
        db()->exec("DELETE FROM url_blacklist");
        $ins = db()->prepare("INSERT OR IGNORE INTO url_blacklist (pattern) VALUES (?)");
        foreach ($patterns as $p) $ins->execute([$p]);
        if (!$msg) $msg = 'ok:Настройки сохранены';
    }

    if ($msg) { header('Location: /admin/settings.php?msg=' . urlencode($msg)); exit; }
}

$globalPct = db_config('global_show_percent') ?? '30';
$globalKey  = db_config('global_api_key') ?? '—';
$blacklist  = db()->query("SELECT pattern FROM url_blacklist ORDER BY pattern")->fetchAll(PDO::FETCH_COLUMN);
$flashMsg   = $_GET['msg'] ?? '';

layout_head('Настройки');
?>
<?php if ($flashMsg): ?>
  <?php [$type, $text] = explode(':', $flashMsg, 2) ?>
  <div class="alert alert-<?= $type === 'ok' ? 'success' : 'danger' ?>"><?= htmlspecialchars($text) ?></div>
<?php endif ?>

<div class="row g-4" style="max-width:700px">

  <div class="col-12">
    <div class="card shadow-sm">
      <div class="card-body">
        <h6 class="card-title">Глобальный API Key <span class="text-muted small">(один для всех доноров)</span></h6>
        <div class="d-flex align-items-center gap-2 mb-2">
          <code class="bg-light px-2 py-1 rounded" id="api-key-val" style="letter-spacing:.05em"><?= htmlspecialchars($globalKey) ?></code>
          <button class="btn btn-sm btn-outline-secondary" onclick="navigator.clipboard.writeText('<?= htmlspecialchars($globalKey) ?>')">Копировать</button>
        </div>
        <form method="post" class="d-inline" onsubmit="return confirm('Сгенерировать новый ключ? Все доноры должны будут обновить сниппет.')">
          <input type="hidden" name="regen_api_key" value="1">
          <button class="btn btn-sm btn-outline-warning">Перегенерировать ключ</button>
        </form>
      </div>
    </div>
  </div>

  <div class="col-12">
    <div class="card shadow-sm">
      <div class="card-body">
        <h6 class="card-title">Глобальный show_percent</h6>
        <form method="post" class="d-flex gap-2 align-items-center">
          <input type="number" name="global_show_percent" class="form-control" style="width:100px"
                 min="0" max="100" value="<?= htmlspecialchars($globalPct) ?>">
          <button class="btn btn-primary">Сохранить</button>
        </form>
      </div>
    </div>
  </div>

  <div class="col-12">
    <div class="card shadow-sm">
      <div class="card-body">
        <h6 class="card-title">URL Blacklist <span class="text-muted small">(prefix-match, по одному на строку)</span></h6>
        <form method="post">
          <textarea name="blacklist" class="form-control font-monospace mb-2" rows="8"><?= htmlspecialchars(implode("\n", $blacklist)) ?></textarea>
          <button class="btn btn-primary">Сохранить</button>
        </form>
      </div>
    </div>
  </div>

  <div class="col-12">
    <div class="card shadow-sm">
      <div class="card-body">
        <h6 class="card-title">Изменить пароль</h6>
        <form method="post" style="max-width:300px">
          <div class="mb-2">
            <input type="password" name="new_password" class="form-control" placeholder="Новый пароль" required minlength="6">
          </div>
          <div class="mb-2">
            <input type="password" name="confirm_password" class="form-control" placeholder="Повторите пароль" required>
          </div>
          <button class="btn btn-warning">Изменить пароль</button>
        </form>
      </div>
    </div>
  </div>

</div>
<?php layout_foot() ?>