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\IWritableStorage;
27 use Friendica\Module\BaseAdmin;
28 use Friendica\Util\Strings;
30 class Storage extends BaseAdmin
32 public static function post(array $parameters = [])
34 self::checkAdminAccess();
36 self::checkFormSecurityTokenRedirectOnError('/admin/storage', 'admin_storage');
38 $storagebackend = Strings::escapeTags(trim($parameters['name'] ?? ''));
40 /** @var IWritableStorage $newstorage */
41 $newstorage = DI::storageManager()->getWritableStorageByName($storagebackend);
43 // save storage backend form
44 $storage_opts = $newstorage->getOptions();
45 $storage_form_prefix = preg_replace('|[^a-zA-Z0-9]|', '', $storagebackend);
46 $storage_opts_data = [];
47 foreach ($storage_opts as $name => $info) {
48 $fieldname = $storage_form_prefix . '_' . $name;
49 switch ($info[0]) { // type
52 $value = !empty($_POST[$fieldname]);
55 $value = $_POST[$fieldname] ?? '';
57 $storage_opts_data[$name] = $value;
62 $storage_form_errors = $newstorage->saveOptions($storage_opts_data);
63 if (count($storage_form_errors)) {
64 foreach ($storage_form_errors as $name => $err) {
65 notice('Storage backend, ' . $storage_opts[$name][1] . ': ' . $err);
67 DI::baseUrl()->redirect('admin/storage');
70 if (!empty($_POST['submit_save_set'])) {
71 /** @var IWritableStorage $newstorage */
72 $newstorage = DI::storageManager()->getWritableStorageByName($storagebackend);
74 if (!DI::storageManager()->setBackend($newstorage)) {
75 notice(DI::l10n()->t('Invalid storage backend setting value.'));
79 DI::baseUrl()->redirect('admin/storage');
82 public static function content(array $parameters = [])
84 parent::content($parameters);
86 $current_storage_backend = DI::storage();
87 $available_storage_forms = [];
89 foreach (DI::storageManager()->listBackends() as $name => $class) {
91 // build storage config form,
92 $storage_form_prefix = preg_replace('|[^a-zA-Z0-9]|', '', $name);
95 foreach (DI::storageManager()->getWritableStorageByName($name)->getOptions() as $option => $info) {
97 // Backward compatibilty with yesno field description
98 if ($type == 'yesno') {
100 // Remove translated labels Yes No from field info
104 $info[0] = $storage_form_prefix . '_' . $option;
105 $info['type'] = $type;
106 $info['field'] = 'field_' . $type . '.tpl';
107 $storage_form[$option] = $info;
110 $available_storage_forms[] = [
112 'prefix' => $storage_form_prefix,
113 'form' => $storage_form,
114 'active' => $current_storage_backend instanceof IWritableStorage && $name === $current_storage_backend::getName(),
118 $t = Renderer::getMarkupTemplate('admin/storage.tpl');
120 return Renderer::replaceMacros($t, [
121 '$title' => DI::l10n()->t('Administration'),
122 '$page' => DI::l10n()->t('Storage'),
123 '$save' => DI::l10n()->t('Save'),
124 '$save_activate' => DI::l10n()->t('Save & Activate'),
125 '$activate' => DI::l10n()->t('Activate'),
126 '$save_reload' => DI::l10n()->t('Save & Reload'),
127 '$noconfig' => DI::l10n()->t('This backend doesn\'t have custom settings'),
128 '$baseurl' => DI::baseUrl()->get(true),
129 '$form_security_token' => self::getFormSecurityToken("admin_storage"),
130 '$storagebackend' => $current_storage_backend instanceof IWritableStorage ? $current_storage_backend::getName() : DI::l10n()->t('Database (legacy)'),
131 '$availablestorageforms' => $available_storage_forms,