diff --git a/classes/Core.class.php b/classes/Core.class.php index 4ff27bb..7f1acee 100644 --- a/classes/Core.class.php +++ b/classes/Core.class.php @@ -138,6 +138,10 @@ return; } + if(method_exists($module->getInstance(), 'load')) { + $module->getInstance()->load(); + } + if(!method_exists($module->getInstance(), 'content')) { $this->template->display('error/module_empty', [ 'module' => $module, @@ -169,6 +173,39 @@ $this->template->display('login'); }); + + $this->router->addRoute('/ajax', function() { + if(!Auth::isLoggedIn()) { + header('HTTP/1.1 403 Forbidden'); + require_once(dirname(PATH) . '/placeholder/errors/403.php'); + exit(); + } + + if(empty($_SERVER['HTTP_X_REQUESTED_WITH']) || strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) !== 'xmlhttprequest') { + header('HTTP/1.1 405 Method Not Allowed'); + require_once(dirname(PATH) . '/placeholder/errors/405.php'); + exit(); + } + + // Fire modals + if(isset($_POST['modal']) && !empty($_POST['modal'])) { + $modals = $this->getHooks()->applyFilter('modals', []); + + foreach($modals AS $modal) { + if($modal->getName() === $_POST['modal']) { + $callback = $modal->getCallback('save'); + $result = call_user_func_array($callback, [ $_POST ]); + + if(is_bool($result)) { + print json_encode($result); + return; + } + + print $result; + } + } + } + }); $this->router->run(); } diff --git a/classes/Modal.class.php b/classes/Modal.class.php index e27a2b2..9b7b919 100644 --- a/classes/Modal.class.php +++ b/classes/Modal.class.php @@ -6,6 +6,7 @@ private $title = null; private $content = null; private $buttons = []; + private $callbacks = []; public function __construct($name, $title, $content) { $this->name = $name; @@ -16,6 +17,20 @@ } } + public function getCallback($name) { + if(!isset($this->callbacks[$name])) { + return null; + } + + return $this->callbacks[$name]; + } + + public function onSave($method) { + $this->callbacks['save'] = $method; + + return $this; + } + public function getName() { return $this->name; } diff --git a/classes/Module.class.php b/classes/Module.class.php index 8a45162..63f9d0c 100644 --- a/classes/Module.class.php +++ b/classes/Module.class.php @@ -5,6 +5,7 @@ private $path = null; private $info = null; private $instance = null; + private $enabled = false; public function __construct($path) { $this->path = $path; @@ -23,6 +24,14 @@ return $this->instance; } + public function isEnabled() { + return $this->enabled; + } + + public function setEnabled($state) { + $this->enabled = $state; + } + public function init($core) { $script = sprintf('%s%smodule.php', $this->path, DS); diff --git a/classes/ModuleInterface.class.php b/classes/ModuleInterface.class.php index a9895ee..5b4ab12 100644 --- a/classes/ModuleInterface.class.php +++ b/classes/ModuleInterface.class.php @@ -14,6 +14,10 @@ /* Override Me */ } + public function load() { + /* Override Me */ + } + public function getCore() { return $this->core; } diff --git a/classes/Modules.class.php b/classes/Modules.class.php index c3f4f6f..36facbb 100644 --- a/classes/Modules.class.php +++ b/classes/Modules.class.php @@ -7,6 +7,11 @@ public function __construct($core) { $this->core = $core; + $enabled = []; + + foreach(Database::fetch('SELECT `name` FROM `fh_modules` WHERE `state`=\'ENABLED\'') AS $entry) { + $enabled[] = $entry->name; + } foreach(new \DirectoryIterator($this->getPath()) AS $info) { if($info->isDot()) { @@ -14,8 +19,10 @@ } $path = sprintf('%s%s%s', $this->getPath(), DS, $info->getFilename()); + $module = new Module($path); + $module->setEnabled(in_array(basename($path), $enabled)); - $this->addModule(basename($path), new Module($path)); + $this->addModule(basename($path), $module); } } @@ -24,6 +31,10 @@ } public function addModule($name, $module) { + if(!$module->isEnabled()) { + return; + } + $this->modules[$name] = $module->init($this->core); } diff --git a/classes/Template.class.php b/classes/Template.class.php index d71ad4c..5a9a0ce 100644 --- a/classes/Template.class.php +++ b/classes/Template.class.php @@ -18,8 +18,8 @@ $this->core->getHooks()->addAction('html_head', [ $this, 'head_robots' ]); $this->core->getHooks()->addAction('html_head', [ $this, 'head_scripts' ]); - $this->core->getHooks()->addAction('html_foot', [ $this, 'foot_scripts' ]); $this->core->getHooks()->addAction('html_foot', [ $this, 'foot_modals' ]); + $this->core->getHooks()->addAction('html_foot', [ $this, 'foot_scripts' ]); $this->files->addStylesheet('bootstrap', $this->url('css/bootstrap/bootstrap.min.css'), '4.3.1'); $this->files->addStylesheet('jquery-ui', $this->url('css/jquery-ui/jquery-ui.css'), '1.12.1'); diff --git a/classes/TemplateDefaults.class.php b/classes/TemplateDefaults.class.php index 1605a88..3bd2af8 100644 --- a/classes/TemplateDefaults.class.php +++ b/classes/TemplateDefaults.class.php @@ -63,40 +63,43 @@ foreach($modals AS $modal) { ?>