/** * Full installation process of plugin * * @param string $tempFileName * @param string $tempFilePath * @param stdclass $json * @return stdclass */ function install($tempFileName, $tempFilePath, $json) { // get config values $rmConfig = new RM_Config(); $chmodOctal = intval($rmConfig->getValue('rm_config_chmod_value'), 8); $rootPath = RM_Environment::getConnector()->getRootPath(); $chunks = explode('.', $tempFileName); $pluginName = $chunks[0]; //Plugin name will be always the first chunk, example: price.0.1.1.zip $pluginModel = new RM_Plugins(); $existingPlugin = $pluginModel->fetchByName($pluginName); if ($existingPlugin !== null) { unlink($tempFilePath); throw new RM_Exception($this->_translate->_('Admin.Plugins.InstallMsg', 'PluginAlreadyInstalled')); } $pluginFolderPath = $rootPath . DIRECTORY_SEPARATOR . 'RM' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . $pluginName; if (is_dir($pluginFolderPath)) { $result = RM_Filesystem::deleteFolder($pluginFolderPath); if ($result == false) { unlink($tempFilePath); throw new RM_Exception($this->_translate->_('Admin.Plugins.InstallMsg', 'PluginFolderAlreadyExists') . ': ' . $pluginFolderPath); } } $result = mkdir($pluginFolderPath, $chmodOctal); if ($result == false) { unlink($tempFilePath); throw new RM_Exception($this->_translate->_('Admin.Plugins.InstallMsg', 'CreatePluginFolderFailer')); } else { $json = $this->_addMessageToJson($json, $this->_translate->_('Admin.Plugins.InstallMsg', 'PluginFolderCreatedSuccessfully')); } //4. unzip plugin into new directory if (!extension_loaded('zlib')) { unlink($tempFilePath); RM_Filesystem::deleteFolder($pluginFolderPath); throw new RM_Exception($this->_translate->_('Admin.Plugins.InstallMsg', 'ZlibNotSupported')); } $zip = new PclZip($tempFilePath); $result = $zip->extract(PCLZIP_OPT_PATH, $pluginFolderPath); if (!$result) { unlink($tempFilePath); RM_Filesystem::deleteFolder($pluginFolderPath); throw new RM_Exception($this->_translate->_('Admin.Plugins.InstallMsg', 'UnzipFailed')); } else { $json = $this->_addMessageToJson($json, $this->_translate->_('Admin.Plugins.InstallMsg', 'UnzipSuccessfully')); } unlink($tempFilePath); chmod($pluginFolderPath, $chmodOctal); //4.0. create separate folder in 'userdata/plugins' for a new plugin $userdataFolderPath = $rootPath . DIRECTORY_SEPARATOR . 'RM' . DIRECTORY_SEPARATOR . 'userdata' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . $pluginName; if (is_dir($userdataFolderPath)) { $result = RM_Filesystem::deleteFolder($userdataFolderPath); if ($result == false) { throw new RM_Exception($this->_translate->_('Admin.Plugins.InstallMsg', 'PluginFolderAlreadyExists') . ': ' . $userdataFolderPath); } } $result = mkdir($userdataFolderPath . DIRECTORY_SEPARATOR, $chmodOctal); if ($result == false) { RM_Filesystem::deleteFolder($pluginFolderPath); throw new RM_Exception($this->_translate->_('Admin.Plugins.InstallMsg', 'CreatePluginUserdataFolderFailer')); } else { $json = $this->_addMessageToJson($json, $this->_translate->_('Admin.Plugins.InstallMsg', 'PluginFolderCreatedSuccessfully')); } @rename($pluginFolderPath . DIRECTORY_SEPARATOR . 'views', $userdataFolderPath . DIRECTORY_SEPARATOR . 'views'); @chmod($pluginFolderPath . DIRECTORY_SEPARATOR . 'views', $chmodOctal); @rename($pluginFolderPath . DIRECTORY_SEPARATOR . 'languages', $userdataFolderPath . DIRECTORY_SEPARATOR . 'languages'); @chmod($pluginFolderPath . DIRECTORY_SEPARATOR . 'languages', $chmodOctal); @rename($pluginFolderPath . DIRECTORY_SEPARATOR . 'css', $userdataFolderPath . DIRECTORY_SEPARATOR . 'css'); @chmod($pluginFolderPath . DIRECTORY_SEPARATOR . 'css', $chmodOctal); @rename($pluginFolderPath . DIRECTORY_SEPARATOR . 'images', $userdataFolderPath . DIRECTORY_SEPARATOR . 'images'); @chmod($pluginFolderPath . DIRECTORY_SEPARATOR . 'images', $chmodOctal); @chmod($userdataFolderPath, $chmodOctal); //5. get INI file $iniFilePath = $pluginFolderPath . DIRECTORY_SEPARATOR . self::$_iniFilename; if (is_file($iniFilePath) == false) { RM_Filesystem::deleteFolder($pluginFolderPath); throw new RM_Exception($this->_translate->_('Admin.Plugins.InstallMsg', 'NoIniFile')); } //6. parse INI file $parser = new RM_Plugin_Config_Parser(); try { $config = $parser->getConfig($iniFilePath); } catch (RM_Exception $e) { //Error in ini file parsing RM_Filesystem::deleteFolder($pluginFolderPath); throw new RM_Exception($e->getMessage()); } //Check: could be a module if no 'module' value is in config if ($config->information['module'] == "") { RM_Filesystem::deleteFolder($pluginFolderPath); throw new RM_Exception($this->_translate->_('Admin.Plugins.InstallMsg', 'WrongTryToInstallModule')); } $json = $this->_addMessageToJson($json, $this->_translate->_('Admin.Plugins.InstallMsg', 'UnzipSuccess')); //7. invoke SQL install file try { $result = self::installDatabase($pluginFolderPath); } catch (Exception $e) { self::uninstallDatabase($pluginFolderPath); RM_Filesystem::deleteFolder($pluginFolderPath); throw new RM_Exception($e->getMessage()); } if ($result == false) { self::uninstallDatabase($pluginFolderPath); RM_Filesystem::deleteFolder($pluginFolderPath); throw new RM_Exception($this->_translate->_('Admin.Plugins.InstallMsg', 'WrongInstallSQLQueries')); } else { $json = $this->_addMessageToJson($json, $this->_translate->_('Admin.Plugins.InstallMsg', 'InstallSQLSuccess')); } //8. create a new record in Plugin database $pluginRow = array(); $pluginRow = $config->information; $pluginRow['module_name'] = $pluginRow['module']; unset($pluginRow['module']); //convert creation date into MySQL format $rmConfig = new RM_Config(); $pluginRow['creation_date'] = $rmConfig->convertDates(strtotime($pluginRow['creation_date']), RM_Config::TIMESTAMP_DATEFORMAT, RM_Config::MYSQL_DATEFORMAT); //TODO: //1.check the insertions $pkey = $pluginModel->insert($pluginRow); //Create a new records in dependencies database table $model = new RM_Dependencies(); if (is_array($config->dependencies)) { foreach ($config->dependencies as $dependency) { $model->insert($dependency); } } //9. get the main class of the plugin $pluginClassFilepath = $pluginFolderPath . DIRECTORY_SEPARATOR . RM_Plugin_Config::CLASSES . DIRECTORY_SEPARATOR . 'RM' . DIRECTORY_SEPARATOR . 'Plugin' . DIRECTORY_SEPARATOR . $config->information['name'] . '.php'; if (is_file($pluginClassFilepath) == false) { self::uninstallDatabase($pluginFolderPath); RM_Filesystem::deleteFolder($pluginFolderPath); throw new RM_Exception($this->_translate->_('Admin.Plugins.InstallMsg', 'DoesntExists')); } require_once $pluginClassFilepath; $pluginClassName = 'RM_Plugin_' . $config->information['name']; if (class_exists($pluginClassName) == false) { self::uninstallDatabase($pluginFolderPath); RM_Filesystem::deleteFolder($pluginFolderPath); throw new RM_Exception($pluginClassName . $this->_translate->_('Admin.Plugins.InstallMsg', 'DoesntExists')); } $pluginObject = new $pluginClassName(); if (!$pluginObject instanceof RM_Plugin_Interface) { self::uninstallDatabase($pluginFolderPath); RM_Filesystem::deleteFolder($pluginFolderPath); throw new RM_Exception($pluginClassName . $this->_translate->_('Admin.Plugins.InstallMsg', 'WrongInterface')); } //10. invoke install method of that class (if we need something extra) $result = $pluginObject->install(); $json = $this->_addMessageToJson($json, $this->_translate->_('Admin.Plugins.InstallMsg', 'InstallSuccess')); //Check dependencies and if some of them is validate==false disable plugin $dependencyManager = new RM_Dependency_Manager(); $plugin = $pluginModel->find($pkey)->current(); $dependencies = $dependencyManager->getDependencies($plugin); foreach ($dependencies as $dependency) { if ($dependency->validate() == false) { try { $pluginModel->disable($plugin); break; } catch (Exception $e) { //TODO: add log message that we could not disable this module. } } } return $json; }
public function uninstall() { $pluginsModel = new RM_Plugins(); $plugin = $pluginsModel->find($this->name)->current(); if ($plugin !== null) { $dependenciesModel = new RM_Dependencies(); $dependencies = $dependenciesModel->getDependencies($plugin); foreach ($dependencies as $dependency) { $dependency->delete(); } } }
public function disableJsonAction() { $json = new stdClass(); $json->success = 1; $json->msg = array(); $ids = $this->_getParam('ids', array()); $manager = new RM_Plugin_Manager($this->_translate); $model = new RM_Plugins(); foreach ($ids as $id) { $row = $model->find($id)->current(); try { $manager->disable($row); } catch (Exception $e) { $json->success = 0; $json->msg[] = $e->getMessage(); } } return array('data' => $json); }