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\IStorage;
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($_POST['storagebackend'] ?? ''));
39 /** @var IStorage $newstorage */
40 $newstorage = DI::storageManager()->getByName($storagebackend);
42 // save storage backend form
43 $storage_opts = $newstorage->getOptions();
44 $storage_form_prefix = preg_replace('|[^a-zA-Z0-9]|', '', $storagebackend);
45 $storage_opts_data = [];
46 foreach ($storage_opts as $name => $info) {
47 $fieldname = $storage_form_prefix . '_' . $name;
48 switch ($info[0]) { // type
51 $value = !empty($_POST[$fieldname]);
54 $value = $_POST[$fieldname] ?? '';
56 $storage_opts_data[$name] = $value;
61 $storage_form_errors = $newstorage->saveOptions($storage_opts_data);
62 if (count($storage_form_errors)) {
63 foreach ($storage_form_errors as $name => $err) {
64 notice('Storage backend, ' . $storage_opts[$name][1] . ': ' . $err);
66 DI::baseUrl()->redirect('admin/storage');
69 if (empty($storagebackend) || !DI::storageManager()->setBackend($storagebackend)) {
70 notice(DI::l10n()->t('Invalid storage backend setting value.'));
73 DI::baseUrl()->redirect('admin/storage');
76 public static function content(array $parameters = [])
78 parent::content($parameters);
80 $current_storage_backend = DI::storage();
81 $available_storage_backends = [];
82 $available_storage_forms = [];
84 // show legacy option only if it is the current backend:
85 // once changed can't be selected anymore
86 if ($current_storage_backend == null) {
87 $available_storage_backends[''] = DI::l10n()->t('Database (legacy)');
90 foreach (DI::storageManager()->listBackends() as $name => $class) {
91 $available_storage_backends[$name] = $name;
93 // build storage config form,
94 $storage_form_prefix = preg_replace('|[^a-zA-Z0-9]|', '', $name);
97 foreach (DI::storageManager()->getByName($name)->getOptions() as $option => $info) {
99 // Backward compatibilty with yesno field description
100 if ($type == 'yesno') {
102 // Remove translated labels Yes No from field info
106 $info[0] = $storage_form_prefix . '_' . $option;
107 $info['type'] = $type;
108 $info['field'] = 'field_' . $type . '.tpl';
109 $storage_form[$option] = $info;
112 if (count($storage_form) > 0) {
113 $available_storage_forms[] = [
115 'prefix' => $storage_form_prefix,
116 'form' => $storage_form,
121 $t = Renderer::getMarkupTemplate('admin/storage.tpl');
123 return Renderer::replaceMacros($t, [
124 '$title' => DI::l10n()->t('Administration'),
125 '$page' => DI::l10n()->t('Storage'),
126 '$submit' => DI::l10n()->t('Save Settings'),
127 '$clear' => DI::l10n()->t('Clear'),
128 '$baseurl' => DI::baseUrl()->get(true),
129 '$form_security_token' => self::getFormSecurityToken("admin_storage"),
130 '$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],
131 '$availablestorageforms' => $available_storage_forms,