From 32a78ed1b9fb33c7a43620df5ab3e55a20a1bfdb Mon Sep 17 00:00:00 2001 From: engineerTrooper Date: Tue, 22 Dec 2020 18:07:13 +0100 Subject: [PATCH] VENOM: Hardcode WIP (versustunez) --- install/db.sql | 28 ++++--- public/theme/admin/css/admin-panel.css | 2 +- src/Venom/Admin/AdminModulesLoader.php | 20 +++-- src/Venom/Core/DatabaseHandler.php | 18 +++++ src/Venom/Core/Module.php | 6 ++ src/Venom/Helper/AdminHelper.php | 12 +++ src/Venom/Helper/ErrorHandler.php | 1 + src/Venom/Models/DataModel.php | 16 +--- src/Venom/Models/DatabaseObject.php | 20 ++++- src/Venom/Models/User.php | 8 +- src/Venom/Routing/Router.php | 2 + .../Meta/Controller/MetaAPIController.php | 36 +++++++++ .../Meta/Controller/MetaController.php | 18 +++++ src/modules/{ => Meta}/MetaDataModule.php | 7 +- src/modules/Meta/module.php | 22 +++++ src/modules/OverviewModule.php | 81 ------------------- .../User/Controller/UserAPIController.php | 64 +++++++++++++++ .../User/Controller/UserController.php | 36 +++++++++ src/modules/{ => User}/UserModule.php | 39 ++------- tpl/admin/admin-panel.php | 1 - tpl/admin/jsTemplates/usersList.tpl | 6 +- 21 files changed, 285 insertions(+), 158 deletions(-) create mode 100644 src/modules/Meta/Controller/MetaAPIController.php create mode 100644 src/modules/Meta/Controller/MetaController.php rename src/modules/{ => Meta}/MetaDataModule.php (90%) create mode 100644 src/modules/Meta/module.php delete mode 100644 src/modules/OverviewModule.php create mode 100644 src/modules/User/Controller/UserAPIController.php create mode 100644 src/modules/User/Controller/UserController.php rename src/modules/{ => User}/UserModule.php (52%) diff --git a/install/db.sql b/install/db.sql index 6e0d81d..9c4be2b 100755 --- a/install/db.sql +++ b/install/db.sql @@ -38,13 +38,23 @@ create table if not exists data create table if not exists users ( - id int(255) auto_increment not null unique primary key, - username varchar(255) not null unique, - email varchar(255) not null, - password varchar(255) not null, - token varchar(255) not null, - salt varchar(255) not null, - roles text default 'ROLE_GUEST' not null, - isActive tinyint(1) default 1 null + id int(255) auto_increment not null unique primary key, + username varchar(255) not null unique, + firstname varchar(255) not null, + lastname varchar(255) not null, + email varchar(255) not null, + password varchar(255) not null, + token varchar(255) not null, + salt varchar(255) not null, + roles text default 'ROLE_GUEST' not null, + isActive tinyint(1) default 1 null ) - comment 'User File'; \ No newline at end of file + comment 'User File'; + +create table if not exists roles +( + id int(255) auto_increment not null unique primary key, + name varchar(255) not null unique, + content JSON not null, + isActive tinyint(1) default 1 null +) \ No newline at end of file diff --git a/public/theme/admin/css/admin-panel.css b/public/theme/admin/css/admin-panel.css index 5d2f8a8..041b30e 100644 --- a/public/theme/admin/css/admin-panel.css +++ b/public/theme/admin/css/admin-panel.css @@ -1 +1 @@ -main{display:flex;height:100vh;overflow:hidden}main h1{margin-top:30px;margin-bottom:20px}main h2{margin-top:35px;margin-bottom:25px}main h3{margin-top:20px;margin-bottom:15px}main h4{margin-top:15px;margin-bottom:10px}main.nav-open .menu{transform:translateX(0)}main.nav-open .app{transform:translateX(220px)}main.nav-open .app .nav-toggle span{transition:width .3s;width:0}main.nav-open .app .nav-toggle span:before{transform:translateY(8px) rotate(-135deg)}main.nav-open .app .nav-toggle span:after{transform:translateY(-8px) rotate(135deg)}.app{transform:translateX(0);transition:transform .4s;flex-grow:1;overflow-y:auto;margin:.6rem .8rem;width:100%;max-height:100%;background:rgba(27,27,27,.5);position:relative}.app .nav-toggle{position:absolute;top:1rem;left:1rem;margin-top:10px;height:25px}.app .nav-toggle span,.app .nav-toggle span:after,.app .nav-toggle span:before{cursor:pointer;border-radius:1px;height:2px;width:25px;background:#fff;position:absolute;content:'';transition:transform .5s,width .4s ease-in}.app .nav-toggle span:before{top:-8px}.app .nav-toggle span:after{bottom:-8px}.menu{width:220px;background-color:#1b1b1b;box-shadow:0 3px 6px rgba(0,0,0,.16),0 3px 6px rgba(0,0,0,.23);height:100%;position:fixed;z-index:1;top:0;left:0;overflow-x:hidden;transition:.4s;transform:translateX(-220px);display:flex;flex-direction:column}.menu .logo{text-align:center;font-family:monospace}.menu div[data-link]{padding:.75rem .5rem;position:relative}.menu div[data-link]:after{background-color:#3949ab;content:"";position:absolute;left:0;bottom:0;height:3px;width:100%;transform:scaleX(0);transition:transform .4s;transform-origin:left}.menu div[data-link]:hover:after{transform:scaleX(1)}.menu div[data-link]:last-child{margin-top:auto}.menu div[data-link].active{font-weight:700}.content-area{width:calc(100% - 20px);padding-top:30px;margin:0 auto;display:block}.content-area header{display:block;text-align:center}.content-area header h2{margin:25px 0}.content-area .back-arrow{width:36px;height:36px}.content-area textarea{background:rgba(27,27,27,.5);color:#fff;margin:15px 0 0 0;font-family:sans-serif;font-size:1.1rem;min-width:100%}.content-area .modules div{padding:6px 20px 6px 0}.content-area .add-new,.content-area .overview{width:100%}.content-area .overview div[data-link]{margin-right:10px;padding:10px;background-color:rgba(0,0,0,.3)}.content-area .overview div[data-link]:hover{background-color:rgba(0,0,0,.5)}.content-area .overview .icon{display:inline-block}.content-area .add-new{padding-top:25px}@media only screen and (min-width:768px){.content-area{width:calc(100% - 40px)}.content-area .flexbox{display:flex}.content-area .overview{flex-grow:1;width:60%}.content-area .add-new{padding-top:0;flex-grow:1;width:40%}}@media only screen and (min-width:1024px){.content-area{max-width:860px;padding-top:0;margin:0 0 0 20px}}@media only screen and (min-width:1024px){main,main.nav-open{display:flex}main .app,main.nav-open .app{transform:translateX(0)}main .menu,main.nav-open .menu{position:relative;transform:translateX(0)}main .nav-toggle,main.nav-open .nav-toggle{display:none}}.role-edit .privileges .name{font-size:1.15rem} \ No newline at end of file +main{display:flex;height:100vh;overflow:hidden}main h1{margin-top:30px;margin-bottom:20px}main h2{margin-top:35px;margin-bottom:25px}main h3{margin-top:20px;margin-bottom:15px}main h4{margin-top:15px;margin-bottom:10px}main.nav-open .menu{transform:translateX(0)}main.nav-open .app{transform:translateX(220px)}main.nav-open .app .nav-toggle span{transition:width .3s;width:0}main.nav-open .app .nav-toggle span:before{transform:translateY(8px) rotate(-135deg)}main.nav-open .app .nav-toggle span:after{transform:translateY(-8px) rotate(135deg)}.app{transform:translateX(0);transition:transform .4s;flex-grow:1;overflow-y:auto;margin:.6rem .8rem;width:100%;max-height:100%;background:rgba(27,27,27,.5);position:relative}.app .nav-toggle{position:absolute;cursor:pointer;left:1rem;height:25px;width:25px}.app .nav-toggle span{transform:translateY(21px)}.app .nav-toggle span,.app .nav-toggle span:after,.app .nav-toggle span:before{border-radius:1px;height:2px;width:25px;background:#fff;position:absolute;content:'';transition:transform .5s,width .4s ease-in}.app .nav-toggle span:before{top:-8px}.app .nav-toggle span:after{bottom:-8px}.menu{width:220px;background-color:#1b1b1b;box-shadow:0 3px 6px rgba(0,0,0,.16),0 3px 6px rgba(0,0,0,.23);height:100%;position:fixed;z-index:1;top:0;left:0;overflow-x:hidden;transition:.4s;transform:translateX(-220px);display:flex;flex-direction:column}.menu .logo{text-align:center;font-family:monospace}.menu div[data-link]{padding:.75rem .5rem;position:relative}.menu div[data-link]:after{background-color:#3949ab;content:"";position:absolute;left:0;bottom:0;height:3px;width:100%;transform:scaleX(0);transition:transform .4s;transform-origin:left}.menu div[data-link]:hover:after{transform:scaleX(1)}.menu div[data-link]:last-child{margin-top:auto}.menu div[data-link].active{font-weight:700}.content-area{width:calc(100% - 20px);padding-top:30px;margin:0 auto;display:block}.content-area header{display:block;text-align:center}.content-area header h2{margin:25px 0}.content-area .back-arrow{width:36px;height:36px}.content-area textarea{background:rgba(27,27,27,.5);color:#fff;margin:15px 0 0 0;font-family:sans-serif;font-size:1.1rem;min-width:100%}.content-area .modules div{padding:6px 20px 6px 0}.content-area .add-new,.content-area .overview{width:100%}.content-area .overview div[data-link]{margin-right:10px;padding:10px;background-color:rgba(0,0,0,.3)}.content-area .overview div[data-link]:hover{background-color:rgba(0,0,0,.5)}.content-area .overview .icon{display:inline-block}.content-area .add-new{padding-top:25px}@media only screen and (min-width:768px){.content-area{width:calc(100% - 40px)}.content-area .flexbox{display:flex}.content-area .overview{flex-grow:1;width:60%}.content-area .add-new{padding-top:0;flex-grow:1;width:40%}}@media only screen and (min-width:1024px){.content-area{max-width:860px;padding-top:0;margin:0 0 0 20px}}@media only screen and (min-width:1024px){main,main.nav-open{display:flex}main .app,main.nav-open .app{transform:translateX(0)}main .menu,main.nav-open .menu{position:relative;transform:translateX(0)}main .nav-toggle,main.nav-open .nav-toggle{display:none}}.role-edit .privileges .name{font-size:1.15rem} \ No newline at end of file diff --git a/src/Venom/Admin/AdminModulesLoader.php b/src/Venom/Admin/AdminModulesLoader.php index da4b77f..38e687a 100644 --- a/src/Venom/Admin/AdminModulesLoader.php +++ b/src/Venom/Admin/AdminModulesLoader.php @@ -3,18 +3,24 @@ namespace Venom\Admin; +use Modules\Meta\MetaDataModule; +use Modules\PageModule; +use Modules\RoleModule; +use Modules\SeoUrlModule; +use Modules\User\UserModule; +use Modules\VenomStatusModule; + class AdminModulesLoader { public static function getModules(): array { return [ - 'metaData' => \Modules\MetaDataModule::class, - 'overview' => \Modules\OverviewModule::class, - 'pages' => \Modules\PageModule::class, - 'role' => \Modules\RoleModule::class, - 'seoUrl' => \Modules\SeoUrlModule::class, - 'users' => \Modules\UserModule::class, - 'venomStatus' => \Modules\VenomStatusModule::class, + 'metaData' => MetaDataModule::class, + 'pages' => PageModule::class, + 'role' => RoleModule::class, + 'seoUrl' => SeoUrlModule::class, + 'users' => UserModule::class, + 'venomStatus' => VenomStatusModule::class, ]; } } \ No newline at end of file diff --git a/src/Venom/Core/DatabaseHandler.php b/src/Venom/Core/DatabaseHandler.php index 1e76f2b..cf16f58 100755 --- a/src/Venom/Core/DatabaseHandler.php +++ b/src/Venom/Core/DatabaseHandler.php @@ -71,4 +71,22 @@ class DatabaseHandler $stmt = $this->db->prepare($query); return $stmt->execute($args); } + + // Returns a Select like this: SELECT id, name, ... FROM table || do what you want + public static function createEasySelect(array $fields, string $table): string + { + return "SELECT " . implode(",", $fields) . " FROM " . $table; + } + + public static function getUpdateString(array $data, string $table, string $where): array + { + $string = []; + $save = []; + foreach ($data as $key => $value) { + $k = ":" . strtolower($key); + $string[] = $key . "= " . $k; + $save[$k] = $value; + } + return ["UPDATE " . $table . " SET " . implode(",", $string) . " " . $where, $save]; + } } \ No newline at end of file diff --git a/src/Venom/Core/Module.php b/src/Venom/Core/Module.php index ea44204..821bd0a 100644 --- a/src/Venom/Core/Module.php +++ b/src/Venom/Core/Module.php @@ -8,6 +8,12 @@ use Venom\Venom; interface Module { + const NAME = "name"; + const AUTHOR = "author"; + const SECURE = "secure"; + const ROUTE = "routes"; + const DESC = "description"; + public function register(Venom $venom): bool; public function init(): void; diff --git a/src/Venom/Helper/AdminHelper.php b/src/Venom/Helper/AdminHelper.php index ab019ff..eb42ceb 100644 --- a/src/Venom/Helper/AdminHelper.php +++ b/src/Venom/Helper/AdminHelper.php @@ -22,4 +22,16 @@ class AdminHelper echo json_encode($response); die(); } + + public static function sendStatus(bool $isSuccess, string $message = "") + { + if ($message == "") { + $message = $isSuccess ? "Operation Success" : "Operation failed"; + } + echo json_encode([ + "status" => $isSuccess ? 'success' : 'failed', + "message" => $message + ]); + die(); + } } \ No newline at end of file diff --git a/src/Venom/Helper/ErrorHandler.php b/src/Venom/Helper/ErrorHandler.php index c3d9e68..a1b265f 100755 --- a/src/Venom/Helper/ErrorHandler.php +++ b/src/Venom/Helper/ErrorHandler.php @@ -4,6 +4,7 @@ namespace Venom\Helper; +use http\Exception\RuntimeException; use Venom\Core\ArgumentHandler; class ErrorHandler diff --git a/src/Venom/Models/DataModel.php b/src/Venom/Models/DataModel.php index e5dc2d1..a864bdb 100755 --- a/src/Venom/Models/DataModel.php +++ b/src/Venom/Models/DataModel.php @@ -9,23 +9,15 @@ class DataModel public const TYPE_CONTENT = 'content'; public const TYPE_FORM = 'form'; - public string $id; - public string $raw; - public string $generated; - public string $type; public int $active = 1; public function __construct( - string $id, - string $type = self::TYPE_CONTENT, - string $raw = '', - string $generated = '' + public string $id, + public string $type = self::TYPE_CONTENT, + public string $raw = '', + public string $generated = '' ) { - $this->id = $id; - $this->type = $type; - $this->raw = $raw; - $this->generated = $generated; } public function getId(): string diff --git a/src/Venom/Models/DatabaseObject.php b/src/Venom/Models/DatabaseObject.php index 26a8cd4..9c4ca50 100644 --- a/src/Venom/Models/DatabaseObject.php +++ b/src/Venom/Models/DatabaseObject.php @@ -4,12 +4,14 @@ namespace Venom\Models; +use JsonSerializable; + /** * Database Object to use queries like this $obj->id, $obj->value * also the option to print it in csv format ; as delimiter * @package Venom\Database */ -class DatabaseObject +class DatabaseObject implements JsonSerializable { private array $data = []; @@ -26,19 +28,29 @@ class DatabaseObject $this->data[$name] = $value; } - public function __isset($name) + public function __isset($name): bool { return isset($this->data[$name]); } - public function toString() + public function toString(): string { return implode(';', $this->data); } - public function getHead() + public function getHead(): string { $keys = array_keys($this->data); return implode(';', $keys); } + + public function getData(): array + { + return $this->data; + } + + public function jsonSerialize(): array + { + return $this->data; + } } \ No newline at end of file diff --git a/src/Venom/Models/User.php b/src/Venom/Models/User.php index b18e57f..310692d 100644 --- a/src/Venom/Models/User.php +++ b/src/Venom/Models/User.php @@ -12,9 +12,11 @@ class User public const GUEST_ROLE = 'ROLE_GUEST'; private string $username = 'GUEST'; private string $email = 'GUEST'; - private string $password = '---'; - private string $salt = '---'; - private string $token = '---'; + private string $firstname = ''; + private string $lastname = ''; + private string $password = ''; + private string $salt = ''; + private string $token = ''; private string $id = '-1'; private array $roles = []; private bool $isLoaded = false; diff --git a/src/Venom/Routing/Router.php b/src/Venom/Routing/Router.php index 8632246..698949f 100644 --- a/src/Venom/Routing/Router.php +++ b/src/Venom/Routing/Router.php @@ -6,6 +6,7 @@ namespace Venom\Routing; use Exception; use Venom\Core\Config; +use Venom\Exceptions\ExceptionHandler; use Venom\Models\User; use Venom\Security\Security; @@ -122,6 +123,7 @@ class Router $route->$fnc(...$params); return true; } catch (Exception $ex) { + ExceptionHandler::handleException($ex); return false; } } diff --git a/src/modules/Meta/Controller/MetaAPIController.php b/src/modules/Meta/Controller/MetaAPIController.php new file mode 100644 index 0000000..f5b277a --- /dev/null +++ b/src/modules/Meta/Controller/MetaAPIController.php @@ -0,0 +1,36 @@ +getRouter(Router::ADMIN_ROUTER)->addRoutes([ '/metaData' => [ - 'cl' => MetaDataModule::class, + 'cl' => MetaAPIController::class, 'roles' => ['ROLE_ADMIN'], 'routes' => [ '*' => [ @@ -40,6 +40,7 @@ class MetaDataModule implements Module, Route "GET" => 'getById', "POST" => 'update', "PUT" => 'insert', + "DELETE" => 'delete' ] ] ] diff --git a/src/modules/Meta/module.php b/src/modules/Meta/module.php new file mode 100644 index 0000000..bb7230c --- /dev/null +++ b/src/modules/Meta/module.php @@ -0,0 +1,22 @@ + 'MetaModule', + Module::DESC => 'Meta Data Module for SEO', + Module::AUTHOR => 'VstZ dev', + Module::SECURE => true, + MODULE::ROUTE => [ + '/' + ], + MODULE::TEMPLATES => [ + // Include Templates with shorter names! $render->include("meta_roles") + 'meta_roles' => 'PATH_TO_TEMPLATE' + ], + MODULE::ADMIN_TEMPLATES => [ + // + ] +]; +$venom = $venom ?? die(); +$venom->registerModule($module);*/ \ No newline at end of file diff --git a/src/modules/OverviewModule.php b/src/modules/OverviewModule.php deleted file mode 100644 index d3b9173..0000000 --- a/src/modules/OverviewModule.php +++ /dev/null @@ -1,81 +0,0 @@ -isAdmin()) { - $this->registerAdminRoutes($venom); - } - return true; - } - - public function init(): void - { - } - - private function registerAdminRoutes(Venom $venom) - { - $venom->getRouter(Router::ADMIN_ROUTER)->addRoutes([ - '/overview' => [ - 'cl' => OverviewModule::class, - 'roles' => ['ROLE_ADMIN'], - 'routes' => [ - '*' => [ - "GET" => 'get', - ], - '1' => [ - "GET" => 'getById', - "POST" => 'update', - "PUT" => 'insert', - ] - ] - ] - ]); - } - - public function get() - { - AdminHelper::sendResponse([ - 'pages' => [ - ['id' => 1, 'name' => 'Flamingos going wild!', 'icon' => 'vt-edit'], - ['id' => 2, 'name' => 'Turbinen sind geil.', 'icon' => 'vt-edit'], - ['id' => 3, 'name' => 'Aufbau und Umbau des neuen VENOMs Plugins', 'icon' => 'vt-edit'], - ['id' => 4, 'name' => 'Aber Mama hat gesagt!', 'icon' => 'vt-edit'], - ['id' => 5, 'name' => 'Frische Fische nur heute!', 'icon' => 'vt-edit'] - ] - ]); - } - - public function update(): bool - { - return false; - } - - public function insert(): bool - { - return false; - } - - public function getById($id) - { - AdminHelper::sendResponse([ - 'caseName' => 'ROLE_ADMIN', - 'id' => $id, - 'name' => 'Admin', - 'icon' => 'vt-visibility', - ]); - } -} \ No newline at end of file diff --git a/src/modules/User/Controller/UserAPIController.php b/src/modules/User/Controller/UserAPIController.php new file mode 100644 index 0000000..f64b6b4 --- /dev/null +++ b/src/modules/User/Controller/UserAPIController.php @@ -0,0 +1,64 @@ + 1, 'name' => 'engineertrooper', 'icon' => 'vt-edit'], + $data = UserController::get(["id", "username", "firstname", "lastname", "email", "isActive"]); + AdminHelper::sendResponse(["users" => $data]); + } + + public function getById($id) + { + $d = UserController::getById($id, ["id", "username", "firstname", "lastname", "email", "isActive"]); + AdminHelper::sendResponse($d); + } + + public function update($id) + { + $original = UserController::getById($id); + if ($original == null) { + AdminHelper::sendStatus(false, "User not Found"); + } + $args = ArgumentHandler::get(); + $data = []; + $d = $original->getData(); + foreach ($d as $key => $item) { + if ($args->hasPostItem($key)) { + $val = $args->getPostItem($key); + if ($val != $item) { + $data[$key] = $val; + } + } + } + parse_str(file_get_contents('php://input'), $_PUT); + var_dump(array_keys($_PUT)); + //var_dump($data, $d, $_POST); + // $args->getPostItem("username")//UPDATE users SET lastname='Doe', firstname='' WHERE id=2 + AdminHelper::sendStatus(UserController::update($id, $data)); + } + + public function delete($id) + { + AdminHelper::sendStatus(DatabaseHandler::get()->execute( + "DELETE FROM users WHERE id=:id", + [ + ':id' => $id + ] + )); + } + + public function create($id) + { + // INSERT INTO + AdminHelper::sendStatus(true); + } +} \ No newline at end of file diff --git a/src/modules/User/Controller/UserController.php b/src/modules/User/Controller/UserController.php new file mode 100644 index 0000000..dc7cffb --- /dev/null +++ b/src/modules/User/Controller/UserController.php @@ -0,0 +1,36 @@ +getOne($sel, [ + ':id' => $id + ]); + } + + public static function get(array $fields = ["*"]): array + { + return DatabaseHandler::get()->getAll(DatabaseHandler::createEasySelect($fields, "users")); + } + + public static function update($id, array $values = []): bool + { + if (count($values) === 0) { + return false; + } + return DatabaseHandler::get()->execute(...DatabaseHandler::getUpdateString($values, "users", "WHERE id = :id")); + } +} \ No newline at end of file diff --git a/src/modules/UserModule.php b/src/modules/User/UserModule.php similarity index 52% rename from src/modules/UserModule.php rename to src/modules/User/UserModule.php index 7daa631..48ebafa 100644 --- a/src/modules/UserModule.php +++ b/src/modules/User/UserModule.php @@ -1,9 +1,10 @@ getRouter(Router::ADMIN_ROUTER)->addRoutes([ '/users' => [ - 'cl' => UserModule::class, + 'cl' => UserAPIController::class, 'roles' => ['ROLE_ADMIN'], 'routes' => [ '*' => [ @@ -38,41 +39,11 @@ class UserModule implements Module, Route ], '1' => [ "GET" => 'getById', - "POST" => 'update', - "PUT" => 'insert', + "POST" => 'insert', + "PUT" => 'update', ] ] ] ]); } - - public function get() - { - AdminHelper::sendResponse([ - 'users' => [ - ['id' => 1, 'name' => 'engineertrooper', 'icon' => 'vt-edit'], - ['id' => 2, 'name' => 'versustunez', 'icon' => 'vt-edit'] - ] - ]); - } - - public function update(): bool - { - return false; - } - - public function insert(): bool - { - return false; - } - - public function getById($id) - { - AdminHelper::sendResponse([ - 'caseName' => 'ROLE_ADMIN', - 'id' => $id, - 'name' => 'Admin', - 'icon' => 'vt-visibility', - ]); - } } \ No newline at end of file diff --git a/tpl/admin/admin-panel.php b/tpl/admin/admin-panel.php index 3288f7d..8feb1c2 100644 --- a/tpl/admin/admin-panel.php +++ b/tpl/admin/admin-panel.php @@ -2,7 +2,6 @@