Update copyright
[friendica.git/.git] / src / Module / Admin / Themes / Details.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\Content\Text\Markdown;
25 use Friendica\Core\Renderer;
26 use Friendica\Core\Theme;
27 use Friendica\DI;
28 use Friendica\Module\BaseAdmin;
29 use Friendica\Util\Strings;
30
31 class Details extends BaseAdmin
32 {
33         public static function content(array $parameters = [])
34         {
35                 parent::content($parameters);
36
37                 $theme = Strings::sanitizeFilePathItem($parameters['theme']);
38                 if (!is_dir("view/theme/$theme")) {
39                         notice(DI::l10n()->t("Item not found."));
40                         return '';
41                 }
42
43                 $isEnabled = in_array($theme, Theme::getAllowedList());
44                 if ($isEnabled) {
45                         $status = "on";
46                         $action = DI::l10n()->t("Disable");
47                 } else {
48                         $status = "off";
49                         $action = DI::l10n()->t("Enable");
50                 }
51
52                 if (!empty($_GET['action']) && $_GET['action'] == 'toggle') {
53                         self::checkFormSecurityTokenRedirectOnError('/admin/themes', 'admin_themes', 't');
54
55                         if ($isEnabled) {
56                                 Theme::uninstall($theme);
57                                 info(DI::l10n()->t('Theme %s disabled.', $theme));
58                         } elseif (Theme::install($theme)) {
59                                 info(DI::l10n()->t('Theme %s successfully enabled.', $theme));
60                         } else {
61                                 notice(DI::l10n()->t('Theme %s failed to install.', $theme));
62                         }
63
64                         DI::baseUrl()->redirect('admin/themes/' . $theme);
65                 }
66
67                 $readme = null;
68                 if (is_file("view/theme/$theme/README.md")) {
69                         $readme = Markdown::convert(file_get_contents("view/theme/$theme/README.md"), false);
70                 } elseif (is_file("view/theme/$theme/README")) {
71                         $readme = "<pre>" . file_get_contents("view/theme/$theme/README") . "</pre>";
72                 }
73
74                 $admin_form = '';
75                 if (is_file("view/theme/$theme/config.php")) {
76                         require_once "view/theme/$theme/config.php";
77
78                         if (function_exists('theme_admin')) {
79                                 $admin_form = '<iframe onload="resizeIframe(this);" src="/admin/themes/' . $theme . '/embed?mode=minimal" width="100%" height="600px" frameborder="no"></iframe>';
80                         }
81                 }
82
83                 $screenshot = [Theme::getScreenshot($theme), DI::l10n()->t('Screenshot')];
84                 if (!stristr($screenshot[0], $theme)) {
85                         $screenshot = null;
86                 }
87
88                 $t = Renderer::getMarkupTemplate('admin/addons/details.tpl');
89                 return Renderer::replaceMacros($t, [
90                         '$title' => DI::l10n()->t('Administration'),
91                         '$page' => DI::l10n()->t('Themes'),
92                         '$toggle' => DI::l10n()->t('Toggle'),
93                         '$settings' => DI::l10n()->t('Settings'),
94                         '$baseurl' => DI::baseUrl()->get(true),
95                         '$addon' => $theme,
96                         '$status' => $status,
97                         '$action' => $action,
98                         '$info' => Theme::getInfo($theme),
99                         '$function' => 'themes',
100                         '$admin_form' => $admin_form,
101                         '$str_author' => DI::l10n()->t('Author: '),
102                         '$str_maintainer' => DI::l10n()->t('Maintainer: '),
103                         '$screenshot' => $screenshot,
104                         '$readme' => $readme,
105
106                         '$form_security_token' => self::getFormSecurityToken("admin_themes"),
107                 ]);
108         }
109 }