]> git.mxchange.org Git - friendica.git/blob - src/Module/Admin/Themes/Embed.php
Merge pull request #13110 from anubis2814/develop
[friendica.git] / src / Module / Admin / Themes / Embed.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, 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\App;
25 use Friendica\Core\L10n;
26 use Friendica\Core\Renderer;
27 use Friendica\DI;
28 use Friendica\Module\BaseAdmin;
29 use Friendica\Module\Response;
30 use Friendica\Util\Profiler;
31 use Friendica\Util\Strings;
32 use Psr\Log\LoggerInterface;
33
34 class Embed extends BaseAdmin
35 {
36         /** @var App */
37         protected $app;
38         /** @var App\Mode */
39         protected $mode;
40
41         public function __construct(App $app, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, App\Mode $mode, array $server, array $parameters = [])
42         {
43                 parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
44
45                 $this->app  = $app;
46                 $this->mode = $mode;
47
48                 $theme = Strings::sanitizeFilePathItem($this->parameters['theme']);
49                 if (is_file("view/theme/$theme/config.php")) {
50                         $this->app->setCurrentTheme($theme);
51                 }
52         }
53
54         protected function post(array $request = [])
55         {
56                 self::checkAdminAccess();
57
58                 $theme = Strings::sanitizeFilePathItem($this->parameters['theme']);
59                 if (is_file("view/theme/$theme/config.php")) {
60                         require_once "view/theme/$theme/config.php";
61                         if (function_exists('theme_admin_post')) {
62                                 self::checkFormSecurityTokenRedirectOnError('/admin/themes/' . $theme . '/embed?mode=minimal', 'admin_theme_settings');
63                                 theme_admin_post($this->app);
64                         }
65                 }
66
67                 if ($this->mode->isAjax()) {
68                         return;
69                 }
70
71                 $this->baseUrl->redirect('admin/themes/' . $theme . '/embed?mode=minimal');
72         }
73
74         protected function content(array $request = []): string
75         {
76                 parent::content();
77
78                 $theme = Strings::sanitizeFilePathItem($this->parameters['theme']);
79                 if (!is_dir("view/theme/$theme")) {
80                         DI::sysmsg()->addNotice($this->t('Unknown theme.'));
81                         return '';
82                 }
83
84                 $admin_form = '';
85                 if (is_file("view/theme/$theme/config.php")) {
86                         require_once "view/theme/$theme/config.php";
87
88                         if (function_exists('theme_admin')) {
89                                 $admin_form = theme_admin($this->app);
90                         }
91                 }
92
93                 // Overrides normal theme style include to strip user param to show embedded theme settings
94                 Renderer::$theme['stylesheet'] = 'view/theme/' . $theme . '/style.pcss';
95
96                 $t = Renderer::getMarkupTemplate('admin/addons/embed.tpl');
97                 return Renderer::replaceMacros($t, [
98                         '$action' => 'admin/themes/' . $theme . '/embed?mode=minimal',
99                         '$form' => $admin_form,
100                         '$form_security_token' => self::getFormSecurityToken("admin_theme_settings"),
101                 ]);
102         }
103 }