]> git.mxchange.org Git - friendica.git/blob - src/Core/Theme.php
Merge pull request #8261 from MrPetovan/task/8251-use-about-for-pdesc
[friendica.git] / src / Core / Theme.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
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\Core;
23
24 use Friendica\DI;
25 use Friendica\Model\Profile;
26 use Friendica\Util\Strings;
27
28 require_once 'boot.php';
29
30 /**
31  * Some functions to handle themes
32  */
33 class Theme
34 {
35         public static function getAllowedList()
36         {
37                 $allowed_themes_str = DI::config()->get('system', 'allowed_themes');
38                 $allowed_themes_raw = explode(',', str_replace(' ', '', $allowed_themes_str));
39                 $allowed_themes = [];
40                 if (count($allowed_themes_raw)) {
41                         foreach ($allowed_themes_raw as $theme) {
42                                 $theme = Strings::sanitizeFilePathItem(trim($theme));
43                                 if (strlen($theme) && is_dir("view/theme/$theme")) {
44                                         $allowed_themes[] = $theme;
45                                 }
46                         }
47                 }
48
49                 return array_unique($allowed_themes);
50         }
51
52         public static function setAllowedList(array $allowed_themes)
53         {
54                 DI::config()->set('system', 'allowed_themes', implode(',', array_unique($allowed_themes)));
55         }
56
57         /**
58          * Parse theme comment in search of theme infos.
59          *
60          * like
61          * \code
62          * ..* Name: My Theme
63          *   * Description: My Cool Theme
64          * . * Version: 1.2.3
65          *   * Author: John <profile url>
66          *   * Maintainer: Jane <profile url>
67          *   *
68          * \endcode
69          * @param string $theme the name of the theme
70          * @return array
71          */
72         public static function getInfo($theme)
73         {
74                 $theme = Strings::sanitizeFilePathItem($theme);
75
76                 $info = [
77                         'name' => $theme,
78                         'description' => "",
79                         'author' => [],
80                         'maintainer' => [],
81                         'version' => "",
82                         'credits' => "",
83                         'experimental' => file_exists("view/theme/$theme/experimental"),
84                         'unsupported' => file_exists("view/theme/$theme/unsupported")
85                 ];
86
87                 if (!is_file("view/theme/$theme/theme.php")) {
88                         return $info;
89                 }
90
91                 $stamp1 = microtime(true);
92                 $theme_file = file_get_contents("view/theme/$theme/theme.php");
93                 DI::profiler()->saveTimestamp($stamp1, "file", System::callstack());
94
95                 $result = preg_match("|/\*.*\*/|msU", $theme_file, $matches);
96
97                 if ($result) {
98                         $comment_lines = explode("\n", $matches[0]);
99                         foreach ($comment_lines as $comment_line) {
100                                 $comment_line = trim($comment_line, "\t\n\r */");
101                                 if ($comment_line != "") {
102                                         list($key, $value) = array_map("trim", explode(":", $comment_line, 2));
103                                         $key = strtolower($key);
104                                         if ($key == "author") {
105                                                 $result = preg_match("|([^<]+)<([^>]+)>|", $value, $matches);
106                                                 if ($result) {
107                                                         $info['author'][] = ['name' => $matches[1], 'link' => $matches[2]];
108                                                 } else {
109                                                         $info['author'][] = ['name' => $value];
110                                                 }
111                                         } elseif ($key == "maintainer") {
112                                                 $result = preg_match("|([^<]+)<([^>]+)>|", $value, $matches);
113                                                 if ($result) {
114                                                         $info['maintainer'][] = ['name' => $matches[1], 'link' => $matches[2]];
115                                                 } else {
116                                                         $info['maintainer'][] = ['name' => $value];
117                                                 }
118                                         } elseif (array_key_exists($key, $info)) {
119                                                 $info[$key] = $value;
120                                         }
121                                 }
122                         }
123                 }
124                 return $info;
125         }
126
127         /**
128          * Returns the theme's screenshot.
129          *
130          * The screenshot is expected as view/theme/$theme/screenshot.[png|jpg].
131          *
132          * @param string $theme The name of the theme
133          * @return string
134          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
135          */
136         public static function getScreenshot($theme)
137         {
138                 $theme = Strings::sanitizeFilePathItem($theme);
139
140                 $exts = ['.png', '.jpg'];
141                 foreach ($exts as $ext) {
142                         if (file_exists('view/theme/' . $theme . '/screenshot' . $ext)) {
143                                 return DI::baseUrl() . '/view/theme/' . $theme . '/screenshot' . $ext;
144                         }
145                 }
146                 return DI::baseUrl() . '/images/blank.png';
147         }
148
149         public static function uninstall($theme)
150         {
151                 $theme = Strings::sanitizeFilePathItem($theme);
152
153                 // silently fail if theme was removed or if $theme is funky
154                 if (file_exists("view/theme/$theme/theme.php")) {
155                         include_once "view/theme/$theme/theme.php";
156
157                         $func = "{$theme}_uninstall";
158                         if (function_exists($func)) {
159                                 $func();
160                         }
161                 }
162
163                 $allowed_themes = Theme::getAllowedList();
164                 $key = array_search($theme, $allowed_themes);
165                 if ($key !== false) {
166                         unset($allowed_themes[$key]);
167                         Theme::setAllowedList($allowed_themes);
168                 }
169         }
170
171         public static function install($theme)
172         {
173                 $theme = Strings::sanitizeFilePathItem($theme);
174
175                 // silently fail if theme was removed or if $theme is funky
176                 if (!file_exists("view/theme/$theme/theme.php")) {
177                         return false;
178                 }
179
180                 try {
181                         include_once "view/theme/$theme/theme.php";
182
183                         $func = "{$theme}_install";
184                         if (function_exists($func)) {
185                                 $func();
186                         }
187
188                         $allowed_themes = Theme::getAllowedList();
189                         $allowed_themes[] = $theme;
190                         Theme::setAllowedList($allowed_themes);
191
192                         return true;
193                 } catch (\Exception $e) {
194                         Logger::error('Theme installation failed', ['theme' => $theme, 'error' => $e->getMessage()]);
195                         return false;
196                 }
197         }
198
199         /**
200          * Get the full path to relevant theme files by filename
201          *
202          * This function searches in order in the current theme directory, in the current theme parent directory, and lastly
203          * in the base view/ folder.
204          *
205          * @param string $file Filename
206          * @return string Path to the file or empty string if the file isn't found
207          * @throws \Exception
208          */
209         public static function getPathForFile($file)
210         {
211                 $a = DI::app();
212
213                 $theme = $a->getCurrentTheme();
214
215                 $parent = Strings::sanitizeFilePathItem($a->theme_info['extends'] ?? $theme);
216
217                 $paths = [
218                         "view/theme/$theme/$file",
219                         "view/theme/$parent/$file",
220                         "view/$file",
221                 ];
222
223                 foreach ($paths as $path) {
224                         if (file_exists($path)) {
225                                 return $path;
226                         }
227                 }
228
229                 return '';
230         }
231
232         /**
233          * Return relative path to theme stylesheet file
234          *
235          * Provide a sane default if nothing is chosen or the specified theme does not exist.
236          *
237          * @param string $theme Theme name
238          *
239          * @return string
240          */
241         public static function getStylesheetPath($theme)
242         {
243                 $theme = Strings::sanitizeFilePathItem($theme);
244
245                 if (!file_exists('view/theme/' . $theme . '/style.php')) {
246                         return 'view/theme/' . $theme . '/style.css';
247                 }
248
249                 $a = DI::app();
250
251                 $query_params = [];
252
253                 $puid = Profile::getThemeUid($a);
254                 if ($puid) {
255                         $query_params['puid'] = $puid;
256                 }
257
258                 return 'view/theme/' . $theme . '/style.pcss' . (!empty($query_params) ? '?' . http_build_query($query_params) : '');
259         }
260 }