3 * @copyright Copyright (C) 2010-2023, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Module\Admin;
24 use Friendica\Core\Renderer;
26 use Friendica\Core\Storage\Exception\InvalidClassStorageException;
27 use Friendica\Core\Storage\Capability\ICanConfigureStorage;
28 use Friendica\Core\Storage\Capability\ICanWriteToStorage;
29 use Friendica\Module\BaseAdmin;
30 use Friendica\Util\Strings;
32 class Storage extends BaseAdmin
34 protected function post(array $request = [])
36 self::checkAdminAccess();
38 self::checkFormSecurityTokenRedirectOnError('/admin/storage', 'admin_storage');
40 $storagebackend = trim($this->parameters['name'] ?? '');
43 /** @var ICanConfigureStorage|false $newStorageConfig */
44 $newStorageConfig = DI::storageManager()->getConfigurationByName($storagebackend);
45 } catch (InvalidClassStorageException $storageException) {
46 DI::sysmsg()->addNotice(DI::l10n()->t('Storage backend, %s is invalid.', $storagebackend));
47 DI::baseUrl()->redirect('admin/storage');
50 if ($newStorageConfig !== false) {
51 // save storage backend form
52 $storage_opts = $newStorageConfig->getOptions();
53 $storage_form_prefix = preg_replace('|[^a-zA-Z0-9]|', '', $storagebackend);
54 $storage_opts_data = [];
55 foreach ($storage_opts as $name => $info) {
56 $fieldname = $storage_form_prefix . '_' . $name;
57 switch ($info[0]) { // type
60 $value = !empty($_POST[$fieldname]);
63 $value = $_POST[$fieldname] ?? '';
65 $storage_opts_data[$name] = $value;
70 $storage_form_errors = $newStorageConfig->saveOptions($storage_opts_data);
71 if (count($storage_form_errors)) {
72 foreach ($storage_form_errors as $name => $err) {
73 DI::sysmsg()->addNotice(DI::l10n()->t('Storage backend %s error: %s', $storage_opts[$name][1], $err));
75 DI::baseUrl()->redirect('admin/storage');
79 if (!empty($_POST['submit_save_set']) && DI::config()->isWritable('storage', 'name') ) {
81 $newstorage = DI::storageManager()->getWritableStorageByName($storagebackend);
83 if (!DI::storageManager()->setBackend($newstorage)) {
84 DI::sysmsg()->addNotice(DI::l10n()->t('Invalid storage backend setting value.'));
86 } catch (InvalidClassStorageException $storageException) {
87 DI::sysmsg()->addNotice(DI::l10n()->t('Invalid storage backend setting value.'));
91 DI::baseUrl()->redirect('admin/storage');
94 protected function content(array $request = []): string
98 $current_storage_backend = DI::storage();
99 $available_storage_forms = [];
101 foreach (DI::storageManager()->listBackends() as $name) {
103 // build storage config form,
104 $storage_form_prefix = preg_replace('|[^a-zA-Z0-9]|', '', $name);
107 $storageConfig = DI::storageManager()->getConfigurationByName($name);
109 if ($storageConfig !== false) {
110 foreach ($storageConfig->getOptions() as $option => $info) {
113 // Backward compatibility with yesno field description
114 if ($type == 'yesno') {
116 // Remove translated labels Yes No from field info
120 $info[0] = $storage_form_prefix . '_' . $option;
121 $info['type'] = $type;
122 $info['field'] = 'field_' . $type . '.tpl';
123 $storage_form[$option] = $info;
127 $available_storage_forms[] = [
129 'prefix' => $storage_form_prefix,
130 'form' => $storage_form,
131 'active' => $current_storage_backend instanceof ICanWriteToStorage && $name === $current_storage_backend::getName(),
135 $t = Renderer::getMarkupTemplate('admin/storage.tpl');
137 return Renderer::replaceMacros($t, [
138 '$title' => DI::l10n()->t('Administration'),
139 '$label_current' => DI::l10n()->t('Current Storage Backend'),
140 '$label_config' => DI::l10n()->t('Storage Configuration'),
141 '$page' => DI::l10n()->t('Storage'),
142 '$save' => DI::l10n()->t('Save'),
143 '$save_use' => DI::l10n()->t('Save & Use storage backend'),
144 '$use' => DI::l10n()->t('Use storage backend'),
145 '$save_reload' => DI::l10n()->t('Save & Reload'),
146 '$noconfig' => DI::l10n()->t('This backend doesn\'t have custom settings'),
147 '$form_security_token' => self::getFormSecurityToken("admin_storage"),
148 '$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') : '',
149 '$is_writable' => DI::config()->isWritable('storage', 'name'),
150 '$storagebackend' => $current_storage_backend instanceof ICanWriteToStorage ? $current_storage_backend::getName() : DI::l10n()->t('Database (legacy)'),
151 '$availablestorageforms' => $available_storage_forms,