]> git.mxchange.org Git - friendica.git/blob - src/Module/Admin/Storage.php
Some removed escapeTags calls
[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\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;
31
32 class Storage extends BaseAdmin
33 {
34         public static function post(array $parameters = [])
35         {
36                 self::checkAdminAccess();
37
38                 self::checkFormSecurityTokenRedirectOnError('/admin/storage', 'admin_storage');
39
40                 $storagebackend = trim($parameters['name'] ?? '');
41
42                 try {
43                         /** @var ICanConfigureStorage|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');
48                 }
49
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
58                                         case 'checkbox':
59                                         case 'yesno':
60                                                 $value = !empty($_POST[$fieldname]);
61                                                 break;
62                                         default:
63                                                 $value = $_POST[$fieldname] ?? '';
64                                 }
65                                 $storage_opts_data[$name] = $value;
66                         }
67                         unset($name);
68                         unset($info);
69
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));
74                                 }
75                                 DI::baseUrl()->redirect('admin/storage');
76                         }
77                 }
78
79                 if (!empty($_POST['submit_save_set'])) {
80                         try {
81                                 $newstorage = DI::storageManager()->getWritableStorageByName($storagebackend);
82
83                                 if (!DI::storageManager()->setBackend($newstorage)) {
84                                         notice(DI::l10n()->t('Invalid storage backend setting value.'));
85                                 }
86                         } catch (InvalidClassStorageException $storageException) {
87                                 notice(DI::l10n()->t('Invalid storage backend setting value.'));
88                         }
89                 }
90
91                 DI::baseUrl()->redirect('admin/storage');
92         }
93
94         public static function content(array $parameters = [])
95         {
96                 parent::content($parameters);
97
98                 $current_storage_backend = DI::storage();
99                 $available_storage_forms = [];
100
101                 foreach (DI::storageManager()->listBackends() as $name) {
102
103                         // build storage config form,
104                         $storage_form_prefix = preg_replace('|[^a-zA-Z0-9]|', '', $name);
105
106                         $storage_form  = [];
107                         $storageConfig = DI::storageManager()->getConfigurationByName($name);
108
109                         if ($storageConfig !== false) {
110                                 foreach ($storageConfig->getOptions() as $option => $info) {
111
112                                         $type = $info[0];
113                                         // Backward compatibilty with yesno field description
114                                         if ($type == 'yesno') {
115                                                 $type = 'checkbox';
116                                                 // Remove translated labels Yes No from field info
117                                                 unset($info[4]);
118                                         }
119
120                                         $info[0]               = $storage_form_prefix . '_' . $option;
121                                         $info['type']          = $type;
122                                         $info['field']         = 'field_' . $type . '.tpl';
123                                         $storage_form[$option] = $info;
124                                 }
125                         }
126
127                         $available_storage_forms[] = [
128                                 'name'   => $name,
129                                 'prefix' => $storage_form_prefix,
130                                 'form'   => $storage_form,
131                                 'active' => $current_storage_backend instanceof ICanWriteToStorage && $name === $current_storage_backend::getName(),
132                         ];
133                 }
134
135                 $t = Renderer::getMarkupTemplate('admin/storage.tpl');
136
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                         '$baseurl'               => DI::baseUrl()->get(true),
148                         '$form_security_token'   => self::getFormSecurityToken("admin_storage"),
149                         '$storagebackend'        => $current_storage_backend instanceof ICanWriteToStorage ? $current_storage_backend::getName() : DI::l10n()->t('Database (legacy)'),
150                         '$availablestorageforms' => $available_storage_forms,
151                 ]);
152         }
153 }