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 --- ChangeLog | 1 + 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 +- doc/cli.markdown | 26 +++++ doc/config.markdown | 13 ++- doc/index.markdown | 1 + doc/plugin-directory.markdown | 15 +++ kanboard | 48 +++++--- 20 files changed, 563 insertions(+), 61 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 create mode 100644 doc/plugin-directory.markdown diff --git a/ChangeLog b/ChangeLog index 94da950e..a1113dcc 100644 --- a/ChangeLog +++ b/ChangeLog @@ -3,6 +3,7 @@ Version 1.0.29 (unreleased) New features: +* Manage plugin from the user interface and from the command line * Added the possibility to convert a subtask to a task * Added menu entry to add tasks from all project views * Add tasks in bulk from the board 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'); diff --git a/doc/cli.markdown b/doc/cli.markdown index 20e3566a..96bffe2d 100644 --- a/doc/cli.markdown +++ b/doc/cli.markdown @@ -41,6 +41,10 @@ Available commands: locale:sync Synchronize all translations based on the fr_FR locale notification notification:overdue-tasks Send notifications for overdue tasks + plugin + plugin:install Install a plugin from a remote Zip archive + plugin:uninstall Remove a plugin + plugin:upgrade Update all installed plugins projects projects:daily-stats Calculate daily statistics for all projects trigger @@ -170,3 +174,25 @@ You will be prompted for a password and confirmation. Characters are not printed ```bash ./kanboard user:reset-2fa my_user ``` + +### Install a plugin + +```bash +./kanboard plugin:install https://github.com/kanboard/plugin-github-auth/releases/download/v1.0.1/GithubAuth-1.0.1.zip +``` + +Note: Installed files will have the same permissions as the current user + +### Remove a plugin + +```bash +./kanboard plugin:uninstall Budget +``` + +### Upgrade all plugins + +```bash +./kanboard plugin:upgrade +* Updating plugin: Budget Planning +* Plugin up to date: Github Authentication +``` diff --git a/doc/config.markdown b/doc/config.markdown index 0e3c3198..0325358d 100644 --- a/doc/config.markdown +++ b/doc/config.markdown @@ -15,14 +15,21 @@ define('LOG_DRIVER', 'file'); // Other drivers are: syslog, stdout, stderr or fi The log driver must be defined if you enable the debug mode. The debug mode logs all SQL queries and the time taken to generate pages. -Plugins folder --------------- +Plugins +------- + +Plugin folder: ```php -// Plugin directory define('PLUGINS_DIR', 'data/plugins'); ``` +Enable/disable plugin installation from the user interface: + +```php +define('PLUGIN_INSTALLER', true); // Default is true +``` + Folder for uploaded files ------------------------- diff --git a/doc/index.markdown b/doc/index.markdown index 5fc576d8..ee982dbb 100644 --- a/doc/index.markdown +++ b/doc/index.markdown @@ -110,6 +110,7 @@ Technical details - [Environment variables](env.markdown) - [Email configuration](email-configuration.markdown) - [URL rewriting](nice-urls.markdown) +- [Plugin Directory](plugin-directory.markdown) ### Database diff --git a/doc/plugin-directory.markdown b/doc/plugin-directory.markdown new file mode 100644 index 00000000..385e3360 --- /dev/null +++ b/doc/plugin-directory.markdown @@ -0,0 +1,15 @@ +Plugin Directory Configuration +============================== + +To install, update and remove plugins from the user interface, you must have those requirements: + +- The plugin directory must be writeable by the web server user +- The Zip extension must be available on your server +- The config parameter `PLUGIN_INSTALLER` must be set at `true` + +To disable this feature, change the value of `PLUGIN_INSTALLER` to `false` in your config file. +You can also change the permissions of the plugin folder on the filesystem. + +Only administrators are allowed to install plugins from the user interface. + +By default, only plugin listed on Kanboard's website are available. diff --git a/kanboard b/kanboard index 6a51c937..49f3fe17 100755 --- a/kanboard +++ b/kanboard @@ -1,8 +1,9 @@ #!/usr/bin/env php dispatch('app.bootstrap', new Event); -$application = new Application('Kanboard', APP_VERSION); -$application->add(new TaskOverdueNotificationCommand($container)); -$application->add(new SubtaskExportCommand($container)); -$application->add(new TaskExportCommand($container)); -$application->add(new ProjectDailyStatsCalculationCommand($container)); -$application->add(new ProjectDailyColumnStatsExportCommand($container)); -$application->add(new TransitionExportCommand($container)); -$application->add(new LocaleSyncCommand($container)); -$application->add(new LocaleComparatorCommand($container)); -$application->add(new TaskTriggerCommand($container)); -$application->add(new CronjobCommand($container)); -$application->add(new ResetPasswordCommand($container)); -$application->add(new ResetTwoFactorCommand($container)); -$application->run(); +try { + + require __DIR__.'/app/common.php'; + + $container['dispatcher']->dispatch('app.bootstrap', new Event); + + $application = new Application('Kanboard', APP_VERSION); + $application->add(new TaskOverdueNotificationCommand($container)); + $application->add(new SubtaskExportCommand($container)); + $application->add(new TaskExportCommand($container)); + $application->add(new ProjectDailyStatsCalculationCommand($container)); + $application->add(new ProjectDailyColumnStatsExportCommand($container)); + $application->add(new TransitionExportCommand($container)); + $application->add(new LocaleSyncCommand($container)); + $application->add(new LocaleComparatorCommand($container)); + $application->add(new TaskTriggerCommand($container)); + $application->add(new CronjobCommand($container)); + $application->add(new ResetPasswordCommand($container)); + $application->add(new ResetTwoFactorCommand($container)); + $application->add(new PluginUpgradeCommand($container)); + $application->add(new PluginInstallCommand($container)); + $application->add(new PluginUninstallCommand($container)); + $application->run(); + +} catch (Exception $e) { + echo $e->getMessage().PHP_EOL; + exit(255); +} -- cgit v1.2.3