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
commit c33bb4cb3a
37 changed files with 677 additions and 187 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

58
base/config.base.php Normal file
View file

@ -0,0 +1,58 @@
<?php
use Venom\Core\Config;
use Venom\Core\DatabaseHandler;
$config = Config::getInstance();
$config->setVersion(1.0);
$config->setDatabase([
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_PORT => '3306', //default port is 3306
DatabaseHandler::DB_USER => 'venom',
DatabaseHandler::DB_PASSWORD => 'venomPassword',
DatabaseHandler::DB_DB => 'venomCMS',
DatabaseHandler::DB_EXTRA => '' // need to start with ';'
]);
/**
* Cron Mailing is something that will send only mails after a specific time like 1 min.
* it is used to prevent spamming.
* 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([
'useCron' => true, //if true it will not send mails directly.
'writeToDB' => true, //is needed for cron and is always true if batch is use
'host' => 'localhost',
'port' => '587',
'useTLS' => true, //use startTLS. is the default case ;) here it's important the security Cert is secure...
'user' => 'youruser@yourdomain.de',
'password' => 'this-is-secret',
'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 themes are in __DIR__/public/theme/
$config->setRender([
'theme' => 'default', //very important! it will search for a folder with this name.
'baseFile' => 'base', //this will called after all templates are rendered...
'useCache' => false, //is only on big systems good
'cacheName' => 'defaultCache', //this is for bigger systems, ignore it
'uploadDir' => 'content/',
'useStaticUrl' => false,
]);
$config->setEnableRouter(false);
$config->setMaintainMode(false);
$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->setSeoMode(true); //seo mode is to lookup the complete entered url in the database! without query-parameters and load the get parameters from it
$config->close();

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

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

9
base/module.base.php Normal file
View file

@ -0,0 +1,9 @@
<?php
//register modules -> need to have the Module Class at parent with the init function ;)
$modules = [];
// register controllers that can handle templates ;) need to have a render function for this
$controllers = [
'test' => \Modules\TestController::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

@ -0,0 +1,42 @@
<?php
namespace Venom\Core;
class ArgumentHandler
{
public static ?ArgumentHandler $instance = null;
private array $arguments = [];
public function __construct()
{
foreach ($_GET as $key => $item) {
$this->arguments[htmlspecialchars($key)] = htmlspecialchars($item);
}
foreach ($_POST as $key => $item) {
$this->arguments[htmlspecialchars($key)] = htmlspecialchars($item);
}
}
public static function get(): ArgumentHandler
{
if (self::$instance === null) {
self::$instance = new ArgumentHandler();
}
return self::$instance;
}
public function getItem(string $key, $default = null)
{
if (isset($this->arguments[$key])) {
return $this->arguments[$key];
}
return $default;
}
public function setItem(string $key, $item)
{
$this->arguments[$key] = $item;
}
}

181
src/Venom/Core/Config.php Normal file
View file

@ -0,0 +1,181 @@
<?php
namespace Venom\Core;
use Venom\Models\ConfigObject;
class Config
{
private static ?Config $instance = null;
private bool $isWriteable = true;
private float $version = 1.0;
private ConfigObject $mail;
private ConfigObject $renderer;
private ConfigObject $security;
private bool $maintenance = false;
private bool $devMode = false;
private bool $isAdmin = false;
private string $baseUrl = '';
private bool $seoMode = false;
private bool $useRouter = false;
private function __construct()
{
$this->mail = new ConfigObject();
$this->renderer = new ConfigObject();
$this->security = new ConfigObject();
}
public static function getInstance(): Config
{
if (self::$instance === null) {
self::$instance = new Config();
}
return self::$instance;
}
public function setDatabase(array $array): void
{
if ($this->isWriteable) {
DatabaseHandler::get()->init($array);
} else {
trigger_error('try to write closed config!');
}
}
public function setRender(array $array): void
{
$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
{
$this->set('maintenance', $mode);
}
public function getVersion(): float
{
return $this->version;
}
public function setVersion(float $param): void
{
$this->set('version', $param);
}
public function getMail(): ConfigObject
{
return $this->mail;
}
public function setMail(array $array): void
{
$this->set('mail', $array);
}
public function getRenderer(): ConfigObject
{
return $this->renderer;
}
public function isMaintenance(): bool
{
return $this->maintenance;
}
public function isDevMode(): bool
{
return $this->devMode;
}
public function setDevMode(bool $mode): void
{
$this->set('devMode', $mode);
}
/**
* @return bool
*/
public function isAdmin(): bool
{
return $this->isAdmin;
}
/**
* @param bool $isAdmin
*/
public function setIsAdmin(bool $isAdmin): void
{
$this->set('isAdmin', $isAdmin);
}
public function setSeoMode(bool $mode): void
{
$this->set('seoMode', $mode);
}
public function getSeoEnabled(): bool
{
return $this->seoMode;
}
/**
* @return bool
*/
public function isClosed(): bool
{
return !$this->isWriteable;
}
/**
* function to close the write mode... this make sure after the config is init no other tool can write to it!
*/
public function close(): void
{
$this->isWriteable = false;
}
public function getBaseUrl(): string
{
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

@ -0,0 +1,74 @@
<?php
namespace Venom\Core;
use PDO;
use PDOException;
use Venom\Models\DatabaseObject;
/**
* Singleton DatabaseHandler... make sure we only have one connection to the database..
* @package Venom\Database
*/
class DatabaseHandler
{
// constants
public const DB_TYPE = 'type';
public const DB_HOST = 'host';
public const DB_PORT = 'port';
public const DB_USER = 'user';
public const DB_PASSWORD = 'pw';
public const DB_DB = 'db';
public const DB_EXTRA = 'extra';
private static ?self $instance = null;
private ?PDO $db = null;
public static function get(): DatabaseHandler
{
if (self::$instance === null) {
self::$instance = new self();
}
return self::$instance;
}
public function init(array $data): void
{
//init instance with the current data... only working if the db is not init!
if ($this->db != null) {
return;
}
$dsn = '%s:host=%s;dbname=%s;port=%s';
$connectString = sprintf($dsn, $data[self::DB_TYPE], $data[self::DB_HOST], $data[self::DB_DB], $data[self::DB_PORT] . $data[self::DB_EXTRA]);
try {
$this->db = new PDO($connectString, $data[self::DB_USER], $data[self::DB_PASSWORD]);
} catch (PDOException $e) {
trigger_error($e->getMessage());
die($e->getCode());
}
}
public function getOne(string $query, array $args): ?DatabaseObject
{
$data = $this->getAll($query, $args);
if (count($data) > 0) {
return $data[0];
}
return null;
}
public function getAll(string $query, array $args): array
{
$stmt = $this->db->prepare($query);
$stmt->setFetchMode(PDO::FETCH_CLASS, DatabaseObject::class);
$stmt->execute($args);
return $stmt->fetchAll();
}
public function execute(string $query, array $args): void
{
$stmt = $this->db->prepare($query);
$stmt->execute($args);
}
}

View file

@ -0,0 +1,61 @@
<?php
namespace Venom\Core;
use RuntimeException;
use Venom\Models\DatabaseObject;
class Language
{
private string $language = "";
private array $languages = [];
private ?DatabaseObject $defaultLang;
public function __construct()
{
$this->defaultLang = DatabaseHandler::get()->getOne("select language, shortTag from language WHERE isActive = 1 and isDefault = 1", []);
}
public function initLang()
{
$lang = ArgumentHandler::get()->getItem("lang", $this->defaultLang->shortTag);
//check if language exists
$data = DatabaseHandler::get()->getOne("select id from language where shortTag = :shortTag", [
':shortTag' => $lang
]);
if (isset($data->id)) {
$this->language = $lang;
} else {
throw new RuntimeException("Language \"$lang\" not found");
}
}
public function registerLang(string $key, array $values)
{
$this->languages[$key] = $values;
}
public function getCurrentLang()
{
return $this->language;
}
public function getTranslations()
{
return $this->languages[$this->language] ?: [];
}
public function getTranslation($key)
{
if (!isset($this->languages[$this->language])) {
return $key;
}
if (isset($this->languages[$this->language][$key])) {
return $this->languages[$this->language][$key];
}
return $key;
}
}

12
src/Venom/Core/Module.php Normal file
View file

@ -0,0 +1,12 @@
<?php
namespace Venom\Core;
interface Module
{
public function register(): bool;
public function init(): void;
}

View file

@ -0,0 +1,41 @@
<?php
namespace Venom\Core;
use Venom\Routing\SeoController;
/**
* Singleton Class... hold current URL => can
* @package Venom
*/
class Registry
{
private static ?Registry $instance = null;
private SeoController $seo;
private Language $lang;
private function __construct()
{
$this->seo = new SeoController();
$this->lang = new Language();
}
public static function getInstance(): Registry
{
if (self::$instance === null) {
self::$instance = new Registry();
}
return self::$instance;
}
public function getSeo(): SeoController
{
return $this->seo;
}
public function getLang(): Language
{
return $this->lang;
}
}

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!");
}
}
}
}

Some files were not shown because too many files have changed in this diff Show more