<?php
/**
 * VFM - veno file manager: admin-panel/view/admin-head.php
 * main php setup
 *
 * PHP version >= 5.3
 *
 * @category  PHP
 * @package   VenoFileManager
 * @author    Nicola Franchini <support@veno.it>
 * @copyright 2013 Nicola Franchini
 * @license   https://veno.es/shop/help Veno Commercial License
 * @link      http://filemanager.veno.it/
 */
$php_min = '7.4';
if (version_compare(phpversion(), $php_min, '<')) {
    // PHP version too low.
    header('Content-type: text/html; charset=utf-8');
    exit('<h2>Veno File Manager 4 requires PHP >= '.$php_min.'</h2><p>Current: PHP '.phpversion().', please update your server settings.</p>');
}
if (!defined('VFM_APP')) {
    return;
}

require_once 'config.php';

if ($_CONFIG['debug_mode'] === true) {
    error_reporting(E_ALL);
    ini_set('display_errors', 1);
} else {
    error_reporting(E_ALL ^ E_NOTICE);
}
if ($_CONFIG['firstrun'] === true) {
    header('Location:setup.php');
    exit;
}

require_once dirname(dirname(__DIR__)).'/translations/en.php';
require_once dirname(dirname(__DIR__)).'/class.php';
require_once dirname(dirname(__DIR__)).'/class/class.admin.php';
require_once dirname(dirname(__DIR__)).'/admin-panel/updater/class.vfmupdater.php';

$setUp = new SetUp();
$gateKeeper = new GateKeeper();
$gateKeeper->init('', '_admin');
$admin = new Admin();
$updater = new Updater();

$script_url = $setUp->getConfig('script_url', '../');

if (!$gateKeeper->isSuperAdmin()) {
    header('Location:'.$script_url);
    exit;
}

global $baselang;
$baselang = $_TRANSLATIONS;

$jsonindex = "translations/index.json";
global $translations_index;
$translations_index = json_decode(file_get_contents($jsonindex), true);

$posteditlang = filter_input(INPUT_POST, "editlang", FILTER_SANITIZE_SPECIAL_CHARS);
$postnewlang = filter_input(INPUT_POST, "newlang", FILTER_SANITIZE_SPECIAL_CHARS);
$thelang = ($posteditlang ? $posteditlang : "en");
$thenewlang = ($postnewlang ? $postnewlang : null);
$editlang = ($thenewlang ? $thenewlang : $thelang);

global $_TRANSLATIONSEDIT;

if (file_exists('_content/translations/'.$editlang.'.php')) {
    include '_content/translations/'.$editlang.'.php';
} elseif (file_exists('translations/'.$editlang.'.php')) {
    include 'translations/'.$editlang.'.php';
}

if ($posteditlang) {
    $_TRANSLATIONSEDIT = $_TRANSLATIONS;
} else {
    $_TRANSLATIONSEDIT = $baselang;
}
$lang = $setUp->lang;
// require 'translations/'.$lang.'.php';

global $translations;
$translations = $admin->getLanguages();
$activesec = "home";

$allsections = array(
    'superadmin_can_statistics' => 'appearance',
    'superadmin_can_users' => 'users',
    'superadmin_can_translations' => 'translations',
    'superadmin_can_statistics' => 'logs',
);

$get_section = filter_input(INPUT_GET, 'section', FILTER_SANITIZE_SPECIAL_CHARS);
$get_action = filter_input(INPUT_GET, 'action', FILTER_SANITIZE_SPECIAL_CHARS);

if (!$get_section && !$gateKeeper->canSuperAdmin('superadmin_can_preferences') ) {

    $default_section = false;

    foreach ($allsections as $key => $section) {
        if ($gateKeeper->canSuperAdmin($key)) {
            $default_section = $section;
            break;
        }
    }
    if ($default_section) {
        $get_section = $default_section;
    }
}

$lang_direction = $setUp->getLangDirection();
$rtl_ext = $lang_direction['ext'];
$rtl_att = $lang_direction['att'];
$rtl_class = $lang_direction['class'];

// Mappa: sezione => permesso richiesto
$sections_map = [
    'appearance'   => 'superadmin_can_appearance',
    'users'        => 'superadmin_can_users',
    'translations' => 'superadmin_can_translations',
    'updates'      => 'superadmin_can_updates',
    'logs'         => 'superadmin_can_statistics',
];

// Controllo centralizzato
if ($get_section !== null && isset($sections_map[$get_section])) {
    $permesso = $sections_map[$get_section];
    
    if ($gateKeeper->canSuperAdmin($permesso)) {
        switch ($get_section) {
        case 'appearance':
            $activesec = 'appearance';
            include_once __DIR__.'/admin-head-appearance.php';
            break;
        case 'users':
            $activesec = 'users';
            include_once __DIR__.'/admin-head-users.php';
            break;
        case 'logs':
            $activesec = 'logs';
            break;
        case 'translations':
            $activesec = 'lang';
            include_once __DIR__.'/language/langlist.php';
            include_once __DIR__.'/admin-head-translations.php';
            break;
        case 'updates':
            $activesec = 'updates';
            include_once dirname(__DIR__).'/updater/admin-head-updates.php';
            break;
        }
    } else {
        // Gestione errore UX: l'utente vede il pannello ma non la sezione
        Utils::setError("Non hai i permessi per accedere a questa sezione.");
    }
} else {
    // Default: General Settings
    if ($gateKeeper->canSuperAdmin('superadmin_can_preferences')) {
        include_once __DIR__.'/admin-head-settings.php';
    }
}
