Exemplo n.º 1
0
 /**
  * {@inheritdoc}
  */
 public function execute()
 {
     $function = rex_request('function', 'string');
     if (!in_array($function, ['install', 'uninstall', 'activate', 'deactivate', 'delete'])) {
         throw new rex_api_exception('Unknown package function "' . $function . '"!');
     }
     $packageId = rex_request('package', 'string');
     $package = rex_package::get($packageId);
     if ($function == 'uninstall' && !$package->isInstalled() || $function == 'activate' && $package->isAvailable() || $function == 'deactivate' && !$package->isAvailable() || $function == 'delete' && !rex_package::exists($packageId)) {
         throw new rex_api_exception('Illegal operation "' . $function . '" for package "' . $packageId . '"');
     }
     if ($package instanceof rex_null_package) {
         throw new rex_api_exception('Package "' . $packageId . '" doesn\'t exists!');
     }
     $reinstall = 'install' === $function && $package->isInstalled();
     $manager = rex_package_manager::factory($package);
     $success = $manager->{$function}();
     $message = $manager->getMessage();
     $result = new rex_api_result($success, $message);
     if ($success && !$reinstall) {
         $result->setRequiresReboot(true);
     }
     return $result;
 }
Exemplo n.º 2
0
 /**
  * checks whether an api function is bound to the current requests. If so, so the api function will be executed.
  */
 public static function handleCall()
 {
     if (static::hasFactoryClass()) {
         return static::callFactoryClass(__FUNCTION__, func_get_args());
     }
     $apiFunc = self::factory();
     if ($apiFunc != null) {
         if ($apiFunc->published !== true) {
             if (rex::isBackend() !== true) {
                 throw new rex_http_exception(new rex_api_exception('the api function ' . get_class($apiFunc) . ' is not published, therefore can only be called from the backend!'), rex_response::HTTP_FORBIDDEN);
             }
             if (!rex::getUser()) {
                 throw new rex_http_exception(new rex_api_exception('missing backend session to call api function ' . get_class($apiFunc) . '!'), rex_response::HTTP_UNAUTHORIZED);
             }
         }
         $urlResult = rex_get(self::REQ_RESULT_PARAM, 'string');
         if ($urlResult) {
             // take over result from url and do not execute the apiFunc
             $result = rex_api_result::fromJSON($urlResult);
             $apiFunc->result = $result;
         } else {
             try {
                 $result = $apiFunc->execute();
                 if (!$result instanceof rex_api_result) {
                     throw new rex_exception('Illegal result returned from api-function ' . rex_get(self::REQ_CALL_PARAM) . '. Expected a instance of rex_api_result but got "' . (is_object($result) ? get_class($result) : gettype($result)) . '".');
                 }
                 $apiFunc->result = $result;
                 if ($result->requiresReboot()) {
                     $context = rex_context::fromGet();
                     // add api call result to url
                     $context->setParam(self::REQ_RESULT_PARAM, $result->toJSON());
                     // and redirect to SELF for reboot
                     rex_response::sendRedirect($context->getUrl([], false));
                 }
             } catch (rex_api_exception $e) {
                 $message = $e->getMessage();
                 $result = new rex_api_result(false, $message);
                 $apiFunc->result = $result;
             }
         }
     }
 }
Exemplo n.º 3
0
 public function execute()
 {
     if (!rex::getUser()->isAdmin()) {
         throw new rex_api_exception('You do not have the permission!');
     }
     $installAddon = rex_addon::get('install');
     $versions = self::getVersions();
     $versionId = rex_request('version_id', 'int');
     if (!isset($versions[$versionId])) {
         return null;
     }
     $version = $versions[$versionId];
     if (!rex_string::versionCompare($version['version'], rex::getVersion(), '>')) {
         throw new rex_api_exception(sprintf('Existing version of Core (%s) is newer than %s', rex::getVersion(), $version['version']));
     }
     try {
         $archivefile = rex_install_webservice::getArchive($version['path']);
     } catch (rex_functional_exception $e) {
         throw new rex_api_exception($e->getMessage());
     }
     $message = '';
     $temppath = rex_path::coreCache('.new.core/');
     try {
         if ($version['checksum'] != md5_file($archivefile)) {
             throw new rex_functional_exception($installAddon->i18n('warning_zip_wrong_checksum'));
         }
         if (!rex_install_archive::extract($archivefile, $temppath)) {
             throw new rex_functional_exception($installAddon->i18n('warning_core_zip_not_extracted'));
         }
         if (!is_dir($temppath . 'core')) {
             throw new rex_functional_exception($installAddon->i18n('warning_zip_wrong_format'));
         }
         $coreAddons = [];
         /** @var rex_addon[] $updateAddons */
         $updateAddons = [];
         if (is_dir($temppath . 'addons')) {
             foreach (rex_finder::factory($temppath . 'addons')->dirsOnly() as $dir) {
                 $addonkey = $dir->getBasename();
                 $addonPath = $dir->getRealPath() . '/';
                 if (!file_exists($addonPath . rex_package::FILE_PACKAGE)) {
                     continue;
                 }
                 $config = rex_file::getConfig($addonPath . rex_package::FILE_PACKAGE);
                 if (!isset($config['version']) || rex_addon::exists($addonkey) && rex_string::versionCompare($config['version'], rex_addon::get($addonkey)->getVersion(), '<')) {
                     continue;
                 }
                 $coreAddons[$addonkey] = $addonkey;
                 if (rex_addon::exists($addonkey)) {
                     $updateAddons[$addonkey] = rex_addon::get($addonkey);
                     $updateAddonsConfig[$addonkey] = $config;
                 }
             }
         }
         //$config = rex_file::getConfig($temppath . 'core/default.config.yml');
         //foreach ($config['system_addons'] as $addonkey) {
         //    if (is_dir($temppath . 'addons/' . $addonkey) && rex_addon::exists($addonkey)) {
         //        $updateAddons[$addonkey] = rex_addon::get($addonkey);
         //    }
         //}
         $this->checkRequirements($temppath, $version['version'], $updateAddonsConfig);
         if (file_exists($temppath . 'core/update.php')) {
             include $temppath . 'core/update.php';
         }
         foreach ($updateAddons as $addonkey => $addon) {
             if ($addon->isInstalled() && file_exists($file = $temppath . 'addons/' . $addonkey . '/' . rex_package::FILE_UPDATE)) {
                 try {
                     $addon->includeFile($file);
                     if ($msg = $addon->getProperty('updatemsg', '')) {
                         throw new rex_functional_exception($msg);
                     }
                     if (!$addon->getProperty('update', true)) {
                         throw new rex_functional_exception(rex_i18n::msg('package_no_reason'));
                     }
                 } catch (rex_functional_exception $e) {
                     throw new rex_functional_exception($addonkey . ': ' . $e->getMessage(), $e);
                 } catch (rex_sql_exception $e) {
                     throw new rex_functional_exception($addonkey . ': SQL error: ' . $e->getMessage(), $e);
                 }
             }
         }
         // create backup
         $installConfig = rex_file::getCache($installAddon->getDataPath('config.json'));
         if (isset($installConfig['backups']) && $installConfig['backups']) {
             rex_dir::create($installAddon->getDataPath());
             $archive = $installAddon->getDataPath(strtolower(preg_replace('/[^a-z0-9-_.]/i', '_', rex::getVersion())) . '.zip');
             rex_install_archive::copyDirToArchive(rex_path::core(), $archive);
             foreach ($updateAddons as $addonkey => $addon) {
                 rex_install_archive::copyDirToArchive($addon->getPath(), $archive, 'addons/' . $addonkey);
             }
         }
         // copy plugins to new addon dirs
         foreach ($updateAddons as $addonkey => $addon) {
             foreach ($addon->getRegisteredPlugins() as $plugin) {
                 $pluginPath = $temppath . 'addons/' . $addonkey . '/plugins/' . $plugin->getName();
                 if (!is_dir($pluginPath)) {
                     rex_dir::copy($plugin->getPath(), $pluginPath);
                 } elseif ($plugin->isInstalled() && is_dir($pluginPath . '/assets')) {
                     rex_dir::copy($pluginPath . '/assets', $plugin->getAssetsPath());
                 }
             }
         }
         // move temp dirs to permanent destination
         rex_dir::delete(rex_path::core());
         rename($temppath . 'core', rex_path::core());
         if (is_dir(rex_path::core('assets'))) {
             rex_dir::copy(rex_path::core('assets'), rex_path::coreAssets());
         }
         foreach ($coreAddons as $addonkey) {
             if (isset($updateAddons[$addonkey])) {
                 rex_dir::delete(rex_path::addon($addonkey));
             }
             rename($temppath . 'addons/' . $addonkey, rex_path::addon($addonkey));
             if (is_dir(rex_path::addon($addonkey, 'assets'))) {
                 rex_dir::copy(rex_path::addon($addonkey, 'assets'), rex_path::addonAssets($addonkey));
             }
         }
     } catch (rex_functional_exception $e) {
         $message = $e->getMessage();
     } catch (rex_sql_exception $e) {
         $message = 'SQL error: ' . $e->getMessage();
     }
     rex_file::delete($archivefile);
     rex_dir::delete($temppath);
     if ($message) {
         $message = $installAddon->i18n('warning_core_not_updated') . '<br />' . $message;
         $success = false;
     } else {
         $message = $installAddon->i18n('info_core_updated');
         $success = true;
         rex_delete_cache();
         rex_install_webservice::deleteCache('core');
     }
     $result = new rex_api_result($success, $message);
     if ($success) {
         $result->setRequiresReboot(true);
     }
     return $result;
 }