Skip to content

Commit

Permalink
Fixed issue #16685: Delete Plugin files is not working (#1607)
Browse files Browse the repository at this point in the history
The 'Delete files' button existed but had no action assigned.
Added new 'delete files' feature. Limited to the 'upload' plugin type.
Added pluginType to the info returned by the scanPlugins method
  • Loading branch information
gabrieljenik committed Oct 6, 2020
1 parent 990190b commit de658f2
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 11 deletions.
41 changes: 40 additions & 1 deletion application/controllers/admin/PluginManagerController.php
Expand Up @@ -97,6 +97,19 @@ public function scanFiles()
$oPluginManager = App()->getPluginManager();
$result = $oPluginManager->scanPlugins();

// Add delete URL for each plugin
foreach($result as $name => &$scannedPlugin) {
if (isset($scannedPlugin['pluginType'])&&$scannedPlugin['pluginType']=='upload') {
$scannedPlugin['deleteUrl'] = $this->getController()->createUrl(
'/admin/pluginmanager',
[
'sa' => 'deleteFiles',
'plugin' => $name,
]
);
}
}

Yii::app()->setFlashMessage(
sprintf(
gT('Found %s plugins in file system'),
Expand All @@ -113,7 +126,7 @@ public function scanFiles()
'sa' => 'installPluginFromFile'
]
);
$aData['scanFilesUrl'] = $this->getController()->createUrl(
$data['scanFilesUrl'] = $this->getController()->createUrl(
'/admin/pluginmanager',
[
'sa' => 'scanFiles',
Expand All @@ -132,6 +145,32 @@ public function scanFiles()
//$this->getController()->redirect($indexUrl);
}

public function deleteFiles($plugin) {
$this->checkUpdatePermission();

// Pre supposes the plugin is in the uploads folder. Other plugins are not deletable by button.
$pluginDir =Yii::getPathOfAlias(App()->getPluginManager()->pluginDirs['upload']) . DIRECTORY_SEPARATOR . $plugin;

if (!file_exists($pluginDir)) {
Yii::app()->setFlashMessage(gT('Plugin folder does not exist.'), 'error');
$this->getController()->redirect($this->getPluginManagerUrl());
}

if (!is_writable($pluginDir)) {
Yii::app()->setFlashMessage(gT('Plugin files cannot be deleted due to permissions problem.'), 'error');
$this->getController()->redirect($this->getPluginManagerUrl());
}

if (!rmdirr($pluginDir)) {
Yii::app()->setFlashMessage(gT('Could not remove plugin files.'), 'error');
$this->getController()->redirect($this->getPluginManagerUrl());
} else {
Yii::app()->setFlashMessage(gT('Plugin files successfully deleted.'), 'success');
$this->getController()->redirect($this->getPluginManagerUrl());
}

}

/**
* Activate a plugin
*
Expand Down
14 changes: 12 additions & 2 deletions application/libraries/PluginManager/PluginManager.php
Expand Up @@ -276,7 +276,7 @@ public function scanPlugins($includeInstalledPlugins = false)
$this->shutdownObject->enable();

$result = array();
foreach ($this->pluginDirs as $pluginDir) {
foreach ($this->pluginDirs as $pluginType => $pluginDir) {
$currentDir = Yii::getPathOfAlias($pluginDir);
if (is_dir($currentDir)) {
foreach (new \DirectoryIterator($currentDir) as $fileInfo) {
Expand All @@ -292,6 +292,15 @@ public function scanPlugins($includeInstalledPlugins = false)
if (file_exists($file) && $this->_checkWhitelist($pluginName)) {
try {
$result[$pluginName] = $this->getPluginInfo($pluginName, $pluginDir);
// getPluginInfo returns false instead of an array when config is not found.
// So we build an "empty" array
if (!$result[$pluginName]) {
$result[$pluginName] = array(
'extensionConfig' => null,
'pluginType' => $pluginType,
'load_error' => 0,
);
}
} catch (\Throwable $ex) {
// Load error.
$error = [
Expand All @@ -313,7 +322,8 @@ public function scanPlugins($includeInstalledPlugins = false)
$result[$pluginName] = [
'pluginName' => $pluginName,
'load_error' => 1,
'isCompatible' => false
'isCompatible' => false,
'pluginType' => $plugin->plugin_type,
];
} else {
}
Expand Down
18 changes: 10 additions & 8 deletions application/views/admin/pluginmanager/scanFilesResult.php
Expand Up @@ -20,7 +20,10 @@ class='btn btn-default'
<label class='control-label col-sm-4'>
<?php echo $name; ?>
</label>
<?php if ($scannedPlugin['isCompatible']): ?>
<?php if ($scannedPlugin['load_error'] == 0 && $scannedPlugin['extensionConfig'] == null): ?>
<i class='fa fa-ban text-warning'></i>&nbsp;
<span class='text-warning'><?php eT('Missing configuration file.'); ?></span>
<?php elseif ($scannedPlugin['isCompatible']): ?>
<?php echo CHtml::beginForm($installUrl, 'post', ['style' => 'display: inline-block;']); ?>
<input type='hidden' name='pluginName' value='<?php echo $name; ?>' />
<button href='' class='btn btn-success' data-toggle='tooltip' title='<?php eT('Install this plugin'); ?>'>
Expand All @@ -33,18 +36,17 @@ class='btn btn-default'
&& !$scannedPlugin['isCompatible']): ?>
<i class='fa fa-ban text-warning'></i>&nbsp;
<span class='text-warning'><?php eT('Plugin is not compatible with your LimeSurvey version.'); ?></span>
<?php elseif ($scannedPlugin['load_error'] == 0 && $scannedPlugin['extensionConfig'] == null): ?>
<i class='fa fa-ban text-warning'></i>&nbsp;
<span class='text-warning'><?php eT('Missing configuration file.'); ?></span>
<?php else: ?>
<i class='fa fa-exclamation-triangle text-warning'></i>&nbsp;
<span class='text-warning'><?php eT('Load error. Please contact the plugin author.'); ?></span>
<?php endif; ?>

<a href='' class='btn btn-danger' data-toggle='tooltip' title='<?php eT('Delete this plugin from the file system'); ?>'>
<i class='fa fa-trash'></i>&nbsp;
Delete files
</a>
<?php if (isset($scannedPlugin['deleteUrl'])): ?>
<a href='#' class='btn btn-danger' data-target='#confirmation-modal' data-toggle='modal' data-href='<?=$scannedPlugin['deleteUrl']?>' data-message='<?php eT('Are you sure you want to delete this plugin from the file system?'); ?>' type='submit'>
<i class='fa fa-trash'></i>&nbsp;
<span data-toggle='tooltip' title='<?php eT('Delete this plugin from the file system'); ?>'>Delete files</span>
</a>
<?php endif; ?>
</div>
<?php endforeach; ?>
</div>

0 comments on commit de658f2

Please sign in to comment.