<?php
define('ADMIN_PASSWORD', 'LEXUSis350');

define('DATA_DIR', __DIR__ . '/data');
define('SECTIONS_DIR', DATA_DIR . '/sections');
define('TASKBAR_FILE', DATA_DIR . '/taskbar.json');

ini_set('display_errors', '0');
header_remove('X-Powered-By');

if (!is_dir(DATA_DIR)) mkdir(DATA_DIR, 0775, true);
if (!is_dir(SECTIONS_DIR)) mkdir(SECTIONS_DIR, 0775, true);

if (session_status() === PHP_SESSION_NONE) session_start();

function is_admin(): bool {
  return !empty($_SESSION['is_admin']);
}

function require_admin() {
  if (!is_admin()) {
    http_response_code(401);
    echo json_encode(['ok'=>false,'error'=>'Unauthorized']);
    exit;
  }
}

function json_out($data, int $code=200) {
  http_response_code($code);
  header('Content-Type: application/json');
  echo json_encode($data);
  exit;
}

function read_json_body(): array {
  $raw = file_get_contents('php://input');
  return json_decode($raw, true) ?: [];
}

function safe_id(string $id): string {
  return preg_replace('/[^a-zA-Z0-9_-]/','',$id);
}
?>
