From cde42ec3fa1f73fb55a6a7c2f2215b0af12e1c5b Mon Sep 17 00:00:00 2001 From: slawkens Date: Thu, 19 Oct 2017 10:06:43 +0200 Subject: [PATCH] * new command line tool: install_plugin.php * can be used to install plugins from command line. Usage: "php install_plugin.php path_to_file" * fixed if HTTP_HOST not set in common.php when executed from command line * Added 'Are you sure?' popup when uninstalling plugin * Moved plugin install logic to a new class: Plugins * added some warnings when plugin json file is incomplete --- common.php | 19 +-- system/bin/install_plugin.php | 38 ++++++ system/libs/plugins.php | 151 +++++++++++++++++++++++ system/pages/admin/plugins.php | 110 +++-------------- system/templates/admin.plugins.html.twig | 2 +- 5 files changed, 216 insertions(+), 104 deletions(-) create mode 100644 system/bin/install_plugin.php create mode 100644 system/libs/plugins.php diff --git a/common.php b/common.php index d23132e8..b70b1909 100644 --- a/common.php +++ b/common.php @@ -86,12 +86,15 @@ $basedir = str_replace('/admin', '', $basedir); $basedir = str_replace('/install', '', $basedir); define('BASE_DIR', $basedir); -if(isset($_SERVER['HTTPS'][0]) && $_SERVER['HTTPS'] == 'on') - define('SERVER_URL', 'https://' . $_SERVER['HTTP_HOST']); -else - define('SERVER_URL', 'http://' . $_SERVER['HTTP_HOST']); - -define('BASE_URL', SERVER_URL . BASE_DIR . '/'); -define('ADMIN_URL', SERVER_URL . BASE_DIR . '/admin/'); -//define('CURRENT_URL', BASE_URL . $_SERVER['REQUEST_URI']); +if(isset($_SERVER['HTTP_HOST'])) { + if (isset($_SERVER['HTTPS'][0]) && $_SERVER['HTTPS'] == 'on') + define('SERVER_URL', 'https://' . $_SERVER['HTTP_HOST']); + else + define('SERVER_URL', 'http://' . $_SERVER['HTTP_HOST']); + + define('BASE_URL', SERVER_URL . BASE_DIR . '/'); + define('ADMIN_URL', SERVER_URL . BASE_DIR . '/admin/'); + + //define('CURRENT_URL', BASE_URL . $_SERVER['REQUEST_URI']); +} ?> diff --git a/system/bin/install_plugin.php b/system/bin/install_plugin.php new file mode 100644 index 00000000..4d7e02e1 --- /dev/null +++ b/system/bin/install_plugin.php @@ -0,0 +1,38 @@ + \ No newline at end of file diff --git a/system/libs/plugins.php b/system/libs/plugins.php new file mode 100644 index 00000000..8ef65c9a --- /dev/null +++ b/system/libs/plugins.php @@ -0,0 +1,151 @@ + + * @author Slawkens + * @copyright 2017 MyAAC + * @version 0.6.1 + * @link http://my-aac.org + */ +defined('MYAAC') or die('Direct access not allowed!'); + +class Plugins { + private static $warnings = array(); + private static $error = null; + private static $pluginInfo = array(); + + public static function install($file) { + global $db, $cache; + + $zip = new ZipArchive(); + if($zip->open($file)) { + for ($i = 0; $i < $zip->numFiles; $i++) { + $tmp = $zip->getNameIndex($i); + if(pathinfo($tmp, PATHINFO_DIRNAME) == 'plugins' && pathinfo($tmp, PATHINFO_EXTENSION) == 'json') + $json_file = $tmp; + } + + if(!isset($json_file)) { + self::$error = 'Cannot find plugin info .json file. Installation is discontinued.'; + return false; + } + + if($zip->extractTo(BASE)) { // place in the directory with same name + $file_name = BASE . $json_file; + if(!file_exists($file_name)) { + self::$error = "Cannot load " . $file_name . ". File doesn't exist."; + return false; + } + else { + $string = file_get_contents($file_name); + $plugin = json_decode($string, true); + self::$pluginInfo = $plugin; + if ($plugin == null) { + self::$warnings[] = 'Cannot load ' . $file_name . '. File might be not a valid json code.'; + } + else { + $continue = true; + + if(!isset($plugin['name'])) { + self::$warnings[] = 'Plugin "name" tag is not set.'; + } + if(!isset($plugin['description'])) { + self::$warnings[] = 'Plugin "description" tag is not set.'; + } + if(!isset($plugin['version'])) { + self::$warnings[] = 'Plugin "version" tag is not set.'; + } + if(!isset($plugin['author'])) { + self::$warnings[] = 'Plugin "author" tag is not set.'; + } + if(!isset($plugin['contact'])) { + self::$warnings[] = 'Plugin "contact" tag is not set.'; + } + + if(isset($plugin['require'])) { + $require = $plugin['require']; + if(isset($require['myaac'])) { + $require_myaac = $require['myaac']; + if(version_compare(MYAAC_VERSION, $require_myaac, '<')) { + self::$warnings[] = "This plugin requires MyAAC version " . $require_myaac . ", you're using version " . MYAAC_VERSION . " - please update."; + $continue = false; + } + } + + if(isset($require['php'])) { + $require_php = $require['php']; + if(version_compare(phpversion(), $require_php, '<')) { + self::$warnings[] = "This plugin requires PHP version " . $require_php . ", you're using version " . phpversion() . " - please update."; + $continue = false; + } + } + + if(isset($require['database'])) { + $require_database = $require['database']; + if($require_database < DATABASE_VERSION) { + self::$warnings[] = "This plugin requires database version " . $require_database . ", you're using version " . DATABASE_VERSION . " - please update."; + $continue = false; + } + } + } + + if($continue) { + if (isset($plugin['install'])) { + if (file_exists(BASE . $plugin['install'])) + require(BASE . $plugin['install']); + else + self::$warnings[] = 'Cannot load install script. Your plugin might be not working correctly.'; + } + + if (isset($plugin['hooks'])) { + foreach ($plugin['hooks'] as $_name => $info) { + if (isset($hook_types[$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->query('UPDATE `' . TABLE_PREFIX . 'hooks` SET `type` = ' . $hook_types[$info['type']] . ', `file` = ' . $db->quote($info['file']) . ' WHERE `id` = ' . (int)$query['id'] . ';'); + } else { + $db->query('INSERT INTO `' . TABLE_PREFIX . 'hooks` (`id`, `name`, `type`, `file`) VALUES (NULL, ' . $db->quote($_name) . ', ' . $hook_types[$info['type']] . ', ' . $db->quote($info['file']) . ');'); + } + } else + self::$warnings[] = 'Unknown event type: ' . $info['type']; + } + } + + if($cache->enabled()) { + $cache->delete('templates'); + } + + $zip->close(); + return true; + } + } + } + } + else { + self::$error = 'There was a problem with extracting zip archive.'; + } + + $zip->close(); + } + else { + self::$error = 'There was a problem with opening zip archive.'; + } + + return false; + } + + public static function getWarnings() { + return self::$warnings; + } + + public static function getError() { + return self::$error; + } + + public static function getPluginInfo() { + return self::$pluginInfo; + } +} \ No newline at end of file diff --git a/system/pages/admin/plugins.php b/system/pages/admin/plugins.php index e1062e26..b18dc3bb 100644 --- a/system/pages/admin/plugins.php +++ b/system/pages/admin/plugins.php @@ -12,6 +12,7 @@ defined('MYAAC') or die('Direct access not allowed!'); $title = 'Plugin manager'; require(SYSTEM . 'hooks.php'); +require(LIBS . 'plugins.php'); function deleteDirectory($dir) { if(!file_exists($dir)) { @@ -115,98 +116,17 @@ else if(isset($_FILES["plugin"]["name"])) $targetzip = BASE . 'plugins/' . $tmp_filename . '.zip'; if(move_uploaded_file($tmp_name, $targetzip)) { // move uploaded file - $zip = new ZipArchive(); - if ($zip->open($targetzip)) { - for ($i = 0; $i < $zip->numFiles; $i++) { - $tmp = $zip->getNameIndex($i); - if(pathinfo($tmp, PATHINFO_DIRNAME) == 'plugins' && pathinfo($tmp, PATHINFO_EXTENSION) == 'json') - $json_file = $tmp; + if(Plugins::install($targetzip)) { + foreach(Plugins::getWarnings() as $warning) { + warning($warning); } - - if(!isset($json_file)) { - error('Cannot find plugin info .json file. Installation is discontinued.'); - } - else if($zip->extractTo(BASE)) { // place in the directory with same name - $file_name = BASE . $json_file; - if(!file_exists($file_name)) - warning("Cannot load " . $file_name . ". File doesn't exist."); - else { - $string = file_get_contents($file_name); - $plugin = json_decode($string, true); - if ($plugin == null) { - warning('Cannot load ' . $file_name . '. File might be not a valid json code.'); - } - else { - $continue = true; - - if(isset($plugin['require'])) { - $require = $plugin['require']; - if(isset($require['myaac'])) { - $require_myaac = $require['myaac']; - if(version_compare(MYAAC_VERSION, $require_myaac, '<')) { - warning("This plugin requires MyAAC version " . $require_myaac . ", you're using version " . MYAAC_VERSION . " - please update."); - $continue = false; - } - } - - if(isset($require['php'])) { - $require_php = $require['php']; - if(version_compare(phpversion(), $require_php, '<')) { - warning("This plugin requires PHP version " . $require_php . ", you're using version " . phpversion() . " - please update."); - $continue = false; - } - } - - if(isset($require['database'])) { - $require_database = $require['database']; - if($require_database < DATABASE_VERSION) { - warning("This plugin requires database version " . $require_database . ", you're using version " . DATABASE_VERSION . " - please update."); - $continue = false; - } - } - } - - if($continue) { - if (isset($plugin['install'])) { - if (file_exists(BASE . $plugin['install'])) - require(BASE . $plugin['install']); - else - warning('Cannot load install script. Your plugin might be not working correctly.'); - } - - if (isset($plugin['hooks'])) { - foreach ($plugin['hooks'] as $_name => $info) { - if (isset($hook_types[$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->query('UPDATE `' . TABLE_PREFIX . 'hooks` SET `type` = ' . $hook_types[$info['type']] . ', `file` = ' . $db->quote($info['file']) . ' WHERE `id` = ' . (int)$query['id'] . ';'); - } else { - $db->query('INSERT INTO `' . TABLE_PREFIX . 'hooks` (`id`, `name`, `type`, `file`) VALUES (NULL, ' . $db->quote($_name) . ', ' . $hook_types[$info['type']] . ', ' . $db->quote($info['file']) . ');'); - } - } else - warning('Unknown event type: ' . $info['type']); - } - } - - if($cache->enabled()) { - $cache->delete('templates'); - } - success('' . $plugin['name'] . ' plugin has been successfully installed.'); - } - } - } - } - else { - error('There was a problem with extracting zip archive.'); - } - - $zip->close(); - unlink($targetzip); // delete the Zipped file - } - else { - error('There was a problem with opening zip archive.'); + $info = Plugins::getPluginInfo(); + success((isset($info['name']) ? '' . $info['name'] . ' p' : 'P') . 'lugin has been successfully installed.'); } + else + error(Plugins::getError()); + + unlink($targetzip); // delete the Zipped file } else error('There was a problem with the upload. Please try again.'); @@ -231,11 +151,11 @@ foreach(get_plugins() as $plugin) } else { $plugins[] = array( - 'name' => $plugin_info['name'], - 'description' => $plugin_info['description'], - 'version' => $plugin_info['version'], - 'author' => $plugin_info['author'], - 'contact' => $plugin_info['contact'], + 'name' => isset($plugin_info['name']) ? $plugin_info['name'] : '', + 'description' => isset($plugin_info['description']) ? $plugin_info['description'] : '', + 'version' => isset($plugin_info['version']) ? $plugin_info['version'] : '', + 'author' => isset($plugin_info['author']) ? $plugin_info['author'] : '', + 'contact' => isset($plugin_info['contact']) ? $plugin_info['contact'] : '', 'file' => $plugin, 'uninstall' => isset($plugin_info['uninstall']) ); diff --git a/system/templates/admin.plugins.html.twig b/system/templates/admin.plugins.html.twig index 9549c8f2..c0130026 100644 --- a/system/templates/admin.plugins.html.twig +++ b/system/templates/admin.plugins.html.twig @@ -15,7 +15,7 @@ {{ plugin.version }} {{ plugin.author }} {{ plugin.contact }} - Uninstall + Uninstall {% endfor %} \ No newline at end of file