protected function body() { if (!$this->userHasPrivileges(User::pluginsAdd)) { return false; } $inputs = array('name' => array('isName', 'isNotEmpty')); if (!$this->isInputValid($inputs)) { return false; } $name = $this->getParams('name'); $existingPluginsWithSameName = Repositories::getRepository(Repositories::Plugin)->findBy(['name' => $name]); if (count($existingPluginsWithSameName) > 0) { return $this->death(StringID::PluginNameAlreadyExists); } $pluginFile = $this->getUploadedFile('plugin'); if (!$pluginFile) { return false; } $pluginFolder = Config::get('paths', 'plugins') . $name; if (file_exists($pluginFolder)) { return $this->death(StringID::PluginFolderAlreadyExists); } if (!Filesystem::createDir($pluginFolder)) { return $this->death(StringID::FileSystemError); } if (!Compression::unzip($pluginFile, $pluginFolder)) { $this->death(StringID::UnzipUnsuccessful); goto cleanup_error; } $manifestFile = $pluginFolder . DIRECTORY_SEPARATOR . 'manifest.xml'; $manifest = null; if (!($manifest = $this->parsePluginManifest($manifestFile))) { $this->death(StringID::BadlyFormedPlugin); goto cleanup_error; } if (!file_exists($pluginFolder . DIRECTORY_SEPARATOR . $manifest['mainFile'])) { $this->death(StringID::BadlyFormedPlugin); goto cleanup_error; } $plugin = new \Plugin(); $plugin->setIdentifier($manifest['identifier']); $plugin->setDescription($manifest['description']); $plugin->setConfig($manifest['arguments']); $plugin->setMainfile($name . '/' . $manifest['mainFile']); $plugin->setName($name); $plugin->setType($manifest['type']); Repositories::persistAndFlush($plugin); Filesystem::removeFile($pluginFile); return true; cleanup_error: Filesystem::removeDir($pluginFolder); Filesystem::removeFile($pluginFile); return false; }
public function testZipSingleFolder() { mkdir('extracted'); $this->assertTrue(Compression::zip('hiDir', 'hi.zip')); $this->assertTrue(file_exists('hi.zip')); $this->assertFalse(file_exists('extracted/hi.txt')); $this->assertFalse(file_exists('extracted/hiDir')); $this->assertTrue(Compression::unzip('hi.zip', 'extracted')); $this->assertTrue(file_exists('extracted/hi.txt')); $this->assertFalse(file_exists('extracted/hiDir/hi.txt')); unlink('hi.zip'); unlink('extracted/hi.txt'); rmdir('extracted'); }
/** * Runs plugin with supplied arguments. * * @li First element in @c $args must be a path to ZIP archive with input data. * @li Plugin execution is stopped on all triggered errors or uncaught exceptions * and plugin error is returned. * @li setUp() and execute() methods are run respectively after input is * unpacked and output folder is created. * @li Plugin creates and later removes two temporary folders. It can also create * one ZIP archive with plugin output (path is returned in plugin results). * * @param array $args simple array with plugin arguments * @return PluginResponse plugin results or error */ public final function run(array $args) { set_error_handler(array('asm\\utils\\Utils', 'turnErrorToException')); $cwd = getcwd(); try { if ($args == null || count($args) < 1) { throw new PluginUseException('Data file argument missing'); } $this->dataFolder = Filesystem::tempDir(); $dataFile = array_shift($args); if (!Compression::unzip($dataFile, $this->dataFolder, $unzipMessage)) { $response = PluginResponse::createError("ZIP extraction failed (" . $unzipMessage . ").\n\nPerhaps you did not submit a ZIP file " . "or that file was corrupted during upload. You may try again. Extraction was attempted to folder " . str_replace("\\", "/", $this->dataFolder)); } else { $this->outputFolder = Filesystem::tempDir(); chdir($this->dataFolder); $this->setUp($args); $this->execute(); chdir($cwd); $outputFile = $this->packOutput(); $outputPath = $outputFile != null ? realpath($outputFile) : null; $response = PluginResponse::create($this->results, $outputPath); } } catch (PluginException $e) { $response = PluginResponse::createError($e->getMessage()); } catch (Exception $e) { $response = PluginResponse::createError('Runtime error: ' . $e->getMessage() . " (file " . $e->getFile() . ", line " . $e->getLine()); } // If an exception occurred during $this->setUp or $this->execute, we must still change the current directory back, // in case more plugins are to be run (in a test case battery) chdir($cwd); restore_error_handler(); if ($this->dataFolder != null) { Filesystem::removeDir($this->dataFolder); } if ($this->outputFolder != null) { Filesystem::removeDir($this->outputFolder); } return $response; }