]> git.mxchange.org Git - friendica.git/blob - src/Module/Admin/Storage.php
1e1678b138b6bd62b0535a31ebfee786e6524be3
[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\Module\BaseAdmin;
27 use Friendica\Util\Strings;
28 use Psr\Log\LogLevel;
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
40                 // save storage backend form
41                 if (DI::storageManager()->setBackend($storagebackend)) {
42                         $storage_opts     = DI::storage()->getOptions();
43                         $storage_form_prefix = preg_replace('|[^a-zA-Z0-9]|', '', $storagebackend);
44                         $storage_opts_data   = [];
45                         foreach ($storage_opts as $name => $info) {
46                                 $fieldname = $storage_form_prefix . '_' . $name;
47                                 switch ($info[0]) { // type
48                                         case 'checkbox':
49                                         case 'yesno':
50                                                 $value = !empty($_POST[$fieldname]);
51                                                 break;
52                                         default:
53                                                 $value = $_POST[$fieldname] ?? '';
54                                 }
55                                 $storage_opts_data[$name] = $value;
56                         }
57                         unset($name);
58                         unset($info);
59
60                         $storage_form_errors = DI::storage()->saveOptions($storage_opts_data);
61                         if (count($storage_form_errors)) {
62                                 foreach ($storage_form_errors as $name => $err) {
63                                         notice('Storage backend, ' . $storage_opts[$name][1] . ': ' . $err);
64                                 }
65                                 DI::baseUrl()->redirect('admin/storage');
66                         }
67                 } elseif (!empty($storagebackend)) {
68                         notice(DI::l10n()->t('Invalid storage backend setting value.'));
69                 }
70
71                 DI::baseUrl()->redirect('admin/storage');
72         }
73
74         public static function content(array $parameters = [])
75         {
76                 parent::content($parameters);
77
78                 $current_storage_backend = DI::storage();
79                 $available_storage_backends = [];
80
81                 // show legacy option only if it is the current backend:
82                 // once changed can't be selected anymore
83                 if ($current_storage_backend == null) {
84                         $available_storage_backends[''] = DI::l10n()->t('Database (legacy)');
85                 }
86
87                 foreach (DI::storageManager()->listBackends() as $name => $class) {
88                         $available_storage_backends[$name] = $name;
89                 }
90
91                 // build storage config form,
92                 $storage_form_prefix = preg_replace('|[^a-zA-Z0-9]|' ,'', $current_storage_backend);
93
94                 $storage_form = [];
95                 if (!is_null($current_storage_backend) && $current_storage_backend != '') {
96                         foreach ($current_storage_backend->getOptions() as $name => $info) {
97                                 $type = $info[0];
98                                 // Backward compatibilty with yesno field description
99                                 if ($type == 'yesno') {
100                                         $type = 'checkbox';
101                                         // Remove translated labels Yes No from field info
102                                         unset($info[4]);
103                                 }
104
105                                 $info[0] = $storage_form_prefix . '_' . $name;
106                                 $info['type'] = $type;
107                                 $info['field'] = 'field_' . $type . '.tpl';
108                                 $storage_form[$name] = $info;
109                         }
110                 }
111
112                 $t = Renderer::getMarkupTemplate('admin/storage.tpl');
113
114                 return Renderer::replaceMacros($t, [
115                         '$title' => DI::l10n()->t('Administration'),
116                         '$page' => DI::l10n()->t('Storage'),
117                         '$submit' => DI::l10n()->t('Save Settings'),
118                         '$clear' => DI::l10n()->t('Clear'),
119                         '$baseurl' => DI::baseUrl()->get(true),
120                         '$form_security_token' => self::getFormSecurityToken("admin_storage"),
121                         '$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],
122                         '$storageform'      => $storage_form,
123                 ]);
124         }
125 }