Initial commit
This commit is contained in:
110
rss/php/classes/application/Frontend.php
Normal file
110
rss/php/classes/application/Frontend.php
Normal file
@@ -0,0 +1,110 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
||||
63
rss/php/classes/application/user/Auth.php
Normal file
63
rss/php/classes/application/user/Auth.php
Normal file
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
namespace Vor\application\user;
|
||||
|
||||
use Vor\core\Sys;
|
||||
use Vor\core\Main;
|
||||
use Exception;
|
||||
|
||||
class Auth{
|
||||
public $username;
|
||||
public $email;
|
||||
public $password;
|
||||
|
||||
public static function isAuth(){
|
||||
$authData = $_SERVER['VOR_AUTH'] ?? false;
|
||||
|
||||
if(!$authData){
|
||||
return false;
|
||||
}
|
||||
|
||||
return (int)Sys::session('uid') === (int)$authData['uid'];
|
||||
}
|
||||
|
||||
public function login(){
|
||||
if(isset($this->email) && isset($this->password)){
|
||||
$userData = Main::select('users', ['email', 'username', 'password', 'id'], ['email' => trim($this->email)]);
|
||||
if(!$userData){
|
||||
return false;
|
||||
}
|
||||
|
||||
if(password_verify($this->password, $userData['password'])){
|
||||
$sid = bin2hex(random_bytes(16));
|
||||
|
||||
$payload = [
|
||||
'uid' => (int)$userData['id'],
|
||||
'sid' => $sid,
|
||||
'exp' => time() + 86400
|
||||
];
|
||||
|
||||
if(Sys::cookieSet('v_auth', $payload)){
|
||||
Sys::validateSession($sid);
|
||||
|
||||
Sys::session('uid', (int)$userData['id']);
|
||||
Sys::session('logged_in_at', time());
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function logout(){
|
||||
Sys::cookieClear('v_auth');
|
||||
if(session_status() === PHP_SESSION_ACTIVE){
|
||||
session_unset();
|
||||
session_destroy();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
?>
|
||||
Reference in New Issue
Block a user