rework structure

added Router
added .htaccess
removed API-Module => is not used
renamed batch to CRON
This commit is contained in:
Maurice Grönwoldt 2020-09-10 22:47:58 +02:00
parent ccf2f506f0
commit c33bb4cb3a
37 changed files with 680 additions and 190 deletions

7
.gitignore vendored
View file

@ -461,5 +461,8 @@ MigrationBackup/
# real ignore for venom # real ignore for venom
config.inc.php conf/config.inc.php
module.inc.php conf/module.inc.php
conf/routers.inc.php
conf/lang.inc.php
logs/Exception.log

View file

@ -1,12 +1,12 @@
<?php <?php
use Venom\Config; use Venom\Core\Config;
use Venom\Database\DatabaseHandler; use Venom\Core\DatabaseHandler;
$config = Config::getInstance(); $config = Config::getInstance();
$config->setVersion(1.0); $config->setVersion(1.0);
$config->setDatabase([ $config->setDatabase([
DatabaseHandler::DB_TYPE => 'mysql', //please change only if you now what you're doing! this can break a lot. DatabaseHandler::DB_TYPE => 'mysql', //please change only if you know what you're doing! this can break a lot.
DatabaseHandler::DB_HOST => '127.0.0.1', DatabaseHandler::DB_HOST => '127.0.0.1',
DatabaseHandler::DB_PORT => '3306', //default port is 3306 DatabaseHandler::DB_PORT => '3306', //default port is 3306
DatabaseHandler::DB_USER => 'venom', DatabaseHandler::DB_USER => 'venom',
@ -15,26 +15,14 @@ $config->setDatabase([
DatabaseHandler::DB_EXTRA => '' // need to start with ';' DatabaseHandler::DB_EXTRA => '' // need to start with ';'
]); ]);
// used for external batch-mailing and error reporting.
// please generate login data at: api.vstz.dev
// api will check this data before processing
$config->setAPIAuth([
'useAPI' => true,
'url' => 'https://api.vstz.dev/v1/',
'user' => 'vstz',
'pw' => '6(f&B~ZxH3DfC#qJ',
'logsError' => true
]);
/** /**
* BATCH Mailing is something that will send only mails after a specific time like 1 min. * Cron Mailing is something that will send only mails after a specific time like 1 min.
* it is used to prevent spamming. * it is used to prevent spamming.
* batch mailing only works via API!... if API not used BatchMailing will disabled... or you have a cron that works like a batch... * CronMailing looks if the Same Mail is in the Database in the last 24 Hours! if it's already in then it will skip the sending!
*/ */
$config->setMail([ $config->setMail([
'useBatch' => true, //if true it will not send mails. 'useCron' => true, //if true it will not send mails directly.
'writeToDB' => true, //is needed for batch and is always true if batch is use 'writeToDB' => true, //is needed for cron and is always true if batch is use
//this stuff is only important if not using batch mailing!
'host' => 'localhost', 'host' => 'localhost',
'port' => '587', 'port' => '587',
'useTLS' => true, //use startTLS. is the default case ;) here it's important the security Cert is secure... 'useTLS' => true, //use startTLS. is the default case ;) here it's important the security Cert is secure...
@ -43,6 +31,13 @@ $config->setMail([
'from' => 'info@venom.io' 'from' => 'info@venom.io'
]); ]);
$config->setSecurity([
'useSecurity' => true, // should init the Security Module
'securityClass' => Venom\Security\BaseLogin::class, // Security class that is used
'secret' => 'venomDefaultSecret', // add to the hash.. ;) use HashingAlgo
'algo' => 'SHA256' // SHA256, SHA512...,
]);
// all templates are in __DIR__/tpl/ // all templates are in __DIR__/tpl/
// all themes are in __DIR__/public/theme/ // all themes are in __DIR__/public/theme/
$config->setRender([ $config->setRender([
@ -50,9 +45,11 @@ $config->setRender([
'baseFile' => 'base', //this will called after all templates are rendered... 'baseFile' => 'base', //this will called after all templates are rendered...
'useCache' => false, //is only on big systems good 'useCache' => false, //is only on big systems good
'cacheName' => 'defaultCache', //this is for bigger systems, ignore it 'cacheName' => 'defaultCache', //this is for bigger systems, ignore it
'uploadDir' => 'content/' 'uploadDir' => 'content/',
'useStaticUrl' => false,
]); ]);
$config->setEnableRouter(false);
$config->setMaintainMode(false); $config->setMaintainMode(false);
$config->setDevMode(true); $config->setDevMode(true);
$config->setBaseUrl(''); // can changed to something like that: https://example.com !not enter a / after the url! this will break all $config->setBaseUrl(''); // can changed to something like that: https://example.com !not enter a / after the url! this will break all

3
base/lang.base.php Normal file
View file

@ -0,0 +1,3 @@
<?php
require_once __DIR__ . '/../lang/de.php';

View file

@ -6,6 +6,4 @@ $modules = [];
// register controllers that can handle templates ;) need to have a render function for this // register controllers that can handle templates ;) need to have a render function for this
$controllers = [ $controllers = [
'test' => \Modules\TestController::class, 'test' => \Modules\TestController::class,
//api-controller
'apiMailer' => \Modules\API\APIMailer::class,
]; ];

29
base/router.base.php Normal file
View file

@ -0,0 +1,29 @@
<?php
// file is included by Setup! so it can Render direct into VenomCore!
// this routes are only for API use!
// if you not need the Router disable it in the Config then the Default Seo-Loader will only used
use Venom\Routing\Route;
use Venom\Routing\Router;
if (!isset($venom)) {
echo 'make sure Venom is loaded!';
exit(1);
}
$router = new Router('defaultRouter', 1.0, 'api/');
$router->addRoutes([
'/test' => [
'cl' => Route::class,
'roles' => ['ROLE_GUEST'],
'routes' => [
'*' => [
"GET" => 'getAll'
],
'1' => [
"GET" => 'getAll'
]
]
],
]);
$venom->addRouter('defaultRouter', $router);

View file

@ -1,3 +0,0 @@
<?php
require_once __DIR__ . '/lang/de.php';

View file

@ -1,6 +1,7 @@
<?php <?php
use Venom\Registry; use Venom\Core\Registry;
Registry::getInstance()->getLang()->registerLang('de', [ Registry::getInstance()->getLang()->registerLang('de', [
'HEADLINE' => 'VenomCMS', 'HEADLINE' => 'VenomCMS',
'TEST_TRANSLATION' => 'Das ist ein Test :)', 'TEST_TRANSLATION' => 'Das ist ein Test :)',

0
logs/.gitkeep Normal file
View file

7
public/.htaccess Normal file
View file

@ -0,0 +1,7 @@
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (/[^.]*|\.)$ [NC]
RewriteRule .* index.php [L]

7
public/admin/.htaccess Normal file
View file

@ -0,0 +1,7 @@
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (/[^.]*|\.)$ [NC]
RewriteRule .* index.php [L]

View file

@ -1,30 +1,24 @@
<?php <?php
use Venom\Config; use Venom\Core\Config;
use Venom\Core\Setup;
use Venom\Venom; use Venom\Venom;
require_once '../../vendor/autoload.php'; require_once '../../vendor/autoload.php';
if (!file_exists('../../config.inc.php')) { Setup::loadConfig(true);
copy('../../config.base.php', '../../config.inc.php'); Setup::loadLanguage();
echo 'Please change Config @ config.inc.php';
exit();
}
if (!file_exists('../../module.inc.php')) {
copy('../../module.base.php', '../../module.inc.php');
echo 'Please change Modules @ module.inc.php';
exit();
}
$config = Config::getInstance(); $config = Config::getInstance();
$config->setIsAdmin(true); if ($config->isMaintenance()) {
echo 'Currently not available';
require_once '../../config.inc.php'; exit;
require_once '../../module.inc.php'; }
//if devMode is on show all errors!
if ($config->isDevMode()) { if ($config->isDevMode()) {
error_reporting(E_ALL); error_reporting(E_ALL);
ini_set('error_reporting', E_ALL); ini_set('error_reporting', E_ALL);
} }
$venom = new Venom(); $venom = new Venom();
$venom->initModules($modules); Setup::loadRouters($venom);
$venom->initControllers($controllers); Setup::loadModules($venom);
$venom->run(); $venom->run();

View file

@ -1,22 +1,12 @@
<?php <?php
use Venom\Config; use Venom\Core\Config;
use Venom\Core\Setup;
use Venom\Venom; use Venom\Venom;
require_once '../vendor/autoload.php'; require_once '../vendor/autoload.php';
if (!file_exists('../config.inc.php')) { Setup::loadConfig(false);
copy('../config.base.php', '../config.inc.php'); Setup::loadLanguage();
echo 'Please change Config @ config.inc.php';
exit();
}
if (!file_exists('../module.inc.php')) {
copy('../module.base.php', '../module.inc.php');
echo 'Please change Modules @ module.inc.php';
exit();
}
require_once '../config.inc.php';
require_once '../module.inc.php';
require_once '../lang.php';
$config = Config::getInstance(); $config = Config::getInstance();
if ($config->isMaintenance()) { if ($config->isMaintenance()) {
@ -29,6 +19,6 @@ if ($config->isDevMode()) {
ini_set('error_reporting', E_ALL); ini_set('error_reporting', E_ALL);
} }
$venom = new Venom(); $venom = new Venom();
$venom->initModules($modules); Setup::loadRouters($venom);
$venom->initControllers($controllers); Setup::loadModules($venom);
$venom->run(); $venom->run();

View file

@ -1,7 +1,7 @@
<?php <?php
namespace Venom; namespace Venom\Core;
class ArgumentHandler class ArgumentHandler
@ -21,7 +21,7 @@ class ArgumentHandler
public static function get(): ArgumentHandler public static function get(): ArgumentHandler
{ {
if (self::$instance == null) { if (self::$instance === null) {
self::$instance = new ArgumentHandler(); self::$instance = new ArgumentHandler();
} }
return self::$instance; return self::$instance;

View file

@ -1,25 +1,29 @@
<?php <?php
namespace Venom; namespace Venom\Core;
use Venom\Database\DatabaseHandler; use Venom\Models\ConfigObject;
class Config class Config
{ {
private static ?Config $instance = null; private static ?Config $instance = null;
private bool $isWriteable = true; private bool $isWriteable = true;
private float $version = 1.0; private float $version = 1.0;
private array $api = []; private ConfigObject $mail;
private array $mail = []; private ConfigObject $renderer;
private array $renderer = []; private ConfigObject $security;
private bool $maintenance = false; private bool $maintenance = false;
private bool $devMode = false; private bool $devMode = false;
private bool $isAdmin = false; private bool $isAdmin = false;
private string $baseUrl = ''; private string $baseUrl = '';
private bool $seoMode = false; private bool $seoMode = false;
private bool $useRouter = false;
private function __construct() private function __construct()
{ {
$this->mail = new ConfigObject();
$this->renderer = new ConfigObject();
$this->security = new ConfigObject();
} }
public static function getInstance(): Config public static function getInstance(): Config
@ -39,16 +43,24 @@ class Config
} }
} }
public function setAPIAuth(array $array): void
{
$this->set('api', $array);
}
public function setRender(array $array): void public function setRender(array $array): void
{ {
$this->set('renderer', $array); $this->set('renderer', $array);
} }
public function set(string $variable, $value): void
{
if (!$this->isWriteable) {
trigger_error('try to write closed config!');
return;
}
if ($this->$variable instanceof ConfigObject) {
$this->$variable->setData($value);
} else {
$this->$variable = $value;
}
}
public function setMaintainMode(bool $mode): void public function setMaintainMode(bool $mode): void
{ {
$this->set('maintenance', $mode); $this->set('maintenance', $mode);
@ -64,12 +76,7 @@ class Config
$this->set('version', $param); $this->set('version', $param);
} }
public function getApi(): array public function getMail(): ConfigObject
{
return $this->api;
}
public function getMail(): array
{ {
return $this->mail; return $this->mail;
} }
@ -79,7 +86,7 @@ class Config
$this->set('mail', $array); $this->set('mail', $array);
} }
public function getRenderer(): array public function getRenderer(): ConfigObject
{ {
return $this->renderer; return $this->renderer;
} }
@ -115,15 +122,13 @@ class Config
$this->set('isAdmin', $isAdmin); $this->set('isAdmin', $isAdmin);
} }
public function setBaseUrl(string $url) { public function setSeoMode(bool $mode): void
$this->set('baseUrl', $url); {
}
public function setSeoMode(bool $mode) {
$this->set('seoMode', $mode); $this->set('seoMode', $mode);
} }
public function getSeoEnabled() { public function getSeoEnabled(): bool
{
return $this->seoMode; return $this->seoMode;
} }
@ -143,16 +148,34 @@ class Config
$this->isWriteable = false; $this->isWriteable = false;
} }
public function set(string $variable, $value) { public function getBaseUrl(): string
if (!$this->isWriteable) {
trigger_error('try to write closed config!');
return;
}
$this->$variable = $value;
}
public function getBaseUrl()
{ {
return $this->baseUrl; return $this->baseUrl;
} }
public function setBaseUrl(string $url): void
{
$this->set('baseUrl', $url);
}
public function getSecurity(): ConfigObject
{
return $this->security;
}
public function setSecurity(array $security): void
{
$this->set('security', $security);
}
public function setEnableRouter(bool $value): void
{
$this->set('useRouter', $value);
}
public function isRouterEnabled(): bool
{
return $this->useRouter;
}
} }

View file

@ -1,11 +1,12 @@
<?php <?php
namespace Venom\Database; namespace Venom\Core;
use PDO; use PDO;
use PDOException; use PDOException;
use Venom\Models\DatabaseObject;
/** /**
* Singleton DatabaseHandler... make sure we only have one connection to the database.. * Singleton DatabaseHandler... make sure we only have one connection to the database..

View file

@ -1,11 +1,11 @@
<?php <?php
namespace Venom; namespace Venom\Core;
use Venom\Database\DatabaseHandler; use RuntimeException;
use Venom\Database\DatabaseObject; use Venom\Models\DatabaseObject;
class Language class Language
{ {
@ -29,7 +29,7 @@ class Language
if (isset($data->id)) { if (isset($data->id)) {
$this->language = $lang; $this->language = $lang;
} else { } else {
throw new \RuntimeException("Language \"$lang\" not found"); throw new RuntimeException("Language \"$lang\" not found");
} }
} }

View file

@ -1,7 +1,7 @@
<?php <?php
namespace Venom; namespace Venom\Core;
interface Module interface Module

View file

@ -1,10 +1,9 @@
<?php <?php
namespace Venom; namespace Venom\Core;
use Venom\Routing\SeoController;
use Venom\SEO\SeoController;
/** /**
* Singleton Class... hold current URL => can * Singleton Class... hold current URL => can

58
src/Venom/Core/Setup.php Normal file
View file

@ -0,0 +1,58 @@
<?php
namespace Venom\Core;
use Venom\Venom;
class Setup
{
public static function loadConfig(bool $isAdmin): void
{
$config = Config::getInstance();
$config->setIsAdmin($isAdmin);
$file = self::tryLoading('config.inc.php', 'config.base.php', "Config");
require $file;
}
public static function tryLoading(string $file, string $baseFile, string $type): string
{
$newFile = __DIR__ . '/../../../conf/' . $file;
if (!file_exists($newFile)) {
$newBaseFile = __DIR__ . '/../../../base/' . $baseFile;
if (copy($newBaseFile, $newFile)) {
echo 'Created File for: ' . $type . '! Please Adjust the file';
} else {
echo 'Cannot create File for: ' . $type . '!';
}
exit(1);
}
return $newFile;
}
public static function loadModules(Venom $venom): void
{
$file = self::tryLoading('modules.inc.php', 'module.base.php', "Modules");
require $file;
if (isset($modules)) {
$venom->initModules($modules);
}
if (isset($controllers)) {
$venom->initControllers($controllers);
}
}
public static function loadRouters(Venom $venom): void
{
$file = self::tryLoading('routers.inc.php', 'router.base.php', "Routers");
require $file;
}
public static function loadLanguage(): void
{
$file = self::tryLoading('lang.inc.php', 'lang.base.php', "Languages");
require $file;
}
}

View file

@ -0,0 +1,48 @@
<?php
namespace Venom\Exceptions;
use Exception;
use Throwable;
class ExceptionHandler
{
private string $logFile = __DIR__ . '/../../../logs/Exception.log';
private $file;
public function __construct()
{
try {
$this->file = fopen($this->logFile, 'ab+');
} catch (Throwable $ex) {
$this->file = null;
}
}
public static function setExceptionHandler(): void
{
set_exception_handler(array(__CLASS__, 'handleException'));
}
public static function handleException(Throwable $ex): void
{
$handler = new ExceptionHandler();
$handler->writeException($ex);
exit(255);
}
public function writeException(Throwable $ex): void
{
if ($this->file !== null) {
try {
$trace = "=====[FATAL ERROR]=====\n" . $ex->getMessage() . "\n" . $ex->getTraceAsString() . "\n=====[FATAL ERROR END]=====\n";
fwrite($this->file, $trace);
fclose($this->file);
} catch (Exception $e) {
trigger_error("cannot write Exception file!");
}
}
}
}

View file

@ -0,0 +1,35 @@
<?php
namespace Venom\Helper;
use Venom\Core\ArgumentHandler;
class ErrorHandler
{
public static function setFatalError(): void
{
self::setError(500);
}
public static function setNotFound(): void
{
self::setError(404);
}
public static function setNoContent(): void
{
self::setError(204);
}
public static function setError(int $errorCode): void
{
http_response_code($errorCode);
ArgumentHandler::get()->setItem('cl', 'error');
ArgumentHandler::get()->setItem('fnc', 'handleError');
ArgumentHandler::get()->setItem('errorCode', $errorCode);
}
}

View file

@ -0,0 +1,14 @@
<?php
namespace Venom\Helper;
/**
* Class MetaGenerator
* @package Venom\Helper
*/
class MetaGenerator
{
}

View file

@ -0,0 +1,45 @@
<?php
namespace Venom\Helper;
class URLHelper
{
private static ?URLHelper $instance = null;
private string $parsedUrl;
public function __construct()
{
$this->parsedUrl = htmlspecialchars(parse_url($_SERVER['REQUEST_URI'])['path']);
}
public static function getInstance(): URLHelper
{
if (self::$instance === null) {
self::$instance = new self();
}
return self::$instance;
}
public function getUrl(): string
{
return $this->parsedUrl;
}
public function getUrlForId($id): string
{
return '';
}
public function getUrlForController($cl): string
{
return '';
}
public function generateFullUrl($url): string
{
return $url;
}
}

View file

@ -0,0 +1,38 @@
<?php
namespace Venom\Models;
class ConfigObject
{
private array $data = [];
public function __get($name)
{
if (isset($this->data[$name])) {
return $this->data[$name];
}
return null;
}
public function __set($name, $value)
{
$this->data[$name] = $value;
}
public function __isset($name)
{
return isset($this->data[$name]);
}
public function setData(array $data): void
{
$this->data = $data;
}
public function toString(): string
{
return implode(';', $this->data);
}
}

View file

@ -1,7 +1,7 @@
<?php <?php
namespace Venom\Database; namespace Venom\Models;
/** /**
@ -11,11 +11,7 @@ namespace Venom\Database;
*/ */
class DatabaseObject class DatabaseObject
{ {
/** private array $data = [];
* assoc array
* @var array
*/
private $data = [];
public function __get($name) public function __get($name)
{ {

14
src/Venom/Models/User.php Normal file
View file

@ -0,0 +1,14 @@
<?php
namespace Venom\Models;
class User
{
private string $username;
private string $password;
private string $salt;
private string $token;
private array $roles;
}

View file

@ -0,0 +1,10 @@
<?php
namespace Venom\Routing;
interface Route
{
}

View file

@ -0,0 +1,111 @@
<?php
namespace Venom\Routing;
use Exception;
class Router
{
protected string $id = 'defaultRouter';
protected int $version;
protected string $prefix = '';
protected array $routes = [];
public function __construct(string $id, int $version, string $prefix = '')
{
$this->id = $id;
$this->version = $version;
$this->prefix = $prefix;
}
public function addRoutes(array $routes): void
{
$this->routes = $routes;
}
public function addRoute(string $path, array $route): void
{
$this->routes[$path] = $route;
}
/*
* return matched route or null
*/
public function findRoute($url, $method): ?array
{
$url = $this->removeIfFirst($url, $this->prefix);
// check if full match... this can easily done if the url isset select the empty!
$method = strtoupper($method);
$route = $this->getRouteByName($url, $method);
if ($route !== null) {
return $route;
}
$url = $this->removeIfFirst($url, '/');
$baseRoute = $this->getNearestBaseRoute(explode("/", $url));
if ($baseRoute !== null) {
$count = count($baseRoute['params']);
return $this->getRouteByName($baseRoute['url'], $method, $count, $baseRoute['params']) ?? $this->getRouteByName($baseRoute['url'], $method);
}
return null;
}
private function removeIfFirst($rawString, $string)
{
if ($string !== '' && 0 === strpos($rawString, $string)) {
return substr($rawString, strlen($string));
}
return $rawString;
}
/* @todo implement Security Check if SecurityModule is used */
private function getRouteByName($url, $method, $subRoute = '*', $params = []): ?array
{
$routeAvailable = isset($this->routes[$url]);
$subRouteFound = isset($this->routes[$url]['routes'][$subRoute]);
$methodFound = isset($this->routes[$url]['routes'][$subRoute][$method]);
if ($routeAvailable && $subRouteFound && $methodFound) {
return [
'cl' => $this->routes[$url]['cl'],
'fnc' => $this->routes[$url]['routes'][$subRoute][$method],
'params' => $params
];
}
return null;
}
private function getNearestBaseRoute(array $params): ?array
{
$count = count($params);
$baseUrlArray = [];
$newParams = [];
foreach ($params as $value) {
$baseUrlArray[] = $value;
}
for ($i = 0; $i < $count; $i++) {
$newParams[] = array_pop($baseUrlArray);
$url = '/' . implode('/', $baseUrlArray);
if (isset($this->routes[$url])) {
return ['url' => $url, 'params' => $newParams];
}
}
return null;
}
public function tryFunctionCall(?array $aRoute): bool
{
if ($aRoute === null || !is_callable(array($aRoute['cl'], $aRoute['fnc']))) {
return false;
}
$route = new $aRoute['cl']();
try {
$fnc = $aRoute['fnc'];
$params = $aRoute['params'] ?? [];
$route->$fnc(...$params);
return true;
} catch (Exception $ex) {
return false;
}
}
}

View file

@ -1,12 +1,14 @@
<?php <?php
namespace Venom\SEO; namespace Venom\Routing;
use Venom\ArgumentHandler; use Venom\Core\ArgumentHandler;
use Venom\Config; use Venom\Core\Config;
use Venom\Database\DatabaseHandler; use Venom\Core\DatabaseHandler;
use Venom\Helper\ErrorHandler;
use Venom\Helper\URLHelper;
class SeoController class SeoController
{ {
@ -19,30 +21,32 @@ class SeoController
$this->shouldUse = Config::getInstance()->getSeoEnabled(); $this->shouldUse = Config::getInstance()->getSeoEnabled();
} }
public function loadSite() public function loadSite(): void
{ {
if (!$this->shouldUse) { if (!$this->shouldUse) {
return; return;
} }
$url = htmlspecialchars(parse_url($_SERVER['REQUEST_URI'])['path']); $url = URLHelper::getInstance()->getUrl();
$data = DatabaseHandler::get()->getOne("SELECT * FROM seoData WHERE seo = :url", [ $data = DatabaseHandler::get()->getOne("SELECT * FROM seoData WHERE seo = :url", [
':url' => $url, ':url' => $url,
]); ]);
$this->data = $data; $this->data = $data;
if($this->data != null) { if ($this->data !== null) {
parse_str(parse_url($this->data->raw)['query'], $queryItems); parse_str(parse_url($this->data->raw)['query'], $queryItems);
foreach ($queryItems as $key => $item) { foreach ($queryItems as $key => $item) {
ArgumentHandler::get()->setItem($key, $item); ArgumentHandler::get()->setItem($key, $item);
} }
} else {
ErrorHandler::setNotFound();
} }
} }
public function addSite() public function addSite(): void
{ {
} }
public function deleteSite() public function deleteSite(): void
{ {
} }

View file

@ -0,0 +1,13 @@
<?php
namespace Venom\Security;
/**
* Class that Login stupid via Password, Username
*/
class BaseLogin
{
}

View file

@ -0,0 +1,22 @@
<?php
namespace Venom\Security;
use Venom\Models\User;
interface Login
{
public function __construct(User $user);
public function checkUsername(): bool;
public function checkPassword(): bool;
public function checkCredentials(): bool;
public function login(): bool;
public function redirect(): void;
}

View file

@ -0,0 +1,31 @@
<?php
namespace Venom\Security;
class Security
{
private static ?Security $instance = null;
public static function get(): Security
{
if (self::$instance === null) {
self::$instance = new self();
}
return self::$instance;
}
/* @todo implement logic */
public function hasRole(string $role): bool
{
return true;
}
/* @todo implement logic */
public function hasRoles(array $roles): bool
{
return true;
}
}

View file

@ -4,6 +4,14 @@
namespace Venom; namespace Venom;
use Venom\Core\ArgumentHandler;
use Venom\Core\Config;
use Venom\Core\Module;
use Venom\Core\Registry;
use Venom\Exceptions\ExceptionHandler;
use Venom\Helper\ErrorHandler;
use Venom\Helper\URLHelper;
use Venom\Routing\Router;
use Venom\Views\Asset; use Venom\Views\Asset;
use Venom\Views\RenderController; use Venom\Views\RenderController;
use Venom\Views\VenomRenderer; use Venom\Views\VenomRenderer;
@ -13,9 +21,11 @@ class Venom
private VenomRenderer $renderer; private VenomRenderer $renderer;
private array $controllers = []; private array $controllers = [];
private array $modules = []; private array $modules = [];
private array $routers = [];
public function __construct() public function __construct()
{ {
ExceptionHandler::setExceptionHandler();
$this->renderer = new VenomRenderer($this); $this->renderer = new VenomRenderer($this);
Asset::get()->setRenderer($this->renderer); Asset::get()->setRenderer($this->renderer);
} }
@ -24,17 +34,40 @@ class Venom
{ {
// we need to load the current controller and the current start template. // we need to load the current controller and the current start template.
// after this we can start the renderer // after this we can start the renderer
if (Config::getInstance()->isRouterEnabled()) {
$status = $this->useRouter();
if ($status['found']) {
if ($status['status']) {
exit(0);
}
ErrorHandler::setFatalError();
}
}
$registry = Registry::getInstance(); $registry = Registry::getInstance();
$registry->getSeo()->loadSite();
$registry->getLang()->initLang(); $registry->getLang()->initLang();
$registry->getSeo()->loadSite();
$this->renderer->init($this->findController()); $this->renderer->init($this->findController());
$this->renderer->render(); $this->renderer->render();
} }
private function useRouter(): array
{
$url = URLHelper::getInstance()->getUrl();
/** @var Router $router */
foreach ($this->routers as $router) {
$route = $router->findRoute($url, $_SERVER['REQUEST_METHOD']);
$status = $router->tryFunctionCall($route);
if ($route !== null) {
return ['found' => true, 'status' => $status];
}
}
return ['found' => false, 'status' => true];
}
private function findController(): ?RenderController private function findController(): ?RenderController
{ {
$cl = ArgumentHandler::get()->getItem('cl'); $cl = ArgumentHandler::get()->getItem('cl');
if ($cl != null && isset($this->controllers[$cl])) { if ($cl !== null && isset($this->controllers[$cl])) {
return $this->controllers[$cl]; return $this->controllers[$cl];
} }
return null; return null;
@ -60,8 +93,8 @@ class Venom
} }
} }
private function prepare() public function addRouter(string $name, Router $router): void
{ {
$this->routers[$name] = $router;
} }
} }

View file

@ -4,14 +4,14 @@
namespace Venom\Views; namespace Venom\Views;
use Venom\Config; use Venom\Core\Config;
class Asset class Asset
{ {
static ?Asset $instance = null; public static ?Asset $instance = null;
private ?VenomRenderer $renderer = null;
private array $jsFiles = []; private array $jsFiles = [];
private array $cssFiles = []; private array $cssFiles = [];
private VenomRenderer $renderer;
private function __construct() private function __construct()
{ {
@ -19,13 +19,13 @@ class Asset
public static function get(): Asset public static function get(): Asset
{ {
if (self::$instance == null) { if (self::$instance === null) {
self::$instance = new Asset(); self::$instance = new Asset();
} }
return self::$instance; return self::$instance;
} }
public function addJS(string $name, string $filepath, $pos = 99999) public function addJS(string $name, string $filepath, $pos = 99999): void
{ {
$this->jsFiles[$name] = [ $this->jsFiles[$name] = [
'file' => $filepath, 'file' => $filepath,
@ -33,7 +33,7 @@ class Asset
]; ];
} }
public function addCSS(string $name, string $filepath, $pos = 99999) public function addCSS(string $name, string $filepath, $pos = 99999): void
{ {
$this->cssFiles[$name] = [ $this->cssFiles[$name] = [
'file' => $filepath, 'file' => $filepath,
@ -43,19 +43,20 @@ class Asset
public function getImagePath(string $filepath, bool $useAbsolute = false) public function getImagePath(string $filepath, bool $useAbsolute = false)
{ {
$preDir = '/content'; $preDir = '/content/';
if ($useAbsolute) { if ($useAbsolute) {
$preDir = Config::getInstance()->getBaseUrl() . $preDir; $preDir = Config::getInstance()->getBaseUrl() . $preDir;
} }
return $preDir . $filepath;
} }
public function setRenderer(VenomRenderer $renderer) public function setRenderer(VenomRenderer $renderer): void
{ {
$this->renderer = $renderer; $this->renderer = $renderer;
} }
//this will output all js files! sorted by position //this will output all js files! sorted by position
public function renderJS() public function renderJS(): void
{ {
usort($this->jsFiles, function ($a, $b) { usort($this->jsFiles, function ($a, $b) {
return $a['pos'] <=> $b['pos']; return $a['pos'] <=> $b['pos'];
@ -66,24 +67,25 @@ class Asset
} }
} }
public function renderCSS() private function getPath($base): string
{
usort($this->cssFiles, function ($a, $b) {
return $a['pos'] <=> $b['pos'];
});
$theme = $this->getPath('/theme/' . Config::getInstance()->getRenderer()['theme'] . '/css/');
foreach ($this->cssFiles as $key => $file) {
echo '<link rel="stylesheet" href="' . $theme . $file['file'] . '" id="css-' . $key . '">';
}
}
private function getPath($base)
{ {
$preDir = $base; $preDir = $base;
$config = Config::getInstance();
$baseUrl = Config::getInstance()->getBaseUrl(); $baseUrl = Config::getInstance()->getBaseUrl();
if ($baseUrl != '') { if ($baseUrl != '' && $config->getRenderer()->useStaticUrl) {
$preDir = Config::getInstance()->getBaseUrl() . $preDir; $preDir = Config::getInstance()->getBaseUrl() . $preDir;
} }
return $preDir; return $preDir;
} }
public function renderCSS(): void
{
usort($this->cssFiles, function ($a, $b) {
return $a['pos'] <=> $b['pos'];
});
$theme = $this->getPath('/theme/' . Config::getInstance()->getRenderer()->theme . '/css/');
foreach ($this->cssFiles as $key => $file) {
echo '<link rel="stylesheet" href="' . $theme . $file['file'] . '" id="css-' . $key . '">';
}
}
} }

View file

@ -4,8 +4,8 @@
namespace Venom\Views; namespace Venom\Views;
use Venom\ArgumentHandler; use Venom\Core\ArgumentHandler;
use Venom\Config; use Venom\Core\Config;
use Venom\Venom; use Venom\Venom;
class VenomRenderer class VenomRenderer
@ -68,7 +68,7 @@ class VenomRenderer
return ''; return '';
} }
public function addVar($name, $value) public function addVar($name, $value): void
{ {
$this->vars[$name] = $value; $this->vars[$name] = $value;
} }
@ -82,8 +82,8 @@ class VenomRenderer
{ {
$this->controller = $controller; $this->controller = $controller;
$data = Config::getInstance()->getRenderer(); $data = Config::getInstance()->getRenderer();
$this->baseTemplate = $data['baseFile'] . '.php' ?? 'base.php'; $this->baseTemplate = $data->baseFile . '.php' ?? 'base.php';
$this->templateDir = __DIR__ . '/../../../tpl/' . $data['theme'] . '/'; $this->templateDir = __DIR__ . '/../../../tpl/' . $data->theme . '/';
$this->assetsDir = __DIR__ . '/../../../public/theme/' . $data['theme'] . '/'; $this->assetsDir = __DIR__ . '/../../../public/theme/' . $data->theme . '/';
} }
} }

View file

@ -1,33 +0,0 @@
<?php
namespace Modules\API;
use Venom\Config;
use Venom\Views\RenderController;
use Venom\Views\VenomRenderer;
class APIMailer implements RenderController
{
private bool $useAPI = false;
public function register(): bool
{
$this->useAPI = Config::getInstance()->getApi()['useAPI'] === true;
return true;
}
public function render(VenomRenderer $renderer): bool
{
//check for batch rendering... this is very important!
echo 'batch...';
return $this->useAPI;
}
//return async to say the backend that this should exit after rendering
public function getTemplate(): string
{
return 'async';
}
}

View file

@ -1,6 +1,6 @@
<?php <?php
use Venom\Registry; use Venom\Core\Registry;
use Venom\Views\Asset; use Venom\Views\Asset;
$reg = Registry::getInstance(); $reg = Registry::getInstance();
@ -20,7 +20,7 @@ $lang = $reg->getLang();
</head> </head>
<body> <body>
<header> <header>
<?=$lang->getTranslation("HEADLINE");?> <?=$lang->getTranslation("HEADLINE")?>
</header> </header>
<?php <?php
$lang->getTranslation("TEST_TRANSLATION"); $lang->getTranslation("TEST_TRANSLATION");