]> git.mxchange.org Git - friendica.git/blob - src/Module/Admin/Storage.php
Fix legacy storage config
[friendica.git] / src / Module / Admin / Storage.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 namespace Friendica\Module\Admin;
23
24 use Friendica\Core\Renderer;
25 use Friendica\DI;
26 use Friendica\Model\Storage\IStorage;
27 use Friendica\Module\BaseAdmin;
28 use Friendica\Util\Strings;
29
30 class Storage extends BaseAdmin
31 {
32         public static function post(array $parameters = [])
33         {
34                 self::checkAdminAccess();
35
36                 self::checkFormSecurityTokenRedirectOnError('/admin/storage', 'admin_storage');
37
38                 $storagebackend = Strings::escapeTags(trim($parameters['name'] ?? ''));
39
40                 /** @var IStorage $newstorage */
41                 $newstorage = DI::storageManager()->getByName($storagebackend);
42
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
50                                 case 'checkbox':
51                                 case 'yesno':
52                                         $value = !empty($_POST[$fieldname]);
53                                         break;
54                                 default:
55                                         $value = $_POST[$fieldname] ?? '';
56                         }
57                         $storage_opts_data[$name] = $value;
58                 }
59                 unset($name);
60                 unset($info);
61
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);
66                         }
67                         DI::baseUrl()->redirect('admin/storage');
68                 }
69
70                 if (!empty($_POST['submit_save_set'])) {
71                         if (empty($storagebackend) || !DI::storageManager()->setBackend($storagebackend)) {
72                                 notice(DI::l10n()->t('Invalid storage backend setting value.'));
73                         }
74                 }
75
76                 DI::baseUrl()->redirect('admin/storage');
77         }
78
79         public static function content(array $parameters = [])
80         {
81                 parent::content($parameters);
82
83                 $current_storage_backend = DI::storage();
84                 $available_storage_forms = [];
85
86                 foreach (DI::storageManager()->listBackends() as $name => $class) {
87
88                         // build storage config form,
89                         $storage_form_prefix = preg_replace('|[^a-zA-Z0-9]|', '', $name);
90
91                         $storage_form = [];
92                         foreach (DI::storageManager()->getByName($name)->getOptions() as $option => $info) {
93                                 $type = $info[0];
94                                 // Backward compatibilty with yesno field description
95                                 if ($type == 'yesno') {
96                                         $type = 'checkbox';
97                                         // Remove translated labels Yes No from field info
98                                         unset($info[4]);
99                                 }
100
101                                 $info[0]               = $storage_form_prefix . '_' . $option;
102                                 $info['type']          = $type;
103                                 $info['field']         = 'field_' . $type . '.tpl';
104                                 $storage_form[$option] = $info;
105                         }
106
107                         $available_storage_forms[] = [
108                                 'name'   => $name,
109                                 'prefix' => $storage_form_prefix,
110                                 'form'   => $storage_form,
111                                 'active' => $current_storage_backend instanceof IStorage && $name === $current_storage_backend::getName(),
112                         ];
113                 }
114
115                 $t = Renderer::getMarkupTemplate('admin/storage.tpl');
116
117                 return Renderer::replaceMacros($t, [
118                         '$title'                 => DI::l10n()->t('Administration'),
119                         '$page'                  => DI::l10n()->t('Storage'),
120                         '$save'                  => DI::l10n()->t('Save'),
121                         '$save_activate'         => DI::l10n()->t('Save & Activate'),
122                         '$activate'              => DI::l10n()->t('Activate'),
123                         '$save_reload'           => DI::l10n()->t('Save & Reload'),
124                         '$noconfig'              => DI::l10n()->t('This backend doesn\'t have custom settings'),
125                         '$baseurl'               => DI::baseUrl()->get(true),
126                         '$form_security_token'   => self::getFormSecurityToken("admin_storage"),
127                         '$storagebackend'        => $current_storage_backend instanceof IStorage ? $current_storage_backend::getName() : DI::l10n()->t('Database (legacy)'),
128                         '$availablestorageforms' => $available_storage_forms,
129                 ]);
130         }
131 }