]> git.mxchange.org Git - friendica.git/blob - src/Core/Theme.php
Fix worker priorities
[friendica.git] / src / Core / Theme.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, 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\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                 DI::profiler()->startRecording('file');
92                 $theme_file = file_get_contents("view/theme/$theme/theme.php");
93                 DI::profiler()->stopRecording();
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 (strpos($comment_line, ':') !== false) {
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                         Hook::delete(['file' => "view/theme/$theme/theme.php"]);
163                 }
164
165                 $allowed_themes = Theme::getAllowedList();
166                 $key = array_search($theme, $allowed_themes);
167                 if ($key !== false) {
168                         unset($allowed_themes[$key]);
169                         Theme::setAllowedList($allowed_themes);
170                 }
171         }
172
173         public static function install($theme)
174         {
175                 $theme = Strings::sanitizeFilePathItem($theme);
176
177                 // silently fail if theme was removed or if $theme is funky
178                 if (!file_exists("view/theme/$theme/theme.php")) {
179                         return false;
180                 }
181
182                 try {
183                         include_once "view/theme/$theme/theme.php";
184
185                         $func = "{$theme}_install";
186                         if (function_exists($func)) {
187                                 $func();
188                         }
189
190                         $allowed_themes = Theme::getAllowedList();
191                         $allowed_themes[] = $theme;
192                         Theme::setAllowedList($allowed_themes);
193
194                         return true;
195                 } catch (\Exception $e) {
196                         Logger::error('Theme installation failed', ['theme' => $theme, 'error' => $e->getMessage()]);
197                         return false;
198                 }
199         }
200
201         /**
202          * Get the full path to relevant theme files by filename
203          *
204          * This function searches in order in the current theme directory, in the current theme parent directory, and lastly
205          * in the base view/ folder.
206          *
207          * @param string $file Filename
208          * @return string Path to the file or empty string if the file isn't found
209          * @throws \Exception
210          */
211         public static function getPathForFile($file)
212         {
213                 $a = DI::app();
214
215                 $theme = $a->getCurrentTheme();
216
217                 $parent = Strings::sanitizeFilePathItem($a->getThemeInfoValue('extends', $theme));
218
219                 $paths = [
220                         "view/theme/$theme/$file",
221                         "view/theme/$parent/$file",
222                         "view/$file",
223                 ];
224
225                 foreach ($paths as $path) {
226                         if (file_exists($path)) {
227                                 return $path;
228                         }
229                 }
230
231                 return '';
232         }
233
234         /**
235          * Return relative path to theme stylesheet file
236          *
237          * Provide a sane default if nothing is chosen or the specified theme does not exist.
238          *
239          * @param string $theme Theme name
240          *
241          * @return string
242          */
243         public static function getStylesheetPath($theme)
244         {
245                 $theme = Strings::sanitizeFilePathItem($theme);
246
247                 if (!file_exists('view/theme/' . $theme . '/style.php')) {
248                         return 'view/theme/' . $theme . '/style.css';
249                 }
250
251                 $a = DI::app();
252
253                 $query_params = [];
254
255                 $puid = Profile::getThemeUid($a);
256                 if ($puid) {
257                         $query_params['puid'] = $puid;
258                 }
259
260                 return 'view/theme/' . $theme . '/style.pcss' . (!empty($query_params) ? '?' . http_build_query($query_params) : '');
261         }
262
263         /**
264          * Returns the path of the provided theme
265          *
266          * @param $theme
267          * @return string|null
268          */
269         public static function getConfigFile($theme)
270         {
271                 $theme = Strings::sanitizeFilePathItem($theme);
272
273                 $a = DI::app();
274                 $base_theme = $a->getThemeInfoValue('extends') ?? '';
275
276                 if (file_exists("view/theme/$theme/config.php")) {
277                         return "view/theme/$theme/config.php";
278                 }
279                 if ($base_theme && file_exists("view/theme/$base_theme/config.php")) {
280                         return "view/theme/$base_theme/config.php";
281                 }
282                 return null;
283         }
284         
285         /**
286          * Returns the background color of the provided theme if available.
287          *
288          * @param string   $theme
289          * @param int|null $uid   Current logged-in user id
290          * @return string|null
291          */
292         public static function getBackgroundColor(string $theme, $uid = null)
293         {
294                 $theme = Strings::sanitizeFilePathItem($theme);
295
296                 $return = null;
297
298                 // silently fail if theme was removed or if $theme is funky
299                 if (file_exists("view/theme/$theme/theme.php")) {
300                         include_once "view/theme/$theme/theme.php";
301
302                         $func = "{$theme}_get_background_color";
303                         if (function_exists($func)) {
304                                 $return = $func($uid);
305                         }
306                 }
307
308                 return $return;
309         }
310
311         /**
312          * Returns the theme color of the provided theme if available.
313          *
314          * @param string   $theme
315          * @param int|null $uid   Current logged-in user id
316          * @return string|null
317          */
318         public static function getThemeColor(string $theme, int $uid = null)
319         {
320                 $theme = Strings::sanitizeFilePathItem($theme);
321
322                 $return = null;
323
324                 // silently fail if theme was removed or if $theme is funky
325                 if (file_exists("view/theme/$theme/theme.php")) {
326                         include_once "view/theme/$theme/theme.php";
327
328                         $func = "{$theme}_get_theme_color";
329                         if (function_exists($func)) {
330                                 $return = $func($uid);
331                         }
332                 }
333
334                 return $return;
335         }
336 }