diff --git a/common.php b/common.php index 5267aeff..dde03773 100644 --- a/common.php +++ b/common.php @@ -27,7 +27,7 @@ session_start(); define('MYAAC', true); define('MYAAC_VERSION', '0.8-dev'); -define('DATABASE_VERSION', 27); +define('DATABASE_VERSION', 28); define('TABLE_PREFIX', 'myaac_'); define('START_TIME', microtime(true)); define('MYAAC_OS', stripos(PHP_OS, 'WIN') === 0 ? 'WINDOWS' : (strtoupper(PHP_OS) === 'DARWIN' ? 'MAC' : 'LINUX')); @@ -106,4 +106,4 @@ if(isset($_SERVER['HTTP_HOST'])) { //define('CURRENT_URL', BASE_URL . $_SERVER['REQUEST_URI']); } -require SYSTEM . 'exception.php'; \ No newline at end of file +require SYSTEM . 'exception.php'; diff --git a/install/includes/schema.sql b/install/includes/schema.sql index b2e71b80..710c18c1 100644 --- a/install/includes/schema.sql +++ b/install/includes/schema.sql @@ -120,17 +120,6 @@ CREATE TABLE `myaac_forum` KEY `section` (`section`) ) ENGINE=InnoDB DEFAULT CHARACTER SET=utf8; -CREATE TABLE `myaac_hooks` -( - `id` INT(11) NOT NULL AUTO_INCREMENT, - `name` VARCHAR(30) NOT NULL DEFAULT '', - `type` INT(2) NOT NULL DEFAULT 0, - `file` VARCHAR(100) NOT NULL, - `ordering` INT(11) NOT NULL DEFAULT 0, - `enabled` INT(1) NOT NULL DEFAULT 1, - PRIMARY KEY (`id`) -) ENGINE=InnoDB DEFAULT CHARACTER SET=utf8; - CREATE TABLE `myaac_menu` ( `id` INT(11) NOT NULL AUTO_INCREMENT, diff --git a/plugins/example.json b/plugins/example.json index 88276648..b3cf2633 100644 --- a/plugins/example.json +++ b/plugins/example.json @@ -1,4 +1,5 @@ { + "enabled": 0, "name": "Example Plugin", "description": "This is just an example of a Plugin for MyAAC.", "version": "1.0", @@ -32,4 +33,4 @@ "file": "plugins/example/before.php" } } - } \ No newline at end of file + } diff --git a/system/hooks.php b/system/hooks.php index 9e422eed..d976f0fa 100644 --- a/system/hooks.php +++ b/system/hooks.php @@ -27,6 +27,7 @@ define('HOOK_LOGOUT', 15); define('HOOK_FIRST', HOOK_STARTUP); define('HOOK_LAST', HOOK_LOGOUT); +require_once LIBS . 'plugins.php'; class Hook { private $_name, $_type, $_file; @@ -90,27 +91,8 @@ class Hooks public function load() { - global $db; - - $cache = Cache::getInstance(); - if ($cache->enabled()) { - $tmp = ''; - if ($cache->fetch('hooks', $tmp)) { - $hooks = unserialize($tmp); - } - } - - if (!isset($hooks)) { - $hooks = $db->query('SELECT `name`, `type`, `file` FROM `' . TABLE_PREFIX . 'hooks` WHERE `enabled` = 1 ORDER BY `ordering`;')->fetchAll(); - - if ($cache->enabled()) { - $cache->set('hooks', serialize($hooks), 600); - } - } - - foreach($hooks as $hook) { + foreach(Plugins::getHooks() as $hook) { $this->register($hook['name'], $hook['type'], $hook['file']); } } } -?> diff --git a/system/libs/plugins.php b/system/libs/plugins.php index e91d3ff3..fa0ef098 100644 --- a/system/libs/plugins.php +++ b/system/libs/plugins.php @@ -73,6 +73,51 @@ class Plugins { private static $error = null; private static $plugin = array(); + public static function getHooks() + { + $cache = Cache::getInstance(); + if ($cache->enabled()) { + $tmp = ''; + if ($cache->fetch('hooks', $tmp)) { + return unserialize($tmp); + } + } + + $hooks = []; + foreach(get_plugins() as $filename) { + $string = file_get_contents(PLUGINS . $filename . '.json'); + $string = self::removeComments($string); + $plugin = json_decode($string, true); + self::$plugin = $plugin; + if ($plugin == null) { + self::$warnings[] = 'Cannot load ' . $filename . '.json. File might be not a valid json code.'; + continue; + } + + if(isset($plugin['enabled']) && $plugin['enabled'] === 0) { + self::$warnings[] = 'Skipping ' . $filename . '... The plugin is disabled.'; + continue; + } + + if (isset($plugin['hooks'])) { + foreach ($plugin['hooks'] as $_name => $info) { + if (defined('HOOK_'. $info['type'])) { + $hook = constant('HOOK_'. $info['type']); + $hooks[] = ['name' => $_name, 'type' => $hook, 'file' => $info['file']]; + } else { + self::$warnings[] = 'Plugin: ' . $filename . '. Unknown event type: ' . $info['type']; + } + } + } + } + + if ($cache->enabled()) { + $cache->set('hooks', serialize($hooks), 600); + } + + return $hooks; + } + public static function install($file) { global $db; @@ -97,7 +142,7 @@ class Plugins { } else { $string = file_get_contents($file_name); - $string = Plugins::removeComments($string); + $string = self::removeComments($string); $plugin = json_decode($string, true); self::$plugin = $plugin; if ($plugin == null) { @@ -243,22 +288,6 @@ class Plugins { self::$warnings[] = 'Cannot load install script. Your plugin might be not working correctly.'; } - if (isset($plugin['hooks'])) { - foreach ($plugin['hooks'] as $_name => $info) { - if (defined('HOOK_'. $info['type'])) { - $hook = constant('HOOK_'. $info['type']); - $query = $db->query('SELECT `id` FROM `' . TABLE_PREFIX . 'hooks` WHERE `name` = ' . $db->quote($_name) . ';'); - if ($query->rowCount() == 1) { // found something - $query = $query->fetch(); - $db->update(TABLE_PREFIX . 'hooks', array('type' => $hook, 'file' => $info['file']), array('id' => (int)$query['id'])); - } else { - $db->insert(TABLE_PREFIX . 'hooks', array('id' => null, 'name' => $_name, 'type' => $hook, 'file' => $info['file'])); - } - } else - self::$warnings[] = 'Unknown event type: ' . $info['type']; - } - } - $cache = Cache::getInstance(); if($cache->enabled()) { $cache->delete('templates'); @@ -407,4 +436,4 @@ class Plugins { return $string; } -} \ No newline at end of file +} diff --git a/system/migrations/28.php b/system/migrations/28.php new file mode 100644 index 00000000..fd6943cf --- /dev/null +++ b/system/migrations/28.php @@ -0,0 +1,8 @@ +exec('DROP TABLE IF EXISTS `' . TABLE_PREFIX . 'hooks`;'); + +$cache = Cache::getInstance(); +if($cache->enabled()) { + $cache->delete('hooks'); +} \ No newline at end of file diff --git a/system/pages/admin/plugins.php b/system/pages/admin/plugins.php index 54aa1dc6..e3a19d81 100644 --- a/system/pages/admin/plugins.php +++ b/system/pages/admin/plugins.php @@ -10,7 +10,7 @@ defined('MYAAC') or die('Direct access not allowed!'); $title = 'Plugin manager'; -require LIBS . 'plugins.php'; +require_once LIBS . 'plugins.php'; $twig->display('admin.plugins.form.html.twig'); @@ -111,4 +111,3 @@ foreach (get_plugins() as $plugin) { $twig->display('admin.plugins.html.twig', array( 'plugins' => $plugins )); -?> \ No newline at end of file