110 lines
3.7 KiB
PHP
110 lines
3.7 KiB
PHP
|
|
<?php
|
||
|
|
namespace Vor\application;
|
||
|
|
|
||
|
|
use Vor\core\Sys;
|
||
|
|
use Vor\core\Main;
|
||
|
|
|
||
|
|
class Frontend{
|
||
|
|
|
||
|
|
public static function render() {
|
||
|
|
Sys::start();
|
||
|
|
$pageName = $_GET['page'] ?? 'index';
|
||
|
|
$jsonPath = BASE . "/rss/json/pages/$pageName.json";
|
||
|
|
|
||
|
|
if (!file_exists($jsonPath)) {
|
||
|
|
http_response_code(404);
|
||
|
|
$pageName = '404';
|
||
|
|
$jsonPath = BASE . "/rss/json/pages/404.json";
|
||
|
|
}
|
||
|
|
|
||
|
|
$conf = json_decode(file_get_contents($jsonPath), true);
|
||
|
|
|
||
|
|
if (!self::validate($conf)) {
|
||
|
|
header('Location: ' . $conf['rules']['redirect_login']);
|
||
|
|
exit;
|
||
|
|
}
|
||
|
|
|
||
|
|
self::authRedirect($conf);
|
||
|
|
|
||
|
|
$viewData = [];
|
||
|
|
if (isset($conf['init']) && is_array($conf['init'])) {
|
||
|
|
foreach ($conf['init'] as $task) {
|
||
|
|
$viewData[$task['return']] = self::execute($task);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
$validatedScripts = [];
|
||
|
|
|
||
|
|
$pageScript = "/rss/js/pages/{$conf['layout']['body']}.js";
|
||
|
|
if(file_exists(BASE . $pageScript)){
|
||
|
|
$validatedScripts[] = ['src' => $pageScript, 'type' => 'module'];
|
||
|
|
}
|
||
|
|
|
||
|
|
foreach(($conf['scripts'] ?? []) as $s){
|
||
|
|
if($src = self::getScriptPath($s)){
|
||
|
|
$validatedScripts[] = ['src' => $src, 'type' => $s['type'] == 'module' ? 'module' : 'text/javascript'];
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
return [
|
||
|
|
'header' => BASE . "/rss/php/views/headers/" . ($conf['layout']['header'] ?? 'default') . ".php",
|
||
|
|
'view' => BASE . "/rss/php/views/pages/$pageName.php",
|
||
|
|
'footer' => BASE . "/rss/php/views/footers/" . ($conf['layout']['footer'] ?? 'default') . ".php",
|
||
|
|
'data' => self::clean($viewData),
|
||
|
|
'scripts' => $validatedScripts,
|
||
|
|
'conf' => $conf
|
||
|
|
];
|
||
|
|
}
|
||
|
|
|
||
|
|
private static function validate($c) {
|
||
|
|
$restricted = $c['rules']['restricted'] ?? false;
|
||
|
|
if (!$restricted) return true;
|
||
|
|
|
||
|
|
return $_SERVER['VOR_AUTH'];
|
||
|
|
}
|
||
|
|
|
||
|
|
private static function authRedirect($c){
|
||
|
|
$loginRestricted = $c['rules']['login_restricted'] ?? false;
|
||
|
|
if($loginRestricted && isset($_SERVER['VOR_AUTH'])){
|
||
|
|
header('location: ' . $c['rules']['login_redirect'] ?? '/');
|
||
|
|
exit();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
private static function execute($f) {
|
||
|
|
$className = "\\Vor\\application\\" . $f['class'];
|
||
|
|
|
||
|
|
if (class_exists($className)) {
|
||
|
|
$instance = new $className();
|
||
|
|
$method = $f['function'];
|
||
|
|
|
||
|
|
if (method_exists($instance, $method)) {
|
||
|
|
return $instance->$method();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function clean($data){
|
||
|
|
if(is_array($data)){
|
||
|
|
return array_map([self::class, 'clean'], $data);
|
||
|
|
}
|
||
|
|
return htmlspecialchars(trim((string)$data), ENT_QUOTES, 'UTF-8');
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function loadScripts($conf) {
|
||
|
|
if (isset($conf['scripts']) && is_array($conf['scripts'])) {
|
||
|
|
foreach ($conf['scripts'] as $script) {
|
||
|
|
echo '<script src="/rss/js/' . $script . '.js"></script>' . PHP_EOL;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function getScriptPath($script){
|
||
|
|
$folder = ($script['type'] ?? '') === 'module' ? 'modules' : 'scripts';
|
||
|
|
$name = $script['name'];
|
||
|
|
$path = "/rss/js/$folder/$name";
|
||
|
|
|
||
|
|
return file_exists(BASE . $path) ? $path : null;
|
||
|
|
}
|
||
|
|
}
|