]> git.mxchange.org Git - friendica.git/blob - src/Module/Admin/Themes/Index.php
Merge branch '2021.03-rc' into copyright-2021
[friendica.git] / src / Module / Admin / Themes / Index.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\Themes;
23
24 use Friendica\Core\Renderer;
25 use Friendica\Core\Theme;
26 use Friendica\DI;
27 use Friendica\Module\BaseAdmin;
28 use Friendica\Util\Strings;
29
30 class Index extends BaseAdmin
31 {
32         public static function content(array $parameters = [])
33         {
34                 parent::content($parameters);
35
36                 $allowed_themes = Theme::getAllowedList();
37
38                 // reload active themes
39                 if (!empty($_GET['action'])) {
40                         self::checkFormSecurityTokenRedirectOnError(DI::baseUrl()->get() . '/admin/themes', 'admin_themes', 't');
41
42                         switch ($_GET['action']) {
43                                 case 'reload':
44                                         $allowed_themes = array_unique($allowed_themes);
45                                         foreach ($allowed_themes as $theme) {
46                                                 Theme::uninstall($theme);
47                                                 Theme::install($theme);
48                                         }
49                                         Theme::setAllowedList($allowed_themes);
50
51                                         info(DI::l10n()->t('Themes reloaded'));
52                                         break;
53
54                                 case 'toggle' :
55                                         $theme = $_GET['addon'] ?? '';
56                                         if ($theme) {
57                                                 $theme = Strings::sanitizeFilePathItem($theme);
58                                                 if (!is_dir("view/theme/$theme")) {
59                                                         notice(DI::l10n()->t('Item not found.'));
60                                                         return '';
61                                                 }
62
63                                                 if (in_array($theme, Theme::getAllowedList())) {
64                                                         Theme::uninstall($theme);
65                                                         info(DI::l10n()->t('Theme %s disabled.', $theme));
66                                                 } elseif (Theme::install($theme)) {
67                                                         info(DI::l10n()->t('Theme %s successfully enabled.', $theme));
68                                                 } else {
69                                                         notice(DI::l10n()->t('Theme %s failed to install.', $theme));
70                                                 }
71                                         }
72
73                                         break;
74
75                         }
76
77                         DI::baseUrl()->redirect('admin/themes');
78                 }
79
80                 $themes = [];
81                 $files = glob('view/theme/*');
82                 if (is_array($files)) {
83                         foreach ($files as $file) {
84                                 $theme = basename($file);
85
86                                 // Is there a style file?
87                                 $theme_files = glob('view/theme/' . $theme . '/style.*');
88
89                                 // If not then quit
90                                 if (count($theme_files) == 0) {
91                                         continue;
92                                 }
93
94                                 $is_experimental = intval(file_exists($file . '/experimental'));
95                                 $is_supported = 1 - (intval(file_exists($file . '/unsupported')));
96                                 $is_allowed = intval(in_array($theme, $allowed_themes));
97
98                                 if ($is_allowed || $is_supported || DI::config()->get('system', 'show_unsupported_themes')) {
99                                         $themes[] = ['name' => $theme, 'experimental' => $is_experimental, 'supported' => $is_supported, 'allowed' => $is_allowed];
100                                 }
101                         }
102                 }
103
104                 $addons = [];
105                 foreach ($themes as $theme) {
106                         $addons[] = [$theme['name'], (($theme['allowed']) ? 'on' : 'off'), Theme::getInfo($theme['name'])];
107                 }
108
109                 $t = Renderer::getMarkupTemplate('admin/addons/index.tpl');
110                 return Renderer::replaceMacros($t, [
111                         '$title'               => DI::l10n()->t('Administration'),
112                         '$page'                => DI::l10n()->t('Themes'),
113                         '$submit'              => DI::l10n()->t('Save Settings'),
114                         '$reload'              => DI::l10n()->t('Reload active themes'),
115                         '$baseurl'             => DI::baseUrl()->get(true),
116                         '$function'            => 'themes',
117                         '$addons'              => $addons,
118                         '$pcount'              => count($themes),
119                         '$noplugshint'         => DI::l10n()->t('No themes found on the system. They should be placed in %1$s', '<code>/view/themes</code>'),
120                         '$experimental'        => DI::l10n()->t('[Experimental]'),
121                         '$unsupported'         => DI::l10n()->t('[Unsupported]'),
122                         '$form_security_token' => self::getFormSecurityToken('admin_themes'),
123                 ]);
124         }
125 }