<?php
if (session_status() !== PHP_SESSION_ACTIVE) {
  session_start();
}

if (!function_exists('thunder_admin_is_admin_area')) {
  function thunder_admin_is_admin_area(): bool {
    $phpSelf = (string)($_SERVER['PHP_SELF'] ?? '');
    $script = (string)($_SERVER['SCRIPT_NAME'] ?? '');
    return strpos($phpSelf, '/admin/') !== false || strpos($script, '/admin/') !== false;
  }
}

if (!function_exists('thunder_site_prefix')) {
  function thunder_site_prefix(): string {
    return thunder_admin_is_admin_area() ? '../' : '';
  }
}

if (!function_exists('thunder_admin_href')) {
  function thunder_admin_href(string $path = 'index.php'): string {
    return thunder_site_prefix() . 'admin/' . ltrim($path, '/');
  }
}

if (!function_exists('thunder_site_href')) {
  function thunder_site_href(string $path = 'index.php'): string {
    return thunder_site_prefix() . ltrim($path, '/');
  }
}

if (!function_exists('thunder_redirect')) {
  function thunder_redirect(string $target): void {
    header('Location: ' . $target);
    exit;
  }
}

$timeout = 4 * 60 * 60;
if (isset($_SESSION['last_activity']) && (time() - (int)$_SESSION['last_activity']) > $timeout) {
  session_unset();
  session_destroy();
  thunder_redirect('login.php?timeout=1');
}
$_SESSION['last_activity'] = time();

if (!isset($_SESSION['user']) && !empty($_SESSION['admin_logged_in'])) {
  $_SESSION['user'] = $_SESSION['admin_user'] ?? 'admin';
  $_SESSION['role'] = $_SESSION['admin_role'] ?? $_SESSION['role'] ?? ((($_SESSION['admin_user'] ?? '') === 'admin') ? 'admin' : 'editor');
  if (!empty($_SESSION['admin_access']) && is_array($_SESSION['admin_access'])) {
    $perms = [];
    foreach ($_SESSION['admin_access'] as $f) {
      $perms[] = basename((string)$f, '.php');
    }
    $_SESSION['permissions'] = implode(',', array_unique($perms));
  }
}

if (isset($_SESSION['user']) && empty($_SESSION['admin_logged_in'])) {
  $_SESSION['admin_logged_in'] = true;
  $_SESSION['admin_user'] = $_SESSION['user'];
  $_SESSION['admin_role'] = $_SESSION['role'] ?? ((($_SESSION['admin_user'] ?? '') === 'admin') ? 'admin' : 'editor');
  $base = ['index.php','about.php','comments.php','djs.php','friends.php','requests.php','schedule.php','poll.php','logout.php'];
  $extra = ['newsreel_admin.php','admin-webstats.php','create_user.php','applications.php','contact_messages.php','backup.php','chat.php','events.php','fans.php','media.php','navigation.php','pages.php','player.php','radiostats.php','top50.php','users.php','westats.php','news.php','contact.php'];
  $_SESSION['admin_access'] = array_unique(array_merge($base, $extra));
}

if (!isset($_SESSION['user']) && empty($_SESSION['admin_logged_in'])) {
  thunder_redirect('login.php');
}

$page = basename((string)($_SERVER['PHP_SELF'] ?? ''), '.php');
$adminOnly = ['users'];
$role = $_SESSION['admin_role'] ?? $_SESSION['role'] ?? ((($_SESSION['admin_user'] ?? '') === 'admin') ? 'admin' : 'editor');
if ($role !== 'admin' && in_array($page, $adminOnly, true)) {
  http_response_code(403);
  die("<div class='container mt-5'><div class='alert alert-danger'>Access denied: This page is for administrators only.</div></div>");
}
$GLOBALS['CAN_VIEW'] = true;
if ($role === 'admin') {
  $GLOBALS['CAN_EDIT'] = true;
} else {
  $editablePages = array_filter(array_map('trim', explode(',', (string)($_SESSION['permissions'] ?? ''))));
  $GLOBALS['CAN_EDIT'] = in_array($page, $editablePages, true);
}
?>
