From 8d69c49da595c60dae51c77d48f397ab97fdf318 Mon Sep 17 00:00:00 2001 From: Frederic Guillot Date: Fri, 20 May 2016 12:51:05 -0400 Subject: Manage plugins from the user interface and from the command line --- app/Console/BaseCommand.php | 2 + app/Console/PluginInstallCommand.php | 35 ++++++ app/Console/PluginUninstallCommand.php | 35 ++++++ app/Console/PluginUpgradeCommand.php | 53 +++++++++ app/Controller/PluginController.php | 93 ++++++++++++++- app/Core/Plugin/Installer.php | 162 +++++++++++++++++++++++++++ app/Core/Plugin/Loader.php | 15 +-- app/Core/Plugin/PluginInstallerException.php | 15 +++ app/ServiceProvider/RouteProvider.php | 2 +- app/Template/board/table_column.php | 2 +- app/Template/plugin/directory.php | 65 +++++++---- app/Template/plugin/remove.php | 13 +++ app/Template/plugin/show.php | 22 +++- app/constants.php | 6 +- 14 files changed, 479 insertions(+), 41 deletions(-) create mode 100644 app/Console/PluginInstallCommand.php create mode 100644 app/Console/PluginUninstallCommand.php create mode 100644 app/Console/PluginUpgradeCommand.php create mode 100644 app/Core/Plugin/Installer.php create mode 100644 app/Core/Plugin/PluginInstallerException.php create mode 100644 app/Template/plugin/remove.php (limited to 'app') diff --git a/app/Console/BaseCommand.php b/app/Console/BaseCommand.php index 4444ceba..ca566266 100644 --- a/app/Console/BaseCommand.php +++ b/app/Console/BaseCommand.php @@ -26,6 +26,8 @@ use Symfony\Component\Console\Command\Command; * @property \Kanboard\Model\UserNotification $userNotification * @property \Kanboard\Model\UserNotificationFilter $userNotificationFilter * @property \Kanboard\Model\ProjectUserRole $projectUserRole + * @property \Kanboard\Core\Plugin\Loader $pluginLoader + * @property \Kanboard\Core\Http\Client $httpClient * @property \Symfony\Component\EventDispatcher\EventDispatcher $dispatcher */ abstract class BaseCommand extends Command diff --git a/app/Console/PluginInstallCommand.php b/app/Console/PluginInstallCommand.php new file mode 100644 index 00000000..1c6e14b3 --- /dev/null +++ b/app/Console/PluginInstallCommand.php @@ -0,0 +1,35 @@ +setName('plugin:install') + ->setDescription('Install a plugin from a remote Zip archive') + ->addArgument('url', InputArgument::REQUIRED, 'Archive URL'); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + if (!Installer::isConfigured()) { + $output->writeln('Kanboard is not configured to install plugins itself'); + } + + try { + $installer = new Installer($this->container); + $installer->install($input->getArgument('url')); + $output->writeln('Plugin installed successfully'); + } catch (PluginInstallerException $e) { + $output->writeln(''.$e->getMessage().''); + } + } +} diff --git a/app/Console/PluginUninstallCommand.php b/app/Console/PluginUninstallCommand.php new file mode 100644 index 00000000..c645e03f --- /dev/null +++ b/app/Console/PluginUninstallCommand.php @@ -0,0 +1,35 @@ +setName('plugin:uninstall') + ->setDescription('Remove a plugin') + ->addArgument('pluginId', InputArgument::REQUIRED, 'Plugin directory name'); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + if (!Installer::isConfigured()) { + $output->writeln('Kanboard is not configured to remove plugins itself'); + } + + try { + $installer = new Installer($this->container); + $installer->uninstall($input->getArgument('pluginId')); + $output->writeln('Plugin removed successfully'); + } catch (PluginInstallerException $e) { + $output->writeln(''.$e->getMessage().''); + } + } +} diff --git a/app/Console/PluginUpgradeCommand.php b/app/Console/PluginUpgradeCommand.php new file mode 100644 index 00000000..6ec5836d --- /dev/null +++ b/app/Console/PluginUpgradeCommand.php @@ -0,0 +1,53 @@ +setName('plugin:upgrade') + ->setDescription('Update all installed plugins') + ; + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + if (!Installer::isConfigured()) { + $output->writeln('Kanboard is not configured to upgrade plugins itself'); + } + + $installer = new Installer($this->container); + $availablePlugins = $this->httpClient->getJson(PLUGIN_API_URL); + + foreach ($this->pluginLoader->getPlugins() as $installedPlugin) { + $pluginDetails = $this->getPluginDetails($availablePlugins, $installedPlugin); + + if ($pluginDetails === null) { + $output->writeln('* Plugin not available in the directory: '.$installedPlugin->getPluginName().''); + } elseif ($pluginDetails['version'] > $installedPlugin->getPluginVersion()) { + $output->writeln('* Updating plugin: '.$installedPlugin->getPluginName().''); + $installer->update($pluginDetails['download']); + } else { + $output->writeln('* Plugin up to date: '.$installedPlugin->getPluginName().''); + } + } + } + + protected function getPluginDetails(array $availablePlugins, BasePlugin $installedPlugin) + { + foreach ($availablePlugins as $availablePlugin) { + if ($availablePlugin['title'] === $installedPlugin->getPluginName()) { + return $availablePlugin; + } + } + + return null; + } +} diff --git a/app/Controller/PluginController.php b/app/Controller/PluginController.php index 8d5628f1..b6f9a33b 100644 --- a/app/Controller/PluginController.php +++ b/app/Controller/PluginController.php @@ -2,6 +2,9 @@ namespace Kanboard\Controller; +use Kanboard\Core\Plugin\Installer; +use Kanboard\Core\Plugin\PluginInstallerException; + /** * Class PluginController * @@ -18,8 +21,9 @@ class PluginController extends BaseController public function show() { $this->response->html($this->helper->layout->plugin('plugin/show', array( - 'plugins' => $this->pluginLoader->plugins, + 'plugins' => $this->pluginLoader->getPlugins(), 'title' => t('Installed Plugins'), + 'is_configured' => Installer::isConfigured(), ))); } @@ -28,11 +32,94 @@ class PluginController extends BaseController */ public function directory() { - $plugins = $this->httpClient->getJson(PLUGIN_API_URL); + $installedPlugins = array(); + + foreach ($this->pluginLoader->getPlugins() as $plugin) { + $installedPlugins[$plugin->getPluginName()] = $plugin->getPluginVersion(); + } $this->response->html($this->helper->layout->plugin('plugin/directory', array( - 'plugins' => $plugins, + 'installed_plugins' => $installedPlugins, + 'available_plugins' => $this->httpClient->getJson(PLUGIN_API_URL), 'title' => t('Plugin Directory'), + 'is_configured' => Installer::isConfigured(), ))); } + + /** + * Install plugin from URL + * + * @throws \Kanboard\Core\Controller\AccessForbiddenException + */ + public function install() + { + $this->checkCSRFParam(); + $pluginArchiveUrl = urldecode($this->request->getStringParam('archive_url')); + + try { + $installer = new Installer($this->container); + $installer->install($pluginArchiveUrl); + $this->flash->success(t('Plugin installed successfully.')); + } catch (PluginInstallerException $e) { + $this->flash->failure($e->getMessage()); + } + + $this->response->redirect($this->helper->url->to('PluginController', 'show')); + } + + /** + * Update plugin from URL + * + * @throws \Kanboard\Core\Controller\AccessForbiddenException + */ + public function update() + { + $this->checkCSRFParam(); + $pluginArchiveUrl = urldecode($this->request->getStringParam('archive_url')); + + try { + $installer = new Installer($this->container); + $installer->update($pluginArchiveUrl); + $this->flash->success(t('Plugin updated successfully.')); + } catch (PluginInstallerException $e) { + $this->flash->failure($e->getMessage()); + } + + $this->response->redirect($this->helper->url->to('PluginController', 'show')); + } + + /** + * Confirmation before to remove the plugin + */ + public function confirm() + { + $pluginId = $this->request->getStringParam('pluginId'); + $plugins = $this->pluginLoader->getPlugins(); + + $this->response->html($this->template->render('plugin/remove', array( + 'plugin_id' => $pluginId, + 'plugin' => $plugins[$pluginId], + ))); + } + + /** + * Remove a plugin + * + * @throws \Kanboard\Core\Controller\AccessForbiddenException + */ + public function uninstall() + { + $this->checkCSRFParam(); + $pluginId = $this->request->getStringParam('pluginId'); + + try { + $installer = new Installer($this->container); + $installer->uninstall($pluginId); + $this->flash->success(t('Plugin removed successfully.')); + } catch (PluginInstallerException $e) { + $this->flash->failure($e->getMessage()); + } + + $this->response->redirect($this->helper->url->to('PluginController', 'show')); + } } diff --git a/app/Core/Plugin/Installer.php b/app/Core/Plugin/Installer.php new file mode 100644 index 00000000..48c4d978 --- /dev/null +++ b/app/Core/Plugin/Installer.php @@ -0,0 +1,162 @@ +downloadPluginArchive($archiveUrl); + + if (! $zip->extractTo(PLUGINS_DIR)) { + $this->cleanupArchive($zip); + throw new PluginInstallerException(t('Unable to extract plugin archive.')); + } + + $this->cleanupArchive($zip); + } + + /** + * Uninstall a plugin + * + * @access public + * @param string $pluginId + * @throws PluginInstallerException + */ + public function uninstall($pluginId) + { + $pluginFolder = PLUGINS_DIR.DIRECTORY_SEPARATOR.basename($pluginId); + + if (! file_exists($pluginFolder)) { + throw new PluginInstallerException(t('Plugin not found.')); + } + + if (! is_writable($pluginFolder)) { + throw new PluginInstallerException(e('You don\'t have the permission to remove this plugin.')); + } + + $this->removeAllDirectories($pluginFolder); + } + + /** + * Update a plugin + * + * @access public + * @param string $archiveUrl + * @throws PluginInstallerException + */ + public function update($archiveUrl) + { + $zip = $this->downloadPluginArchive($archiveUrl); + + $firstEntry = $zip->statIndex(0); + $this->uninstall($firstEntry['name']); + + if (! $zip->extractTo(PLUGINS_DIR)) { + $this->cleanupArchive($zip); + throw new PluginInstallerException(t('Unable to extract plugin archive.')); + } + + $this->cleanupArchive($zip); + } + + /** + * Download archive from URL + * + * @access protected + * @param string $archiveUrl + * @return ZipArchive + * @throws PluginInstallerException + */ + protected function downloadPluginArchive($archiveUrl) + { + $zip = new ZipArchive(); + $archiveData = $this->httpClient->get($archiveUrl); + $archiveFile = tempnam(sys_get_temp_dir(), 'kb_plugin'); + + if (empty($archiveData)) { + unlink($archiveFile); + throw new PluginInstallerException(t('Unable to download plugin archive.')); + } + + if (file_put_contents($archiveFile, $archiveData) === false) { + unlink($archiveFile); + throw new PluginInstallerException(t('Unable to write temporary file for plugin.')); + } + + if ($zip->open($archiveFile) !== true) { + unlink($archiveFile); + throw new PluginInstallerException(t('Unable to open plugin archive.')); + } + + if ($zip->numFiles === 0) { + unlink($archiveFile); + throw new PluginInstallerException(t('There is no file in the plugin archive.')); + } + + return $zip; + } + + /** + * Remove archive file + * + * @access protected + * @param ZipArchive $zip + */ + protected function cleanupArchive(ZipArchive $zip) + { + unlink($zip->filename); + $zip->close(); + } + + /** + * Remove recursively a directory + * + * @access protected + * @param string $directory + */ + protected function removeAllDirectories($directory) + { + $it = new RecursiveDirectoryIterator($directory, RecursiveDirectoryIterator::SKIP_DOTS); + $files = new RecursiveIteratorIterator($it, RecursiveIteratorIterator::CHILD_FIRST); + + foreach ($files as $file) { + if ($file->isDir()) { + rmdir($file->getRealPath()); + } else { + unlink($file->getRealPath()); + } + } + + rmdir($directory); + } +} diff --git a/app/Core/Plugin/Loader.php b/app/Core/Plugin/Loader.php index 400517b7..f2f6add7 100644 --- a/app/Core/Plugin/Loader.php +++ b/app/Core/Plugin/Loader.php @@ -18,16 +18,16 @@ class Loader extends \Kanboard\Core\Base /** * Plugin instances * - * @access public + * @access protected * @var array */ - public $plugins = array(); + protected $plugins = array(); /** * Get list of loaded plugins * * @access public - * @return array + * @return Base[] */ public function getPlugins() { @@ -52,7 +52,7 @@ class Loader extends \Kanboard\Core\Base if ($fileInfo->isDir() && substr($fileInfo->getFilename(), 0, 1) !== '.') { $pluginName = $fileInfo->getFilename(); $this->loadSchema($pluginName); - $this->initializePlugin($this->loadPlugin($pluginName)); + $this->initializePlugin($pluginName, $this->loadPlugin($pluginName)); } } } @@ -95,9 +95,10 @@ class Loader extends \Kanboard\Core\Base * Initialize plugin * * @access public - * @param Base $plugin + * @param string $pluginName + * @param Base $plugin */ - public function initializePlugin(Base $plugin) + public function initializePlugin($pluginName, Base $plugin) { if (method_exists($plugin, 'onStartup')) { $this->dispatcher->addListener('app.bootstrap', array($plugin, 'onStartup')); @@ -107,6 +108,6 @@ class Loader extends \Kanboard\Core\Base Tool::buildDICHelpers($this->container, $plugin->getHelpers()); $plugin->initialize(); - $this->plugins[] = $plugin; + $this->plugins[$pluginName] = $plugin; } } diff --git a/app/Core/Plugin/PluginInstallerException.php b/app/Core/Plugin/PluginInstallerException.php new file mode 100644 index 00000000..7d356c9b --- /dev/null +++ b/app/Core/Plugin/PluginInstallerException.php @@ -0,0 +1,15 @@ +addRoute('extensions', 'PluginController', 'show'); - $container['route']->addRoute('extensions/list', 'PluginController', 'directory'); + $container['route']->addRoute('extensions/directory', 'PluginController', 'directory'); // Doc $container['route']->addRoute('documentation/:file', 'doc', 'show'); diff --git a/app/Template/board/table_column.php b/app/Template/board/table_column.php index a356849c..eced52dc 100644 --- a/app/Template/board/table_column.php +++ b/app/Template/board/table_column.php @@ -37,7 +37,7 @@ user->hasProjectAccess('TaskCreationController', 'show', $column['project_id'])): ?>
  • - + url->link(t('Create tasks in bulk'), 'TaskBulkController', 'show', array('project_id' => $column['project_id'], 'column_id' => $column['id'], 'swimlane_id' => $swimlane['id']), false, 'popover') ?>
  • 0): ?> diff --git a/app/Template/plugin/directory.php b/app/Template/plugin/directory.php index 82b9a441..b6c6734c 100644 --- a/app/Template/plugin/directory.php +++ b/app/Template/plugin/directory.php @@ -2,29 +2,54 @@

    - + +

    + +

    + + +

    - + +
    - - - - - + + + + + + + + + - - - - - - - - - -
    + text->e($plugin['title']) ?> +
    + text->e($plugin['author']) ?> + + text->e($plugin['version']) ?> + + + + + url->link(t('Install'), 'PluginController', 'install', array('archive_url' => urlencode($plugin['download'])), true) ?> + + + url->link(t('Update'), 'PluginController', 'update', array('archive_url' => urlencode($plugin['download'])), true) ?> + + + + + + + + +
    +
    + text->markdown($plugin['description']) ?> +
    +
    - text->e($plugin['title']) ?> - text->e($plugin['author']) ?>text->e($plugin['version']) ?>text->e($plugin['description']) ?> -
    + diff --git a/app/Template/plugin/remove.php b/app/Template/plugin/remove.php new file mode 100644 index 00000000..bd8f4eb8 --- /dev/null +++ b/app/Template/plugin/remove.php @@ -0,0 +1,13 @@ + + +
    +

    getPluginName()) ?>

    + +
    + url->link(t('Yes'), 'PluginController', 'uninstall', array('pluginId' => $plugin_id), true, 'btn btn-red') ?> + + url->link(t('cancel'), 'PluginController', 'show', array(), false, 'close-popover') ?> +
    +
    diff --git a/app/Template/plugin/show.php b/app/Template/plugin/show.php index 8358fb2a..9c3d6d20 100644 --- a/app/Template/plugin/show.php +++ b/app/Template/plugin/show.php @@ -5,15 +5,17 @@

    - +
    - - + + - + + + - + $plugin): ?> - + + + + + +
    getPluginHomepage()): ?> @@ -24,7 +26,15 @@ text->e($plugin->getPluginAuthor()) ?> text->e($plugin->getPluginVersion()) ?>text->e($plugin->getPluginDescription()) ?> + + url->link(t('Uninstall'), 'PluginController', 'confirm', array('pluginId' => $pluginFolder), false, 'popover') ?> +
    text->e($plugin->getPluginDescription()) ?>
    diff --git a/app/constants.php b/app/constants.php index 31510c5f..3c404d8b 100644 --- a/app/constants.php +++ b/app/constants.php @@ -12,8 +12,10 @@ defined('DATA_DIR') or define('DATA_DIR', ROOT_DIR.DIRECTORY_SEPARATOR.'data'); // Files directory (attachments) defined('FILES_DIR') or define('FILES_DIR', DATA_DIR.DIRECTORY_SEPARATOR.'files'); -// Plugins directory +// Plugins settings defined('PLUGINS_DIR') or define('PLUGINS_DIR', ROOT_DIR.DIRECTORY_SEPARATOR.'plugins'); +defined('PLUGIN_API_URL') or define('PLUGIN_API_URL', 'https://kanboard.net/plugins.json'); +defined('PLUGIN_INSTALLER') or define('PLUGIN_INSTALLER', true); // Enable/disable debug defined('DEBUG') or define('DEBUG', strtolower(getenv('DEBUG')) === 'true'); @@ -131,5 +133,3 @@ defined('HTTP_PROXY_HOSTNAME') or define('HTTP_PROXY_HOSTNAME', ''); defined('HTTP_PROXY_PORT') or define('HTTP_PROXY_PORT', '3128'); defined('HTTP_PROXY_USERNAME') or define('HTTP_PROXY_USERNAME', ''); defined('HTTP_PROXY_PASSWORD') or define('HTTP_PROXY_PASSWORD', ''); - -defined('PLUGIN_API_URL') or define('PLUGIN_API_URL', 'https://kanboard.net/plugins.json'); -- cgit v1.2.3