Powerful and easy to use cms to build websites, blogs or ecommerce stores.
HTML backendblogblog-engineblog-platformbloggingcmscontent-managementcontent-management-systemecommerceecommerce-platformno-codepage-builderphpphp-cmsphp7php8self-hostedshopping-cartweb-applicationwebsite-builder
Changed post/product type event name and moved code to functions
givanz9a355bb23914
diff --git a/admin/controller/base.php b/admin/controller/base.phpindex 530af68..66c2c3c 100755--- a/admin/controller/base.php+++ b/admin/controller/base.php@@ -30,6 +30,7 @@ use function Vvveb\clearLanguageCache;use function Vvveb\filter;use function Vvveb\setLanguage;use Vvveb\Sql\taxonomySQL;+use Vvveb\System\Cache;use Vvveb\System\Core\FrontController;use Vvveb\System\Core\Request;use Vvveb\System\Core\View;@@ -103,7 +104,7 @@ class Base {$custom_posts_types = \Vvveb\getSetting('post', 'types', []);$custom_posts_types += $default_custom_posts;- list($custom_posts_types) = Event::trigger(__CLASS__, __FUNCTION__, $custom_posts_types);+ list($custom_posts_types) = Event::trigger('Vvveb\postTypes', __FUNCTION__, $custom_posts_types);$custom_post_menu = \Vvveb\config('custom-post-menu', []);$posts_menu = [];@@ -170,7 +171,7 @@ class Base {$custom_products_types = \Vvveb\getSetting('product', 'types', []);$custom_products_types += $default_custom_products;- list($custom_products_types) = Event::trigger(__CLASS__, __FUNCTION__, $custom_products_types);+ list($custom_products_types) = Event::trigger('Vvveb\postTypes', __FUNCTION__, $custom_products_types);$custom_product_menu = \Vvveb\config('custom-product-menu', []);$products_menu = [];@@ -378,26 +379,28 @@ class Base {$view->removeVattrs(false);if (isset($this->request->get['errors']) && $this->request->get['errors']) {- $view->errors['get'] = htmlentities($this->request->get['errors']);+ $view->errors['get'] = htmlspecialchars($this->request->get['errors']);}if (isset($this->request->get['success']) && $this->request->get['success']) {- $view->success['get'] = htmlentities($this->request->get['success']);+ $view->success['get'] = htmlspecialchars($this->request->get['success']);}//prevent admin loading in iframe$this->response->addHeader('X-Frame-Options', 'SAMEORIGIN');$this->response->addHeader('X-Content-Type-Options', 'nosniff');+ if (! $this->session->get('csrf')) {+ $this->session->set('csrf', Str::random());+ }+$admin = Admin::current();if (! $admin) {return $this->requireLogin();}- if (! $this->session->get('csrf')) {- $this->session->set('csrf', Str::random());- }+ $this->checkCsrf();if (($site_id = ($this->request->post['site'] ?? false)) && is_numeric($site_id)) {$this->setSite($site_id);@@ -474,25 +477,34 @@ class Base {if (defined('CLI')) {return;}- $menu = \Vvveb\config('admin-menu', []);- //custom posts -- add to menu- $this->taxonomies = $this->getTaxonomies();- $posts_menu = $this->customPost();- $menu = arrayInsertArrayAfter('edit', $menu, $posts_menu);+ $cacheDriver = Cache :: getInstance();+ $cacheKey = $this->global['admin_id'] . '.' . $this->global['site_id'] . '.' . $this->global['language_id'];++ if ($menu = $cacheDriver->get('admin-menu', $cacheKey)) {+ } else {+ $menu = \Vvveb\config('admin-menu', []);- //products - add to menu- $products_menu = $this->customProduct();- $menu = arrayInsertArrayAfter('sales', $menu, $products_menu);+ //custom posts -- add to menu+ $this->taxonomies = $this->getTaxonomies();+ $posts_menu = $this->customPost();+ $menu = arrayInsertArrayAfter('edit', $menu, $posts_menu);- list($menu) = Event::trigger(__CLASS__, __FUNCTION__ . '-menu', $menu);+ //products - add to menu+ $products_menu = $this->customProduct();+ $menu = arrayInsertArrayAfter('sales', $menu, $products_menu);- $urls = [];- $this->getPermissionsFromUrl($menu, $urls);- $permissions = Admin::hasPermission($urls);- //$urls = array_map(fn ($value) => $value ? ($permissions[$value] ?? false) : false, $urls);- $urls = array_map(function ($value) use ($permissions) { return $value ? ($permissions[$value] ?? false) : false; }, $urls);- $this->setPermissionsFromUrl($menu, $urls);+ list($menu) = Event::trigger(__CLASS__, __FUNCTION__ . '-menu', $menu);++ $urls = [];+ $this->getPermissionsFromUrl($menu, $urls);+ $permissions = Admin::hasPermission($urls);+ //$urls = array_map(fn ($value) => $value ? ($permissions[$value] ?? false) : false, $urls);+ $urls = array_map(function ($value) use ($permissions) { return $value ? ($permissions[$value] ?? false) : false; }, $urls);+ $this->setPermissionsFromUrl($menu, $urls);++ $cacheDriver->set('admin-menu', $cacheKey, $menu);+ }$view->menu = $menu;$view->global = $this->global;@@ -542,6 +554,18 @@ class Base {die(0);}+ /**+ * Call this method on form posts to protect against csrf attacks.+ *+ */+ protected function checkCsrf() {+ if (! defined('CLI') &&+ (($method = $this->request->getMethod()) == 'post') &&+ (! ($csrf = $this->session->get('csrf')) || ! ($postCsrf = ($this->request->post['csrf'] ?? false)) || ($csrf != $postCsrf))) {+ $this->notFound('Invalid csrf!', 403);+ }+ }+/*** Shows a "Not found", "Internal server error" or "Permission denied" page.*diff --git a/admin/controller/settings/post-types.php b/admin/controller/settings/post-types.phpindex f76d858..794b85c 100644--- a/admin/controller/settings/post-types.php+++ b/admin/controller/settings/post-types.php@@ -23,7 +23,7 @@namespace Vvveb\Controller\Settings;use Vvveb\Controller\Listing;-use Vvveb\System\Event;+use function Vvveb\postTypes;class PostTypes extends Listing {protected $type = 'post';@@ -51,45 +51,8 @@ class PostTypes extends Listing {return $this->index();}- protected $defaultTypes = [- 'post' => [- 'name' => 'Post',- 'source' => 'default',- 'site_id' => '0',- 'type' => 'post',- 'plural' => 'posts',- 'icon' => 'icon-document-text-outline',- 'comments'=> true,- ],- 'page' => [- 'name' => 'Page',- 'source' => 'default',- 'site_id' => '0',- 'type' => 'page',- 'plural' => 'pages',- 'icon' => 'icon-document-outline',- 'comments'=> false,- ],- ];-function index() {- $type = ucfirst($this->type);- list($pluginTypes) = Event::trigger('Vvveb\Controller\Base', "custom$type", []);- array_walk($pluginTypes, function (&$type,$key) {$type['source'] = 'plugin'; $type['name'] = ucfirst($key); });-- $userTypes = \Vvveb\getSetting($this->type, 'types', []);-- $params = ['module' => "settings/{$this->type}-type"];- $paramsList = ['module' => "settings/{$this->type}-types"];-- array_walk($userTypes, function (&$type,$key) use ($params, $paramsList) {- $type['source'] = 'user';- $type['name'] = ucfirst($key);- $type['url'] = \Vvveb\url($params + ['type' => $type['type']]);- $type['delete-url'] = \Vvveb\url($paramsList + ['action' => 'delete', 'type[]' => $type['type']]);- });-- $types = $this->defaultTypes + $pluginTypes + $userTypes;+ $types = postTypes($this->type);$this->view->type = $types;$this->view->count = count($types);diff --git a/admin/controller/settings/product-types.php b/admin/controller/settings/product-types.phpindex 86bd2d7..899adc8 100644--- a/admin/controller/settings/product-types.php+++ b/admin/controller/settings/product-types.php@@ -24,15 +24,4 @@ namespace Vvveb\Controller\Settings;class ProductTypes extends PostTypes {protected $type = 'product';-- protected $defaultTypes = [- 'product' => [- 'name' => 'Product',- 'type' => 'product',- 'site_id' => '0',- 'source' => 'default',- 'plural' => 'products',- 'icon' => 'icon-cube-outline',- ],- ];}diff --git a/system/functions.php b/system/functions.phpindex b3f4b09..0665a15 100755--- a/system/functions.php+++ b/system/functions.php@@ -1081,6 +1081,62 @@ function controller($name) {return new $controllerClass();}+function postTypes($type) {+ $defaultTypes = [+ 'post' => [+ 'post' => [+ 'name' => 'Post',+ 'source' => 'default',+ 'site_id' => '0',+ 'type' => 'post',+ 'plural' => 'posts',+ 'icon' => 'icon-document-text-outline',+ 'comments'=> true,+ ],+ 'page' => [+ 'name' => 'Page',+ 'source' => 'default',+ 'site_id' => '0',+ 'type' => 'page',+ 'plural' => 'pages',+ 'icon' => 'icon-document-outline',+ 'comments'=> false,+ ],+ ],+ 'product' => [+ 'product' => [+ 'name' => 'Product',+ 'type' => 'product',+ 'site_id' => '0',+ 'source' => 'default',+ 'plural' => 'products',+ 'icon' => 'icon-cube-outline',+ ],+ ],+ ];++ $default = $defaultTypes[$type] ?? [];+ $typeName = ucfirst($type);+ list($pluginTypes) = System\Event::trigger('Vvveb\postTypes', "custom$typeName", []);+ array_walk($pluginTypes, function (&$type,$key) {$type['source'] = 'plugin'; $type['name'] = ucfirst($key); });++ $userTypes = getSetting($type, 'types', []);++ $params = ['module' => "settings/{$type}-type"];+ $paramsList = ['module' => "settings/{$type}-types"];++ array_walk($userTypes, function (&$type,$key) use ($params, $paramsList) {+ $type['source'] = 'user';+ $type['name'] = ucfirst($key);+ $type['url'] = \Vvveb\url($params + ['type' => $type['type']]);+ $type['delete-url'] = \Vvveb\url($paramsList + ['action' => 'delete', 'type[]' => $type['type']]);+ });++ $types = $default + $pluginTypes + $userTypes;++ return $types;+}+function d(...$variables) {foreach ($variables as $variable) {echo highlight_string("<?php\n" . var_export($variable, true), true);@@ -1213,7 +1269,7 @@ function availableLanguages() {return [];}, 259200);- return $languages;+ return $languages ?: [];}function availableCurrencies() {