]> git.mxchange.org Git - friendica.git/blobdiff - src/Module/Admin/Storage.php
Apply suggestions from code review
[friendica.git] / src / Module / Admin / Storage.php
index 1e1678b138b6bd62b0535a31ebfee786e6524be3..434b43b32ed6e7095ed75b5b1f2fdfbc3e65fedd 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * @copyright Copyright (C) 2010-2021, the Friendica project
+ * @copyright Copyright (C) 2010-2023, the Friendica project
  *
  * @license GNU AGPL version 3 or any later version
  *
@@ -23,23 +23,33 @@ namespace Friendica\Module\Admin;
 
 use Friendica\Core\Renderer;
 use Friendica\DI;
+use Friendica\Core\Storage\Exception\InvalidClassStorageException;
+use Friendica\Core\Storage\Capability\ICanConfigureStorage;
+use Friendica\Core\Storage\Capability\ICanWriteToStorage;
 use Friendica\Module\BaseAdmin;
 use Friendica\Util\Strings;
-use Psr\Log\LogLevel;
 
 class Storage extends BaseAdmin
 {
-       public static function post(array $parameters = [])
+       protected function post(array $request = [])
        {
                self::checkAdminAccess();
 
                self::checkFormSecurityTokenRedirectOnError('/admin/storage', 'admin_storage');
 
-               $storagebackend    = Strings::escapeTags(trim($_POST['storagebackend'] ?? ''));
+               $storagebackend = trim($this->parameters['name'] ?? '');
 
-               // save storage backend form
-               if (DI::storageManager()->setBackend($storagebackend)) {
-                       $storage_opts     = DI::storage()->getOptions();
+               try {
+                       /** @var ICanConfigureStorage|false $newStorageConfig */
+                       $newStorageConfig = DI::storageManager()->getConfigurationByName($storagebackend);
+               } catch (InvalidClassStorageException $storageException) {
+                       DI::sysmsg()->addNotice(DI::l10n()->t('Storage backend, %s is invalid.', $storagebackend));
+                       DI::baseUrl()->redirect('admin/storage');
+               }
+
+               if ($newStorageConfig !== false) {
+                       // save storage backend form
+                       $storage_opts        = $newStorageConfig->getOptions();
                        $storage_form_prefix = preg_replace('|[^a-zA-Z0-9]|', '', $storagebackend);
                        $storage_opts_data   = [];
                        foreach ($storage_opts as $name => $info) {
@@ -57,69 +67,88 @@ class Storage extends BaseAdmin
                        unset($name);
                        unset($info);
 
-                       $storage_form_errors = DI::storage()->saveOptions($storage_opts_data);
+                       $storage_form_errors = $newStorageConfig->saveOptions($storage_opts_data);
                        if (count($storage_form_errors)) {
                                foreach ($storage_form_errors as $name => $err) {
-                                       notice('Storage backend, ' . $storage_opts[$name][1] . ': ' . $err);
+                                       DI::sysmsg()->addNotice(DI::l10n()->t('Storage backend %s error: %s', $storage_opts[$name][1], $err));
                                }
                                DI::baseUrl()->redirect('admin/storage');
                        }
-               } elseif (!empty($storagebackend)) {
-                       notice(DI::l10n()->t('Invalid storage backend setting value.'));
+               }
+
+               if (!empty($_POST['submit_save_set']) && DI::config()->isWritable('storage', 'name') ) {
+                       try {
+                               $newstorage = DI::storageManager()->getWritableStorageByName($storagebackend);
+
+                               if (!DI::storageManager()->setBackend($newstorage)) {
+                                       DI::sysmsg()->addNotice(DI::l10n()->t('Invalid storage backend setting value.'));
+                               }
+                       } catch (InvalidClassStorageException $storageException) {
+                               DI::sysmsg()->addNotice(DI::l10n()->t('Invalid storage backend setting value.'));
+                       }
                }
 
                DI::baseUrl()->redirect('admin/storage');
        }
 
-       public static function content(array $parameters = [])
+       protected function content(array $request = []): string
        {
-               parent::content($parameters);
+               parent::content();
 
                $current_storage_backend = DI::storage();
-               $available_storage_backends = [];
+               $available_storage_forms = [];
 
-               // show legacy option only if it is the current backend:
-               // once changed can't be selected anymore
-               if ($current_storage_backend == null) {
-                       $available_storage_backends[''] = DI::l10n()->t('Database (legacy)');
-               }
+               foreach (DI::storageManager()->listBackends() as $name) {
 
-               foreach (DI::storageManager()->listBackends() as $name => $class) {
-                       $available_storage_backends[$name] = $name;
-               }
+                       // build storage config form,
+                       $storage_form_prefix = preg_replace('|[^a-zA-Z0-9]|', '', $name);
 
-               // build storage config form,
-               $storage_form_prefix = preg_replace('|[^a-zA-Z0-9]|' ,'', $current_storage_backend);
-
-               $storage_form = [];
-               if (!is_null($current_storage_backend) && $current_storage_backend != '') {
-                       foreach ($current_storage_backend->getOptions() as $name => $info) {
-                               $type = $info[0];
-                               // Backward compatibilty with yesno field description
-                               if ($type == 'yesno') {
-                                       $type = 'checkbox';
-                                       // Remove translated labels Yes No from field info
-                                       unset($info[4]);
-                               }
+                       $storage_form  = [];
+                       $storageConfig = DI::storageManager()->getConfigurationByName($name);
+
+                       if ($storageConfig !== false) {
+                               foreach ($storageConfig->getOptions() as $option => $info) {
 
-                               $info[0] = $storage_form_prefix . '_' . $name;
-                               $info['type'] = $type;
-                               $info['field'] = 'field_' . $type . '.tpl';
-                               $storage_form[$name] = $info;
+                                       $type = $info[0];
+                                       // Backward compatibility with yesno field description
+                                       if ($type == 'yesno') {
+                                               $type = 'checkbox';
+                                               // Remove translated labels Yes No from field info
+                                               unset($info[4]);
+                                       }
+
+                                       $info[0]               = $storage_form_prefix . '_' . $option;
+                                       $info['type']          = $type;
+                                       $info['field']         = 'field_' . $type . '.tpl';
+                                       $storage_form[$option] = $info;
+                               }
                        }
+
+                       $available_storage_forms[] = [
+                               'name'   => $name,
+                               'prefix' => $storage_form_prefix,
+                               'form'   => $storage_form,
+                               'active' => $current_storage_backend instanceof ICanWriteToStorage && $name === $current_storage_backend::getName(),
+                       ];
                }
 
                $t = Renderer::getMarkupTemplate('admin/storage.tpl');
 
                return Renderer::replaceMacros($t, [
-                       '$title' => DI::l10n()->t('Administration'),
-                       '$page' => DI::l10n()->t('Storage'),
-                       '$submit' => DI::l10n()->t('Save Settings'),
-                       '$clear' => DI::l10n()->t('Clear'),
-                       '$baseurl' => DI::baseUrl()->get(true),
-                       '$form_security_token' => self::getFormSecurityToken("admin_storage"),
-                       '$storagebackend'   => ['storagebackend', DI::l10n()->t('File storage backend'), $current_storage_backend, DI::l10n()->t('The backend used to store uploaded data. If you change the storage backend, you can manually move the existing files. If you do not do so, the files uploaded before the change will still be available at the old backend. Please see <a href="/help/Settings#1_2_3_1">the settings documentation</a> for more information about the choices and the moving procedure.'), $available_storage_backends],
-                       '$storageform'      => $storage_form,
+                       '$title'                 => DI::l10n()->t('Administration'),
+                       '$label_current'         => DI::l10n()->t('Current Storage Backend'),
+                       '$label_config'          => DI::l10n()->t('Storage Configuration'),
+                       '$page'                  => DI::l10n()->t('Storage'),
+                       '$save'                  => DI::l10n()->t('Save'),
+                       '$save_use'              => DI::l10n()->t('Save & Use storage backend'),
+                       '$use'                   => DI::l10n()->t('Use storage backend'),
+                       '$save_reload'           => DI::l10n()->t('Save & Reload'),
+                       '$noconfig'              => DI::l10n()->t('This backend doesn\'t have custom settings'),
+                       '$form_security_token'   => self::getFormSecurityToken("admin_storage"),
+                       '$storagebackend_ro_txt' => !DI::config()->isWritable('storage', 'name') ? DI::l10n()->t('Changing the current backend is prohibited because it is set by an environment variable') : '',
+                       '$is_writable'           => DI::config()->isWritable('storage', 'name'),
+                       '$storagebackend'        => $current_storage_backend instanceof ICanWriteToStorage ? $current_storage_backend::getName() : DI::l10n()->t('Database (legacy)'),
+                       '$availablestorageforms' => $available_storage_forms,
                ]);
        }
 }