|
|
<?php |
|
|
|
|
|
namespace Kanboard\Core\Plugin; |
|
|
|
|
|
use ZipArchive; |
|
|
use Kanboard\Core\Tool; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Installer extends \Kanboard\Core\Base |
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static function isConfigured() |
|
|
{ |
|
|
return PLUGIN_INSTALLER && is_writable(PLUGINS_DIR) && extension_loaded('zip'); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function install($archiveUrl) |
|
|
{ |
|
|
$zip = $this->downloadPluginArchive($archiveUrl); |
|
|
|
|
|
if (! $zip->extractTo(PLUGINS_DIR)) { |
|
|
$this->cleanupArchive($zip); |
|
|
throw new PluginInstallerException(t('Unable to extract plugin archive.')); |
|
|
} |
|
|
|
|
|
$this->cleanupArchive($zip); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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.')); |
|
|
} |
|
|
|
|
|
Tool::removeAllFiles($pluginFolder); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected function downloadPluginArchive($archiveUrl) |
|
|
{ |
|
|
$zip = new ZipArchive(); |
|
|
$archiveData = $this->httpClient->get($archiveUrl); |
|
|
$archiveFile = tempnam(ini_get('upload_tmp_dir') ? ini_get('upload_tmp_dir') : 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; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected function cleanupArchive(ZipArchive $zip) |
|
|
{ |
|
|
$filename = $zip->filename; |
|
|
$zip->close(); |
|
|
unlink($filename); |
|
|
} |
|
|
} |
|
|
|