<?php


# Абсолютный путь
$path = dirname(__FILE__) . '/';

/* 
if(isset($_SESSION)) {
    , $_SESSION['managerClipboardFile']['path'] . " " . $_SESSION['managerClipboardFile']['action'] . " " . session_id() . PHP_EOL, FILE_APPEND);
file_put_contents($path . '/error.txt', "" . PHP_EOL, FILE_APPEND);
file_put_contents($path . '/error.txt', $_SESSION['username'] . " " . print(adminsConfig()) . PHP_EOL, FILE_APPEND);

}*/

session_start();

$_SESSION['access'] = true;

/* if(!isset($_SESSION['managerClipboardFile'])) {
    $_SESSION['managerClipboardFile']['path'] = 0;
}
$_SESSION['managerClipboardFile']['path']++; */

# Подключение библиотеки функций
include_once $path . 'data/func.php';

# Подключение конфигов 
SetConfig();

$config['server']       ='http://'.$_SERVER['HTTP_HOST'].'/';
//echo $config['server'];
if ($_SERVER['REQUEST_URI']=='/')
$REQUESTxURI            =GetRequestURL('/index.html');
else
$REQUESTxURI            =GetRequestURL($_SERVER['REQUEST_URI']);
$config['lng']          = SetLng();

$REQUEST_URI            =$REQUESTxURI['str'];
$config['REQUEST_URI']  =$_SERVER['REQUEST_URI'];
$ansv                   =URLstr($path . 'data/filepath.'.$config['lng'].'.php',$REQUEST_URI);
$config['path']         =$path;
//----------------------------------
$config['request_url']  =$REQUEST_URI;
$config['template']     =$ansv['template'];
$config['sitename']     =$ansv['sitename'];
$config['title'] = $ansv['title'];
$config['smap']         =$ansv['URLLine'];
$config['page_url'] = $ansv['FileURL'];
if (!isset($_SESSION['page_url'])) {
    $_SESSION['page_url'] = $config['page_url'];
}
$config['LngMenu'] = LngMenu();
$configAdmins = adminsConfig();

//if ($REQUESTxURI['act']=='edit' && $REQUESTxURI['act']!=''){
//echo $editXML->saveXML();
//}
//else{

$menu =explode(',',$ansv['page']['PageMenu']);

$config['TopMenu']      =GetXMLMenu($ansv['XML']->index,$REQUEST_URI,$menu[0],'h');
$config['sideMenu'] = GetSideXMLMenu($ansv['XML']->index,$REQUEST_URI,$menu[0]);
$config['SiteMenu']     =GetXMLMenu($ansv['XML']->index,$REQUEST_URI,$menu[1],'h');

// && $config['REQUEST_URI']=='/'
if (empty($config['TopMenu'])) {    
    if ($_SESSION['Login'] == 'false') {    
        $_SESSION['formType'] = 'log_err';
    } else {                                
        $_SESSION['formType']= 'log_on';
    }
}

# Подключение страницы или вывод ошибки
if (is_file($path . $config['page_url'].".page.php")) {
$xmlstr                 = simplexml_load_file($path . $config['page_url'].".page.php");
} else {
    if (!isset($_GET['handleJsonRpcRequestAction'])) {
        error405(true, $config['encoding']);
    }
}

//$EditPage['url']      =$path .'content/scan/index.page.php';
//$EditPage['page']     =simplexml_load_file($EditPage['url']);
$config['sitename']     .=' > '.$config['title']; 
$config['ctitle']       =$xmlstr->content['tclass'];
$config['cfloat']       =$xmlstr->content['bclass'];
$access                 =FindPageUser($ansv['page']['users'],$_SESSION['username']);
if ($access) {
    # Формируем левые блоки
    $config['left'] = GetBlock($xmlstr->lblock->block, 'left');
    $config['left'] = "<div id='left-float'>" . $config['left'] . "</div>";
    
    # Формируем правые блоки
    $config['right'] = GetBlock($xmlstr->rblock->block, 'right');
    $config['right'] = "<div id='right-float'>" . $config['right'] . "</div>";
    
    # Формируем центральные блоки
    $center = '<div class="center-float">' . GetBlock($xmlstr->cblock->block, 'center') . '<div>';
    $config['content'] = $xmlstr->content->{$config['lng']};
} else {
    if ($_SESSION['log_in']) {
        $xmlstr = simplexml_load_file($path . 'content/adm/user.page.php');
    } else {
        $xmlstr = simplexml_load_file($path . 'content/adm/access.page.php');
    }
    
    # Формируем левые блоки
    $config['left'] = '';
    
    # Формируем правые блоки
    $config['right'] = '';
    
    # Формируем центральные блоки
    $center = '<div class="center-float">' . GetBlock($xmlstr->cblock->block, 'center') . '<div>';
    $config['content'] = $xmlstr->content->$config['lng'];
}
foreach ($config as $key=>$value)if ($key!="request_url")
$config['content']      =str_replace('%'.$key.'%', $value, $config['content']);
$config['content']      =str_replace('%center%', $center, $config['content']);
$config['home']         ='index.html'; 

if (isset($_SESSION['username'])) {
    $config['User'] = $_SESSION['username'];
}
else{
    $config['User'] = 'Гость';
}

#Подключение шаблона, редактора и основных файлов
$html = "";
$html .= '<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">';
$html .= '<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>';
$html .= file_get_contents($path . 'data/top.php');
$html .= file_get_contents($path . 'template/'.$config['template'].'/page.php');
$html .= file_get_contents($path . 'data/foot.php');
$html .= '<link rel="stylesheet" href="/data/fonts/fonts.css">';

$lang = include $path . 'data/lang.php';
$lng = $_SESSION['lng'] ?? 'en';
$Js = file_get_contents($path . 'data/Basic_functions.js');
foreach ($lang[$lng] as $key => $value) {
    $Js = str_replace('{{' . $key . '}}', $value, $Js);
}
$html .= '<script>' . $Js . '</script>';

$html .= '<link rel="stylesheet" href="/data/Basic_functions.css">';
$html .= includePhp();

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $raw = file_get_contents('php://input');
    $input = json_decode($raw, true);

    if (json_last_error() === JSON_ERROR_NONE && !empty($input['handleJsonRpcRequestAction'])) {
        handleJsonRpcRequest($input['handleJsonRpcRequestAction']);
    }
}
if ($_SERVER['REQUEST_METHOD'] == 'POST' && $_POST['handleRequestAction']) {
    handleRequest($_POST['handleRequestAction']);
}

foreach ($config as $key=>$value)if ($key!="request_url")
    $html                   =str_replace('%'.$key.'%', $value, $html);

# Заголовок кодировки
header('Content-type: text/html; charset='. $config['encoding']);

if ($_POST['handleRequestAction'] && $_POST['handleRequestAction'] != 'API' && $_POST['handleRequestAction'] != 'managerClipboard') {
    exit;
}
if (!empty($input['handleJsonRpcRequestAction']) && $input['handleJsonRpcRequestAction'] != 'API' && $input['handleJsonRpcRequestAction'] != 'managerClipboard') {
    exit;
}

unset($_SESSION['page_url']);
if ($_POST['handleRequestAction'] == 'API' && !isset($_POST['lng'])) {
    exit;
} 
if (!empty($input['handleJsonRpcRequestAction']) && $input['handleJsonRpcRequestAction'] == 'API' && (!isset($input['params']['lng']) || $input['params']['lng'] === '')) {
    exit;
}

if ($_POST['handleRequestAction'] || !empty($input['handleJsonRpcRequestAction'])) {
    if ($_POST['handleRequestAction'] != 'managerClipboard' && $input['handleJsonRpcRequestAction'] != 'managerClipboard') {
        echo $html;
    }
} else {
    echo $html;
}

/* $_SESSION['managerClipboardFile'] = $_SESSION['managerClipboardFile']; */

//var_dump($REQUEST_URI);
//echo password_hash("gena", PASSWORD_DEFAULT); //a004b486e740bb4580a04125975893b0 gettype(
//}s
?>

