3 * @copyright Copyright (C) 2010-2021, 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\Model\Storage\InvalidClassStorageException;
27 use Friendica\Model\Storage\IStorageConfiguration;
28 use Friendica\Model\Storage\IWritableStorage;
29 use Friendica\Module\BaseAdmin;
30 use Friendica\Util\Strings;
32 class Storage extends BaseAdmin
34 public static function post(array $parameters = [])
36 self::checkAdminAccess();
38 self::checkFormSecurityTokenRedirectOnError('/admin/storage', 'admin_storage');
40 $storagebackend = Strings::escapeTags(trim($parameters['name'] ?? ''));
43 /** @var IStorageConfiguration|false $newStorageConfig */
44 $newStorageConfig = DI::storageManager()->getConfigurationByName($storagebackend);
45 } catch (InvalidClassStorageException $storageException) {
46 notice(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 notice(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'])) {
81 /** @var IWritableStorage $newstorage */
82 $newstorage = DI::storageManager()->getWritableStorageByName($storagebackend);
84 if (!DI::storageManager()->setBackend($newstorage)) {
85 notice(DI::l10n()->t('Invalid storage backend setting value.'));
87 } catch (InvalidClassStorageException $storageException) {
88 notice(DI::l10n()->t('Invalid storage backend setting value.'));
92 DI::baseUrl()->redirect('admin/storage');
95 public static function content(array $parameters = [])
97 parent::content($parameters);
99 $current_storage_backend = DI::storage();
100 $available_storage_forms = [];
102 foreach (DI::storageManager()->listBackends() as $name) {
104 // build storage config form,
105 $storage_form_prefix = preg_replace('|[^a-zA-Z0-9]|', '', $name);
108 $storageConfig = DI::storageManager()->getConfigurationByName($name);
110 if ($storageConfig !== false) {
111 foreach ($storageConfig->getOptions() as $option => $info) {
114 // Backward compatibilty with yesno field description
115 if ($type == 'yesno') {
117 // Remove translated labels Yes No from field info
121 $info[0] = $storage_form_prefix . '_' . $option;
122 $info['type'] = $type;
123 $info['field'] = 'field_' . $type . '.tpl';
124 $storage_form[$option] = $info;
128 $available_storage_forms[] = [
130 'prefix' => $storage_form_prefix,
131 'form' => $storage_form,
132 'active' => $current_storage_backend instanceof IWritableStorage && $name === $current_storage_backend::getName(),
136 $t = Renderer::getMarkupTemplate('admin/storage.tpl');
138 return Renderer::replaceMacros($t, [
139 '$title' => DI::l10n()->t('Administration'),
140 '$label_current' => DI::l10n()->t('Current Storage Backend'),
141 '$label_config' => DI::l10n()->t('Storage Configuration'),
142 '$page' => DI::l10n()->t('Storage'),
143 '$save' => DI::l10n()->t('Save'),
144 '$save_use' => DI::l10n()->t('Save & Use storage backend'),
145 '$use' => DI::l10n()->t('Use storage backend'),
146 '$save_reload' => DI::l10n()->t('Save & Reload'),
147 '$noconfig' => DI::l10n()->t('This backend doesn\'t have custom settings'),
148 '$baseurl' => DI::baseUrl()->get(true),
149 '$form_security_token' => self::getFormSecurityToken("admin_storage"),
150 '$storagebackend' => $current_storage_backend instanceof IWritableStorage ? $current_storage_backend::getName() : DI::l10n()->t('Database (legacy)'),
151 '$availablestorageforms' => $available_storage_forms,