]> git.mxchange.org Git - friendica.git/blob - src/Module/Admin/Storage.php
19379beeba082334f015706cc77e58fbd0fd0704
[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($_POST['storagebackend'] ?? ''));
39                 /** @var IStorage $newstorage */
40                 $newstorage = DI::storageManager()->getByName($storagebackend);
41
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
49                                 case 'checkbox':
50                                 case 'yesno':
51                                         $value = !empty($_POST[$fieldname]);
52                                         break;
53                                 default:
54                                         $value = $_POST[$fieldname] ?? '';
55                         }
56                         $storage_opts_data[$name] = $value;
57                 }
58                 unset($name);
59                 unset($info);
60
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);
65                         }
66                         DI::baseUrl()->redirect('admin/storage');
67                 }
68
69                 if (empty($storagebackend) || !DI::storageManager()->setBackend($storagebackend)) {
70                         notice(DI::l10n()->t('Invalid storage backend setting value.'));
71                 }
72
73                 DI::baseUrl()->redirect('admin/storage');
74         }
75
76         public static function content(array $parameters = [])
77         {
78                 parent::content($parameters);
79
80                 $current_storage_backend    = DI::storage();
81                 $available_storage_backends = [];
82                 $available_storage_forms    = [];
83
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)');
88                 }
89
90                 foreach (DI::storageManager()->listBackends() as $name => $class) {
91                         $available_storage_backends[$name] = $name;
92
93                         // build storage config form,
94                         $storage_form_prefix = preg_replace('|[^a-zA-Z0-9]|', '', $name);
95
96                         $storage_form = [];
97                         foreach (DI::storageManager()->getByName($name)->getOptions() as $option => $info) {
98                                 $type = $info[0];
99                                 // Backward compatibilty with yesno field description
100                                 if ($type == 'yesno') {
101                                         $type = 'checkbox';
102                                         // Remove translated labels Yes No from field info
103                                         unset($info[4]);
104                                 }
105
106                                 $info[0]               = $storage_form_prefix . '_' . $option;
107                                 $info['type']          = $type;
108                                 $info['field']         = 'field_' . $type . '.tpl';
109                                 $storage_form[$option] = $info;
110                         }
111
112                         if (count($storage_form) > 0) {
113                                 $available_storage_forms[] = [
114                                         'name' => $name,
115                                         'prefix' => $storage_form_prefix,
116                                         'form' => $storage_form,
117                                 ];
118                         }
119                 }
120
121                 $t = Renderer::getMarkupTemplate('admin/storage.tpl');
122
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,
132                 ]);
133         }
134 }